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