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 )
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 );
55 FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len )
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 );
67 FGSimpleSound::~FGSimpleSound() {
68 delete pitch_envelope;
69 delete volume_envelope;
73 void FGSimpleSound::play( slScheduler *sched, bool looped ) {
75 // make sure sound isn't already playing
76 if ( sample->getPlayCount() > 0 ) {
77 sched->stopSample(sample);
82 sched->loopSample(sample);
84 sched->playSample(sample);
87 sched->addSampleEnvelope(sample, 0, 0, pitch_envelope, SL_PITCH_ENVELOPE);
88 sched->addSampleEnvelope(sample, 0, 1, volume_envelope, SL_VOLUME_ENVELOPE);
91 void FGSimpleSound::stop( slScheduler *sched, bool quick ) {
93 sched->stopSample( sample );
101 FGSoundMgr::FGSoundMgr() {
102 audio_sched = new slScheduler( 8000 );
103 if ( audio_sched->notWorking() ) {
104 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
106 audio_sched -> setMaxConcurrent ( 6 );
108 audio_mixer = new smMixer;
110 SG_LOG( SG_GENERAL, SG_INFO,
111 "Rate = " << audio_sched->getRate()
112 << " Bps = " << audio_sched->getBps()
113 << " Stereo = " << audio_sched->getStereo() );
119 FGSoundMgr::~FGSoundMgr() {
122 // Remove the samples from the sample manager.
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;
129 audio_sched->stopSample(sr->sample);
135 // Remove the sounds from the sound manager.
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;
142 audio_sched->stopSample(s->get_sample());
151 // initialize the sound manager
152 void FGSoundMgr::init() {
153 safety = FG_MAX_SOUND_SAFETY;
155 // audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
156 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
160 // Remove the samples from the sample manager.
162 sample_map_iterator sample_current = samples.begin();
163 sample_map_iterator sample_end = samples.end();
164 for ( ; sample_current != sample_end; ++sample_current ) {
165 sample_ref *sr = sample_current->second;
172 // Remove the sounds from the sound manager.
174 sound_map_iterator sound_current = sounds.begin();
175 sound_map_iterator sound_end = sounds.end();
176 for ( ; sound_current != sound_end; ++sound_current ) {
177 FGSimpleSound *s = sound_current->second;
178 delete s->get_sample();
185 void FGSoundMgr::bind ()
190 void FGSoundMgr::unbind ()
196 // run the audio scheduler
197 void FGSoundMgr::update( double dt ) {
201 safety = safety * 0.99 + dt * 0.01;
203 if ( safety > FG_MAX_SOUND_SAFETY ) {
204 safety = FG_MAX_SOUND_SAFETY;
206 // cout << "safety = " << safety << endl;
207 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
209 if ( !audio_sched->not_working() )
210 audio_sched -> update();
217 audio_sched->pauseSample(0, 0);
222 FGSoundMgr::resume ()
224 audio_sched->resumeSample(0, 0);
228 // add a sound effect, return true if successful
229 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
231 sound_map_iterator sound_it = sounds.find( refname );
232 if ( sound_it != sounds.end() ) {
233 // sound already exists
237 sample_map_iterator sample_it = samples.find( refname );
238 if ( sample_it != samples.end() ) {
239 // this sound has existed in the past and it's sample is still
240 // here, delete the sample so we can replace it.
241 samples.erase( sample_it );
244 sample_ref *sr = new sample_ref;
247 sr->sample = sound->get_sample();
248 samples[refname] = sr;
250 sounds[refname] = sound;
256 // add a sound from a file, return the sample if successful, else return NULL
257 FGSimpleSound *FGSoundMgr::add( const string& refname, const string &file ) {
258 FGSimpleSound *sound;
263 sample_map_iterator it = samples.find(file);
264 if (it == samples.end()) {
265 sound = new FGSimpleSound(file);
266 sounds[refname] = sound;
268 sample_ref *sr = new sample_ref;
271 sr->sample = sound->get_sample();
275 sample_ref *sr = it->second;
279 new FGSimpleSound(sr->sample->getBuffer(), sr->sample->getLength());
280 sounds[refname] = sound;
288 // remove a sound effect, return true if successful
289 bool FGSoundMgr::remove( const string& refname ) {
291 sound_map_iterator it = sounds.find( refname );
292 if ( it != sounds.end() ) {
293 // first stop the sound from playing (so we don't bomb the
295 FGSimpleSound *sample = it->second;
297 // cout << "Playing " << sample->get_sample()->getPlayCount()
298 // << " instances!" << endl;
300 audio_sched->stopSample( sample->get_sample() );
301 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
304 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
306 SL_VOLUME_ENVELOPE );
308 // must call audio_sched->update() after stopping the sound
309 // but before deleting it.
310 audio_sched -> update();
311 // cout << "Still playing " << sample->get_sample()->getPlayCount()
312 // << " instances!" << endl;
316 // Due to the change in the sound manager, samples live
317 // until the sound manager gets removed.
322 // cout << "sndmgr: removed -> " << refname << endl;
325 // cout << "sndmgr: failed remove -> " << refname << endl;
331 // return true of the specified sound exists in the sound manager system
332 bool FGSoundMgr::exists( const string& refname ) {
333 sound_map_iterator it = sounds.find( refname );
334 if ( it != sounds.end() ) {
342 // return a pointer to the FGSimpleSound if the specified sound exists
343 // in the sound manager system, otherwise return NULL
344 FGSimpleSound *FGSoundMgr::find( const string& refname ) {
345 sound_map_iterator it = sounds.find( refname );
346 if ( it != sounds.end() ) {
354 // tell the scheduler to play the indexed sample in a continuous
356 bool FGSoundMgr::play_looped( const string& refname ) {
357 FGSimpleSound *sample;
359 if ((sample = find( refname )) == NULL)
362 sample->play(audio_sched, true);
367 // tell the scheduler to play the indexed sample once
368 bool FGSoundMgr::play_once( const string& refname ) {
369 FGSimpleSound *sample;
371 if ((sample = find( refname )) == NULL)
374 sample->play(audio_sched, false);
379 // return true of the specified sound is currently being played
380 bool FGSoundMgr::is_playing( const string& refname ) {
381 FGSimpleSound *sample;
383 if ((sample = find( refname )) == NULL)
386 return (sample->get_sample()->getPlayCount() > 0 );
390 // immediate stop playing the sound
391 bool FGSoundMgr::stop( const string& refname ) {
392 FGSimpleSound *sample;
394 if ((sample = find( refname )) == NULL)
397 audio_sched->stopSample( sample->get_sample() );