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>
28 #include <simgear/debug/logstream.hxx>
30 #include "soundmgr_openal.hxx"
31 #include "soundmgr_openal_private.hxx"
32 #include "sample_group.hxx"
36 bool isNaN(float *v) {
37 return (isnan(v[0]) || isnan(v[1]) || isnan(v[2]));
40 SGSampleGroup::SGSampleGroup () :
47 _tied_to_listener(false),
48 _velocity(SGVec3d::zeros()),
49 _orientation(SGQuatd::zeros())
54 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
61 _tied_to_listener(false),
62 _velocity(SGVec3d::zeros()),
63 _orientation(SGQuatd::zeros())
65 _smgr->add(this, refname);
69 SGSampleGroup::~SGSampleGroup ()
76 void SGSampleGroup::update( double dt ) {
78 if ( !_active || _pause ) return;
80 testForALError("start of update!!\n");
82 // Delete any OpenAL buffers that might still be in use.
83 unsigned int size = _removed_samples.size();
84 for (unsigned int i=0; i<size; ) {
85 SGSoundSample *sample = _removed_samples[i];
86 ALint result = AL_STOPPED;
88 if ( sample->is_valid_source() ) {
89 int source = sample->get_source();
91 alGetSourcei( source, AL_SOURCE_STATE, &result );
92 if ( sample->is_looping() ) {
93 if ( result != AL_STOPPED ) {
94 alSourceStop( source );
95 alGetSourcei( source, AL_SOURCE_STATE, &result );
98 if ( result == AL_STOPPED ) {
99 sample->no_valid_source();
100 _smgr->release_source( source );
104 if ( result == AL_STOPPED ) {
106 if (( !sample->is_queue() )&&
107 (sample->is_valid_buffer()))
109 _smgr->release_buffer(sample);
111 _removed_samples.erase( _removed_samples.begin()+i );
118 // Update the position and orientation information for all samples.
119 if ( _changed || _smgr->has_changed() ) {
120 update_pos_and_orientation();
124 sample_map_iterator sample_current = _samples.begin();
125 sample_map_iterator sample_end = _samples.end();
126 for ( ; sample_current != sample_end; ++sample_current ) {
127 SGSoundSample *sample = sample_current->second;
129 if ( !sample->is_valid_source() && sample->is_playing() && !sample->test_out_of_range()) {
131 // a request to start playing a sound has been filed.
133 ALuint source = _smgr->request_source();
134 if (alIsSource(source) == AL_TRUE )
136 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
137 if ( sample->is_queue() )
139 sample->set_source( source );
140 update_sample_config( sample );
142 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
143 alSourcei( source, AL_LOOPING, looping );
144 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
145 alSourcePlay( source );
146 testForALError("sample play");
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 sample->set_source( source );
166 update_sample_config( sample );
168 alSourcei( source, AL_LOOPING, looping );
169 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
170 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
171 alSourcePlay( source );
172 testForALError("sample play");
174 SG_LOG( SG_SOUND, SG_ALERT, "No such buffer!");
178 } else if ( sample->is_valid_source() ) {
179 // check if the sound has stopped by itself
181 unsigned int source = sample->get_source();
184 alGetSourcei( source, AL_SOURCE_STATE, &result );
185 if ( result == AL_STOPPED ) {
186 // sample is stopped because it wasn't looping
188 sample->no_valid_source();
189 _smgr->release_source( source );
190 _smgr->release_buffer( sample );
191 remove( sample->get_sample_name() );
194 if ( sample->has_changed() ) {
195 if ( !sample->is_playing() ) {
196 // a request to stop playing the sound has been filed.
198 sample->no_valid_source();
199 _smgr->release_source( sample->get_source() );
200 } else if ( _smgr->has_changed() ) {
201 update_sample_config( sample );
206 testForALError("update");
210 // add a sound effect, return true if successful
211 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
213 sample_map_iterator sample_it = _samples.find( refname );
214 if ( sample_it != _samples.end() ) {
215 // sample name already exists
219 _samples[refname] = sound;
224 // remove a sound effect, return true if successful
225 bool SGSampleGroup::remove( const string &refname ) {
227 sample_map_iterator sample_it = _samples.find( refname );
228 if ( sample_it == _samples.end() ) {
229 // sample was not found
233 if ( sample_it->second->is_valid_buffer() )
234 _removed_samples.push_back( sample_it->second );
236 _samples.erase( sample_it );
242 // return true of the specified sound exists in the sound manager system
243 bool SGSampleGroup::exists( const string &refname ) {
244 sample_map_iterator sample_it = _samples.find( refname );
245 if ( sample_it == _samples.end() ) {
246 // sample was not found
254 // return a pointer to the SGSoundSample if the specified sound exists
255 // in the sound manager system, otherwise return NULL
256 SGSoundSample *SGSampleGroup::find( const string &refname ) {
257 sample_map_iterator sample_it = _samples.find( refname );
258 if ( sample_it == _samples.end() ) {
259 // sample was not found
263 return sample_it->second;
268 SGSampleGroup::stop ()
271 sample_map_iterator sample_current = _samples.begin();
272 sample_map_iterator sample_end = _samples.end();
273 for ( ; sample_current != sample_end; ++sample_current ) {
274 SGSoundSample *sample = sample_current->second;
276 if ( sample->is_valid_source() ) {
277 ALint source = sample->get_source();
278 if ( sample->is_playing() ) {
279 alSourceStop( source );
280 testForALError("stop");
282 _smgr->release_source( source );
283 sample->no_valid_source();
286 if ( sample->is_valid_buffer() ) {
287 _smgr->release_buffer( sample );
288 sample->no_valid_buffer();
293 // stop playing all associated samples
295 SGSampleGroup::suspend ()
297 if (_active && _pause == false) {
299 sample_map_iterator sample_current = _samples.begin();
300 sample_map_iterator sample_end = _samples.end();
301 for ( ; sample_current != sample_end; ++sample_current ) {
302 SGSoundSample *sample = sample_current->second;
304 if ( sample->is_valid_source() && sample->is_playing() ) {
305 alSourcePause( sample->get_source() );
308 testForALError("suspend");
312 // resume playing all associated samples
314 SGSampleGroup::resume ()
316 if (_active && _pause == true) {
317 sample_map_iterator sample_current = _samples.begin();
318 sample_map_iterator sample_end = _samples.end();
319 for ( ; sample_current != sample_end; ++sample_current ) {
320 SGSoundSample *sample = sample_current->second;
322 if ( sample->is_valid_source() && sample->is_playing() ) {
323 alSourcePlay( sample->get_source() );
326 testForALError("resume");
332 // tell the scheduler to play the indexed sample in a continuous loop
333 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
334 SGSoundSample *sample = find( refname );
336 if ( sample == NULL ) {
340 sample->play( looping );
345 // return true of the specified sound is currently being played
346 bool SGSampleGroup::is_playing( const string& refname ) {
347 SGSoundSample *sample = find( refname );
349 if ( sample == NULL ) {
353 return ( sample->is_playing() ) ? true : false;
356 // immediate stop playing the sound
357 bool SGSampleGroup::stop( const string& refname ) {
358 SGSoundSample *sample = find( refname );
360 if ( sample == NULL ) {
368 void SGSampleGroup::set_volume( float vol )
370 if (vol > _volume*1.01 || vol < _volume*0.99) {
372 if (_volume < 0.0) _volume = 0.0;
373 if (_volume > 1.0) _volume = 1.0;
378 // set the source position and orientation of all managed sounds
379 void SGSampleGroup::update_pos_and_orientation() {
381 SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
382 SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
383 SGQuatd ec2body = hlOr*_orientation;
385 SGVec3f velocity = SGVec3f::zeros();
386 if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
387 velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
390 sample_map_iterator sample_current = _samples.begin();
391 sample_map_iterator sample_end = _samples.end();
392 for ( ; sample_current != sample_end; ++sample_current ) {
393 SGSoundSample *sample = sample_current->second;
394 sample->set_master_volume( _volume );
395 sample->set_orientation( _orientation );
396 sample->set_rotation( ec2body );
397 sample->set_position( position );
398 sample->set_velocity( velocity );
400 // Test if a sample is farther away than max distance, if so
401 // stop the sound playback and free it's source.
402 if (!_tied_to_listener) {
403 float max2 = sample->get_max_dist() * sample->get_max_dist();
404 float dist2 = position[0]*position[0]
405 + position[1]*position[1] + position[2]*position[2];
406 if ((dist2 > max2) && !sample->test_out_of_range()) {
407 sample->set_out_of_range(true);
408 } else if ((dist2 < max2) && sample->test_out_of_range()) {
409 sample->set_out_of_range(false);
415 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
416 SGVec3f orientation, velocity;
419 if ( _tied_to_listener ) {
420 orientation = _smgr->get_direction();
421 position = SGVec3d::zeros();
422 velocity = _smgr->get_velocity();
424 sample->update_pos_and_orientation();
425 orientation = sample->get_orientation();
426 position = sample->get_position();
427 velocity = sample->get_velocity();
430 if (_smgr->bad_doppler_effect()) {
435 if (length(position) > 20000)
436 printf("%s source and listener distance greater than 20km!\n",
438 if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
439 if (isNaN(orientation.data())) printf("NaN in source orientation\n");
440 if (isNaN(velocity.data())) printf("NaN in source velocity\n");
443 unsigned int source = sample->get_source();
444 alSourcefv( source, AL_POSITION, toVec3f(position).data() );
445 alSourcefv( source, AL_VELOCITY, velocity.data() );
446 alSourcefv( source, AL_DIRECTION, orientation.data() );
447 testForALError("position and orientation");
449 alSourcef( source, AL_PITCH, sample->get_pitch() );
450 alSourcef( source, AL_GAIN, sample->get_volume() );
451 testForALError("pitch and gain");
453 if ( sample->has_static_data_changed() ) {
454 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
455 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
456 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
457 testForALError("audio cone");
459 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
460 alSourcef( source, AL_REFERENCE_DISTANCE,
461 sample->get_reference_dist() );
462 testForALError("distance rolloff");
466 bool SGSampleGroup::testForError(void *p, string s)
469 SG_LOG( SG_SOUND, SG_ALERT, "Error (sample group): " << s);
475 bool SGSampleGroup::testForALError(string s)
477 ALenum error = alGetError();
478 if (error != AL_NO_ERROR) {
479 SG_LOG( SG_SOUND, SG_ALERT, "AL Error (" << _refname << "): "
480 << alGetString(error) << " at " << s);