]> git.mxchange.org Git - flightgear.git/blob - src/Sound/soundmgr.hxx
Stop the sound when paused. We really need a separate control for
[flightgear.git] / src / Sound / soundmgr.hxx
1 // soundmgr.hxx -- Sound effect management class
2 //
3 // Sound manager initially written by David Findlay
4 // <david_j_findlay@yahoo.com.au> 2001
5 //
6 // C++-ified by Curtis Olson, started March 2001.
7 //
8 // Copyright (C) 2001  Curtis L. Olson - curt@flightgear.org
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #ifndef _SOUNDMGR_HXX
28 #define _SOUNDMGR_HXX
29
30 #ifdef HAVE_CONFIG_H
31 #  include <config.h>
32 #endif
33
34 #include <simgear/compiler.h>
35
36 #include STL_STRING
37 #include <map>
38
39 #include <plib/sl.h>
40 #include <plib/sm.h>
41
42 #include <simgear/timing/timestamp.hxx>
43
44 #include <Main/fgfs.hxx>
45
46 SG_USING_STD(map);
47 SG_USING_STD(string);
48
49
50 // manages everything we need to know for an individual sound sample
51 class FGSimpleSound {
52
53 private:
54
55     slSample *sample;
56     slEnvelope *pitch_envelope;
57     slEnvelope *volume_envelope;
58     double pitch;
59     double volume;
60     int requests;
61
62 public:
63
64     FGSimpleSound( string file );
65     FGSimpleSound( unsigned char *buffer, int len );
66     ~FGSimpleSound();
67
68     void play( slScheduler *sched, bool looped );
69     void stop( slScheduler *sched, bool quick = true );
70
71     inline void play_once( slScheduler *sched ) { play( sched, false); }
72     inline void play_looped( slScheduler *sched ) { play( sched, true); }
73     inline bool is_playing( ) {
74         return ( sample->getPlayCount() > 0 );
75     }
76
77     inline double get_pitch() const { return pitch; }
78     inline void set_pitch( double p ) {
79        pitch = p;
80        pitch_envelope->setStep( 0, 0.01, pitch );
81     }
82     inline double get_volume() const { return volume; }
83     inline void set_volume( double v ) {
84        volume = v;
85        volume_envelope->setStep( 0, 0.01, volume );
86     }
87
88     inline slSample *get_sample() { return sample; }
89     inline slEnvelope *get_pitch_envelope() { return pitch_envelope; }
90     inline slEnvelope *get_volume_envelope() { return volume_envelope; }
91 };
92
93
94 typedef struct {
95         int n;
96         slSample *sample;
97 } sample_ref;
98
99 typedef map < string, sample_ref * > sample_map;
100 typedef sample_map::iterator sample_map_iterator;
101 typedef sample_map::const_iterator const_sample_map_iterator;
102
103
104 typedef map < string, FGSimpleSound * > sound_map;
105 typedef sound_map::iterator sound_map_iterator;
106 typedef sound_map::const_iterator const_sound_map_iterator;
107
108
109 class FGSoundMgr : public FGSubsystem
110 {
111
112     slScheduler *audio_sched;
113     smMixer *audio_mixer;
114
115     sound_map sounds;
116     sample_map samples;
117
118     SGTimeStamp last;
119     double safety;
120
121 public:
122
123     FGSoundMgr();
124     ~FGSoundMgr();
125
126
127     /**
128      * Initialize the sound manager.
129      */
130     void init();
131
132
133     /**
134      * Bind properties for the sound manager.
135      */
136     void bind ();
137
138
139     /**
140      * Unbind properties for the sound manager.
141      */
142     void unbind ();
143
144
145     /**
146      * Run the audio scheduler.
147      */
148     void update(int dt);
149
150
151     /**
152      * Pause all sounds.
153      */
154     void pause ();
155
156
157     /**
158      * Resume all sounds.
159      */
160     void resume ();
161
162
163     // is audio working?
164     inline bool is_working() const { return !audio_sched->notWorking(); }
165
166     // add a sound effect, return true if successful
167     bool add( FGSimpleSound *sound, const string& refname);
168
169     // add a sound file, return the sample if successful, else return NULL
170     FGSimpleSound *add( const string& refname, const string& file = "" );
171
172     // remove a sound effect, return true if successful
173     bool remove( const string& refname );
174
175     // return true of the specified sound exists in the sound manager system
176     bool exists( const string& refname );
177
178     // return a pointer to the FGSimpleSound if the specified sound
179     // exists in the sound manager system, otherwise return NULL
180     FGSimpleSound *find( const string& refname );
181
182     // tell the scheduler to play the indexed sample in a continuous
183     // loop
184     bool play_looped( const string& refname );
185
186     // tell the scheduler to play the indexed sample once
187     bool play_once( const string& refname );
188
189     // return true of the specified sound is currently being played
190     bool is_playing( const string& refname );
191
192     // immediate stop playing the sound
193     bool stop( const string& refname );
194
195     // return the audio scheduler 
196     inline slScheduler *get_scheduler( ) { return audio_sched; };
197 };
198
199
200 #endif // _SOUNDMGR_HXX
201
202