From c400405b0a4c63debe7fea0dfc9ec6e5343b0fba Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 1 Jan 2016 14:53:02 -0600 Subject: [PATCH] Chatter-queue moved out of globals - now lives as part of the sound-manager, yay. --- src/Main/fg_commands.cxx | 41 ------------------------------------ src/Main/fg_init.cxx | 1 - src/Main/globals.cxx | 15 +------------ src/Main/globals.hxx | 8 +------ src/Sound/soundmanager.cxx | 43 +++++++++++++++++++++++++++++++++++++- src/Sound/soundmanager.hxx | 7 ++++++- 6 files changed, 50 insertions(+), 65 deletions(-) diff --git a/src/Main/fg_commands.cxx b/src/Main/fg_commands.cxx index 3151b2bf6..a7a5945dc 100644 --- a/src/Main/fg_commands.cxx +++ b/src/Main/fg_commands.cxx @@ -1061,46 +1061,6 @@ do_add_model (const SGPropertyNode * arg) return true; } -/** - * Built-in command: play an audio message (i.e. a wav file) This is - * fire and forget. Call this once per message and it will get dumped - * into a queue. Messages are played sequentially so they do not - * overlap. - */ -static bool -do_play_audio_sample (const SGPropertyNode * arg) -{ - SGSoundMgr *smgr = globals->get_subsystem(); - if (!smgr) { - SG_LOG(SG_GENERAL, SG_WARN, "play-audio-sample: sound-manager not running"); - return false; - } - - string path = arg->getStringValue("path"); - string file = arg->getStringValue("file"); - float volume = arg->getFloatValue("volume"); - // cout << "playing " << path << " / " << file << endl; - try { - FGSampleQueue *queue = globals->get_chatter_queue(); - if ( !queue ) { - queue = new FGSampleQueue(smgr, "chatter"); - queue->tie_to_listener(); - globals->set_chatter_queue(queue); - } - - SGSoundSample *msg = new SGSoundSample(file.c_str(), path); - msg->set_volume( volume ); - queue->add( msg ); - - return true; - - } catch (const sg_io_exception&) { - SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: " - "failed to load" << path << '/' << file); - return false; - } -} - /** * Built-in command: commit presets (read from in /sim/presets/) */ @@ -1470,7 +1430,6 @@ static struct { { "open-browser", do_open_browser }, { "gui-redraw", do_gui_redraw }, { "add-model", do_add_model }, - { "play-audio-sample", do_play_audio_sample }, { "presets-commit", do_presets_commit }, { "log-level", do_log_level }, { "replay", do_replay }, diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index 64232f96e..b2639671d 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -1069,7 +1069,6 @@ void fgStartNewReset() globals->set_renderer(NULL); globals->set_matlib(NULL); - globals->set_chatter_queue(NULL); simgear::clearEffectCache(); simgear::SGModelLib::resetPropertyRoot(); diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx index 42a27bd46..58fb75eec 100644 --- a/src/Main/globals.cxx +++ b/src/Main/globals.cxx @@ -54,7 +54,6 @@ #include #include #include -#include #include "globals.hxx" #include "locale.hxx" @@ -160,8 +159,7 @@ FGGlobals::FGGlobals() : channel_options_list( NULL ), initial_waypoints( NULL ), channellist( NULL ), - haveUserSettings(false), - _chatter_queue(NULL) + haveUserSettings(false) { SGPropertyNode* root = new SGPropertyNode; props = SGPropertyNode_ptr(root); @@ -224,7 +222,6 @@ FGGlobals::~FGGlobals() // renderer touches subsystems during its destruction set_renderer(NULL); - _chatter_queue.clear(); delete subsystem_mgr; subsystem_mgr = NULL; // important so ::get_subsystem returns NULL @@ -720,16 +717,6 @@ FGControls *FGGlobals::get_controls() const return get_subsystem(); } -FGSampleQueue* FGGlobals::get_chatter_queue() const -{ - return _chatter_queue; -} - -void FGGlobals::set_chatter_queue(FGSampleQueue* queue) -{ - _chatter_queue = queue; -} - void FGGlobals::addListenerToCleanup(SGPropertyChangeListener* l) { _listeners_to_cleanup.push_back(l); diff --git a/src/Main/globals.hxx b/src/Main/globals.hxx index 4951792c9..34eff9c6f 100644 --- a/src/Main/globals.hxx +++ b/src/Main/globals.hxx @@ -63,7 +63,6 @@ class FGTileMgr; class FGViewMgr; class FGViewer; class FGRenderer; -class FGSampleQueue; namespace simgear { namespace pkg { class Root; @@ -140,9 +139,7 @@ private: * helper to initialise standard properties on a new property tree */ void initProperties(); - - SGSharedPtr _chatter_queue; - + void cleanupListeners(); typedef std::vector SGPropertyChangeListenerVec; @@ -341,9 +338,6 @@ public: */ void saveUserSettings(); - FGSampleQueue* get_chatter_queue() const; - void set_chatter_queue(FGSampleQueue* queue); - void addListenerToCleanup(SGPropertyChangeListener* l); simgear::pkg::Root* packageRoot(); diff --git a/src/Sound/soundmanager.cxx b/src/Sound/soundmanager.cxx index bb1b83418..0637b96a0 100644 --- a/src/Sound/soundmanager.cxx +++ b/src/Sound/soundmanager.cxx @@ -22,10 +22,13 @@ #endif #include +#include #if defined(ENABLE_FLITE) #include "VoiceSynthesizer.hxx" #endif + +#include "sample_queue.hxx" #include "soundmanager.hxx" #include "Main/globals.hxx" #include "Main/fg_props.hxx" @@ -87,6 +90,9 @@ void FGSoundManager::init() SGPropertyNode_ptr scenery_loaded = fgGetNode("sim/sceneryloaded", true); scenery_loaded->addChangeListener(_listener.get()); + globals->get_commands()->addCommand("play-audio-sample", this, &FGSoundManager::playAudioSampleCommand); + + reinit(); } @@ -96,7 +102,11 @@ void FGSoundManager::shutdown() scenery_loaded->removeChangeListener(_listener.get()); stop(); - + + _chatterQueue.clear(); + globals->get_commands()->removeCommand("play-audio-sample"); + + SGSoundMgr::shutdown(); } @@ -202,6 +212,37 @@ void FGSoundManager::update(double dt) } } +/** + * Built-in command: play an audio message (i.e. a wav file) This is + * fire and forget. Call this once per message and it will get dumped + * into a queue. Messages are played sequentially so they do not + * overlap. + */ +bool FGSoundManager::playAudioSampleCommand(const SGPropertyNode * arg) +{ + string path = arg->getStringValue("path"); + string file = arg->getStringValue("file"); + float volume = arg->getFloatValue("volume"); + // cout << "playing " << path << " / " << file << endl; + try { + if ( !_chatterQueue ) { + _chatterQueue = new FGSampleQueue(this, "chatter"); + _chatterQueue->tie_to_listener(); + } + + SGSoundSample *msg = new SGSoundSample(file.c_str(), path); + msg->set_volume( volume ); + _chatterQueue->add( msg ); + + return true; + + } catch (const sg_io_exception&) { + SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: " + "failed to load" << path << '/' << file); + return false; + } +} + #if defined(ENABLE_FLITE) VoiceSynthesizer * FGSoundManager::getSynthesizer( const std::string & voice ) { diff --git a/src/Sound/soundmanager.hxx b/src/Sound/soundmanager.hxx index a22b73884..cebf34806 100644 --- a/src/Sound/soundmanager.hxx +++ b/src/Sound/soundmanager.hxx @@ -26,6 +26,7 @@ #include #include +class FGSampleQueue; class SGSoundMgr; class Listener; #if defined(ENABLE_FLITE) @@ -51,7 +52,11 @@ public: #endif private: bool stationaryView() const; - + + bool playAudioSampleCommand(const SGPropertyNode * arg); + + SGSharedPtr _chatterQueue; + bool _is_initialized, _enabled; SGPropertyNode_ptr _sound_working, _sound_enabled, _volume, _device_name; SGPropertyNode_ptr _currentView; -- 2.39.5