1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     externalFileApp.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 "externalFileApp.h"
19 #include "externalFile.csid"
20 
21 namespace
22 {
23 /*
24 
25     定数
26 
27  */
28 const s32 SOUND_THREAD_PRIORITY = 4;
29 const s32 LOAD_THREAD_PRIORITY = 3;
30 const s32 SOUND_HEAP_SIZE = 1 * 1024 * 1024;
31 const char DEMO_TITLE[] = "externalFile";
32 const char SOUND_ARC_PATH[]      = NW_SND_DEMO_PATH_PREFIX "externalFile.bcsar";
33 const char EXTERNAL_FILE_PATH[]  = NW_SND_DEMO_PATH_PREFIX "WARC_TEST.bcwar";
34 
35 } // anonymous namespace
36 
37 
38 
39 /*
40 
41     ExternalFileApp クラス定義
42 
43  */
ExternalFileApp()44 ExternalFileApp::ExternalFileApp()
45 {
46 }
47 
OnInitialize()48 void ExternalFileApp::OnInitialize()
49 {
50     InitializeSoundSystem();
51     InitializeDemo();
52 }
53 
InitializeSoundSystem()54 void ExternalFileApp::InitializeSoundSystem()
55 {
56     // サウンドシステムの初期化
57     {
58         nw::snd::SoundSystem::SoundSystemParam param;
59         size_t workMemSize = nw::snd::SoundSystem::GetRequiredMemSize( param );
60         m_pMemoryForSoundSystem = MemAlloc( workMemSize );
61 
62         nw::snd::SoundSystem::Initialize(
63                 param,
64                 reinterpret_cast<uptr>( m_pMemoryForSoundSystem ),
65                 workMemSize );
66     }
67 
68     // サウンドアーカイブ
69     if ( ! m_Archive.Open( SOUND_ARC_PATH ) )
70     {
71         NW_ASSERTMSG( 0, "cannot open bcsar(%s)\n", SOUND_ARC_PATH );
72     }
73 
74     // INFO ブロックのロード
75     {
76         u32 infoBlockSize = m_Archive.GetHeaderSize();
77         m_pMemoryForInfoBlock = MemAlloc( infoBlockSize );
78         if ( ! m_Archive.LoadHeader( m_pMemoryForInfoBlock, infoBlockSize ) )
79         {
80             NW_ASSERTMSG( 0, "cannot load infoBlock(%s)", SOUND_ARC_PATH );
81         }
82     }
83 
84     // STRING ブロックのロード
85     {
86         u32 stringBlockSize = m_Archive.GetLabelStringDataSize();
87         m_pMemoryForStringBlock = MemAlloc( stringBlockSize );
88         if ( ! m_Archive.LoadLabelStringData( m_pMemoryForStringBlock, stringBlockSize ) )
89         {
90             NW_ASSERTMSG( 0, "cannot load stringBlock(%s)", SOUND_ARC_PATH );
91         }
92     }
93 
94     // サウンドデータマネージャーの初期化
95     {
96         u32 setupSize = m_DataManager.GetRequiredMemSize( &m_Archive );
97         m_pMemoryForSoundDataManager = MemAlloc( setupSize );
98         m_DataManager.Initialize( &m_Archive, m_pMemoryForSoundDataManager, setupSize );
99     }
100 
101     // サウンドアーカイブプレイヤーの初期化
102     {
103         u32 setupSize = m_ArchivePlayer.GetRequiredMemSize( &m_Archive );
104         m_pMemoryForSoundArchivePlayer = MemAlloc( setupSize, 32 );
105         u32 setupStrmBufferSize =
106             m_ArchivePlayer.GetRequiredStreamBufferSize( &m_Archive );
107         m_pMemoryForStreamBuffer = MemAlloc( setupStrmBufferSize, 32 );
108         bool result = m_ArchivePlayer.Initialize(
109                 &m_Archive,
110                 &m_DataManager,
111                 m_pMemoryForSoundArchivePlayer, setupSize,
112                 m_pMemoryForStreamBuffer, setupStrmBufferSize );
113         NW_ASSERT( result );
114     }
115 
116     // サウンドヒープの構築
117     {
118         m_pMemoryForSoundHeap = MemAlloc( SOUND_HEAP_SIZE );
119         bool result = m_Heap.Create( m_pMemoryForSoundHeap, SOUND_HEAP_SIZE );
120         NW_ASSERT( result );
121     }
122 }
123 
OnFinalize()124 void ExternalFileApp::OnFinalize()
125 {
126     nw::snd::SoundSystem::Finalize();
127 
128     MemFree( m_pMemoryForSoundSystem );
129     MemFree( m_pMemoryForInfoBlock );
130     MemFree( m_pMemoryForStringBlock );
131     MemFree( m_pMemoryForSoundDataManager );
132     MemFree( m_pMemoryForSoundArchivePlayer );
133     MemFree( m_pMemoryForSoundHeap );
134     MemFree( m_pMemoryForStreamBuffer );
135     MemFree( m_pMemoryForExternalFile );
136 }
137 
OnDrawUpLCD(nw::font::TextWriter & writer)138 void ExternalFileApp::OnDrawUpLCD( nw::font::TextWriter& writer )
139 {
140     writer.Printf(" DEMO nw::snd %s\n\n", DEMO_TITLE );
141 
142     writer.Print("    -- usage --\n\n");
143     writer.Print("    [A] Play SE_1\n");
144     writer.Print("    [X] Play SE_2\n");
145     writer.Print("    [B] Stop Sound\n\n");
146 }
147 
OnDrawDownLCD(nw::font::TextWriter &)148 void ExternalFileApp::OnDrawDownLCD( nw::font::TextWriter& )
149 {
150     // なにも書かない
151 }
152 
OnUpdatePad(nw::demo::Pad & pad)153 void ExternalFileApp::OnUpdatePad( nw::demo::Pad& pad )
154 {
155     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_A ) )
156     {
157         m_Handle.Stop(0);
158         bool result = m_ArchivePlayer.StartSound( &m_Handle, WSD_YOSHI ).IsSuccess();
159         NN_LOG("WSD_YOSHI ... (%d)\n", result);
160     }
161 
162     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_X ) )
163     {
164         m_Handle.Stop(0);
165         bool result = m_ArchivePlayer.StartSound( &m_Handle, WSD_WIHAHO ).IsSuccess();
166         NN_LOG("WSD_WIHAHO ... (%d)\n", result);
167     }
168 
169     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_B ) )
170     {
171         m_Handle.Stop( 3 );
172     }
173 }
174 
InitializeDemo()175 void ExternalFileApp::InitializeDemo()
176 {
177     // ウェーブサウンドデータのみ、サウンドヒープにロード
178     if ( ! m_DataManager.LoadData( WSD_YOSHI, &m_Heap, nw::snd::SoundDataManager::LOAD_WSD ) )
179     {
180         NW_ASSERTMSG( false, "LoadData(WSD_YOSHI) failed.\n");
181     }
182 
183     // 波形アーカイブファイルをメモリにロード
184     {
185         nn::fs::FileReader reader( EXTERNAL_FILE_PATH );
186         m_LengthForExternalFile = (s32)reader.GetSize();
187         m_pMemoryForExternalFile = MemAlloc( m_LengthForExternalFile, 32 );
188         (void)reader.Read( m_pMemoryForExternalFile, m_LengthForExternalFile );
189         reader.Finalize();
190     }
191 
192     // ロードした波形アーカイブファイルを SoundDataManager に登録
193     {
194         u32 fileId = m_Archive.GetItemFileId( WARC_TEST );
195         m_DataManager.SetFileAddress( fileId, m_pMemoryForExternalFile );
196     }
197 
198     // SoundDataManager の登録を解除し、波形アーカイブファイルをメモリから解放
199     // {
200     //     m_DataManager.InvalidateSoundData(
201     //             m_pMemoryForExternalFile, m_LengthForExternalFile );
202     //     MemFree( m_pMemoryForExternalFile );
203     // }
204 }
205 
OnUpdate()206 void ExternalFileApp::OnUpdate()
207 {
208     m_ArchivePlayer.Update();
209 }
210