1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     SoundActorApp.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 "SoundActorApp.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[] = "SoundActor";
31 
32 const f32 ACTOR_PAN_MIN = -1.0f;
33 const f32 ACTOR_PAN_MAX =  1.0f;
34 const f32 ACTOR_PAN_DEFAULT = 0.0f;
35 const f32 ACTOR_VOLUME_MIN = 0.1f;
36 const f32 ACTOR_VOLUME_MAX = 2.0f;
37 const f32 ACTOR_VOLUME_DEFAULT = 1.0f;
38 const f32 CONTROL_PAD_STEP = 0.05f;
39 }
40 
OnInitialize()41 void SoundActorApp::OnInitialize()
42 {
43     InitializeSoundSystem();
44 
45     // サウンドデータのロード
46     if ( ! m_DataManager.LoadData( SE_SIN440, &m_Heap ) )
47     {
48         NW_ASSERTMSG( false, "LoadData(SE_SIN440) failed." );
49     }
50     if ( ! m_DataManager.LoadData( SE_YOSHI, &m_Heap ) )
51     {
52         NW_ASSERTMSG( false, "LoadData(SE_YOSHI) failed." );
53     }
54 
55     // サウンドアクターの初期化
56     m_ActorPan = ACTOR_PAN_DEFAULT;
57     m_ActorVolume = ACTOR_VOLUME_DEFAULT;
58 
59     m_Actor.Initialize( m_ArchivePlayer );
60     m_Actor.SetPlayableSoundCount( 0, 1 );
61 }
62 
InitializeSoundSystem()63 void SoundActorApp::InitializeSoundSystem()
64 {
65     // サウンドシステムの初期化
66     {
67         nw::snd::SoundSystem::SoundSystemParam param;
68         size_t workMemSize = nw::snd::SoundSystem::GetRequiredMemSize( param );
69         m_pMemoryForSoundSystem = MemAlloc( workMemSize );
70 
71         nw::snd::SoundSystem::Initialize(
72                 param,
73                 reinterpret_cast<uptr>( m_pMemoryForSoundSystem ),
74                 workMemSize );
75     }
76 
77     // サウンドアーカイブの初期化
78     if ( ! m_Archive.Open( SOUND_ARC_PATH ) )
79     {
80         NW_ASSERTMSG( 0, "cannot open bcsar(%s)\n", SOUND_ARC_PATH );
81     }
82 
83     // INFO ブロックのロード
84     {
85         u32 infoBlockSize = m_Archive.GetHeaderSize();
86         m_pMemoryForInfoBlock = MemAlloc( infoBlockSize );
87         if ( ! m_Archive.LoadHeader( m_pMemoryForInfoBlock, infoBlockSize ) )
88         {
89             NW_ASSERTMSG( 0, "cannot load infoBlock(%s)", SOUND_ARC_PATH );
90         }
91     }
92 
93     // サウンドデータマネージャーの初期化
94     {
95         u32 setupSize = m_DataManager.GetRequiredMemSize( &m_Archive );
96         m_pMemoryForSoundDataManager = MemAlloc( setupSize );
97         m_DataManager.Initialize( &m_Archive, m_pMemoryForSoundDataManager, setupSize );
98     }
99 
100     // サウンドアーカイブプレイヤーの初期化
101     {
102         u32 setupSize = m_ArchivePlayer.GetRequiredMemSize( &m_Archive );
103         m_pMemoryForSoundArchivePlayer = MemAlloc( setupSize, 32 );
104         u32 setupStrmBufferSize =
105             m_ArchivePlayer.GetRequiredStreamBufferSize( &m_Archive );
106         m_pMemoryForStreamBuffer = MemAlloc( setupStrmBufferSize, 32 );
107         bool result = m_ArchivePlayer.Initialize(
108                 &m_Archive,
109                 &m_DataManager,
110                 m_pMemoryForSoundArchivePlayer, setupSize,
111                 m_pMemoryForStreamBuffer, setupStrmBufferSize );
112         NW_ASSERT( result );
113     }
114 
115     // サウンドヒープの構築
116     {
117         m_pMemoryForSoundHeap = MemAlloc( SOUND_HEAP_SIZE );
118         bool result = m_Heap.Create( m_pMemoryForSoundHeap, SOUND_HEAP_SIZE );
119         NW_ASSERT( result );
120     }
121 }
122 
OnFinalize()123 void SoundActorApp::OnFinalize()
124 {
125     m_Actor.Finalize();
126     nw::snd::SoundSystem::Finalize();
127 
128     MemFree( m_pMemoryForSoundSystem );
129     MemFree( m_pMemoryForInfoBlock );
130     MemFree( m_pMemoryForSoundDataManager );
131     MemFree( m_pMemoryForSoundArchivePlayer );
132     MemFree( m_pMemoryForSoundHeap );
133     MemFree( m_pMemoryForStreamBuffer );
134 }
135 
OnDrawUpLCD(nw::font::TextWriter & writer)136 void SoundActorApp::OnDrawUpLCD( nw::font::TextWriter& writer )
137 {
138     writer.Printf(" DEMO nw::snd %s\n\n", DEMO_TITLE);
139 
140     writer.Print ("    -- usage --\n\n");
141     writer.Print ("    [A] Play Sound\n");
142     writer.Print ("    [B] Stop Sound\n");
143     writer.Print ("    [LEFT/RIGHT] Modify Actor Pan\n");
144     writer.Print ("    [UP/DOWN]    Modify Actor Volume\n\n\n");
145 
146     writer.Print ("    -- status --\n\n");
147     writer.Printf("    pan    % 2.2f\n", m_ActorPan );
148     writer.Printf("    volume % 2.2f\n", m_ActorVolume );
149 }
150 
OnDrawDownLCD(nw::font::TextWriter & writer)151 void SoundActorApp::OnDrawDownLCD( nw::font::TextWriter& writer )
152 {
153     (void)writer;
154 }
155 
OnUpdatePad(nw::demo::Pad & pad)156 void SoundActorApp::OnUpdatePad( nw::demo::Pad& pad )
157 {
158     // A, B で再生停止
159     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_A ) )
160     {
161         // m_Handle.Stop( 0 );
162         bool result = m_Actor.StartSound( &m_Handle, SE_YOSHI ).IsSuccess();
163         NN_LOG("SE_YOSHI ... (%d)\n", result);
164     }
165     if ( pad.IsButtonDown( nw::demo::Pad::BUTTON_B ) )
166     {
167         m_Actor.StopAllSound( 3 );
168     }
169 
170     // 左右で、位置変更
171     if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_LEFT ) )
172     {
173         m_ActorPan = m_ActorPan <= ACTOR_PAN_MIN ?
174             ACTOR_PAN_MIN : m_ActorPan - CONTROL_PAD_STEP;
175         m_Actor.HoldSound( &m_HandleForHoldSound, SE_SIN440 );
176     }
177     if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_RIGHT ) )
178     {
179         m_ActorPan = m_ActorPan >= ACTOR_PAN_MAX ?
180             ACTOR_PAN_MAX : m_ActorPan + CONTROL_PAD_STEP;
181         m_Actor.HoldSound( &m_HandleForHoldSound, SE_SIN440 );
182     }
183 
184     // 上下で、サイズ変更
185     if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_UP ) )
186     {
187         m_ActorVolume = m_ActorVolume >= ACTOR_VOLUME_MAX ?
188             ACTOR_VOLUME_MAX : m_ActorVolume + CONTROL_PAD_STEP;
189         m_Actor.HoldSound( &m_HandleForHoldSound, SE_SIN440 );
190     }
191     if ( pad.IsButtonPress( nw::demo::Pad::BUTTON_DOWN ) )
192     {
193         m_ActorVolume = m_ActorVolume <= ACTOR_VOLUME_MIN ?
194             ACTOR_VOLUME_MIN : m_ActorVolume - CONTROL_PAD_STEP;
195         m_Actor.HoldSound( &m_HandleForHoldSound, SE_SIN440 );
196     }
197 }
198 
OnUpdate()199 void SoundActorApp::OnUpdate()
200 {
201     m_ArchivePlayer.Update();
202 
203     // アクターの更新
204     {
205         m_Actor.SetVolume( m_ActorVolume );
206         m_Actor.SetPan( m_ActorPan );
207     }
208 }
209