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 if ( sample->is_looping() ) {
98 sample->no_valid_source();
99 _smgr->release_source( sample->get_source() );
102 alGetSourcei( sample->get_source(), AL_SOURCE_STATE, &result );
105 if ( result == AL_STOPPED ) {
107 if ( !sample->is_queue() ) {
108 ALuint buffer = sample->get_buffer();
109 alDeleteBuffers( 1, &buffer );
110 testForALError("buffer remove");
112 _removed_samples.erase( _removed_samples.begin()+i );
119 // Update the position and orientation information for all samples.
120 if ( _changed || _smgr->has_changed() ) {
121 update_pos_and_orientation();
125 sample_map_iterator sample_current = _samples.begin();
126 sample_map_iterator sample_end = _samples.end();
127 for ( ; sample_current != sample_end; ++sample_current ) {
128 SGSoundSample *sample = sample_current->second;
130 if ( !sample->is_valid_source() && sample->is_playing() ) {
132 // a request to start playing a sound has been filed.
134 ALuint source = _smgr->request_source();
135 if (alIsSource(source) == AL_TRUE )
137 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
138 if ( sample->is_queue() )
140 sample->set_source( source );
141 update_sample_config( sample );
143 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
144 alSourcei( source, AL_LOOPING, looping );
145 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
146 alSourcePlay( source );
147 testForALError("sample play");
151 if (_smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER)
154 // start playing the sample
155 ALuint buffer = sample->get_buffer();
156 if ( alIsBuffer(buffer) == AL_TRUE )
158 alSourcei( source, AL_BUFFER, buffer );
159 testForALError("assign buffer to source");
161 sample->set_source( source );
162 update_sample_config( sample );
164 alSourcei( source, AL_LOOPING, looping );
165 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
166 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
167 alSourcePlay( source );
168 testForALError("sample play");
170 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
174 } else if ( sample->is_valid_source() ) {
175 // check if the sound has stopped by itself
177 unsigned int source = sample->get_source();
180 alGetSourcei( source, AL_SOURCE_STATE, &result );
181 if ( result == AL_STOPPED ) {
182 // sample is stoped because it wasn't looping
184 sample->no_valid_source();
185 _smgr->release_source( source );
186 _smgr->release_buffer( sample );
187 remove( sample->get_sample_name() );
190 if ( sample->has_changed() ) {
191 if ( !sample->is_playing() ) {
192 // a request to stop playing the sound has been filed.
194 sample->no_valid_source();
195 _smgr->release_source( sample->get_source() );
196 } else if ( _smgr->has_changed() ) {
197 update_sample_config( sample );
202 testForALError("update");
206 // add a sound effect, return true if successful
207 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
209 sample_map_iterator sample_it = _samples.find( refname );
210 if ( sample_it != _samples.end() ) {
211 // sample name already exists
215 _samples[refname] = sound;
220 // remove a sound effect, return true if successful
221 bool SGSampleGroup::remove( const string &refname ) {
223 sample_map_iterator sample_it = _samples.find( refname );
224 if ( sample_it == _samples.end() ) {
225 // sample was not found
229 if ( sample_it->second->is_valid_buffer() )
230 _removed_samples.push_back( sample_it->second );
232 _samples.erase( sample_it );
238 // return true of the specified sound exists in the sound manager system
239 bool SGSampleGroup::exists( const string &refname ) {
240 sample_map_iterator sample_it = _samples.find( refname );
241 if ( sample_it == _samples.end() ) {
242 // sample was not found
250 // return a pointer to the SGSoundSample if the specified sound exists
251 // in the sound manager system, otherwise return NULL
252 SGSoundSample *SGSampleGroup::find( const string &refname ) {
253 sample_map_iterator sample_it = _samples.find( refname );
254 if ( sample_it == _samples.end() ) {
255 // sample was not found
259 return sample_it->second;
264 SGSampleGroup::stop ()
267 sample_map_iterator sample_current = _samples.begin();
268 sample_map_iterator sample_end = _samples.end();
269 for ( ; sample_current != sample_end; ++sample_current ) {
270 SGSoundSample *sample = sample_current->second;
272 if ( sample->is_valid_source() ) {
273 ALint source = sample->get_source();
274 if ( sample->is_playing() ) {
275 alSourceStop( source );
277 _smgr->release_source( source );
278 sample->no_valid_source();
281 if ( sample->is_valid_buffer() ) {
282 _smgr->release_buffer( sample );
283 sample->no_valid_buffer();
286 testForALError("stop");
289 // stop playing all associated samples
291 SGSampleGroup::suspend ()
293 if (_active && _pause == false) {
295 sample_map_iterator sample_current = _samples.begin();
296 sample_map_iterator sample_end = _samples.end();
297 for ( ; sample_current != sample_end; ++sample_current ) {
298 SGSoundSample *sample = sample_current->second;
300 if ( sample->is_valid_source() && sample->is_playing() ) {
301 alSourcePause( sample->get_source() );
304 testForALError("suspend");
308 // resume playing all associated samples
310 SGSampleGroup::resume ()
312 if (_active && _pause == true) {
313 sample_map_iterator sample_current = _samples.begin();
314 sample_map_iterator sample_end = _samples.end();
315 for ( ; sample_current != sample_end; ++sample_current ) {
316 SGSoundSample *sample = sample_current->second;
318 if ( sample->is_valid_source() && sample->is_playing() ) {
319 alSourcePlay( sample->get_source() );
322 testForALError("resume");
328 // tell the scheduler to play the indexed sample in a continuous loop
329 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
330 SGSoundSample *sample = find( refname );
332 if ( sample == NULL ) {
336 sample->play( looping );
341 // return true of the specified sound is currently being played
342 bool SGSampleGroup::is_playing( const string& refname ) {
343 SGSoundSample *sample = find( refname );
345 if ( sample == NULL ) {
349 return ( sample->is_playing() ) ? true : false;
352 // immediate stop playing the sound
353 bool SGSampleGroup::stop( const string& refname ) {
354 SGSoundSample *sample = find( refname );
356 if ( sample == NULL ) {
364 void SGSampleGroup::set_volume( float vol )
366 if (vol > _volume*1.01 || vol < _volume*0.99) {
368 if (_volume < 0.0) _volume = 0.0;
369 if (_volume > 1.0) _volume = 1.0;
374 // set the source position and orientation of all managed sounds
375 void SGSampleGroup::update_pos_and_orientation() {
377 SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
378 SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
379 SGQuatd ec2body = hlOr*_orientation;
381 SGVec3f velocity = SGVec3f::zeros();
382 if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
383 velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
386 sample_map_iterator sample_current = _samples.begin();
387 sample_map_iterator sample_end = _samples.end();
388 for ( ; sample_current != sample_end; ++sample_current ) {
389 SGSoundSample *sample = sample_current->second;
390 sample->set_master_volume( _volume );
391 sample->set_orientation( _orientation );
392 sample->set_rotation( ec2body );
393 sample->set_position( position );
394 sample->set_velocity( velocity );
398 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
399 SGVec3f orientation, velocity;
402 if ( _tied_to_listener ) {
403 orientation = _smgr->get_direction();
404 position = SGVec3d::zeros();
405 velocity = _smgr->get_velocity();
407 sample->update_pos_and_orientation();
408 orientation = sample->get_orientation();
409 position = sample->get_position();
410 velocity = sample->get_velocity();
413 if (_smgr->bad_doppler_effect()) {
418 if (length(position) > 20000)
419 printf("%s source and listener distance greater than 20km!\n",
421 if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
422 if (isNaN(orientation.data())) printf("NaN in source orientation\n");
423 if (isNaN(velocity.data())) printf("NaN in source velocity\n");
426 unsigned int source = sample->get_source();
427 alSourcefv( source, AL_POSITION, toVec3f(position).data() );
428 alSourcefv( source, AL_VELOCITY, velocity.data() );
429 alSourcefv( source, AL_DIRECTION, orientation.data() );
430 testForALError("position and orientation");
432 alSourcef( source, AL_PITCH, sample->get_pitch() );
433 alSourcef( source, AL_GAIN, sample->get_volume() );
434 testForALError("pitch and gain");
436 if ( sample->has_static_data_changed() ) {
437 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
438 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
439 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
440 testForALError("audio cone");
442 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
443 alSourcef( source, AL_REFERENCE_DISTANCE,
444 sample->get_reference_dist() );
445 testForALError("distance rolloff");
449 bool SGSampleGroup::testForError(void *p, string s)
452 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
458 bool SGSampleGroup::testForALError(string s)
460 ALenum error = alGetError();
461 if (error != AL_NO_ERROR) {
462 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
463 << alGetString(error) << " at " << s);