Click to See Complete Forum and Search --> : Convert a buffer to WAV format?


Frank
June 9th, 1999, 09:24 AM
Hi,

I'm currently writing a sound recorder application. I'm using the waveInStart,
waveInStop, etc... functions to record a sound from the microphone. Now, I want
to save the sound captured into a .WAV file. Is there an easy way to convert the
captured buffer to the WAV format without having to care about the WAV file chunk,
or do I have to create the file myself by manually create the header,the format chunk
and the data chunk?

Ivan Zhakov
June 9th, 1999, 02:02 PM
Hi,
Folowing code writes sound to disk in WAV format.

///////////////////////////////////////////////////////////
// lpszPathName - Pathname of file
// pSound - Pointer to sound buffer
// cbSound - size in bytes of sound buffer
// pWfx - pointer to WAVEFORMATEX structure
// cbWfx - size of this structure

BOOL SaveWAVFile(LPCTSTR lpszPathName, HPSTR pSound, LONG cbSound,
WAVEFORMATEX pWfx, LONG cbWfx)
{
MMIOINFO mmioinfo;
MMCKINFO ckRIFF;
MMCKINFO ck;
HMMIO hmmio;

::ZeroMemory(&mmioinfo,sizeof(mmioinfo));

hmmio = mmioOpen((LPSTR) lpszPathName,&mmioinfo,MMIO_WRITE|MMIO_CREATE);

if (hmmio == NULL) {
TRACE("mmioOpen returns NULL\n");
return FALSE;
}

// Creating 'RIFF' and 'WAVE' chunks
ckRIFF.fccType = mmioFOURCC('W', 'A', 'V', 'E');
ckRIFF.cksize = 0L;
ckRIFF.dwFlags = MMIO_DIRTY;
mmioCreateChunk(hmmio, &ckRIFF, MMIO_CREATERIFF);

// Creating 'fmt ' chunk
ck.ckid = mmioFOURCC('f', 'm', 't', ' ');
ck.cksize = 0L;
ck.dwFlags = MMIO_DIRTY;
mmioCreateChunk(hmmio, &ck, 0);
mmioWrite(hmmio, (HPSTR) pWfx, cbWfx);

// Goto to 'WAVE' chunk and update 'fmt ' chunk size
mmioAscend(hmmio, &ck, 0);

// Creating 'data' chunk
ck.ckid = mmioFOURCC('d', 'a', 't', 'a');
ck.cksize = 0L;
ck.dwFlags = MMIO_DIRTY;
mmioCreateChunk(hmmio, &ck, 0);
mmioWrite(hmmio,(HPSTR) pSound,cbSound);

// Goto to 'WAVE' chunk and update 'data' chunk size
mmioAscend(hmmio, &ck, 0);

// Goto to 'RIFF' chunk and update 'WAVE' chunk size
mmioAscend(hmmio, &ckRIFF, 0);
mmioClose(hmmio,0);
}



That's all.