]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.cxx
use auto_ptr instead
[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.0 );
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             alSourcePause( sample->get_source() );
251         }
252     }
253     testForALError("suspend");
254 }
255
256 // resume playing all associated samples
257 void
258 SGSampleGroup::resume ()
259 {
260     sample_map_iterator sample_current = _samples.begin();
261     sample_map_iterator sample_end = _samples.end();
262     for ( ; sample_current != sample_end; ++sample_current ) {
263         SGSoundSample *sample = sample_current->second;
264
265         if ( sample->is_valid_source() && sample->is_playing() ) {
266             alSourcePlay( sample->get_source() );
267         }
268     }
269     testForALError("resume");
270     _active = true;
271 }
272
273
274 // tell the scheduler to play the indexed sample in a continuous loop
275 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
276     SGSoundSample *sample = find( refname );
277
278     if ( sample == NULL ) {
279         return false;
280     }
281
282     sample->play( looping );
283     return true;
284 }
285
286
287 // return true of the specified sound is currently being played
288 bool SGSampleGroup::is_playing( const string& refname ) {
289     SGSoundSample *sample = find( refname );
290
291     if ( sample == NULL ) {
292         return false;
293     }
294
295     return ( sample->is_playing() ) ? true : false;
296 }
297
298 // immediate stop playing the sound
299 bool SGSampleGroup::stop( const string& refname ) {
300     SGSoundSample *sample  = find( refname );
301
302     if ( sample == NULL ) {
303         return false;
304     }
305
306     sample->stop();
307     return true;
308 }
309
310 // set source velocity of all managed sounds
311 void SGSampleGroup::set_velocity( const SGVec3d &vel ) {
312     if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) )
313     {
314         SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
315         return;
316     }
317
318     if (_velocity != vel) {
319         sample_map_iterator sample_current = _samples.begin();
320         sample_map_iterator sample_end = _samples.end();
321         for ( ; sample_current != sample_end; ++sample_current ) {
322             SGSoundSample *sample = sample_current->second;
323             sample->set_velocity( vel );
324         }
325         _velocity = vel;
326     }
327 }
328
329 // set the source position of all managed sounds
330 void SGSampleGroup::set_position( const SGGeod& pos ) {
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_position( pos );
337     }
338     _position = pos;
339 }
340
341
342 // set the source orientation of all managed sounds
343 void SGSampleGroup::set_orientation( const SGQuatd& ori ) {
344
345     if (_orientation != ori) {
346         sample_map_iterator sample_current = _samples.begin();
347         sample_map_iterator sample_end = _samples.end();
348         for ( ; sample_current != sample_end; ++sample_current ) {
349             SGSoundSample *sample = sample_current->second;
350             sample->set_orientation( ori );
351         }
352         _orientation = ori;
353     }
354 }
355
356 void SGSampleGroup::set_volume( float vol )
357 {
358     _volume = vol;
359     if (_volume < 0.0) _volume = 0.0;
360     if (_volume > 1.0) _volume = 1.0;
361
362     sample_map_iterator sample_current = _samples.begin();
363     sample_map_iterator sample_end = _samples.end();
364     for ( ; sample_current != sample_end; ++sample_current ) {
365         SGSoundSample *sample = sample_current->second;
366         sample->set_master_volume( _volume );
367     }
368 }
369
370 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
371     if ( sample->is_valid_source() ) {
372         unsigned int source = sample->get_source();
373
374         if ( _tied_to_listener && _smgr->has_changed() ) {
375             alSourcefv( source, AL_POSITION, _smgr->get_position().data() );
376             alSourcefv( source, AL_DIRECTION, _smgr->get_direction().data() );
377             alSourcefv( source, AL_VELOCITY, _smgr->get_velocity().data() );
378         } else {
379             alSourcefv( source, AL_POSITION, sample->get_position() );
380             alSourcefv( source, AL_DIRECTION, sample->get_orientation() );
381             alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
382         }
383         testForALError("position and orientation");
384
385         alSourcef( source, AL_PITCH, sample->get_pitch() );
386         alSourcef( source, AL_GAIN, sample->get_volume() );
387         testForALError("pitch and gain");
388
389         if ( sample->has_static_data_changed() ) {
390             alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
391             alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
392             alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
393             testForALError("audio cone");
394
395             alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
396             alSourcef( source, AL_REFERENCE_DISTANCE,
397                                sample->get_reference_dist() );
398             testForALError("distance rolloff");
399         }
400     }
401 }
402
403 bool SGSampleGroup::testForError(void *p, string s)
404 {
405    if (p == NULL) {
406       SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
407       return true;
408    }
409    return false;
410 }
411
412 bool SGSampleGroup::testForALError(string s)
413 {
414     ALenum error = alGetError();
415     if (error != AL_NO_ERROR)  {
416        SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
417                                       << alGetString(error) << " at " << s);
418        return true;
419     }
420     return false;
421 }
422