]> git.mxchange.org Git - flightgear.git/blobdiff - src/Sound/soundmgr.cxx
Major overhaul:
[flightgear.git] / src / Sound / soundmgr.cxx
index eb51afac65d836bdd020673c2d0e259743f44f5c..2b7e25576c4dee1eb1686c4fe3eb14c3d7f2cfad 100644 (file)
 //
 
 // constructor
-FGSimpleSound::FGSimpleSound( string file ) {
+FGSimpleSound::FGSimpleSound( string file )
+  : pitch(1.0),
+    volume(1.0)
+{
     SGPath slfile( globals->get_fg_root() );
     slfile.append( file );
     sample = new slSample ( (char *)slfile.c_str() );
@@ -49,7 +52,10 @@ 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)
+{
     sample = new slSample ( buffer, len );
     pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
     volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
@@ -64,17 +70,27 @@ FGSimpleSound::~FGSimpleSound() {
     delete sample;
 }
 
-void FGSimpleSound::play_once( slScheduler *sched ) {
-    sched->stopSample(sample);
-    sched->playSample(sample);
+void FGSimpleSound::play( slScheduler *sched, bool looped ) {
+    
+    // make sure sound isn't already playing
+    if ( sample->getPlayCount() > 0 ) {
+       sched->stopSample(sample);
+    //   return;
+    }
+
+    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::play_looped( slScheduler *sched ) {
-    sched->loopSample(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 ) {
+
+    sched->stopSample( sample );
 }
 
 //
@@ -175,7 +191,8 @@ void FGSoundMgr::unbind ()
 
 
 // run the audio scheduler
-void FGSoundMgr::update(int dt) {
+void FGSoundMgr::update(double dt) {
+                               // FIXME: use dt supplied (seconds)
     SGTimeStamp current;
     current.stamp();
 
@@ -198,12 +215,35 @@ void FGSoundMgr::update(int dt) {
 }
 
 
+void
+FGSoundMgr::pause ()
+{
+  audio_sched->pauseSample(0, 0);
+}
+
+
+void
+FGSoundMgr::resume ()
+{
+  audio_sched->resumeSample(0, 0);
+}
+
+
 // 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;
 
@@ -216,11 +256,12 @@ 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;
 
-    if (file == (string)"")
+    if (file.empty())
        return NULL;
 
     sample_map_iterator it = samples.find(file);
@@ -247,6 +288,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 ) {
 
@@ -281,9 +323,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;
     }
 }
 
@@ -305,7 +349,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;
     }
 }
@@ -343,7 +387,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 );
 }