// Sun.cpp: implementation of the WavePlay class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "WavePlay.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
WavePlay::WavePlay()
{
the3Flage = true;
the2Flage1 = false;
the2Flage2 = false;
mPlayState.isPlaied = false;
mPlayState.isLoopFileEnd = false;
mPlayState.nowisfileno = 0;
once = false;
bPlay = false;
DataSize = 0;
DataSize1 = 0;
hData = 0;
hData1 = 0;
hWaveOut = NULL;
lpData = NULL;
lpData1 = NULL;
OpenFlage = 0;
bPause = false;
bPlay2Pause = false;
bPlay3Pause = false;
}
WavePlay::~WavePlay()
{
if (1 == OpenFlage)//无重放的文件
{
if (hWaveOut)
{
waveOutReset(hWaveOut);
waveOutClose(hWaveOut);
waveOutUnprepareHeader(hWaveOut, &WaveHead, sizeof(WAVEHDR));
}
if (lpData)
{
LocalUnlock(hData);
lpData = NULL;
}
if (hData)
{
GlobalFree(hData);
hData = NULL;
}
OpenFlage = 0;
DataSize = 0;
}
else if (2 == OpenFlage)//有重放的文件
{
if (hWaveOut)
{
waveOutReset(hWaveOut);
waveOutClose(hWaveOut);
waveOutUnprepareHeader(hWaveOut, &WaveHead, sizeof(WAVEHDR));
}
if (lpData1)
{
LocalUnlock(hData1);
lpData1 = NULL;
}
if (hData1)
{
GlobalFree(hData1);
hData1 = NULL;
}
OpenFlage = 0;
DataSize1 = 0;
}
once = false;
bPlay = false;
Sleep(100);
}
int WavePlay::Open(LPCTSTR name, bool Loop)
{
if (OpenFlage)//如果已经打开过了就返回 OpenFlage=1 or OpenFlage=3
{
TRACE("FILE IS OPEND!\n");
return -1;
}
HMMIO hMmio = NULL;
MMCKINFO pinfo;
MMCKINFO cinfo;
if (hMmio)
Close();
//打开WAV文件,返回一个HMMIO句柄
hMmio = mmioOpen((LPTSTR) name, NULL, MMIO_READ);
if (!hMmio)
return FALSE;
if (Loop)
OpenFlage = 3;//第三种情况 单个循环
else
OpenFlage = 1;//不循环情况
//查找父块“wave”
pinfo.fccType = mmioFOURCC('W', 'A', 'V', 'E');
if (mmioDescend(hMmio, &pinfo, NULL, MMIO_FINDRIFF))
goto FALSE_END;
//查找子块"fmt" parent"riff"
cinfo.ckid = mmioFOURCC('f', 'm', 't', ' ');
if (mmioDescend(hMmio, &cinfo, NULL, MMIO_FINDCHUNK))
goto FALSE_END;
mmioRead(hMmio, (LPSTR) & pFormat, sizeof(PCMWAVEFORMAT));//cinfo.cksize
if (pFormat.wf.wFormatTag != WAVE_FORMAT_PCM)
goto FALSE_END;
//跳入块"FMT"
mmioAscend(hMmio, &cinfo, 0);
//查找数据块
cinfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
if (mmioDescend(hMmio, &cinfo, &pinfo, MMIO_FINDCHUNK))
goto FALSE_END;
DataSize = cinfo.cksize;
//读取数据
hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, DataSize);
lpData = (UINT32 *) GlobalLock(hData);
if (!hData || !lpData)
goto FALSE_END;
if (mmioRead(hMmio, (HPSTR) lpData, DataSize) != (LRESULT) DataSize)
goto FALSE_END;
//close and return
mmioClose(hMmio, MMIO_FHOPEN);
return TRUE;
FALSE_END:
if (hMmio)
mmioClose(hMmio, MMIO_FHOPEN);
if (lpData)
LocalUnlock(hData);
if (hData)
GlobalFree(hData);
memset(this, 0, sizeof(WavePlay));
return FALSE;
}
int WavePlay::Play(bool bStartplay)
{
if (1 == OpenFlage)//单个文件不重复播放
{
Play1(bStartplay);
return 1;
}
else if (2 == OpenFlage)//后一个文件重复播放
{
Play2(bStartplay);
return 2;
}
else if (3 == OpenFlage)//单个文件重复播放
{
Play3(bStartplay);
return 3;
}
else
{
TRACE("没有打开文件, OpenFlag = %d\n", OpenFlage);
return -1;
}
}
int WavePlay::Stop()
{
//if(1 == )
return !waveOutReset(hWaveOut);
}
int WavePlay::Close(int no)
{
if (1 == no)//重复中不重复的文件
{
if (lpData1 != NULL)
{
if (hWaveOut)
{
waveOutReset(hWaveOut);
waveOutClose(hWaveOut);
waveOutUnprepareHeader(hWaveOut, &WaveHead, sizeof(WAVEHDR));
}
}
if (lpData1)
{
LocalUnlock(hData1);
lpData1 = NULL;
}
if (hData1)
{
GlobalFree(hData1);
hData1 = NULL;
}
return 1;
}
if (2 == no)//要重复的第二个文件
{
if (hWaveOut)
{
waveOutReset(hWaveOut);
waveOutClose(hWaveOut);
waveOutUnprepareHeader(hWaveOut, &WaveHead, sizeof(WAVEHDR));
}
if (lpData1)
{
LocalUnlock(hData1);
lpData1 = NULL;
}
if (hData1)
{
GlobalFree(hData1);
hData1 = NULL;
}
if (lpData)
{
LocalUnlock(hData);
lpData = NULL;
}
if (hData)
{
GlobalFree(hData);
hData = NULL;
}
return 2;
}
//if(3 == no || 1 == no)
{
if (hWaveOut)
{
waveOutReset(hWaveOut);
waveOutClose(hWaveOut);
waveOutUnprepareHeader(hWaveOut, &WaveHead, sizeof(WAVEHDR));
}
if (lpData)
{
LocalUnlock(hData);
lpData = NULL;
}
if (hData)
{
GlobalFree(hData);
hData = NULL;
}
return 3;
}
return 0;
}
int WavePlay::Open(LPCTSTR name1, LPCTSTR name2, bool Loop)
{
if (OpenFlage == 2)//如果已经打开过了就返回
{
TRACE("FILE IS OPEND!\n");
return 2;
}
HMMIO hMmio = NULL;
MMCKINFO pinfo;
MMCKINFO cinfo;
if (hMmio)
Close();
//打开WAV文件1,返回一个HMMIO句柄
hMmio = mmioOpen((LPTSTR) name1, NULL, MMIO_READ);
if (!hMmio)
return FALSE;
OpenFlage = 1;
//查找父块“wave”
pinfo.fccType = mmioFOURCC('W', 'A', 'V', 'E');
if (mmioDescend(hMmio, &pinfo, NULL, MMIO_FINDRIFF))
goto FALSE_END1;
//查找子块"fmt" parent"riff"
cinfo.ckid = mmioFOURCC('f', 'm', 't', ' ');
if (mmioDescend(hMmio, &cinfo, NULL, MMIO_FINDCHUNK))
goto FALSE_END1;
mmioRead(hMmio, (LPSTR) & pFormat, sizeof(PCMWAVEFORMAT));//cinfo.cksize
if (pFormat.wf.wFormatTag != WAVE_FORMAT_PCM)
goto FALSE_END1;
//跳入块"FMT"
mmioAscend(hMmio, &cinfo, 0);
//查找数据块
cinfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
if (mmioDescend(hMmio, &cinfo, &pinfo, MMIO_FINDCHUNK))
goto FALSE_END1;
DataSize = cinfo.cksize;
//读取数据
hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, DataSize);
lpData = (UINT32 *) GlobalLock(hData);
if (!hData || !lpData)
goto FALSE_END1;
if (mmioRead(hMmio, (HPSTR) lpData, DataSize) != (LRESULT) DataSize)
goto FALSE_END1;
//close and return
mmioClose(hMmio, MMIO_FHOPEN);
//+++++++++++++++++++++++++++
//打开下一个WAV文件,返回一个HMMIO句柄
hMmio = mmioOpen((LPTSTR) name2, NULL, MMIO_READ);
if (!hMmio)
return FALSE;
OpenFlage = 2;
//查找父块“wave”
pinfo.fccType = mmioFOURCC('W', 'A', 'V', 'E');
if (mmioDescend(hMmio, &pinfo, NULL, MMIO_FINDRIFF))
goto FALSE_END2;
//查找子块"fmt" parent"riff"
cinfo.ckid = mmioFOURCC('f', 'm', 't', ' ');
if (mmioDescend(hMmio, &cinfo, NULL, MMIO_FINDCHUNK))
goto FALSE_END2;
mmioRead(hMmio, (LPSTR) & pFormat1, sizeof(PCMWAVEFORMAT));//cinfo.cksize
if (pFormat1.wf.wFormatTag != WAVE_FORMAT_PCM)
goto FALSE_END2;
//跳入块"FMT"
mmioAscend(hMmio, &cinfo, 0);
//查找数据块
cinfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
if (mmioDescend(hMmio, &cinfo, &pinfo, MMIO_FINDCHUNK))
goto FALSE_END2;
DataSize1 = cinfo.cksize;
//读取数据
hData1 = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, DataSize1);
lpData1 = (UINT32 *) GlobalLock(hData1);
if (!hData1 || !lpData1)
goto FALSE_END2;
if (mmioRead(hMmio, (HPSTR) lpData1, DataSize1) != (LRESULT) DataSize1)
goto FALSE_END2;
//close and return
mmioClose(hMmio, MMIO_FHOPEN);
return TRUE;
FALSE_END1:
if (hMmio)
mmioClose(hMmio, MMIO_FHOPEN);
if (lpData1)
LocalUnlock(hData1);
if (hData1)
GlobalFree(hData1);
memset(this, 0, sizeof(WavePlay));
return FALSE;
FALSE_END2:
if (hMmio)
mmioClose(hMmio, MMIO_FHOPEN);
if (lpData1)
LocalUnlock(hData1);
if (hData1)
GlobalFree(hData1);
memset(this, 0, sizeof(WavePlay));
return FALSE;