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 ALuint buffer = sample->get_buffer();
150 ALuint source = _smgr->request_source();
151 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
153 sample->set_source( source );
155 alSourcei( source, AL_BUFFER, buffer );
156 testForALError("assign buffer to source");
158 sample->set_source( source );
159 update_sample_config( sample );
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");
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.");
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.
180 sample->no_valid_source();
181 _smgr->release_source( sample->get_source() );
183 update_sample_config( sample );
186 } else if ( sample->is_valid_source() ) {
187 // check if the sound has stopped by itself
189 unsigned int source = sample->get_source();
192 alGetSourcei( source, AL_SOURCE_STATE, &result );
193 if ( result == AL_STOPPED ) {
194 // sample is stoped because it wasn't looping
196 sample->no_valid_source();
197 _smgr->release_source( source );
198 _smgr->release_buffer( sample );
201 testForALError("update");
205 // add a sound effect, return true if successful
206 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
208 sample_map_iterator sample_it = _samples.find( refname );
209 if ( sample_it != _samples.end() ) {
210 // sample name already exists
214 _samples[refname] = sound;
219 // remove a sound effect, return true if successful
220 bool SGSampleGroup::remove( const string &refname ) {
222 sample_map_iterator sample_it = _samples.find( refname );
223 if ( sample_it == _samples.end() ) {
224 // sample was not found
228 if ( sample_it->second->is_valid_buffer() )
229 _removed_samples.push_back( sample_it->second );
230 _samples.erase( sample_it );
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
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
257 return sample_it->second;
261 // stop playing all associated samples
263 SGSampleGroup::suspend ()
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;
271 if ( sample->is_valid_source() && sample->is_playing() ) {
272 alSourcePause( sample->get_source() );
275 testForALError("suspend");
278 // resume playing all associated samples
280 SGSampleGroup::resume ()
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;
287 if ( sample->is_valid_source() && sample->is_playing() ) {
288 alSourcePlay( sample->get_source() );
291 testForALError("resume");
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 );
300 if ( sample == NULL ) {
304 sample->play( looping );
309 // return true of the specified sound is currently being played
310 bool SGSampleGroup::is_playing( const string& refname ) {
311 SGSoundSample *sample = find( refname );
313 if ( sample == NULL ) {
317 return ( sample->is_playing() ) ? true : false;
320 // immediate stop playing the sound
321 bool SGSampleGroup::stop( const string& refname ) {
322 SGSoundSample *sample = find( refname );
324 if ( sample == NULL ) {
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]) )
336 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
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 );
351 // set the source position of all managed sounds
352 void SGSampleGroup::set_position( const SGGeod& pos ) {
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 );
364 // set the source orientation of all managed sounds
365 void SGSampleGroup::set_orientation( const SGQuatd& ori ) {
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 );
378 void SGSampleGroup::set_volume( float vol )
381 if (_volume < 0.0) _volume = 0.0;
382 if (_volume > 1.0) _volume = 1.0;
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 );
392 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
393 if ( sample->is_valid_source() ) {
394 unsigned int source = sample->get_source();
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() );
401 alSourcefv( source, AL_POSITION, sample->get_position() );
402 alSourcefv( source, AL_DIRECTION, sample->get_orientation() );
403 alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
405 testForALError("position and orientation");
407 alSourcef( source, AL_PITCH, sample->get_pitch() );
408 alSourcef( source, AL_GAIN, sample->get_volume() );
409 testForALError("pitch and gain");
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");
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");
425 bool SGSampleGroup::testForError(void *p, string s)
428 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
434 bool SGSampleGroup::testForALError(string s)
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);