]> git.mxchange.org Git - flightgear.git/blobdiff - src/Sound/soundmgr.cxx
Modified FGSubsystem::update() to take an int parameter for delta time
[flightgear.git] / src / Sound / soundmgr.cxx
index 97a7b0aa7cc90df2d74a330b9c02a11ca86cb67f..a1caaf6ffb6356b6444b1accf586cd5af34ca384 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,15 +64,18 @@ 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() );
 }
 
 // destructor
+
 FGSoundMgr::~FGSoundMgr() {
     sound_map_iterator current = sounds.begin();
     sound_map_iterator end = sounds.end();
@@ -86,9 +91,12 @@ FGSoundMgr::~FGSoundMgr() {
 
 
 // initialize the sound manager
-bool FGSoundMgr::init() {
-    audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
-    audio_sched -> setSafetyMargin ( 1.0 ) ;
+void FGSoundMgr::init() {
+    last.stamp();
+    safety = FG_MAX_SOUND_SAFETY;
+
+    // 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();
@@ -99,22 +107,40 @@ bool FGSoundMgr::init() {
     }
     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() {
-    if ( !audio_sched->not_working() ) {
-       audio_sched -> update();
-       return true;
+void FGSoundMgr::update(int dt) {
+    SGTimeStamp current;
+    current.stamp();
+
+    double elapsed = (double)(current - last) / 1000000.0;
+    last = current;
+
+    if ( elapsed > safety ) {
+       safety = elapsed;
     } else {
-       return false;
+       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();
 }
 
 
@@ -224,7 +250,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;