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 - http://www.flightgear.org/~curt
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.
26 #include <simgear/compiler.h>
28 #if defined(__APPLE__)
29 # include <OpenAL/al.h>
30 # include <OpenAL/alut.h>
31 # include <OpenAL/alc.h>
38 #if defined (__APPLE__)
39 // any C++ header file undefines isinf and isnan
40 // so this should be included before <iostream>
41 inline int (isinf)(double r) { return isinf(r); }
42 inline int (isnan)(double r) { return isnan(r); }
45 #if defined(__MINGW32__)
46 #define isnan(x) _isnan(x)
49 #if defined (__FreeBSD__)
50 # if __FreeBSD_version < 500000
52 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
58 #if defined (__FreeBSD__)
59 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
64 #include <simgear/debug/logstream.hxx>
65 #include <simgear/misc/sg_path.hxx>
67 #include "soundmgr_openal.hxx"
75 SGSoundMgr::SGSoundMgr() {
77 SG_LOG( SG_GENERAL, SG_INFO, "Initializing OpenAL sound manager" );
83 if ( alGetError() == AL_NO_ERROR) {
87 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
90 listener_pos[0] = 0.0;
91 listener_pos[1] = 0.0;
92 listener_pos[2] = 0.0;
94 listener_vel[0] = 0.0;
95 listener_vel[1] = 0.0;
96 listener_vel[2] = 0.0;
98 listener_ori[0] = 0.0;
99 listener_ori[1] = 0.0;
100 listener_ori[2] = -1.0;
101 listener_ori[3] = 0.0;
102 listener_ori[4] = 1.0;
103 listener_ori[5] = 0.0;
105 alListenerfv( AL_POSITION, listener_pos );
106 alListenerfv( AL_VELOCITY, listener_vel );
107 alListenerfv( AL_ORIENTATION, listener_ori );
109 if ( alGetError() != AL_NO_ERROR) {
110 SG_LOG( SG_GENERAL, SG_ALERT,
111 "Oops AL error after audio initialization!" );
114 // exaggerate the ear candy?
115 alDopplerFactor(1.0);
116 alDopplerVelocity(340.0); // speed of sound in meters per second.
121 SGSoundMgr::~SGSoundMgr() {
124 // Remove the samples from the sample manager.
126 sample_map_iterator sample_current = samples.begin();
127 sample_map_iterator sample_end = samples.end();
128 for ( ; sample_current != sample_end; ++sample_current ) {
129 SGSoundSample *sample = sample_current->second;
135 // initialize the sound manager
136 void SGSoundMgr::init() {
138 // Remove the samples from the sample manager.
140 sample_map_iterator sample_current = samples.begin();
141 sample_map_iterator sample_end = samples.end();
142 for ( ; sample_current != sample_end; ++sample_current ) {
143 SGSoundSample *sample = sample_current->second;
150 void SGSoundMgr::bind ()
156 void SGSoundMgr::unbind ()
162 // run the audio scheduler
163 void SGSoundMgr::update( double dt ) {
170 ALCcontext *pCurContext = alcGetCurrentContext();
171 alcSuspendContext( pCurContext );
172 if ( alGetError() != AL_NO_ERROR) {
173 SG_LOG( SG_GENERAL, SG_ALERT,
174 "Oops AL error after soundmgr pause()!" );
180 SGSoundMgr::resume ()
182 ALCcontext *pCurContext = alcGetCurrentContext();
183 alcProcessContext( pCurContext );
184 if ( alGetError() != AL_NO_ERROR) {
185 SG_LOG( SG_GENERAL, SG_ALERT,
186 "Oops AL error after soundmgr resume()!" );
191 // add a sound effect, return true if successful
192 bool SGSoundMgr::add( SGSoundSample *sound, const string& refname ) {
194 sample_map_iterator sample_it = samples.find( refname );
195 if ( sample_it != samples.end() ) {
196 // sound already exists
200 samples[refname] = sound;
206 // remove a sound effect, return true if successful
207 bool SGSoundMgr::remove( const string &refname ) {
209 sample_map_iterator sample_it = samples.find( refname );
210 if ( sample_it != samples.end() ) {
211 // first stop the sound from playing (so we don't bomb the
213 SGSoundSample *sample = sample_it->second;
215 samples.erase( sample_it );
217 // cout << "sndmgr: removed -> " << refname << endl;
220 // cout << "sndmgr: failed remove -> " << refname << endl;
226 // return true of the specified sound exists in the sound manager system
227 bool SGSoundMgr::exists( const string &refname ) {
228 sample_map_iterator sample_it = samples.find( refname );
229 if ( sample_it != samples.end() ) {
237 // return a pointer to the SGSoundSample if the specified sound exists
238 // in the sound manager system, otherwise return NULL
239 SGSoundSample *SGSoundMgr::find( const string &refname ) {
240 sample_map_iterator sample_it = samples.find( refname );
241 if ( sample_it != samples.end() ) {
242 return sample_it->second;
249 // tell the scheduler to play the indexed sample in a continuous
251 bool SGSoundMgr::play_looped( const string &refname ) {
252 SGSoundSample *sample;
254 if ( (sample = find( refname )) == NULL ) {
257 sample->play( true );
263 // tell the scheduler to play the indexed sample once
264 bool SGSoundMgr::play_once( const string& refname ) {
265 SGSoundSample *sample;
267 if ( (sample = find( refname )) == NULL ) {
270 sample->play( false );
276 // return true of the specified sound is currently being played
277 bool SGSoundMgr::is_playing( const string& refname ) {
278 SGSoundSample *sample;
280 if ( (sample = find( refname )) == NULL ) {
283 return ( sample->is_playing() );
288 // immediate stop playing the sound
289 bool SGSoundMgr::stop( const string& refname ) {
290 SGSoundSample *sample;
292 if ( (sample = find( refname )) == NULL ) {
301 // set source position of all managed sounds
302 void SGSoundMgr::set_source_pos_all( ALfloat *pos ) {
303 if ( isnan(pos[0]) || isnan(pos[1]) || isnan(pos[2]) ) {
304 // bail if a bad position is passed in
308 sample_map_iterator sample_current = samples.begin();
309 sample_map_iterator sample_end = samples.end();
310 for ( ; sample_current != sample_end; ++sample_current ) {
311 SGSoundSample *sample = sample_current->second;
312 sample->set_source_pos( pos );
317 // set source velocity of all managed sounds
318 void SGSoundMgr::set_source_vel_all( ALfloat *vel ) {
319 if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) ) {
320 // bail if a bad velocity is passed in
324 sample_map_iterator sample_current = samples.begin();
325 sample_map_iterator sample_end = samples.end();
326 for ( ; sample_current != sample_end; ++sample_current ) {
327 SGSoundSample *sample = sample_current->second;
328 sample->set_source_vel( vel );