DirectSound SetFormat

Started by
1 comment, last by Gamero 18 years, 1 month ago
Im trying to set up directsound. I managed to set up secondary buffers and play them, but found that i could only play stereo wav files, not mono files. I figured that maybe I have to create the primary buffer with the same format as the wav files i'm going to play. So i try to do that. Here is the problem: SetFormat returns an undefined error. The code: if(FAILED(DirectSoundCreate8(NULL, &g_pDSound, NULL))) return E_FAIL; if(FAILED(g_pDSound->SetCooperativeLevel(g_hWndMain, DSSCL_NORMAL))) return E_FAIL; CWaveFile *WavFile=new CWaveFile; WAVEFORMATEX wfx; if(FAILED(WavFile->Open("Sound\\punch.wav", &wfx, WAVEFILE_READ))) return E_FAIL; DSBUFFERDESC dsbdesc; memset(&dsbdesc, 0, sizeof(DSBUFFERDESC)); dsbdesc.dwSize = sizeof(DSBUFFERDESC); dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER; dsbdesc.dwBufferBytes = 0; dsbdesc.lpwfxFormat = NULL; LPDIRECTSOUNDBUFFER pDsb = NULL; if(FAILED(g_pDSound->CreateSoundBuffer(&dsbdesc, &pDsb, NULL))) return E_FAIL; if(FAILED(pDsb->SetFormat(WavFile->GetFormat()))) return E_FAIL; Im new to directsound, so i have probably missed something basic. Can someone please tell me what?
Advertisement
What exact error code does it return? And have you set the cooperative level to higher than DSSCL_NORMAL, to enable you to change the primary buffer format?
Ah, that helped. I didnt know that I needed to set the priority high to be able to set the format of the primary buffer. However, now I have a new problem: when i try to create secondary buffers, I get the DSERR_INVALIDPARAM error from CreateSoundBuffer. This part worked before i tried to create the primary buffer myself.

The code:

SoundBuffer = new LPDIRECTSOUNDBUFFER8[MAXACTS];
CWaveFile *WavFile=new CWaveFile;
WAVEFORMATEX wfx;
DSBUFFERDESC dsbdesc;
LPDIRECTSOUNDBUFFER pDsb = NULL;

//Fill the soundbuffers
for(int i=0;i<MAXACTS;i++)
{
if(FAILED(WavFile->Open("Sound\\punch.wav", &wfx, WAVEFILE_READ)))
return E_FAIL;

memset(&wfx, 0, sizeof(WAVEFORMATEX));
wfx=*WavFile->GetFormat();

memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_CTRLVOLUME;
dsbdesc.dwBufferBytes = WavFile->GetSize();
dsbdesc.lpwfxFormat = &wfx
if(FAILED(g_pDSound->CreateSoundBuffer(&dsbdesc, &pDsb, NULL)))
return E_FAIL;

if(FAILED(pDsb->QueryInterface(IID_IDirectSoundBuffer8, (LPVOID*) &SoundBuffer)))
return E_FAIL;
pDsb->Release();

VOID* pDSLockedBuffer = NULL;
DWORD dwDSLockedBufferSize = 0;
DWORD dwWavDataRead = 0;

if( FAILED(SoundBuffer->Lock( 0, 0, &pDSLockedBuffer, &dwDSLockedBufferSize, NULL, NULL, DSBLOCK_ENTIREBUFFER ) ) )
return E_FAIL;
if( FAILED (WavFile->Read( (BYTE*) pDSLockedBuffer, dwDSLockedBufferSize, &dwWavDataRead ) ) )
return E_FAIL;
if(FAILED(SoundBuffer->Unlock(pDSLockedBuffer, dwDSLockedBufferSize, NULL, 0)))
return E_FAIL;
if(FAILED(WavFile->Close()))
return E_FAIL;
}

This topic is closed to new replies.

Advertisement