Step 5: Creating the Wave File

At this point it is presumed that you have obtained a valid filename and are ready to start saving sound data in a wave file. The process is initiated in the following function:

BOOL StartWrite(TCHAR *pszFileName)
{
    if (WaveCreateFile(pszFileName, &hmmio, &wfx, 
            &mmckinfoData, &mmckinfoParent)) 
      return FALSE;
 
    if (WaveStartDataWrite(&hmmio, &mmckinfoData, &mmioinfo))
    {
        WaveCloseWriteFile(&hmmio, &mmckinfoData, 
                &mmckinfoParent, &mmioinfo, 
                dwTotalBytesWritten / (wfx.wBitsPerSample / 8));
        DeleteFile(pszFileName);
        return FALSE;
    }
    if FAILED(IDirectSoundCaptureBuffer_Start(lpdscb,
            DSCBSTART_LOOPING))
    {
        WaveCloseWriteFile(&hmmio, &mmckinfoData,
                &mmckinfoParent, &mmioinfo, 0);
        DeleteFile(pszFileName);
        return FALSE;
    }
    
    dwTotalBytesWritten = 0;
 
    return TRUE;
}

This function first calls the WaveCreateFile function in Wave.c, in order to create a RIFF file and write the header for the wave format. It then calls the WaveStartDataWrite function, which advances the file pointer to the data chunk. Finally, it starts the capture buffer. In half a second your application will be notified that data is available, and you must be ready to copy it to the file.

Note also the initialization of dwTotalBytesWritten. This value is going to be needed for the data chunk header after capture is complete.