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