]> git.mxchange.org Git - flightgear.git/blob - src/Sound/soundmgr.cxx
Fixed gauge spelling error.
[flightgear.git] / src / Sound / soundmgr.cxx
1 // soundmgr.cxx -- 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 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/sg_path.hxx>
29
30 #include <Main/globals.hxx>
31
32 #include "soundmgr.hxx"
33
34 #define FG_SOUND_SAFETY_MULT 3
35 #define FG_MAX_SOUND_SAFETY ( 1.0 / FG_SOUND_SAFETY_MULT )
36
37 // constructor
38 FGSimpleSound::FGSimpleSound( string file ) {
39     SGPath slfile( globals->get_fg_root() );
40     slfile.append( file );
41     sample = new slSample ( (char *)slfile.c_str() );
42     pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
43     volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
44     pitch_envelope->setStep ( 0, 0.01, 1.0 );
45     volume_envelope->setStep ( 0, 0.01, 1.0 );
46 }
47
48 FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len ) {
49     sample = new slSample ( buffer, len );
50     pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
51     volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
52     pitch_envelope->setStep ( 0, 0.01, 1.0 );
53     volume_envelope->setStep ( 0, 0.01, 1.0 );
54 }
55
56 // destructor
57 FGSimpleSound::~FGSimpleSound() {
58     delete pitch_envelope;
59     delete volume_envelope;
60     delete sample;
61 }
62
63
64 // constructor
65 FGSoundMgr::FGSoundMgr() {
66     audio_sched = new slScheduler( 8000 );
67     audio_sched -> setMaxConcurrent ( 6 ); 
68
69     audio_mixer = new smMixer;
70
71     SG_LOG( SG_GENERAL, SG_INFO,
72             "Rate = " << audio_sched->getRate()
73             << "  Bps = " << audio_sched->getBps()
74             << "  Stereo = " << audio_sched->getStereo() );
75 }
76
77 // destructor
78 FGSoundMgr::~FGSoundMgr() {
79     sound_map_iterator current = sounds.begin();
80     sound_map_iterator end = sounds.end();
81     for ( ; current != end; ++current ) {
82         FGSimpleSound *s = current->second;
83         delete s->get_sample();
84         delete s;
85     }
86
87     delete audio_sched;
88     delete audio_mixer;
89 }
90
91
92 // initialize the sound manager
93 bool FGSoundMgr::init() {
94     last.stamp();
95     safety = FG_MAX_SOUND_SAFETY;
96
97     // audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
98     audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
99
100     sound_map_iterator current = sounds.begin();
101     sound_map_iterator end = sounds.end();
102     for ( ; current != end; ++current ) {
103         FGSimpleSound *s = current->second;
104         delete s->get_sample();
105         delete s;
106     }
107     sounds.clear();
108
109     if ( audio_sched->not_working() ) {
110         return false;
111     } else {
112         return true;
113     }
114 }
115
116
117 // run the audio scheduler
118 bool FGSoundMgr::update() {
119     SGTimeStamp current;
120     current.stamp();
121
122     double elapsed = (double)(current - last) / 1000000.0;
123     last = current;
124
125     if ( elapsed > safety ) {
126         safety = elapsed;
127     } else {
128         safety = safety * 0.99 + elapsed * 0.01;
129     }
130     if ( safety > FG_MAX_SOUND_SAFETY ) {
131         safety = FG_MAX_SOUND_SAFETY;
132     }
133     // cout << "safety = " << safety << endl;
134     audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
135
136     if ( !audio_sched->not_working() ) {
137         audio_sched -> update();
138         return true;
139     } else {
140         return false;
141     }
142 }
143
144
145 // add a sound effect, return true if successful
146 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname  ) {
147     sounds[refname] = sound;
148
149     return true;
150 }
151
152
153 // remove a sound effect, return true if successful
154 bool FGSoundMgr::remove( const string& refname ) {
155
156     sound_map_iterator it = sounds.find( refname );
157     if ( it != sounds.end() ) {
158         // first stop the sound from playing (so we don't bomb the
159         // audio scheduler)
160         FGSimpleSound *sample = it->second;
161
162         // cout << "Playing " << sample->get_sample()->getPlayCount()
163         //      << " instances!" << endl;
164
165         audio_sched->stopSample( sample->get_sample() );
166         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
167                                         NULL,
168                                         SL_PITCH_ENVELOPE );
169         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
170                                         NULL,
171                                         SL_VOLUME_ENVELOPE );
172
173 #if defined ( PLIB_1_2_X )
174         // if PLIB_1_2_X, we can't reliably remove sounds
175         // that are currently being played. :-( So, let's just not
176         // remove them and return false.  The effects of this are that
177         // the sound sample will continue to finish playing (or
178         // continue to loop forever.)  And the sound sample will
179         // remain registered in the plib audio system.  This is a
180         // memory leak, and eventually this could cause us to max out
181         // the total number of allowed sound samples in plib, but what
182         // are you going to do?  Hopefully the plib team will do a new
183         // stable relase with these problems fixed.
184
185         // cout << "plib broken audio, skipping actual remove" << endl;
186
187         return false;
188 #else
189         // must call audio_sched->update() after stopping the sound
190         // but before deleting it.
191         audio_sched -> update();
192         // cout << "Still playing " << sample->get_sample()->getPlayCount()
193         //      << " instances!" << endl;
194
195         delete sample;
196         sounds.erase( it );
197
198         return true;
199 #endif
200    } else {
201         return false;
202     }
203 }
204
205
206 // return true of the specified sound exists in the sound manager system
207 bool FGSoundMgr::exists( const string& refname ) {
208     sound_map_iterator it = sounds.find( refname );
209     if ( it != sounds.end() ) {
210         return true;
211    } else {
212         return false;
213     }
214 }
215
216
217 // return a pointer to the FGSimpleSound if the specified sound exists
218 // in the sound manager system, otherwise return NULL
219 FGSimpleSound *FGSoundMgr::find( const string& refname ) {
220     sound_map_iterator it = sounds.find( refname );
221     if ( it != sounds.end() ) {
222         return it->second;
223    } else {
224         return NULL;
225     }
226 }
227
228
229 // tell the scheduler to play the indexed sample in a continuous
230 // loop
231 bool FGSoundMgr::play_looped( const string& refname ) {
232     sound_map_iterator it = sounds.find( refname );
233     if ( it != sounds.end() ) {
234         FGSimpleSound *sample = it->second;
235         audio_sched->loopSample( sample->get_sample() );
236         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
237                                         sample->get_pitch_envelope(),
238                                         SL_PITCH_ENVELOPE );
239         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
240                                         sample->get_volume_envelope(),
241                                         SL_VOLUME_ENVELOPE );
242         
243         return true;
244     } else {
245         return false;
246     }
247 }
248
249
250 // tell the scheduler to play the indexed sample once
251 bool FGSoundMgr::play_once( const string& refname ) {
252     sound_map_iterator it = sounds.find( refname );
253     if ( it != sounds.end() ) {
254         FGSimpleSound *sample = it->second;
255         audio_sched->stopSample( sample->get_sample() );
256         audio_sched->playSample( sample->get_sample() );
257         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
258                                         sample->get_pitch_envelope(),
259                                         SL_PITCH_ENVELOPE );
260         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
261                                         sample->get_volume_envelope(),
262                                         SL_VOLUME_ENVELOPE );
263         
264         return true;
265     } else {
266         return false;
267     }
268 }
269
270
271 // return true of the specified sound is currently being played
272 bool FGSoundMgr::is_playing( const string& refname ) {
273     sound_map_iterator it = sounds.find( refname );
274     if ( it != sounds.end() ) {
275         FGSimpleSound *sample = it->second;
276         return (sample->get_sample()->getPlayCount() > 0 );
277         return true;
278     } else {
279         return false;
280     }
281 }
282
283
284 // immediate stop playing the sound
285 bool FGSoundMgr::stop( const string& refname ) {
286     sound_map_iterator it = sounds.find( refname );
287     if ( it != sounds.end() ) {
288         FGSimpleSound *sample = it->second;
289         audio_sched->stopSample( sample->get_sample() );
290         return true;
291     } else {
292         return false;
293     }
294 }