]> git.mxchange.org Git - flightgear.git/blobdiff - src/Sound/soundmgr.hxx
Patch from Julian Foad:
[flightgear.git] / src / Sound / soundmgr.hxx
index 6b404599e0842fad2600ef67ac27d7e7470b5dae..d9e9dd8fb6d4eb5eb3cc90ce7bb0bfb9c9a2b089 100644 (file)
 #include <plib/sl.h>
 #include <plib/sm.h>
 
-FG_USING_STD(map);
-FG_USING_STD(string);
+#include <simgear/timing/timestamp.hxx>
+
+#include <Main/fgfs.hxx>
+
+SG_USING_STD(map);
+SG_USING_STD(string);
 
 
 // manages everything we need to know for an individual sound sample
 class FGSimpleSound {
 
+private:
+
     slSample *sample;
     slEnvelope *pitch_envelope;
     slEnvelope *volume_envelope;
@@ -58,15 +64,24 @@ public:
     FGSimpleSound( unsigned char *buffer, int len );
     ~FGSimpleSound();
 
+    void play( slScheduler *sched, bool looped );
+    void stop( slScheduler *sched, bool quick = true );
+
+    inline void play_once( slScheduler *sched ) { play( sched, false); }
+    inline void play_looped( slScheduler *sched ) { play( sched, true); }
+    inline bool is_playing( ) {
+        return ( sample->getPlayCount() > 0 );
+    }
+
     inline double get_pitch() const { return pitch; }
     inline void set_pitch( double p ) {
-       pitch = p;
-       pitch_envelope->setStep( 0, 0.01, pitch );
+       pitch = p;
+       pitch_envelope->setStep( 0, 0.01, pitch );
     }
     inline double get_volume() const { return volume; }
     inline void set_volume( double v ) {
-       volume = v;
-       volume_envelope->setStep( 0, 0.01, volume );
+       volume = v;
+       volume_envelope->setStep( 0, 0.01, volume );
     }
 
     inline slSample *get_sample() { return sample; }
@@ -75,33 +90,93 @@ public:
 };
 
 
+typedef struct {
+       int n;
+       slSample *sample;
+} sample_ref;
+
+typedef map < string, sample_ref * > sample_map;
+typedef sample_map::iterator sample_map_iterator;
+typedef sample_map::const_iterator const_sample_map_iterator;
+
+
 typedef map < string, FGSimpleSound * > sound_map;
 typedef sound_map::iterator sound_map_iterator;
 typedef sound_map::const_iterator const_sound_map_iterator;
 
 
-class FGSoundMgr {
+class FGSoundMgr : public FGSubsystem
+{
 
     slScheduler *audio_sched;
     smMixer *audio_mixer;
+
     sound_map sounds;
+    sample_map samples;
+
+    SGTimeStamp last;
+    double safety;
 
 public:
 
     FGSoundMgr();
     ~FGSoundMgr();
 
-    // initialize the sound manager
-    bool init();
 
-    // run the audio scheduler
-    bool update();
+    /**
+     * Initialize the sound manager.
+     */
+    void init();
+
+
+    /**
+     * Bind properties for the sound manager.
+     */
+    void bind ();
+
+
+    /**
+     * Unbind properties for the sound manager.
+     */
+    void unbind ();
+
+
+    /**
+     * Run the audio scheduler.
+     */
+    void update(double dt);
+
+
+    /**
+     * Pause all sounds.
+     */
+    void pause ();
+
+
+    /**
+     * Resume all sounds.
+     */
+    void resume ();
+
 
     // is audio working?
-    inline bool is_working() const { return !audio_sched->not_working(); }
+    inline bool is_working() const { return !audio_sched->notWorking(); }
 
-    // add a sound effect, return the index of the sound
-    bool add( FGSimpleSound *sound, const string& refname );
+    // add a sound effect, return true if successful
+    bool add( FGSimpleSound *sound, const string& refname);
+
+    // add a sound file, return the sample if successful, else return NULL
+    FGSimpleSound *add( const string& refname, const string& file = "" );
+
+    // remove a sound effect, return true if successful
+    bool remove( const string& refname );
+
+    // return true of the specified sound exists in the sound manager system
+    bool exists( const string& refname );
+
+    // return a pointer to the FGSimpleSound if the specified sound
+    // exists in the sound manager system, otherwise return NULL
+    FGSimpleSound *find( const string& refname );
 
     // tell the scheduler to play the indexed sample in a continuous
     // loop
@@ -109,6 +184,15 @@ public:
 
     // tell the scheduler to play the indexed sample once
     bool play_once( const string& refname );
+
+    // return true of the specified sound is currently being played
+    bool is_playing( const string& refname );
+
+    // immediate stop playing the sound
+    bool stop( const string& refname );
+
+    // return the audio scheduler 
+    inline slScheduler *get_scheduler( ) { return audio_sched; };
 };