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();
236 audio_sched->pauseSample(0, 0);
241 FGSoundMgr::resume ()
243 audio_sched->resumeSample(0, 0);
247 // add a sound effect, return true if successful
248 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
250 sound_map_iterator sound_it = sounds.find( refname );
251 if ( sound_it != sounds.end() ) {
252 // sound already exists
256 sample_map_iterator sample_it = samples.find( refname );
257 if ( sample_it != samples.end() ) {
258 // this sound has existed in the past and it's sample is still
259 // here, delete the sample so we can replace it.
260 samples.erase( sample_it );
263 sample_ref *sr = new sample_ref;
266 sr->sample = sound->get_sample();
267 samples[refname] = sr;
269 sounds[refname] = sound;
275 // add a sound from a file, return the sample if successful, else return NULL
276 FGSimpleSound *FGSoundMgr::add( const string& refname, const string &file ) {
277 FGSimpleSound *sound;
282 sample_map_iterator it = samples.find(file);
283 if (it == samples.end()) {
284 sound = new FGSimpleSound(file);
285 sounds[refname] = sound;
287 sample_ref *sr = new sample_ref;
290 sr->sample = sound->get_sample();
294 sample_ref *sr = it->second;
298 new FGSimpleSound(sr->sample->getBuffer(), sr->sample->getLength());
299 sounds[refname] = sound;
307 // remove a sound effect, return true if successful
308 bool FGSoundMgr::remove( const string& refname ) {
310 sound_map_iterator it = sounds.find( refname );
311 if ( it != sounds.end() ) {
312 // first stop the sound from playing (so we don't bomb the
314 FGSimpleSound *sample = it->second;
316 // cout << "Playing " << sample->get_sample()->getPlayCount()
317 // << " instances!" << endl;
319 audio_sched->stopSample( sample->get_sample() );
320 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
323 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
325 SL_VOLUME_ENVELOPE );
327 // must call audio_sched->update() after stopping the sound
328 // but before deleting it.
329 audio_sched -> update();
330 // cout << "Still playing " << sample->get_sample()->getPlayCount()
331 // << " instances!" << endl;
335 // Due to the change in the sound manager, samples live
336 // until the sound manager gets removed.
341 // cout << "sndmgr: removed -> " << refname << endl;
344 // cout << "sndmgr: failed remove -> " << refname << endl;
350 // return true of the specified sound exists in the sound manager system
351 bool FGSoundMgr::exists( const string& refname ) {
352 sound_map_iterator it = sounds.find( refname );
353 if ( it != sounds.end() ) {
361 // return a pointer to the FGSimpleSound if the specified sound exists
362 // in the sound manager system, otherwise return NULL
363 FGSimpleSound *FGSoundMgr::find( const string& refname ) {
364 sound_map_iterator it = sounds.find( refname );
365 if ( it != sounds.end() ) {
373 // tell the scheduler to play the indexed sample in a continuous
375 bool FGSoundMgr::play_looped( const string& refname ) {
376 FGSimpleSound *sample;
378 if ((sample = find( refname )) == NULL)
381 sample->play(audio_sched, true);
386 // tell the scheduler to play the indexed sample once
387 bool FGSoundMgr::play_once( const string& refname ) {
388 FGSimpleSound *sample;
390 if ((sample = find( refname )) == NULL)
393 sample->play(audio_sched, false);
398 // return true of the specified sound is currently being played
399 bool FGSoundMgr::is_playing( const string& refname ) {
400 FGSimpleSound *sample;
402 if ((sample = find( refname )) == NULL)
405 return (sample->get_sample()->getPlayCount() > 0 );
409 // immediate stop playing the sound
410 bool FGSoundMgr::stop( const string& refname ) {
411 FGSimpleSound *sample;
413 if ((sample = find( refname )) == NULL)
416 audio_sched->stopSample( sample->get_sample() );