]> git.mxchange.org Git - flightgear.git/blobdiff - src/Sound/soundmgr.cxx
MSVC fixes.
[flightgear.git] / src / Sound / soundmgr.cxx
index 8b3756abf1ccc24cbcea5a79904dc86a492c4124..8903da4f97690ac30fa90b0ea74b37ae8a90cd2e 100644 (file)
@@ -118,7 +118,7 @@ bool FGSoundMgr::update() {
 }
 
 
-// add a sound effect
+// add a sound effect, return true if successful
 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname  ) {
     sounds[refname] = sound;
 
@@ -126,6 +126,82 @@ bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname  ) {
 }
 
 
+// remove a sound effect, return true if successful
+bool FGSoundMgr::remove( const string& refname ) {
+
+    sound_map_iterator it = sounds.find( refname );
+    if ( it != sounds.end() ) {
+       // first stop the sound from playing (so we don't bomb the
+       // audio scheduler)
+       FGSimpleSound *sample = it->second;
+
+       // cout << "Playing " << sample->get_sample()->getPlayCount()
+       //      << " instances!" << endl;
+
+       audio_sched->stopSample( sample->get_sample() );
+       audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
+                                       NULL,
+                                       SL_PITCH_ENVELOPE );
+       audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
+                                       NULL,
+                                       SL_VOLUME_ENVELOPE );
+
+#if defined ( PLIB_1_2_X )
+       // if PLIB_1_2_X, we can't reliably remove sounds
+       // that are currently being played. :-( So, let's just not
+       // remove them and return false.  The effects of this are that
+       // the sound sample will continue to finish playing (or
+       // continue to loop forever.)  And the sound sample will
+       // remain registered in the plib audio system.  This is a
+       // memory leak, and eventually this could cause us to max out
+       // the total number of allowed sound samples in plib, but what
+       // are you going to do?  Hopefully the plib team will do a new
+       // stable relase with these problems fixed.
+
+       // cout << "plib broken audio, skipping actual remove" << endl;
+
+       return false;
+#else
+       // must call audio_sched->update() after stopping the sound
+       // but before deleting it.
+       audio_sched -> update();
+       // cout << "Still playing " << sample->get_sample()->getPlayCount()
+       //      << " instances!" << endl;
+
+       delete sample;
+        sounds.erase( it );
+
+       return true;
+#endif
+   } else {
+       return false;
+    }
+}
+
+
+// return true of the specified sound exists in the sound manager system
+bool FGSoundMgr::exists( const string& refname ) {
+    sound_map_iterator it = sounds.find( refname );
+    if ( it != sounds.end() ) {
+       return true;
+   } else {
+       return false;
+    }
+}
+
+
+// return a pointer to the FGSimpleSound if the specified sound exists
+// in the sound manager system, otherwise return NULL
+FGSimpleSound *FGSoundMgr::find( const string& refname ) {
+    sound_map_iterator it = sounds.find( refname );
+    if ( it != sounds.end() ) {
+       return it->second;
+   } else {
+       return NULL;
+    }
+}
+
+
 // tell the scheduler to play the indexed sample in a continuous
 // loop
 bool FGSoundMgr::play_looped( const string& refname ) {
@@ -148,10 +224,11 @@ bool FGSoundMgr::play_looped( const string& refname ) {
 
 
 // tell the scheduler to play the indexed sample once
-bool FGSoundMgr::FGSoundMgr::play_once( const string& refname ) {
+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(),
@@ -165,3 +242,29 @@ bool FGSoundMgr::FGSoundMgr::play_once( const string& refname ) {
        return false;
     }
 }
+
+
+// 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;
+    }
+}
+
+
+// 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;
+    }
+}