Okay, I'm learning how to load a WAV file into DirectSound in order to play it. I've successfully surfed through the headers of the WAV file, and loaded it. However, when I go to create a buffer with this WAV sound data I've run into a road block. I'm getting a DSERR_INVALIDPARAM error when I try to create this sound buffer from a WAV format variable. I have successfully created a blank primary buffer with the wfxformat tag being NULL and the size being 0. Here's my code for my WAV buffer:

Here's my class that I use to access my m_lpDSBuffer

class DSound {
    DWORD                m_dwDSBufferSize;
    DWave*				 m_lpWaveFile;
    DWORD                m_dwNumBuffers;
    DWORD                m_dwCreationFlags;
	bool inUse;
	bool Open(LPTSTR strFileName);
bool DWave::Open(LPTSTR strFileName, WAVEFORMATEX* wavFmt, DWORD dwFlags) {

	HMMIO hWav; // WAVE's handle
	WAVEFORMATEX wfmex; // WAVE's format
	MMCKINFO child, parent;
	UCHAR *p_wavData; // file loads here
	UCHAR *p_wavBuffer1; // pointer to first buffer
	UCHAR *p_wavBuffer2; // second buffer
	DWORD dwLength1; // length of first write buffer
	DWORD dwLength2; // length of second write buffer
	DSound wavFile; // temporary buffer
	DSound *p_wavFile;
	int iCurrSize, i;
	DSBUFFERDESC dsbd; // local direct sound buffer description

	p_wavFile = &wavFile;

	hWav = mmioOpen(strFileName, NULL, MMIO_READ | MMIO_ALLOCBUF);

	if (hWav==NULL) {
		ErrorBox("Could not find / load file at mmioOpen()");
		return false;

	parent.fccType = mmioFOURCC('W', 'A', 'V', 'E');

	// descend to riff
	if (mmioDescend(hWav, &parent, NULL, MMIO_FINDRIFF)) {
		ErrorBox("Could not descend to RIFF, file is corrupt or not a WAVE file.");
		mmioClose(hWav, 0);
		return false;

	// descend to WAVEfmt
	child.ckid = mmioFOURCC('f', 'm', 't', ' ');
	if (mmioDescend(hWav, &child,  &parent, 0)) {
		ErrorBox("Could not descend to WAVEfmt, file is corrupt or not a WAVE file.");
		mmioClose(hWav, 0);
		return false;

	// open WAV file for reading, check to make sure format is correct
	if (mmioRead(hWav, (char*)&wfmex, sizeof(WAVEFORMATEX)) != sizeof(WAVEFORMATEX)) {
		ErrorBox("Error reading WAV format, file is corrupt or not a WAVE file.");
		mmioClose(hWav, 0);
		return false;

	if (wfmex.wFormatTag != WAVE_FORMAT_PCM) {
		ErrorBox("WAVE file is not of PCM format.");
		mmioClose(hWav, 0);
		return false;

	if (mmioAscend(hWav, &child, 0)) {
		ErrorBox("Error ascending to WAVE data.");
		mmioClose(hWav, 0);
		return false;

	// descend to data
	child.ckid = mmioFOURCC('d', 'a', 't', 'a');
	if (mmioDescend(hWav, &child, &parent, MMIO_FINDCHUNK)) {
		ErrorBox("Error descending to the WAVE data level.");
		mmioClose(hWav, 0);
		return false;

	// verification complete, now read information

	// allocate memory and load pointer
	if (!(p_wavData = new UCHAR [child.cksize])) {
		ErrorBox("Insufficient memory to load WAVE file.");
		mmioClose(hWav, 0);
		return false;

	// read WAVE data

	long lBytesRead;
	lBytesRead = mmioRead(hWav, (char*)p_wavData, child.cksize);
	if (lBytesRead<0) {
		ErrorBox("Unable to read WAVE file data.");
		mmioClose(hWav, 0);
		return false;

	mmioClose(hWav, 0);

	// place our information on our buffer, prepare our description for direct sound
	dsbd.dwSize = sizeof(DSBUFFERDESC);
	dsbd.dwBufferBytes = child.cksize; // size of buffer is equal to the wave's size
	dsbd.dwFlags = DSBCAPS_STATIC | 
          DSBCAPS_LOCSOFTWARE |     // Force loading in software so we can't loose it.
			    DSBCAPS_GLOBALFOCUS |     // Don't mute on 'lostfocus'
	dsbd.lpwfxFormat = &wfmex;

	// create our sound buffer

	HRESULT hr = dsSound.m_lpDS->CreateSoundBuffer(&dsbd, &p_wavFile->m_lpDSBuffer, NULL);
		delete [] p_wavData;
		p_wavFile = NULL;
		ErrorBox("Error creating sound buffer.");
		return false;

	return true;

I keep getting a DSERR_INVALIDPARAM error, help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.