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 #include "soundmgr_openal.hxx"
30 #include "sample_group.hxx"
32 bool isNaN(float *v) {
33 return (isnan(v[0]) || isnan(v[1]) || isnan(v[2]));
36 SGSampleGroup::SGSampleGroup () :
43 _tied_to_listener(false),
44 _velocity(SGVec3d::zeros()),
45 _orientation(SGQuatd::zeros())
50 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
57 _tied_to_listener(false),
58 _velocity(SGVec3d::zeros()),
59 _orientation(SGQuatd::zeros())
61 _smgr->add(this, refname);
65 SGSampleGroup::~SGSampleGroup ()
69 sample_map_iterator sample_current = _samples.begin();
70 sample_map_iterator sample_end = _samples.end();
71 for ( ; sample_current != sample_end; ++sample_current ) {
72 SGSoundSample *sample = sample_current->second;
74 if ( sample->is_valid_source() && sample->is_playing() ) {
75 sample->no_valid_source();
76 _smgr->release_source( sample->get_source() );
77 _smgr->release_buffer( sample );
84 void SGSampleGroup::update( double dt ) {
86 if ( !_active || _pause ) return;
88 testForALError("start of update!!\n");
90 // Delete any OpenAL buffers that might still be in use.
91 unsigned int size = _removed_samples.size();
92 for (unsigned int i=0; i<size; ) {
93 SGSoundSample *sample = _removed_samples[i];
94 ALint result = AL_STOPPED;
96 if ( sample->is_valid_source() ) {
97 int source = sample->get_source();
99 alGetSourcei( source, AL_SOURCE_STATE, &result );
100 if ( sample->is_looping() ) {
101 if ( result != AL_STOPPED ) {
102 alSourceStop( source );
103 alGetSourcei( source, AL_SOURCE_STATE, &result );
105 if ( result == AL_STOPPED ) {
106 sample->no_valid_source();
107 _smgr->release_source( sample->get_source() );
112 if ( result == AL_STOPPED ) {
114 if ( !sample->is_queue() ) {
115 ALuint buffer = sample->get_buffer();
116 // disconnect buffer from its source - otherwise it cannot be deleted
117 alSourceUnqueueBuffers(sample->get_source(), 1, &buffer);
118 alDeleteBuffers( 1, &buffer );
119 testForALError("buffer remove");
121 _removed_samples.erase( _removed_samples.begin()+i );
128 // Update the position and orientation information for all samples.
129 if ( _changed || _smgr->has_changed() ) {
130 update_pos_and_orientation();
134 sample_map_iterator sample_current = _samples.begin();
135 sample_map_iterator sample_end = _samples.end();
136 for ( ; sample_current != sample_end; ++sample_current ) {
137 SGSoundSample *sample = sample_current->second;
139 if ( !sample->is_valid_source() && sample->is_playing() ) {
141 // a request to start playing a sound has been filed.
143 ALuint source = _smgr->request_source();
144 if (alIsSource(source) == AL_TRUE )
146 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
147 if ( sample->is_queue() )
149 sample->set_source( source );
150 update_sample_config( sample );
152 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
153 alSourcei( source, AL_LOOPING, looping );
154 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
155 alSourcePlay( source );
156 testForALError("sample play");
160 ALuint buffer = _smgr->request_buffer(sample);
161 if (buffer == SGSoundMgr::FAILED_BUFFER ||
162 buffer == SGSoundMgr::NO_BUFFER)
164 _smgr->release_source(source);
168 // start playing the sample
169 buffer = sample->get_buffer();
170 if ( alIsBuffer(buffer) == AL_TRUE )
172 alSourcei( source, AL_BUFFER, buffer );
173 testForALError("assign buffer to source");
175 sample->set_source( source );
176 update_sample_config( sample );
178 alSourcei( source, AL_LOOPING, looping );
179 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
180 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
181 alSourcePlay( source );
182 testForALError("sample play");
184 SG_LOG( SG_SOUND, SG_ALERT, "No such buffer!\n");
188 } else if ( sample->is_valid_source() ) {
189 // check if the sound has stopped by itself
191 unsigned int source = sample->get_source();
194 alGetSourcei( source, AL_SOURCE_STATE, &result );
195 if ( result == AL_STOPPED ) {
196 // sample is stoped because it wasn't looping
198 sample->no_valid_source();
199 _smgr->release_source( source );
200 _smgr->release_buffer( sample );
201 remove( sample->get_sample_name() );
204 if ( sample->has_changed() ) {
205 if ( !sample->is_playing() ) {
206 // a request to stop playing the sound has been filed.
208 sample->no_valid_source();
209 _smgr->release_source( sample->get_source() );
210 } else if ( _smgr->has_changed() ) {
211 update_sample_config( sample );
216 testForALError("update");
220 // add a sound effect, return true if successful
221 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
223 sample_map_iterator sample_it = _samples.find( refname );
224 if ( sample_it != _samples.end() ) {
225 // sample name already exists
229 _samples[refname] = sound;
234 // remove a sound effect, return true if successful
235 bool SGSampleGroup::remove( const string &refname ) {
237 sample_map_iterator sample_it = _samples.find( refname );
238 if ( sample_it == _samples.end() ) {
239 // sample was not found
243 if ( sample_it->second->is_valid_buffer() )
244 _removed_samples.push_back( sample_it->second );
246 _samples.erase( sample_it );
252 // return true of the specified sound exists in the sound manager system
253 bool SGSampleGroup::exists( const string &refname ) {
254 sample_map_iterator sample_it = _samples.find( refname );
255 if ( sample_it == _samples.end() ) {
256 // sample was not found
264 // return a pointer to the SGSoundSample if the specified sound exists
265 // in the sound manager system, otherwise return NULL
266 SGSoundSample *SGSampleGroup::find( const string &refname ) {
267 sample_map_iterator sample_it = _samples.find( refname );
268 if ( sample_it == _samples.end() ) {
269 // sample was not found
273 return sample_it->second;
278 SGSampleGroup::stop ()
281 sample_map_iterator sample_current = _samples.begin();
282 sample_map_iterator sample_end = _samples.end();
283 for ( ; sample_current != sample_end; ++sample_current ) {
284 SGSoundSample *sample = sample_current->second;
286 if ( sample->is_valid_source() ) {
287 ALint source = sample->get_source();
288 if ( sample->is_playing() ) {
289 alSourceStop( source );
291 _smgr->release_source( source );
292 sample->no_valid_source();
295 if ( sample->is_valid_buffer() ) {
296 _smgr->release_buffer( sample );
297 sample->no_valid_buffer();
300 testForALError("stop");
303 // stop playing all associated samples
305 SGSampleGroup::suspend ()
307 if (_active && _pause == false) {
309 sample_map_iterator sample_current = _samples.begin();
310 sample_map_iterator sample_end = _samples.end();
311 for ( ; sample_current != sample_end; ++sample_current ) {
312 SGSoundSample *sample = sample_current->second;
314 if ( sample->is_valid_source() && sample->is_playing() ) {
315 alSourcePause( sample->get_source() );
318 testForALError("suspend");
322 // resume playing all associated samples
324 SGSampleGroup::resume ()
326 if (_active && _pause == true) {
327 sample_map_iterator sample_current = _samples.begin();
328 sample_map_iterator sample_end = _samples.end();
329 for ( ; sample_current != sample_end; ++sample_current ) {
330 SGSoundSample *sample = sample_current->second;
332 if ( sample->is_valid_source() && sample->is_playing() ) {
333 alSourcePlay( sample->get_source() );
336 testForALError("resume");
342 // tell the scheduler to play the indexed sample in a continuous loop
343 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
344 SGSoundSample *sample = find( refname );
346 if ( sample == NULL ) {
350 sample->play( looping );
355 // return true of the specified sound is currently being played
356 bool SGSampleGroup::is_playing( const string& refname ) {
357 SGSoundSample *sample = find( refname );
359 if ( sample == NULL ) {
363 return ( sample->is_playing() ) ? true : false;
366 // immediate stop playing the sound
367 bool SGSampleGroup::stop( const string& refname ) {
368 SGSoundSample *sample = find( refname );
370 if ( sample == NULL ) {
378 void SGSampleGroup::set_volume( float vol )
380 if (vol > _volume*1.01 || vol < _volume*0.99) {
382 if (_volume < 0.0) _volume = 0.0;
383 if (_volume > 1.0) _volume = 1.0;
388 // set the source position and orientation of all managed sounds
389 void SGSampleGroup::update_pos_and_orientation() {
391 SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
392 SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
393 SGQuatd ec2body = hlOr*_orientation;
395 SGVec3f velocity = SGVec3f::zeros();
396 if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
397 velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
400 sample_map_iterator sample_current = _samples.begin();
401 sample_map_iterator sample_end = _samples.end();
402 for ( ; sample_current != sample_end; ++sample_current ) {
403 SGSoundSample *sample = sample_current->second;
404 sample->set_master_volume( _volume );
405 sample->set_orientation( _orientation );
406 sample->set_rotation( ec2body );
407 sample->set_position( position );
408 sample->set_velocity( velocity );
410 // Test if a sample is farther away than max distance, if so
411 // stop the sound playback and free it's source.
412 if (!_tied_to_listener) {
413 float max2 = sample->get_max_dist() * sample->get_max_dist();
414 float dist2 = position[0]*position[0]
415 + position[1]*position[1] + position[2]*position[2];
416 if ((dist2 > max2) && !sample->test_out_of_range()) {
417 sample->set_out_of_range(true);
418 } else if ((dist2 < max2) && sample->test_out_of_range()) {
419 sample->set_out_of_range(false);
425 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
426 SGVec3f orientation, velocity;
429 if ( _tied_to_listener ) {
430 orientation = _smgr->get_direction();
431 position = SGVec3d::zeros();
432 velocity = _smgr->get_velocity();
434 sample->update_pos_and_orientation();
435 orientation = sample->get_orientation();
436 position = sample->get_position();
437 velocity = sample->get_velocity();
440 if (_smgr->bad_doppler_effect()) {
445 if (length(position) > 20000)
446 printf("%s source and listener distance greater than 20km!\n",
448 if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
449 if (isNaN(orientation.data())) printf("NaN in source orientation\n");
450 if (isNaN(velocity.data())) printf("NaN in source velocity\n");
453 unsigned int source = sample->get_source();
454 alSourcefv( source, AL_POSITION, toVec3f(position).data() );
455 alSourcefv( source, AL_VELOCITY, velocity.data() );
456 alSourcefv( source, AL_DIRECTION, orientation.data() );
457 testForALError("position and orientation");
459 alSourcef( source, AL_PITCH, sample->get_pitch() );
460 alSourcef( source, AL_GAIN, sample->get_volume() );
461 testForALError("pitch and gain");
463 if ( sample->has_static_data_changed() ) {
464 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
465 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
466 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
467 testForALError("audio cone");
469 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
470 alSourcef( source, AL_REFERENCE_DISTANCE,
471 sample->get_reference_dist() );
472 testForALError("distance rolloff");
476 bool SGSampleGroup::testForError(void *p, string s)
479 SG_LOG( SG_SOUND, SG_ALERT, "Error (sample group): " << s);
485 bool SGSampleGroup::testForALError(string s)
487 ALenum error = alGetError();
488 if (error != AL_NO_ERROR) {
489 SG_LOG( SG_SOUND, SG_ALERT, "AL Error (" << _refname << "): "
490 << alGetString(error) << " at " << s);