直接解析wav文档 char szFileName[128]; // filename of file to open HMMIO hmmio; // file handle for open file MMCKINFO mmckinfoParent; // parent chunk information MMCKINFO mmckinfoSubchunk; // subchunk information structure DWORD dwFmtSize; // size of "FMT" chunk DWORD dwDataSize; // size of "DATA" chunk WAVEFORMAT *pFormat; // address of "FMT" chunk HPSTR lpData; // address of "DATA" chunk
// Get the filename from the edit control. . . . // Open the file for reading with buffered I/O // by using the default internal buffer if(!(hmmio = mmioOpen(szFileName, NULL, MMIO_READ | MMIO_ALLOCBUF))) { Error("Failed to open file."); return; }
// Locate a "RIFF" chunk with a "WAVE" form type to make // sure the file is a waveform-audio file. mmckinfoParent.fccType = mmioFOURCC('W', 'A', 'V', 'E'); if (mmioDescend(hmmio, (LPMMCKINFO) &mmckinfoParent, NULL, MMIO_FINDRIFF)) { Error("This is not a waveform-audio file."); mmioClose(hmmio, 0); return; } // Find the "FMT" chunk (form type "FMT"); it must be // a subchunk of the "RIFF" chunk. mmckinfoSubchunk.ckid = mmioFOURCC('f', 'm', 't', ' '); if (mmioDescend(hmmio, &mmckinfoSubchunk, &mmckinfoParent, MMIO_FINDCHUNK)) { Error("Waveform-audio file has no "FMT" chunk."); mmioClose(hmmio, 0); return; }
// Get the size of the "FMT" chunk. Allocate // and lock memory for it. dwFmtSize = mmckinfoSubchunk.cksize; . . . // Read the "FMT" chunk. if (mmioRead(hmmio, (HPSTR) pFormat, dwFmtSize) != dwFmtSize){ Error("Failed to read format chunk."); . . . mmioClose(hmmio, 0); return; }
MMCKINFO mmckinfoParent; // 父块信息
MMCKINFO mmckinfoSubchunk; // 子块信息
DWORD dwFmtSize; // “fmt”数据块的大小
DWORD dwDataSize; // “data”数据块的大小
WAVEFORMAT *pFormat; // “fmt”数据块的地址
HPSTR lpData; // “data”数据块的地址
// 获取WAVE文件名
// 以内部缓冲区方式打开文件
if(!(hmmio = mmioOpen("C:\\WINDOWS\\Media\\chimes.wav",NULL,MMIO_READ | MMIO_ALLOCBUF)))
{
// 打开文件出错,错误处理
AfxMessageBox("Open file Error!");
return;
}
// 寻找形式类型为“WAVE”的“RIFF”数据块,以确定文件为WAVE文件
mmckinfoParent.fccType = mmioFOURCC('W', 'A', 'V', ' ');
if (mmioDescend(hmmio, (LPMMCKINFO) &mmckinfoParent, NULL, MMIO_FINDRIFF))
{
// 文件不是WAVE文件,错误处理
AfxMessageBox("not a wav file!");
mmioClose(hmmio, 0);
return;
}
// 寻找RIFF数据块的“fmt”子块
mmckinfoSubchunk.ckid = mmioFOURCC('f', 'm', 't', ' ');
if (mmioDescend(hmmio, &mmckinfoSubchunk, &mmckinfoParent, MMIO_FINDCHUNK))
{
// 不能找到“fmt”子块,错误处理
AfxMessageBox("can't find fmt chunk!");
mmioClose(hmmio, 0);
return;
}
// 获取“fmt”子块的大小,并分配内存
dwFmtSize = mmckinfoSubchunk.cksize;
// 读取“fmt”子块数据
if (mmioRead(hmmio, (HPSTR) pFormat, dwFmtSize) != dwFmtSize)
{
// 读“fmt”子块数据出错,错误处理
AfxMessageBox("Read fmt chunk Error!");
mmioClose(hmmio, 0);
return;
}
// 移出“fmt”子块
mmioAscend(hmmio, &mmckinfoSubchunk, 0);
// 寻找“data”子块
// 当前文件位置应该是“data”子块的起始位置
mmckinfoSubchunk.ckid = mmioFOURCC('d', 'a', 't', 'a');
if (mmioDescend(hmmio, &mmckinfoSubchunk, &mmckinfoParent, MMIO_FINDCHUNK))
{
// 不能找到“data”子块,错误处理
AfxMessageBox("can't find date chunk!");
mmioClose(hmmio, 0);
return;
}
// 获取“data”子块的大小
dwDataSize = mmckinfoSubchunk.cksize;
if (dwDataSize == 0L)
{
// “data”子块中无数据,错误处理
AfxMessageBox("nothing in data chunk!");
mmioClose(hmmio, 0);
return;
}
// 打开WAVE输出设备
// 为WAVE数据分配并锁定内存
// 读出WAVE数据子块
if(mmioRead(hmmio, (HPSTR) lpData, dwDataSize) != dwDataSize)
{
// 读“data”子块数据出错,错误处理
AfxMessageBox("Read data chunk Error!");
mmioClose(hmmio, 0);
return;
}
// 关闭文件
mmioClose(hmmio, 0);
char szFileName[128]; // filename of file to open
HMMIO hmmio; // file handle for open file
MMCKINFO mmckinfoParent; // parent chunk information
MMCKINFO mmckinfoSubchunk; // subchunk information structure
DWORD dwFmtSize; // size of "FMT" chunk
DWORD dwDataSize; // size of "DATA" chunk
WAVEFORMAT *pFormat; // address of "FMT" chunk
HPSTR lpData; // address of "DATA" chunk
// Get the filename from the edit control.
.
.
.
// Open the file for reading with buffered I/O
// by using the default internal buffer
if(!(hmmio = mmioOpen(szFileName, NULL,
MMIO_READ | MMIO_ALLOCBUF)))
{
Error("Failed to open file.");
return;
}
// Locate a "RIFF" chunk with a "WAVE" form type to make
// sure the file is a waveform-audio file.
mmckinfoParent.fccType = mmioFOURCC('W', 'A', 'V', 'E');
if (mmioDescend(hmmio, (LPMMCKINFO) &mmckinfoParent, NULL,
MMIO_FINDRIFF))
{
Error("This is not a waveform-audio file.");
mmioClose(hmmio, 0);
return;
}
// Find the "FMT" chunk (form type "FMT"); it must be
// a subchunk of the "RIFF" chunk.
mmckinfoSubchunk.ckid = mmioFOURCC('f', 'm', 't', ' ');
if (mmioDescend(hmmio, &mmckinfoSubchunk, &mmckinfoParent,
MMIO_FINDCHUNK))
{
Error("Waveform-audio file has no "FMT" chunk.");
mmioClose(hmmio, 0);
return;
}
// Get the size of the "FMT" chunk. Allocate
// and lock memory for it.
dwFmtSize = mmckinfoSubchunk.cksize;
.
.
.
// Read the "FMT" chunk.
if (mmioRead(hmmio, (HPSTR) pFormat, dwFmtSize) != dwFmtSize){
Error("Failed to read format chunk.");
.
.
.
mmioClose(hmmio, 0);
return;
}
#include <windows.h>
#pragma comment(lib,"winmm.lib")
#include <mmsystem.h>
这些头文件,我做的是根据一个wav文件创建一个wav文件,所以建一个控制台程序就行了