WAV files are resource Interchange file formats (RIFF), including a number of named blocks that contain header information, such as a sound sampling format, or data (such as the sample itself). The Win API provides functions such as turning off Fiff files, finding blocks, and so on. The names of these functions begin with "Mmio".
The DirectSound API does not include methods for writing WAV files. However, the DXUTsound.cpp file implements a Cwavefile class that has the following methods of managing capture files:
· The Open method. Opens a file and writes a header block.
· Write method. Writes data from a buffer into a block of data and increases the write pointer.
· The Close method. Writes the size of the data block to the head and closes the file.
The first step to writing to a WAV file is to invoke the Cwavefile::open method. This creates a file and writes a WAV format block. The parameter is a filename, a pointer to the initialized WAVEFORMATEX structure, and a wavefile_write flag. This method returns an HRESULT.
The following code opens a WAV file for writing:
Code
CWaveFile g_pWaveFile;
WAVEFORMATEX wfxInput;
ZeroMemory( &wfxInput, sizeof(wfxInput));
wfxInput.wFormatTag = WAVE_FORMAT_PCM;
wfxInput.nSamplesPerSec = 22050
wfxInput.wBitsPerSample = 8;
wfxInput.nChannels = 1;
wfxInput.nBlockAlign =
wfxInput.nChannels * (wfxInput.wBitsPerSample / 8);
wfxInput.nAvgBytesPerSec =
wfxInput.nBlockAlign * wfxInput.nSamplesPerSec;
g_pWaveFile = new CWaveFile;
if (FAILED(g_pWaveFile->Open("mywave.wav", &wfxInput,WAVEFILE_WRITE)))
{
g_pWaveFile->Close();
}