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