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 )
38 FGSimpleSound::FGSimpleSound( string file ) {
39 SGPath slfile( globals->get_fg_root() );
40 slfile.append( file );
41 sample = new slSample ( (char *)slfile.c_str() );
42 pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
43 volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
44 pitch_envelope->setStep ( 0, 0.01, 1.0 );
45 volume_envelope->setStep ( 0, 0.01, 1.0 );
48 FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len ) {
49 sample = new slSample ( buffer, len );
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 );
57 FGSimpleSound::~FGSimpleSound() {
58 delete pitch_envelope;
59 delete volume_envelope;
65 FGSoundMgr::FGSoundMgr() {
66 audio_sched = new slScheduler( 8000 );
67 if ( audio_sched->notWorking() ) {
68 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
70 audio_sched -> setMaxConcurrent ( 6 );
72 audio_mixer = new smMixer;
74 SG_LOG( SG_GENERAL, SG_INFO,
75 "Rate = " << audio_sched->getRate()
76 << " Bps = " << audio_sched->getBps()
77 << " Stereo = " << audio_sched->getStereo() );
83 FGSoundMgr::~FGSoundMgr() {
86 // Remove the samples from the sample manager.
88 sample_map_iterator sample_current = samples.begin();
89 sample_map_iterator sample_end = samples.end();
90 for ( ; sample_current != sample_end; ++sample_current ) {
91 sample_ref *sr = sample_current->second;
97 // Remove the sounds from the sound manager.
99 sound_map_iterator sound_current = sounds.begin();
100 sound_map_iterator sound_end = sounds.end();
101 for ( ; sound_current != sound_end; ++sound_current ) {
102 FGSimpleSound *s = sound_current->second;
111 // initialize the sound manager
112 void FGSoundMgr::init() {
114 safety = FG_MAX_SOUND_SAFETY;
116 // audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
117 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
121 // Remove the samples from the sample manager.
123 sample_map_iterator sample_current = samples.begin();
124 sample_map_iterator sample_end = samples.end();
125 for ( ; sample_current != sample_end; ++sample_current ) {
126 sample_ref *sr = sample_current->second;
133 // Remove the sounds from the sound manager.
135 sound_map_iterator sound_current = sounds.begin();
136 sound_map_iterator sound_end = sounds.end();
137 for ( ; sound_current != sound_end; ++sound_current ) {
138 FGSimpleSound *s = sound_current->second;
139 delete s->get_sample();
146 void FGSoundMgr::bind ()
151 void FGSoundMgr::unbind ()
157 // run the audio scheduler
158 void FGSoundMgr::update(int dt) {
162 double elapsed = (double)(current - last) / 1000000.0;
165 if ( elapsed > safety ) {
168 safety = safety * 0.99 + elapsed * 0.01;
170 if ( safety > FG_MAX_SOUND_SAFETY ) {
171 safety = FG_MAX_SOUND_SAFETY;
173 // cout << "safety = " << safety << endl;
174 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
176 if ( !audio_sched->not_working() )
177 audio_sched -> update();
181 // add a sound effect, return true if successful
182 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
184 sample_map_iterator it = samples.find(refname);
185 if (it != samples.end())
188 sample_ref *sr = new sample_ref;
191 sr->sample = sound->get_sample();
192 samples[refname] = sr;
194 sounds[refname] = sound;
199 // add a sound from a file, return the sample if successful, else return NULL
200 FGSimpleSound *FGSoundMgr::add( const string& refname, const string &file ) {
201 FGSimpleSound *sound;
203 if (file == (string)"")
206 sample_map_iterator it = samples.find(file);
207 if (it == samples.end()) {
208 sound = new FGSimpleSound(file);
209 sounds[refname] = sound;
211 sample_ref *sr = new sample_ref;
214 sr->sample = sound->get_sample();
218 sample_ref *sr = it->second;
222 new FGSimpleSound(sr->sample->getBuffer(), sr->sample->getLength());
223 sounds[refname] = sound;
230 // remove a sound effect, return true if successful
231 bool FGSoundMgr::remove( const string& refname ) {
233 sound_map_iterator it = sounds.find( refname );
234 if ( it != sounds.end() ) {
235 // first stop the sound from playing (so we don't bomb the
237 FGSimpleSound *sample = it->second;
239 // cout << "Playing " << sample->get_sample()->getPlayCount()
240 // << " instances!" << endl;
242 audio_sched->stopSample( sample->get_sample() );
243 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
246 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
248 SL_VOLUME_ENVELOPE );
250 // must call audio_sched->update() after stopping the sound
251 // but before deleting it.
252 audio_sched -> update();
253 // cout << "Still playing " << sample->get_sample()->getPlayCount()
254 // << " instances!" << endl;
258 // Due to the change in the sound manager, samples live
259 // until the sound manager gets removed.
271 // return true of the specified sound exists in the sound manager system
272 bool FGSoundMgr::exists( const string& refname ) {
273 sound_map_iterator it = sounds.find( refname );
274 if ( it != sounds.end() ) {
282 // return a pointer to the FGSimpleSound if the specified sound exists
283 // in the sound manager system, otherwise return NULL
284 FGSimpleSound *FGSoundMgr::find( const string& refname ) {
285 sound_map_iterator it = sounds.find( refname );
286 if ( it != sounds.end() ) {
294 // tell the scheduler to play the indexed sample in a continuous
296 bool FGSoundMgr::play_looped( const string& refname ) {
297 sound_map_iterator it = sounds.find( refname );
298 if ( it != sounds.end() ) {
299 FGSimpleSound *sample = it->second;
300 audio_sched->loopSample( sample->get_sample() );
301 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
302 sample->get_pitch_envelope(),
304 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
305 sample->get_volume_envelope(),
306 SL_VOLUME_ENVELOPE );
315 // tell the scheduler to play the indexed sample once
316 bool FGSoundMgr::play_once( const string& refname ) {
317 sound_map_iterator it = sounds.find( refname );
318 if ( it != sounds.end() ) {
319 FGSimpleSound *sample = it->second;
320 audio_sched->stopSample( sample->get_sample() );
321 audio_sched->playSample( sample->get_sample() );
322 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
323 sample->get_pitch_envelope(),
325 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
326 sample->get_volume_envelope(),
327 SL_VOLUME_ENVELOPE );
336 // return true of the specified sound is currently being played
337 bool FGSoundMgr::is_playing( const string& refname ) {
338 sound_map_iterator it = sounds.find( refname );
339 if ( it != sounds.end() ) {
340 FGSimpleSound *sample = it->second;
341 return (sample->get_sample()->getPlayCount() > 0 );
349 // immediate stop playing the sound
350 bool FGSoundMgr::stop( const string& refname ) {
351 sound_map_iterator it = sounds.find( refname );
352 if ( it != sounds.end() ) {
353 FGSimpleSound *sample = it->second;
354 audio_sched->stopSample( sample->get_sample() );