Microsoft DirectX 9.0

CWaveFile Sample Class

The CWaveFile sample class is used for reading from and writing to WAV files and reading from WAV resources or waveforms stored in memory. An object of this class is created by CSoundManager when creating a CSound or CStreamingSound object. Applications do not usually need to call methods of CWaveFile directly.

class CWaveFile
{
public:
    WAVEFORMATEX* m_pwfx;
    HMMIO         m_hmmio;
    MMCKINFO      m_ck;
    MMCKINFO      m_ckRiff;
    DWORD         m_dwSize;
    MMIOINFO      m_mmioinfoOut;
    DWORD         m_dwFlags;
    BOOL          m_bIsReadingFromMemory;
    BYTE*         m_pbData;
    BYTE*         m_pbDataCur;
    ULONG         m_ulDataSize;
    CHAR*         m_pResourceBuffer;

protected:
    HRESULT ReadMMIO();
    HRESULT WriteMMIO(WAVEFORMATEX *pwfxDest);

public:
    CWaveFile();
    ~CWaveFile();

    HRESULT Open(LPTSTR strFileName, WAVEFORMATEX* pwfx, DWORD dwFlags);
    HRESULT OpenFromMemory(BYTE* pbData, ULONG ulDataSize, 
        WAVEFORMATEX* pwfx, DWORD dwFlags);
    HRESULT Close();
    HRESULT Read(BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead);
    HRESULT Write(UINT nSizeToWrite, BYTE* pbData, UINT* pnSizeWrote);
    DWORD   GetSize();
    HRESULT ResetFile();
    WAVEFORMATEX* GetFormat() { return m_pwfx; };
};

Constructor

The constructor initializes data members.

Public Methods

The class contains the following public methods, in alphabetical order.

Method Description
Close Closes the file. If the file is being written to, first writes RIFF chunk sizes.
GetFormat Returns a WAVEFORMATEX structure describing the format of the waveform.
GetSize Retrieves the size of the data chunk, after Open has been called.
Open Opens the file or loads the resource and retrieves the WAV format and data chunk size, or opens the file for writing and writes the header chunks. The strFileName parameter can be a file path or the name of a resource of type "WAVE" or "WAV". Pass WAVEFILE_READ in dwFlags to read from the file, or WAVEFILE_WRITE if capturing to the file. Use the pwfx parameter to specify the format of captured data; this parameter is ignored if the file is being read.
OpenFromMemory Initializes data members so that subsequent calls to Read will get data from the supplied memory location rather than from a file or resource. It is assumed that the data does not contain any RIFF headers, and that pbData points to the start of the waveform samples.
Read Reads data from the file, resource, or memory and copies it to the supplied address, which the application is responsible for obtaining by using IDirectSoundBuffer8::Lock. Read advances the pointer into the data by the number of bytes specified in dwSizeToRead.
ResetFile Resets the pointer into the file or data to the beginning of the data chunk.
Write Writes data to the file from the specified address, which the application is responsible for obtaining by using IDirectSoundCaptureBuffer8::Lock.

The class is implemented in (SDK root)\samples\C++\Common\Src\Dsutil.cpp.

See Also