1 // soundmgr.cxx -- Sound effect management class
3 // Sound manager initially written by David Findlay
4 // <david_j_findlay@yahoo.com.au> 2001
6 // C++-ified by Curtis Olson, started March 2001.
8 // Copyright (C) 2001 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/misc/sg_path.hxx>
30 #include <Main/globals.hxx>
32 #include "soundmgr.hxx"
34 #define FG_SOUND_SAFETY_MULT 3
35 #define FG_MAX_SOUND_SAFETY ( 1.0 / FG_SOUND_SAFETY_MULT )
42 FGSimpleSound::FGSimpleSound( string file )
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 );
56 FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len )
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 );
69 FGSimpleSound::~FGSimpleSound() {
70 delete pitch_envelope;
71 delete volume_envelope;
75 void FGSimpleSound::play( slScheduler *sched, bool looped ) {
79 // make sure sound isn't already playing
80 if ( sample->getPlayCount() > 0 ) {
84 // sched->stopSample(sample);
86 sched->loopSample(sample);
88 sched->playSample(sample);
91 sched->addSampleEnvelope(sample, 0, 0, pitch_envelope, SL_PITCH_ENVELOPE);
92 sched->addSampleEnvelope(sample, 0, 1, volume_envelope, SL_VOLUME_ENVELOPE);
95 void FGSimpleSound::stop( slScheduler *sched, bool quick ) {
100 if ( --requests < 0 ) {
105 if ( requests > 0 ) {
109 sched->stopSample( sample );
117 FGSoundMgr::FGSoundMgr() {
118 audio_sched = new slScheduler( 8000 );
119 if ( audio_sched->notWorking() ) {
120 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
122 audio_sched -> setMaxConcurrent ( 6 );
124 audio_mixer = new smMixer;
126 SG_LOG( SG_GENERAL, SG_INFO,
127 "Rate = " << audio_sched->getRate()
128 << " Bps = " << audio_sched->getBps()
129 << " Stereo = " << audio_sched->getStereo() );
135 FGSoundMgr::~FGSoundMgr() {
138 // Remove the samples from the sample manager.
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;
149 // Remove the sounds from the sound manager.
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;
163 // initialize the sound manager
164 void FGSoundMgr::init() {
166 safety = FG_MAX_SOUND_SAFETY;
168 // audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
169 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
173 // Remove the samples from the sample manager.
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;
185 // Remove the sounds from the sound manager.
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();
198 void FGSoundMgr::bind ()
203 void FGSoundMgr::unbind ()
209 // run the audio scheduler
210 void FGSoundMgr::update(int dt) {
214 double elapsed = (double)(current - last) / 1000000.0;
217 if ( elapsed > safety ) {
220 safety = safety * 0.99 + elapsed * 0.01;
222 if ( safety > FG_MAX_SOUND_SAFETY ) {
223 safety = FG_MAX_SOUND_SAFETY;
225 // cout << "safety = " << safety << endl;
226 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
228 if ( !audio_sched->not_working() )
229 audio_sched -> update();
233 // add a sound effect, return true if successful
234 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
236 sound_map_iterator sound_it = sounds.find( refname );
237 if ( sound_it != sounds.end() ) {
238 // sound already exists
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 );
249 sample_ref *sr = new sample_ref;
252 sr->sample = sound->get_sample();
253 samples[refname] = sr;
255 sounds[refname] = sound;
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;
268 sample_map_iterator it = samples.find(file);
269 if (it == samples.end()) {
270 sound = new FGSimpleSound(file);
271 sounds[refname] = sound;
273 sample_ref *sr = new sample_ref;
276 sr->sample = sound->get_sample();
280 sample_ref *sr = it->second;
284 new FGSimpleSound(sr->sample->getBuffer(), sr->sample->getLength());
285 sounds[refname] = sound;
293 // remove a sound effect, return true if successful
294 bool FGSoundMgr::remove( const string& refname ) {
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
300 FGSimpleSound *sample = it->second;
302 // cout << "Playing " << sample->get_sample()->getPlayCount()
303 // << " instances!" << endl;
305 audio_sched->stopSample( sample->get_sample() );
306 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
309 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
311 SL_VOLUME_ENVELOPE );
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;
321 // Due to the change in the sound manager, samples live
322 // until the sound manager gets removed.
327 // cout << "sndmgr: removed -> " << refname << endl;
330 // cout << "sndmgr: failed remove -> " << refname << endl;
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() ) {
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() ) {
359 // tell the scheduler to play the indexed sample in a continuous
361 bool FGSoundMgr::play_looped( const string& refname ) {
362 FGSimpleSound *sample;
364 if ((sample = find( refname )) == NULL)
367 sample->play(audio_sched, true);
372 // tell the scheduler to play the indexed sample once
373 bool FGSoundMgr::play_once( const string& refname ) {
374 FGSimpleSound *sample;
376 if ((sample = find( refname )) == NULL)
379 sample->play(audio_sched, false);
384 // return true of the specified sound is currently being played
385 bool FGSoundMgr::is_playing( const string& refname ) {
386 FGSimpleSound *sample;
388 if ((sample = find( refname )) == NULL)
391 return (sample->get_sample()->getPlayCount() > 0 );
395 // immediate stop playing the sound
396 bool FGSoundMgr::stop( const string& refname ) {
397 FGSimpleSound *sample;
399 if ((sample = find( refname )) == NULL)
402 audio_sched->stopSample( sample->get_sample() );