]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.cxx
documentation, licensing, copyright and small api updates.
[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     _tied_to_listener(false),
71     _velocity(SGVec3d::zeros()),
72     _orientation(SGQuatd::zeros()),
73     _position(SGGeod())
74 {
75     _samples.clear();
76 }
77
78 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
79     _smgr(smgr),
80     _refname(refname),
81     _active(false), 
82     _tied_to_listener(false),
83     _velocity(SGVec3d::zeros()),
84     _orientation(SGQuatd::zeros()),
85     _position(SGGeod())
86 {
87     _smgr->add(this, refname);
88     _samples.clear();
89 }
90
91 SGSampleGroup::~SGSampleGroup ()
92 {
93     _active = false;
94
95     sample_map_iterator sample_current = _samples.begin();
96     sample_map_iterator sample_end = _samples.end();
97     for ( ; sample_current != sample_end; ++sample_current ) {
98         SGSoundSample *sample = sample_current->second;
99
100         if ( sample->is_valid_source() && sample->is_playing() ) {
101             sample->no_valid_source();
102             _smgr->release_source( sample->get_source() );
103             _smgr->release_buffer( sample );
104         }
105     }
106
107     _smgr = 0;
108 }
109
110 void SGSampleGroup::update( double dt ) {
111
112     if ( !_active ) return;
113
114     // testForALError("start of update!!\n");
115
116     sample_map_iterator sample_current = _samples.begin();
117     sample_map_iterator sample_end = _samples.end();
118     for ( ; sample_current != sample_end; ++sample_current ) {
119         SGSoundSample *sample = sample_current->second;
120
121         if ( !sample->is_valid_source() && sample->is_playing() ) {
122             //
123             // a request to start playing a sound has been filed.
124             //
125             if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
126                 continue;
127
128             // start playing the sample
129             ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE;
130             ALuint buffer = sample->get_buffer();
131             ALuint source = _smgr->request_source();
132             if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
133             {
134                 sample->set_source( source );
135                 
136                 alSourcei( source, AL_BUFFER, buffer );
137                 testForALError("assign buffer to source");
138
139                 sample->set_source( source );
140                 update_sample_config( sample );
141
142                 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
143                 alSourcei( source, AL_LOOPING, looping );
144                 alSourcef( source, AL_ROLLOFF_FACTOR, 1.2 );
145                 alSourcePlay( source );
146                 testForALError("sample play");
147             } else {
148                 if (alIsBuffer(buffer) == AL_FALSE) 
149                    SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
150                 // sample->no_valid_source();
151                 // sadly, no free source available at this time
152             }
153
154         } else if ( sample->is_valid_source() && sample->has_changed() ) {
155             if ( !sample->is_playing() ) {
156                 // a request to stop playing the sound has been filed.
157
158                 sample->no_valid_source();
159                 sample->stop();
160                 _smgr->release_source( sample->get_source() );
161             } else  {
162                 update_sample_config( sample );
163             }
164
165         } else if ( sample->is_valid_source() ) {
166             // check if the sound has stopped by itself
167
168             unsigned int source = sample->get_source();
169             int result;
170
171             alGetSourcei( source, AL_SOURCE_STATE, &result );
172             if ( result == AL_STOPPED ) {
173                 // sample is stoped because it wasn't looping
174                 sample->no_valid_source();
175                 sample->stop();
176                 _smgr->release_source( source );
177             }
178         }
179         testForALError("update");
180     }
181 }
182
183 // add a sound effect, return true if successful
184 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
185
186     sample_map_iterator sample_it = _samples.find( refname );
187     if ( sample_it != _samples.end() ) {
188         // sample name already exists
189         return false;
190     }
191
192     _samples[refname] = sound;
193     return true;
194 }
195
196
197 // remove a sound effect, return true if successful
198 bool SGSampleGroup::remove( const string &refname ) {
199
200     sample_map_iterator sample_it = _samples.find( refname );
201     if ( sample_it == _samples.end() ) {
202         // sample was not found
203         return false;
204     }
205
206     // remove the sources buffer
207     _smgr->release_buffer( sample_it->second );
208     _samples.erase( refname );
209
210     return true;
211 }
212
213
214 // return true of the specified sound exists in the sound manager system
215 bool SGSampleGroup::exists( const string &refname ) {
216     sample_map_iterator sample_it = _samples.find( refname );
217     if ( sample_it == _samples.end() ) {
218         // sample was not found
219         return false;
220     }
221
222     return true;
223 }
224
225
226 // return a pointer to the SGSoundSample if the specified sound exists
227 // in the sound manager system, otherwise return NULL
228 SGSoundSample *SGSampleGroup::find( const string &refname ) {
229     sample_map_iterator sample_it = _samples.find( refname );
230     if ( sample_it == _samples.end() ) {
231         // sample was not found
232         return NULL;
233     }
234
235     return sample_it->second;
236 }
237
238
239 // stop playing all associated samples
240 void
241 SGSampleGroup::suspend ()
242 {
243     _active = false;
244     sample_map_iterator sample_current = _samples.begin();
245     sample_map_iterator sample_end = _samples.end();
246     for ( ; sample_current != sample_end; ++sample_current ) {
247         SGSoundSample *sample = sample_current->second;
248
249         if ( sample->is_valid_source() && sample->is_playing() ) {
250             unsigned int source = sample->get_source();
251             alSourcePause( source );
252         }
253     }
254     testForALError("suspend");
255 }
256
257 // resume playing all associated samples
258 void
259 SGSampleGroup::resume ()
260 {
261     sample_map_iterator sample_current = _samples.begin();
262     sample_map_iterator sample_end = _samples.end();
263     for ( ; sample_current != sample_end; ++sample_current ) {
264         SGSoundSample *sample = sample_current->second;
265
266         if ( sample->is_valid_source() && sample->is_playing() ) {
267             unsigned int source = sample->get_source();
268             alSourcePlay( source );
269         }
270     }
271     testForALError("resume");
272     _active = true;
273 }
274
275
276 // tell the scheduler to play the indexed sample in a continuous loop
277 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
278     SGSoundSample *sample = find( refname );
279
280     if ( sample == NULL ) {
281         return false;
282     }
283
284     sample->play( looping );
285     return true;
286 }
287
288
289 // return true of the specified sound is currently being played
290 bool SGSampleGroup::is_playing( const string& refname ) {
291     SGSoundSample *sample = find( refname );
292
293     if ( sample == NULL ) {
294         return false;
295     }
296
297     return ( sample->is_playing() ) ? true : false;
298 }
299
300 // immediate stop playing the sound
301 bool SGSampleGroup::stop( const string& refname ) {
302     SGSoundSample *sample  = find( refname );
303
304     if ( sample == NULL ) {
305         return false;
306     }
307
308     sample->stop();
309     return true;
310 }
311
312 // set source velocity of all managed sounds
313 void SGSampleGroup::set_velocity( const SGVec3d &vel ) {
314     if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) )
315     {
316         SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
317         return;
318     }
319
320     if (_velocity != vel) {
321         sample_map_iterator sample_current = _samples.begin();
322         sample_map_iterator sample_end = _samples.end();
323         for ( ; sample_current != sample_end; ++sample_current ) {
324             SGSoundSample *sample = sample_current->second;
325             sample->set_velocity( vel );
326         }
327         _velocity = vel;
328     }
329 }
330
331 // set the source position of all managed sounds
332 void SGSampleGroup::set_position( const SGGeod& pos ) {
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_position( pos );
339     }
340     _position = pos;
341 }
342
343
344 // set the source orientation of all managed sounds
345 void SGSampleGroup::set_orientation( const SGQuatd& ori ) {
346
347     if (_orientation != ori) {
348         sample_map_iterator sample_current = _samples.begin();
349         sample_map_iterator sample_end = _samples.end();
350         for ( ; sample_current != sample_end; ++sample_current ) {
351             SGSoundSample *sample = sample_current->second;
352             sample->set_orientation( ori );
353         }
354         _orientation = ori;
355     }
356 }
357
358 void SGSampleGroup::set_volume( float vol )
359 {
360     _volume = vol;
361     if (_volume < 0.0) _volume = 0.0;
362     if (_volume > 1.0) _volume = 1.0;
363
364     sample_map_iterator sample_current = _samples.begin();
365     sample_map_iterator sample_end = _samples.end();
366     for ( ; sample_current != sample_end; ++sample_current ) {
367         SGSoundSample *sample = sample_current->second;
368         sample->set_master_volume( _volume );
369     }
370 }
371
372 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
373     if ( sample->is_valid_source() ) {
374         unsigned int source = sample->get_source();
375
376         if ( _tied_to_listener && _smgr->has_changed() ) {
377             alSourcefv( source, AL_POSITION, _smgr->get_position().data() );
378             alSourcefv( source, AL_DIRECTION, _smgr->get_direction().data() );
379             alSourcefv( source, AL_VELOCITY, _smgr->get_velocity().data() );
380         } else {
381             alSourcefv( source, AL_POSITION, sample->get_position() );
382             alSourcefv( source, AL_DIRECTION, sample->get_orientation() );
383             alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
384         }
385         testForALError("position and orientation");
386
387         alSourcef( source, AL_PITCH, sample->get_pitch() );
388         alSourcef( source, AL_GAIN, sample->get_volume() );
389         testForALError("pitch and gain");
390
391         if ( sample->has_static_data_changed() ) {
392             alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
393             alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
394             alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
395             testForALError("audio cone");
396
397             alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
398             alSourcef( source, AL_REFERENCE_DISTANCE,
399                                sample->get_reference_dist() );
400             testForALError("distance rolloff");
401         }
402     }
403 }
404
405 bool SGSampleGroup::testForError(void *p, string s)
406 {
407    if (p == NULL) {
408       SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
409       return true;
410    }
411    return false;
412 }
413
414 bool SGSampleGroup::testForALError(string s)
415 {
416     ALenum error = alGetError();
417     if (error != AL_NO_ERROR)  {
418        SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
419                                       << alGetString(error) << " at " << s);
420        return true;
421     }
422     return false;
423 }
424