]> git.mxchange.org Git - flightgear.git/commitdiff
Tracked down a potential segfault when trying to audibly ident a vor station.
authorcurt <curt>
Thu, 18 Apr 2002 21:43:00 +0000 (21:43 +0000)
committercurt <curt>
Thu, 18 Apr 2002 21:43:00 +0000 (21:43 +0000)
This led to an investigation into why the ident playing didn't work anymore.
Recent changes in the sound manager broke some assumptions the radiostack
code was making.  These patches should hopefully fix all that back up.
Erik, please review these changes to double check I didn't do more damage
than good. :-)

src/Cockpit/radiostack.cxx
src/Sound/soundmgr.cxx
src/Sound/soundmgr.hxx

index c0b373600e8e4730ed65b869a8ed799d3e76f89a..643a6015935441771a0afbd388d4f703833b3b51 100644 (file)
@@ -473,20 +473,36 @@ FGRadioStack::update(int dt)
        if ( nav1_vol_btn > 0.1 && nav1_ident_btn ) {
            FGSimpleSound *sound;
            sound = globals->get_soundmgr()->find( "nav1-vor-ident" );
-           sound->set_volume( nav1_vol_btn * 0.3 );
+            if ( sound != NULL ) {
+                sound->set_volume( nav1_vol_btn );
+            } else {
+                SG_LOG( SG_COCKPIT, SG_ALERT,
+                        "Can't find nav1-vor-ident sound" );
+            }
            sound = globals->get_soundmgr()->find( "nav1-dme-ident" );
-           sound->set_volume( nav1_vol_btn * 0.3 );
+            if ( sound != NULL ) {
+                sound->set_volume( nav1_vol_btn );
+            } else {
+                SG_LOG( SG_COCKPIT, SG_ALERT,
+                        "Can't find nav1-dme-ident sound" );
+            }
+            // cout << "nav1_last_time = " << nav1_last_time << " ";
+            // cout << "cur_time = " << globals->get_time_params()->get_cur_time();
            if ( nav1_last_time <
                 globals->get_time_params()->get_cur_time() - 30 ) {
                nav1_last_time = globals->get_time_params()->get_cur_time();
                nav1_play_count = 0;
            }
+            // cout << " nav1_play_count = " << nav1_play_count << endl;
+            // cout << "playing = "
+            //      << globals->get_soundmgr()->is_playing("nav1-vor-ident")
+            //      << endl;
            if ( nav1_play_count < 4 ) {
                // play VOR ident
                if ( !globals->get_soundmgr()->is_playing("nav1-vor-ident") ) {
                    globals->get_soundmgr()->play_once( "nav1-vor-ident" );
                    ++nav1_play_count;
-               }
+                }
            } else if ( nav1_play_count < 5 && nav1_has_dme ) {
                // play DME ident
                if ( !globals->get_soundmgr()->is_playing("nav1-vor-ident") &&
@@ -564,10 +580,20 @@ FGRadioStack::update(int dt)
        if ( nav2_vol_btn > 0.1 && nav2_ident_btn ) {
            FGSimpleSound *sound;
            sound = globals->get_soundmgr()->find( "nav2-vor-ident" );
-           sound->set_volume( nav2_vol_btn * 0.3 );
+            if ( sound != NULL ) {
+                sound->set_volume( nav2_vol_btn );
+            } else {
+                SG_LOG( SG_COCKPIT, SG_ALERT,
+                        "Can't find nav2-vor-ident sound" );
+            }
            sound = globals->get_soundmgr()->find( "nav2-dme-ident" );
-           sound->set_volume( nav2_vol_btn * 0.3 );
-           if ( nav2_last_time <
+            if ( sound != NULL ) {
+                sound->set_volume( nav2_vol_btn );
+            } else {
+                SG_LOG( SG_COCKPIT, SG_ALERT,
+                        "Can't find nav2-dme-ident sound" );
+            }
+            if ( nav2_last_time <
                 globals->get_time_params()->get_cur_time() - 30 ) {
                nav2_last_time = globals->get_time_params()->get_cur_time();
                nav2_play_count = 0;
@@ -679,7 +705,11 @@ FGRadioStack::update(int dt)
        if ( adf_vol_btn > 0.1 && adf_ident_btn ) {
            FGSimpleSound *sound;
            sound = globals->get_soundmgr()->find( "adf-ident" );
-           sound->set_volume( adf_vol_btn * 0.3 );
+            if ( sound != NULL ) {
+                sound->set_volume( adf_vol_btn );
+            } else {
+                SG_LOG( SG_COCKPIT, SG_ALERT, "Can't find adf-ident sound" );
+            }
            if ( adf_last_time <
                 globals->get_time_params()->get_cur_time() - 30 ) {
                adf_last_time = globals->get_time_params()->get_cur_time();
@@ -863,7 +893,11 @@ void FGRadioStack::search()
            FGSimpleSound *sound;
            sound = morse.make_ident( nav1_trans_ident, LO_FREQUENCY );
            sound->set_volume( 0.3 );
-           globals->get_soundmgr()->add( sound, "nav1-vor-ident" );
+           if ( globals->get_soundmgr()->add( sound, "nav1-vor-ident" ) ) {
+                // cout << "Added nav1-vor-ident sound" << endl;
+            } else {
+                // cout << "Failed to add v1-vor-ident sound" << endl;
+            }
 
            if ( globals->get_soundmgr()->exists( "nav1-dme-ident" ) ) {
                globals->get_soundmgr()->remove( "nav1-dme-ident" );
@@ -892,7 +926,9 @@ void FGRadioStack::search()
        nav1_trans_ident = "";
        last_nav1_ident = "";
 #ifdef ENABLE_AUDIO_SUPPORT
-       globals->get_soundmgr()->remove( "nav1-vor-ident" );
+       if ( ! globals->get_soundmgr()->remove( "nav1-vor-ident" ) ) {
+            // cout << "Failed to remove nav1-vor-ident sound" << endl;
+        }
        globals->get_soundmgr()->remove( "nav1-dme-ident" );
 #endif
        // cout << "not picking up vor1. :-(" << endl;
index edd46700d4fa4a05b80148f6aa55b757a31b4eda..eb2f41fd7461f10386f3b176de39d0bf2951e521 100644 (file)
@@ -75,14 +75,18 @@ FGSimpleSound::~FGSimpleSound() {
 void FGSimpleSound::play( slScheduler *sched, bool looped ) {
     
     requests++;
-    if (requests > 1)
-       return;
+
+    // make sure sound isn't already playing
+    if ( sample->getPlayCount() > 0 ) {
+        return;
+    }
 
     // sched->stopSample(sample);
-    if (looped)
-       sched->loopSample(sample);
-    else
-       sched->playSample(sample);
+    if ( looped ) {
+        sched->loopSample(sample);
+    } else {
+        sched->playSample(sample);
+    }
     
     sched->addSampleEnvelope(sample, 0, 0, pitch_envelope, SL_PITCH_ENVELOPE);
     sched->addSampleEnvelope(sample, 0, 1, volume_envelope, SL_VOLUME_ENVELOPE);
@@ -90,14 +94,17 @@ void FGSimpleSound::play( slScheduler *sched, bool looped ) {
 
 void FGSimpleSound::stop( slScheduler *sched, bool quick ) {
 
-    if (quick)
-       requests = 0;
-    else
-       if (--requests < 0)
-          requests = 0;
+    if ( quick ) {
+        requests = 0;
+    } else {
+        if ( --requests < 0 ) {
+            requests = 0;
+        }
+    }
 
-    if (requests > 0)
+    if ( requests > 0 ) {
        return;
+    }
 
     sched->stopSample( sample );
 }
@@ -226,9 +233,18 @@ void FGSoundMgr::update(int dt) {
 // add a sound effect, return true if successful
 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
 
-    sample_map_iterator it = samples.find(refname);
-    if (it != samples.end())
-       return false;
+    sound_map_iterator sound_it = sounds.find( refname );
+    if ( sound_it != sounds.end() ) {
+        // sound already exists
+        return false;
+    }
+
+    sample_map_iterator sample_it = samples.find( refname );
+    if ( sample_it != samples.end() ) {
+        // this sound has existed in the past and it's sample is still
+        // here, delete the sample so we can replace it.
+        samples.erase( sample_it );
+    }
 
     sample_ref *sr = new sample_ref;
 
@@ -241,6 +257,7 @@ bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
     return true;
 }
 
+
 // add a sound from a file, return the sample if successful, else return NULL
 FGSimpleSound *FGSoundMgr::add( const string& refname, const string &file ) {
      FGSimpleSound *sound;
@@ -272,6 +289,7 @@ FGSimpleSound *FGSoundMgr::add( const string& refname, const string &file ) {
     return sound;
 }
 
+
 // remove a sound effect, return true if successful
 bool FGSoundMgr::remove( const string& refname ) {
 
@@ -306,9 +324,11 @@ bool FGSoundMgr::remove( const string& refname ) {
        // delete sample;
         sounds.erase( it );
 
+        // cout << "sndmgr: removed -> " << refname << endl;
        return true;
-   } else {
-       return false;
+    } else {
+        // cout << "sndmgr: failed remove -> " << refname << endl;
+        return false;
     }
 }
 
@@ -330,7 +350,7 @@ FGSimpleSound *FGSoundMgr::find( const string& refname ) {
     sound_map_iterator it = sounds.find( refname );
     if ( it != sounds.end() ) {
        return it->second;
-   } else {
+    } else {
        return NULL;
     }
 }
@@ -368,7 +388,7 @@ bool FGSoundMgr::is_playing( const string& refname ) {
     if ((sample = find( refname )) == NULL)
        return false;
 
-    return sample->is_playing();
+    return (sample->get_sample()->getPlayCount() > 0 );
 }
 
 
index 8a54ee457822a20eb3d7e7df73160f6e237190e9..7503a1719f8d2bd4113ef84c5ca0c4f95361c3f3 100644 (file)
@@ -70,7 +70,9 @@ public:
 
     inline void play_once( slScheduler *sched ) { play( sched, false); }
     inline void play_looped( slScheduler *sched ) { play( sched, true); }
-    inline bool is_playing( ) { return (requests > 0 ); }
+    inline bool is_playing( ) {
+        return ( sample->getPlayCount() > 0 );
+    }
 
     inline double get_pitch() const { return pitch; }
     inline void set_pitch( double p ) {