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 ALuint buffer = sample->get_buffer();
108 alDeleteBuffers( 1, &buffer );
109 testForALError("buffer remove");
110 _removed_samples.erase( _removed_samples.begin()+i );
117 // Update the position and orientation information for all samples.
118 if ( _changed || _smgr->has_changed() ) {
119 update_pos_and_orientation();
123 sample_map_iterator sample_current = _samples.begin();
124 sample_map_iterator sample_end = _samples.end();
125 for ( ; sample_current != sample_end; ++sample_current ) {
126 SGSoundSample *sample = sample_current->second;
128 if ( !sample->is_valid_source() && sample->is_playing() ) {
130 // a request to start playing a sound has been filed.
132 if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
135 // start playing the sample
136 ALuint buffer = sample->get_buffer();
137 ALuint source = _smgr->request_source();
138 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
140 sample->set_source( source );
142 alSourcei( source, AL_BUFFER, buffer );
143 testForALError("assign buffer to source");
145 sample->set_source( source );
146 update_sample_config( sample );
148 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
149 alSourcei( source, AL_LOOPING, looping );
150 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
151 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
152 alSourcePlay( source );
153 testForALError("sample play");
155 if (alIsBuffer(buffer) == AL_FALSE)
156 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
157 // sample->no_valid_source();
158 // sadly, no free source available at this time
161 } else if ( sample->is_valid_source() ) {
162 // check if the sound has stopped by itself
164 unsigned int source = sample->get_source();
167 alGetSourcei( source, AL_SOURCE_STATE, &result );
168 if ( result == AL_STOPPED ) {
169 // sample is stoped because it wasn't looping
171 sample->no_valid_source();
172 _smgr->release_source( source );
173 _smgr->release_buffer( sample );
174 remove( sample->get_sample_name() );
177 if ( sample->has_changed() ) {
178 if ( !sample->is_playing() ) {
179 // a request to stop playing the sound has been filed.
181 sample->no_valid_source();
182 _smgr->release_source( sample->get_source() );
183 } else if ( _smgr->has_changed() ) {
184 update_sample_config( sample );
189 testForALError("update");
193 // add a sound effect, return true if successful
194 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
196 sample_map_iterator sample_it = _samples.find( refname );
197 if ( sample_it != _samples.end() ) {
198 // sample name already exists
202 _samples[refname] = sound;
207 // remove a sound effect, return true if successful
208 bool SGSampleGroup::remove( const string &refname ) {
210 sample_map_iterator sample_it = _samples.find( refname );
211 if ( sample_it == _samples.end() ) {
212 // sample was not found
216 if ( sample_it->second->is_valid_buffer() )
217 _removed_samples.push_back( sample_it->second );
218 _samples.erase( sample_it );
224 // return true of the specified sound exists in the sound manager system
225 bool SGSampleGroup::exists( const string &refname ) {
226 sample_map_iterator sample_it = _samples.find( refname );
227 if ( sample_it == _samples.end() ) {
228 // sample was not found
236 // return a pointer to the SGSoundSample if the specified sound exists
237 // in the sound manager system, otherwise return NULL
238 SGSoundSample *SGSampleGroup::find( const string &refname ) {
239 sample_map_iterator sample_it = _samples.find( refname );
240 if ( sample_it == _samples.end() ) {
241 // sample was not found
245 return sample_it->second;
250 SGSampleGroup::stop ()
253 sample_map_iterator sample_current = _samples.begin();
254 sample_map_iterator sample_end = _samples.end();
255 for ( ; sample_current != sample_end; ++sample_current ) {
256 SGSoundSample *sample = sample_current->second;
258 if ( sample->is_valid_source() ) {
259 ALint source = sample->get_source();
260 if ( sample->is_playing() ) {
261 alSourceStop( source );
262 alSourcei( source, AL_BUFFER, 0 );
264 _smgr->release_source( source );
265 sample->no_valid_source();
268 if (sample->is_valid_buffer() ) {
269 _smgr->release_buffer( sample );
270 sample->no_valid_buffer();
273 testForALError("stop");
276 // stop playing all associated samples
278 SGSampleGroup::suspend ()
280 if (_active && _pause == false) {
282 sample_map_iterator sample_current = _samples.begin();
283 sample_map_iterator sample_end = _samples.end();
284 for ( ; sample_current != sample_end; ++sample_current ) {
285 SGSoundSample *sample = sample_current->second;
287 if ( sample->is_valid_source() && sample->is_playing() ) {
288 alSourcePause( sample->get_source() );
291 testForALError("suspend");
295 // resume playing all associated samples
297 SGSampleGroup::resume ()
299 if (_active && _pause == true) {
300 sample_map_iterator sample_current = _samples.begin();
301 sample_map_iterator sample_end = _samples.end();
302 for ( ; sample_current != sample_end; ++sample_current ) {
303 SGSoundSample *sample = sample_current->second;
305 if ( sample->is_valid_source() && sample->is_playing() ) {
306 alSourcePlay( sample->get_source() );
309 testForALError("resume");
315 // tell the scheduler to play the indexed sample in a continuous loop
316 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
317 SGSoundSample *sample = find( refname );
319 if ( sample == NULL ) {
323 sample->play( looping );
328 // return true of the specified sound is currently being played
329 bool SGSampleGroup::is_playing( const string& refname ) {
330 SGSoundSample *sample = find( refname );
332 if ( sample == NULL ) {
336 return ( sample->is_playing() ) ? true : false;
339 // immediate stop playing the sound
340 bool SGSampleGroup::stop( const string& refname ) {
341 SGSoundSample *sample = find( refname );
343 if ( sample == NULL ) {
351 void SGSampleGroup::set_volume( float vol )
353 if (vol > _volume*1.01 || vol < _volume*0.99) {
355 if (_volume < 0.0) _volume = 0.0;
356 if (_volume > 1.0) _volume = 1.0;
361 // set the source position and orientation of all managed sounds
362 void SGSampleGroup::update_pos_and_orientation() {
364 SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
365 SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
366 SGQuatd ec2body = hlOr*_orientation;
368 SGVec3f velocity = SGVec3f::zeros();
369 if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
370 velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
373 sample_map_iterator sample_current = _samples.begin();
374 sample_map_iterator sample_end = _samples.end();
375 for ( ; sample_current != sample_end; ++sample_current ) {
376 SGSoundSample *sample = sample_current->second;
377 sample->set_master_volume( _volume );
378 sample->set_orientation( _orientation );
379 sample->set_rotation( ec2body );
380 sample->set_position( position );
381 sample->set_velocity( velocity );
385 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
386 SGVec3f orientation, velocity;
389 if ( _tied_to_listener ) {
390 orientation = _smgr->get_direction();
391 position = SGVec3d::zeros();
392 velocity = _smgr->get_velocity();
394 sample->update_pos_and_orientation();
395 orientation = sample->get_orientation();
396 position = sample->get_position();
397 velocity = sample->get_velocity();
400 if (_smgr->bad_doppler_effect()) {
405 if (length(position) > 20000)
406 printf("%s source and listener distance greater than 20km!\n",
408 if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
409 if (isNaN(orientation.data())) printf("NaN in source orientation\n");
410 if (isNaN(velocity.data())) printf("NaN in source velocity\n");
413 unsigned int source = sample->get_source();
414 alSourcefv( source, AL_POSITION, toVec3f(position).data() );
415 alSourcefv( source, AL_VELOCITY, velocity.data() );
416 alSourcefv( source, AL_DIRECTION, orientation.data() );
417 testForALError("position and orientation");
419 alSourcef( source, AL_PITCH, sample->get_pitch() );
420 alSourcef( source, AL_GAIN, sample->get_volume() );
421 testForALError("pitch and gain");
423 if ( sample->has_static_data_changed() ) {
424 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
425 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
426 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
427 testForALError("audio cone");
429 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
430 alSourcef( source, AL_REFERENCE_DISTANCE,
431 sample->get_reference_dist() );
432 testForALError("distance rolloff");
436 bool SGSampleGroup::testForError(void *p, string s)
439 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
445 bool SGSampleGroup::testForALError(string s)
447 ALenum error = alGetError();
448 if (error != AL_NO_ERROR) {
449 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
450 << alGetString(error) << " at " << s);