]> git.mxchange.org Git - flightgear.git/blobdiff - src/Sound/soundmgr.cxx
Patches from Erik.
[flightgear.git] / src / Sound / soundmgr.cxx
index bc3386539f4ec7faead58e881c444bb1f3a3eccd..eb51afac65d836bdd020673c2d0e259743f44f5c 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 ) {
     SGPath slfile( globals->get_fg_root() );
@@ -60,28 +64,63 @@ FGSimpleSound::~FGSimpleSound() {
     delete sample;
 }
 
+void FGSimpleSound::play_once( slScheduler *sched ) {
+    sched->stopSample(sample);
+    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);
+}
+
+//
+// Sound Manager
+//
 
 // constructor
 FGSoundMgr::FGSoundMgr() {
     audio_sched = new slScheduler( 8000 );
-    audio_sched -> setMaxConcurrent ( 6 ); 
+    if ( audio_sched->notWorking() ) {
+       SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
+    } else {
+       audio_sched -> setMaxConcurrent ( 6 ); 
 
-    audio_mixer = new smMixer;
+       audio_mixer = new smMixer;
 
-    SG_LOG( SG_GENERAL, SG_INFO,
-           "Rate = " << audio_sched->getRate()
-           << "  Bps = " << audio_sched->getBps()
-           << "  Stereo = " << audio_sched->getStereo() );
+       SG_LOG( SG_GENERAL, SG_INFO,
+               "Rate = " << audio_sched->getRate()
+               << "  Bps = " << audio_sched->getBps()
+               << "  Stereo = " << audio_sched->getStereo() );
+    }
 }
 
 // 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;
@@ -90,32 +129,53 @@ FGSoundMgr::~FGSoundMgr() {
 
 
 // initialize the sound manager
-bool FGSoundMgr::init() {
+void FGSoundMgr::init() {
     last.stamp();
     safety = FG_MAX_SOUND_SAFETY;
 
-    audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
+    // 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;
     }
     sounds.clear();
 
-    if ( audio_sched->not_working() ) {
-       return false;
-    } else {
-       return true;
-    }
+}
+
+void FGSoundMgr::bind ()
+{
+  // no properties yet
+}
+
+void FGSoundMgr::unbind ()
+{
+  // no properties yet
 }
 
 
 // run the audio scheduler
-bool FGSoundMgr::update() {
+void FGSoundMgr::update(int dt) {
     SGTimeStamp current;
     current.stamp();
 
@@ -133,22 +193,59 @@ bool FGSoundMgr::update() {
     // cout << "safety = " << safety << endl;
     audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
 
-    if ( !audio_sched->not_working() ) {
+    if ( !audio_sched->not_working() )
        audio_sched -> update();
-       return true;
-    } else {
-       return false;
-    }
 }
 
 
 // add a sound effect, return true if successful
-bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname  ) {
+bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
+
+    sample_map_iterator it = samples.find(refname);
+    if (it != samples.end())
+       return false;
+
+    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 == (string)"")
+       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 ) {
@@ -170,33 +267,21 @@ bool FGSoundMgr::remove( const string& refname ) {
                                        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;
+        //
+        // FIXME:
+        // Due to the change in the sound manager, samples live
+       // until the sound manager gets removed.
+        //
+       // delete sample;
         sounds.erase( it );
 
        return true;
-#endif
    } else {
        return false;
     }
@@ -229,66 +314,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->is_playing();
 }
 
 
 // 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;
 }