1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_AnimEventPlayer.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_AnimEventPlayer.h>
21 #include <nw/snd/snd_SequenceSoundHandle.h>
22 #include <nw/ut/ut_Inlines.h>
23 
24 namespace nw {
25 namespace snd {
26 namespace internal {
27 
28 namespace
29 {
30 
ConvertSpeedF32ToS16(float speed)31 s16 ConvertSpeedF32ToS16( float speed )
32 {
33     static const s32 S16_SPEED_MIN = -32768;
34     static const s32 S16_SPEED_MAX = 32767;
35     s32 s32Speed = static_cast<s32>( speed * 100 );
36     s16 s16Speed = static_cast<s16>( ut::Clamp( s32Speed, S16_SPEED_MIN, S16_SPEED_MAX ) );
37     return s16Speed;
38 }
39 
40 } // anonymous namespace
41 
AnimEventPlayer()42 AnimEventPlayer::AnimEventPlayer()
43 : m_pEventInfo( NULL ),
44   m_IsStopWhenFinalize( false )
45 {
46 }
47 
~AnimEventPlayer()48 AnimEventPlayer::~AnimEventPlayer()
49 {
50     Finalize();
51 }
52 
InitParam(const AnimSoundFile::EventInfo & eventInfo,bool isStopWhenFinalize)53 void AnimEventPlayer::InitParam(
54         const AnimSoundFile::EventInfo& eventInfo, bool isStopWhenFinalize )
55 {
56     // 初期パラメータ設定
57     if ( eventInfo.volume < 128 )
58     {
59         m_Handle.SetVolume( static_cast<f32>( eventInfo.volume ) / 128.0f );
60     }
61     if ( eventInfo.pitch != 1.0f )
62     {
63         m_Handle.SetPitch( eventInfo.pitch );
64     }
65     m_pEventInfo = &eventInfo;
66     m_IsStopWhenFinalize =
67         isStopWhenFinalize && (! eventInfo.IsNotStopSoundWhenAnimationFinish());
68 }
69 
Finalize()70 void AnimEventPlayer::Finalize()
71 {
72     // 再生中のサウンドを停止する
73     if ( m_Handle.IsAttachedSound() && m_IsStopWhenFinalize )
74     {
75         m_Handle.Stop( 0 );
76     }
77 }
78 
StartEvent(const AnimSoundFile::EventInfo & eventInfo,SoundStartable & starter,bool isStopWhenFinalize)79 bool AnimEventPlayer::StartEvent(
80     const AnimSoundFile::EventInfo& eventInfo,
81     SoundStartable& starter,
82     bool isStopWhenFinalize
83 )
84 {
85     // 再生
86     if ( eventInfo.placeForSoundId == SoundArchive::INVALID_ID )
87     {
88         const char* soundLabel = eventInfo.GetSoundLabel();
89         if ( ! starter.StartSound( &m_Handle, soundLabel ).IsSuccess() )
90         {
91             return false;
92         }
93     }
94     else
95     {
96         if ( ! starter.StartSound( &m_Handle, eventInfo.placeForSoundId ).IsSuccess() )
97         {
98             return false;
99         }
100     }
101 
102     InitParam( eventInfo, isStopWhenFinalize );
103     return true;
104 }
105 
HoldEvent(const AnimSoundFile::EventInfo & eventInfo,SoundStartable & starter,bool isStopWhenFinalize)106 bool AnimEventPlayer::HoldEvent(
107     const AnimSoundFile::EventInfo& eventInfo,
108     SoundStartable& starter,
109     bool isStopWhenFinalize
110 )
111 {
112     // 再生
113     if ( eventInfo.placeForSoundId == SoundArchive::INVALID_ID )
114     {
115         const char* soundLabel = eventInfo.GetSoundLabel();
116         if ( ! starter.HoldSound( &m_Handle, soundLabel ).IsSuccess() )
117         {
118             return false;
119         }
120     }
121     else
122     {
123         if ( ! starter.HoldSound( &m_Handle, eventInfo.placeForSoundId ).IsSuccess() )
124         {
125             return false;
126         }
127     }
128 
129     // HoldSoundの自動停止をやめる
130     m_Handle.detail_GetAttachedSound()->SetAutoStopCounter( 0 );
131 
132     InitParam( eventInfo, isStopWhenFinalize );
133     return true;
134 }
135 
ForceStop()136 void AnimEventPlayer::ForceStop()
137 {
138     m_Handle.Stop( 0 );
139     m_pEventInfo = NULL;
140 }
141 
UpdateFrame()142 void AnimEventPlayer::UpdateFrame()
143 {
144     // 自動停止をチェック
145     if ( ! m_Handle.IsAttachedSound() )
146     {
147         m_pEventInfo = NULL;
148     }
149 }
150 
WritePlaySpeedToSequenceVariable(u8 sequenceVariableNo,f32 speed)151 void AnimEventPlayer::WritePlaySpeedToSequenceVariable( u8 sequenceVariableNo, f32 speed )
152 {
153     // ローカル変数反映
154     if ( sequenceVariableNo < 16 )
155     {
156         if ( ! m_Handle.IsAttachedSound() )
157         {
158             return;
159         }
160         SequenceSoundHandle handle( &m_Handle );
161         handle.WriteVariable( sequenceVariableNo, ConvertSpeedF32ToS16( speed ) );
162     }
163     // グローバル変数反映
164     else if ( sequenceVariableNo < 32 )
165     {
166         SequenceSoundHandle::WriteGlobalVariable(
167                 sequenceVariableNo - 16,
168                 ConvertSpeedF32ToS16( speed ) );
169     }
170 }
171 
172 } // namespace nw::snd::internal
173 } // namespace nw::snd
174 } // namespace nw
175 
176