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/fgpath.hxx>
30 #include <Main/globals.hxx>
32 #include "soundmgr.hxx"
36 FGSimpleSound::FGSimpleSound( string file ) {
37 FGPath slfile( globals->get_fg_root() );
38 slfile.append( file );
39 sample = new slSample ( (char *)slfile.c_str() );
40 pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
41 volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
42 pitch_envelope->setStep ( 0, 0.01, 1.0 );
43 volume_envelope->setStep ( 0, 0.01, 1.0 );
46 FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len ) {
47 sample = new slSample ( buffer, len );
48 pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
49 volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
50 pitch_envelope->setStep ( 0, 0.01, 1.0 );
51 volume_envelope->setStep ( 0, 0.01, 1.0 );
55 FGSimpleSound::~FGSimpleSound() {
56 delete pitch_envelope;
57 delete volume_envelope;
63 FGSoundMgr::FGSoundMgr() {
64 audio_sched = new slScheduler( 8000 );
65 audio_mixer = new smMixer;
67 FG_LOG( FG_GENERAL, FG_INFO,
68 "Rate = " << audio_sched->getRate()
69 << " Bps = " << audio_sched->getBps()
70 << " Stereo = " << audio_sched->getStereo() );
74 FGSoundMgr::~FGSoundMgr() {
75 sound_map_iterator current = sounds.begin();
76 sound_map_iterator end = sounds.end();
77 for ( ; current != end; ++current ) {
78 FGSimpleSound *s = current->second;
79 delete s->get_sample();
88 // initialize the sound manager
89 bool FGSoundMgr::init() {
90 audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
91 audio_sched -> setSafetyMargin ( 1.0 ) ;
93 sound_map_iterator current = sounds.begin();
94 sound_map_iterator end = sounds.end();
95 for ( ; current != end; ++current ) {
96 FGSimpleSound *s = current->second;
97 delete s->get_sample();
102 if ( audio_sched->not_working() ) {
110 // run the audio scheduler
111 bool FGSoundMgr::update() {
112 if ( !audio_sched->not_working() ) {
113 audio_sched -> update();
121 // add a sound effect, return true if successful
122 bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
123 sounds[refname] = sound;
129 // remove a sound effect, return true if successful
130 bool FGSoundMgr::remove( const string& refname ) {
132 #if defined ( PLIB_AUDIO_IS_BROKEN )
133 // if PLIB_AUDIO_IS_BROKEN, we can't reliably remove sounds that
134 // are currently being played. :-( So, let's just not remove them
135 // and return false. The effects of this are that the sound
136 // sample will continue to finish playing (or continue to loop
137 // forever.) And the sound sample will remain registered in the
138 // plib audio system. This is a memory leak, and eventually this
139 // could cause us to max out the total number of allowed sound
140 // samples in plib, but what are you going to do? Hopefully the
141 // plib team will do a new stable relase with these problems
144 cout << "plib broken audio, skipping remove" << endl;
149 sound_map_iterator it = sounds.find( refname );
150 if ( it != sounds.end() ) {
151 // first stop the sound from playing (so we don't bomb the
153 FGSimpleSound *sample = it->second;
156 << sample->get_sample()->getPlayCount() << " instances!" << endl;
158 audio_sched->stopSample( sample->get_sample() );
159 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
162 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
164 SL_VOLUME_ENVELOPE );
166 #if defined ( PLIB_AUDIO_IS_BROKEN )
167 // if PLIB_AUDIO_IS_BROKEN, we can't reliably remove sounds
168 // that are currently being played. :-( So, let's just not
169 // remove them and return false. The effects of this are that
170 // the sound sample will continue to finish playing (or
171 // continue to loop forever.) And the sound sample will
172 // remain registered in the plib audio system. This is a
173 // memory leak, and eventually this could cause us to max out
174 // the total number of allowed sound samples in plib, but what
175 // are you going to do? Hopefully the plib team will do a new
176 // stable relase with these problems fixed.
178 cout << "plib broken audio, skipping actual remove" << endl;
182 // must call audio_sched->update() after stopping the sound
183 // but before deleting it.
184 audio_sched -> update();
185 cout << "Still playing "
186 << sample->get_sample()->getPlayCount() << " instances!" << endl;
199 // return true of the specified sound exists in the sound manager system
200 bool FGSoundMgr::exists( const string& refname ) {
201 sound_map_iterator it = sounds.find( refname );
202 if ( it != sounds.end() ) {
210 // tell the scheduler to play the indexed sample in a continuous
212 bool FGSoundMgr::play_looped( const string& refname ) {
213 sound_map_iterator it = sounds.find( refname );
214 if ( it != sounds.end() ) {
215 FGSimpleSound *sample = it->second;
216 audio_sched->loopSample( sample->get_sample() );
217 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
218 sample->get_pitch_envelope(),
220 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
221 sample->get_volume_envelope(),
222 SL_VOLUME_ENVELOPE );
231 // tell the scheduler to play the indexed sample once
232 bool FGSoundMgr::FGSoundMgr::play_once( const string& refname ) {
233 sound_map_iterator it = sounds.find( refname );
234 if ( it != sounds.end() ) {
235 FGSimpleSound *sample = it->second;
236 audio_sched->stopSample( sample->get_sample() );
237 audio_sched->playSample( sample->get_sample() );
238 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
239 sample->get_pitch_envelope(),
241 audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
242 sample->get_volume_envelope(),
243 SL_VOLUME_ENVELOPE );
252 // return true of the specified sound is currently being played
253 bool FGSoundMgr::is_playing( const string& refname ) {
254 sound_map_iterator it = sounds.find( refname );
255 if ( it != sounds.end() ) {
256 FGSimpleSound *sample = it->second;
257 return (sample->get_sample()->getPlayCount() > 0 );
265 // immediate stop playing the sound
266 bool FGSoundMgr::stop( const string& refname ) {
267 sound_map_iterator it = sounds.find( refname );
268 if ( it != sounds.end() ) {
269 FGSimpleSound *sample = it->second;
270 audio_sched->stopSample( sample->get_sample() );