Sign in to follow this  
Gamero

DirectSound SetFormat

Recommended Posts

Gamero    122
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?

Share this post


Link to post
Share on other sites
Evil Steve    2017
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?

Share this post


Link to post
Share on other sites
Gamero    122
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[i])))
return E_FAIL;
pDsb->Release();

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

if( FAILED(SoundBuffer[i]->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[i]->Unlock(pDSLockedBuffer, dwDSLockedBufferSize, NULL, 0)))
return E_FAIL;
if(FAILED(WavFile->Close()))
return E_FAIL;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this