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 audio_sched -> setMaxConcurrent ( 6 );
69 audio_mixer = new smMixer;
71 SG_LOG( SG_GENERAL, SG_INFO,
72 "Rate = " << audio_sched->getRate()
73 << " Bps = " << audio_sched->getBps()
74 << " Stereo = " << audio_sched->getStereo() );
79 FGSoundMgr::~FGSoundMgr() {
80 sound_map_iterator current = sounds.begin();
81 sound_map_iterator end = sounds.end();
82 for ( ; current != end; ++current ) {
83 FGSimpleSound *s = current->second;
84 delete s->get_sample();
93 // initialize the sound manager
94 void FGSoundMgr::init() {
96 safety = FG_MAX_SOUND_SAFETY;
98 // audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
99 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
101 sound_map_iterator current = sounds.begin();
102 sound_map_iterator end = sounds.end();
103 for ( ; current != end; ++current ) {
104 FGSimpleSound *s = current->second;
105 delete s->get_sample();
112 void FGSoundMgr::bind ()
117 void FGSoundMgr::unbind ()
123 // run the audio scheduler
124 void FGSoundMgr::update(int dt) {
128 double elapsed = (double)(current - last) / 1000000.0;
131 if ( elapsed > safety ) {
134 safety = safety * 0.99 + elapsed * 0.01;
136 if ( safety > FG_MAX_SOUND_SAFETY ) {
137 safety = FG_MAX_SOUND_SAFETY;
139 // cout << "safety = " << safety << endl;
140 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
142 if ( !audio_sched->not_working() )
143 audio_sched -> update();
147 // add a sound effect, return true if successful
148 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
149 sounds[refname] = sound;
155 // remove a sound effect, return true if successful
156 bool FGSoundMgr::remove( const string& refname ) {
158 sound_map_iterator it = sounds.find( refname );
159 if ( it != sounds.end() ) {
160 // first stop the sound from playing (so we don't bomb the
162 FGSimpleSound *sample = it->second;
164 // cout << "Playing " << sample->get_sample()->getPlayCount()
165 // << " instances!" << endl;
167 audio_sched->stopSample( sample->get_sample() );
168 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
171 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
173 SL_VOLUME_ENVELOPE );
175 // must call audio_sched->update() after stopping the sound
176 // but before deleting it.
177 audio_sched -> update();
178 // cout << "Still playing " << sample->get_sample()->getPlayCount()
179 // << " instances!" << endl;
191 // return true of the specified sound exists in the sound manager system
192 bool FGSoundMgr::exists( const string& refname ) {
193 sound_map_iterator it = sounds.find( refname );
194 if ( it != sounds.end() ) {
202 // return a pointer to the FGSimpleSound if the specified sound exists
203 // in the sound manager system, otherwise return NULL
204 FGSimpleSound *FGSoundMgr::find( const string& refname ) {
205 sound_map_iterator it = sounds.find( refname );
206 if ( it != sounds.end() ) {
214 // tell the scheduler to play the indexed sample in a continuous
216 bool FGSoundMgr::play_looped( const string& refname ) {
217 sound_map_iterator it = sounds.find( refname );
218 if ( it != sounds.end() ) {
219 FGSimpleSound *sample = it->second;
220 audio_sched->loopSample( sample->get_sample() );
221 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
222 sample->get_pitch_envelope(),
224 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
225 sample->get_volume_envelope(),
226 SL_VOLUME_ENVELOPE );
235 // tell the scheduler to play the indexed sample once
236 bool FGSoundMgr::play_once( const string& refname ) {
237 sound_map_iterator it = sounds.find( refname );
238 if ( it != sounds.end() ) {
239 FGSimpleSound *sample = it->second;
240 audio_sched->stopSample( sample->get_sample() );
241 audio_sched->playSample( sample->get_sample() );
242 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
243 sample->get_pitch_envelope(),
245 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
246 sample->get_volume_envelope(),
247 SL_VOLUME_ENVELOPE );
256 // return true of the specified sound is currently being played
257 bool FGSoundMgr::is_playing( const string& refname ) {
258 sound_map_iterator it = sounds.find( refname );
259 if ( it != sounds.end() ) {
260 FGSimpleSound *sample = it->second;
261 return (sample->get_sample()->getPlayCount() > 0 );
269 // immediate stop playing the sound
270 bool FGSoundMgr::stop( const string& refname ) {
271 sound_map_iterator it = sounds.find( refname );
272 if ( it != sounds.end() ) {
273 FGSimpleSound *sample = it->second;
274 audio_sched->stopSample( sample->get_sample() );