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/alc.h>
36 #if defined (__APPLE__)
38 # if ( __GNUC__ >= 3 ) && ( __GNUC_MINOR__ >= 3 )
40 inline int (isnan)(double r) { return !(r <= 0 || r >= 0); }
42 // any C++ header file undefines isinf and isnan
43 // so this should be included before <iostream>
44 // the functions are STILL in libm (libSystem on mac os x)
45 extern "C" int isnan (double);
46 extern "C" int isinf (double);
49 // inline int (isinf)(double r) { return isinf(r); }
50 // inline int (isnan)(double r) { return isnan(r); }
54 #if defined (__FreeBSD__)
55 # if __FreeBSD_version < 500000
57 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"
69 #if defined(__MINGW32__)
70 #define isnan(x) _isnan(x)
78 SGSoundMgr::SGSoundMgr() {
80 SG_LOG( SG_GENERAL, SG_INFO, "Initializing OpenAL sound manager" );
83 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
84 if (!alutInit(NULL, NULL))
86 ALenum error = alutGetError ();
87 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
88 SG_LOG( SG_GENERAL, SG_ALERT, " "+string(alutGetErrorString(error)));
91 context = alcGetCurrentContext();
93 if ( (dev = alcOpenDevice( NULL )) != NULL
94 && ( context = alcCreateContext( dev, NULL )) != NULL ) {
96 alcMakeContextCurrent( context );
100 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
104 listener_pos[0] = 0.0;
105 listener_pos[1] = 0.0;
106 listener_pos[2] = 0.0;
108 listener_vel[0] = 0.0;
109 listener_vel[1] = 0.0;
110 listener_vel[2] = 0.0;
112 listener_ori[0] = 0.0;
113 listener_ori[1] = 0.0;
114 listener_ori[2] = -1.0;
115 listener_ori[3] = 0.0;
116 listener_ori[4] = 1.0;
117 listener_ori[5] = 0.0;
119 alListenerf( AL_GAIN, 0.0f );
120 alListenerfv( AL_POSITION, listener_pos );
121 alListenerfv( AL_VELOCITY, listener_vel );
122 alListenerfv( AL_ORIENTATION, listener_ori );
124 if ( alGetError() != AL_NO_ERROR) {
125 SG_LOG( SG_GENERAL, SG_ALERT,
126 "Oops AL error after audio initialization!" );
129 // exaggerate the ear candy?
130 alDopplerFactor(1.0);
131 alDopplerVelocity(340.0); // speed of sound in meters per second.
136 SGSoundMgr::~SGSoundMgr() {
138 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
142 alcDestroyContext( context );
147 // initialize the sound manager
148 void SGSoundMgr::init() {
150 // Remove the samples from the sample manager.
156 void SGSoundMgr::bind ()
162 void SGSoundMgr::unbind ()
168 // run the audio scheduler
169 void SGSoundMgr::update( double dt ) {
177 alcSuspendContext( context );
178 if ( alGetError() != AL_NO_ERROR) {
179 SG_LOG( SG_GENERAL, SG_ALERT,
180 "Oops AL error after soundmgr pause()!" );
187 SGSoundMgr::resume ()
190 alcProcessContext( context );
191 if ( alGetError() != AL_NO_ERROR) {
192 SG_LOG( SG_GENERAL, SG_ALERT,
193 "Oops AL error after soundmgr resume()!" );
199 // add a sound effect, return true if successful
200 bool SGSoundMgr::add( SGSoundSample *sound, const string& refname ) {
202 sample_map_iterator sample_it = samples.find( refname );
203 if ( sample_it != samples.end() ) {
204 // sound already exists
208 samples[refname] = sound;
214 // remove a sound effect, return true if successful
215 bool SGSoundMgr::remove( const string &refname ) {
217 sample_map_iterator sample_it = samples.find( refname );
218 if ( sample_it != samples.end() ) {
219 // first stop the sound from playing (so we don't bomb the
221 samples.erase( sample_it );
223 // cout << "sndmgr: removed -> " << refname << endl;
226 // cout << "sndmgr: failed remove -> " << refname << endl;
232 // return true of the specified sound exists in the sound manager system
233 bool SGSoundMgr::exists( const string &refname ) {
234 sample_map_iterator sample_it = samples.find( refname );
235 if ( sample_it != samples.end() ) {
243 // return a pointer to the SGSoundSample if the specified sound exists
244 // in the sound manager system, otherwise return NULL
245 SGSoundSample *SGSoundMgr::find( const string &refname ) {
246 sample_map_iterator sample_it = samples.find( refname );
247 if ( sample_it != samples.end() ) {
248 return sample_it->second;
255 // tell the scheduler to play the indexed sample in a continuous
257 bool SGSoundMgr::play_looped( const string &refname ) {
258 SGSoundSample *sample;
260 if ( (sample = find( refname )) == NULL ) {
263 sample->play( true );
269 // tell the scheduler to play the indexed sample once
270 bool SGSoundMgr::play_once( const string& refname ) {
271 SGSoundSample *sample;
273 if ( (sample = find( refname )) == NULL ) {
276 sample->play( false );
282 // return true of the specified sound is currently being played
283 bool SGSoundMgr::is_playing( const string& refname ) {
284 SGSoundSample *sample;
286 if ( (sample = find( refname )) == NULL ) {
289 return ( sample->is_playing() );
294 // immediate stop playing the sound
295 bool SGSoundMgr::stop( const string& refname ) {
296 SGSoundSample *sample;
298 if ( (sample = find( refname )) == NULL ) {
307 // set source position of all managed sounds
308 void SGSoundMgr::set_source_pos_all( ALfloat *pos ) {
309 if ( isnan(pos[0]) || isnan(pos[1]) || isnan(pos[2]) ) {
310 // bail if a bad position is passed in
314 sample_map_iterator sample_current = samples.begin();
315 sample_map_iterator sample_end = samples.end();
316 for ( ; sample_current != sample_end; ++sample_current ) {
317 SGSoundSample *sample = sample_current->second;
318 sample->set_source_pos( pos );
323 // set source velocity of all managed sounds
324 void SGSoundMgr::set_source_vel_all( ALfloat *vel ) {
325 if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) ) {
326 // bail if a bad velocity is passed in
330 sample_map_iterator sample_current = samples.begin();
331 sample_map_iterator sample_end = samples.end();
332 for ( ; sample_current != sample_end; ++sample_current ) {
333 SGSoundSample *sample = sample_current->second;
334 sample->set_source_vel( vel );