]> git.mxchange.org Git - flightgear.git/blob - src/Sound/soundmgr.cxx
Major overhaul:
[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         delete sr->sample;
129         delete sr;
130     }
131
132     //
133     // Remove the sounds from the sound manager.
134     //
135     sound_map_iterator sound_current = sounds.begin();
136     sound_map_iterator sound_end = sounds.end();
137     for ( ; sound_current != sound_end; ++sound_current ) {
138         FGSimpleSound *s = sound_current->second;
139         delete s;
140     }
141
142     delete audio_sched;
143     delete audio_mixer;
144 }
145
146
147 // initialize the sound manager
148 void FGSoundMgr::init() {
149     last.stamp();
150     safety = FG_MAX_SOUND_SAFETY;
151
152     // audio_mixer -> setMasterVolume ( 80 ) ;  /* 80% of max volume. */
153     audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
154
155
156     //
157     // Remove the samples from the sample manager.
158     //
159     sample_map_iterator sample_current = samples.begin();
160     sample_map_iterator sample_end = samples.end();
161     for ( ; sample_current != sample_end; ++sample_current ) {
162         sample_ref *sr = sample_current->second;
163         delete sr->sample;
164         delete sr;
165     }
166     samples.clear();
167  
168     //
169     // Remove the sounds from the sound manager.
170     //
171     sound_map_iterator sound_current = sounds.begin();
172     sound_map_iterator sound_end = sounds.end();
173     for ( ; sound_current != sound_end; ++sound_current ) {
174         FGSimpleSound *s = sound_current->second;
175         delete s->get_sample();
176         delete s;
177     }
178     sounds.clear();
179
180 }
181
182 void FGSoundMgr::bind ()
183 {
184   // no properties yet
185 }
186
187 void FGSoundMgr::unbind ()
188 {
189   // no properties yet
190 }
191
192
193 // run the audio scheduler
194 void FGSoundMgr::update(double dt) {
195                                 // FIXME: use dt supplied (seconds)
196     SGTimeStamp current;
197     current.stamp();
198
199     double elapsed = (double)(current - last) / 1000000.0;
200     last = current;
201
202     if ( elapsed > safety ) {
203         safety = elapsed;
204     } else {
205         safety = safety * 0.99 + elapsed * 0.01;
206     }
207     if ( safety > FG_MAX_SOUND_SAFETY ) {
208         safety = FG_MAX_SOUND_SAFETY;
209     }
210     // cout << "safety = " << safety << endl;
211     audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
212
213     if ( !audio_sched->not_working() )
214         audio_sched -> update();
215 }
216
217
218 void
219 FGSoundMgr::pause ()
220 {
221   audio_sched->pauseSample(0, 0);
222 }
223
224
225 void
226 FGSoundMgr::resume ()
227 {
228   audio_sched->resumeSample(0, 0);
229 }
230
231
232 // add a sound effect, return true if successful
233 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
234
235     sound_map_iterator sound_it = sounds.find( refname );
236     if ( sound_it != sounds.end() ) {
237         // sound already exists
238         return false;
239     }
240
241     sample_map_iterator sample_it = samples.find( refname );
242     if ( sample_it != samples.end() ) {
243         // this sound has existed in the past and it's sample is still
244         // here, delete the sample so we can replace it.
245         samples.erase( sample_it );
246     }
247
248     sample_ref *sr = new sample_ref;
249
250     sr->n=1;
251     sr->sample = sound->get_sample();
252     samples[refname] = sr;
253
254     sounds[refname] = sound;
255
256     return true;
257 }
258
259
260 // add a sound from a file, return the sample if successful, else return NULL
261 FGSimpleSound *FGSoundMgr::add( const string& refname, const string &file ) {
262      FGSimpleSound *sound;
263
264     if (file.empty())
265        return NULL;
266
267     sample_map_iterator it = samples.find(file);
268     if (it == samples.end()) {
269        sound = new FGSimpleSound(file);
270        sounds[refname] = sound;
271
272        sample_ref *sr = new sample_ref;
273
274        sr->n=1;
275        sr->sample = sound->get_sample();
276        samples[file] = sr;
277
278     } else {
279        sample_ref *sr = it->second;
280
281        sr->n++;
282        sound =
283            new FGSimpleSound(sr->sample->getBuffer(), sr->sample->getLength());
284        sounds[refname] = sound;
285
286     }
287
288     return sound;
289 }
290
291
292 // remove a sound effect, return true if successful
293 bool FGSoundMgr::remove( const string& refname ) {
294
295     sound_map_iterator it = sounds.find( refname );
296     if ( it != sounds.end() ) {
297         // first stop the sound from playing (so we don't bomb the
298         // audio scheduler)
299         FGSimpleSound *sample = it->second;
300
301         // cout << "Playing " << sample->get_sample()->getPlayCount()
302         //      << " instances!" << endl;
303
304         audio_sched->stopSample( sample->get_sample() );
305         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0, 
306                                         NULL,
307                                         SL_PITCH_ENVELOPE );
308         audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1, 
309                                         NULL,
310                                         SL_VOLUME_ENVELOPE );
311
312         // must call audio_sched->update() after stopping the sound
313         // but before deleting it.
314         audio_sched -> update();
315         // cout << "Still playing " << sample->get_sample()->getPlayCount()
316         //      << " instances!" << endl;
317
318         //
319         // FIXME:
320         // Due to the change in the sound manager, samples live
321         // until the sound manager gets removed.
322         //
323         // delete sample;
324         sounds.erase( it );
325
326         // cout << "sndmgr: removed -> " << refname << endl;
327         return true;
328     } else {
329         // cout << "sndmgr: failed remove -> " << refname << endl;
330         return false;
331     }
332 }
333
334
335 // return true of the specified sound exists in the sound manager system
336 bool FGSoundMgr::exists( const string& refname ) {
337     sound_map_iterator it = sounds.find( refname );
338     if ( it != sounds.end() ) {
339         return true;
340    } else {
341         return false;
342     }
343 }
344
345
346 // return a pointer to the FGSimpleSound if the specified sound exists
347 // in the sound manager system, otherwise return NULL
348 FGSimpleSound *FGSoundMgr::find( const string& refname ) {
349     sound_map_iterator it = sounds.find( refname );
350     if ( it != sounds.end() ) {
351         return it->second;
352     } else {
353         return NULL;
354     }
355 }
356
357
358 // tell the scheduler to play the indexed sample in a continuous
359 // loop
360 bool FGSoundMgr::play_looped( const string& refname ) {
361    FGSimpleSound *sample;
362
363     if ((sample = find( refname )) == NULL)
364        return false;
365
366     sample->play(audio_sched, true);
367     return true;
368 }
369
370
371 // tell the scheduler to play the indexed sample once
372 bool FGSoundMgr::play_once( const string& refname ) {
373     FGSimpleSound *sample;
374
375     if ((sample = find( refname )) == NULL)
376        return false;
377
378     sample->play(audio_sched, false);
379     return true;
380 }
381
382
383 // return true of the specified sound is currently being played
384 bool FGSoundMgr::is_playing( const string& refname ) {
385     FGSimpleSound *sample;
386
387     if ((sample = find( refname )) == NULL)
388        return false;
389
390     return (sample->get_sample()->getPlayCount() > 0 );
391 }
392
393
394 // immediate stop playing the sound
395 bool FGSoundMgr::stop( const string& refname ) {
396     FGSimpleSound *sample;
397
398     if ((sample = find( refname )) == NULL)
399        return false;
400
401     audio_sched->stopSample( sample->get_sample() );
402     return true;
403 }