1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_Sound3DManager.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/snd_Sound3DManager.h>
21 
22 #include <nw/snd/snd_Sound3DEngine.h>
23 #include <nw/snd/snd_SoundArchive.h>
24 
25 namespace nw {
26 namespace snd {
27 
28 namespace {
29 
30 Sound3DEngine sSound3DEngine;
31 
32 }
33 
34 /* ------------------------------------------------------------------------
35         member function
36    ------------------------------------------------------------------------ */
37 
38 /*---------------------------------------------------------------------------*
39   Name:         Sound3DManager
40 
41   Description:  コンストラクタ
42 
43   Arguments:    None.
44 
45   Returns:      None.
46  *---------------------------------------------------------------------------*/
Sound3DManager()47 Sound3DManager::Sound3DManager()
48 : m_pSound3DEngine( &sSound3DEngine ),
49   m_MaxPriorityReduction( 32 ),
50   m_PanRange( 0.9f ),
51   m_SonicVelocity( 0.0f ),
52   m_BiquadFilterType( 0 ),
53   m_pSoundParamPoolBuffer( NULL ),
54   m_SoundParamPoolBufferSize( 0 ),
55   m_FreeMemSizeAfterCheking( 0 ),
56   m_IsInitialized( false )
57 {
58 }
59 
60 /*---------------------------------------------------------------------------*
61   Name:         GetRequiredMemSize
62 
63   Description:  3Dサウンドに必要なメモリサイズを取得する
64 
65   Arguments:    arc - サウンドアーカイブ
66 
67   Returns:      必要となるメモリサイズ
68  *---------------------------------------------------------------------------*/
GetRequiredMemSize(const SoundArchive * arc)69 size_t Sound3DManager::GetRequiredMemSize( const SoundArchive* arc )
70 {
71     NW_NULL_ASSERT( arc );
72 
73     s32 numSounds = 0;
74 
75     SoundArchive::SoundArchivePlayerInfo soundArchivePlayerInfo;
76     if ( arc->ReadSoundArchivePlayerInfo( &soundArchivePlayerInfo ) )
77     {
78         numSounds += soundArchivePlayerInfo.sequenceSoundMax;
79         numSounds += soundArchivePlayerInfo.streamSoundMax;
80         numSounds += soundArchivePlayerInfo.waveSoundMax;
81     }
82 
83     return static_cast<size_t>( numSounds ) * sizeof( Sound3DParam );
84 }
85 
86 /*---------------------------------------------------------------------------*
87   Name:         AssignWorkBuffer
88 
89   Description:  3Dサウンドを使用するメモリの初期化を行う
90 
91   Arguments:    arc - サウンドアーカイブ
92                 buffer - バッファのアドレス
93                 size - バッファサイズ
94 
95   Returns:      成功したら true
96  *---------------------------------------------------------------------------*/
Initialize(const SoundArchive * arc,void * buffer,size_t size)97 bool Sound3DManager::Initialize(
98     const SoundArchive* arc,
99     void* buffer,
100     size_t size
101 )
102 {
103     if ( m_IsInitialized == true )
104     {
105         return false;
106     }
107     NW_UNUSED_VARIABLE( arc );
108 
109     NW_NULL_ASSERT( arc );
110     NW_NULL_ASSERT( buffer );
111     size_t memSize = GetRequiredMemSize( arc );
112     NW_ASSERT( size >= memSize );
113 
114     // ActorParams Create
115     m_ParamPool.Create( buffer, size );
116     m_pSoundParamPoolBuffer = buffer;
117     m_SoundParamPoolBufferSize = size;
118     m_FreeMemSizeAfterCheking = size - memSize;
119     m_IsInitialized = true;
120 
121     return true;
122 }
123 
InitializeWithMoreSoundArchive(const SoundArchive * arc)124 bool Sound3DManager::InitializeWithMoreSoundArchive( const SoundArchive* arc )
125 {
126     if ( m_IsInitialized == false )
127     {
128         return false;
129     }
130 
131     NW_UNUSED_VARIABLE( arc );
132     NW_NULL_ASSERT( arc );
133     size_t memSize = GetRequiredMemSize( arc );
134 
135     m_FreeMemSizeAfterCheking -= memSize;
136     NW_ASSERT( m_FreeMemSizeAfterCheking >= 0 );
137     return true;
138 }
139 
Finalize()140 bool Sound3DManager::Finalize()
141 {
142     if ( m_IsInitialized == false )
143     {
144         return false;
145     }
146 
147     // 初期値に戻す
148     m_pSound3DEngine = &sSound3DEngine;
149     m_MaxPriorityReduction = 32;
150     m_PanRange = 0.9f;
151     m_SonicVelocity = 0.0f;
152     m_BiquadFilterType = 0;
153 
154     // リスナーを外す
155     while ( !m_ListenerList.IsEmpty() )
156     {
157         Sound3DListener& listener = m_ListenerList.GetBack();
158         m_ListenerList.Erase( &listener );
159     }
160 
161     // パラメータプールを破棄する
162     m_ParamPool.Destroy( m_pSoundParamPoolBuffer, m_SoundParamPoolBufferSize );
163     m_pSoundParamPoolBuffer = NULL;
164     m_SoundParamPoolBufferSize = 0;
165     m_FreeMemSizeAfterCheking = 0;
166 
167     m_IsInitialized = false;
168     return true;
169 }
170 
171 /*---------------------------------------------------------------------------*
172   Name:         SetEngine
173 
174   Description:  3D エンジンを登録する
175 
176   Arguments:    engine - 3D エンジン
177 
178   Returns:      なし
179  *---------------------------------------------------------------------------*/
SetEngine(Sound3DEngine * engine)180 void Sound3DManager::SetEngine( Sound3DEngine* engine )
181 {
182     m_pSound3DEngine = engine;
183 }
184 
185 /*---------------------------------------------------------------------------*
186   Name:         detail_UpdateAmbientParam
187 
188   Description:  3Dサウンドのパラメータ更新
189 
190   Arguments:    arg   - AmbientArgのポインタ
191                 soundId - サウンドID
192                 param - 3Dサウンドの計算結果を格納するパラメータ構造体
193 
194   Returns:      None.
195  *---------------------------------------------------------------------------*/
detail_UpdateAmbientParam(const void * arg,u32 soundId,SoundAmbientParam * param)196 void Sound3DManager::detail_UpdateAmbientParam(
197     const void* arg,
198     u32 soundId,
199     SoundAmbientParam* param
200 )
201 {
202     const Sound3DParam* actorParam = static_cast<const Sound3DParam*>( arg );
203 
204     if ( m_pSound3DEngine != NULL )
205     {
206         m_pSound3DEngine->UpdateAmbientParam(
207             this,
208             actorParam,
209             soundId,
210             param
211         );
212     }
213 }
214 
215 /*---------------------------------------------------------------------------*
216   Name:         detail_GetAmbientPriority
217 
218   Description:  アンビエントプライオリティの計算
219 
220   Arguments:    arg   - AmbientArgのポインタ
221                 soundId - サウンドID
222 
223   Returns:      None.
224   *---------------------------------------------------------------------------*/
detail_GetAmbientPriority(const void * arg,u32 soundId)225 int Sound3DManager::detail_GetAmbientPriority(
226     const void* arg,
227     u32 soundId
228 )
229 {
230     const Sound3DParam* actorParam = static_cast<const Sound3DParam*>( arg );
231 
232     int priority = 0;
233 
234     if ( m_pSound3DEngine != NULL )
235     {
236         priority = m_pSound3DEngine->GetAmbientPriority(
237             this,
238             actorParam,
239             soundId
240         );
241     }
242 
243     return priority;
244 }
245 
246 /*---------------------------------------------------------------------------*
247   Name:         AllocAmbientArg
248 
249   Description:  AmbientArgを確保します
250 
251   Arguments:    argSize - AmbientArgのサイズ
252 
253   Returns:      AmbientArgへのポインタ
254  *---------------------------------------------------------------------------*/
detail_AllocAmbientArg(size_t argSize)255 void* Sound3DManager::detail_AllocAmbientArg( size_t argSize )
256 {
257     if ( argSize != sizeof( Sound3DParam ) ) return NULL;
258     return m_ParamPool.Alloc();
259 }
260 
261 /*---------------------------------------------------------------------------*
262   Name:         FreeAmbientArg
263 
264   Description:  AmbientArgを解放します
265 
266   Arguments:    arg - 解放するAmbientArg
267                 sound - サウンドのインスタンス
268 
269   Returns:      None.
270  *---------------------------------------------------------------------------*/
detail_FreeAmbientArg(void * arg,const internal::BasicSound * sound)271 void Sound3DManager::detail_FreeAmbientArg( void* arg, const internal::BasicSound* sound )
272 {
273     (void)sound;
274 
275     Sound3DParam* actorParam = static_cast<Sound3DParam*>( arg );
276     m_ParamPool.Free( actorParam );
277 }
278 
279 /*---------------------------------------------------------------------------*
280   Name:         SetBiquadFilterType
281 
282   Description:  Biquad フィルタの種類を指定します
283 
284   Arguments:    type - Biquad フィルタの種類
285 
286   Returns:      なし
287  *---------------------------------------------------------------------------*/
SetBiquadFilterType(int type)288 void Sound3DManager::SetBiquadFilterType( int type )
289 {
290     NW_MINMAX_ASSERT( type, 0, BIQUAD_FILTER_TYPE_USER_MAX );
291     m_BiquadFilterType = type;
292 }
293 
Sound3DParam()294 Sound3DParam::Sound3DParam()
295 : ctrlFlag( 0 ),
296   actorUserParam( 0 ),
297   soundUserParam( 0 ),
298   decayRatio( 0.5f ),
299   decayCurve( SoundArchive::Sound3DInfo::DECAY_CURVE_LOG ),
300   dopplerFactor( 0 )
301 {
302 }
303 
304 } // namespace nw::snd
305 } // namespace nw
306 
307