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