1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_WaveSoundPlayer.h
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: 22284 $
14  *---------------------------------------------------------------------------*/
15 
16 /**
17  * :include nw/snd/snd_WaveSoundPlayer.h
18  *
19  * @file snd_WaveSoundPlayer.h
20  */
21 
22 #ifndef NW_SND_WAVE_SOUND_PLAYER_H_
23 #define NW_SND_WAVE_SOUND_PLAYER_H_
24 
25 #include <nw/snd/snd_BasicSoundPlayer.h>
26 #include <nw/snd/snd_DisposeCallback.h>
27 #include <nw/snd/snd_CurveLfo.h>
28 #include <nw/snd/snd_WaveSoundFileReader.h>   // WaveSoundInfo, WaveSoundNoteInfo
29 #include <nw/snd/snd_Channel.h>
30 #include <nw/snd/snd_SoundThread.h>
31 
32 namespace nw {
33 namespace snd {
34 
35 //---------------------------------------------------------------------------
36 //! @brief    ウェーブサウンドデータのパラメータセットです。
37 //!
38 //!           この情報は @ref WaveSoundHandle::ReadWaveSoundDataInfo から取得できます。
39 //!
40 //! @see WaveSoundHandle::ReadWaveSoundDataInfo
41 //!
42 //! @date 2010/03/08 loopStart と loopEnd の型を変更 (unsigned long → u32)
43 //! @date 2010/01/22 初版
44 //---------------------------------------------------------------------------
45 struct WaveSoundDataInfo
46 {
47     //---------------------------------------------------------------------------
48     //! @brief    ウェーブサウンドデータがループするなら true、
49     //!           終端で終了するなら false となります。
50     //---------------------------------------------------------------------------
51     bool loopFlag;
52 
53     //---------------------------------------------------------------------------
54     //! @brief    ウェーブサウンドデータのサンプリングレートです。
55     //---------------------------------------------------------------------------
56     int sampleRate;
57 
58     //---------------------------------------------------------------------------
59     //! @brief    ウェーブサウンドデータがループする時のループ開始位置を、
60     //!           ウェーブサウンドの先頭からのサンプル数で表します。
61     //---------------------------------------------------------------------------
62     u32 loopStart;
63 
64     //---------------------------------------------------------------------------
65     //! @brief    ウェーブサウンドデータがループする時のループ終了位置を、
66     //!           ウェーブサウンドの先頭からのサンプル数で表します。
67     //!           ループしない時は、データの終端をサンプル数で表します。
68     //!           (再生される最後のサンプルの次のサンプルを指します)
69     //---------------------------------------------------------------------------
70     u32 loopEnd;
71 };
72 
73 namespace internal {
74 namespace driver {
75 
76 class WaveSoundPlayer : public BasicSoundPlayer, public DisposeCallback, public SoundThread::PlayerCallback
77 {
78     /* ------------------------------------------------------------------------
79             constant variable
80        ------------------------------------------------------------------------ */
81   public:
82     static const int PAUSE_RELEASE_VALUE    = 127;
83     static const int MUTE_RELEASE_VALUE     = 127;
84     static const int DEFAULT_PRIORITY       = 64;
85 
86     enum StartOffsetType
87     {
88         START_OFFSET_TYPE_SAMPLE,
89         START_OFFSET_TYPE_MILLISEC
90     };
91 
92     class WaveSoundCallback
93     {
94       public:
~WaveSoundCallback()95         virtual ~WaveSoundCallback() {}
96 
97         virtual bool GetWaveSoundData(
98             WaveSoundInfo* info,
99             WaveSoundNoteInfo* noteInfo,
100             WaveInfo* waveData,
101             const void* waveSoundData,
102             int index,
103             int noteIndex,
104             u32 callbackData
105         ) const = 0;
106     };
107 
108     /* ------------------------------------------------------------------------
109             class member
110        ------------------------------------------------------------------------ */
111   public:
112     WaveSoundPlayer();
113 
114     virtual void Initialize();
115     virtual void Finalize();
116 
117     bool Prepare(
118         const void* waveSoundBase,
119         int index,
120         StartOffsetType startOffsetType,
121         int startOffset,
122         const WaveSoundCallback* callback,
123         u32 callbackData
124     );
125     virtual void Start();
126     virtual void Stop();
127     virtual void Pause( bool flag );
128 
129     //------------------------------------------------------------------
130     // プレイヤーパラメータ
131 
132     void SetPanRange( float panRange );
133     void SetChannelPriority( int priority );
134     void SetReleasePriorityFix( bool fix );
135 
GetPanRange()136     float GetPanRange() const { return m_PanRange; }
GetChannelPriority()137     int GetChannelPriority() const { return m_Priority; }
138 
139     // invalidate
140 
141     virtual void InvalidateData( const void* start, const void* end );
142 
143     // info
144 
145     bool ReadWaveSoundDataInfo( WaveSoundDataInfo* info ) const;
146     s32 GetPlaySamplePosition() const;
147 
148   public:
DebugUpdate()149     void DebugUpdate() { if ( m_ActiveFlag ) { Update(); } }
150 
151   protected:
OnUpdateFrameSoundThread()152     virtual void OnUpdateFrameSoundThread() { Update(); }
OnShutdownSoundThread()153     virtual void OnShutdownSoundThread() { Stop(); }
154 
155   private:
156     bool m_WavePlayFlag; // チャンネルスタートしたかどうか
157     bool m_ReleasePriorityFixFlag;
158 
159     f32 m_PanRange;
160     u8 m_Priority;
161 
162     const WaveSoundCallback* m_pCallback;
163     u32 m_CallbackData;
164 
165     const void* m_pWaveSoundData;
166     int m_WaveSoundIndex;
167     StartOffsetType m_StartOffsetType;
168     int m_StartOffset;
169 
170     CurveLfoParam m_LfoParam;
171 
172     WaveSoundInfo m_WaveSoundInfo;
173 
174     Channel* m_pChannel;
175 
176     void FinishPlayer();
177     void Update();
IsChannelActive()178     bool IsChannelActive() const { return ( m_pChannel != NULL ) && m_pChannel->IsActive(); }
179     bool StartChannel( const WaveSoundCallback* callback, u32 callbackData );
180     void CloseChannel();
181     void UpdateChannel();
GetWaveSoundDataAddress()182     const void* GetWaveSoundDataAddress() const { return m_pWaveSoundData; }
183     static void ChannelCallbackFunc(
184         Channel* dropChannel,
185         Channel::ChannelCallbackStatus status,
186         u32 userData
187     );
188 };
189 
190 } // namespace nw::snd::internal::driver
191 } // namespace nw::snd::internal
192 } // namespace nw::snd
193 } // namespace nw
194 
195 
196 #endif /* NW_SND_WAVE_SOUND_PLAYER_H_ */
197 
198