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 ) {
43 SGPath slfile( globals->get_fg_root() );
44 slfile.append( file );
45 sample = new slSample ( (char *)slfile.c_str() );
46 pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
47 volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
48 pitch_envelope->setStep ( 0, 0.01, 1.0 );
49 volume_envelope->setStep ( 0, 0.01, 1.0 );
52 FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len ) {
53 sample = new slSample ( buffer, len );
54 pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
55 volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
56 pitch_envelope->setStep ( 0, 0.01, 1.0 );
57 volume_envelope->setStep ( 0, 0.01, 1.0 );
61 FGSimpleSound::~FGSimpleSound() {
62 delete pitch_envelope;
63 delete volume_envelope;
67 void FGSimpleSound::play_once( slScheduler *sched ) {
68 sched->stopSample(sample);
69 sched->playSample(sample);
70 sched->addSampleEnvelope(sample, 0, 0, pitch_envelope, SL_PITCH_ENVELOPE);
71 sched->addSampleEnvelope(sample, 0, 1, volume_envelope, SL_VOLUME_ENVELOPE);
74 void FGSimpleSound::play_looped( slScheduler *sched ) {
75 sched->loopSample(sample);
76 sched->addSampleEnvelope(sample, 0, 0, pitch_envelope, SL_PITCH_ENVELOPE);
77 sched->addSampleEnvelope(sample, 0, 1, volume_envelope, SL_VOLUME_ENVELOPE);
85 FGSoundMgr::FGSoundMgr() {
86 audio_sched = new slScheduler( 8000 );
87 if ( audio_sched->notWorking() ) {
88 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
90 audio_sched -> setMaxConcurrent ( 6 );
92 audio_mixer = new smMixer;
94 SG_LOG( SG_GENERAL, SG_INFO,
95 "Rate = " << audio_sched->getRate()
96 << " Bps = " << audio_sched->getBps()
97 << " Stereo = " << audio_sched->getStereo() );
103 FGSoundMgr::~FGSoundMgr() {
106 // Remove the samples from the sample manager.
108 sample_map_iterator sample_current = samples.begin();
109 sample_map_iterator sample_end = samples.end();
110 for ( ; sample_current != sample_end; ++sample_current ) {
111 sample_ref *sr = sample_current->second;
117 // Remove the sounds from the sound manager.
119 sound_map_iterator sound_current = sounds.begin();
120 sound_map_iterator sound_end = sounds.end();
121 for ( ; sound_current != sound_end; ++sound_current ) {
122 FGSimpleSound *s = sound_current->second;
131 // initialize the sound manager
132 void FGSoundMgr::init() {
134 safety = FG_MAX_SOUND_SAFETY;
136 // audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
137 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
141 // Remove the samples from the sample manager.
143 sample_map_iterator sample_current = samples.begin();
144 sample_map_iterator sample_end = samples.end();
145 for ( ; sample_current != sample_end; ++sample_current ) {
146 sample_ref *sr = sample_current->second;
153 // Remove the sounds from the sound manager.
155 sound_map_iterator sound_current = sounds.begin();
156 sound_map_iterator sound_end = sounds.end();
157 for ( ; sound_current != sound_end; ++sound_current ) {
158 FGSimpleSound *s = sound_current->second;
159 delete s->get_sample();
166 void FGSoundMgr::bind ()
171 void FGSoundMgr::unbind ()
177 // run the audio scheduler
178 void FGSoundMgr::update(int dt) {
182 double elapsed = (double)(current - last) / 1000000.0;
185 if ( elapsed > safety ) {
188 safety = safety * 0.99 + elapsed * 0.01;
190 if ( safety > FG_MAX_SOUND_SAFETY ) {
191 safety = FG_MAX_SOUND_SAFETY;
193 // cout << "safety = " << safety << endl;
194 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
196 if ( !audio_sched->not_working() )
197 audio_sched -> update();
201 // add a sound effect, return true if successful
202 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
204 sample_map_iterator it = samples.find(refname);
205 if (it != samples.end())
208 sample_ref *sr = new sample_ref;
211 sr->sample = sound->get_sample();
212 samples[refname] = sr;
214 sounds[refname] = sound;
219 // add a sound from a file, return the sample if successful, else return NULL
220 FGSimpleSound *FGSoundMgr::add( const string& refname, const string &file ) {
221 FGSimpleSound *sound;
223 if (file == (string)"")
226 sample_map_iterator it = samples.find(file);
227 if (it == samples.end()) {
228 sound = new FGSimpleSound(file);
229 sounds[refname] = sound;
231 sample_ref *sr = new sample_ref;
234 sr->sample = sound->get_sample();
238 sample_ref *sr = it->second;
242 new FGSimpleSound(sr->sample->getBuffer(), sr->sample->getLength());
243 sounds[refname] = sound;
250 // remove a sound effect, return true if successful
251 bool FGSoundMgr::remove( const string& refname ) {
253 sound_map_iterator it = sounds.find( refname );
254 if ( it != sounds.end() ) {
255 // first stop the sound from playing (so we don't bomb the
257 FGSimpleSound *sample = it->second;
259 // cout << "Playing " << sample->get_sample()->getPlayCount()
260 // << " instances!" << endl;
262 audio_sched->stopSample( sample->get_sample() );
263 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
266 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
268 SL_VOLUME_ENVELOPE );
270 // must call audio_sched->update() after stopping the sound
271 // but before deleting it.
272 audio_sched -> update();
273 // cout << "Still playing " << sample->get_sample()->getPlayCount()
274 // << " instances!" << endl;
278 // Due to the change in the sound manager, samples live
279 // until the sound manager gets removed.
291 // return true of the specified sound exists in the sound manager system
292 bool FGSoundMgr::exists( const string& refname ) {
293 sound_map_iterator it = sounds.find( refname );
294 if ( it != sounds.end() ) {
302 // return a pointer to the FGSimpleSound if the specified sound exists
303 // in the sound manager system, otherwise return NULL
304 FGSimpleSound *FGSoundMgr::find( const string& refname ) {
305 sound_map_iterator it = sounds.find( refname );
306 if ( it != sounds.end() ) {
314 // tell the scheduler to play the indexed sample in a continuous
316 bool FGSoundMgr::play_looped( const string& refname ) {
317 FGSimpleSound *sample;
319 if ((sample = find( refname )) == NULL)
322 sample->play(audio_sched, true);
327 // tell the scheduler to play the indexed sample once
328 bool FGSoundMgr::play_once( const string& refname ) {
329 FGSimpleSound *sample;
331 if ((sample = find( refname )) == NULL)
334 sample->play(audio_sched, false);
339 // return true of the specified sound is currently being played
340 bool FGSoundMgr::is_playing( const string& refname ) {
341 FGSimpleSound *sample;
343 if ((sample = find( refname )) == NULL)
346 return sample->is_playing();
350 // immediate stop playing the sound
351 bool FGSoundMgr::stop( const string& refname ) {
352 FGSimpleSound *sample;
354 if ((sample = find( refname )) == NULL)
357 audio_sched->stopSample( sample->get_sample() );