Jump to content
  • Advertisement
Sign in to follow this  
rOOdolf

half second glitches using DirectSound9 on certain computers

This topic is 5395 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

I wrote my sound module for my current game around 9 months ago or so, and it was all going smoothly, worked perfectly and had been tried on a few friends computers. Recently however I built a new computer with an Abit AI7 motherboard with onboard sound... the problem is that when I run the game on this, I get large glitches where the computer freezes now and then when it is trying to play sounds (this took ages to pin down, thought it was a graphics issue lol). I haven't noticed any similar problems on any other games I've been playing. :( My best guess so far is that it has something to do with sound acceleration, and the motherboard trying to use 'hardware accelerated sound'. When I turn the sound performance slider from fully accelerated to emulation, the glitches seem to go away, albeit giving a long delay between playing and hearing the sounds. I'm wondering whether I might be able to cure it just by changing some of the flags I'm using, perhaps on creating the secondary sound buffers (I have around 30 or so short sounds). The flags i'm currently using on creation are : dsbdesc.dwFlags = DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME; and this gives around 0.5 second glitches all over the place. I tried changing adding a DSBCAPS_LOCSOFTWARE flag, but this didn't solve the problem. However when I tried adding a DSBCAPS_LOCDEFER flag this cured the problem in most areas, it just has problems with a few repeated sounds which seems to do multiple freezes and almost crash the computer. I've tried whapping the debug DirectSound output to full but it doesn't provide any unusual warning or error messages. So anyone any ideas what might be causing this? thanks!

Share this post


Link to post
Share on other sites
Advertisement
It's tough to pin down the source of your problems, it could be a number of different things. Best guess is that it's something with your creation settings like you said.

Could you post the code for the creation of your primary buffer and secondary buffers? If you call Restore on your sound buffer when, where and how often do you do it? Also, are you streaming data in or just loading the sound into the buffer all at once?

Sorry I could be of more immediate help.

Share this post


Link to post
Share on other sites
no that's ok, any help is useful :

here's initializing:


// initialize direct sound
HRESULT hr = DirectSoundCreate(NULL, &g_pDirectSound, NULL);
assert (!FAILED(hr));

g_pDirectSound->SetCooperativeLevel(g_hWnd, DSSCL_PRIORITY);




I've looked over at it and I don't seem to be setting the primary buffer format, just using what appears to be the default primary buffer, however I've just tried explicitly setting it to 2 channel 22050hz 16 bit and 2 channel 44100hz 16bit and that hasn't cured it, using this code :


bool SoSoundManager::SetPrimaryBufferFormat( unsigned int dwPrimaryChannels, unsigned int dwPrimaryFreq, unsigned int dwPrimaryBitRate )
{
HRESULT hr;
LPDIRECTSOUNDBUFFER pDSBPrimary = NULL;

if( g_pDirectSound == NULL )
return false;

// Get the primary buffer
DSBUFFERDESC dsbd;
ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
dsbd.dwBufferBytes = 0;
dsbd.lpwfxFormat = NULL;

if( FAILED( hr = g_pDirectSound->CreateSoundBuffer( &dsbd, &pDSBPrimary, NULL ) ) )
{
return false;
// return DXTRACE_ERR( TEXT("CreateSoundBuffer"), hr );
}

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

if( FAILED( hr = pDSBPrimary->SetFormat(&wfx) ) )
{
return false;
// return DXTRACE_ERR( TEXT("SetFormat"), hr );
}

if (pDSBPrimary)
{
pDSBPrimary->Release();
pDSBPrimary = 0;
}

return S_OK;
}




here's the code I use for creating a secondary sound buffer:


SN_DSSOUNDBUFFER * SoSoundManager::CreateSoundFromRawData(const unsigned char * pData, int iNumBytes)
{
if (!iNumBytes) return 0;

SoWaveFile wav;

WAVEFORMATEX fmt;
DSBUFFERDESC dsbdesc;
SN_DSSOUNDBUFFER * pSB = 0;

wav.OpenFromFileInMemory((unsigned char *) pData, iNumBytes,
&fmt, WAVEFILE_READ);

fmt = *wav.GetFormat();


// Set up DSBUFFERDESC structure.
memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME; // added LOCDEFER to solve glitch on new comp
dsbdesc.dwBufferBytes = wav.GetSize();
dsbdesc.lpwfxFormat = &fmt;

// Create sound buffer.
HRESULT hr = g_pDirectSound->CreateSoundBuffer(&dsbdesc, &pSB, NULL);

// should check hr lol


// lock
void * pWrite;
DWORD dwLength;
hr = pSB->Lock(0, 0, &pWrite, &dwLength, 0, 0, DSBLOCK_ENTIREBUFFER);
assert (SUCCEEDED(hr));


wav.ResetFile();
DWORD dwRead;
wav.Read((BYTE *) pWrite, dwLength, &dwRead);
assert (dwRead == dwLength);

// unlock
hr = pSB->Unlock(pWrite, dwLength, 0, 0);
assert (SUCCEEDED(hr));

// finished with the wav file
wav.Close();

return pSB;
}



No streaming, just loading the sounds in one time only on game start.. i'm also not calling restore at all yet, not dealing with special situations (I'm assuming this would cause the sound buffer->Play command to fail).

Playing the sounds :

pSB->SetPan(LONG(fPan));
pSB->SetVolume(DSBVOLUME_MIN + LONG(fVolume));

HRESULT hr;
hr = pSB->Stop();
hr = pSB->SetCurrentPosition(0);
hr = pSB->Play(0, 0, 0);
assert (!FAILED(hr));



As far as I can remember, that's all there is to it, so hopefully the problem is somewhere in there. I've since found that the problem exists when the sound performance slider is set to anything other than no acceleration (emulate).

I'm keeping my fingers crossed this isn't a driver issue, but if there are no ideas here I'll have to start looking to see if there are any updates to my motherboard bios / drivers [:(].

Share this post


Link to post
Share on other sites
A few things you can try / think about:

Set the format of the primary buffer to whatever the format that your sound files are. Do this before creating any secondary buffers. This won't have any effect on WDM drivers but depending on your driver you might be doing format conversion of the sound file on the fly if the formats don't match.

For secondary buffer creation flags try using DSBCAPS_STATIC in addition to the others. This will force the driver to use hardware buffers if they are available and your card isn't on the PCI bus. If you ever read or set the cursor position after the sound has started playing also use this: DSBCAPS_GETCURRENTPOSITION2. Older versions of DX has cursor position problems when reading, that flag will fix them.

The reason I asked about Restore is that it can cause blips if your calling it every frame. Only call it when GetStatus indicates DSBSTATUS_BUFFERLOST.

Share this post


Link to post
Share on other sites
sorry to bring this back to the top after a couple of days.. but I thought it might help someone...

It turned out it was a driver bug.

I literally spent weeks on and off identifying and trying to get rid of this problem, when in the end the solution was to download the latest audio drivers for my motherboard from abit. [tears]

I've had this happen before, spend an extremely long time on a problem trying all different alternatives, only to find it was a driver bug. It's just because in order to program I always assume that the system I'm working on top of is bug-free, and that I'm the cause of all bugs (which is true 99.9% of the time). This ends up meaning that updating the drivers is the last thing I try. [disturbed]

But it does happen... so if you get strange sound / video / 3d problems that you can't explain, try another set of drivers. I've even had situations where current 3d card drivers have a bug, that are not present in earlier drivers. AWwwwwkkk frustrating!!!! [flaming]

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!