13#pragma comment(lib, "winmm.lib")
35 CWavePlayer(
void) : m_bankNumber(0), m_hWave(NULL), m_currentBank(0), m_pBuffers(NULL), m_pSyncBank(NULL), m_hasBuffer(false)
48 if ( m_hWave != NULL )
50 ::waveOutReset(m_hWave);
51 ::waveOutClose(m_hWave);
54 if ( m_pBuffers != NULL )
59 if ( m_pSyncBank != NULL )
76 MMRESULT
Open(
const WAVEFORMATEX& format,
int bankNumber, UINT bankSize)
79 m_bankNumber = bankNumber;
81 MMRESULT mr = ::waveOutOpen(&m_hWave, WAVE_MAPPER, &format,
82 reinterpret_cast<DWORD_PTR
>(ms_WaveCallback),
83 reinterpret_cast<DWORD_PTR
>(m_pSyncBank), CALLBACK_FUNCTION);
84 if ( mr == MMSYSERR_NOERROR )
86 m_pBuffers =
new CBuffer[m_bankNumber];
87 loop ( i, m_bankNumber )
89 mr = m_pBuffers[i].Init(m_hWave, bankSize);
90 if ( mr != MMSYSERR_NOERROR )
97 if ( mr != MMSYSERR_NOERROR )
114 MMRESULT
Write(
bool& _isPlayed,
const BYTE* pData,
size_t size)
116 if ( m_bankNumber == 0 ) {
return MMSYSERR_ERROR; }
126 MMRESULT r = m_pBuffers[m_currentBank].Write(isPlayed, nWritten, pData, size);
127 if ( r != MMSYSERR_NOERROR )
134 m_currentBank = (m_currentBank + 1) % m_bankNumber;
139 return MMSYSERR_NOERROR;
150 MMRESULT r = MMSYSERR_NOERROR;
151 if ( m_bankNumber != 0 )
155 r = m_pBuffers[m_currentBank].Flush();
158 m_currentBank = (m_currentBank + 1) % m_bankNumber;
168 loop ( i, m_bankNumber )
170 m_pBuffers[i].Clear();
178 ::waveOutPause(m_hWave);
184 ::waveOutRestart(m_hWave);
190 ::waveOutReset(m_hWave);
200 DWORD lvol = vol * 512;
201 DWORD rvol = vol * 512;
202 ::waveOutSetVolume(m_hWave, MAKELONG(lvol, rvol));
212 ::waveOutGetVolume(m_hWave, &vol);
213 return LOWORD(vol) / 512;
225 CBuffer(
void) : m_hWave(NULL), m_sentBytes(0)
232 if ( m_hdr.lpData != NULL )
234 ::waveOutUnprepareHeader(m_hWave, &m_hdr,
sizeof(WAVEHDR));
235 ::LocalFree(m_hdr.lpData);
246 MMRESULT Init(HWAVEOUT hWave, UINT size)
251 m_hdr.lpData =
static_cast<LPSTR
>(::LocalAlloc(LMEM_FIXED, size));
252 if ( m_hdr.lpData == NULL )
254 return MMSYSERR_ERROR;
256 m_hdr.dwBufferLength = size;
257 return ::waveOutPrepareHeader(hWave, &m_hdr,
sizeof(WAVEHDR));
262 size_t s = m_hdr.dwBufferLength;
263 m_hdr.dwBufferLength = m_sentBytes;
264 MMRESULT r = ::waveOutWrite(m_hWave, &m_hdr,
sizeof(WAVEHDR));
266 m_hdr.dwBufferLength = s;
283 MMRESULT
Write(
bool& _isPlayed,
size_t& _writtenSize,
const BYTE* pData,
size_t size)
285 _writtenSize = min(m_hdr.dwBufferLength - m_sentBytes, size);
286 ::CopyMemory(m_hdr.lpData + m_sentBytes, pData, _writtenSize);
287 m_sentBytes += _writtenSize;
288 if ( m_sentBytes == m_hdr.dwBufferLength )
292 return ::waveOutWrite(m_hWave, &m_hdr,
sizeof(WAVEHDR));
295 return MMSYSERR_NOERROR;
299 static void CALLBACK ms_WaveCallback(HWAVEOUT hWave, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
301 if ( uMsg == WOM_DONE )
#define loop(VAR, CNT)
loop構文.
virtual bool Lock(DWORD dwTime=INFINITE) const
[排他] ロック
virtual void Unlock(void) const
[排他] アンロック
MMRESULT Flush(void)
[書込] 再生
MMRESULT Write(bool &_isPlayed, const BYTE *pData, size_t size)
[書込] 再生
void Pause(void)
[処理] 一時停止
void SetVolume(int vol)
[設定] ボリューム設定.
MMRESULT Open(const WAVEFORMATEX &format, int bankNumber, UINT bankSize)
[初期] オープン
void Restart(void)
[処理] 再スタート
int GetVolume(void) const
[取得] ボリューム取得.
void Zero(V &value)
[設定] ゼロクリア.