Step 2: Open the Wave File

The sample program is a general-purpose wave-file reader that does not expect sound data in a particular format. That being the case, it's necessary to put off creating the secondary sound buffer until the format of the data is known.

The steps in opening the file and creating the secondary buffer are grouped in the SetupStreamBuffer function. This function first performs any necessary cleanup from the last time a sound was played:

BOOL SetupStreamBuffer(LPSTR lpzFileName)

{

// Close any open file and release interfaces

WaveCloseReadFile(&hmmio, &pwfx); // Function in Wave.c

if (lpdsNotify != NULL)

{

lpdsNotify->Release();

lpdsNotify = NULL;

}

if (lpdsb != NULL)

{

lpdsb->Release();

lpdsb = NULL;

}

The function now opens a wave file, gets the format, and advances the file pointer to the beginning of the sound data. It does this by using two functions in Wave.c.

if (WaveOpenFile(lpzFileName, &hmmio, &pwfx,

&mmckinfoParent) != 0)

return FALSE;

if (WaveStartDataRead(&hmmio, &mmckinfoData,

&mmckinfoParent) != 0)

return FALSE;

The WaveOpenFile function initializes three of the global variables declared at the beginning of the tutorial: a file handle, a pointer to a WAVEFORMATEX structure, and the MMCKINFO structure for the parent chunk (that is, information about the file as a whole).

The file handle and chunk information are then passed to the WaveStartDataRead function, which returns information about the data chunk in mmckinfoData. You would be interested in this structure if you were creating a static buffer just big enough to accommodate all the data bytes. In this tutorial, though, you're creating a streaming buffer, so you don't need to know the size of the data chunk.