From cc27909e6fbec22f03145da10340a9b7c1b5e569 Mon Sep 17 00:00:00 2001 From: fredb Date: Sun, 12 Feb 2006 19:57:57 +0000 Subject: [PATCH] Catch sound exceptions at the earliest, report problem has an alert, and continue without segfaulting ( hopefully ) --- src/ATC/ATC.cxx | 25 ++++++---- src/ATC/ATCmgr.cxx | 11 ++++- src/Instrumentation/marker_beacon.cxx | 18 ++++--- src/Instrumentation/navradio.cxx | 54 +++++++++++---------- src/Sound/beacon.cxx | 69 ++++++++++++++------------- src/Sound/fg_fx.cxx | 13 +++-- 6 files changed, 111 insertions(+), 79 deletions(-) diff --git a/src/ATC/ATC.cxx b/src/ATC/ATC.cxx index 9d0a37a04..096b7cdfc 100644 --- a/src/ATC/ATC.cxx +++ b/src/ATC/ATC.cxx @@ -23,6 +23,7 @@ #endif #include +#include #include
#include
@@ -231,16 +232,20 @@ void FGATC::Render(string& msg, const string& refname, bool repeating) { int len; unsigned char* buf = _vPtr->WriteMessage((char*)msg.c_str(), len, _voice); if(_voice) { - SGSoundSample *simple - = new SGSoundSample(buf, len, 8000); - // TODO - at the moment the volume is always set off comm1 - // and can't be changed after the transmission has started. - simple->set_volume(5.0 * fgGetDouble("/instrumentation/comm[0]/volume")); - globals->get_soundmgr()->add(simple, refname); - if(repeating) { - globals->get_soundmgr()->play_looped(refname); - } else { - globals->get_soundmgr()->play_once(refname); + try { + SGSoundSample *simple + = new SGSoundSample(buf, len, 8000); + // TODO - at the moment the volume is always set off comm1 + // and can't be changed after the transmission has started. + simple->set_volume(5.0 * fgGetDouble("/instrumentation/comm[0]/volume")); + globals->get_soundmgr()->add(simple, refname); + if(repeating) { + globals->get_soundmgr()->play_looped(refname); + } else { + globals->get_soundmgr()->play_once(refname); + } + } catch ( sg_io_exception &e ) { + SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage()); } } delete[] buf; diff --git a/src/ATC/ATCmgr.cxx b/src/ATC/ATCmgr.cxx index dfb483ace..12ac4aaa5 100644 --- a/src/ATC/ATCmgr.cxx +++ b/src/ATC/ATCmgr.cxx @@ -110,8 +110,15 @@ void FGATCMgr::init() { // For now we'll do one hardwired one v1 = new FGATCVoice; - voiceOK = v1->LoadVoice("default"); - voice = true; + try { + voiceOK = v1->LoadVoice("default"); + voice = true; + } catch ( sg_io_exception & ) { + voiceOK = false; + voice = false; + delete v1; + v1 = 0; + } /* I've loaded the voice even if /sim/sound/pause is true * since I know no way of forcing load of the voice if the user diff --git a/src/Instrumentation/marker_beacon.cxx b/src/Instrumentation/marker_beacon.cxx index 018d5baf7..910efc56d 100644 --- a/src/Instrumentation/marker_beacon.cxx +++ b/src/Instrumentation/marker_beacon.cxx @@ -293,8 +293,10 @@ void FGMarkerBeacon::search() if ( last_beacon != OUTER ) { if ( ! globals->get_soundmgr()->exists( "outer-marker" ) ) { SGSoundSample *sound = beacon.get_outer(); - sound->set_volume( 0.3 ); - globals->get_soundmgr()->add( sound, "outer-marker" ); + if ( sound ) { + sound->set_volume( 0.3 ); + globals->get_soundmgr()->add( sound, "outer-marker" ); + } } } if ( audio_btn->getBoolValue() ) { @@ -310,8 +312,10 @@ void FGMarkerBeacon::search() if ( last_beacon != MIDDLE ) { if ( ! globals->get_soundmgr()->exists( "middle-marker" ) ) { SGSoundSample *sound = beacon.get_middle(); - sound->set_volume( 0.3 ); - globals->get_soundmgr()->add( sound, "middle-marker" ); + if ( sound ) { + sound->set_volume( 0.3 ); + globals->get_soundmgr()->add( sound, "middle-marker" ); + } } } if ( audio_btn->getBoolValue() ) { @@ -327,8 +331,10 @@ void FGMarkerBeacon::search() if ( last_beacon != INNER ) { if ( ! globals->get_soundmgr()->exists( "inner-marker" ) ) { SGSoundSample *sound = beacon.get_inner(); - sound->set_volume( 0.3 ); - globals->get_soundmgr()->add( sound, "inner-marker" ); + if ( sound ) { + sound->set_volume( 0.3 ); + globals->get_soundmgr()->add( sound, "inner-marker" ); + } } } if ( audio_btn->getBoolValue() ) { diff --git a/src/Instrumentation/navradio.cxx b/src/Instrumentation/navradio.cxx index 399e3c507..466564010 100644 --- a/src/Instrumentation/navradio.cxx +++ b/src/Instrumentation/navradio.cxx @@ -905,32 +905,36 @@ void FGNavRadio::search() if ( globals->get_soundmgr()->exists( nav_fx_name ) ) { globals->get_soundmgr()->remove( nav_fx_name ); } - SGSoundSample *sound; - sound = morse.make_ident( trans_ident, LO_FREQUENCY ); - sound->set_volume( 0.3 ); - if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) { - // cout << "Added nav-vor-ident sound" << endl; - } else { - SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound"); + try { + SGSoundSample *sound; + sound = morse.make_ident( trans_ident, LO_FREQUENCY ); + sound->set_volume( 0.3 ); + if ( globals->get_soundmgr()->add( sound, nav_fx_name ) ) { + // cout << "Added nav-vor-ident sound" << endl; + } else { + SG_LOG(SG_COCKPIT, SG_WARN, "Failed to add v1-vor-ident sound"); + } + + if ( globals->get_soundmgr()->exists( dme_fx_name ) ) { + globals->get_soundmgr()->remove( dme_fx_name ); + } + sound = morse.make_ident( trans_ident, HI_FREQUENCY ); + sound->set_volume( 0.3 ); + globals->get_soundmgr()->add( sound, dme_fx_name ); + + int offset = (int)(sg_random() * 30.0); + play_count = offset / 4; + last_time = globals->get_time_params()->get_cur_time() - offset; + // cout << "offset = " << offset << " play_count = " + // << play_count << " last_time = " + // << last_time << " current time = " + // << globals->get_time_params()->get_cur_time() << endl; + + // cout << "Found a vor station in range" << endl; + // cout << " id = " << nav->get_ident() << endl; + } catch ( sg_io_exception &e ) { + SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage()); } - - if ( globals->get_soundmgr()->exists( dme_fx_name ) ) { - globals->get_soundmgr()->remove( dme_fx_name ); - } - sound = morse.make_ident( trans_ident, HI_FREQUENCY ); - sound->set_volume( 0.3 ); - globals->get_soundmgr()->add( sound, dme_fx_name ); - - int offset = (int)(sg_random() * 30.0); - play_count = offset / 4; - last_time = globals->get_time_params()->get_cur_time() - offset; - // cout << "offset = " << offset << " play_count = " - // << play_count << " last_time = " - // << last_time << " current time = " - // << globals->get_time_params()->get_cur_time() << endl; - - // cout << "Found a vor station in range" << endl; - // cout << " id = " << nav->get_ident() << endl; } } else { is_valid = false; diff --git a/src/Sound/beacon.cxx b/src/Sound/beacon.cxx index 5d9ae9594..4b3bd21d7 100644 --- a/src/Sound/beacon.cxx +++ b/src/Sound/beacon.cxx @@ -23,6 +23,7 @@ #include "beacon.hxx" +#include // constructor FGBeacon::FGBeacon() @@ -56,44 +57,48 @@ bool FGBeacon::init() { ptr += INNER_DIT_LEN; } - inner = new SGSoundSample( inner_buf, INNER_SIZE, BYTES_PER_SECOND ); - inner->set_reference_dist( 10.0 ); - inner->set_max_dist( 20.0 ); + try { + inner = new SGSoundSample( inner_buf, INNER_SIZE, BYTES_PER_SECOND ); + inner->set_reference_dist( 10.0 ); + inner->set_max_dist( 20.0 ); - // Make middle marker beacon sound - len= (int)(MIDDLE_DIT_LEN / 2.0 ); - unsigned char middle_dit[MIDDLE_DIT_LEN]; - make_tone( middle_dit, MIDDLE_FREQ, len, MIDDLE_DIT_LEN, - TRANSITION_BYTES ); + // Make middle marker beacon sound + len= (int)(MIDDLE_DIT_LEN / 2.0 ); + unsigned char middle_dit[MIDDLE_DIT_LEN]; + make_tone( middle_dit, MIDDLE_FREQ, len, MIDDLE_DIT_LEN, + TRANSITION_BYTES ); - len= (int)(MIDDLE_DAH_LEN * 3 / 4.0 ); - unsigned char middle_dah[MIDDLE_DAH_LEN]; - make_tone( middle_dah, MIDDLE_FREQ, len, MIDDLE_DAH_LEN, - TRANSITION_BYTES ); + len= (int)(MIDDLE_DAH_LEN * 3 / 4.0 ); + unsigned char middle_dah[MIDDLE_DAH_LEN]; + make_tone( middle_dah, MIDDLE_FREQ, len, MIDDLE_DAH_LEN, + TRANSITION_BYTES ); - ptr = middle_buf; - memcpy( ptr, middle_dit, MIDDLE_DIT_LEN ); - ptr += MIDDLE_DIT_LEN; - memcpy( ptr, middle_dah, MIDDLE_DAH_LEN ); + ptr = 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->set_reference_dist( 10.0 ); - middle->set_max_dist( 20.0 ); + middle = new SGSoundSample( middle_buf, MIDDLE_SIZE, BYTES_PER_SECOND ); + middle->set_reference_dist( 10.0 ); + middle->set_max_dist( 20.0 ); - // Make outer marker beacon sound - len= (int)(OUTER_DAH_LEN * 3.0 / 4.0 ); - unsigned char outer_dah[OUTER_DAH_LEN]; - make_tone( outer_dah, OUTER_FREQ, len, OUTER_DAH_LEN, - TRANSITION_BYTES ); - - ptr = outer_buf; - memcpy( ptr, outer_dah, OUTER_DAH_LEN ); - ptr += OUTER_DAH_LEN; - memcpy( ptr, outer_dah, OUTER_DAH_LEN ); + // Make outer marker beacon sound + len= (int)(OUTER_DAH_LEN * 3.0 / 4.0 ); + unsigned char outer_dah[OUTER_DAH_LEN]; + make_tone( outer_dah, OUTER_FREQ, len, OUTER_DAH_LEN, + TRANSITION_BYTES ); + + ptr = 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->set_reference_dist( 10.0 ); - outer->set_max_dist( 20.0 ); + 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 ) { + SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage()); + } return true; } diff --git a/src/Sound/fg_fx.cxx b/src/Sound/fg_fx.cxx index 7054e358e..5537bb119 100644 --- a/src/Sound/fg_fx.cxx +++ b/src/Sound/fg_fx.cxx @@ -85,7 +85,7 @@ FGFX::init() SGPropertyNode root; try { readProperties(path.str(), &root); - } catch (const sg_exception &e) { + } catch (const sg_exception &) { SG_LOG(SG_GENERAL, SG_ALERT, "Incorrect path specified in configuration file"); return; @@ -95,10 +95,15 @@ FGFX::init() for (i = 0; i < node->nChildren(); i++) { SGXmlSound *sound = new SGXmlSound(); - sound->init(globals->get_props(), node->getChild(i), - globals->get_soundmgr(), globals->get_fg_root()); + try { + sound->init(globals->get_props(), node->getChild(i), + globals->get_soundmgr(), globals->get_fg_root()); - _sound.push_back(sound); + _sound.push_back(sound); + } catch ( sg_io_exception &e ) { + SG_LOG(SG_GENERAL, SG_ALERT, e.getFormattedMessage()); + delete sound; + } } }