From 5c8a93b4864f66a896735caee6ef8be29d301820 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sun, 22 Jun 2008 12:39:40 +0000 Subject: Update comment. svn-id: r32752 --- backends/platform/sdl/sdl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'backends/platform/sdl/sdl.cpp') diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp index be3aaad926..60f8d3c95c 100644 --- a/backends/platform/sdl/sdl.cpp +++ b/backends/platform/sdl/sdl.cpp @@ -403,7 +403,7 @@ bool OSystem_SDL::setSoundCallback(SoundProc proc, void *param) { _samplesPerSec = SAMPLES_PER_SEC; // Determine the sample buffer size. We want it to store enough data for - // about 1/32th of a second. Note that it must be a power of two. + // about 1/16th of a second. Note that it must be a power of two. // So e.g. at 22050 Hz, we request a sample buffer size of 2048. int samples = 8192; while (16 * samples >= _samplesPerSec) { -- cgit v1.2.3 From c45d632f3b8c2d8c8aa46b05db758898de863e97 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sat, 28 Jun 2008 15:28:29 +0000 Subject: Patch ##1956946 (Audio::Mixer internal API revision) with some tweaks svn-id: r32828 --- backends/platform/sdl/sdl.cpp | 51 +++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 19 deletions(-) (limited to 'backends/platform/sdl/sdl.cpp') diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp index 60f8d3c95c..290fe63663 100644 --- a/backends/platform/sdl/sdl.cpp +++ b/backends/platform/sdl/sdl.cpp @@ -30,7 +30,7 @@ #include "backends/saves/default/default-saves.h" #include "backends/timer/default/default-timer.h" -#include "sound/mixer.h" +#include "sound/mixer_intern.h" #include "icons/scummvm.xpm" @@ -131,9 +131,7 @@ void OSystem_SDL::initBackend() { // Create and hook up the mixer, if none exists yet (we check for this to // allow subclasses to provide their own). if (_mixer == 0) { - _mixer = new Audio::Mixer(); - bool result = setSoundCallback(Audio::Mixer::mixCallback, _mixer); - _mixer->setReady(result); + setupMixer(); } // Create and hook up the timer manager, if none exists yet (we check for @@ -391,7 +389,15 @@ void OSystem_SDL::deleteMutex(MutexRef mutex) { #pragma mark --- Audio --- #pragma mark - -bool OSystem_SDL::setSoundCallback(SoundProc proc, void *param) { +void OSystem_SDL::mixCallback(void *sys, byte *samples, int len) { + OSystem_SDL *this_ = (OSystem_SDL *)sys; + assert(this_); + + if (this_->_mixer) + this_->_mixer->mixCallback(samples, len); +} + +void OSystem_SDL::setupMixer() { SDL_AudioSpec desired; SDL_AudioSpec obtained; @@ -415,23 +421,30 @@ bool OSystem_SDL::setSoundCallback(SoundProc proc, void *param) { desired.format = AUDIO_S16SYS; desired.channels = 2; desired.samples = (uint16)samples; - desired.callback = proc; - desired.userdata = param; + desired.callback = mixCallback; + desired.userdata = this; + + // Create the mixer instance + assert(!_mixer); + _mixer = new Audio::MixerImpl(this); + assert(_mixer); + if (SDL_OpenAudio(&desired, &obtained) != 0) { warning("Could not open audio device: %s", SDL_GetError()); - return false; + _samplesPerSec = 0; + _mixer->setReady(false); + } else { + // Note: This should be the obtained output rate, but it seems that at + // least on some platforms SDL will lie and claim it did get the rate + // even if it didn't. Probably only happens for "weird" rates, though. + _samplesPerSec = obtained.freq; + debug(1, "Output sample rate: %d Hz", _samplesPerSec); + + // Tell the mixer that we are ready and start the sound processing + _mixer->setOutputRate(_samplesPerSec); + _mixer->setReady(true); + SDL_PauseAudio(0); } - // Note: This should be the obtained output rate, but it seems that at - // least on some platforms SDL will lie and claim it did get the rate - // even if it didn't. Probably only happens for "weird" rates, though. - _samplesPerSec = obtained.freq; - debug(1, "Output sample rate: %d Hz", _samplesPerSec); - SDL_PauseAudio(0); - return true; -} - -int OSystem_SDL::getOutputSampleRate() const { - return _samplesPerSec; } Audio::Mixer *OSystem_SDL::getMixer() { -- cgit v1.2.3 From 943b4c2036002454b276e0190dfc2c8919fb0cbf Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 15 Jul 2008 17:13:06 +0000 Subject: Implemented audio double buffering (for now OSX only) svn-id: r33075 --- backends/platform/sdl/sdl.cpp | 126 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 6 deletions(-) (limited to 'backends/platform/sdl/sdl.cpp') diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp index 290fe63663..cc5f2478e5 100644 --- a/backends/platform/sdl/sdl.cpp +++ b/backends/platform/sdl/sdl.cpp @@ -191,7 +191,7 @@ OSystem_SDL::OSystem_SDL() OSystem_SDL::~OSystem_SDL() { SDL_RemoveTimer(_timerID); - SDL_CloseAudio(); + closeMixer(); free(_dirtyChecksums); free(_currentPalette); @@ -199,7 +199,6 @@ OSystem_SDL::~OSystem_SDL() { free(_mouseData); delete _savefile; - delete _mixer; delete _timer; } @@ -306,7 +305,7 @@ void OSystem_SDL::quit() { SDL_ShowCursor(SDL_ENABLE); SDL_RemoveTimer(_timerID); - SDL_CloseAudio(); + closeMixer(); free(_dirtyChecksums); free(_currentPalette); @@ -314,7 +313,6 @@ void OSystem_SDL::quit() { free(_mouseData); delete _savefile; - delete _mixer; delete _timer; SDL_Quit(); @@ -389,14 +387,110 @@ void OSystem_SDL::deleteMutex(MutexRef mutex) { #pragma mark --- Audio --- #pragma mark - +#ifdef MIXER_DOUBLE_BUFFERING + +void OSystem_SDL::mixerProducerThread() { + byte nextSoundBuffer; + + SDL_LockMutex(_soundMutex); + while (true) { + // Wait till we are allowed to produce data + SDL_CondWait(_soundCond, _soundMutex); + + if (_soundThreadShouldQuit) + break; + + // Generate samples and put them into the next buffer + nextSoundBuffer = _activeSoundBuf ^ 1; + _mixer->mixCallback(_soundBuffers[nextSoundBuffer], _soundBufSize); + + // Swap buffers + _activeSoundBuf = nextSoundBuffer; + } + SDL_UnlockMutex(_soundMutex); +} + +int SDLCALL OSystem_SDL::mixerProducerThreadEntry(void *arg) { + OSystem_SDL *this_ = (OSystem_SDL *)arg; + assert(this_); + this_->mixerProducerThread(); + return 0; +} + + +void OSystem_SDL::initThreadedMixer(Audio::MixerImpl *mixer, uint bufSize) { + _soundThreadIsRunning = false; + _soundThreadShouldQuit = false; + + // Create mutex and condition variable + _soundMutex = SDL_CreateMutex(); + _soundCond = SDL_CreateCond(); + + // Create two sound buffers + _activeSoundBuf = 0; + _soundBufSize = bufSize; + _soundBuffers[0] = (byte *)calloc(1, bufSize); + _soundBuffers[1] = (byte *)calloc(1, bufSize); + + _soundThreadIsRunning = true; + + // Finally start the thread + _soundThread = SDL_CreateThread(mixerProducerThreadEntry, this); +} + +void OSystem_SDL::deinitThreadedMixer() { + // Kill thread?? _soundThread + + if (_soundThreadIsRunning) { + // Signal the producer thread to end, and wait for it to actually finish. + _soundThreadShouldQuit = true; + SDL_CondBroadcast(_soundCond); + SDL_WaitThread(_soundThread, NULL); + + // Kill the mutex & cond variables. + // Attention: AT this point, the mixer callback must not be running + // anymore, else we will crash! + SDL_DestroyMutex(_soundMutex); + SDL_DestroyCond(_soundCond); + + _soundThreadIsRunning = false; + + free(_soundBuffers[0]); + free(_soundBuffers[1]); + } +} + + +void OSystem_SDL::mixCallback(void *arg, byte *samples, int len) { + OSystem_SDL *this_ = (OSystem_SDL *)arg; + assert(this_); + assert(this_->_mixer); + + assert((int)this_->_soundBufSize == len); + + // Lock mutex, to ensure our data is not overwritten by the producer thread + SDL_LockMutex(this_->_soundMutex); + + // Copy data from the current sound buffer + memcpy(samples, this_->_soundBuffers[this_->_activeSoundBuf], len); + + // Unlock mutex and wake up the produced thread + SDL_UnlockMutex(this_->_soundMutex); + SDL_CondSignal(this_->_soundCond); +} + +#else + void OSystem_SDL::mixCallback(void *sys, byte *samples, int len) { OSystem_SDL *this_ = (OSystem_SDL *)sys; assert(this_); + assert(this_->_mixer); - if (this_->_mixer) - this_->_mixer->mixCallback(samples, len); + this_->_mixer->mixCallback(samples, len); } +#endif + void OSystem_SDL::setupMixer() { SDL_AudioSpec desired; SDL_AudioSpec obtained; @@ -443,10 +537,30 @@ void OSystem_SDL::setupMixer() { // Tell the mixer that we are ready and start the sound processing _mixer->setOutputRate(_samplesPerSec); _mixer->setReady(true); + +#ifdef MIXER_DOUBLE_BUFFERING + initThreadedMixer(_mixer, obtained.samples * 4); +#endif + + // start the sound system SDL_PauseAudio(0); } } +void OSystem_SDL::closeMixer() { + if (_mixer) + _mixer->setReady(false); + delete _mixer; + _mixer = 0; + + SDL_CloseAudio(); + +#ifdef MIXER_DOUBLE_BUFFERING + deinitThreadedMixer(); +#endif + +} + Audio::Mixer *OSystem_SDL::getMixer() { assert(_mixer); return _mixer; -- cgit v1.2.3 From cb366384f7f6a7d45ace3d67f8df529fe8312584 Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Fri, 18 Jul 2008 09:36:49 +0000 Subject: In OSystem_SDL::closeMixer moved the call to SDL_CloseAudio to before the deletion of the _mixer variable in to fix an assert that was being generated in OSystem_SDL::mixCallback svn-id: r33095 --- backends/platform/sdl/sdl.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'backends/platform/sdl/sdl.cpp') diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp index cc5f2478e5..76ac91c282 100644 --- a/backends/platform/sdl/sdl.cpp +++ b/backends/platform/sdl/sdl.cpp @@ -550,11 +550,12 @@ void OSystem_SDL::setupMixer() { void OSystem_SDL::closeMixer() { if (_mixer) _mixer->setReady(false); - delete _mixer; - _mixer = 0; SDL_CloseAudio(); + delete _mixer; + _mixer = 0; + #ifdef MIXER_DOUBLE_BUFFERING deinitThreadedMixer(); #endif -- cgit v1.2.3 From 4a9104515c1ddc5ac7aee3fa075f3c43b9752898 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 29 Jul 2008 00:02:06 +0000 Subject: SDL: Properly init vars related to mixer double buffering svn-id: r33385 --- backends/platform/sdl/sdl.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'backends/platform/sdl/sdl.cpp') diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp index 76ac91c282..d8394b5c9c 100644 --- a/backends/platform/sdl/sdl.cpp +++ b/backends/platform/sdl/sdl.cpp @@ -170,6 +170,10 @@ OSystem_SDL::OSystem_SDL() _joystick(0), _currentShakePos(0), _newShakePos(0), _paletteDirtyStart(0), _paletteDirtyEnd(0), +#ifdef MIXER_DOUBLE_BUFFERING + _soundMutex(0), _soundCond(0), _soundThread(0), + _soundThreadIsRunning(false), _soundThreadShouldQuit(false), +#endif _savefile(0), _mixer(0), _timer(0), -- cgit v1.2.3