]> git.mxchange.org Git - flightgear.git/blobdiff - src/Sound/soundmgr.cxx
Tracked down a potential segfault when trying to audibly ident a vor station.
[flightgear.git] / src / Sound / soundmgr.cxx
index 834632fbac75c3dd628929d2fe4eeb7941e28b4a..eb2f41fd7461f10386f3b176de39d0bf2951e521 100644 (file)
 #define FG_SOUND_SAFETY_MULT 3
 #define FG_MAX_SOUND_SAFETY ( 1.0 / FG_SOUND_SAFETY_MULT )
 
+//
+// SimpleSound
+//
+
 // constructor
-FGSimpleSound::FGSimpleSound( string file ) {
+FGSimpleSound::FGSimpleSound( string file )
+  : pitch(1.0),
+    volume(1.0),
+    requests(0)
+{
     SGPath slfile( globals->get_fg_root() );
     slfile.append( file );
     sample = new slSample ( (char *)slfile.c_str() );
@@ -45,7 +53,11 @@ FGSimpleSound::FGSimpleSound( string file ) {
     volume_envelope->setStep ( 0, 0.01, 1.0 );
 }
 
-FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len ) {
+FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len )
+  : pitch(1.0),
+    volume(1.0),
+    requests(0)
+{
     sample = new slSample ( buffer, len );
     pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
     volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
@@ -60,6 +72,46 @@ FGSimpleSound::~FGSimpleSound() {
     delete sample;
 }
 
+void FGSimpleSound::play( slScheduler *sched, bool looped ) {
+    
+    requests++;
+
+    // make sure sound isn't already playing
+    if ( sample->getPlayCount() > 0 ) {
+        return;
+    }
+
+    // sched->stopSample(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);
+}
+
+void FGSimpleSound::stop( slScheduler *sched, bool quick ) {
+
+    if ( quick ) {
+        requests = 0;
+    } else {
+        if ( --requests < 0 ) {
+            requests = 0;
+        }
+    }
+
+    if ( requests > 0 ) {
+       return;
+    }
+
+    sched->stopSample( sample );
+}
+
+//
+// Sound Manager
+//
 
 // constructor
 FGSoundMgr::FGSoundMgr() {
@@ -81,12 +133,26 @@ FGSoundMgr::FGSoundMgr() {
 // destructor
 
 FGSoundMgr::~FGSoundMgr() {
-    sound_map_iterator current = sounds.begin();
-    sound_map_iterator end = sounds.end();
-    for ( ; current != end; ++current ) {
-       FGSimpleSound *s = current->second;
-       delete s->get_sample();
-       delete s;
+
+    //
+    // Remove the samples from the sample manager.
+    //
+    sample_map_iterator sample_current = samples.begin();
+    sample_map_iterator sample_end = samples.end();
+    for ( ; sample_current != sample_end; ++sample_current ) {
+       sample_ref *sr = sample_current->second;
+       delete sr->sample;
+       delete sr;
+    }
+
+    //
+    // Remove the sounds from the sound manager.
+    //
+    sound_map_iterator sound_current = sounds.begin();
+    sound_map_iterator sound_end = sounds.end();
+    for ( ; sound_current != sound_end; ++sound_current ) {
+        FGSimpleSound *s = sound_current->second;
+        delete s;
     }
 
     delete audio_sched;
@@ -102,10 +168,26 @@ void FGSoundMgr::init() {
     // audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
     audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
 
-    sound_map_iterator current = sounds.begin();
-    sound_map_iterator end = sounds.end();
-    for ( ; current != end; ++current ) {
-       FGSimpleSound *s = current->second;
+
+    //
+    // Remove the samples from the sample manager.
+    //
+    sample_map_iterator sample_current = samples.begin();
+    sample_map_iterator sample_end = samples.end();
+    for ( ; sample_current != sample_end; ++sample_current ) {
+        sample_ref *sr = sample_current->second;
+        delete sr->sample;
+        delete sr;
+    }
+    samples.clear();
+    //
+    // Remove the sounds from the sound manager.
+    //
+    sound_map_iterator sound_current = sounds.begin();
+    sound_map_iterator sound_end = sounds.end();
+    for ( ; sound_current != sound_end; ++sound_current ) {
+       FGSimpleSound *s = sound_current->second;
        delete s->get_sample();
        delete s;
     }
@@ -149,13 +231,65 @@ void FGSoundMgr::update(int dt) {
 
 
 // add a sound effect, return true if successful
-bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname  ) {
+bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
+
+    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;
+
+    sr->n=1;
+    sr->sample = sound->get_sample();
+    samples[refname] = sr;
+
     sounds[refname] = sound;
 
     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;
+
+    if (file.empty())
+       return NULL;
+
+    sample_map_iterator it = samples.find(file);
+    if (it == samples.end()) {
+       sound = new FGSimpleSound(file);
+       sounds[refname] = sound;
+
+       sample_ref *sr = new sample_ref;
+
+       sr->n=1;
+       sr->sample = sound->get_sample();
+       samples[file] = sr;
+
+    } else {
+       sample_ref *sr = it->second;
+
+       sr->n++;
+       sound =
+           new FGSimpleSound(sr->sample->getBuffer(), sr->sample->getLength());
+       sounds[refname] = sound;
+
+    }
+
+    return sound;
+}
+
+
 // remove a sound effect, return true if successful
 bool FGSoundMgr::remove( const string& refname ) {
 
@@ -182,12 +316,19 @@ bool FGSoundMgr::remove( const string& refname ) {
        // cout << "Still playing " << sample->get_sample()->getPlayCount()
        //      << " instances!" << endl;
 
-       delete sample;
+        //
+        // FIXME:
+        // Due to the change in the sound manager, samples live
+       // until the sound manager gets removed.
+        //
+       // delete sample;
         sounds.erase( it );
 
+        // cout << "sndmgr: removed -> " << refname << endl;
        return true;
-   } else {
-       return false;
+    } else {
+        // cout << "sndmgr: failed remove -> " << refname << endl;
+        return false;
     }
 }
 
@@ -209,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;
     }
 }
@@ -218,66 +359,46 @@ FGSimpleSound *FGSoundMgr::find( const string& refname ) {
 // tell the scheduler to play the indexed sample in a continuous
 // loop
 bool FGSoundMgr::play_looped( const string& refname ) {
-    sound_map_iterator it = sounds.find( refname );
-    if ( it != sounds.end() ) {
-       FGSimpleSound *sample = it->second;
-       audio_sched->loopSample( sample->get_sample() );
-       audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
-                                       sample->get_pitch_envelope(),
-                                       SL_PITCH_ENVELOPE );
-       audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
-                                       sample->get_volume_envelope(),
-                                       SL_VOLUME_ENVELOPE );
-       
-       return true;
-    } else {
-       return false;
-    }
+   FGSimpleSound *sample;
+
+    if ((sample = find( refname )) == NULL)
+       return false;
+
+    sample->play(audio_sched, true);
+    return true;
 }
 
 
 // tell the scheduler to play the indexed sample once
 bool FGSoundMgr::play_once( const string& refname ) {
-    sound_map_iterator it = sounds.find( refname );
-    if ( it != sounds.end() ) {
-       FGSimpleSound *sample = it->second;
-       audio_sched->stopSample( sample->get_sample() );
-       audio_sched->playSample( sample->get_sample() );
-       audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
-                                       sample->get_pitch_envelope(),
-                                       SL_PITCH_ENVELOPE );
-       audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
-                                       sample->get_volume_envelope(),
-                                       SL_VOLUME_ENVELOPE );
-       
-       return true;
-    } else {
-       return false;
-    }
+    FGSimpleSound *sample;
+
+    if ((sample = find( refname )) == NULL)
+       return false;
+
+    sample->play(audio_sched, false);
+    return true;
 }
 
 
 // return true of the specified sound is currently being played
 bool FGSoundMgr::is_playing( const string& refname ) {
-    sound_map_iterator it = sounds.find( refname );
-    if ( it != sounds.end() ) {
-       FGSimpleSound *sample = it->second;
-       return (sample->get_sample()->getPlayCount() > 0 );
-       return true;
-    } else {
-       return false;
-    }
+    FGSimpleSound *sample;
+
+    if ((sample = find( refname )) == NULL)
+       return false;
+
+    return (sample->get_sample()->getPlayCount() > 0 );
 }
 
 
 // immediate stop playing the sound
 bool FGSoundMgr::stop( const string& refname ) {
-    sound_map_iterator it = sounds.find( refname );
-    if ( it != sounds.end() ) {
-       FGSimpleSound *sample = it->second;
-       audio_sched->stopSample( sample->get_sample() );
-       return true;
-    } else {
-       return false;
-    }
+    FGSimpleSound *sample;
+
+    if ((sample = find( refname )) == NULL)
+       return false;
+
+    audio_sched->stopSample( sample->get_sample() );
+    return true;
 }