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