]> git.mxchange.org Git - simgear.git/blob - simgear/sound/soundmgr_openal.cxx
- new FSF addresses
[simgear.git] / simgear / sound / soundmgr_openal.cxx
1 // soundmgr.cxx -- Sound effect management class
2 //
3 // Sound manager initially written by David Findlay
4 // <david_j_findlay@yahoo.com.au> 2001
5 //
6 // C++-ified by Curtis Olson, started March 2001.
7 //
8 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
9 //
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.
14 //
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.
19 //
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26 #ifdef HAVE_CONFIG_H
27 #  include <simgear_config.h>
28 #endif
29
30 #include <simgear/compiler.h>
31
32 #if defined(__APPLE__)
33 # include <OpenAL/al.h>
34 # include <OpenAL/alc.h>
35 #else
36 # include <AL/al.h>
37 # include <AL/alc.h>
38 #endif
39
40 #if defined (__APPLE__)
41 #  ifdef __GNUC__
42 #    if ( __GNUC__ >= 3 ) && ( __GNUC_MINOR__ >= 3 )
43 //  #        include <math.h>
44 inline int (isnan)(double r) { return !(r <= 0 || r >= 0); }
45 #    else
46     // any C++ header file undefines isinf and isnan
47     // so this should be included before <iostream>
48     // the functions are STILL in libm (libSystem on mac os x)
49 extern "C" int isnan (double);
50 extern "C" int isinf (double);
51 #    endif
52 #  else
53 //    inline int (isinf)(double r) { return isinf(r); }
54 //    inline int (isnan)(double r) { return isnan(r); }
55 #  endif
56 #endif
57
58 #if defined (__FreeBSD__)
59 #  if __FreeBSD_version < 500000
60      extern "C" {
61        inline int isnan(double r) { return !(r <= 0 || r >= 0); }
62      }
63 #  endif
64 #endif
65
66 #include STL_IOSTREAM
67
68 #include <simgear/debug/logstream.hxx>
69 #include <simgear/misc/sg_path.hxx>
70
71 #include "soundmgr_openal.hxx"
72
73 #if defined(__MINGW32__)
74 #define isnan(x) _isnan(x)
75 #endif
76
77 //
78 // Sound Manager
79 //
80
81 // constructor
82 SGSoundMgr::SGSoundMgr() {
83
84     SG_LOG( SG_GENERAL, SG_INFO, "Initializing OpenAL sound manager" );
85
86     // initialize OpenAL
87 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
88     if (!alutInit(NULL, NULL))
89     {
90         ALenum error = alutGetError ();
91         SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
92         SG_LOG( SG_GENERAL, SG_ALERT, "   "+string(alutGetErrorString(error)));
93         working = false;
94     }
95     context = alcGetCurrentContext();
96 #else
97     if ( (dev = alcOpenDevice( NULL )) != NULL
98             && ( context = alcCreateContext( dev, NULL )) != NULL ) {
99         working = true;
100         alcMakeContextCurrent( context );
101     } else {
102         working = false;
103         context = 0;
104         SG_LOG( SG_GENERAL, SG_ALERT, "Audio initialization failed!" );
105     }
106 #endif
107
108     listener_pos[0] = 0.0;
109     listener_pos[1] = 0.0;
110     listener_pos[2] = 0.0;
111
112     listener_vel[0] = 0.0;
113     listener_vel[1] = 0.0;
114     listener_vel[2] = 0.0;
115     
116     listener_ori[0] = 0.0;
117     listener_ori[1] = 0.0;
118     listener_ori[2] = -1.0;
119     listener_ori[3] = 0.0;
120     listener_ori[4] = 1.0;
121     listener_ori[5] = 0.0;
122
123     alListenerf( AL_GAIN, 0.0f );
124     alListenerfv( AL_POSITION, listener_pos );
125     alListenerfv( AL_VELOCITY, listener_vel );
126     alListenerfv( AL_ORIENTATION, listener_ori );
127     alGetError();
128     if ( alGetError() != AL_NO_ERROR) {
129         SG_LOG( SG_GENERAL, SG_ALERT,
130                 "Oops AL error after audio initialization!" );
131     }
132
133     // exaggerate the ear candy?
134     alDopplerFactor(1.0);
135     alDopplerVelocity(340.0);   // speed of sound in meters per second.
136 }
137
138 // destructor
139
140 SGSoundMgr::~SGSoundMgr() {
141
142 #if defined(ALUT_API_MAJOR_VERSION) && ALUT_API_MAJOR_VERSION >= 1
143     alutExit ();
144 #else
145     if (context)
146         alcDestroyContext( context );
147 #endif
148 }
149
150
151 // initialize the sound manager
152 void SGSoundMgr::init() {
153     //
154     // Remove the samples from the sample manager.
155     //
156     samples.clear();
157 }
158
159
160 void SGSoundMgr::bind ()
161 {
162     // no properties
163 }
164
165
166 void SGSoundMgr::unbind ()
167 {
168     // no properties
169 }
170
171
172 // run the audio scheduler
173 void SGSoundMgr::update( double dt ) {
174 }
175
176
177 void
178 SGSoundMgr::pause ()
179 {
180     if (context) {
181         alcSuspendContext( context );
182         if ( alGetError() != AL_NO_ERROR) {
183             SG_LOG( SG_GENERAL, SG_ALERT,
184                     "Oops AL error after soundmgr pause()!" );
185         }
186     }
187 }
188
189
190 void
191 SGSoundMgr::resume ()
192 {
193     if (context) {
194         alcProcessContext( context );
195         if ( alGetError() != AL_NO_ERROR) {
196             SG_LOG( SG_GENERAL, SG_ALERT,
197                     "Oops AL error after soundmgr resume()!" );
198         }
199     }
200 }
201
202
203 // add a sound effect, return true if successful
204 bool SGSoundMgr::add( SGSoundSample *sound, const string& refname ) {
205
206     sample_map_iterator sample_it = samples.find( refname );
207     if ( sample_it != samples.end() ) {
208         // sound already exists
209         return false;
210     }
211
212     samples[refname] = sound;
213
214     return true;
215 }
216
217
218 // remove a sound effect, return true if successful
219 bool SGSoundMgr::remove( const string &refname ) {
220
221     sample_map_iterator sample_it = samples.find( refname );
222     if ( sample_it != samples.end() ) {
223         // first stop the sound from playing (so we don't bomb the
224         // audio scheduler)
225         samples.erase( sample_it );
226
227         // cout << "sndmgr: removed -> " << refname << endl;
228         return true;
229     } else {
230         // cout << "sndmgr: failed remove -> " << refname << endl;
231         return false;
232     }
233 }
234
235
236 // return true of the specified sound exists in the sound manager system
237 bool SGSoundMgr::exists( const string &refname ) {
238     sample_map_iterator sample_it = samples.find( refname );
239     if ( sample_it != samples.end() ) {
240         return true;
241     } else {
242         return false;
243     }
244 }
245
246
247 // return a pointer to the SGSoundSample if the specified sound exists
248 // in the sound manager system, otherwise return NULL
249 SGSoundSample *SGSoundMgr::find( const string &refname ) {
250     sample_map_iterator sample_it = samples.find( refname );
251     if ( sample_it != samples.end() ) {
252         return sample_it->second;
253     } else {
254         return NULL;
255     }
256 }
257
258
259 // tell the scheduler to play the indexed sample in a continuous
260 // loop
261 bool SGSoundMgr::play_looped( const string &refname ) {
262     SGSoundSample *sample;
263
264     if ( (sample = find( refname )) == NULL ) {
265         return false;
266     } else {
267         sample->play( true );
268         return true;
269     }
270 }
271
272
273 // tell the scheduler to play the indexed sample once
274 bool SGSoundMgr::play_once( const string& refname ) {
275     SGSoundSample *sample;
276
277     if ( (sample = find( refname )) == NULL ) {
278         return false;
279     } else {
280         sample->play( false );
281         return true;
282     }
283 }
284
285
286 // return true of the specified sound is currently being played
287 bool SGSoundMgr::is_playing( const string& refname ) {
288     SGSoundSample *sample;
289
290     if ( (sample = find( refname )) == NULL ) {
291         return false;
292     } else {
293         return ( sample->is_playing() );
294     }
295 }
296
297
298 // immediate stop playing the sound
299 bool SGSoundMgr::stop( const string& refname ) {
300     SGSoundSample *sample;
301
302     if ( (sample = find( refname )) == NULL ) {
303         return false;
304     } else {
305         sample->stop();
306         return true;
307     }
308 }
309
310
311 // set source position of all managed sounds
312 void SGSoundMgr::set_source_pos_all( ALfloat *pos ) {
313     if ( isnan(pos[0]) || isnan(pos[1]) || isnan(pos[2]) ) {
314         // bail if a bad position is passed in
315         return;
316     }
317
318     sample_map_iterator sample_current = samples.begin();
319     sample_map_iterator sample_end = samples.end();
320     for ( ; sample_current != sample_end; ++sample_current ) {
321         SGSoundSample *sample = sample_current->second;
322         sample->set_source_pos( pos );
323     }
324 }
325
326
327 // set source velocity of all managed sounds
328 void SGSoundMgr::set_source_vel_all( ALfloat *vel ) {
329     if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) ) {
330         // bail if a bad velocity is passed in
331         return;
332     }
333
334     sample_map_iterator sample_current = samples.begin();
335     sample_map_iterator sample_end = samples.end();
336     for ( ; sample_current != sample_end; ++sample_current ) {
337         SGSoundSample *sample = sample_current->second;
338         sample->set_source_vel( vel );
339     }
340 }