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 ) {
81 // sched->stopSample(sample);
83 sched->loopSample(sample);
85 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 ) {
102 sched->stopSample( sample );
110 FGSoundMgr::FGSoundMgr() {
111 audio_sched = new slScheduler( 8000 );
112 if ( audio_sched->notWorking() ) {
113 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
115 audio_sched -> setMaxConcurrent ( 6 );
117 audio_mixer = new smMixer;
119 SG_LOG( SG_GENERAL, SG_INFO,
120 "Rate = " << audio_sched->getRate()
121 << " Bps = " << audio_sched->getBps()
122 << " Stereo = " << audio_sched->getStereo() );
128 FGSoundMgr::~FGSoundMgr() {
131 // Remove the samples from the sample manager.
133 sample_map_iterator sample_current = samples.begin();
134 sample_map_iterator sample_end = samples.end();
135 for ( ; sample_current != sample_end; ++sample_current ) {
136 sample_ref *sr = sample_current->second;
142 // Remove the sounds from the sound manager.
144 sound_map_iterator sound_current = sounds.begin();
145 sound_map_iterator sound_end = sounds.end();
146 for ( ; sound_current != sound_end; ++sound_current ) {
147 FGSimpleSound *s = sound_current->second;
156 // initialize the sound manager
157 void FGSoundMgr::init() {
159 safety = FG_MAX_SOUND_SAFETY;
161 // audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
162 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
166 // Remove the samples from the sample manager.
168 sample_map_iterator sample_current = samples.begin();
169 sample_map_iterator sample_end = samples.end();
170 for ( ; sample_current != sample_end; ++sample_current ) {
171 sample_ref *sr = sample_current->second;
178 // Remove the sounds from the sound manager.
180 sound_map_iterator sound_current = sounds.begin();
181 sound_map_iterator sound_end = sounds.end();
182 for ( ; sound_current != sound_end; ++sound_current ) {
183 FGSimpleSound *s = sound_current->second;
184 delete s->get_sample();
191 void FGSoundMgr::bind ()
196 void FGSoundMgr::unbind ()
202 // run the audio scheduler
203 void FGSoundMgr::update(int dt) {
207 double elapsed = (double)(current - last) / 1000000.0;
210 if ( elapsed > safety ) {
213 safety = safety * 0.99 + elapsed * 0.01;
215 if ( safety > FG_MAX_SOUND_SAFETY ) {
216 safety = FG_MAX_SOUND_SAFETY;
218 // cout << "safety = " << safety << endl;
219 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
221 if ( !audio_sched->not_working() )
222 audio_sched -> update();
226 // add a sound effect, return true if successful
227 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
229 sample_map_iterator it = samples.find(refname);
230 if (it != samples.end())
233 sample_ref *sr = new sample_ref;
236 sr->sample = sound->get_sample();
237 samples[refname] = sr;
239 sounds[refname] = sound;
244 // add a sound from a file, return the sample if successful, else return NULL
245 FGSimpleSound *FGSoundMgr::add( const string& refname, const string &file ) {
246 FGSimpleSound *sound;
251 sample_map_iterator it = samples.find(file);
252 if (it == samples.end()) {
253 sound = new FGSimpleSound(file);
254 sounds[refname] = sound;
256 sample_ref *sr = new sample_ref;
259 sr->sample = sound->get_sample();
263 sample_ref *sr = it->second;
267 new FGSimpleSound(sr->sample->getBuffer(), sr->sample->getLength());
268 sounds[refname] = sound;
275 // remove a sound effect, return true if successful
276 bool FGSoundMgr::remove( const string& refname ) {
278 sound_map_iterator it = sounds.find( refname );
279 if ( it != sounds.end() ) {
280 // first stop the sound from playing (so we don't bomb the
282 FGSimpleSound *sample = it->second;
284 // cout << "Playing " << sample->get_sample()->getPlayCount()
285 // << " instances!" << endl;
287 audio_sched->stopSample( sample->get_sample() );
288 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
291 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
293 SL_VOLUME_ENVELOPE );
295 // must call audio_sched->update() after stopping the sound
296 // but before deleting it.
297 audio_sched -> update();
298 // cout << "Still playing " << sample->get_sample()->getPlayCount()
299 // << " instances!" << endl;
303 // Due to the change in the sound manager, samples live
304 // until the sound manager gets removed.
316 // return true of the specified sound exists in the sound manager system
317 bool FGSoundMgr::exists( const string& refname ) {
318 sound_map_iterator it = sounds.find( refname );
319 if ( it != sounds.end() ) {
327 // return a pointer to the FGSimpleSound if the specified sound exists
328 // in the sound manager system, otherwise return NULL
329 FGSimpleSound *FGSoundMgr::find( const string& refname ) {
330 sound_map_iterator it = sounds.find( refname );
331 if ( it != sounds.end() ) {
339 // tell the scheduler to play the indexed sample in a continuous
341 bool FGSoundMgr::play_looped( const string& refname ) {
342 FGSimpleSound *sample;
344 if ((sample = find( refname )) == NULL)
347 sample->play(audio_sched, true);
352 // tell the scheduler to play the indexed sample once
353 bool FGSoundMgr::play_once( const string& refname ) {
354 FGSimpleSound *sample;
356 if ((sample = find( refname )) == NULL)
359 sample->play(audio_sched, false);
364 // return true of the specified sound is currently being played
365 bool FGSoundMgr::is_playing( const string& refname ) {
366 FGSimpleSound *sample;
368 if ((sample = find( refname )) == NULL)
371 return sample->is_playing();
375 // immediate stop playing the sound
376 bool FGSoundMgr::stop( const string& refname ) {
377 FGSimpleSound *sample;
379 if ((sample = find( refname )) == NULL)
382 audio_sched->stopSample( sample->get_sample() );