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