]> git.mxchange.org Git - flightgear.git/blobdiff - src/Sound/soundmgr.cxx
This patch creates a sample manager next to the sound manager. The
[flightgear.git] / src / Sound / soundmgr.cxx
index 7a70607bdf29d495cf3c3ecc301d477a223a71ab..aa4b6f4f0d8c4acc846c2006850dfa164823e2f8 100644 (file)
@@ -64,22 +64,43 @@ FGSimpleSound::~FGSimpleSound() {
 // constructor
 FGSoundMgr::FGSoundMgr() {
     audio_sched = new slScheduler( 8000 );
-    audio_mixer = new smMixer;
+    if ( audio_sched->notWorking() ) {
+       SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
+    } else {
+       audio_sched -> setMaxConcurrent ( 6 ); 
+
+       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;
@@ -88,32 +109,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();
 
@@ -131,22 +173,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 ) {
@@ -168,33 +247,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;
     }