1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_BasicSound.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: 31311 $
16  *---------------------------------------------------------------------------*/
17 
18 #include "precompiled.h"
19 
20 #include <nw/snd/snd_BasicSound.h>
21 #include <nw/snd/snd_BasicSoundPlayer.h>
22 #include <nw/snd/snd_SoundPlayer.h>
23 #include <nw/snd/snd_ExternalSoundPlayer.h>
24 #include <nw/snd/snd_SoundActor.h>
25 #include <cstring>  // std::memcpy
26 
27 namespace nw {
28 namespace snd {
29 namespace internal {
30 
31 NW_UT_RUNTIME_TYPEINFO_ROOT_DEFINITION(BasicSound);
32 
33 /*---------------------------------------------------------------------------*
34   Name:         BasicSound
35 
36   Description:  コンストラクタ
37 
38   Arguments:    None.
39 
40   Returns:      None.
41  *---------------------------------------------------------------------------*/
BasicSound()42 BasicSound::BasicSound()
43 : m_InitializeFlag(false)
44 {
45 }
46 
47 /*---------------------------------------------------------------------------*
48   Name:         Initialize
49 
50   Description:  初期化
51 
52   Arguments:    None.
53 
54   Returns:      None.
55  *---------------------------------------------------------------------------*/
Initialize()56 void BasicSound::Initialize()
57 {
58     driver::BasicSoundPlayer* basicPlayer = GetBasicSoundPlayerHandle();
59     DriverCommandManager& cmdmgr = DriverCommandManager::GetInstance();
60 
61     m_pPlayerHeap = NULL;
62     m_pGeneralHandle = NULL;
63     m_pTempGeneralHandle = NULL;
64     m_pSoundPlayer = NULL;
65     m_pSoundActor = NULL;
66     m_pExtSoundPlayer = NULL;
67     m_PlayerState = PLAYER_STATE_INIT;
68     m_Id = INVALID_ID;
69 
70     m_AmbientInfo.paramUpdateCallback = NULL;
71     m_AmbientInfo.argUpdateCallback = NULL;
72     m_AmbientInfo.argAllocatorCallback = NULL;
73     m_AmbientInfo.arg = NULL;
74     m_AmbientInfo.argSize = 0;
75 
76     m_PlayerState   = PLAYER_STATE_INIT;
77     m_PauseState    = PAUSE_STATE_NORMAL;
78     m_UnPauseFlag    = false;
79     m_StartFlag      = false;
80     m_StartedFlag    = false;
81     m_AutoStopFlag   = false;
82     m_FadeOutFlag    = false;
83     m_PlayerAvailableFlag = false;
84 
85     m_AutoStopCounter = 0;
86     m_UpdateCounter  = 0;
87 
88     m_FadeVolume.InitValue( 1.0f );
89     m_PauseFadeVolume.InitValue( 1.0f );
90 
91     m_InitVolume     = 1.0f;
92     m_ExtPitch       = 1.0f;
93     m_ExtPan         = 0.0f;
94     m_ExtMoveVolume.InitValue( 1.0f );
95     m_LpfFreq = 0.0f;
96     m_BiquadFilterType = BIQUAD_FILTER_TYPE_NONE;
97     m_BiquadFilterValue = 0.0f;
98     m_ExtSurroundPan = 0.0f;
99 
100     m_MainSend = 0.0f;
101     for ( int i=0; i<AUX_BUS_NUM; i++ )
102     {
103         m_FxSend[i] = 0.0f;
104     }
105 
106     // AmbientParam
107     m_AmbientParam.volume            = 1.0f;
108     m_AmbientParam.pitch             = 1.0f;
109     m_AmbientParam.pan               = 0.0f;
110     m_AmbientParam.span              = 0.0f;
111     m_AmbientParam.fxSend            = 0.0f;
112     m_AmbientParam.lpf               = 0.0f;
113     m_AmbientParam.biquadFilterValue = 0.0f;
114     m_AmbientParam.biquadFilterType  = 0;
115     m_AmbientParam.priority          = 0;
116 
117     m_ActorParam.Reset();
118 
119     DriverCommandPlayerInit* command =
120         cmdmgr.AllocCommand<DriverCommandPlayerInit>();
121     command->id = DRIVER_COMMAND_PLAYER_INIT;
122     command->player = basicPlayer;
123     command->availableFlagPtr = &m_PlayerAvailableFlag;
124     cmdmgr.PushCommand(command);
125 
126     m_InitializeFlag = true;
127 }
128 
SetPriority(int priority,int ambientPriority)129 void BasicSound::SetPriority( int priority, int ambientPriority )
130 {
131     NW_MINMAX_ASSERT( priority, PRIORITY_MIN, PRIORITY_MAX );
132 
133     m_Priority = static_cast<u8>(priority);
134     m_AmbientParam.priority = ambientPriority;
135 }
GetPriority(int * priority,int * ambientPriority) const136 void BasicSound::GetPriority( int* priority, int* ambientPriority ) const
137 {
138     if ( priority != NULL )
139     {
140         *priority = m_Priority;
141     }
142     if ( ambientPriority != NULL )
143     {
144         *ambientPriority = m_AmbientParam.priority;
145     }
146 }
147 
148 /*---------------------------------------------------------------------------*
149   Name:         Finalize
150 
151   Description:
152 
153   Arguments:    None.
154 
155   Returns:      None.
156  *---------------------------------------------------------------------------*/
Finalize()157 void BasicSound::Finalize()
158 {
159     if ( ! m_InitializeFlag ) return;
160 
161     // プレイヤーの停止
162     if ( m_StartedFlag )
163     {
164         DriverCommandManager& cmdmgr = DriverCommandManager::GetInstance();
165 
166         DriverCommandPlayer* command =
167             cmdmgr.AllocCommand<DriverCommandPlayer>();
168         command->id = DRIVER_COMMAND_PLAYER_STOP;
169         command->player = GetBasicSoundPlayerHandle();
170         command->flag = m_FadeOutFlag;
171         cmdmgr.PushCommand(command);
172     }
173     m_PlayerAvailableFlag = false;
174     m_PlayerState = PLAYER_STATE_STOP;
175 
176     // IDを無効化
177     SetId( INVALID_ID );
178 
179     // ハンドルとの切断
180     if ( IsAttachedGeneralHandle() )
181     {
182         DetachGeneralHandle();
183     }
184     if ( IsAttachedTempGeneralHandle() )
185     {
186         DetachTempGeneralHandle();
187     }
188     if ( IsAttachedTempSpecialHandle() )
189     {
190         DetachTempSpecialHandle();
191     }
192 
193     if ( m_pPlayerHeap != NULL ) {
194         m_pSoundPlayer->detail_FreePlayerHeap( this );
195     }
196 
197     // プレイヤーとの切断
198     if ( m_pSoundPlayer != NULL ) m_pSoundPlayer->detail_RemoveSound( this );
199     if ( m_pExtSoundPlayer != NULL ) m_pExtSoundPlayer->RemoveSound( this );
200 
201     if ( m_AmbientInfo.argAllocatorCallback != NULL )
202     {
203         m_AmbientInfo.argAllocatorCallback->detail_FreeAmbientArg( m_AmbientInfo.arg, this );
204         m_AmbientInfo.arg = NULL;
205     }
206 
207     // フラグクリア
208     m_StartedFlag = false;
209     m_FadeOutFlag = false;
210 
211     DriverCommandManager& cmdmgr = DriverCommandManager::GetInstance();
212     DriverCommandPlayer* command =
213         cmdmgr.AllocCommand<DriverCommandPlayer>();
214     command->id = DRIVER_COMMAND_PLAYER_FINALIZE;
215     command->player = GetBasicSoundPlayerHandle();
216     cmdmgr.PushCommand(command);
217 
218     m_InitializeFlag = false;
219 }
220 
221 /*---------------------------------------------------------------------------*
222   Name:         StartPrepared
223 
224   Description:  プリペアが完了していたらサウンドをスタートする
225 
226   Arguments:    None.
227 
228   Returns:      None.
229  *---------------------------------------------------------------------------*/
StartPrepared()230 void BasicSound::StartPrepared()
231 {
232     m_StartFlag = true;
233 }
234 
235 /*---------------------------------------------------------------------------*
236   Name:         Stop
237 
238   Description:  サウンド停止
239 
240   Arguments:    fadeFrames - フェードアウトフレーム
241 
242   Returns:      None.
243  *---------------------------------------------------------------------------*/
Stop(int fadeFrames)244 void BasicSound::Stop( int fadeFrames )
245 {
246     if ( ( fadeFrames == 0 ) ||
247          ( m_PlayerState != PLAYER_STATE_PLAY ) ||
248          ( m_PauseState == PAUSE_STATE_PAUSED )
249        )
250     {
251         Finalize();
252         return;
253     }
254 
255     int frames = static_cast<int>( fadeFrames * m_FadeVolume.GetValue() );
256     m_FadeVolume.SetTarget( 0.0f, frames );
257     SetPlayerPriority( PRIORITY_MIN );
258     m_AutoStopFlag = false;
259     m_PauseState = PAUSE_STATE_NORMAL;
260     m_UnPauseFlag = false;
261     m_FadeOutFlag = true;
262 }
263 
264 /*---------------------------------------------------------------------------*
265   Name:         Pause
266 
267   Description:  サウンドの一時停止または再開
268 
269   Arguments:    flag       - 一時停止または再開
270                 fadeFrames - フェードフレーム数
271 
272   Returns:      None.
273  *---------------------------------------------------------------------------*/
Pause(bool flag,int fadeFrames)274 void BasicSound::Pause( bool flag, int fadeFrames )
275 {
276     int frames;
277     if ( flag )
278     {
279         // 一時停止処理
280         switch( m_PauseState ) {
281         case PAUSE_STATE_NORMAL:
282         case PAUSE_STATE_PAUSING:
283         case PAUSE_STATE_UNPAUSING:
284             frames = static_cast<int>( fadeFrames * m_PauseFadeVolume.GetValue() );
285             if ( frames <= 0 ) frames = 1;
286             m_PauseFadeVolume.SetTarget( 0.0f, frames);
287             m_PauseState = PAUSE_STATE_PAUSING;
288             m_UnPauseFlag = false;
289             break;
290         case PAUSE_STATE_PAUSED:
291             // do nothing
292             return;
293         default:
294             NW_ASSERTMSG( false, "Unexpected pause state %d", m_PauseState );
295             return;
296         }
297     }
298     else
299     {
300         switch( m_PauseState ) {
301         case PAUSE_STATE_NORMAL:
302             // do nothing
303             return;
304         case PAUSE_STATE_PAUSING:
305         case PAUSE_STATE_UNPAUSING:
306         case PAUSE_STATE_PAUSED:
307             frames = static_cast<int>( fadeFrames * ( 1.0f - m_PauseFadeVolume.GetValue() ) );
308             if ( frames <= 0 ) frames = 1;
309             m_PauseFadeVolume.SetTarget( 1.0f, frames);
310             m_PauseState = PAUSE_STATE_UNPAUSING;
311             m_UnPauseFlag = true;
312             break;
313         default:
314             NW_ASSERTMSG( false, "Unexpected pause state %d", m_PauseState );
315             return;
316         }
317     }
318 }
319 
320 /*---------------------------------------------------------------------------*
321   Name:         SetAutoStopCounter
322 
323   Description:  サウンドが自動停止するまでのフレーム数を設定する
324 
325   Arguments:    frames - 自動停止するまでのフーレム数
326 
327   Returns:      None.
328  *---------------------------------------------------------------------------*/
SetAutoStopCounter(int frames)329 void BasicSound::SetAutoStopCounter( int frames )
330 {
331     m_AutoStopCounter = frames;
332     m_AutoStopFlag = ( frames > 0 );
333 }
334 
335 /*---------------------------------------------------------------------------*
336   Name:         FadeIn
337 
338   Description:  フェードイン
339 
340   Arguments:    frames - 変化フレーム数
341 
342   Returns:      None.
343  *---------------------------------------------------------------------------*/
FadeIn(int frames)344 void BasicSound::FadeIn( int frames )
345 {
346     if ( m_FadeOutFlag )
347     {
348         return;
349     }
350     if ( m_UpdateCounter == 0 )
351     {
352         m_FadeVolume.InitValue( 0.0f );
353         m_FadeVolume.SetTarget( 1.0f, frames );
354     }
355 }
356 
357 /*---------------------------------------------------------------------------*
358   Name:         IsPause
359 
360   Description:  一時停止中かどうかを取得
361 
362   Arguments:    None.
363 
364   Returns:      一時停止中ならば true
365  *---------------------------------------------------------------------------*/
IsPause() const366 bool BasicSound::IsPause() const
367 {
368     return (m_PauseState == PAUSE_STATE_PAUSING) || (m_PauseState == PAUSE_STATE_PAUSED);
369 }
370 
371 /*---------------------------------------------------------------------------*
372   Name:         Update
373 
374   Description:  サウンドの更新
375 
376   Arguments:    None.
377 
378   Returns:      None.
379  *---------------------------------------------------------------------------*/
Update()380 void BasicSound::Update()
381 {
382     // 自動停止カウンター処理
383     if ( m_AutoStopFlag )
384     {
385         if ( m_AutoStopCounter == 0 )
386         {
387             if ( ( m_PauseState == PAUSE_STATE_NORMAL ) ||
388                  ( m_PauseState == PAUSE_STATE_UNPAUSING ) )
389             {
390                 Stop( 0 );
391                 return;
392             }
393         }
394         else
395         {
396             --m_AutoStopCounter;
397         }
398     }
399 
400     // スタート前の処理
401     bool playerStartFlag = false;
402     if ( ! m_StartedFlag )
403     {
404         if ( ! m_StartFlag ) return;
405         if ( ! IsPrepared() ) return;
406 
407         m_PlayerState = PLAYER_STATE_PLAY;
408         playerStartFlag = true;
409     }
410 
411     // カウンタ更新
412     if ( m_PlayerState == PLAYER_STATE_PLAY )
413     {
414         if ( m_UpdateCounter < 0xffffffff ) m_UpdateCounter++;
415     }
416 
417     // 自動停止チェック
418     if ( m_PlayerAvailableFlag && GetBasicSoundPlayerHandle()->IsPlayFinished() )
419     {
420         Finalize();
421         return;
422     }
423 
424     // フェードフレームの更新
425     switch ( m_PauseState )
426     {
427     case PAUSE_STATE_PAUSING:
428         m_PauseFadeVolume.Update();
429         break;
430     case PAUSE_STATE_UNPAUSING:
431         m_PauseFadeVolume.Update();
432         UpdateMoveValue();
433         break;
434     case PAUSE_STATE_NORMAL:
435         UpdateMoveValue();
436         break;
437     }
438 
439     // 外部パラメータの更新
440     if ( m_AmbientInfo.argUpdateCallback != NULL )
441     {
442         m_AmbientInfo.argUpdateCallback->detail_UpdateAmbientArg(
443             m_AmbientInfo.arg,
444             this
445         );
446     }
447 
448     if ( m_AmbientInfo.paramUpdateCallback != NULL )
449     {
450         SoundAmbientParam ambientParam;
451         if ( m_UpdateCounter > 0 )
452         {
453             ambientParam.volume            = m_AmbientParam.volume;
454             ambientParam.pitch             = m_AmbientParam.pitch;
455             ambientParam.pan               = m_AmbientParam.pan;
456             ambientParam.span              = m_AmbientParam.span;
457             ambientParam.fxSend            = m_AmbientParam.fxSend;
458             ambientParam.lpf               = m_AmbientParam.lpf;
459             ambientParam.biquadFilterValue = m_AmbientParam.biquadFilterValue;
460             ambientParam.biquadFilterType  = m_AmbientParam.biquadFilterType;
461             ambientParam.priority          = m_AmbientParam.priority;
462             ambientParam.userData          = m_AmbientParam.userData;
463         }
464         else
465         {
466             ambientParam.userData = 0;
467         }
468         m_AmbientInfo.paramUpdateCallback->detail_UpdateAmbientParam(
469             m_AmbientInfo.arg,
470             m_Id,
471             &ambientParam
472         );
473 
474         m_AmbientParam.volume            = ambientParam.volume;
475         m_AmbientParam.pitch             = ambientParam.pitch;
476         m_AmbientParam.pan               = ambientParam.pan;
477         m_AmbientParam.span              = ambientParam.span;
478         m_AmbientParam.fxSend            = ambientParam.fxSend;
479         m_AmbientParam.lpf               = ambientParam.lpf;
480         m_AmbientParam.biquadFilterValue = ambientParam.biquadFilterValue;
481         m_AmbientParam.biquadFilterType  = ambientParam.biquadFilterType;
482         m_AmbientParam.priority          = ambientParam.priority;
483         m_AmbientParam.userData          = ambientParam.userData;
484 
485     }
486 
487     if ( m_pSoundActor != NULL )
488     {
489         m_ActorParam = m_pSoundActor->detail_GetActorParam();
490     }
491 
492     UpdateParam();
493 
494     // フェードアウト完了チェック
495     if ( m_FadeOutFlag && m_FadeVolume.IsFinished() )
496     {
497         m_FadeOutFlag = false;
498         Finalize();
499         return;
500     }
501 
502     DriverCommandManager& cmdmgr = DriverCommandManager::GetInstance();
503 
504     // プレイヤーの開始処理
505     if ( playerStartFlag )
506     {
507         DriverCommandPlayer* command =
508             cmdmgr.AllocCommand<DriverCommandPlayer>();
509         command->id = DRIVER_COMMAND_PLAYER_START;
510         command->player = GetBasicSoundPlayerHandle();
511         cmdmgr.PushCommand(command);
512 
513         m_StartedFlag = true;
514         m_StartFlag = false;
515     }
516 
517     // ポーズステータスチェック
518     if ( m_PauseState == PAUSE_STATE_PAUSING )
519     {
520         if ( m_PauseFadeVolume.IsFinished() )
521         {
522             DriverCommandPlayer* command =
523                 cmdmgr.AllocCommand<DriverCommandPlayer>();
524             command->id = DRIVER_COMMAND_PLAYER_PAUSE;
525             command->player = GetBasicSoundPlayerHandle();
526             command->flag = true;
527             cmdmgr.PushCommand(command);
528 
529             m_PauseState = PAUSE_STATE_PAUSED;
530         }
531     }
532     else if ( m_PauseState == PAUSE_STATE_UNPAUSING )
533     {
534         if ( m_PauseFadeVolume.IsFinished() )
535         {
536             m_PauseState = PAUSE_STATE_NORMAL;
537         }
538     }
539     if ( m_UnPauseFlag ) {
540         DriverCommandPlayer* command =
541             cmdmgr.AllocCommand<DriverCommandPlayer>();
542         command->id = DRIVER_COMMAND_PLAYER_PAUSE;
543         command->player = GetBasicSoundPlayerHandle();
544         command->flag = false;
545         cmdmgr.PushCommand(command);
546 
547         m_UnPauseFlag = false;
548     }
549 }
550 
UpdateMoveValue()551 void BasicSound::UpdateMoveValue()
552 {
553     m_FadeVolume.Update();
554     m_ExtMoveVolume.Update();
555 }
556 
UpdateParam()557 void BasicSound::UpdateParam()
558 {
559     // パラメータの更新
560     f32 volume = 1.0f;
561     volume *= m_InitVolume;
562     volume *= GetSoundPlayer()->GetVolume();
563     volume *= m_ExtMoveVolume.GetValue();
564     volume *= m_FadeVolume.GetValue();
565     volume *= m_PauseFadeVolume.GetValue();
566     volume *= m_AmbientParam.volume;
567     volume *= m_ActorParam.volume;
568 
569     f32 pan = 0.0f;
570     pan += m_ExtPan;
571     pan += m_AmbientParam.pan;
572     pan += m_ActorParam.pan;
573 
574     f32 surroundPan = 0.0f;
575     surroundPan += m_ExtSurroundPan;
576     surroundPan += m_AmbientParam.span;
577 
578     f32 pitch = 1.0f;
579     pitch *= m_ExtPitch;
580     pitch *= m_AmbientParam.pitch;
581     pitch *= m_ActorParam.pitch;
582 
583     f32 lpfFreq = m_LpfFreq;
584     lpfFreq += m_AmbientParam.lpf;
585     lpfFreq += GetSoundPlayer()->GetLpfFreq();
586 
587     int biquadFilterType = m_BiquadFilterType;
588     f32 biquadFilterValue = m_BiquadFilterValue;
589     if ( biquadFilterType == BIQUAD_FILTER_TYPE_NONE )
590     {
591         biquadFilterType = GetSoundPlayer()->GetBiquadFilterType();
592         biquadFilterValue = GetSoundPlayer()->GetBiquadFilterValue();
593 
594         if( biquadFilterType == BIQUAD_FILTER_TYPE_NONE )
595         {
596             biquadFilterType = m_AmbientParam.biquadFilterType;
597             biquadFilterValue = m_AmbientParam.biquadFilterValue;
598         }
599     }
600 
601     f32 mainSend = 0.0f;
602     mainSend += m_MainSend;
603     mainSend += GetSoundPlayer()->GetMainSend();
604 
605     f32 fxSend[AUX_BUS_NUM];
606     for ( int i=0; i<AUX_BUS_NUM; i++ )
607     {
608         fxSend[ i ] = 0.0f;
609         fxSend[ i ] += m_FxSend[ i ];
610         fxSend[ i ] += GetSoundPlayer()->GetFxSend( static_cast<AuxBus>( i ) );
611     }
612     fxSend[ 0 ] += m_AmbientParam.fxSend;   // AUX A のみ 3D サウンドの影響を受ける
613 
614     DriverCommandManager& cmdmgr = DriverCommandManager::GetInstance();
615 
616     DriverCommandPlayerParam* command =
617         cmdmgr.AllocCommand<DriverCommandPlayerParam>();
618     command->id = DRIVER_COMMAND_PLAYER_PARAM;
619     command->player = GetBasicSoundPlayerHandle();
620     command->volume = volume;
621     command->pitch = pitch;
622     command->pan = pan ;
623     command->surroundPan = surroundPan;
624     command->lpfFreq = lpfFreq;
625     command->biquadFilterType = biquadFilterType;
626     command->biquadFilterValue = biquadFilterValue;
627     command->mainSend = mainSend;
628     for ( int i=0; i<AUX_BUS_NUM; i++ )
629     {
630         command->fxSend[i] = fxSend[i];
631     }
632 
633     cmdmgr.PushCommand(command);
634 }
635 
636 /*---------------------------------------------------------------------------*
637   Name:         AttachPlayerHeap
638 
639   Description:  PlayerHeapを結びつける
640 
641   Arguments:    pHeap - 結びつけるPlayerHeap
642 
643   Returns:      None.
644   *---------------------------------------------------------------------------*/
AttachPlayerHeap(PlayerHeap * pHeap)645 void BasicSound::AttachPlayerHeap( PlayerHeap* pHeap )
646 {
647     NW_NULL_ASSERT( pHeap );
648     NW_ASSERT( m_pPlayerHeap == NULL );
649 
650     m_pPlayerHeap = pHeap;
651 }
652 
653 /*---------------------------------------------------------------------------*
654   Name:         DetachPlayerHeap
655 
656   Description:  PlayerHeapを切り離す
657 
658   Arguments:    heap - 切り離すPlayerHeap
659 
660   Returns:      None.
661   *---------------------------------------------------------------------------*/
DetachPlayerHeap(PlayerHeap * pHeap)662 void BasicSound::DetachPlayerHeap( PlayerHeap* pHeap )
663 {
664     NW_NULL_ASSERT( pHeap );
665     NW_ASSERT( pHeap == m_pPlayerHeap );
666     (void)pHeap;
667 
668     m_pPlayerHeap = NULL;
669 }
670 
671 /*---------------------------------------------------------------------------*
672   Name:         AttachSoundPlayer
673 
674   Description:  SoundPlayerを結びつける
675 
676   Arguments:    player - 結びつけるSoundPlayer
677 
678   Returns:      None.
679   *---------------------------------------------------------------------------*/
AttachSoundPlayer(SoundPlayer * player)680 void BasicSound::AttachSoundPlayer( SoundPlayer* player )
681 {
682     NW_NULL_ASSERT( player );
683     NW_ASSERT( m_pSoundPlayer == NULL );
684 
685     m_pSoundPlayer = player;
686 }
687 
688 /*---------------------------------------------------------------------------*
689   Name:         DetachSoundPlayer
690 
691   Description:  SoundPlayerを切り離す
692 
693   Arguments:    player - 切り離すSoundPlayer
694 
695   Returns:      None.
696   *---------------------------------------------------------------------------*/
DetachSoundPlayer(SoundPlayer * player)697 void BasicSound::DetachSoundPlayer( SoundPlayer* player )
698 {
699     NW_NULL_ASSERT( player );
700     NW_ASSERT( player == m_pSoundPlayer );
701     (void)player;
702 
703     m_pSoundPlayer = NULL;
704 }
705 
706 /*---------------------------------------------------------------------------*
707   Name:         AttachSoundActor
708 
709   Description:  SoundActorを結びつける
710 
711   Arguments:    actor - 結びつけるSoundActor
712 
713   Returns:      None.
714   *---------------------------------------------------------------------------*/
AttachSoundActor(SoundActor * actor)715 void BasicSound::AttachSoundActor( SoundActor* actor )
716 {
717     NW_NULL_ASSERT( actor );
718     NW_ASSERT( m_pSoundActor == NULL );
719 
720     m_pSoundActor = actor;
721 }
722 
723 /*---------------------------------------------------------------------------*
724   Name:         DetachSoundActor
725 
726   Description:  SoundActorを切り離す
727 
728   Arguments:    actor - 切り離すSoundActor
729 
730   Returns:      None.
731   *---------------------------------------------------------------------------*/
DetachSoundActor(SoundActor * actor)732 void BasicSound::DetachSoundActor( SoundActor* actor )
733 {
734     NW_NULL_ASSERT( actor );
735     NW_ASSERT( actor == m_pSoundActor );
736     (void)actor;
737 
738     m_pSoundActor = NULL;
739 }
740 
741 /*---------------------------------------------------------------------------*
742   Name:         AttachExternalSoundPlayer
743 
744   Description:  ExternalSoundPlayerを結びつける
745 
746   Arguments:    extPlayer - 結びつけるExternalSoundPlayer
747 
748   Returns:      None.
749   *---------------------------------------------------------------------------*/
AttachExternalSoundPlayer(ExternalSoundPlayer * extPlayer)750 void BasicSound::AttachExternalSoundPlayer( ExternalSoundPlayer* extPlayer )
751 {
752     NW_NULL_ASSERT( extPlayer );
753     NW_ASSERT( m_pExtSoundPlayer == NULL );
754 
755     m_pExtSoundPlayer = extPlayer;
756 }
757 
758 /*---------------------------------------------------------------------------*
759   Name:         DetachExternalSoundPlayer
760 
761   Description:  ExternalSoundPlayerを切り離す
762 
763   Arguments:    extPlayer - 切り離すExternalSoundPlayer
764 
765   Returns:      None.
766   *---------------------------------------------------------------------------*/
DetachExternalSoundPlayer(ExternalSoundPlayer * extPlayer)767 void BasicSound::DetachExternalSoundPlayer( ExternalSoundPlayer* extPlayer )
768 {
769     NW_NULL_ASSERT( extPlayer );
770     NW_ASSERT( extPlayer == m_pExtSoundPlayer );
771     (void)extPlayer;
772 
773     m_pExtSoundPlayer = NULL;
774 }
775 
776 /*---------------------------------------------------------------------------*
777   Name:         GetFadeFramesLeft
778 
779   Description:  フェードの残りフレームを取得する
780 
781   Arguments:    なし
782 
783   Returns:      残りフレーム
784  *---------------------------------------------------------------------------*/
GetRemainingFadeFrames() const785 int BasicSound::GetRemainingFadeFrames() const
786 {
787     return m_FadeVolume.GetRemainingCount();
788 }
789 
790 /*---------------------------------------------------------------------------*
791   Name:         GetPauseFadeFramesLeft
792 
793   Description:  一時停止フェードの残りフレームを取得する
794 
795   Arguments:    なし
796 
797   Returns:      残りフレーム
798  *---------------------------------------------------------------------------*/
GetRemainingPauseFadeFrames() const799 int BasicSound::GetRemainingPauseFadeFrames() const
800 {
801     return m_PauseFadeVolume.GetRemainingCount();
802 }
803 
804 
805 /*---------------------------------------------------------------------------*
806   Name:         SetPlayerPriority
807 
808   Description:  プレイヤープライオリティを設定する
809 
810   Arguments:    priority - プレイヤープライオリティ
811 
812   Returns:      None.
813  *---------------------------------------------------------------------------*/
SetPlayerPriority(int priority)814 void BasicSound::SetPlayerPriority( int priority )
815 {
816     NW_MINMAX_ASSERT( priority, PRIORITY_MIN, PRIORITY_MAX );
817     m_Priority = static_cast<u8>( priority );
818     if ( m_pSoundPlayer != NULL )
819     {
820         m_pSoundPlayer->detail_SortPriorityList( this );
821     }
822 
823     OnUpdatePlayerPriority();
824 }
825 
826 /*---------------------------------------------------------------------------*
827   Name:         SetInitialVolume
828 
829   Description:  初期ボリュームの設定
830 
831   Arguments:    volume - ボリューム
832 
833   Returns:      None.
834  *---------------------------------------------------------------------------*/
SetInitialVolume(f32 volume)835 void BasicSound::SetInitialVolume( f32 volume )
836 {
837     NW_ASSERT( volume >= 0.0f );
838     if ( volume < 0.0f ) volume = 0.0f;
839     m_InitVolume = volume;
840 }
GetInitialVolume() const841 f32 BasicSound::GetInitialVolume() const
842 {
843     return m_InitVolume;
844 }
845 
846 /*---------------------------------------------------------------------------*
847   Name:         SetVolume
848 
849   Description:  ボリュームの変更
850 
851   Arguments:    volume - ボリューム
852 
853   Returns:      None.
854  *---------------------------------------------------------------------------*/
SetVolume(f32 volume,int frames)855 void BasicSound::SetVolume( f32 volume, int frames )
856 {
857     NW_ASSERT( volume >= 0.0f );
858     if ( volume < 0.0f ) volume = 0.0f;
859     m_ExtMoveVolume.SetTarget( volume, frames );
860 }
GetVolume() const861 f32 BasicSound::GetVolume() const
862 {
863     return m_ExtMoveVolume.GetValue();
864 }
865 
866 /*---------------------------------------------------------------------------*
867   Name:         SetPitch
868 
869   Description:  音程の変更
870 
871   Arguments:    pitch - 変更する比率
872 
873   Returns:      None.
874  *---------------------------------------------------------------------------*/
SetPitch(f32 pitch)875 void BasicSound::SetPitch( f32 pitch )
876 {
877     NW_ASSERT( pitch >= 0.0f );
878     m_ExtPitch = pitch;
879 }
GetPitch() const880 f32 BasicSound::GetPitch() const
881 {
882     return m_ExtPitch;
883 }
884 
885 /*---------------------------------------------------------------------------*
886   Name:         SetPan
887 
888   Description:  パンの変更
889 
890   Arguments:    pan - パン
891 
892   Returns:      None.
893  *---------------------------------------------------------------------------*/
SetPan(f32 pan)894 void BasicSound::SetPan( f32 pan )
895 {
896     m_ExtPan = pan;
897 }
GetPan() const898 f32 BasicSound::GetPan() const
899 {
900     return m_ExtPan;
901 }
902 
903 /*---------------------------------------------------------------------------*
904   Name:         SetLpfFreq
905 
906   Description:  ローパスフィルタのカットオフ周波数の変更
907 
908   Arguments:    lpfFreq - カットオフ
909 
910   Returns:      None.
911  *---------------------------------------------------------------------------*/
SetLpfFreq(f32 lpfFreq)912 void BasicSound::SetLpfFreq( f32 lpfFreq )
913 {
914     m_LpfFreq = lpfFreq;
915 }
GetLpfFreq() const916 f32 BasicSound::GetLpfFreq() const
917 {
918     return m_LpfFreq;
919 }
920 
921 /*---------------------------------------------------------------------------*
922   Name:         SetBiquadFilter
923 
924   Description:  Biquadフィルタの設定
925 
926   Arguments:    type  - フィルタの種類 (0 - 127)
927                 value - フィルタの値 (0.0f - 1.0f)
928 
929   Returns:      None.
930  *---------------------------------------------------------------------------*/
SetBiquadFilter(int type,f32 value)931 void BasicSound::SetBiquadFilter( int type, f32 value )
932 {
933     m_BiquadFilterType = static_cast<u8>( type );
934     m_BiquadFilterValue = value;
935 }
GetBiquadFilter(int * type,f32 * value) const936 void BasicSound::GetBiquadFilter( int* type, f32* value ) const
937 {
938     if ( type != NULL )
939     {
940         *type = m_BiquadFilterType;
941     }
942     if ( value != NULL )
943     {
944         *value = m_BiquadFilterValue;
945     }
946 }
947 
948 /*---------------------------------------------------------------------------*
949   Name:         SetSurroundPan
950 
951   Description:  サラウンドパンの変更
952 
953   Arguments:    surroundPan - サラウンドパン
954 
955   Returns:      None.
956  *---------------------------------------------------------------------------*/
SetSurroundPan(f32 surroundPan)957 void BasicSound::SetSurroundPan( f32 surroundPan )
958 {
959     m_ExtSurroundPan = surroundPan;
960 }
GetSurroundPan() const961 f32 BasicSound::GetSurroundPan() const
962 {
963     return m_ExtSurroundPan;
964 }
965 
966 /*---------------------------------------------------------------------------*
967   Name:         SetMainSend
968 
969   Description:  メインセンドバスに送る音量を変更する
970 
971   Arguments:    send - 音量
972 
973   Returns:      None.
974  *---------------------------------------------------------------------------*/
SetMainSend(f32 send)975 void BasicSound::SetMainSend( f32 send )
976 {
977     m_MainSend = send;
978 }
GetMainSend() const979 f32 BasicSound::GetMainSend() const
980 {
981     return m_MainSend;
982 }
983 
984 /*---------------------------------------------------------------------------*
985   Name:         SetFxSend
986 
987   Description:  エフェクトセンドバスに送る音量を変更する
988 
989   Arguments:    bus - センドバス
990                 send - 音量
991 
992   Returns:      None.
993  *---------------------------------------------------------------------------*/
SetFxSend(AuxBus bus,f32 send)994 void BasicSound::SetFxSend( AuxBus bus, f32 send )
995 {
996     NW_MINMAXLT_ASSERT( bus, 0, AUX_BUS_NUM );
997     m_FxSend[bus] = send;
998 }
GetFxSend(AuxBus bus) const999 f32 BasicSound::GetFxSend( AuxBus bus ) const
1000 {
1001     if ( bus < 0 || bus >= AUX_BUS_NUM )
1002     {
1003         return 0.0f;
1004     }
1005     return m_FxSend[bus];
1006 }
1007 
1008 /*---------------------------------------------------------------------------*
1009   Name:         SetPanMode
1010 
1011   Description:  パンモードを変更
1012 
1013   Arguments:    panMode - パンモード
1014 
1015   Returns:      なし
1016  *---------------------------------------------------------------------------*/
SetPanMode(PanMode panMode)1017 void BasicSound::SetPanMode( PanMode panMode )
1018 {
1019     DriverCommandManager& cmdmgr = DriverCommandManager::GetInstance();
1020 
1021     DriverCommandPlayerPanParam* command =
1022         cmdmgr.AllocCommand<DriverCommandPlayerPanParam>();
1023     command->id = DRIVER_COMMAND_PLAYER_PANMODE;
1024     command->player = GetBasicSoundPlayerHandle();
1025     command->panMode = panMode;
1026     cmdmgr.PushCommand(command);
1027 }
1028 
1029 /*---------------------------------------------------------------------------*
1030   Name:         SetPanCurve
1031 
1032   Description:  パンカーブを変更
1033 
1034   Arguments:    panCurve - パンカーブ
1035 
1036   Returns:      なし
1037  *---------------------------------------------------------------------------*/
SetPanCurve(PanCurve panCurve)1038 void BasicSound::SetPanCurve( PanCurve panCurve )
1039 {
1040     DriverCommandManager& cmdmgr = DriverCommandManager::GetInstance();
1041 
1042     DriverCommandPlayerPanParam* command =
1043         cmdmgr.AllocCommand<DriverCommandPlayerPanParam>();
1044     command->id = DRIVER_COMMAND_PLAYER_PANCURVE;
1045     command->player = GetBasicSoundPlayerHandle();
1046     command->panCurve = panCurve;
1047     cmdmgr.PushCommand(command);
1048 }
1049 
SetFrontBypass(bool isFrontBypass)1050 void BasicSound::SetFrontBypass( bool isFrontBypass )
1051 {
1052     DriverCommandManager& cmdmgr = DriverCommandManager::GetInstance();
1053 
1054     DriverCommandPlayer* command =
1055         cmdmgr.AllocCommand<DriverCommandPlayer>();
1056     command->id = DRIVER_COMMAND_PLAYER_FRONTBYPASS;
1057     command->player = GetBasicSoundPlayerHandle();
1058     command->flag = isFrontBypass;
1059     cmdmgr.PushCommand(command);
1060 }
1061 
1062 /*---------------------------------------------------------------------------*
1063   Name:         SetAmbientInfo
1064 
1065   Description:  アンビエント情報を設定する
1066 
1067   Arguments:
1068 
1069   Returns:      None.
1070  *---------------------------------------------------------------------------*/
SetAmbientInfo(const AmbientInfo & ambientArgInfo)1071 void BasicSound::SetAmbientInfo( const AmbientInfo& ambientArgInfo )
1072 {
1073     // AmbientArg の複製を作成
1074     NW_NULL_ASSERT( ambientArgInfo.argAllocatorCallback );
1075     void* ambientArg =
1076         ambientArgInfo.argAllocatorCallback->detail_AllocAmbientArg( ambientArgInfo.argSize );
1077     if ( ambientArg == NULL )
1078     {
1079         NW_WARNING( ambientArg != NULL, "Failed to alloc AmbientArg." );
1080         return;
1081     }
1082     std::memcpy( ambientArg, ambientArgInfo.arg, ambientArgInfo.argSize );
1083 
1084     // AmbientInfo の設定
1085     m_AmbientInfo = ambientArgInfo;
1086     m_AmbientInfo.arg = ambientArg;
1087 }
1088 
1089 /*---------------------------------------------------------------------------*
1090   Name:         GetAmbientPriority [static]
1091 
1092   Description:  アンビエントプライオリティの取得
1093 
1094   Arguments:    ambientInfo - アンビエント情報
1095                 soundId - サウンドID
1096 
1097   Returns:      アンビエントプライオリティ値を返す
1098   *---------------------------------------------------------------------------*/
GetAmbientPriority(const AmbientInfo & ambientInfo,u32 soundId)1099 int BasicSound::GetAmbientPriority( const AmbientInfo& ambientInfo, u32 soundId )
1100 {
1101     if ( ambientInfo.paramUpdateCallback == NULL ) return 0;
1102 
1103     int priority = ambientInfo.paramUpdateCallback->detail_GetAmbientPriority(
1104         ambientInfo.arg,
1105         soundId
1106     );
1107 
1108     return priority;
1109 }
1110 
1111 // ハンドル関数
IsAttachedGeneralHandle()1112 bool BasicSound::IsAttachedGeneralHandle()
1113 {
1114     return m_pGeneralHandle != NULL;
1115 }
1116 
IsAttachedTempGeneralHandle()1117 bool BasicSound::IsAttachedTempGeneralHandle()
1118 {
1119     return m_pTempGeneralHandle != NULL;
1120 }
1121 
DetachGeneralHandle()1122 void BasicSound::DetachGeneralHandle()
1123 {
1124     m_pGeneralHandle->DetachSound();
1125 }
1126 
DetachTempGeneralHandle()1127 void BasicSound::DetachTempGeneralHandle()
1128 {
1129     m_pTempGeneralHandle->DetachSound();
1130 }
1131 
SetId(u32 id)1132 void BasicSound::SetId( u32 id )
1133 {
1134     m_Id = id;
1135 }
1136 
1137 } // namespace nw::snd::internal
1138 } // namespace nw::snd
1139 } // namespace nw
1140 
1141