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 () :
42 _tied_to_listener(false),
43 _velocity(SGVec3d::zeros()),
44 _orientation(SGQuatd::zeros()),
45 _position(SGVec3d::zeros()),
46 _pos_offs(SGVec3d::zeros()),
47 _position_geod(SGGeod())
52 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
58 _tied_to_listener(false),
59 _velocity(SGVec3d::zeros()),
60 _orientation(SGQuatd::zeros()),
61 _position(SGVec3d::zeros()),
62 _pos_offs(SGVec3d::zeros()),
63 _position_geod(SGGeod())
65 _smgr->add(this, refname);
69 SGSampleGroup::~SGSampleGroup ()
73 sample_map_iterator sample_current = _samples.begin();
74 sample_map_iterator sample_end = _samples.end();
75 for ( ; sample_current != sample_end; ++sample_current ) {
76 SGSoundSample *sample = sample_current->second;
78 if ( sample->is_valid_source() && sample->is_playing() ) {
79 sample->no_valid_source();
80 _smgr->release_source( sample->get_source() );
81 _smgr->release_buffer( sample );
88 void SGSampleGroup::update( double dt ) {
90 if ( !_active || _pause ) return;
92 testForALError("start of update!!\n");
94 // Delete any OpenAL buffers that might still be in use.
95 unsigned int size = _removed_samples.size();
96 for (unsigned int i=0; i<size; ) {
97 SGSoundSample *sample = _removed_samples[i];
98 ALint result = AL_STOPPED;
100 if ( sample->is_valid_source() ) {
101 if ( sample->is_looping() ) {
102 sample->no_valid_source();
103 _smgr->release_source( sample->get_source() );
106 alGetSourcei( sample->get_source(), AL_SOURCE_STATE, &result );
109 if ( result == AL_STOPPED ) {
110 ALuint buffer = sample->get_buffer();
111 alDeleteBuffers( 1, &buffer );
112 testForALError("buffer remove");
113 _removed_samples.erase( _removed_samples.begin()+i );
120 // Update the position and orientation information for all samples.
122 update_pos_and_orientation();
126 sample_map_iterator sample_current = _samples.begin();
127 sample_map_iterator sample_end = _samples.end();
128 for ( ; sample_current != sample_end; ++sample_current ) {
129 SGSoundSample *sample = sample_current->second;
131 if ( !sample->is_valid_source() && sample->is_playing() ) {
133 // a request to start playing a sound has been filed.
135 if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
138 // start playing the sample
139 ALuint buffer = sample->get_buffer();
140 ALuint source = _smgr->request_source();
141 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
143 sample->set_source( source );
145 alSourcei( source, AL_BUFFER, buffer );
146 testForALError("assign buffer to source");
148 sample->set_source( source );
149 update_sample_config( sample );
151 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
152 alSourcei( source, AL_LOOPING, looping );
153 alSourcef( source, AL_ROLLOFF_FACTOR, 1.0 );
154 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
155 alSourcePlay( source );
156 testForALError("sample play");
158 if (alIsBuffer(buffer) == AL_FALSE)
159 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
160 // sample->no_valid_source();
161 // sadly, no free source available at this time
164 } else if ( sample->is_valid_source() && sample->has_changed() ) {
165 if ( !sample->is_playing() ) {
166 // a request to stop playing the sound has been filed.
169 sample->no_valid_source();
170 _smgr->release_source( sample->get_source() );
171 } else if ( _smgr->has_changed() ) {
172 update_sample_config( sample );
175 } else if ( sample->is_valid_source() ) {
176 // check if the sound has stopped by itself
178 unsigned int source = sample->get_source();
181 alGetSourcei( source, AL_SOURCE_STATE, &result );
182 if ( result == AL_STOPPED ) {
183 // sample is stoped because it wasn't looping
185 sample->no_valid_source();
186 _smgr->release_source( source );
187 _smgr->release_buffer( sample );
188 remove( sample->get_sample_name() );
191 testForALError("update");
195 // add a sound effect, return true if successful
196 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
198 sample_map_iterator sample_it = _samples.find( refname );
199 if ( sample_it != _samples.end() ) {
200 // sample name already exists
204 _samples[refname] = sound;
209 // remove a sound effect, return true if successful
210 bool SGSampleGroup::remove( const string &refname ) {
212 sample_map_iterator sample_it = _samples.find( refname );
213 if ( sample_it == _samples.end() ) {
214 // sample was not found
218 if ( sample_it->second->is_valid_buffer() )
219 _removed_samples.push_back( sample_it->second );
220 _samples.erase( sample_it );
226 // return true of the specified sound exists in the sound manager system
227 bool SGSampleGroup::exists( const string &refname ) {
228 sample_map_iterator sample_it = _samples.find( refname );
229 if ( sample_it == _samples.end() ) {
230 // sample was not found
238 // return a pointer to the SGSoundSample if the specified sound exists
239 // in the sound manager system, otherwise return NULL
240 SGSoundSample *SGSampleGroup::find( const string &refname ) {
241 sample_map_iterator sample_it = _samples.find( refname );
242 if ( sample_it == _samples.end() ) {
243 // sample was not found
247 return sample_it->second;
251 // stop playing all associated samples
253 SGSampleGroup::suspend ()
256 sample_map_iterator sample_current = _samples.begin();
257 sample_map_iterator sample_end = _samples.end();
258 for ( ; sample_current != sample_end; ++sample_current ) {
259 SGSoundSample *sample = sample_current->second;
261 if ( sample->is_valid_source() && sample->is_playing() ) {
262 alSourcePause( sample->get_source() );
265 testForALError("suspend");
268 // resume playing all associated samples
270 SGSampleGroup::resume ()
272 sample_map_iterator sample_current = _samples.begin();
273 sample_map_iterator sample_end = _samples.end();
274 for ( ; sample_current != sample_end; ++sample_current ) {
275 SGSoundSample *sample = sample_current->second;
277 if ( sample->is_valid_source() && sample->is_playing() ) {
278 alSourcePlay( sample->get_source() );
281 testForALError("resume");
286 // tell the scheduler to play the indexed sample in a continuous loop
287 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
288 SGSoundSample *sample = find( refname );
290 if ( sample == NULL ) {
294 sample->play( looping );
299 // return true of the specified sound is currently being played
300 bool SGSampleGroup::is_playing( const string& refname ) {
301 SGSoundSample *sample = find( refname );
303 if ( sample == NULL ) {
307 return ( sample->is_playing() ) ? true : false;
310 // immediate stop playing the sound
311 bool SGSampleGroup::stop( const string& refname ) {
312 SGSoundSample *sample = find( refname );
314 if ( sample == NULL ) {
322 // set source velocity of all managed sounds
323 void SGSampleGroup::set_velocity( const SGVec3f &vel ) {
324 if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) )
326 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
330 if (_velocity != vel) {
331 sample_map_iterator sample_current = _samples.begin();
332 sample_map_iterator sample_end = _samples.end();
333 for ( ; sample_current != sample_end; ++sample_current ) {
334 SGSoundSample *sample = sample_current->second;
335 sample->set_velocity( vel );
341 // set the source position of all managed sounds
342 void SGSampleGroup::update_pos_and_orientation() {
344 SGVec3d position = SGVec3d::fromGeod( _position_geod );
345 SGVec3d pos_offs = SGVec3d::fromGeod( _smgr->get_position_geod() );
347 if (_position != position || _pos_offs != pos_offs) {
348 _position = position;
349 _pos_offs = pos_offs;
351 sample_map_iterator sample_current = _samples.begin();
352 sample_map_iterator sample_end = _samples.end();
353 for ( ; sample_current != sample_end; ++sample_current ) {
354 SGSoundSample *sample = sample_current->second;
355 sample->set_position( _position );
356 sample->set_position_offset( _pos_offs );
360 // The rotation rotating from the earth centerd frame to
361 // the horizontal local frame
362 SGQuatd hlOr = SGQuatd::fromLonLat(_position_geod);
364 // Rotate the x-forward, y-right, z-down coordinate system
365 // into the OpenGL camera system with x-right, y-up, z-back.
366 SGQuatd q(-0.5, -0.5, 0.5, 0.5);
368 // Compute the sounds orientation and position
369 // wrt the earth centered frame - that is global coorinates
370 SGQuatd sc2body = hlOr*_orientation*q;
372 sample_map_iterator sample_current = _samples.begin();
373 sample_map_iterator sample_end = _samples.end();
374 for ( ; sample_current != sample_end; ++sample_current ) {
375 SGSoundSample *sample = sample_current->second;
376 sample->set_orientation( _orientation );
377 sample->set_rotation( sc2body );
381 void SGSampleGroup::set_volume( float vol )
384 if (_volume < 0.0) _volume = 0.0;
385 if (_volume > 1.0) _volume = 1.0;
387 sample_map_iterator sample_current = _samples.begin();
388 sample_map_iterator sample_end = _samples.end();
389 for ( ; sample_current != sample_end; ++sample_current ) {
390 SGSoundSample *sample = sample_current->second;
391 sample->set_master_volume( _volume );
395 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
396 SGVec3f orientation, velocity;
399 if ( _tied_to_listener ) {
400 orientation = _smgr->get_direction();
401 position = _smgr->get_position();
402 velocity = _smgr->get_velocity();
404 sample->update_pos_and_orientation();
405 orientation = sample->get_orientation();
406 position = sample->get_position();
407 velocity = sample->get_velocity();
411 if (length(position) > 20000)
412 printf("source and listener distance greater than 20km!\n");
413 if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
414 if (isNaN(orientation.data())) printf("NaN in source orientation\n");
415 if (isNaN(velocity.data())) printf("NaN in source velocity\n");
418 unsigned int source = sample->get_source();
419 alSourcefv( source, AL_POSITION, toVec3f(position).data() );
420 alSourcefv( source, AL_VELOCITY, velocity.data() );
421 alSourcefv( source, AL_DIRECTION, orientation.data() );
422 testForALError("position and orientation");
424 alSourcef( source, AL_PITCH, sample->get_pitch() );
425 alSourcef( source, AL_GAIN, sample->get_volume() );
426 testForALError("pitch and gain");
428 if ( sample->has_static_data_changed() ) {
429 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
430 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
431 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
432 testForALError("audio cone");
434 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
435 alSourcef( source, AL_REFERENCE_DISTANCE,
436 sample->get_reference_dist() );
437 testForALError("distance rolloff");
441 bool SGSampleGroup::testForError(void *p, string s)
444 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
450 bool SGSampleGroup::testForALError(string s)
452 ALenum error = alGetError();
453 if (error != AL_NO_ERROR) {
454 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
455 << alGetString(error) << " at " << s);