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() {
84 sound_map_iterator current = sounds.begin();
85 sound_map_iterator end = sounds.end();
86 for ( ; current != end; ++current ) {
87 FGSimpleSound *s = current->second;
88 delete s->get_sample();
97 // initialize the sound manager
98 void FGSoundMgr::init() {
100 safety = FG_MAX_SOUND_SAFETY;
102 // audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
103 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
105 sound_map_iterator current = sounds.begin();
106 sound_map_iterator end = sounds.end();
107 for ( ; current != end; ++current ) {
108 FGSimpleSound *s = current->second;
109 delete s->get_sample();
116 void FGSoundMgr::bind ()
121 void FGSoundMgr::unbind ()
127 // run the audio scheduler
128 void FGSoundMgr::update(int dt) {
132 double elapsed = (double)(current - last) / 1000000.0;
135 if ( elapsed > safety ) {
138 safety = safety * 0.99 + elapsed * 0.01;
140 if ( safety > FG_MAX_SOUND_SAFETY ) {
141 safety = FG_MAX_SOUND_SAFETY;
143 // cout << "safety = " << safety << endl;
144 audio_sched -> setSafetyMargin ( FG_SOUND_SAFETY_MULT * safety ) ;
146 if ( !audio_sched->not_working() )
147 audio_sched -> update();
151 // add a sound effect, return true if successful
152 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
153 sounds[refname] = sound;
159 // remove a sound effect, return true if successful
160 bool FGSoundMgr::remove( const string& refname ) {
162 sound_map_iterator it = sounds.find( refname );
163 if ( it != sounds.end() ) {
164 // first stop the sound from playing (so we don't bomb the
166 FGSimpleSound *sample = it->second;
168 // cout << "Playing " << sample->get_sample()->getPlayCount()
169 // << " instances!" << endl;
171 audio_sched->stopSample( sample->get_sample() );
172 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
175 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
177 SL_VOLUME_ENVELOPE );
179 // must call audio_sched->update() after stopping the sound
180 // but before deleting it.
181 audio_sched -> update();
182 // cout << "Still playing " << sample->get_sample()->getPlayCount()
183 // << " instances!" << endl;
195 // return true of the specified sound exists in the sound manager system
196 bool FGSoundMgr::exists( const string& refname ) {
197 sound_map_iterator it = sounds.find( refname );
198 if ( it != sounds.end() ) {
206 // return a pointer to the FGSimpleSound if the specified sound exists
207 // in the sound manager system, otherwise return NULL
208 FGSimpleSound *FGSoundMgr::find( const string& refname ) {
209 sound_map_iterator it = sounds.find( refname );
210 if ( it != sounds.end() ) {
218 // tell the scheduler to play the indexed sample in a continuous
220 bool FGSoundMgr::play_looped( const string& refname ) {
221 sound_map_iterator it = sounds.find( refname );
222 if ( it != sounds.end() ) {
223 FGSimpleSound *sample = it->second;
224 audio_sched->loopSample( sample->get_sample() );
225 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
226 sample->get_pitch_envelope(),
228 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
229 sample->get_volume_envelope(),
230 SL_VOLUME_ENVELOPE );
239 // tell the scheduler to play the indexed sample once
240 bool FGSoundMgr::play_once( const string& refname ) {
241 sound_map_iterator it = sounds.find( refname );
242 if ( it != sounds.end() ) {
243 FGSimpleSound *sample = it->second;
244 audio_sched->stopSample( sample->get_sample() );
245 audio_sched->playSample( sample->get_sample() );
246 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
247 sample->get_pitch_envelope(),
249 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
250 sample->get_volume_envelope(),
251 SL_VOLUME_ENVELOPE );
260 // return true of the specified sound is currently being played
261 bool FGSoundMgr::is_playing( const string& refname ) {
262 sound_map_iterator it = sounds.find( refname );
263 if ( it != sounds.end() ) {
264 FGSimpleSound *sample = it->second;
265 return (sample->get_sample()->getPlayCount() > 0 );
273 // immediate stop playing the sound
274 bool FGSoundMgr::stop( const string& refname ) {
275 sound_map_iterator it = sounds.find( refname );
276 if ( it != sounds.end() ) {
277 FGSimpleSound *sample = it->second;
278 audio_sched->stopSample( sample->get_sample() );