]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.cxx
Initialization was done much earlier than expected resulting in some sample
[simgear.git] / simgear / sound / sample_group.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <simgear_config.h>
3 #endif
4
5 #include <simgear/compiler.h>
6
7 #if defined (__APPLE__)
8 #  ifdef __GNUC__
9 #    if ( __GNUC__ >= 3 ) && ( __GNUC_MINOR__ >= 3 )
10 //  #        include <math.h>
11 inline int (isnan)(double r) { return !(r <= 0 || r >= 0); }
12 #    else
13     // any C++ header file undefines isinf and isnan
14     // so this should be included before <iostream>
15     // the functions are STILL in libm (libSystem on mac os x)
16 extern "C" int isnan (double);
17 extern "C" int isinf (double);
18 #    endif
19 #  else
20 //    inline int (isinf)(double r) { return isinf(r); }
21 //    inline int (isnan)(double r) { return isnan(r); }
22 #  endif
23 #endif
24
25 #if defined (__FreeBSD__)
26 #  if __FreeBSD_version < 500000
27      extern "C" {
28        inline int isnan(double r) { return !(r <= 0 || r >= 0); }
29      }
30 #  endif
31 #endif
32
33 #if defined (__CYGWIN__)
34 #  include <ieeefp.h>
35 #endif
36
37 #if defined(__MINGW32__)
38 #  define isnan(x) _isnan(x)
39 #endif
40
41 #include "soundmgr_openal.hxx"
42 #include "sample_group.hxx"
43
44 SGSampleGroup::SGSampleGroup () :
45     _smgr(NULL),
46     _refname(""),
47     _active(false),
48     _tied_to_listener(false),
49     _velocity(SGVec3d::zeros()),
50     _position(SGGeod()),
51     _orientation(SGQuatd::zeros())
52 {
53     _samples.clear();
54 }
55
56 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
57     _smgr(smgr),
58     _refname(refname),
59     _active(false), 
60     _tied_to_listener(false),
61     _velocity(SGVec3d::zeros()),
62     _position(SGGeod()),
63     _orientation(SGQuatd::zeros())
64 {
65     _smgr->add(this, refname);
66     _samples.clear();
67 }
68
69 SGSampleGroup::~SGSampleGroup ()
70 {
71     _active = false;
72
73     sample_map_iterator sample_current = _samples.begin();
74     sample_map_iterator sample_end = _samples.end();
75     for ( ; sample_current != sample_end; ++sample_current ) {
76         SGSoundSample *sample = sample_current->second;
77
78         if ( sample->is_valid_source() && sample->is_playing() ) {
79             sample->no_valid_source();
80             _smgr->release_source( sample->get_source() );
81             _smgr->release_buffer( sample );
82         }
83     }
84
85     _smgr = 0;
86 }
87
88 void SGSampleGroup::update( double dt ) {
89
90     if ( !_active ) return;
91
92     // testForALError("start of update!!\n");
93
94     sample_map_iterator sample_current = _samples.begin();
95     sample_map_iterator sample_end = _samples.end();
96     for ( ; sample_current != sample_end; ++sample_current ) {
97         SGSoundSample *sample = sample_current->second;
98
99         if ( !sample->is_valid_source() && sample->is_playing() ) {
100             //
101             // a request to start playing a sound has been filed.
102             //
103             if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
104                 continue;
105
106             // start playing the sample
107             ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE;
108             ALuint buffer = sample->get_buffer();
109             ALuint source = _smgr->request_source();
110             if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
111             {
112                 sample->set_source( source );
113                 
114                 alSourcei( source, AL_BUFFER, buffer );
115                 testForALError("assign buffer to source");
116
117                 sample->set_source( source );
118                 update_sample_config( sample );
119
120                 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
121                 alSourcei( source, AL_LOOPING, looping );
122                 alSourcef( source, AL_ROLLOFF_FACTOR, 1.2 );
123                 alSourcePlay( source );
124                 testForALError("sample play");
125             } else {
126                 if (alIsBuffer(buffer) == AL_FALSE) 
127                    SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
128                 // sample->no_valid_source();
129                 // sadly, no free source available at this time
130             }
131
132         } else if ( sample->is_valid_source() && sample->has_changed() ) {
133             if ( !sample->is_playing() ) {
134                 // a request to stop playing the sound has been filed.
135
136                 sample->no_valid_source();
137                 sample->stop();
138                 _smgr->release_source( sample->get_source() );
139             } else  {
140                 update_sample_config( sample );
141             }
142
143         } else if ( sample->is_valid_source() ) {
144             // check if the sound has stopped by itself
145
146             unsigned int source = sample->get_source();
147             int result;
148
149             alGetSourcei( source, AL_SOURCE_STATE, &result );
150             if ( result == AL_STOPPED ) {
151                 // sample is stoped because it wasn't looping
152                 sample->no_valid_source();
153                 sample->stop();
154                 _smgr->release_source( source );
155             }
156         }
157         testForALError("update");
158     }
159 }
160
161 // add a sound effect, return true if successful
162 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
163
164     sample_map_iterator sample_it = _samples.find( refname );
165     if ( sample_it != _samples.end() ) {
166         // sample name already exists
167         return false;
168     }
169
170     _samples[refname] = sound;
171     return true;
172 }
173
174
175 // remove a sound effect, return true if successful
176 bool SGSampleGroup::remove( const string &refname ) {
177
178     sample_map_iterator sample_it = _samples.find( refname );
179     if ( sample_it == _samples.end() ) {
180         // sample was not found
181         return false;
182     }
183
184     // remove the sources buffer
185     _smgr->release_buffer( sample_it->second );
186     _samples.erase( refname );
187
188     return true;
189 }
190
191
192 // return true of the specified sound exists in the sound manager system
193 bool SGSampleGroup::exists( const string &refname ) {
194     sample_map_iterator sample_it = _samples.find( refname );
195     if ( sample_it == _samples.end() ) {
196         // sample was not found
197         return false;
198     }
199
200     return true;
201 }
202
203
204 // return a pointer to the SGSoundSample if the specified sound exists
205 // in the sound manager system, otherwise return NULL
206 SGSoundSample *SGSampleGroup::find( const string &refname ) {
207     sample_map_iterator sample_it = _samples.find( refname );
208     if ( sample_it == _samples.end() ) {
209         // sample was not found
210         return NULL;
211     }
212
213     return sample_it->second;
214 }
215
216
217 // stop playing all associated samples
218 void
219 SGSampleGroup::suspend ()
220 {
221     _active = false;
222     sample_map_iterator sample_current = _samples.begin();
223     sample_map_iterator sample_end = _samples.end();
224     for ( ; sample_current != sample_end; ++sample_current ) {
225         SGSoundSample *sample = sample_current->second;
226
227         if ( sample->is_valid_source() && sample->is_playing() ) {
228             unsigned int source = sample->get_source();
229             alSourcePause( source );
230         }
231     }
232     testForALError("suspend");
233 }
234
235 // resume playing all associated samples
236 void
237 SGSampleGroup::resume ()
238 {
239     sample_map_iterator sample_current = _samples.begin();
240     sample_map_iterator sample_end = _samples.end();
241     for ( ; sample_current != sample_end; ++sample_current ) {
242         SGSoundSample *sample = sample_current->second;
243
244         if ( sample->is_valid_source() && sample->is_playing() ) {
245             unsigned int source = sample->get_source();
246             alSourcePlay( source );
247         }
248     }
249     testForALError("resume");
250     _active = true;
251 }
252
253
254 // tell the scheduler to play the indexed sample in a continuous loop
255 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
256     SGSoundSample *sample = find( refname );
257
258     if ( sample == NULL ) {
259         return false;
260     }
261
262     sample->play( looping );
263     return true;
264 }
265
266
267 // return true of the specified sound is currently being played
268 bool SGSampleGroup::is_playing( const string& refname ) {
269     SGSoundSample *sample = find( refname );
270
271     if ( sample == NULL ) {
272         return false;
273     }
274
275     return ( sample->is_playing() ) ? true : false;
276 }
277
278 // immediate stop playing the sound
279 bool SGSampleGroup::stop( const string& refname ) {
280     SGSoundSample *sample  = find( refname );
281
282     if ( sample == NULL ) {
283         return false;
284     }
285
286     sample->stop();
287     return true;
288 }
289
290 // set source velocity of all managed sounds
291 void SGSampleGroup::set_velocity( SGVec3d &vel ) {
292     if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) )
293     {
294         SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
295         return;
296     }
297
298     if (_velocity != vel) {
299         sample_map_iterator sample_current = _samples.begin();
300         sample_map_iterator sample_end = _samples.end();
301         for ( ; sample_current != sample_end; ++sample_current ) {
302             SGSoundSample *sample = sample_current->second;
303             sample->set_velocity( vel );
304         }
305         _velocity = vel;
306     }
307 }
308
309 // ste the source orientation of all managed sounds
310 void SGSampleGroup::set_position( SGGeod pos ) {
311
312     sample_map_iterator sample_current = _samples.begin();
313     sample_map_iterator sample_end = _samples.end();
314     for ( ; sample_current != sample_end; ++sample_current ) {
315         SGSoundSample *sample = sample_current->second;
316         sample->set_position( pos );
317     }
318     _position = pos;
319 }
320
321
322 // ste the source orientation of all managed sounds
323 void SGSampleGroup::set_orientation( SGQuatd ori ) {
324
325     if (_orientation != ori) {
326         sample_map_iterator sample_current = _samples.begin();
327         sample_map_iterator sample_end = _samples.end();
328         for ( ; sample_current != sample_end; ++sample_current ) {
329             SGSoundSample *sample = sample_current->second;
330             sample->set_orientation( ori );
331         }
332         _orientation = ori;
333     }
334 }
335
336 void SGSampleGroup::set_volume( float vol )
337 {
338     _volume = vol;
339     if (_volume < 0.0) _volume = 0.0;
340     if (_volume > 1.0) _volume = 1.0;
341
342     sample_map_iterator sample_current = _samples.begin();
343     sample_map_iterator sample_end = _samples.end();
344     for ( ; sample_current != sample_end; ++sample_current ) {
345         SGSoundSample *sample = sample_current->second;
346         sample->set_master_volume( _volume );
347     }
348 }
349
350 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
351     if ( sample->is_valid_source() ) {
352         unsigned int source = sample->get_source();
353
354         if ( _tied_to_listener && _smgr->has_changed() ) {
355             alSourcefv( source, AL_POSITION, _smgr->get_position().data() );
356             alSourcefv( source, AL_DIRECTION, _smgr->get_direction().data() );
357             alSourcefv( source, AL_VELOCITY, _smgr->get_velocity().data() );
358         } else {
359             alSourcefv( source, AL_POSITION, sample->get_position());
360             alSourcefv( source, AL_DIRECTION, sample->get_orientation() );
361             alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
362         }
363         testForALError("position and orientation");
364
365         alSourcef( source, AL_PITCH, sample->get_pitch() );
366         alSourcef( source, AL_GAIN, sample->get_volume() );
367         testForALError("pitch and gain");
368
369         if ( sample->has_static_data_changed() ) {
370             alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
371             alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
372             alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
373             testForALError("audio cone");
374
375             alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
376             alSourcef( source, AL_REFERENCE_DISTANCE,
377                                sample->get_reference_dist() );
378             testForALError("distance rolloff");
379         }
380     }
381 }
382
383 bool SGSampleGroup::testForError(void *p, string s)
384 {
385    if (p == NULL) {
386       SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
387       return true;
388    }
389    return false;
390 }
391
392 bool SGSampleGroup::testForALError(string s)
393 {
394     ALenum error = alGetError();
395     if (error != AL_NO_ERROR)  {
396        SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
397                                       << alGetString(error) << " at " << s);
398        return true;
399     }
400     return false;
401 }
402