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"
38 bool isNaN(float *v) {
39 return (isnan(v[0]) || isnan(v[1]) || isnan(v[2]));
42 SGSampleGroup::SGSampleGroup () :
49 _tied_to_listener(false),
50 _velocity(SGVec3d::zeros()),
51 _orientation(SGQuatd::zeros())
56 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
63 _tied_to_listener(false),
64 _velocity(SGVec3d::zeros()),
65 _orientation(SGQuatd::zeros())
67 _smgr->add(this, refname);
71 SGSampleGroup::~SGSampleGroup ()
78 static bool is_sample_stopped(SGSoundSample *sample)
81 assert(sample->is_valid_source());
82 unsigned int source = sample->get_source();
84 alGetSourcei( source, AL_SOURCE_STATE, &result );
85 return (result == AL_STOPPED);
91 void SGSampleGroup::cleanup_removed_samples()
93 // Delete any OpenAL buffers that might still be in use.
94 unsigned int size = _removed_samples.size();
95 for (unsigned int i=0; i<size; ) {
96 SGSoundSample *sample = _removed_samples[i];
97 bool stopped = is_sample_stopped(sample);
99 if ( sample->is_valid_source() ) {
100 int source = sample->get_source();
102 if ( sample->is_looping() && !stopped) {
104 alSourceStop( source );
106 stopped = is_sample_stopped(sample);
110 sample->no_valid_source();
111 _smgr->release_source( source );
117 if (( !sample->is_queue() )&&
118 (sample->is_valid_buffer()))
120 _smgr->release_buffer(sample);
122 _removed_samples.erase( _removed_samples.begin()+i );
130 void SGSampleGroup::start_playing_sample(SGSoundSample *sample)
134 // a request to start playing a sound has been filed.
136 ALuint source = _smgr->request_source();
137 if (alIsSource(source) == AL_FALSE ) {
141 sample->set_source( source );
142 update_sample_config( sample );
143 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
145 if ( !sample->is_queue() )
147 ALuint buffer = _smgr->request_buffer(sample);
148 if (buffer == SGSoundMgr::FAILED_BUFFER ||
149 buffer == SGSoundMgr::NO_BUFFER)
151 _smgr->release_source(source);
155 // start playing the sample
156 buffer = sample->get_buffer();
157 if ( alIsBuffer(buffer) == AL_TRUE )
159 alSourcei( source, AL_BUFFER, buffer );
160 testForALError("assign buffer to source");
162 SG_LOG( SG_SOUND, SG_ALERT, "No such buffer!");
165 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
166 alSourcei( source, AL_LOOPING, looping );
167 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
168 alSourcePlay( source );
169 testForALError("sample play");
173 void SGSampleGroup::check_playing_sample(SGSoundSample *sample)
175 // check if the sound has stopped by itself
177 if (is_sample_stopped(sample)) {
178 // sample is stopped because it wasn't looping
180 sample->no_valid_source();
181 _smgr->release_source( sample->get_source() );
182 _smgr->release_buffer( sample );
183 remove( sample->get_sample_name() );
184 } else if ( sample->has_changed() ) {
185 if ( !sample->is_playing() ) {
186 // a request to stop playing the sound has been filed.
188 sample->no_valid_source();
189 _smgr->release_source( sample->get_source() );
190 } else if ( _smgr->has_changed() ) {
191 update_sample_config( sample );
196 void SGSampleGroup::update( double dt ) {
198 if ( !_active || _pause ) return;
200 testForALError("start of update!!\n");
202 cleanup_removed_samples();
204 // Update the position and orientation information for all samples.
205 if ( _changed || _smgr->has_changed() ) {
206 update_pos_and_orientation();
210 sample_map_iterator sample_current = _samples.begin();
211 sample_map_iterator sample_end = _samples.end();
212 for ( ; sample_current != sample_end; ++sample_current ) {
213 SGSoundSample *sample = sample_current->second;
215 if ( !sample->is_valid_source() && sample->is_playing() && !sample->test_out_of_range()) {
216 start_playing_sample(sample);
218 } else if ( sample->is_valid_source() ) {
219 check_playing_sample(sample);
221 testForALError("update");
225 // add a sound effect, return true if successful
226 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
228 sample_map_iterator sample_it = _samples.find( refname );
229 if ( sample_it != _samples.end() ) {
230 // sample name already exists
234 _samples[refname] = sound;
239 // remove a sound effect, return true if successful
240 bool SGSampleGroup::remove( const string &refname ) {
242 sample_map_iterator sample_it = _samples.find( refname );
243 if ( sample_it == _samples.end() ) {
244 // sample was not found
248 if ( sample_it->second->is_valid_buffer() )
249 _removed_samples.push_back( sample_it->second );
251 _samples.erase( sample_it );
257 // return true of the specified sound exists in the sound manager system
258 bool SGSampleGroup::exists( const string &refname ) {
259 sample_map_iterator sample_it = _samples.find( refname );
260 if ( sample_it == _samples.end() ) {
261 // sample was not found
269 // return a pointer to the SGSoundSample if the specified sound exists
270 // in the sound manager system, otherwise return NULL
271 SGSoundSample *SGSampleGroup::find( const string &refname ) {
272 sample_map_iterator sample_it = _samples.find( refname );
273 if ( sample_it == _samples.end() ) {
274 // sample was not found
278 return sample_it->second;
283 SGSampleGroup::stop ()
286 sample_map_iterator sample_current = _samples.begin();
287 sample_map_iterator sample_end = _samples.end();
288 for ( ; sample_current != sample_end; ++sample_current ) {
289 SGSoundSample *sample = sample_current->second;
291 if ( sample->is_valid_source() ) {
293 ALint source = sample->get_source();
294 if ( sample->is_playing() ) {
295 alSourceStop( source );
296 testForALError("stop");
298 _smgr->release_source( source );
300 sample->no_valid_source();
303 if ( sample->is_valid_buffer() ) {
304 _smgr->release_buffer( sample );
305 sample->no_valid_buffer();
310 // stop playing all associated samples
312 SGSampleGroup::suspend ()
314 if (_active && _pause == false) {
316 sample_map_iterator sample_current = _samples.begin();
317 sample_map_iterator sample_end = _samples.end();
318 for ( ; sample_current != sample_end; ++sample_current ) {
320 SGSoundSample *sample = sample_current->second;
321 if ( sample->is_valid_source() && sample->is_playing() ) {
322 alSourcePause( sample->get_source() );
326 testForALError("suspend");
330 // resume playing all associated samples
332 SGSampleGroup::resume ()
334 if (_active && _pause == true) {
336 sample_map_iterator sample_current = _samples.begin();
337 sample_map_iterator sample_end = _samples.end();
338 for ( ; sample_current != sample_end; ++sample_current ) {
339 SGSoundSample *sample = sample_current->second;
340 if ( sample->is_valid_source() && sample->is_playing() ) {
341 alSourcePlay( sample->get_source() );
344 testForALError("resume");
351 // tell the scheduler to play the indexed sample in a continuous loop
352 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
353 SGSoundSample *sample = find( refname );
355 if ( sample == NULL ) {
359 sample->play( looping );
364 // return true of the specified sound is currently being played
365 bool SGSampleGroup::is_playing( const string& refname ) {
366 SGSoundSample *sample = find( refname );
368 if ( sample == NULL ) {
372 return ( sample->is_playing() ) ? true : false;
375 // immediate stop playing the sound
376 bool SGSampleGroup::stop( const string& refname ) {
377 SGSoundSample *sample = find( refname );
379 if ( sample == NULL ) {
387 void SGSampleGroup::set_volume( float vol )
389 if (vol > _volume*1.01 || vol < _volume*0.99) {
391 SG_CLAMP_RANGE(_volume, 0.0f, 1.0f);
396 // set the source position and orientation of all managed sounds
397 void SGSampleGroup::update_pos_and_orientation() {
399 SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
400 SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
401 SGQuatd ec2body = hlOr*_orientation;
403 SGVec3f velocity = SGVec3f::zeros();
404 if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
405 velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
408 sample_map_iterator sample_current = _samples.begin();
409 sample_map_iterator sample_end = _samples.end();
410 for ( ; sample_current != sample_end; ++sample_current ) {
411 SGSoundSample *sample = sample_current->second;
412 sample->set_master_volume( _volume );
413 sample->set_orientation( _orientation );
414 sample->set_rotation( ec2body );
415 sample->set_position( position );
416 sample->set_velocity( velocity );
418 // Test if a sample is farther away than max distance, if so
419 // stop the sound playback and free it's source.
420 if (!_tied_to_listener) {
421 float max2 = sample->get_max_dist() * sample->get_max_dist();
422 float dist2 = position[0]*position[0]
423 + position[1]*position[1] + position[2]*position[2];
424 if ((dist2 > max2) && !sample->test_out_of_range()) {
425 sample->set_out_of_range(true);
426 } else if ((dist2 < max2) && sample->test_out_of_range()) {
427 sample->set_out_of_range(false);
433 void SGSampleGroup::update_sample_config( SGSoundSample *sample )
436 SGVec3f orientation, velocity;
439 if ( _tied_to_listener ) {
440 orientation = _smgr->get_direction();
441 position = SGVec3d::zeros();
442 velocity = _smgr->get_velocity();
444 sample->update_pos_and_orientation();
445 orientation = sample->get_orientation();
446 position = sample->get_position();
447 velocity = sample->get_velocity();
450 if (_smgr->bad_doppler_effect()) {
455 if (length(position) > 20000)
456 printf("%s source and listener distance greater than 20km!\n",
458 if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
459 if (isNaN(orientation.data())) printf("NaN in source orientation\n");
460 if (isNaN(velocity.data())) printf("NaN in source velocity\n");
463 unsigned int source = sample->get_source();
464 alSourcefv( source, AL_POSITION, toVec3f(position).data() );
465 alSourcefv( source, AL_VELOCITY, velocity.data() );
466 alSourcefv( source, AL_DIRECTION, orientation.data() );
467 testForALError("position and orientation");
469 alSourcef( source, AL_PITCH, sample->get_pitch() );
470 alSourcef( source, AL_GAIN, sample->get_volume() );
471 testForALError("pitch and gain");
473 if ( sample->has_static_data_changed() ) {
474 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
475 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
476 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
477 testForALError("audio cone");
479 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
480 alSourcef( source, AL_REFERENCE_DISTANCE,
481 sample->get_reference_dist() );
482 testForALError("distance rolloff");
487 bool SGSampleGroup::testForError(void *p, string s)
490 SG_LOG( SG_SOUND, SG_ALERT, "Error (sample group): " << s);
496 bool SGSampleGroup::testForALError(string s)
499 ALenum error = alGetError();
500 if (error != AL_NO_ERROR) {
501 SG_LOG( SG_SOUND, SG_ALERT, "AL Error (" << _refname << "): "
502 << alGetString(error) << " at " << s);