Jump to content
  • Advertisement
Sign in to follow this  
kooper

CreateSoundBuffer returning DSERR_INVALIDPARAM

This topic is 3485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im making a simple 2D game for a university project and when I try to create a sound buffer its returning the error DSERR_INVALIDPARAM and I cant figure out why. I know direct sound is pretty much dead but its what we were told to use so I have to stick with it. Any help would be greatly appreciated.
void Sound::InitializeSound(HWND hWnd)
{

	HRESULT snd = DirectSoundCreate8(NULL, &d3dSound, NULL);

	if (FAILED(snd))
		MessageBox(NULL, "failed to create sound device", "FAIL!", MB_ICONEXCLAMATION | MB_OK);

	HRESULT spr = d3dSound->SetCooperativeLevel(hWnd, DSSCL_NORMAL);

	if (FAILED(spr))
		MessageBox(NULL, "failed to set sound priority", "FAIL!", MB_ICONEXCLAMATION | MB_OK);


	WAVEFORMATEX wfx;
	memset(&wfx, 0, sizeof(WAVEFORMATEX)); 
	wfx.wFormatTag = WAVE_FORMAT_PCM; 
	wfx.nChannels = dwPrimaryChannels; 
	wfx.nSamplesPerSec = dwPrimaryFreq; 
	wfx.nBlockAlign = (wfx.wBitsPerSample / 8 * wfx.nChannels);
	wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; 
	wfx.wBitsPerSample = dwPrimaryBitRate;


	LPDIRECTSOUNDBUFFER pDSBPrimary = NULL;
	DSBUFFERDESC dsbd;
    memset( &dsbd, 0, sizeof(DSBUFFERDESC) );
    dsbd.dwSize        = sizeof(DSBUFFERDESC);
    dsbd.dwFlags = DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY | DSBCAPS_PRIMARYBUFFER; 
    dsbd.dwBufferBytes = 0;
    dsbd.lpwfxFormat   = &wfx;
	

	HRESULT csb = d3dSound->CreateSoundBuffer( &dsbd, &pDSBPrimary, NULL );
	if (FAILED(csb))
		MessageBox(NULL, "failed to create sound buffer", "FAIL!", MB_ICONEXCLAMATION | MB_OK);

	if(pDSBPrimary)
		pDSBPrimary->Release();
}
thanks in advance for any help you can give

Share this post


Link to post
Share on other sites
Advertisement
change this:

memset(&wfx, 0, sizeof(WAVEFORMATEX));
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = dwPrimaryChannels;
wfx.nSamplesPerSec = dwPrimaryFreq;
wfx.nBlockAlign = (wfx.wBitsPerSample / 8 * wfx.nChannels);
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
wfx.wBitsPerSample = dwPrimaryBitRate;

whit this:

memset(&wfx, 0, sizeof(WAVEFORMATEX));
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = dwPrimaryChannels;
wfx.nSamplesPerSec = dwPrimaryFreq;
wfx.wBitsPerSample = dwPrimaryBitRate;
wfx.nBlockAlign = (wfx.wBitsPerSample / 8 * wfx.nChannels);
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;

bye...

Share this post


Link to post
Share on other sites
you used this assignment

wfx.nBlockAlign = (wfx.wBitsPerSample / 8 * wfx.nChannels);

before this

wfx.BitsPerSample = dwPrimaryBitRate;

Share this post


Link to post
Share on other sites
thank you, but im still getting the same error so i guess theres more than 1 problem?

Share this post


Link to post
Share on other sites
I think that you cannot create a primary sound buffer with DSSCL_NORMAL. Try using DSSCL_PRIORITY.

Anyway you shouldn't be using the primary sound buffer. You should use secondary buffers only...can I ask you why are you doing this?

Share this post


Link to post
Share on other sites
Changing the cooperative level didnt seem to work unless theres even more problems.

Im just trying to follow a my university notes and a tutorial in Game Coding 2nd ed. My notes are a bit dis-jointed and hard to follow (I didnt write them, I get lazy in class but Im paying for it now :/ and the tutorial in Game Coding is way more complicated than I need but I havent been able to find anything better so Im trying to piece together the information. If Im not supposed to use the primary buffer its a mistake made out of ignorance.

Share this post


Link to post
Share on other sites
You should always use secondary buffers as explained here:http://msdn.microsoft.com/en-us/library/bb174729%28VS.85%29.aspx.
Try this:

void Sound::InitializeSound(HWND hWnd)
{
HRESULT snd = DirectSoundCreate8(NULL, &d3dSound, NULL);

if (FAILED(snd))
MessageBox(NULL, "failed to create sound device", "FAIL!", MB_ICONEXCLAMATION | MB_OK);

HRESULT spr = d3dSound->SetCooperativeLevel(hWnd, DSSCL_PRIORITY);
if (FAILED(spr))
MessageBox(NULL, "failed to set sound priority", "FAIL!", MB_ICONEXCLAMATION | MB_OK);

WAVEFORMATEX wfx;
memset(&wfx, 0, sizeof(WAVEFORMATEX));
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = dwPrimaryChannels;
wfx.nSamplesPerSec = dwPrimaryFreq;
wfx.wBitsPerSample = dwPrimaryBitRate;
wfx.nBlockAlign = (wfx.wBitsPerSample / 8 * wfx.nChannels);
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;


LPDIRECTSOUNDBUFFER pDSBPrimary = NULL;
DSBUFFERDESC dsbd;
memset( &dsbd, 0, sizeof(DSBUFFERDESC) );
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
dsbd.dwBufferBytes = wfx.nAvgBytesPerSec;
dsbd.lpwfxFormat = &wfx;

HRESULT csb = d3dSound->CreateSoundBuffer( &dsbd, &pDSBPrimary, NULL );
if (FAILED(csb))
MessageBox(NULL, "failed to create sound buffer", "FAIL!", MB_ICONEXCLAMATION | MB_OK);

if(pDSBPrimary)
pDSBPrimary->Release();

}

This creates a secondary buffer whose length is of exactly 1 second of audio. You can change this by assigning a different value to dsbd.dwBufferBytes

Bye! ;)

Share this post


Link to post
Share on other sites
sorry its taken me so long to get back, thanks very much for the help although there were many more problems after that one. a friend of mine tracked them all down for me and it is now working. mmioopen and mmioread seemed to be causing most of the follow up issues, not sure why yet.

anyway, thanks again for your help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!