]> git.mxchange.org Git - flightgear.git/blob - src/Sound/soundmgr.cxx
Tracked down a potential segfault when trying to audibly ident a vor station.
[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 //
38 // SimpleSound
39 //
40
41 // constructor
42 FGSimpleSound::FGSimpleSound( string file )
43   : pitch(1.0),
44     volume(1.0),
45     requests(0)
46 {
47     SGPath slfile( globals->get_fg_root() );
48     slfile.append( file );
49     sample = new slSample ( (char *)slfile.c_str() );
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 FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len )
57   : pitch(1.0),
58     volume(1.0),
59     requests(0)
60 {
61     sample = new slSample ( buffer, len );
62     pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
63     volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
64     pitch_envelope->setStep ( 0, 0.01, 1.0 );
65     volume_envelope->setStep ( 0, 0.01, 1.0 );
66 }
67
68 // destructor
69 FGSimpleSound::~FGSimpleSound() {
70     delete pitch_envelope;
71     delete volume_envelope;
72     delete sample;
73 }
74
75 void FGSimpleSound::play( slScheduler *sched, bool looped ) {
76     
77     requests++;
78
79     // make sure sound isn't already playing
80     if ( sample->getPlayCount() > 0 ) {
81         return;
82     }
83
84     // sched->stopSample(sample);
85     if ( looped ) {
86         sched->loopSample(sample);
87     } else {
88         sched->playSample(sample);
89     }
90     
91     sched->addSampleEnvelope(sample, 0, 0, pitch_envelope, SL_PITCH_ENVELOPE);
92     sched->addSampleEnvelope(sample, 0, 1, volume_envelope, SL_VOLUME_ENVELOPE);
93 }
94
95 void FGSimpleSound::stop( slScheduler *sched, bool quick ) {
96
97     if ( quick ) {
98         requests = 0;
99     } else {
100         if ( --requests < 0 ) {
101             requests = 0;
102         }
103     }
104
105     if ( requests > 0 ) {
106        return;
107     }
108
109     sched->stopSample( sample );
110 }
111
112 //
113 // Sound Manager
114 //
115
116 // constructor
117 FGSoundMgr::FGSoundMgr() {
118     audio_sched = new slScheduler( 8000 );
119     if ( audio_sched->notWorking() ) {
120         SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
121     } else {
122         audio_sched -> setMaxConcurrent ( 6 ); 
123
124         audio_mixer = new smMixer;
125
126         SG_LOG( SG_GENERAL, SG_INFO,
127                 "Rate = " << audio_sched->getRate()
128                 << "  Bps = " << audio_sched->getBps()
129                 << "  Stereo = " << audio_sched->getStereo() );
130     }
131 }
132
133 // destructor
134
135 FGSoundMgr::~FGSoundMgr() {
136
137     //
138     // Remove the samples from the sample manager.
139     //
140     sample_map_iterator sample_current = samples.begin();
141     sample_map_iterator sample_end = samples.end();
142     for ( ; sample_current != sample_end; ++sample_current ) {
143         sample_ref *sr = sample_current->second;
144         delete sr->sample;
145         delete sr;
146     }
147
148     //
149     // Remove the sounds from the sound manager.
150     //
151     sound_map_iterator sound_current = sounds.begin();
152     sound_map_iterator sound_end = sounds.end();
153     for ( ; sound_current != sound_end; ++sound_current ) {
154         FGSimpleSound *s = sound_current->second;
155         delete s;
156     }
157
158     delete audio_sched;
159     delete audio_mixer;
160 }
161
162
163 // initialize the sound manager
164 void FGSoundMgr::init() {
165     last.stamp();
166     safety = FG_MAX_SOUND_SAFETY;
167
168     // audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
169     audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
170
171
172     //
173     // Remove the samples from the sample manager.
174     //
175     sample_map_iterator sample_current = samples.begin();
176     sample_map_iterator sample_end = samples.end();
177     for ( ; sample_current != sample_end; ++sample_current ) {
178         sample_ref *sr = sample_current->second;
179         delete sr->sample;
180         delete sr;
181     }
182     samples.clear();
183  
184     //
185     // Remove the sounds from the sound manager.
186     //
187     sound_map_iterator sound_current = sounds.begin();
188     sound_map_iterator sound_end = sounds.end();
189     for ( ; sound_current != sound_end; ++sound_current ) {
190         FGSimpleSound *s = sound_current->second;
191         delete s->get_sample();
192         delete s;
193     }
194     sounds.clear();
195
196 }
197
198 void FGSoundMgr::bind ()
199 {
200   // no properties yet
201 }
202
203 void FGSoundMgr::unbind ()
204 {
205   // no properties yet
206 }
207
208
209 // run the audio scheduler
210 void FGSoundMgr::update(int dt) {
211     SGTimeStamp current;
212     current.stamp();
213
214     double elapsed = (double)(current - last) / 1000000.0;
215     last = current;
216
217     if ( elapsed > safety ) {
218         safety = elapsed;
219     } else {
220         safety = safety * 0.99 + elapsed * 0.01;
221     }
222     if ( safety > FG_MAX_SOUND_SAFETY ) {
223         safety = FG_MAX_SOUND_SAFETY;
224     }
225     // cout << "safety = " << safety << endl;
226     audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
227
228     if ( !audio_sched->not_working() )
229         audio_sched -> update();
230 }
231
232
233 // add a sound effect, return true if successful
234 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
235
236     sound_map_iterator sound_it = sounds.find( refname );
237     if ( sound_it != sounds.end() ) {
238         // sound already exists
239         return false;
240     }
241
242     sample_map_iterator sample_it = samples.find( refname );
243     if ( sample_it != samples.end() ) {
244         // this sound has existed in the past and it's sample is still
245         // here, delete the sample so we can replace it.
246         samples.erase( sample_it );
247     }
248
249     sample_ref *sr = new sample_ref;
250
251     sr->n=1;
252     sr->sample = sound->get_sample();
253     samples[refname] = sr;
254
255     sounds[refname] = sound;
256
257     return true;
258 }
259
260
261 // add a sound from a file, return the sample if successful, else return NULL
262 FGSimpleSound *FGSoundMgr::add( const string& refname, const string &file ) {
263      FGSimpleSound *sound;
264
265     if (file.empty())
266        return NULL;
267
268     sample_map_iterator it = samples.find(file);
269     if (it == samples.end()) {
270        sound = new FGSimpleSound(file);
271        sounds[refname] = sound;
272
273        sample_ref *sr = new sample_ref;
274
275        sr->n=1;
276        sr->sample = sound->get_sample();
277        samples[file] = sr;
278
279     } else {
280        sample_ref *sr = it->second;
281
282        sr->n++;
283        sound =
284            new FGSimpleSound(sr->sample->getBuffer(), sr->sample->getLength());
285        sounds[refname] = sound;
286
287     }
288
289     return sound;
290 }
291
292
293 // remove a sound effect, return true if successful
294 bool FGSoundMgr::remove( const string& refname ) {
295
296     sound_map_iterator it = sounds.find( refname );
297     if ( it != sounds.end() ) {
298         // first stop the sound from playing (so we don't bomb the
299         // audio scheduler)
300         FGSimpleSound *sample = it->second;
301
302         // cout << "Playing " << sample->get_sample()->getPlayCount()
303         //      << " instances!" << endl;
304
305         audio_sched->stopSample( sample->get_sample() );
306         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
307                                         NULL,
308                                         SL_PITCH_ENVELOPE );
309         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
310                                         NULL,
311                                         SL_VOLUME_ENVELOPE );
312
313         // must call audio_sched->update() after stopping the sound
314         // but before deleting it.
315         audio_sched -> update();
316         // cout << "Still playing " << sample->get_sample()->getPlayCount()
317         //      << " instances!" << endl;
318
319         //
320         // FIXME:
321         // Due to the change in the sound manager, samples live
322         // until the sound manager gets removed.
323         //
324         // delete sample;
325         sounds.erase( it );
326
327         // cout << "sndmgr: removed -> " << refname << endl;
328         return true;
329     } else {
330         // cout << "sndmgr: failed remove -> " << refname << endl;
331         return false;
332     }
333 }
334
335
336 // return true of the specified sound exists in the sound manager system
337 bool FGSoundMgr::exists( const string& refname ) {
338     sound_map_iterator it = sounds.find( refname );
339     if ( it != sounds.end() ) {
340         return true;
341    } else {
342         return false;
343     }
344 }
345
346
347 // return a pointer to the FGSimpleSound if the specified sound exists
348 // in the sound manager system, otherwise return NULL
349 FGSimpleSound *FGSoundMgr::find( const string& refname ) {
350     sound_map_iterator it = sounds.find( refname );
351     if ( it != sounds.end() ) {
352         return it->second;
353     } else {
354         return NULL;
355     }
356 }
357
358
359 // tell the scheduler to play the indexed sample in a continuous
360 // loop
361 bool FGSoundMgr::play_looped( const string& refname ) {
362    FGSimpleSound *sample;
363
364     if ((sample = find( refname )) == NULL)
365        return false;
366
367     sample->play(audio_sched, true);
368     return true;
369 }
370
371
372 // tell the scheduler to play the indexed sample once
373 bool FGSoundMgr::play_once( const string& refname ) {
374     FGSimpleSound *sample;
375
376     if ((sample = find( refname )) == NULL)
377        return false;
378
379     sample->play(audio_sched, false);
380     return true;
381 }
382
383
384 // return true of the specified sound is currently being played
385 bool FGSoundMgr::is_playing( const string& refname ) {
386     FGSimpleSound *sample;
387
388     if ((sample = find( refname )) == NULL)
389        return false;
390
391     return (sample->get_sample()->getPlayCount() > 0 );
392 }
393
394
395 // immediate stop playing the sound
396 bool FGSoundMgr::stop( const string& refname ) {
397     FGSimpleSound *sample;
398
399     if ((sample = find( refname )) == NULL)
400        return false;
401
402     audio_sched->stopSample( sample->get_sample() );
403     return true;
404 }