1 // sample_group.cxx -- Manage a group of samples relative to a base position
3 // Written for the new SoundSystem by Erik Hofman, October 2009
5 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
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.
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.
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.
24 # include <simgear_config.h>
27 #include <simgear/compiler.h>
29 #if defined (__APPLE__)
31 # if ( __GNUC__ >= 3 ) && ( __GNUC_MINOR__ >= 3 )
33 inline int (isnan)(double r) { return !(r <= 0 || r >= 0); }
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);
42 // inline int (isinf)(double r) { return isinf(r); }
43 // inline int (isnan)(double r) { return isnan(r); }
47 #if defined (__FreeBSD__)
48 # if __FreeBSD_version < 500000
50 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
55 #if defined (__CYGWIN__)
59 #if defined(__MINGW32__)
60 # define isnan(x) _isnan(x)
63 #include "soundmgr_openal.hxx"
64 #include "sample_group.hxx"
66 SGSampleGroup::SGSampleGroup () :
71 _tied_to_listener(false),
72 _velocity(SGVec3d::zeros()),
73 _orientation(SGQuatd::zeros()),
79 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
84 _tied_to_listener(false),
85 _velocity(SGVec3d::zeros()),
86 _orientation(SGQuatd::zeros()),
89 _smgr->add(this, refname);
93 SGSampleGroup::~SGSampleGroup ()
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;
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 );
112 void SGSampleGroup::update( double dt ) {
114 if ( !_active || _pause ) return;
116 testForALError("start of update!!\n");
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];
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 );
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;
141 if ( !sample->is_valid_source() && sample->is_playing() ) {
143 // a request to start playing a sound has been filed.
145 if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
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)
154 sample->set_source( source );
156 alSourcei( source, AL_BUFFER, buffer );
157 testForALError("assign buffer to source");
159 sample->set_source( source );
160 update_sample_config( sample );
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");
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
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.
178 sample->no_valid_source();
180 _smgr->release_source( sample->get_source() );
182 update_sample_config( sample );
185 } else if ( sample->is_valid_source() ) {
186 // check if the sound has stopped by itself
188 unsigned int source = sample->get_source();
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();
196 _smgr->release_source( source );
199 testForALError("update");
203 // add a sound effect, return true if successful
204 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
206 sample_map_iterator sample_it = _samples.find( refname );
207 if ( sample_it != _samples.end() ) {
208 // sample name already exists
212 _samples[refname] = sound;
217 // remove a sound effect, return true if successful
218 bool SGSampleGroup::remove( const string &refname ) {
220 sample_map_iterator sample_it = _samples.find( refname );
221 if ( sample_it == _samples.end() ) {
222 // sample was not found
226 _removed_samples.push_back( sample_it->second );
227 _samples.erase( sample_it );
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
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
254 return sample_it->second;
258 // stop playing all associated samples
260 SGSampleGroup::suspend ()
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;
268 if ( sample->is_valid_source() && sample->is_playing() ) {
269 alSourcePause( sample->get_source() );
272 testForALError("suspend");
275 // resume playing all associated samples
277 SGSampleGroup::resume ()
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;
284 if ( sample->is_valid_source() && sample->is_playing() ) {
285 alSourcePlay( sample->get_source() );
288 testForALError("resume");
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 );
297 if ( sample == NULL ) {
301 sample->play( looping );
306 // return true of the specified sound is currently being played
307 bool SGSampleGroup::is_playing( const string& refname ) {
308 SGSoundSample *sample = find( refname );
310 if ( sample == NULL ) {
314 return ( sample->is_playing() ) ? true : false;
317 // immediate stop playing the sound
318 bool SGSampleGroup::stop( const string& refname ) {
319 SGSoundSample *sample = find( refname );
321 if ( sample == NULL ) {
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]) )
333 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
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 );
348 // set the source position of all managed sounds
349 void SGSampleGroup::set_position( const SGGeod& pos ) {
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 );
361 // set the source orientation of all managed sounds
362 void SGSampleGroup::set_orientation( const SGQuatd& ori ) {
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 );
375 void SGSampleGroup::set_volume( float vol )
378 if (_volume < 0.0) _volume = 0.0;
379 if (_volume > 1.0) _volume = 1.0;
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 );
389 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
390 if ( sample->is_valid_source() ) {
391 unsigned int source = sample->get_source();
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() );
398 alSourcefv( source, AL_POSITION, sample->get_position() );
399 alSourcefv( source, AL_DIRECTION, sample->get_orientation() );
400 alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
402 testForALError("position and orientation");
404 alSourcef( source, AL_PITCH, sample->get_pitch() );
405 alSourcef( source, AL_GAIN, sample->get_volume() );
406 testForALError("pitch and gain");
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");
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");
422 bool SGSampleGroup::testForError(void *p, string s)
425 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
431 bool SGSampleGroup::testForALError(string s)
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);