]> git.mxchange.org Git - flightgear.git/blobdiff - src/Sound/soundmgr.cxx
Use the new plib-1.4.1 sound interface to set a maximum of 6 concurrent
[flightgear.git] / src / Sound / soundmgr.cxx
index 5a170e9a4db033d6720630b03b3df2624495f2be..bc3386539f4ec7faead58e881c444bb1f3a3eccd 100644 (file)
 
 
 #include <simgear/debug/logstream.hxx>
-#include <simgear/misc/fgpath.hxx>
+#include <simgear/misc/sg_path.hxx>
 
 #include <Main/globals.hxx>
 
 #include "soundmgr.hxx"
 
+#define FG_SOUND_SAFETY_MULT 3
+#define FG_MAX_SOUND_SAFETY ( 1.0 / FG_SOUND_SAFETY_MULT )
 
 // constructor
 FGSimpleSound::FGSimpleSound( string file ) {
-    FGPath slfile( globals->get_fg_root() );
+    SGPath slfile( globals->get_fg_root() );
     slfile.append( file );
     sample = new slSample ( (char *)slfile.c_str() );
     pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
@@ -62,9 +64,11 @@ FGSimpleSound::~FGSimpleSound() {
 // constructor
 FGSoundMgr::FGSoundMgr() {
     audio_sched = new slScheduler( 8000 );
+    audio_sched -> setMaxConcurrent ( 6 ); 
+
     audio_mixer = new smMixer;
 
-    FG_LOG( FG_GENERAL, FG_INFO,
+    SG_LOG( SG_GENERAL, SG_INFO,
            "Rate = " << audio_sched->getRate()
            << "  Bps = " << audio_sched->getBps()
            << "  Stereo = " << audio_sched->getStereo() );
@@ -87,8 +91,11 @@ FGSoundMgr::~FGSoundMgr() {
 
 // initialize the sound manager
 bool FGSoundMgr::init() {
+    last.stamp();
+    safety = FG_MAX_SOUND_SAFETY;
+
     audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
-    audio_sched -> setSafetyMargin ( 1.0 ) ;
+    audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
 
     sound_map_iterator current = sounds.begin();
     sound_map_iterator end = sounds.end();
@@ -109,6 +116,23 @@ bool FGSoundMgr::init() {
 
 // run the audio scheduler
 bool FGSoundMgr::update() {
+    SGTimeStamp current;
+    current.stamp();
+
+    double elapsed = (double)(current - last) / 1000000.0;
+    last = current;
+
+    if ( elapsed > safety ) {
+       safety = elapsed;
+    } else {
+       safety = safety * 0.99 + elapsed * 0.01;
+    }
+    if ( safety > FG_MAX_SOUND_SAFETY ) {
+       safety = FG_MAX_SOUND_SAFETY;
+    }
+    // cout << "safety = " << safety << endl;
+    audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
+
     if ( !audio_sched->not_working() ) {
        audio_sched -> update();
        return true;
@@ -135,8 +159,8 @@ bool FGSoundMgr::remove( const string& refname ) {
        // audio scheduler)
        FGSimpleSound *sample = it->second;
 
-       cout << "Playing "
-            << sample->get_sample()->getPlayCount() << " instances!" << endl;
+       // cout << "Playing " << sample->get_sample()->getPlayCount()
+       //      << " instances!" << endl;
 
        audio_sched->stopSample( sample->get_sample() );
        audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
@@ -158,15 +182,15 @@ bool FGSoundMgr::remove( const string& refname ) {
        // 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;
+       // 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;
+       // cout << "Still playing " << sample->get_sample()->getPlayCount()
+       //      << " instances!" << endl;
 
        delete sample;
         sounds.erase( it );
@@ -190,6 +214,18 @@ bool FGSoundMgr::exists( const string& refname ) {
 }
 
 
+// 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 ) {
@@ -212,7 +248,7 @@ 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;