try to have more stable sound failure handling

This commit is contained in:
zeromus 2008-05-27 07:04:07 +00:00
parent 7740dbbaf4
commit 72706e995a
2 changed files with 49 additions and 10 deletions

View File

@ -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) {

View File

@ -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;i<count;i++)
sbuf[i] = buffer[i];
player->receive(count*2,tempbuf);
if(player)
player->receive(count*2,tempbuf);
}