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