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>
28 #include <simgear/compiler.h>
29 #include <simgear/sg_inlines.h>
30 #include <simgear/debug/logstream.hxx>
32 #include "soundmgr_openal.hxx"
33 #include "soundmgr_openal_private.hxx"
34 #include "sample_group.hxx"
40 bool isNaN(float *v) {
41 return (isnan(v[0]) || isnan(v[1]) || isnan(v[2]));
44 SGSampleGroup::SGSampleGroup () :
51 _tied_to_listener(false),
52 _velocity(SGVec3d::zeros()),
53 _orientation(SGQuatd::zeros())
58 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr,
59 const std::string &refname ):
66 _tied_to_listener(false),
67 _velocity(SGVec3d::zeros()),
68 _orientation(SGQuatd::zeros())
70 _smgr->add(this, refname);
74 SGSampleGroup::~SGSampleGroup ()
81 static bool is_sample_stopped(SGSoundSample *sample)
84 assert(sample->is_valid_source());
85 unsigned int source = sample->get_source();
87 alGetSourcei( source, AL_SOURCE_STATE, &result );
88 return (result == AL_STOPPED);
94 void SGSampleGroup::cleanup_removed_samples()
96 // Delete any OpenAL buffers that might still be in use.
97 unsigned int size = _removed_samples.size();
98 for (unsigned int i=0; i<size; ) {
99 SGSoundSample *sample = _removed_samples[i];
100 bool stopped = is_sample_stopped(sample);
102 if ( sample->is_valid_source() ) {
103 int source = sample->get_source();
105 if ( sample->is_looping() && !stopped) {
107 alSourceStop( source );
109 stopped = is_sample_stopped(sample);
113 sample->no_valid_source();
114 _smgr->release_source( source );
120 if (( !sample->is_queue() )&&
121 (sample->is_valid_buffer()))
123 _smgr->release_buffer(sample);
125 _removed_samples.erase( _removed_samples.begin()+i );
133 void SGSampleGroup::start_playing_sample(SGSoundSample *sample)
137 // a request to start playing a sound has been filed.
139 ALuint source = _smgr->request_source();
140 if (alIsSource(source) == AL_FALSE ) {
144 sample->set_source( source );
145 update_sample_config( sample );
146 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
148 if ( !sample->is_queue() )
150 ALuint buffer = _smgr->request_buffer(sample);
151 if (buffer == SGSoundMgr::FAILED_BUFFER ||
152 buffer == SGSoundMgr::NO_BUFFER)
154 _smgr->release_source(source);
158 // start playing the sample
159 buffer = sample->get_buffer();
160 if ( alIsBuffer(buffer) == AL_TRUE )
162 alSourcei( source, AL_BUFFER, buffer );
163 testForALError("assign buffer to source");
165 SG_LOG( SG_SOUND, SG_ALERT, "No such buffer!");
168 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
169 alSourcei( source, AL_LOOPING, looping );
170 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
171 alSourcePlay( source );
172 testForALError("sample play");
176 void SGSampleGroup::check_playing_sample(SGSoundSample *sample)
178 // check if the sound has stopped by itself
180 if (is_sample_stopped(sample)) {
181 // sample is stopped because it wasn't looping
183 sample->no_valid_source();
184 _smgr->release_source( sample->get_source() );
185 _smgr->release_buffer( sample );
186 remove( sample->get_sample_name() );
187 } else if ( sample->has_changed() ) {
188 if ( !sample->is_playing() ) {
189 // a request to stop playing the sound has been filed.
191 sample->no_valid_source();
192 _smgr->release_source( sample->get_source() );
193 } else if ( _smgr->has_changed() ) {
194 update_sample_config( sample );
199 void SGSampleGroup::update( double dt ) {
201 if ( !_active || _pause ) return;
203 testForALError("start of update!!\n");
205 cleanup_removed_samples();
207 // Update the position and orientation information for all samples.
208 if ( _changed || _smgr->has_changed() ) {
209 update_pos_and_orientation();
213 sample_map_iterator sample_current = _samples.begin();
214 sample_map_iterator sample_end = _samples.end();
215 for ( ; sample_current != sample_end; ++sample_current ) {
216 SGSoundSample *sample = sample_current->second;
218 if ( !sample->is_valid_source() && sample->is_playing() && !sample->test_out_of_range()) {
219 start_playing_sample(sample);
221 } else if ( sample->is_valid_source() ) {
222 check_playing_sample(sample);
224 testForALError("update");
228 // add a sound effect, return true if successful
229 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound,
230 const std::string& refname )
232 sample_map_iterator sample_it = _samples.find( refname );
233 if ( sample_it != _samples.end() ) {
234 // sample name already exists
238 _samples[refname] = sound;
243 // remove a sound effect, return true if successful
244 bool SGSampleGroup::remove( const std::string &refname ) {
246 sample_map_iterator sample_it = _samples.find( refname );
247 if ( sample_it == _samples.end() ) {
248 // sample was not found
252 if ( sample_it->second->is_valid_buffer() )
253 _removed_samples.push_back( sample_it->second );
255 _samples.erase( sample_it );
261 // return true of the specified sound exists in the sound manager system
262 bool SGSampleGroup::exists( const std::string &refname ) {
263 sample_map_iterator sample_it = _samples.find( refname );
264 if ( sample_it == _samples.end() ) {
265 // sample was not found
273 // return a pointer to the SGSoundSample if the specified sound exists
274 // in the sound manager system, otherwise return NULL
275 SGSoundSample *SGSampleGroup::find( const std::string &refname ) {
276 sample_map_iterator sample_it = _samples.find( refname );
277 if ( sample_it == _samples.end() ) {
278 // sample was not found
282 return sample_it->second;
287 SGSampleGroup::stop ()
290 sample_map_iterator sample_current = _samples.begin();
291 sample_map_iterator sample_end = _samples.end();
292 for ( ; sample_current != sample_end; ++sample_current ) {
293 SGSoundSample *sample = sample_current->second;
295 if ( sample->is_valid_source() ) {
297 ALint source = sample->get_source();
298 if ( sample->is_playing() ) {
299 alSourceStop( source );
300 testForALError("stop");
302 _smgr->release_source( source );
304 sample->no_valid_source();
307 if ( sample->is_valid_buffer() ) {
308 _smgr->release_buffer( sample );
309 sample->no_valid_buffer();
314 // stop playing all associated samples
316 SGSampleGroup::suspend ()
318 if (_active && _pause == false) {
320 sample_map_iterator sample_current = _samples.begin();
321 sample_map_iterator sample_end = _samples.end();
322 for ( ; sample_current != sample_end; ++sample_current ) {
324 SGSoundSample *sample = sample_current->second;
325 if ( sample->is_valid_source() && sample->is_playing() ) {
326 alSourcePause( sample->get_source() );
330 testForALError("suspend");
334 // resume playing all associated samples
336 SGSampleGroup::resume ()
338 if (_active && _pause == true) {
340 sample_map_iterator sample_current = _samples.begin();
341 sample_map_iterator sample_end = _samples.end();
342 for ( ; sample_current != sample_end; ++sample_current ) {
343 SGSoundSample *sample = sample_current->second;
344 if ( sample->is_valid_source() && sample->is_playing() ) {
345 alSourcePlay( sample->get_source() );
348 testForALError("resume");
355 // tell the scheduler to play the indexed sample in a continuous loop
356 bool SGSampleGroup::play( const std::string &refname,
359 SGSoundSample *sample = find( refname );
361 if ( sample == NULL ) {
365 sample->play( looping );
370 // return true of the specified sound is currently being played
371 bool SGSampleGroup::is_playing( const std::string& refname ) {
372 SGSoundSample *sample = find( refname );
374 if ( sample == NULL ) {
378 return ( sample->is_playing() ) ? true : false;
381 // immediate stop playing the sound
382 bool SGSampleGroup::stop( const std::string& refname ) {
383 SGSoundSample *sample = find( refname );
385 if ( sample == NULL ) {
393 void SGSampleGroup::set_volume( float vol )
395 if (vol > _volume*1.01 || vol < _volume*0.99) {
397 SG_CLAMP_RANGE(_volume, 0.0f, 1.0f);
402 // set the source position and orientation of all managed sounds
403 void SGSampleGroup::update_pos_and_orientation() {
405 SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
406 SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
407 SGQuatd ec2body = hlOr*_orientation;
409 SGVec3f velocity = SGVec3f::zeros();
410 if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
411 velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
414 sample_map_iterator sample_current = _samples.begin();
415 sample_map_iterator sample_end = _samples.end();
416 for ( ; sample_current != sample_end; ++sample_current ) {
417 SGSoundSample *sample = sample_current->second;
418 sample->set_master_volume( _volume );
419 sample->set_orientation( _orientation );
420 sample->set_rotation( ec2body );
421 sample->set_position( position );
422 sample->set_velocity( velocity );
424 // Test if a sample is farther away than max distance, if so
425 // stop the sound playback and free it's source.
426 if (!_tied_to_listener) {
427 float max2 = sample->get_max_dist() * sample->get_max_dist();
428 float dist2 = position[0]*position[0]
429 + position[1]*position[1] + position[2]*position[2];
430 if ((dist2 > max2) && !sample->test_out_of_range()) {
431 sample->set_out_of_range(true);
432 } else if ((dist2 < max2) && sample->test_out_of_range()) {
433 sample->set_out_of_range(false);
439 void SGSampleGroup::update_sample_config( SGSoundSample *sample )
442 SGVec3f orientation, velocity;
445 if ( _tied_to_listener ) {
446 orientation = _smgr->get_direction();
447 position = SGVec3d::zeros();
448 velocity = _smgr->get_velocity();
450 sample->update_pos_and_orientation();
451 orientation = sample->get_orientation();
452 position = sample->get_position();
453 velocity = sample->get_velocity();
456 if (_smgr->bad_doppler_effect()) {
461 if (length(position) > 20000)
462 printf("%s source and listener distance greater than 20km!\n",
464 if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
465 if (isNaN(orientation.data())) printf("NaN in source orientation\n");
466 if (isNaN(velocity.data())) printf("NaN in source velocity\n");
469 unsigned int source = sample->get_source();
470 alSourcefv( source, AL_POSITION, toVec3f(position).data() );
471 alSourcefv( source, AL_VELOCITY, velocity.data() );
472 alSourcefv( source, AL_DIRECTION, orientation.data() );
473 testForALError("position and orientation");
475 alSourcef( source, AL_PITCH, sample->get_pitch() );
476 alSourcef( source, AL_GAIN, sample->get_volume() );
477 testForALError("pitch and gain");
479 if ( sample->has_static_data_changed() ) {
480 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
481 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
482 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
483 testForALError("audio cone");
485 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
486 alSourcef( source, AL_REFERENCE_DISTANCE,
487 sample->get_reference_dist() );
488 testForALError("distance rolloff");
493 bool SGSampleGroup::testForError(void *p, std::string s)
496 SG_LOG( SG_SOUND, SG_ALERT, "Error (sample group): " << s);
502 bool SGSampleGroup::testForALError(std::string s)
505 ALenum error = alGetError();
506 if (error != AL_NO_ERROR) {
507 SG_LOG( SG_SOUND, SG_ALERT, "AL Error (" << _refname << "): "
508 << alGetString(error) << " at " << s);