]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.cxx
a few more temporarty debugging statements
[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             ALuint buffer = sample->get_buffer();
150             ALuint source = _smgr->request_source();
151             if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
152             {
153                 sample->set_source( source );
154                 
155                 alSourcei( source, AL_BUFFER, buffer );
156                 testForALError("assign buffer to source");
157
158                 sample->set_source( source );
159                 update_sample_config( sample );
160
161                 ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE;
162                 alSourcei( source, AL_LOOPING, looping );
163                 alSourcef( source, AL_ROLLOFF_FACTOR, 1.0 );
164                 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
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 printf("No free source found.");
173             }
174
175         } else if ( sample->is_valid_source() && sample->has_changed() ) {
176             if ( !sample->is_playing() ) {
177                 // a request to stop playing the sound has been filed.
178
179                 sample->stop();
180                 sample->no_valid_source();
181                 _smgr->release_source( sample->get_source() );
182             } else  {
183                 update_sample_config( sample );
184             }
185
186         } else if ( sample->is_valid_source() ) {
187             // check if the sound has stopped by itself
188
189             unsigned int source = sample->get_source();
190             int result;
191
192             alGetSourcei( source, AL_SOURCE_STATE, &result );
193             if ( result == AL_STOPPED ) {
194                 // sample is stoped because it wasn't looping
195                 sample->stop();
196                 sample->no_valid_source();
197                 _smgr->release_source( source );
198                 _smgr->release_buffer( sample );
199             }
200         }
201         testForALError("update");
202     }
203 }
204
205 // add a sound effect, return true if successful
206 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
207
208     sample_map_iterator sample_it = _samples.find( refname );
209     if ( sample_it != _samples.end() ) {
210         // sample name already exists
211         return false;
212     }
213
214     _samples[refname] = sound;
215     return true;
216 }
217
218
219 // remove a sound effect, return true if successful
220 bool SGSampleGroup::remove( const string &refname ) {
221
222     sample_map_iterator sample_it = _samples.find( refname );
223     if ( sample_it == _samples.end() ) {
224         // sample was not found
225         return false;
226     }
227
228     if ( sample_it->second->is_valid_buffer() )
229         _removed_samples.push_back( sample_it->second );
230     _samples.erase( sample_it );
231
232     return true;
233 }
234
235
236 // return true of the specified sound exists in the sound manager system
237 bool SGSampleGroup::exists( const string &refname ) {
238     sample_map_iterator sample_it = _samples.find( refname );
239     if ( sample_it == _samples.end() ) {
240         // sample was not found
241         return false;
242     }
243
244     return true;
245 }
246
247
248 // return a pointer to the SGSoundSample if the specified sound exists
249 // in the sound manager system, otherwise return NULL
250 SGSoundSample *SGSampleGroup::find( const string &refname ) {
251     sample_map_iterator sample_it = _samples.find( refname );
252     if ( sample_it == _samples.end() ) {
253         // sample was not found
254         return NULL;
255     }
256
257     return sample_it->second;
258 }
259
260
261 // stop playing all associated samples
262 void
263 SGSampleGroup::suspend ()
264 {
265     _pause = true;
266     sample_map_iterator sample_current = _samples.begin();
267     sample_map_iterator sample_end = _samples.end();
268     for ( ; sample_current != sample_end; ++sample_current ) {
269         SGSoundSample *sample = sample_current->second;
270
271         if ( sample->is_valid_source() && sample->is_playing() ) {
272             alSourcePause( sample->get_source() );
273         }
274     }
275     testForALError("suspend");
276 }
277
278 // resume playing all associated samples
279 void
280 SGSampleGroup::resume ()
281 {
282     sample_map_iterator sample_current = _samples.begin();
283     sample_map_iterator sample_end = _samples.end();
284     for ( ; sample_current != sample_end; ++sample_current ) {
285         SGSoundSample *sample = sample_current->second;
286
287         if ( sample->is_valid_source() && sample->is_playing() ) {
288             alSourcePlay( sample->get_source() );
289         }
290     }
291     testForALError("resume");
292     _pause = false;
293 }
294
295
296 // tell the scheduler to play the indexed sample in a continuous loop
297 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
298     SGSoundSample *sample = find( refname );
299
300     if ( sample == NULL ) {
301         return false;
302     }
303
304     sample->play( looping );
305     return true;
306 }
307
308
309 // return true of the specified sound is currently being played
310 bool SGSampleGroup::is_playing( const string& refname ) {
311     SGSoundSample *sample = find( refname );
312
313     if ( sample == NULL ) {
314         return false;
315     }
316
317     return ( sample->is_playing() ) ? true : false;
318 }
319
320 // immediate stop playing the sound
321 bool SGSampleGroup::stop( const string& refname ) {
322     SGSoundSample *sample  = find( refname );
323
324     if ( sample == NULL ) {
325         return false;
326     }
327
328     sample->stop();
329     return true;
330 }
331
332 // set source velocity of all managed sounds
333 void SGSampleGroup::set_velocity( const SGVec3d &vel ) {
334     if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) )
335     {
336         SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
337         return;
338     }
339
340     if (_velocity != vel) {
341         sample_map_iterator sample_current = _samples.begin();
342         sample_map_iterator sample_end = _samples.end();
343         for ( ; sample_current != sample_end; ++sample_current ) {
344             SGSoundSample *sample = sample_current->second;
345             sample->set_velocity( vel );
346         }
347         _velocity = vel;
348     }
349 }
350
351 // set the source position of all managed sounds
352 void SGSampleGroup::set_position( const SGGeod& pos ) {
353
354     sample_map_iterator sample_current = _samples.begin();
355     sample_map_iterator sample_end = _samples.end();
356     for ( ; sample_current != sample_end; ++sample_current ) {
357         SGSoundSample *sample = sample_current->second;
358         sample->set_position( pos );
359     }
360     _position = pos;
361 }
362
363
364 // set the source orientation of all managed sounds
365 void SGSampleGroup::set_orientation( const SGQuatd& ori ) {
366
367     if (_orientation != ori) {
368         sample_map_iterator sample_current = _samples.begin();
369         sample_map_iterator sample_end = _samples.end();
370         for ( ; sample_current != sample_end; ++sample_current ) {
371             SGSoundSample *sample = sample_current->second;
372             sample->set_orientation( ori );
373         }
374         _orientation = ori;
375     }
376 }
377
378 void SGSampleGroup::set_volume( float vol )
379 {
380     _volume = vol;
381     if (_volume < 0.0) _volume = 0.0;
382     if (_volume > 1.0) _volume = 1.0;
383
384     sample_map_iterator sample_current = _samples.begin();
385     sample_map_iterator sample_end = _samples.end();
386     for ( ; sample_current != sample_end; ++sample_current ) {
387         SGSoundSample *sample = sample_current->second;
388         sample->set_master_volume( _volume );
389     }
390 }
391
392 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
393     if ( sample->is_valid_source() ) {
394         unsigned int source = sample->get_source();
395
396         if ( _tied_to_listener && _smgr->has_changed() ) {
397             alSourcefv( source, AL_POSITION, _smgr->get_position().data() );
398             alSourcefv( source, AL_DIRECTION, _smgr->get_direction().data() );
399             alSourcefv( source, AL_VELOCITY, _smgr->get_velocity().data() );
400         } else {
401             alSourcefv( source, AL_POSITION, sample->get_position() );
402             alSourcefv( source, AL_DIRECTION, sample->get_orientation() );
403             alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
404         }
405         testForALError("position and orientation");
406
407         alSourcef( source, AL_PITCH, sample->get_pitch() );
408         alSourcef( source, AL_GAIN, sample->get_volume() );
409         testForALError("pitch and gain");
410
411         if ( sample->has_static_data_changed() ) {
412             alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
413             alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
414             alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
415             testForALError("audio cone");
416
417             alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
418             alSourcef( source, AL_REFERENCE_DISTANCE,
419                                sample->get_reference_dist() );
420             testForALError("distance rolloff");
421         }
422     }
423 }
424
425 bool SGSampleGroup::testForError(void *p, string s)
426 {
427    if (p == NULL) {
428       SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
429       return true;
430    }
431    return false;
432 }
433
434 bool SGSampleGroup::testForALError(string s)
435 {
436     ALenum error = alGetError();
437     if (error != AL_NO_ERROR)  {
438        SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
439                                       << alGetString(error) << " at " << s);
440        return true;
441     }
442     return false;
443 }
444