From 72706e995ab298d25877d5be89ccb8f1599f1846 Mon Sep 17 00:00:00 2001 From: zeromus Date: Tue, 27 May 2008 07:04:07 +0000 Subject: [PATCH] try to have more stable sound failure handling --- src/drivers/win/OutputDS.cpp | 31 +++++++++++++++++++++++++++---- src/drivers/win/sound.cpp | 28 ++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/drivers/win/OutputDS.cpp b/src/drivers/win/OutputDS.cpp index 573ee31e..e7981227 100644 --- a/src/drivers/win/OutputDS.cpp +++ b/src/drivers/win/OutputDS.cpp @@ -33,7 +33,8 @@ public: } virtual ~DSVoice() { driver->freeVoiceInternal(this,true); - ds_buf->Release(); + if(ds_buf) + ds_buf->Release(); } DSVoice(OAKRA_Module_OutputDS *driver, OAKRA_Format &format, IDirectSound *ds_dev, bool global) { this->driver = driver; @@ -66,7 +67,15 @@ public: HRESULT hr = ds_dev->CreateSoundBuffer(&dsbd,&ds_buf,0); cPlay = 0; - hr = ds_buf->Play(0,0,DSBPLAY_LOOPING); + if(hr) + { + hr = ds_buf->Play(0,0,DSBPLAY_LOOPING); + } + + //if we couldnt create the voice, then a sound card is missing. + //we'll use this in getVoice to catch the condition + if(!hr) + dead = true; } //not supported @@ -165,13 +174,27 @@ OAKRA_Module_OutputDS::~OAKRA_Module_OutputDS() { OAKRA_Voice *OAKRA_Module_OutputDS::getVoice(OAKRA_Format &format, OAKRA_Module *source) { DSVoice *dsv = (DSVoice *)getVoice(format); - dsv->setSource(source); + if(dsv->dead) + { + delete dsv; + } + else + { + dsv->setSource(source); + } return dsv; } OAKRA_Voice *OAKRA_Module_OutputDS::getVoice(OAKRA_Format &format) { DSVoice *voice = new DSVoice(this,format,((Data *)data)->ds_dev,((Data *)data)->global); - ((Data *)data)->voices.push_back(voice); + if(voice->dead) + { + delete voice; + } + else + { + ((Data *)data)->voices.push_back(voice); + } return voice; } void OAKRA_Module_OutputDS::freeVoice(OAKRA_Voice *voice) { diff --git a/src/drivers/win/sound.cpp b/src/drivers/win/sound.cpp index b9237056..a9943897 100644 --- a/src/drivers/win/sound.cpp +++ b/src/drivers/win/sound.cpp @@ -265,8 +265,10 @@ void TrashSound() { void DoTrashSound() { if(dsout) delete dsout; if(player) delete player; + if(player8) delete player8; dsout = 0; player = 0; + player8 = 0; trashPending = false; } @@ -293,12 +295,15 @@ void win_Throttle() { player->throttle(); } +static bool killsound; void win_SoundInit(int bits) { - dsout = new OAKRA_Module_OutputDS(); + killsound = false; + dsout = new OAKRA_Module_OutputDS(); if(soundoptions&SO_GFOCUS) dsout->start(0); else dsout->start(hAppWnd); + dsout->beginThread(); OAKRA_Format fmt; fmt.format = bits==8?OAKRA_U8:OAKRA_S16; @@ -306,14 +311,22 @@ void win_SoundInit(int bits) { fmt.rate = soundrate; fmt.size = OAKRA_Module::calcSize(fmt); OAKRA_Voice *voice = dsout->getVoice(fmt); + if(!voice) + { + killsound = true; + FCEUD_PrintError("Couldn't initialize sound buffers. Sound disabled"); + } player = new Player(); player8 = new Player8(player); - dsout->lock(); - if(bits == 8) voice->setSource(player8); - else voice->setSource(player); - dsout->unlock(); + if(voice) + { + dsout->lock(); + if(bits == 8) voice->setSource(player8); + else voice->setSource(player); + dsout->unlock(); + } } @@ -334,6 +347,8 @@ int InitSound() { } win_SoundInit(bits); + if(killsound) + TrashSound(); FCEUI_Sound(soundrate); return 1; @@ -354,7 +369,8 @@ void win_SoundWriteData(int32 *buffer, int count) { short *sbuf = (short *)tempbuf; for(int i=0;ireceive(count*2,tempbuf); + if(player) + player->receive(count*2,tempbuf); }