From 4794ab60952c8aed3d253504dea50557ad0f60c8 Mon Sep 17 00:00:00 2001 From: ehofman Date: Wed, 2 Dec 2009 08:32:59 +0000 Subject: [PATCH] Fix runtime switching of sound devices. --- simgear/sound/sample_group.cxx | 10 ++++--- simgear/sound/soundmgr_openal.cxx | 45 ++++++++++++++++++------------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/simgear/sound/sample_group.cxx b/simgear/sound/sample_group.cxx index de173dbd..72ea9a53 100644 --- a/simgear/sound/sample_group.cxx +++ b/simgear/sound/sample_group.cxx @@ -271,17 +271,21 @@ SGSampleGroup::stop () SGSoundSample *sample = sample_current->second; if ( sample->is_valid_source() ) { + ALint source = sample->get_source(); if ( sample->is_playing() ) { - alSourcePause( sample->get_source() ); + alSourceStop( source ); + alSourcei( source, AL_BUFFER, 0 ); } - _smgr->release_source( sample->get_source() ); + _smgr->release_source( source ); sample->no_valid_source(); + } + if (sample->is_valid_buffer() ) { _smgr->release_buffer( sample ); sample->no_valid_buffer(); } } - testForALError("suspend"); + testForALError("stop"); } // resume playing all associated samples diff --git a/simgear/sound/soundmgr_openal.cxx b/simgear/sound/soundmgr_openal.cxx index 6ead4d19..6100dfcf 100644 --- a/simgear/sound/soundmgr_openal.cxx +++ b/simgear/sound/soundmgr_openal.cxx @@ -186,27 +186,36 @@ void SGSoundMgr::activate() { // stop the sound manager void SGSoundMgr::stop() { - if (_working) { - _working = false; - _active = false; - // clear any OpenAL buffers before shutting down - buffer_map_iterator buffers_current = _buffers.begin(); - buffer_map_iterator buffers_end = _buffers.end(); - for ( ; buffers_current != buffers_end; ++buffers_current ) { - refUint ref = buffers_current->second; - ALuint buffer = ref.id; - alDeleteBuffers(1, &buffer); - } - _buffers.clear(); + // first stop all sample groups + sample_group_map_iterator sample_grp_current = _sample_groups.begin(); + sample_group_map_iterator sample_grp_end = _sample_groups.end(); + for ( ; sample_grp_current != sample_grp_end; ++sample_grp_current ) { + SGSampleGroup *sgrp = sample_grp_current->second; + sgrp->stop(); + } - sample_group_map_iterator sample_grp_current = _sample_groups.begin(); - sample_group_map_iterator sample_grp_end = _sample_groups.end(); - for ( ; sample_grp_current != sample_grp_end; ++sample_grp_current ) { - SGSampleGroup *sgrp = sample_grp_current->second; - sgrp->stop(); - } + // clear all OpenAL sources + for (unsigned int i=0; i<_free_sources.size(); i++) { + ALuint source = _free_sources[i]; + alDeleteSources( 1 , &source ); + } + _free_sources.clear(); + // clear any OpenAL buffers before shutting down + buffer_map_iterator buffers_current = _buffers.begin(); + buffer_map_iterator buffers_end = _buffers.end(); + for ( ; buffers_current != buffers_end; ++buffers_current ) { + refUint ref = buffers_current->second; + ALuint buffer = ref.id; + alDeleteBuffers(1, &buffer); + _buffers.erase( buffers_current ); + } + _buffers.clear(); + + if (_working) { + _working = false; + _active = false; _context = alcGetCurrentContext(); _device = alcGetContextsDevice(_context); alcDestroyContext(_context); -- 2.39.5