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 extern "C" int isnan (double);
41 extern "C" int isinf (double);
43 // any C++ header file undefines isinf and isnan
44 // so this should be included before <iostream>
45 // the functions are STILL in libm (libSystem on mac os x)
46 extern "C" int isnan (double);
47 extern "C" int isinf (double);
50 // inline int (isinf)(double r) { return isinf(r); }
51 // inline int (isnan)(double r) { return isnan(r); }
55 #if defined (__FreeBSD__)
56 # if __FreeBSD_version < 500000
58 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
65 #include <simgear/debug/logstream.hxx>
66 #include <simgear/misc/sg_path.hxx>
68 #include "soundmgr_openal.hxx"
70 #if defined(__MINGW32__)
71 #define isnan(x) _isnan(x)
79 SGSoundMgr::SGSoundMgr() {
81 SG_LOG( SG_GENERAL, SG_INFO, "Initializing OpenAL sound manager" );
84 if ( (dev = alcOpenDevice( NULL )) != NULL
85 && ( context = alcCreateContext( dev, NULL )) != NULL ) {
87 alcMakeContextCurrent( context );
91 SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
94 listener_pos[0] = 0.0;
95 listener_pos[1] = 0.0;
96 listener_pos[2] = 0.0;
98 listener_vel[0] = 0.0;
99 listener_vel[1] = 0.0;
100 listener_vel[2] = 0.0;
102 listener_ori[0] = 0.0;
103 listener_ori[1] = 0.0;
104 listener_ori[2] = -1.0;
105 listener_ori[3] = 0.0;
106 listener_ori[4] = 1.0;
107 listener_ori[5] = 0.0;
109 alListenerf( AL_GAIN, 0.0f );
110 alListenerfv( AL_POSITION, listener_pos );
111 alListenerfv( AL_VELOCITY, listener_vel );
112 alListenerfv( AL_ORIENTATION, listener_ori );
114 if ( alGetError() != AL_NO_ERROR) {
115 SG_LOG( SG_GENERAL, SG_ALERT,
116 "Oops AL error after audio initialization!" );
119 // exaggerate the ear candy?
120 alDopplerFactor(1.0);
121 alDopplerVelocity(340.0); // speed of sound in meters per second.
126 SGSoundMgr::~SGSoundMgr() {
129 alcDestroyContext( context );
131 // Remove the samples from the sample manager.
133 sample_map_iterator sample_current = samples.begin();
134 sample_map_iterator sample_end = samples.end();
135 for ( ; sample_current != sample_end; ++sample_current ) {
136 SGSoundSample *sample = sample_current->second;
142 // initialize the sound manager
143 void SGSoundMgr::init() {
145 // Remove the samples from the sample manager.
147 sample_map_iterator sample_current = samples.begin();
148 sample_map_iterator sample_end = samples.end();
149 for ( ; sample_current != sample_end; ++sample_current ) {
150 SGSoundSample *sample = sample_current->second;
157 void SGSoundMgr::bind ()
163 void SGSoundMgr::unbind ()
169 // run the audio scheduler
170 void SGSoundMgr::update( double dt ) {
178 alcSuspendContext( context );
179 if ( alGetError() != AL_NO_ERROR) {
180 SG_LOG( SG_GENERAL, SG_ALERT,
181 "Oops AL error after soundmgr pause()!" );
188 SGSoundMgr::resume ()
191 alcProcessContext( context );
192 if ( alGetError() != AL_NO_ERROR) {
193 SG_LOG( SG_GENERAL, SG_ALERT,
194 "Oops AL error after soundmgr resume()!" );
200 // add a sound effect, return true if successful
201 bool SGSoundMgr::add( SGSoundSample *sound, const string& refname ) {
203 sample_map_iterator sample_it = samples.find( refname );
204 if ( sample_it != samples.end() ) {
205 // sound already exists
209 samples[refname] = sound;
215 // remove a sound effect, return true if successful
216 bool SGSoundMgr::remove( const string &refname ) {
218 sample_map_iterator sample_it = samples.find( refname );
219 if ( sample_it != samples.end() ) {
220 // first stop the sound from playing (so we don't bomb the
222 SGSoundSample *sample = sample_it->second;
224 samples.erase( sample_it );
226 // cout << "sndmgr: removed -> " << refname << endl;
229 // cout << "sndmgr: failed remove -> " << refname << endl;
235 // return true of the specified sound exists in the sound manager system
236 bool SGSoundMgr::exists( const string &refname ) {
237 sample_map_iterator sample_it = samples.find( refname );
238 if ( sample_it != samples.end() ) {
246 // return a pointer to the SGSoundSample if the specified sound exists
247 // in the sound manager system, otherwise return NULL
248 SGSoundSample *SGSoundMgr::find( const string &refname ) {
249 sample_map_iterator sample_it = samples.find( refname );
250 if ( sample_it != samples.end() ) {
251 return sample_it->second;
258 // tell the scheduler to play the indexed sample in a continuous
260 bool SGSoundMgr::play_looped( const string &refname ) {
261 SGSoundSample *sample;
263 if ( (sample = find( refname )) == NULL ) {
266 sample->play( true );
272 // tell the scheduler to play the indexed sample once
273 bool SGSoundMgr::play_once( const string& refname ) {
274 SGSoundSample *sample;
276 if ( (sample = find( refname )) == NULL ) {
279 sample->play( false );
285 // return true of the specified sound is currently being played
286 bool SGSoundMgr::is_playing( const string& refname ) {
287 SGSoundSample *sample;
289 if ( (sample = find( refname )) == NULL ) {
292 return ( sample->is_playing() );
297 // immediate stop playing the sound
298 bool SGSoundMgr::stop( const string& refname ) {
299 SGSoundSample *sample;
301 if ( (sample = find( refname )) == NULL ) {
310 // set source position of all managed sounds
311 void SGSoundMgr::set_source_pos_all( ALfloat *pos ) {
312 if ( isnan(pos[0]) || isnan(pos[1]) || isnan(pos[2]) ) {
313 // bail if a bad position is passed in
317 sample_map_iterator sample_current = samples.begin();
318 sample_map_iterator sample_end = samples.end();
319 for ( ; sample_current != sample_end; ++sample_current ) {
320 SGSoundSample *sample = sample_current->second;
321 sample->set_source_pos( pos );
326 // set source velocity of all managed sounds
327 void SGSoundMgr::set_source_vel_all( ALfloat *vel ) {
328 if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) ) {
329 // bail if a bad velocity is passed in
333 sample_map_iterator sample_current = samples.begin();
334 sample_map_iterator sample_end = samples.end();
335 for ( ; sample_current != sample_end; ++sample_current ) {
336 SGSoundSample *sample = sample_current->second;
337 sample->set_source_vel( vel );