exceptions

This commit is contained in:
Glenn Maynard
2004-12-01 00:06:41 +00:00
parent a3926cbf2f
commit 67d777f050
5 changed files with 66 additions and 31 deletions
+23 -8
View File
@@ -98,15 +98,18 @@ void DSound::SetPrimaryBufferMode()
DSound::DSound()
{
HRESULT hr;
// Initialize COM
HRESULT hr;
if( FAILED( hr = CoInitialize( NULL ) ) )
RageException::ThrowNonfatal(hr_ssprintf(hr, "CoInitialize"));
RageException::Throw(hr_ssprintf(hr, "CoInitialize"));
}
CString DSound::Init()
{
// Create IDirectSound using the primary sound device
HRESULT hr;
if( FAILED( hr = DirectSoundCreate( NULL, &ds, NULL ) ) )
RageException::ThrowNonfatal(hr_ssprintf(hr, "DirectSoundCreate"));
return hr_ssprintf( hr, "DirectSoundCreate" );
#ifndef _XBOX
static bool bShownInfo = false;
@@ -134,6 +137,8 @@ DSound::DSound()
#endif
SetPrimaryBufferMode();
return "";
}
DSound::~DSound()
@@ -163,9 +168,16 @@ bool DSound::IsEmulated() const
#endif
}
DSoundBuf::DSoundBuf( DSound &ds, DSoundBuf::hw hardware,
DSoundBuf::DSoundBuf()
{
buf = NULL;
temp_buffer = NULL;
}
CString DSoundBuf::Init( DSound &ds, DSoundBuf::hw hardware,
int channels_, int samplerate_, int samplebits_, int writeahead_ )
{
channels = channels_;
samplerate = samplerate_;
samplebits = samplebits_;
@@ -250,7 +262,7 @@ DSoundBuf::DSoundBuf( DSound &ds, DSoundBuf::hw hardware,
HRESULT hr = ds.GetDS()->CreateSoundBuffer( &format, &buf, NULL );
if( FAILED(hr) )
RageException::ThrowNonfatal( hr_ssprintf(hr, "CreateSoundBuffer failed") );
return hr_ssprintf( hr, "CreateSoundBuffer failed" );
#ifndef _XBOX
/* I'm not sure this should ever be needed, but ... */
@@ -258,7 +270,7 @@ DSoundBuf::DSoundBuf( DSound &ds, DSoundBuf::hw hardware,
bcaps.dwSize=sizeof(bcaps);
hr = buf->GetCaps(&bcaps);
if( FAILED(hr) )
RageException::Throw(hr_ssprintf(hr, "buf->GetCaps"));
return hr_ssprintf( hr, "buf->GetCaps" );
if( int(bcaps.dwBufferBytes) != buffersize )
{
LOG->Warn( "bcaps.dwBufferBytes (%i) != buffersize(%i); adjusting", bcaps.dwBufferBytes, buffersize );
@@ -280,6 +292,8 @@ DSoundBuf::DSoundBuf( DSound &ds, DSoundBuf::hw hardware,
#endif
temp_buffer = new char[buffersize];
return "";
}
void DSoundBuf::SetSampleRate(int hz)
@@ -331,7 +345,8 @@ static bool contained( int start, int end, int pos )
DSoundBuf::~DSoundBuf()
{
buf->Release();
if( buf != NULL )
buf->Release();
delete [] temp_buffer;
}
+4 -2
View File
@@ -22,6 +22,7 @@ public:
DSound();
~DSound();
CString Init();
};
class DSoundBuf
@@ -33,8 +34,9 @@ public:
* you use the sample. */
enum { DYNAMIC_SAMPLERATE = -1 };
DSoundBuf(DSound &ds, hw hardware,
int channels, int samplerate, int samplebits, int writeahead);
DSoundBuf();
CString Init( DSound &ds, hw hardware,
int channels, int samplerate, int samplebits, int writeahead );
bool get_output_buf(char **buffer, unsigned *bufsiz, int chunksize);
void release_output_buf(char *buffer, unsigned bufsiz);
@@ -198,34 +198,42 @@ RageSound_DSound::RageSound_DSound():
m_InactiveSoundMutex("InactiveSoundMutex")
{
shutdown = false;
}
CString RageSound_DSound::Init()
{
CString sError = ds.Init();
if( sError != "" )
return sError;
/* Don't bother wasting time trying to create buffers if we're
* emulated. This also gives us better diagnostic information. */
if(ds.IsEmulated())
RageException::ThrowNonfatal("Driver unusable (emulated device)");
if( ds.IsEmulated() )
return "Driver unusable (emulated device)";
/* Create a bunch of streams and put them into the stream pool. */
for(int i = 0; i < 32; ++i) {
DSoundBuf *newbuf;
try {
newbuf = new DSoundBuf(ds,
DSoundBuf::HW_HARDWARE,
channels, DSoundBuf::DYNAMIC_SAMPLERATE, 16, buffersize);
} catch(const RageException &e) {
/* If we didn't get at least 8, fail. */
if(i >= 8) break; /* OK */
for( int i = 0; i < 32; ++i )
{
DSoundBuf *newbuf = new DSoundBuf;
CString sError = newbuf->Init( ds, DSoundBuf::HW_HARDWARE,
channels, DSoundBuf::DYNAMIC_SAMPLERATE, 16, buffersize );
if( sError != "" )
{
/* The channel failed to create. We may be out of hardware streams, or we
* may not have hardware mixing at all. If we didn't get at least 8, fail. */
delete newbuf;
/* Clean up; the dtor won't be called. */
for(int n = 0; n < i; ++n)
delete stream_pool[n];
if(i)
if( i >= 8 )
break; /* OK */
if( i )
{
/* We created at least one hardware buffer. */
LOG->Trace("Could only create %i buffers; need at least 8 (failed with %s). DirectSound driver can't be used.", i, e.what());
RageException::ThrowNonfatal( "not enough hardware buffers available" );
LOG->Trace( "Could only create %i buffers; need at least 8 (failed with %s). Hardware DirectSound driver can't be used.", i, sError.c_str() );
return "not enough hardware buffers available";
}
RageException::ThrowNonfatal( "no hardware buffers available" );
return "no hardware buffers available";
}
stream *s = new stream;
@@ -233,10 +241,12 @@ RageSound_DSound::RageSound_DSound():
stream_pool.push_back(s);
}
LOG->Trace("Got %i hardware buffers", stream_pool.size());
LOG->Trace( "Got %i hardware buffers", stream_pool.size() );
MixingThread.SetName("Mixer thread");
MixingThread.Create( MixerThread_start, this );
return "";
}
RageSound_DSound::~RageSound_DSound()
@@ -63,6 +63,7 @@ class RageSound_DSound: public RageSoundDriver
public:
RageSound_DSound();
~RageSound_DSound();
CString Init();
};
#endif
@@ -66,6 +66,10 @@ RageSound_DSound_Software::RageSound_DSound_Software()
CString RageSound_DSound_Software::Init()
{
CString sError = ds.Init();
if( sError != "" )
return sError;
/* If we're emulated, we're better off with the WaveOut driver; DS
* emulation tends to be desynced. */
if( ds.IsEmulated() )
@@ -76,7 +80,10 @@ CString RageSound_DSound_Software::Init()
max_writeahead = PREFSMAN->m_iSoundWriteAhead;
/* Create a DirectSound stream, but don't force it into hardware. */
pcm = new DSoundBuf( ds, DSoundBuf::HW_DONT_CARE, channels, samplerate, 16, max_writeahead );
pcm = new DSoundBuf;
sError = pcm->Init( ds, DSoundBuf::HW_DONT_CARE, channels, samplerate, 16, max_writeahead );
if( sError != "" )
return sError;
/* Fill a buffer before we start playing, so we don't play whatever junk is
* in the buffer. */