DirectX SDK |
This topic applies only to applications written in C++.
To prepare for writing to a wave file, the application must first declare four variables to be passed to the functions in Wavwrite.cpp:
WAVEFORMATEX wfx; // Wave format info HMMIO hmmio; // File handle MMCKINFO mmckinfoData; // Chunk info MMCKINFO mmckinfoParent; // Parent chunk (file) info
You must also initialize the WAVEFORMATEX structure with the format of the capture buffer.
Now you call the WaveOpenFile function, passing in the desired filename and the addresses of the global variables. The function creates the file and writes some header information. Like other functions in Wavwrite.cpp, WaveOpenFile returns zero if successful.
if (WaveOpenFile(pszFileName, &hmmio, &wfx, &mmckinfoData, &mmckinfoParent)) { // Failure }
Next, call the WaveStartDataWrite function, which initializes the data chunk.
if (WaveStartDataWrite(&hmmio, &mmckinfoData, &mmioinfo)) { // Failure }
The file is now ready to receive data. The following fragment illustrates how data might be copied from a capture buffer to a file.
/* It is assumed that the following variables contain valid assignments: LPDIRECTSOUNDCAPTUREBUFFER lpdscb; // Capture buffer DSCBUFFERDESC dscbDesc; // Capture buffer description DWORD dwReadCursor; // Internal cursor in buffer DWORD dwNumBytes; // Bytes available DWORD dwTotalBytesWritten; // Running total in file */ LPBYTE pbInput1, pbInput2; // Pointers to data in buffer DWORD cbInput1, cbInput2; // Count of bytes in locked portion UINT BytesWritten; // Count of bytes written to file if FAILED(hr = lpdscb->Lock(dwReadCursor, dwNumBytes, (LPVOID *)&pbInput1, &cbInput1, (LPVOID *)&pbInput2, &cbInput2, 0)) { // Failure } else { if (WaveWriteFile(hmmio, cbInput1, pbInput1, &mmckinfoData, &dwBytesWritten, &mmioinfo)) { // Failure } else dwTotalBytesWritten += BytesWritten; if (pbInput2 != NULL) { if (WaveWriteFile(hmmio, cbInput2, pbInput2, &mmckinfoData, &BytesWritten, &mmioinfo)) { // Failure } else dwTotalBytesWritten += BytesWritten; } lpdscb->Unlock(pbInput1, cbInput1, pbInput2, cbInput2); // Increment internal cursor, compensating for wrap around dwReadCursor += dwNumBytes; while (dwReadCursor >= dscbDesc.dwBufferBytes) dwMyReadCursor -= dscbDesc.dwBufferBytes; }
When you are finished capturing data, you close the file:
WaveCloseWriteFile(&hmmio, &mmckinfoData, &mmckinfoParent, &mmioinfo, dwTotalBytesWritten / (wfx.wBitsPerSample / 8));
The WaveCloseWriteFile function calculates the total number of samples in the file and writes this number to the data chunk header.