From 4b5ff5ba1f8db6dfad5e27e4c0ef7115ce29d3fb Mon Sep 17 00:00:00 2001 From: Torbjörn Andersson Date: Tue, 7 Jun 2005 14:43:12 +0000 Subject: Be less prone to crash on really, really stupid output sample rates. Added warning message if SDL_OpenAudio() fails. Might help diagnosing bug reports like #1206314. svn-id: r18361 --- backends/sdl/sdl.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'backends/sdl') diff --git a/backends/sdl/sdl.cpp b/backends/sdl/sdl.cpp index 69b767dd0d..117736d0ad 100644 --- a/backends/sdl/sdl.cpp +++ b/backends/sdl/sdl.cpp @@ -278,19 +278,22 @@ bool OSystem_SDL::setSoundCallback(SoundProc proc, void *param) { memset(&desired, 0, sizeof(desired)); + _samplesPerSec = 0; + if (ConfMan.hasKey("output_rate")) _samplesPerSec = ConfMan.getInt("output_rate"); - else + + if (_samplesPerSec <= 0) _samplesPerSec = SAMPLES_PER_SEC; // Originally, we always used 2048 samples. This loop will produce the // same result at 22050 Hz, and should hopefully produce something // sensible for other frequencies. Note that it must be a power of two. - uint16 samples = 0x8000; + uint32 samples = 0x8000; for (;;) { - if (samples / (_samplesPerSec / 1000) < 100) + if ((1000 * samples) / _samplesPerSec < 100) break; samples >>= 1; } @@ -298,10 +301,11 @@ bool OSystem_SDL::setSoundCallback(SoundProc proc, void *param) { desired.freq = _samplesPerSec; desired.format = AUDIO_S16SYS; desired.channels = 2; - desired.samples = samples; + desired.samples = (uint16)samples; desired.callback = proc; desired.userdata = param; if (SDL_OpenAudio(&desired, &obtained) != 0) { + warning("Could not open audio device: %s", SDL_GetError()); return false; } // Note: This should be the obtained output rate, but it seems that at -- cgit v1.2.3