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 ) {
106 ALuint buffer = sample->get_buffer();
107 alDeleteBuffers( 1, &buffer );
108 testForALError("buffer remove");
109 _removed_samples.erase( _removed_samples.begin()+i );
116 // Update the position and orientation information for all samples.
117 if ( _changed || _smgr->has_changed() ) {
118 update_pos_and_orientation();
122 sample_map_iterator sample_current = _samples.begin();
123 sample_map_iterator sample_end = _samples.end();
124 for ( ; sample_current != sample_end; ++sample_current ) {
125 SGSoundSample *sample = sample_current->second;
127 if ( !sample->is_valid_source() && sample->is_playing() ) {
129 // a request to start playing a sound has been filed.
131 if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
134 // start playing the sample
135 ALuint buffer = sample->get_buffer();
136 ALuint source = _smgr->request_source();
137 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
139 sample->set_source( source );
141 alSourcei( source, AL_BUFFER, buffer );
142 testForALError("assign buffer to source");
144 sample->set_source( source );
145 update_sample_config( sample );
147 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
148 alSourcei( source, AL_LOOPING, looping );
149 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
150 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
151 alSourcePlay( source );
152 testForALError("sample play");
154 if (alIsBuffer(buffer) == AL_FALSE)
155 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
156 // sample->no_valid_source();
157 // sadly, no free source available at this time
160 } else if ( sample->is_valid_source() && sample->has_changed() ) {
161 if ( !sample->is_playing() ) {
162 // a request to stop playing the sound has been filed.
165 sample->no_valid_source();
166 _smgr->release_source( sample->get_source() );
167 } else if ( _smgr->has_changed() ) {
168 update_sample_config( sample );
171 } else if ( sample->is_valid_source() ) {
172 // check if the sound has stopped by itself
174 unsigned int source = sample->get_source();
177 alGetSourcei( source, AL_SOURCE_STATE, &result );
178 if ( result == AL_STOPPED ) {
179 // sample is stoped because it wasn't looping
181 sample->no_valid_source();
182 _smgr->release_source( source );
183 _smgr->release_buffer( sample );
184 remove( sample->get_sample_name() );
187 testForALError("update");
191 // add a sound effect, return true if successful
192 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
194 sample_map_iterator sample_it = _samples.find( refname );
195 if ( sample_it != _samples.end() ) {
196 // sample name already exists
200 _samples[refname] = sound;
205 // remove a sound effect, return true if successful
206 bool SGSampleGroup::remove( const string &refname ) {
208 sample_map_iterator sample_it = _samples.find( refname );
209 if ( sample_it == _samples.end() ) {
210 // sample was not found
214 if ( sample_it->second->is_valid_buffer() )
215 _removed_samples.push_back( sample_it->second );
216 _samples.erase( sample_it );
222 // return true of the specified sound exists in the sound manager system
223 bool SGSampleGroup::exists( const string &refname ) {
224 sample_map_iterator sample_it = _samples.find( refname );
225 if ( sample_it == _samples.end() ) {
226 // sample was not found
234 // return a pointer to the SGSoundSample if the specified sound exists
235 // in the sound manager system, otherwise return NULL
236 SGSoundSample *SGSampleGroup::find( const string &refname ) {
237 sample_map_iterator sample_it = _samples.find( refname );
238 if ( sample_it == _samples.end() ) {
239 // sample was not found
243 return sample_it->second;
248 SGSampleGroup::stop ()
251 sample_map_iterator sample_current = _samples.begin();
252 sample_map_iterator sample_end = _samples.end();
253 for ( ; sample_current != sample_end; ++sample_current ) {
254 SGSoundSample *sample = sample_current->second;
256 if ( sample->is_valid_source() ) {
257 ALint source = sample->get_source();
258 if ( sample->is_playing() ) {
259 alSourceStop( source );
260 alSourcei( source, AL_BUFFER, 0 );
262 _smgr->release_source( source );
263 sample->no_valid_source();
266 if (sample->is_valid_buffer() ) {
267 _smgr->release_buffer( sample );
268 sample->no_valid_buffer();
271 testForALError("stop");
274 // stop playing all associated samples
276 SGSampleGroup::suspend ()
278 if (_active && _pause == false) {
280 sample_map_iterator sample_current = _samples.begin();
281 sample_map_iterator sample_end = _samples.end();
282 for ( ; sample_current != sample_end; ++sample_current ) {
283 SGSoundSample *sample = sample_current->second;
285 if ( sample->is_valid_source() && sample->is_playing() ) {
286 alSourcePause( sample->get_source() );
289 testForALError("suspend");
293 // resume playing all associated samples
295 SGSampleGroup::resume ()
297 if (_active && _pause == true) {
298 sample_map_iterator sample_current = _samples.begin();
299 sample_map_iterator sample_end = _samples.end();
300 for ( ; sample_current != sample_end; ++sample_current ) {
301 SGSoundSample *sample = sample_current->second;
303 if ( sample->is_valid_source() && sample->is_playing() ) {
304 alSourcePlay( sample->get_source() );
307 testForALError("resume");
313 // tell the scheduler to play the indexed sample in a continuous loop
314 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
315 SGSoundSample *sample = find( refname );
317 if ( sample == NULL ) {
321 sample->play( looping );
326 // return true of the specified sound is currently being played
327 bool SGSampleGroup::is_playing( const string& refname ) {
328 SGSoundSample *sample = find( refname );
330 if ( sample == NULL ) {
334 return ( sample->is_playing() ) ? true : false;
337 // immediate stop playing the sound
338 bool SGSampleGroup::stop( const string& refname ) {
339 SGSoundSample *sample = find( refname );
341 if ( sample == NULL ) {
349 void SGSampleGroup::set_volume( float vol )
351 if (vol > _volume*1.01 || vol < _volume*0.99) {
353 if (_volume < 0.0) _volume = 0.0;
354 if (_volume > 1.0) _volume = 1.0;
359 // set the source position and orientation of all managed sounds
360 void SGSampleGroup::update_pos_and_orientation() {
362 SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
363 SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
364 SGQuatd ec2body = hlOr*_orientation;
366 SGVec3f velocity = SGVec3f::zeros();
367 if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
368 velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
371 sample_map_iterator sample_current = _samples.begin();
372 sample_map_iterator sample_end = _samples.end();
373 for ( ; sample_current != sample_end; ++sample_current ) {
374 SGSoundSample *sample = sample_current->second;
375 sample->set_master_volume( _volume );
376 sample->set_orientation( _orientation );
377 sample->set_rotation( ec2body );
378 sample->set_position( position );
379 sample->set_velocity( velocity );
383 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
384 SGVec3f orientation, velocity;
387 if ( _tied_to_listener ) {
388 orientation = _smgr->get_direction();
389 position = SGVec3d::zeros();
390 velocity = _smgr->get_velocity();
392 sample->update_pos_and_orientation();
393 orientation = sample->get_orientation();
394 position = sample->get_position();
395 velocity = sample->get_velocity();
398 if (_smgr->bad_doppler_effect()) {
403 if (length(position) > 20000)
404 printf("%s source and listener distance greater than 20km!\n",
406 if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
407 if (isNaN(orientation.data())) printf("NaN in source orientation\n");
408 if (isNaN(velocity.data())) printf("NaN in source velocity\n");
411 unsigned int source = sample->get_source();
412 alSourcefv( source, AL_POSITION, toVec3f(position).data() );
413 alSourcefv( source, AL_VELOCITY, velocity.data() );
414 alSourcefv( source, AL_DIRECTION, orientation.data() );
415 testForALError("position and orientation");
417 alSourcef( source, AL_PITCH, sample->get_pitch() );
418 alSourcef( source, AL_GAIN, sample->get_volume() );
419 testForALError("pitch and gain");
421 if ( sample->has_static_data_changed() ) {
422 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
423 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
424 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
425 testForALError("audio cone");
427 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
428 alSourcef( source, AL_REFERENCE_DISTANCE,
429 sample->get_reference_dist() );
430 testForALError("distance rolloff");
434 bool SGSampleGroup::testForError(void *p, string s)
437 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
443 bool SGSampleGroup::testForALError(string s)
445 ALenum error = alGetError();
446 if (error != AL_NO_ERROR) {
447 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
448 << alGetString(error) << " at " << s);