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