1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_Sound3DActor.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_Sound3DActor.h>
21 
22 #include <nw/snd/snd_SoundArchivePlayer.h>
23 #include <nw/snd/snd_Sound3DManager.h>
24 #include <nw/snd/snd_SoundHandle.h>
25 #include <nw/snd/snd_Global.h>          // nw::snd::DECAY_CURVE_LOG
26 
27 namespace nw {
28 namespace snd {
29 
30 /*---------------------------------------------------------------------------*
31   Name:         Sound3DActor
32 
33   Description:  コンストラクタ
34 
35   Arguments:    player - サウンドアーカイブプレイヤー
36                 manager - 3Dサウンドマネージャー
37 
38   Returns:      None.
39  *---------------------------------------------------------------------------*/
Sound3DActor(SoundArchivePlayer & player,Sound3DManager & manager)40 Sound3DActor::Sound3DActor( SoundArchivePlayer& player, Sound3DManager& manager )
41 : m_p3dManager( NULL ),
42   m_pArchivePlayer( NULL ),
43   m_UserParam( 0 ),
44   m_Position( 0.0f, 0.0f, 0.0f ),
45   m_Velocity( 0.0f, 0.0f, 0.0f ),
46   m_ResetPositionFlag( true ),
47   m_IsInitialized( false ),
48   m_IsFinalized( true )
49 {
50     Initialize( player, manager );
51 }
52 
Sound3DActor()53 Sound3DActor::Sound3DActor()
54 : m_p3dManager( NULL ),
55   m_pArchivePlayer( NULL ),
56   m_UserParam( 0 ),
57   m_Position( 0.0f, 0.0f, 0.0f ),
58   m_Velocity( 0.0f, 0.0f, 0.0f ),
59   m_ResetPositionFlag( true ),
60   m_IsInitialized( false ),
61   m_IsFinalized( true )
62 {
63 }
64 
65 /*---------------------------------------------------------------------------*
66   Name:         ~Sound3DActor
67 
68   Description:  デストラクタ
69 
70   Arguments:    None.
71 
72   Returns:      None.
73  *---------------------------------------------------------------------------*/
~Sound3DActor()74 Sound3DActor::~Sound3DActor()
75 {
76     Finalize();
77 }
78 
Initialize(SoundArchivePlayer & player,Sound3DManager & manager)79 void Sound3DActor::Initialize( SoundArchivePlayer& player, Sound3DManager& manager )
80 {
81     if ( m_IsInitialized ) return;
82 
83     SoundActor::Initialize( player );
84     m_p3dManager = &manager;
85     m_pArchivePlayer = &player;
86 
87     m_IsInitialized = true;
88     m_IsFinalized = false;
89 }
90 
Finalize()91 void Sound3DActor::Finalize()
92 {
93     if ( m_IsFinalized ) return;
94 
95     // サウンドからの Update が来ないようにする
96     SoundActor::ForEachSound( ClearUpdateCallback );
97     SoundActor::Finalize();
98 
99     m_IsFinalized = true;
100     m_IsInitialized = false;
101     m_p3dManager = NULL;
102     m_pArchivePlayer = NULL;
103 }
104 
105 /*---------------------------------------------------------------------------*
106   Name:         SetupSound [virtual]
107 
108   Description:  再生の実装関数
109 
110   Arguments:    handle  - サウンドハンドル
111                 soundId - サウンドID
112                 startInfo - サウンド再生パラメータ
113                 setupArg - セットアップ引数
114 
115   Returns:      結果コード
116  *---------------------------------------------------------------------------*/
SetupSound(SoundHandle * handle,u32 soundId,const StartInfo * startInfo,void * setupArg)117 SoundStartable::StartResult Sound3DActor::SetupSound(
118     SoundHandle* handle,
119     u32 soundId,
120     const StartInfo* startInfo,
121     void* setupArg
122 )
123 {
124     if ( m_IsInitialized == false )
125     {
126         return StartResult( StartResult::START_ERR_NOT_AVAILABLE );
127     }
128 
129     // 初期パラメータ設定
130     Sound3DParam param;
131     param.position = m_Position;
132     param.velocity = m_Velocity;
133     param.actorUserParam = m_UserParam;
134     if ( m_pArchivePlayer != NULL )
135     {
136         const SoundArchive& soundArchive = m_pArchivePlayer->GetSoundArchive();
137 
138         SoundArchive::Sound3DInfo p;
139         if ( soundArchive.ReadSound3DInfo( soundId, &p ) )
140         {
141             param.ctrlFlag = p.flags;
142             param.decayRatio = p.decayRatio;
143             param.dopplerFactor = p.dopplerFactor;
144 
145             // decayCurve
146             switch ( p.decayCurve )
147             {
148             case SoundArchive::Sound3DInfo::DECAY_CURVE_LOG:
149                 param.decayCurve = nw::snd::DECAY_CURVE_LOG;
150                 break;
151             case SoundArchive::Sound3DInfo::DECAY_CURVE_LINEAR:
152                 param.decayCurve = nw::snd::DECAY_CURVE_LINEAR;
153                 break;
154             default:
155                 param.decayCurve = nw::snd::DECAY_CURVE_LOG;
156                 break;
157             }
158         }
159         param.soundUserParam = soundArchive.GetSoundUserParam( soundId );
160     }
161 
162     internal::BasicSound::AmbientInfo argInfo = {
163         m_p3dManager,
164         this,
165         m_p3dManager,
166         &param,
167         sizeof( param )
168     };
169 
170     SoundStartable::StartResult result = SoundActor::detail_SetupSoundWithAmbientInfo(
171         handle,
172         soundId,
173         startInfo,
174         &argInfo,
175         setupArg
176     );
177 
178     if ( handle->IsAttachedSound() )
179     {
180         handle->detail_GetAttachedSound()->SetPanCurve( PAN_CURVE_SINCOS );
181     }
182 
183     return result;
184 }
185 
186 /*---------------------------------------------------------------------------*
187   Name:         SetPosition
188 
189   Description:  アクターの位置を設定する
190 
191   Arguments:    position - 位置を表す3次元ベクトル
192 
193   Returns:      None.
194  *---------------------------------------------------------------------------*/
SetPosition(const nw::math::VEC3 & position)195 void Sound3DActor::SetPosition( const nw::math::VEC3& position )
196 {
197     if ( ! m_ResetPositionFlag ) {
198         nw::math::VEC3Sub( &m_Velocity, &position, &m_Position );
199     }
200     m_Position = position;
201     m_ResetPositionFlag = false;
202 }
203 
204 /*---------------------------------------------------------------------------*
205   Name:         ResetPosition
206 
207   Description:  アクターの位置情報をリセットする
208 
209   Arguments:    None.
210 
211   Returns:      None.
212  *---------------------------------------------------------------------------*/
ResetPosition()213 void Sound3DActor::ResetPosition()
214 {
215     m_ResetPositionFlag = true;
216     m_Position = m_Velocity = nw::math::VEC3( 0.0f, 0.0f, 0.0f );
217 }
218 
219 /*---------------------------------------------------------------------------*
220   Name:         SetVelocity
221 
222   Description:  アクターの速度を設定する
223 
224   Arguments:    velocity - 速度を表す3次元ベクトル
225 
226   Returns:      None.
227  *---------------------------------------------------------------------------*/
SetVelocity(const nw::math::VEC3 & velocity)228 void Sound3DActor::SetVelocity( const nw::math::VEC3& velocity )
229 {
230     m_Velocity = velocity;
231 }
232 
233 /*---------------------------------------------------------------------------*
234   Name:         Update
235 
236   Description:  アクターのパラメータを更新する
237 
238   Arguments:    arg - 引数
239                 sound - サウンドインスタンス
240 
241   Returns:      None.
242  *---------------------------------------------------------------------------*/
detail_UpdateAmbientArg(void * arg,const internal::BasicSound * sound)243 void Sound3DActor::detail_UpdateAmbientArg( void* arg, const internal::BasicSound* sound )
244 {
245     (void)sound;
246 
247     Sound3DParam* param = static_cast< Sound3DParam* >( arg );
248     param->position = m_Position;
249     param->velocity = m_Velocity;
250     param->actorUserParam = m_UserParam;
251 }
252 
253 /*---------------------------------------------------------------------------*
254   Name:         ClearUpdateCallback [static]
255 
256   Description:  アンビエント引数の更新コールバックのクリア
257 
258   Arguments:    handle - クリア対象のサウンドハンドル
259 
260   Returns:      None.
261   *---------------------------------------------------------------------------*/
ClearUpdateCallback(SoundHandle & handle)262 void Sound3DActor::ClearUpdateCallback( SoundHandle& handle )
263 {
264     if ( handle.IsAttachedSound() )
265     {
266         handle.detail_GetAttachedSound()->ClearAmbientArgUpdateCallback();
267     }
268 }
269 
270 } // namespace nw::snd
271 } // namespace nw
272 
273