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 alDeleteBuffers( 1, &buffer );
117 testForALError("buffer remove");
119 _removed_samples.erase( _removed_samples.begin()+i );
126 // Update the position and orientation information for all samples.
127 if ( _changed || _smgr->has_changed() ) {
128 update_pos_and_orientation();
132 sample_map_iterator sample_current = _samples.begin();
133 sample_map_iterator sample_end = _samples.end();
134 for ( ; sample_current != sample_end; ++sample_current ) {
135 SGSoundSample *sample = sample_current->second;
137 if ( !sample->is_valid_source() && sample->is_playing() ) {
139 // a request to start playing a sound has been filed.
141 ALuint source = _smgr->request_source();
142 if (alIsSource(source) == AL_TRUE )
144 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
145 if ( sample->is_queue() )
147 sample->set_source( source );
148 update_sample_config( sample );
150 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
151 alSourcei( source, AL_LOOPING, looping );
152 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
153 alSourcePlay( source );
154 testForALError("sample play");
158 if (_smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER)
161 // start playing the sample
162 ALuint buffer = sample->get_buffer();
163 if ( alIsBuffer(buffer) == AL_TRUE )
165 alSourcei( source, AL_BUFFER, buffer );
166 testForALError("assign buffer to source");
168 sample->set_source( source );
169 update_sample_config( sample );
171 alSourcei( source, AL_LOOPING, looping );
172 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
173 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
174 alSourcePlay( source );
175 testForALError("sample play");
177 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
181 } else if ( sample->is_valid_source() ) {
182 // check if the sound has stopped by itself
184 unsigned int source = sample->get_source();
187 alGetSourcei( source, AL_SOURCE_STATE, &result );
188 if ( result == AL_STOPPED ) {
189 // sample is stoped because it wasn't looping
191 sample->no_valid_source();
192 _smgr->release_source( source );
193 _smgr->release_buffer( sample );
194 remove( sample->get_sample_name() );
197 if ( sample->has_changed() ) {
198 if ( !sample->is_playing() ) {
199 // a request to stop playing the sound has been filed.
201 sample->no_valid_source();
202 _smgr->release_source( sample->get_source() );
203 } else if ( _smgr->has_changed() ) {
204 update_sample_config( sample );
209 testForALError("update");
213 // add a sound effect, return true if successful
214 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
216 sample_map_iterator sample_it = _samples.find( refname );
217 if ( sample_it != _samples.end() ) {
218 // sample name already exists
222 _samples[refname] = sound;
227 // remove a sound effect, return true if successful
228 bool SGSampleGroup::remove( const string &refname ) {
230 sample_map_iterator sample_it = _samples.find( refname );
231 if ( sample_it == _samples.end() ) {
232 // sample was not found
236 if ( sample_it->second->is_valid_buffer() )
237 _removed_samples.push_back( sample_it->second );
239 _samples.erase( sample_it );
245 // return true of the specified sound exists in the sound manager system
246 bool SGSampleGroup::exists( const string &refname ) {
247 sample_map_iterator sample_it = _samples.find( refname );
248 if ( sample_it == _samples.end() ) {
249 // sample was not found
257 // return a pointer to the SGSoundSample if the specified sound exists
258 // in the sound manager system, otherwise return NULL
259 SGSoundSample *SGSampleGroup::find( const string &refname ) {
260 sample_map_iterator sample_it = _samples.find( refname );
261 if ( sample_it == _samples.end() ) {
262 // sample was not found
266 return sample_it->second;
271 SGSampleGroup::stop ()
274 sample_map_iterator sample_current = _samples.begin();
275 sample_map_iterator sample_end = _samples.end();
276 for ( ; sample_current != sample_end; ++sample_current ) {
277 SGSoundSample *sample = sample_current->second;
279 if ( sample->is_valid_source() ) {
280 ALint source = sample->get_source();
281 if ( sample->is_playing() ) {
282 alSourceStop( source );
284 _smgr->release_source( source );
285 sample->no_valid_source();
288 if ( sample->is_valid_buffer() ) {
289 _smgr->release_buffer( sample );
290 sample->no_valid_buffer();
293 testForALError("stop");
296 // stop playing all associated samples
298 SGSampleGroup::suspend ()
300 if (_active && _pause == false) {
302 sample_map_iterator sample_current = _samples.begin();
303 sample_map_iterator sample_end = _samples.end();
304 for ( ; sample_current != sample_end; ++sample_current ) {
305 SGSoundSample *sample = sample_current->second;
307 if ( sample->is_valid_source() && sample->is_playing() ) {
308 alSourcePause( sample->get_source() );
311 testForALError("suspend");
315 // resume playing all associated samples
317 SGSampleGroup::resume ()
319 if (_active && _pause == true) {
320 sample_map_iterator sample_current = _samples.begin();
321 sample_map_iterator sample_end = _samples.end();
322 for ( ; sample_current != sample_end; ++sample_current ) {
323 SGSoundSample *sample = sample_current->second;
325 if ( sample->is_valid_source() && sample->is_playing() ) {
326 alSourcePlay( sample->get_source() );
329 testForALError("resume");
335 // tell the scheduler to play the indexed sample in a continuous loop
336 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
337 SGSoundSample *sample = find( refname );
339 if ( sample == NULL ) {
343 sample->play( looping );
348 // return true of the specified sound is currently being played
349 bool SGSampleGroup::is_playing( const string& refname ) {
350 SGSoundSample *sample = find( refname );
352 if ( sample == NULL ) {
356 return ( sample->is_playing() ) ? true : false;
359 // immediate stop playing the sound
360 bool SGSampleGroup::stop( const string& refname ) {
361 SGSoundSample *sample = find( refname );
363 if ( sample == NULL ) {
371 void SGSampleGroup::set_volume( float vol )
373 if (vol > _volume*1.01 || vol < _volume*0.99) {
375 if (_volume < 0.0) _volume = 0.0;
376 if (_volume > 1.0) _volume = 1.0;
381 // set the source position and orientation of all managed sounds
382 void SGSampleGroup::update_pos_and_orientation() {
384 SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
385 SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
386 SGQuatd ec2body = hlOr*_orientation;
388 SGVec3f velocity = SGVec3f::zeros();
389 if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
390 velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
393 sample_map_iterator sample_current = _samples.begin();
394 sample_map_iterator sample_end = _samples.end();
395 for ( ; sample_current != sample_end; ++sample_current ) {
396 SGSoundSample *sample = sample_current->second;
397 sample->set_master_volume( _volume );
398 sample->set_orientation( _orientation );
399 sample->set_rotation( ec2body );
400 sample->set_position( position );
401 sample->set_velocity( velocity );
405 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
406 SGVec3f orientation, velocity;
409 if ( _tied_to_listener ) {
410 orientation = _smgr->get_direction();
411 position = SGVec3d::zeros();
412 velocity = _smgr->get_velocity();
414 sample->update_pos_and_orientation();
415 orientation = sample->get_orientation();
416 position = sample->get_position();
417 velocity = sample->get_velocity();
420 if (_smgr->bad_doppler_effect()) {
425 if (length(position) > 20000)
426 printf("%s source and listener distance greater than 20km!\n",
428 if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
429 if (isNaN(orientation.data())) printf("NaN in source orientation\n");
430 if (isNaN(velocity.data())) printf("NaN in source velocity\n");
433 unsigned int source = sample->get_source();
434 alSourcefv( source, AL_POSITION, toVec3f(position).data() );
435 alSourcefv( source, AL_VELOCITY, velocity.data() );
436 alSourcefv( source, AL_DIRECTION, orientation.data() );
437 testForALError("position and orientation");
439 alSourcef( source, AL_PITCH, sample->get_pitch() );
440 alSourcef( source, AL_GAIN, sample->get_volume() );
441 testForALError("pitch and gain");
443 if ( sample->has_static_data_changed() ) {
444 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
445 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
446 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
447 testForALError("audio cone");
449 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
450 alSourcef( source, AL_REFERENCE_DISTANCE,
451 sample->get_reference_dist() );
452 testForALError("distance rolloff");
456 bool SGSampleGroup::testForError(void *p, string s)
459 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
465 bool SGSampleGroup::testForALError(string s)
467 ALenum error = alGetError();
468 if (error != AL_NO_ERROR) {
469 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
470 << alGetString(error) << " at " << s);