From f9445874a02689cbd9a0197c5aac7ffeab1436ac Mon Sep 17 00:00:00 2001 From: ehofman Date: Sat, 24 Oct 2009 08:31:37 +0000 Subject: [PATCH] Don't make the SoundManager a memebr of the subsystem manager; It needs to be initialized very early and destroyed as late as possible. That doesn't work as a subsystem meber onless some sort of priority scheme gets implemented. Get rid of auto_ptr which doesn work for the samples either. --- src/ATCDCL/AIPlane.cxx | 13 ++++--------- src/ATCDCL/ATC.cxx | 15 +++++---------- src/ATCDCL/ATCVoice.cxx | 22 ++++++++++++---------- src/ATCDCL/ATCVoice.hxx | 4 ++-- src/Environment/fgclouds.cxx | 2 +- src/Instrumentation/adf.cxx | 2 +- src/Instrumentation/kr_87.cxx | 2 +- src/Instrumentation/marker_beacon.cxx | 2 +- src/Instrumentation/mk_viii.cxx | 2 +- src/Instrumentation/navradio.cxx | 2 +- src/Main/fg_commands.cxx | 2 +- src/Main/fg_init.cxx | 7 +++++++ src/Main/fg_props.cxx | 2 +- src/Main/globals.cxx | 14 ++++++++++++-- src/Main/globals.hxx | 4 ++++ src/Main/main.cxx | 9 +-------- src/Main/viewmgr.cxx | 2 +- src/Model/acmodel.cxx | 3 +-- src/Sound/beacon.cxx | 23 +++++++++++------------ src/Sound/morse.cxx | 8 +++----- 20 files changed, 71 insertions(+), 69 deletions(-) diff --git a/src/ATCDCL/AIPlane.cxx b/src/ATCDCL/AIPlane.cxx index 1a3e77dc0..3a46ee051 100644 --- a/src/ATCDCL/AIPlane.cxx +++ b/src/ATCDCL/AIPlane.cxx @@ -24,7 +24,6 @@ #include #include #include -#include using std::string; @@ -52,8 +51,7 @@ FGAIPlane::FGAIPlane() { _rollSuspended = false; if ( !_sgr ) { - SGSoundMgr *smgr; - smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); _sgr = smgr->find("atc", true); _sgr->tie_to_listener(); } @@ -199,13 +197,10 @@ void FGAIPlane::Render(const string& refname, const float volume, bool repeating #ifdef ENABLE_AUDIO_SUPPORT voice = (voiceOK && fgGetBool("/sim/sound/voice")); if(voice) { - string buf = vPtr->WriteMessage((char*)pending_transmission.c_str(), voice); + sizte_t len; + void* buf = vPtr->WriteMessage((char*)pending_transmission.c_str(), voice, &len); if(voice && (volume > 0.05)) { - std::auto_ptr ptr( (unsigned char*)buf.c_str() ); - SGSoundSample* simple = - new SGSoundSample(ptr, buf.length(), 8000 ); - // TODO - at the moment the volume can't be changed - // after the transmission has started. + SGSoundSample* simple = new SGSoundSample(buf, len, 8000 ); simple->set_volume(volume); _sgr->add(simple, refname); _sgr->play(refname, repeating); diff --git a/src/ATCDCL/ATC.cxx b/src/ATCDCL/ATC.cxx index 9a8d16028..7e17f1da7 100644 --- a/src/ATCDCL/ATC.cxx +++ b/src/ATCDCL/ATC.cxx @@ -25,7 +25,6 @@ #include "ATC.hxx" #include -#include #include #include @@ -57,8 +56,7 @@ FGATC::FGATC() : _counter(0.0), _max_count(5.0) { - SGSoundMgr *smgr; - smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); _sgr = smgr->find("atc", true); } @@ -231,18 +229,15 @@ void FGATC::Render(string& msg, const float volume, #ifdef ENABLE_AUDIO_SUPPORT _voice = (_voiceOK && fgGetBool("/sim/sound/voice")); if(_voice) { - string buf = _vPtr->WriteMessage((char*)msg.c_str(), _voice); - if(_voice && (volume > 0.05)) { + size_t len; + void* buf = _vPtr->WriteMessage((char*)msg.c_str(), &len); + if(buf && (volume > 0.05)) { NoRender(refname); try { // >>> Beware: must pass a (new) object to the (add) method, // >>> because the (remove) method is going to do a (delete) // >>> whether that's what you want or not. - std::auto_ptr ptr( (unsigned char*)buf.c_str() ); - SGSoundSample *simple = - new SGSoundSample(ptr, buf.length(), 8000); - // TODO - at the moment the volume can't be changed - // after the transmission has started. + SGSoundSample *simple = new SGSoundSample(&buf, len, 8000); simple->set_volume(volume); _sgr->add(simple, refname); _sgr->play(refname, repeating); diff --git a/src/ATCDCL/ATCVoice.cxx b/src/ATCDCL/ATCVoice.cxx index bcd48f54a..5659b1132 100644 --- a/src/ATCDCL/ATCVoice.cxx +++ b/src/ATCDCL/ATCVoice.cxx @@ -72,7 +72,7 @@ bool FGATCVoice::LoadVoice(const string& voice) { string full_path = path.str(); int format, freq; - SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); void *data; if (!smgr->load(full_path, &data, &format, &rawDataSize, &freq)) return false; @@ -134,7 +134,7 @@ typedef tokenList_type::iterator tokenList_iterator; // Given a desired message, return a string containing the // sound-sample data -string FGATCVoice::WriteMessage(const char* message, bool& dataOK) { +void* FGATCVoice::WriteMessage(const char* message, size_t* len) { // What should we do here? // First - parse the message into a list of tokens. @@ -185,8 +185,8 @@ string FGATCVoice::WriteMessage(const char* message, bool& dataOK) { // Check for no tokens found else slScheduler can be crashed if(!word) { - dataOK = false; - return ""; + *len = 0; + return NULL; } boost::shared_array tmpbuf(new char[cumLength]); unsigned int bufpos = 0; @@ -202,8 +202,8 @@ string FGATCVoice::WriteMessage(const char* message, bool& dataOK) { SG_LOG(SG_ATC, SG_ALERT, "Offset + length: " << wdptr[i].offset + wdptr[i].length << " exceeds rawdata size: " << rawDataSize << endl); - dataOK = false; - return ""; + *len = 0; + return NULL; } memcpy(tmpbuf.get() + bufpos, rawSoundData + wdptr[i].offset, wdptr[i].length); bufpos += wdptr[i].length; @@ -213,9 +213,11 @@ string FGATCVoice::WriteMessage(const char* message, bool& dataOK) { unsigned int offsetIn = (int)(cumLength * sg_random()); if(offsetIn > cumLength) offsetIn = cumLength; - string front(tmpbuf.get(), offsetIn); - string back(tmpbuf.get() + offsetIn, cumLength - offsetIn); + void *data = malloc(cumLength); + memcpy(data, tmpbuf.get(), cumLength); + *len = cumLength; + // string front(tmpbuf.get(), offsetIn); + // string back(tmpbuf.get() + offsetIn, cumLength - offsetIn); - dataOK = true; - return back + front; + return data; } diff --git a/src/ATCDCL/ATCVoice.hxx b/src/ATCDCL/ATCVoice.hxx index f4b6c7f50..6ec4f3932 100644 --- a/src/ATCDCL/ATCVoice.hxx +++ b/src/ATCDCL/ATCVoice.hxx @@ -49,8 +49,8 @@ public: bool LoadVoice(const std::string& voice); // Given a desired message, return a pointer to the data buffer and write the buffer length into len. - // Sets dataOK = true if the returned buffer is valid. - std::string WriteMessage(const char* message, bool& dataOK); + // Sets len to something other than 0 if the returned buffer is valid. + void* WriteMessage(const char* message, size_t *len); private: diff --git a/src/Environment/fgclouds.cxx b/src/Environment/fgclouds.cxx index a4264bc58..1498a00f4 100644 --- a/src/Environment/fgclouds.cxx +++ b/src/Environment/fgclouds.cxx @@ -68,7 +68,7 @@ void FGClouds::init(void) { snd_lightning = new SGSoundSample(globals->get_fg_root().c_str(), "Sounds/thunder.wav"); snd_lightning->set_max_dist(7000.0f); snd_lightning->set_reference_dist(3000.0f); - SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); SGSampleGroup *sgr = smgr->find("weather", true); sgr->add( snd_lightning, "thunder" ); sgEnviro.set_sampleGroup( sgr ); diff --git a/src/Instrumentation/adf.cxx b/src/Instrumentation/adf.cxx index 9f9ba2d83..6da2a2ed4 100644 --- a/src/Instrumentation/adf.cxx +++ b/src/Instrumentation/adf.cxx @@ -102,7 +102,7 @@ ADF::init () _ident_node = node->getChild("ident", 0, true); _ident_audible_node = node->getChild("ident-audible", 0, true); - SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); _sgr = smgr->find("avionics", true); _sgr->tie_to_listener(); diff --git a/src/Instrumentation/kr_87.cxx b/src/Instrumentation/kr_87.cxx index d0087c098..41928a097 100644 --- a/src/Instrumentation/kr_87.cxx +++ b/src/Instrumentation/kr_87.cxx @@ -117,7 +117,7 @@ FGKR_87::~FGKR_87() { void FGKR_87::init () { - SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); _sgr = smgr->find("avionics", true); _sgr->tie_to_listener(); morse.init(); diff --git a/src/Instrumentation/marker_beacon.cxx b/src/Instrumentation/marker_beacon.cxx index 3a0d33819..d2e9774b7 100644 --- a/src/Instrumentation/marker_beacon.cxx +++ b/src/Instrumentation/marker_beacon.cxx @@ -117,7 +117,7 @@ FGMarkerBeacon::init () if (serviceable->getType() == simgear::props::NONE) serviceable->setBoolValue( true ); - SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); _sgr = smgr->find("avionics", true); _sgr->tie_to_listener(); diff --git a/src/Instrumentation/mk_viii.cxx b/src/Instrumentation/mk_viii.cxx index 340c4bf11..f2a4dba2c 100755 --- a/src/Instrumentation/mk_viii.cxx +++ b/src/Instrumentation/mk_viii.cxx @@ -2200,7 +2200,7 @@ MK_VIII::VoicePlayer::init () { #define STDPAUSE 0.75 // [SPEC] 6.4.4: "the standard 0.75 second delay" - SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); _sgr = smgr->find("avionics", true); _sgr->tie_to_listener(); diff --git a/src/Instrumentation/navradio.cxx b/src/Instrumentation/navradio.cxx index 79f864c33..4dace88a0 100644 --- a/src/Instrumentation/navradio.cxx +++ b/src/Instrumentation/navradio.cxx @@ -183,7 +183,7 @@ FGNavRadio::~FGNavRadio() void FGNavRadio::init () { - SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); _sgr = smgr->find("avionics", true); _sgr->tie_to_listener(); diff --git a/src/Main/fg_commands.cxx b/src/Main/fg_commands.cxx index 52b892a41..151efa207 100644 --- a/src/Main/fg_commands.cxx +++ b/src/Main/fg_commands.cxx @@ -1259,7 +1259,7 @@ do_play_audio_sample (const SGPropertyNode * arg) try { static FGSampleQueue *queue = 0; if ( !queue ) { - SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); queue = new FGSampleQueue(smgr, "queue"); queue->tie_to_listener(); } diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index a78759c42..cf6c86b70 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -1448,6 +1448,13 @@ bool fgInitSubsystems() { globals->get_event_mgr()->init(); globals->get_event_mgr()->setRealtimeProperty(fgGetNode("/sim/time/delta-realtime-sec", true)); + //////////////////////////////////////////////////////////////////// + // Initialize the sound manager subsystem. + //////////////////////////////////////////////////////////////////// + + globals->get_soundmgr()->bind(); + globals->get_soundmgr()->init(); + //////////////////////////////////////////////////////////////////// // Initialize the property interpolator subsystem. Put into the INIT // group because the "nasal" subsystem may need it at GENERAL take-down. diff --git a/src/Main/fg_props.cxx b/src/Main/fg_props.cxx index 6a54db665..f18c5fb92 100644 --- a/src/Main/fg_props.cxx +++ b/src/Main/fg_props.cxx @@ -222,7 +222,7 @@ setFreeze (bool f) frozen = f; // Stop sound on a pause - SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); if ( smgr != NULL ) { if ( f ) { smgr->suspend(); diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx index 7e6553fa4..da3de6a46 100644 --- a/src/Main/globals.cxx +++ b/src/Main/globals.cxx @@ -15,8 +15,8 @@ // General Public License for more details. // // You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// along with this program; if not, write to the Free Software Foundation, +// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // $Id$ @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -72,6 +73,7 @@ FGGlobals::FGGlobals() : renderer( new FGRenderer ), subsystem_mgr( new SGSubsystemMgr ), event_mgr( new SGEventMgr ), + soundmgr( new SGSoundMgr ), sim_time_sec( 0.0 ), fg_root( "" ), warp( 0 ), @@ -158,6 +160,9 @@ FGGlobals::~FGGlobals() delete channellist; delete airwaynet; delete multiplayer_mgr; + + soundmgr->unbind(); + delete soundmgr; } @@ -259,6 +264,11 @@ FGGlobals::add_subsystem (const char * name, subsystem_mgr->add(name, subsystem, type, min_time_sec); } +SGSoundMgr * +FGGlobals::get_soundmgr () const +{ + return soundmgr; +} SGEventMgr * FGGlobals::get_event_mgr () const diff --git a/src/Main/globals.hxx b/src/Main/globals.hxx index e778590dd..06490eaf3 100644 --- a/src/Main/globals.hxx +++ b/src/Main/globals.hxx @@ -52,6 +52,7 @@ class SGTime; class SGEventMgr; class SGSubsystemMgr; class SGSubsystem; +class SGSoundMgr; class FGAIMgr; class FGATCMgr; @@ -93,6 +94,7 @@ private: FGRenderer *renderer; SGSubsystemMgr *subsystem_mgr; SGEventMgr *event_mgr; + SGSoundMgr *soundmgr; // Number of milliseconds elapsed since the start of the program. double sim_time_sec; @@ -198,6 +200,8 @@ public: virtual SGEventMgr *get_event_mgr () const; + virtual SGSoundMgr *get_soundmgr () const; + inline double get_sim_time_sec () const { return sim_time_sec; } inline void inc_sim_time_sec (double dt) { sim_time_sec += dt; } inline void set_sim_time_sec (double t) { sim_time_sec = t; } diff --git a/src/Main/main.cxx b/src/Main/main.cxx index 30741f1c1..4ab21a885 100644 --- a/src/Main/main.cxx +++ b/src/Main/main.cxx @@ -482,7 +482,7 @@ static void fgMainLoop( void ) { // is processing the scenery (doubled the frame-rate for me) -EMH- //////////////////////////////////////////////////////////////////// #ifdef ENABLE_AUDIO_SUPPORT - static SGSoundMgr *smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + static SGSoundMgr *smgr = globals->get_soundmgr(); smgr->update_late(delta_time_sec); #endif @@ -636,13 +636,6 @@ static void fgIdleFunction ( void ) { } else if ( idle_state == 5 ) { idle_state++; -#ifdef ENABLE_AUDIO_SUPPORT - //////////////////////////////////////////////////////////////////// - // Add the Sound Manager before any other subsystem that uses it. - // This makes sure the SoundMgr is available at construction time. - //////////////////////////////////////////////////////////////////// - globals->add_subsystem("soundmgr", new SGSoundMgr); -#endif //////////////////////////////////////////////////////////////////// // Initialize the 3D aircraft model subsystem (has a dependency on diff --git a/src/Main/viewmgr.cxx b/src/Main/viewmgr.cxx index a23a28d79..6074dcf95 100644 --- a/src/Main/viewmgr.cxx +++ b/src/Main/viewmgr.cxx @@ -43,7 +43,7 @@ FGViewMgr::FGViewMgr( void ) : config_list(fgGetNode("/sim", true)->getChildren("view")), current(0) { - smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + smgr = globals->get_soundmgr(); } // Destructor diff --git a/src/Model/acmodel.cxx b/src/Model/acmodel.cxx index 1dcf2bd22..65104a990 100644 --- a/src/Model/acmodel.cxx +++ b/src/Model/acmodel.cxx @@ -48,8 +48,7 @@ FGAircraftModel::FGAircraftModel () _speed_e(0), _speed_d(0) { - SGSoundMgr *smgr; - smgr = (SGSoundMgr *)globals->get_subsystem("soundmgr"); + SGSoundMgr *smgr = globals->get_soundmgr(); _fx = new FGFX(smgr, "fx"); _fx->init(); } diff --git a/src/Sound/beacon.cxx b/src/Sound/beacon.cxx index 497cbf032..4505017c9 100644 --- a/src/Sound/beacon.cxx +++ b/src/Sound/beacon.cxx @@ -21,7 +21,7 @@ // $Id$ -#include +#include #include "beacon.hxx" @@ -39,13 +39,12 @@ FGBeacon::~FGBeacon() { // allocate and initialize sound samples bool FGBeacon::init() { - int i; - int len; unsigned char *ptr; + size_t i, len; - std::auto_ptrinner_buf( new unsigned char[ INNER_SIZE ] ); - std::auto_ptrmiddle_buf( new unsigned char[ MIDDLE_SIZE ] ); - std::auto_ptrouter_buf( new unsigned char[ OUTER_SIZE ] ); + const unsigned char* inner_buf = (const unsigned char*)malloc( INNER_SIZE ); + const unsigned char* middle_buf = (const unsigned char*)malloc(MIDDLE_SIZE); + const unsigned char* outer_buf = (const unsigned char*)malloc( OUTER_SIZE ); // Make inner marker beacon sound len= (int)(INNER_DIT_LEN / 2.0 ); @@ -53,14 +52,14 @@ bool FGBeacon::init() { make_tone( inner_dit, INNER_FREQ, len, INNER_DIT_LEN, TRANSITION_BYTES ); - ptr = inner_buf.get(); + ptr = (unsigned char*)inner_buf; for ( i = 0; i < 6; ++i ) { memcpy( ptr, inner_dit, INNER_DIT_LEN ); ptr += INNER_DIT_LEN; } try { - inner = new SGSoundSample( inner_buf, INNER_SIZE, BYTES_PER_SECOND ); + inner = new SGSoundSample( &inner_buf, INNER_SIZE, BYTES_PER_SECOND ); inner->set_reference_dist( 10.0 ); inner->set_max_dist( 20.0 ); @@ -75,12 +74,12 @@ bool FGBeacon::init() { make_tone( middle_dah, MIDDLE_FREQ, len, MIDDLE_DAH_LEN, TRANSITION_BYTES ); - ptr = middle_buf.get(); + ptr = (unsigned char*)middle_buf; memcpy( ptr, middle_dit, MIDDLE_DIT_LEN ); ptr += MIDDLE_DIT_LEN; memcpy( ptr, middle_dah, MIDDLE_DAH_LEN ); - middle = new SGSoundSample( middle_buf, MIDDLE_SIZE, BYTES_PER_SECOND ); + middle = new SGSoundSample( &middle_buf, MIDDLE_SIZE, BYTES_PER_SECOND); middle->set_reference_dist( 10.0 ); middle->set_max_dist( 20.0 ); @@ -90,12 +89,12 @@ bool FGBeacon::init() { make_tone( outer_dah, OUTER_FREQ, len, OUTER_DAH_LEN, TRANSITION_BYTES ); - ptr = outer_buf.get(); + ptr = (unsigned char*)outer_buf; memcpy( ptr, outer_dah, OUTER_DAH_LEN ); ptr += OUTER_DAH_LEN; memcpy( ptr, outer_dah, OUTER_DAH_LEN ); - outer = new SGSoundSample( outer_buf, OUTER_SIZE, BYTES_PER_SECOND ); + outer = new SGSoundSample( &outer_buf, OUTER_SIZE, BYTES_PER_SECOND ); outer->set_reference_dist( 10.0 ); outer->set_max_dist( 20.0 ); } catch ( sg_io_exception &e ) { diff --git a/src/Sound/morse.cxx b/src/Sound/morse.cxx index 0943b8a86..942b637a5 100644 --- a/src/Sound/morse.cxx +++ b/src/Sound/morse.cxx @@ -21,8 +21,6 @@ // $Id$ -#include - #include #include "morse.hxx" @@ -221,10 +219,10 @@ SGSoundSample *FGMorse::make_ident( const string& id, const int freq ) { length += 2 * SPACE_SIZE; // 2. Allocate space for the message - std::auto_ptrbuffer( new unsigned char[length] ); + const unsigned char* buffer = (const unsigned char *)malloc(length); // 3. Assemble the message; - unsigned char *buf_ptr = buffer.get(); + unsigned char *buf_ptr = (unsigned char*)buffer; for ( i = 0; i < (int)id.length(); ++i ) { if ( idptr[i] >= 'A' && idptr[i] <= 'Z' ) { @@ -263,7 +261,7 @@ SGSoundSample *FGMorse::make_ident( const string& id, const int freq ) { buf_ptr += SPACE_SIZE; // 4. create the simple sound and return - SGSoundSample *sample = new SGSoundSample( buffer, length, + SGSoundSample *sample = new SGSoundSample( &buffer, length, BYTES_PER_SECOND ); sample->set_reference_dist( 10.0 ); -- 2.39.5