1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     OriginalSoundHeapApp.cpp
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: $
16  *---------------------------------------------------------------------------*/
17 
18 #include "precompiled.h"
19 
20 #include "OriginalSoundHeapApp.h"
21 #include "simple.csid"
22 
23 namespace
24 {
25 
26 const s32 SOUND_THREAD_PRIORITY = 4;
27 const s32 LOAD_THREAD_PRIORITY = 3;
28 const s32 SOUND_HEAP_SIZE = 1 * 1024 * 1024;
29 const char SOUND_ARC_PATH[] = NW_SND_DEMO_PATH_PREFIX "simple.bcsar";
30 const char DEMO_TITLE[] = "OriginalSoundHeap";
31 
32 }
33 
OnInitialize()34 void OriginalSoundHeapApp::OnInitialize()
35 {
36     InitializeSoundSystem();
37 
38     // オリジナルヒープにデータをロードする
39     if ( ! m_DataManager.LoadData( SEQ_MARIOKART, &m_Heap ) )
40     {
41         NW_ASSERTMSG( false, "LoadData(SEQ_MARIOKART) failed.");
42     }
43 }
44 
InitializeSoundSystem()45 void OriginalSoundHeapApp::InitializeSoundSystem()
46 {
47     // サウンドシステムの初期化
48     {
49         nw::snd::SoundSystem::SoundSystemParam param;
50         size_t workMemSize = nw::snd::SoundSystem::GetRequiredMemSize( param );
51         m_pMemoryForSoundSystem = MemAlloc( workMemSize );
52 
53         nw::snd::SoundSystem::Initialize(
54                 param,
55                 reinterpret_cast<uptr>( m_pMemoryForSoundSystem ),
56                 workMemSize );
57     }
58 
59     // サウンドアーカイブ
60     if ( ! m_Archive.Open( SOUND_ARC_PATH ) )
61     {
62         NW_ASSERTMSG( 0, "cannot open bxsar(%s)\n", SOUND_ARC_PATH );
63     }
64 
65     // INFO ブロックのロード
66     {
67         u32 infoBlockSize = m_Archive.GetHeaderSize();
68         m_pMemoryForInfoBlock = MemAlloc( infoBlockSize );
69         if ( ! m_Archive.LoadHeader( m_pMemoryForInfoBlock, infoBlockSize ) )
70         {
71             NW_ASSERTMSG( 0, "cannot load infoBlock(%s)", SOUND_ARC_PATH );
72         }
73     }
74 
75     // STRING ブロックのロード
76     {
77         u32 stringBlockSize = m_Archive.GetLabelStringDataSize();
78         m_pMemoryForStringBlock = MemAlloc( stringBlockSize );
79         if ( ! m_Archive.LoadLabelStringData( m_pMemoryForStringBlock, stringBlockSize ) )
80         {
81             NW_ASSERTMSG( 0, "cannot load stringBlock(%s)", SOUND_ARC_PATH );
82         }
83     }
84 
85     // サウンドデータマネージャーの初期化
86     {
87         u32 setupSize = m_DataManager.GetRequiredMemSize( &m_Archive );
88         m_pMemoryForSoundDataManager = MemAlloc( setupSize );
89         m_DataManager.Initialize( &m_Archive, m_pMemoryForSoundDataManager, setupSize );
90     }
91 
92     // サウンドアーカイブプレイヤーの初期化
93     {
94         u32 setupSize = m_ArchivePlayer.GetRequiredMemSize( &m_Archive );
95         m_pMemoryForSoundArchivePlayer = MemAlloc( setupSize, 32 );
96         u32 setupStrmBufferSize =
97             m_ArchivePlayer.GetRequiredStreamBufferSize( &m_Archive );
98         m_pMemoryForStreamBuffer = MemAlloc( setupStrmBufferSize, 32 );
99         bool result = m_ArchivePlayer.Initialize(
100                 &m_Archive,
101                 &m_DataManager,
102                 m_pMemoryForSoundArchivePlayer, setupSize,
103                 m_pMemoryForStreamBuffer, setupStrmBufferSize );
104         NW_ASSERT( result );
105     }
106 
107     // サウンドヒープの構築
108     {
109         m_pMemoryForSoundHeap = MemAlloc( SOUND_HEAP_SIZE );
110         bool result = m_Heap.Create( m_pMemoryForSoundHeap, SOUND_HEAP_SIZE );
111         NW_ASSERT( result );
112     }
113 }
114 
OnFinalize()115 void OriginalSoundHeapApp::OnFinalize()
116 {
117     nw::snd::SoundSystem::Finalize();
118 
119     MemFree( m_pMemoryForSoundSystem );
120     MemFree( m_pMemoryForInfoBlock );
121     MemFree( m_pMemoryForStringBlock );
122     MemFree( m_pMemoryForSoundDataManager );
123     MemFree( m_pMemoryForSoundArchivePlayer );
124     MemFree( m_pMemoryForSoundHeap );
125     MemFree( m_pMemoryForStreamBuffer );
126 }
127 
OnDrawUpLCD(nw::font::TextWriter & writer)128 void OriginalSoundHeapApp::OnDrawUpLCD( nw::font::TextWriter& writer )
129 {
130     writer.Printf(" DEMO nw::snd %s\n\n", DEMO_TITLE);
131 
132     writer.Print("    -- usage --\n\n");
133     writer.Print("    [A] Play Sequence Sound\n");
134     writer.Print("    [B] Stop Sound\n");
135 }
136 
OnDrawDownLCD(nw::font::TextWriter &)137 void OriginalSoundHeapApp::OnDrawDownLCD( nw::font::TextWriter& /*writer*/ )
138 {
139     // なにもしない
140 }
141 
OnUpdatePad(nw::demo::Pad & pad)142 void OriginalSoundHeapApp::OnUpdatePad( nw::demo::Pad& pad )
143 {
144     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_A ) )
145     {
146         m_Handle.Stop( 0 );
147         bool result = m_ArchivePlayer.StartSound( &m_Handle, SEQ_MARIOKART ).IsSuccess();
148         NN_LOG("[SEQ] SEQ_MARIOKART ... (%d)\n", result);
149     }
150 
151     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_B ) )
152     {
153         m_Handle.Stop( 3 );
154     }
155 }
156 
OnUpdate()157 void OriginalSoundHeapApp::OnUpdate()
158 {
159     m_ArchivePlayer.Update();
160 }
161 
162 
163 
OriginalSoundHeap()164 OriginalSoundHeap::OriginalSoundHeap()
165 : m_IsDirectionHead( true )
166 {
167 }
168 
Create(void * startAddress,size_t size)169 bool OriginalSoundHeap::Create( void* startAddress, size_t size )
170 {
171     m_pBufferHead = startAddress;
172     m_BufferSize = size;
173     m_pFrameHeap = nw::ut::FrameHeap::Create( startAddress, size );
174     if ( m_pFrameHeap != NULL ) { return true; }
175     else { return false; }
176 }
177 
Alloc(size_t size)178 void* OriginalSoundHeap::Alloc( size_t size )
179 {
180     int alignment = m_IsDirectionHead ? ALIGNMENT_SIZE : -ALIGNMENT_SIZE;
181 
182     return m_pFrameHeap->Alloc( size, alignment );
183 }
184 
Free()185 void OriginalSoundHeap::Free()
186 {
187     /* CHECK: メモリ領域を開放する場合は、あらかじめ
188               下記の Dispose / DisposeWave を呼ぶ必要がある */
189     {
190         nw::snd::internal::driver::DisposeCallbackManager& manager =
191             nw::snd::internal::driver::DisposeCallbackManager::GetInstance();
192 
193         manager.Dispose( m_pBufferHead, m_BufferSize );
194     }
195 
196     m_pFrameHeap->Free( nw::ut::FrameHeap::FREE_ALL );
197 }
198 
Destroy()199 void* OriginalSoundHeap::Destroy()
200 {
201     return m_pFrameHeap->Destroy();
202 }
203