WaveBase.cpp
资源名称:AudioWave.rar [点击查看]
上传用户:huifengb
上传日期:2007-12-27
资源大小:334k
文件大小:6k
源码类别:
多媒体
开发平台:
Visual C++
- // WaveBass.cpp : implementation file
- //
- #include <mmsystem.h>
- #include "stdafx.h"
- #include "WaveBase.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CWaveBass
- IMPLEMENT_DYNCREATE(CWaveBase, CCmdTarget)
- CWaveBase::CWaveBase()
- {
- m_Readhmmio=NULL;
- m_bFileExist=FALSE;
- }
- CWaveBase::~CWaveBase()
- {
- if(m_bFileExist)
- {
- mmioClose(m_Readhmmio,0);
- }
- }
- BEGIN_MESSAGE_MAP(CWaveBase, CCmdTarget)
- //{{AFX_MSG_MAP(CWaveBass)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CWaveBass message handlers
- void CWaveBase::Wave_SaveWaveData(char*p, DWORD dataSize)
- {
- mmioWrite(m_Savehmmio,p,dataSize);
- }
- /*
- The mmioWrite function writes a specified number of
- bytes to a file opened by using the mmioOpen function.
- LONG mmioWrite(
- HMMIO hmmio,
- char _huge* pch,
- LONG cch
- );
- Parameters
- hmmio
- File handle of the file.
- pch
- Address of the buffer to be written to the file.
- cch
- Number of bytes to write to the file.
- Return Values
- Returns the number of bytes actually written. If there is an error writing to the file, the return value is -1.
- */
- void CWaveBase::Wave_ReadWaveData(char *p, DWORD &dataSize)
- {
- mmioSeek(m_Readhmmio,m_ReadFileOffset,SEEK_SET);
- dataSize=mmioRead(m_Readhmmio,(HPSTR)p,dataSize);
- m_ReadFileOffset+=dataSize;
- }
- /*
- function changes the current file position in a file opened by using the mmioOpen function.
- LONG mmioSeek( HMMIO hmmio, LONG lOffset, int iOrigin );
- Parameters hmmio File handle of the file to seek in.
- lOffset Offset to change the file position.
- iOrigin Flags indicating how the offset specified by
- lOffset is interpreted
- */
- /*
- mmioRead function reads a specified number of bytes from
- a file opened by using the mmioOpen function.
- LONG mmioRead( HMMIO hmmio, HPSTR pch, LONG cch );
- Parameters
- hmmio File handle of the file to be read.
- pch Address of a buffer to contain the data read from the file.
- cch Number of bytes to read from the file.
- */
- void CWaveBase::Wave_CloseSaveFile()
- {
- mmioAscend(m_Savehmmio,&m_mmckinfoData,0);
- //调mmioAscend函数回填文件头
- mmioAscend(m_Savehmmio,&m_mmckinfoWave,0);
- mmioClose(m_Savehmmio,0l);
- }
- /*
- * The mmioClose function closes a file that was opened by using the mmioOpen function.
- * MMRESULT mmioClose( HMMIO hmmio, UINT wFlags );
- * Parameters
- * hmmio File handle of the file to close.
- * wFlags Flags for the close operation.
- */
- BOOL CWaveBase::Wave_PrepareSaveFile(CString mFileName)
- {
- //创建声音文件
- m_Savehmmio=mmioOpen((char *)LPCTSTR(mFileName),
- NULL, MMIO_CREATE|MMIO_ALLOCBUF|MMIO_WRITE);
- if (NULL == m_Savehmmio)
- {
- AfxMessageBox("文件名或路径错误");
- return (-1);
- }
- //写声音文件头
- m_mmckinfoWave.fccType=mmioFOURCC('W','A','V','E');
- m_mmckinfoWave.cksize=0l;
- mmioCreateChunk(m_Savehmmio,&m_mmckinfoWave,MMIO_CREATERIFF);
- //创建并初始化fmt子块
- m_mmckinfoFmt.ckid=mmioFOURCC('f','m','t',' ');
- m_mmckinfoFmt.cksize=0l;
- mmioCreateChunk(m_Savehmmio,&m_mmckinfoFmt,0);
- mmioWrite(m_Savehmmio,(char _huge *)(&m_WaveFormat),(LONG)sizeof(WAVEFORMATEX));
- mmioAscend(m_Savehmmio,&m_mmckinfoFmt,0);
- // 创建fact块
- // m_mmckinfoFact.ckid=mmioFOURCC('f','a','c','t');
- // m_mmckinfoFact.cksize=4;
- // mmioCreateChunk(m_Savehmmio,&m_mmckinfoFact,0);
- // mmioWrite(m_Savehmmio,(char _huge *)&m_dwDataSamples,sizeof(DWORD));
- //创建data块
- m_mmckinfoData.ckid=mmioFOURCC('d','a','t','a');
- m_mmckinfoData.cksize=m_dwDataSize;
- mmioCreateChunk(m_Savehmmio,&m_mmckinfoData,0);
- return TRUE;
- }
- BOOL CWaveBase::Wave_OpenFile()
- {
- BOOL werr;
- MMCKINFO ckRIFF;
- /*
- The MMCKINFO structure contains information about a chunk in a RIFF file.
- typedef struct {
- FOURCC ckid;
- DWORD cksize;
- FOURCC fccType;
- DWORD dwDataOffset;
- DWORD dwFlags;
- } MMCKINFO;
- */
- MMCKINFO ck;
- DWORD dw;
- if(m_bFileExist)
- {
- mmioClose(m_Readhmmio, 0l);
- }
- //打开WAV文件
- werr = FALSE;
- m_Readhmmio = mmioOpen((char *)LPCTSTR(m_WaveFileName),NULL, MMIO_READ|MMIO_ALLOCBUF);
- if (NULL == m_Readhmmio)
- {
- goto wio_End;
- }
- ckRIFF.fccType = mmioFOURCC('W', 'A', 'V', 'E');
- //The mmioFOURCC macro converts four characters into a four-character code.
- if (mmioDescend(m_Readhmmio, &ckRIFF, NULL, MMIO_FINDRIFF))
- {
- goto wio_Close_End;
- }
- m_dFileSize = ckRIFF.cksize;
- //寻找RRIF块
- while (MMSYSERR_NOERROR == mmioDescend(m_Readhmmio, &ck, &ckRIFF, 0))
- {
- if ((ck.dwDataOffset + ck.cksize) > (ckRIFF.dwDataOffset + ckRIFF.cksize))
- {
- goto wio_Close_End;
- }
- //RRIF的子块 其子块类型可能为:LISTDISPFMTFACTDATA
- switch (ck.ckid)
- {
- case mmioFOURCC('L', 'I', 'S', 'T'):
- break;
- case mmioFOURCC('D', 'I', 'S', 'P'):
- break;
- case mmioFOURCC('f', 'm', 't', ' '):
- //WAV格式参数
- dw = ck.cksize;
- if (dw < sizeof(WAVEFORMATEX))
- dw = sizeof(WAVEFORMATEX);
- dw = ck.cksize;
- if (mmioRead(m_Readhmmio, (HPSTR)&(m_WaveFormat), dw) != (LONG)dw)goto wio_Close_End;
- break;
- case mmioFOURCC('d', 'a', 't', 'a'):
- m_dwDataOffset=m_ReadFileOffset=ck.dwDataOffset;
- m_dwDataSize=ck.cksize;
- werr=TRUE;
- goto wio_End;
- case mmioFOURCC('f', 'a', 'c', 't'):
- break;
- }
- mmioAscend(m_Readhmmio, &ck, 0);
- }
- wio_Close_End:
- mmioClose(m_Readhmmio,0l);
- wio_End:
- m_bFileExist=werr;
- return (werr);
- }
- void CWaveBase::Wave_CloseReadFile()
- {
- mmioClose(m_Readhmmio,0l);
- }