1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     main.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 <nw/snd.h>
21 #include <nw/snd/snd_HioSoundArchive.h>
22 #include "demolib.h"
23 
24 
25 /*---------------------------------------------------------------------------*
26 
27  *---------------------------------------------------------------------------*/
28 namespace
29 {
30 
31 const s32 SOUND_THREAD_PRIORITY = 4;
32 const s32 LOAD_THREAD_PRIORITY = 3;
33 const s32 SOUND_HEAP_SIZE = 10 * 1024 * 1024;
34 const char SOUND_ARC_PATH[] = "simple.bcsar";
35 const char DEMO_TITLE[] = "HioSoundArchive";
36 
37 const int CHAR_BUF_SIZE = 256;
38 char s_HioFilePathBuffer[CHAR_BUF_SIZE];
39 const char* SOUND_ARC_DIR[] =
40 {
41     "tools/SoundMaker/_tmpForSoundPlayer/A",   // DIR_TYPE_A
42     "tools/SoundMaker/_tmpForSoundPlayer/B"    // DIR_TYPE_B
43 };
44 const char* SOUND_ARC_FILE = "tmp.bcsar";
45 const char* LATEST_COPY_FILE = "_latestCopy";
46 
47 enum DirType { DIR_TYPE_A, DIR_TYPE_B };
48 DirType s_DirType;
49 
GetHioNw4cRoot()50 const char* GetHioNw4cRoot()
51 {
52     s32 result = nn::hio::GetEnvironmentVariable(
53             s_HioFilePathBuffer, sizeof(s_HioFilePathBuffer), "NW4C_ROOT" );
54     if ( result < 0 ) { return NULL; }
55     return s_HioFilePathBuffer;
56 }
GetHioSoundArchiveFilePath(DirType type)57 const char* GetHioSoundArchiveFilePath( DirType type )
58 {
59     std::snprintf( s_HioFilePathBuffer, CHAR_BUF_SIZE, "%s/%s/%s",
60             GetHioNw4cRoot(), SOUND_ARC_DIR[type], SOUND_ARC_FILE );
61     return s_HioFilePathBuffer;
62 }
GetHioLatestCopyFilePath(DirType type)63 const char* GetHioLatestCopyFilePath( DirType type )
64 {
65     std::snprintf( s_HioFilePathBuffer, CHAR_BUF_SIZE, "%s/%s/%s",
66             GetHioNw4cRoot(), SOUND_ARC_DIR[type], LATEST_COPY_FILE );
67     return s_HioFilePathBuffer;
68 }
OpenLatestCopyFile(nn::hio::HostFile & file,DirType type)69 bool OpenLatestCopyFile( nn::hio::HostFile& file, DirType type )
70 {
71     bool result = file.Open(
72             GetHioLatestCopyFilePath( type ),
73             nn::hio::HostFile::ACCESS_MODE_READ,
74             nn::hio::HostFile::OPEN_DISP_OPEN_EXISTING ).IsSuccess();
75     NN_LOG("Open (%s) ... %d\n", GetHioLatestCopyFilePath( type ), result );
76     return result;
77 }
78 
79 } // anonymous namespace
80 
81 
82 
83 /*---------------------------------------------------------------------------*
84 
85     �A�v���P�[�V�����N���X
86 
87  *---------------------------------------------------------------------------*/
88 class HioSoundArchiveApp : public nw::snd::demolib::AppBase
89 {
90 protected:
91     virtual void OnInitialize();
92     virtual void OnFinalize();
93     virtual void OnDrawUpLCD( nw::font::TextWriter& );
94     virtual void OnDrawDownLCD( nw::font::TextWriter& );
95     virtual void OnUpdatePad( nw::demo::Pad& );
96     virtual void OnUpdate();
97 
98 private:
99     void InitializeSoundSystem();
100     bool InitializeSoundManager();
101     bool OpenSoundArchive();
102     void CloseSoundArchive();
103 
104     nw::snd::HioSoundArchive    m_Archive;
105     nw::snd::SoundArchivePlayer m_ArchivePlayer;
106     nw::snd::SoundDataManager   m_DataManager;
107     nw::snd::SoundHeap          m_Heap;
108     nw::snd::SoundHandle        m_Handle;
109 
110     void* m_pMemoryForSoundSystem;
111     void* m_pMemoryForInfoBlock;
112     void* m_pMemoryForStringBlock;
113     void* m_pMemoryForSoundDataManager;
114     void* m_pMemoryForSoundArchivePlayer;
115     void* m_pMemoryForSoundHeap;
116     void* m_pMemoryForStreamBuffer;
117 
118     u32 m_SoundIndex;
119     u32 m_SoundCount;
120     bool m_IsOpenedSoundArchive;
121     bool m_IsLoadStringBlock;
122 };
123 
124 
125 
126 /*---------------------------------------------------------------------------*
127 
128     �A�v���P�[�V�����N���X����
129 
130  *---------------------------------------------------------------------------*/
OnInitialize()131 void HioSoundArchiveApp::OnInitialize()
132 {
133     // nn::hio
134     {
135         nn::Result result = nn::hio::Initialize( MemAlloc( nn::hio::WORKMEMORY_SIZE ) );
136         NW_ASSERT( result.IsSuccess() );
137         if ( result.IsFailure() )
138         {
139             return;
140         }
141     }
142 
143     // �A�v���P�[�V����������
144     m_IsOpenedSoundArchive = false;
145     m_IsLoadStringBlock = false;
146     m_SoundIndex = 0;
147     m_SoundCount = 0;
148 
149     InitializeSoundSystem();
150     OpenSoundArchive();
151 }
152 
InitializeSoundSystem()153 void HioSoundArchiveApp::InitializeSoundSystem()
154 {
155     // �T�E���h�V�X�e���̏�����
156     {
157         nw::snd::SoundSystem::SoundSystemParam param;
158         size_t workMemSize = nw::snd::SoundSystem::GetRequiredMemSize( param );
159         m_pMemoryForSoundSystem = MemAlloc( workMemSize );
160 
161         nw::snd::SoundSystem::Initialize(
162                 param,
163                 reinterpret_cast<uptr>( m_pMemoryForSoundSystem ),
164                 workMemSize );
165     }
166 }
167 
InitializeSoundManager()168 bool HioSoundArchiveApp::InitializeSoundManager()
169 {
170     // �T�E���h�f�[�^�}�l�[�W���[�̏�����
171     {
172         size_t setupSize = m_DataManager.GetRequiredMemSize( &m_Archive );
173         m_pMemoryForSoundDataManager = MemAlloc( setupSize );
174         m_DataManager.Initialize( &m_Archive, m_pMemoryForSoundDataManager, setupSize );
175     }
176 
177     // �T�E���h�A�[�J�C�u�v���C���[�̏�����
178     {
179         size_t setupSize = m_ArchivePlayer.GetRequiredMemSize( &m_Archive );
180         m_pMemoryForSoundArchivePlayer = MemAlloc( setupSize, 32 );
181         size_t setupStrmBufferSize =
182             m_ArchivePlayer.GetRequiredStreamBufferSize( &m_Archive );
183         m_pMemoryForStreamBuffer = MemAlloc( setupStrmBufferSize, 32 );
184         bool result = m_ArchivePlayer.Initialize(
185                 &m_Archive,
186                 &m_DataManager,
187                 m_pMemoryForSoundArchivePlayer, setupSize,
188                 m_pMemoryForStreamBuffer, setupStrmBufferSize );
189         NW_ASSERT( result );
190         if ( result == false )
191         {
192             return false;
193         }
194     }
195 
196     // �T�E���h�q�[�v�̍\�z
197     {
198         m_pMemoryForSoundHeap = MemAlloc( SOUND_HEAP_SIZE );
199         bool result = m_Heap.Create( m_pMemoryForSoundHeap, SOUND_HEAP_SIZE );
200         NW_ASSERT( result );
201         if ( result == false )
202         {
203             return false;
204         }
205     }
206 
207     return true;
208 }
209 
OpenSoundArchive()210 bool HioSoundArchiveApp::OpenSoundArchive()
211 {
212     bool ret = false;
213 
214     {
215         nn::hio::HostFile file;
216         ret = OpenLatestCopyFile( file, DIR_TYPE_A );
217         if ( ret == true )
218         {
219             s_DirType = DIR_TYPE_A;
220         }
221         else
222         {
223             ret = OpenLatestCopyFile( file, DIR_TYPE_B );
224             if ( ret == true )
225             {
226                 s_DirType = DIR_TYPE_B;
227             }
228             else
229             {
230                 return false;
231             }
232         }
233         file.Close();
234     }
235 
236     const char* HIO_ARCHIVE_PATH = GetHioSoundArchiveFilePath( s_DirType );
237     NN_LOG("HIO_ARCHIVE_PATH(%s)\n", HIO_ARCHIVE_PATH );
238     // �I�[�v��
239     {
240         ret = m_Archive.Open( HIO_ARCHIVE_PATH );
241         if ( ret == false )
242         {
243             // NW_ASSERTMSG( 0, "cannot open bcsar(%s)\n", HIO_ARCHIVE_PATH );
244             return false;
245         }
246     }
247 
248     // INFO �u���b�N�̃��[�h
249     {
250         u32 infoBlockSize = m_Archive.GetHeaderSize();
251         m_pMemoryForInfoBlock = MemAlloc( infoBlockSize );
252         if ( ! m_Archive.LoadHeader( m_pMemoryForInfoBlock, infoBlockSize ) )
253         {
254             NW_ASSERTMSG( 0, "cannot load infoBlock(%s)", HIO_ARCHIVE_PATH );
255         }
256     }
257 
258     // STRING �u���b�N�̃��[�h
259     {
260         u32 stringBlockSize = m_Archive.GetLabelStringDataSize();
261         // size == 0xffffffff �̂Ƃ��A������u���b�N���܂܂�Ă��Ȃ�
262         if ( stringBlockSize != 0xffffffff )
263         {
264             m_pMemoryForStringBlock = MemAlloc( stringBlockSize );
265             if ( ! m_Archive.LoadLabelStringData( m_pMemoryForStringBlock, stringBlockSize ) )
266             {
267                 NW_ASSERTMSG( 0, "cannot load stringBlock(%s)", HIO_ARCHIVE_PATH );
268             }
269             else
270             {
271                 m_IsLoadStringBlock = true;
272             }
273         }
274     }
275     m_IsOpenedSoundArchive = true;
276     m_SoundCount = m_Archive.GetSoundCount();
277     m_SoundIndex = 0;
278     ret = InitializeSoundManager();
279 
280     return ret;
281 }
282 
283 // �T�E���h�A�[�J�C�u�ēǍ��̂��߁ASoundArchive ����U�‚���B
284 // (�֘A���� SoundArchivePlayer, SoundDataManager �Ȃǂ� Finalize ����)
CloseSoundArchive()285 void HioSoundArchiveApp::CloseSoundArchive()
286 {
287     if ( m_IsOpenedSoundArchive == false )
288     {
289         return;
290     }
291 
292     if ( m_Heap.IsValid() )
293     {
294         m_Heap.Destroy();
295         MemFree( m_pMemoryForSoundHeap );
296     }
297 
298     if ( m_ArchivePlayer.IsAvailable() )
299     {
300         m_ArchivePlayer.Finalize();
301         MemFree( m_pMemoryForSoundArchivePlayer );
302         MemFree( m_pMemoryForStreamBuffer );
303     }
304 
305     if ( m_DataManager.IsAvailable() )
306     {
307         m_DataManager.Finalize();
308         MemFree( m_pMemoryForSoundDataManager );
309     }
310 
311     m_Archive.Close();
312     if ( m_IsLoadStringBlock )
313     {
314         MemFree( m_pMemoryForStringBlock );
315     }
316     MemFree( m_pMemoryForInfoBlock );
317 
318     m_IsOpenedSoundArchive = false;
319     m_IsLoadStringBlock = false;
320     m_SoundIndex = 0;
321     m_SoundCount = 0;
322 }
323 
OnFinalize()324 void HioSoundArchiveApp::OnFinalize()
325 {
326     nw::snd::SoundSystem::Finalize();
327 
328     MemFree( m_pMemoryForSoundSystem );
329     MemFree( m_pMemoryForInfoBlock );
330     MemFree( m_pMemoryForSoundDataManager );
331     MemFree( m_pMemoryForSoundArchivePlayer );
332     MemFree( m_pMemoryForSoundHeap );
333     MemFree( m_pMemoryForStreamBuffer );
334 }
335 
OnDrawUpLCD(nw::font::TextWriter & writer)336 void HioSoundArchiveApp::OnDrawUpLCD( nw::font::TextWriter& writer )
337 {
338     writer.Printf(" DEMO nw::snd %s\n\n", DEMO_TITLE);
339 
340     writer.Print ("    -- usage --\n\n");
341     writer.Print ("    [A] Start Sound\n");
342     writer.Print ("    [B] Stop Sound\n\n");
343     writer.Print ("    [LEFT/RIGHT] Change Sound\n");
344     writer.Print ("    [START] Reload Sound Archive\n\n");
345 
346     // �T�E���h�\��
347     if ( m_IsOpenedSoundArchive )
348     {
349         nw::snd::SoundArchive::ItemId id = m_Archive.GetSoundIdFromIndex( m_SoundIndex );
350         if ( m_IsLoadStringBlock )
351         {
352             const char* label = m_Archive.GetItemLabel( id );
353             if ( label == NULL )
354             {
355                 label = "...";  // ������
356             }
357             writer.Printf("    [%08x] %s\n", id, label );
358         }
359     }
360     else
361     {
362         writer.Print ("   SoundArchive is not opened.\n");
363         writer.Print ("   Press START for open SoundArchive.\n");
364     }
365 }
366 
OnDrawDownLCD(nw::font::TextWriter & writer)367 void HioSoundArchiveApp::OnDrawDownLCD( nw::font::TextWriter& writer )
368 {
369     (void)writer;
370 }
371 
OnUpdatePad(nw::demo::Pad & pad)372 void HioSoundArchiveApp::OnUpdatePad( nw::demo::Pad& pad )
373 {
374     if ( pad.IsButtonRepeatFast( nw::demo::Pad::BUTTON_LEFT ) )
375     {
376         if ( m_SoundIndex == 0 )
377         {
378             m_SoundIndex = m_SoundCount - 1;
379         }
380         else
381         {
382             m_SoundIndex -= 1;
383         }
384     }
385     if ( pad.IsButtonRepeatFast( nw::demo::Pad::BUTTON_RIGHT ) )
386     {
387         if ( m_SoundIndex == m_SoundCount - 1 )
388         {
389             m_SoundIndex = 0;
390         }
391         else
392         {
393             m_SoundIndex += 1;
394         }
395     }
396     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_START ) )
397     {
398         CloseSoundArchive();
399         OpenSoundArchive();
400     }
401 
402     if ( m_IsOpenedSoundArchive )
403     {
404         if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_A ) )
405         {
406             // �O�̃T�E���h���X�g�b�v
407             m_Handle.Stop( 0 );
408 
409             // �f�[�^���[�h (SEQ/WSD�̂�)
410             nw::snd::SoundArchive::ItemId id = m_Archive.GetSoundIdFromIndex( m_SoundIndex );
411             if ( m_Archive.GetSoundType( id ) != nw::snd::SoundArchive::SOUND_TYPE_STRM )
412             {
413                 if ( ! m_DataManager.IsDataLoaded( id ) )
414                 {
415                     m_Heap.LoadState( 0 );  // �q�[�v���Z�b�g
416                     if ( ! m_DataManager.LoadData( id, &m_Heap ) )
417                     {
418                         NW_WARNING( false, "load failed ... %08x is too big", id );
419                     }
420                 }
421             }
422             bool ret = m_ArchivePlayer.StartSound( &m_Handle, id ).IsSuccess();
423             NN_LOG("StartSound(%08x) ... %d\n", id, ret );
424         }
425         if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_B ) )
426         {
427             m_Handle.Stop( 3 );
428         }
429     }
430 }
431 
OnUpdate()432 void HioSoundArchiveApp::OnUpdate()
433 {
434     m_ArchivePlayer.Update();
435 }
436 
437 
438 
439 /*---------------------------------------------------------------------------*
440 
441     ���C������
442 
443  *---------------------------------------------------------------------------*/
nnMain()444 void nnMain()
445 {
446     HioSoundArchiveApp app;
447 
448     app.Initialize();
449     app.Run();
450     app.Finalize();
451 }
452