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