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 () :
41 _tied_to_listener(false),
42 _velocity(SGVec3d::zeros()),
43 _orientation(SGQuatd::zeros()),
49 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
54 _tied_to_listener(false),
55 _velocity(SGVec3d::zeros()),
56 _orientation(SGQuatd::zeros()),
59 _smgr->add(this, refname);
63 SGSampleGroup::~SGSampleGroup ()
67 sample_map_iterator sample_current = _samples.begin();
68 sample_map_iterator sample_end = _samples.end();
69 for ( ; sample_current != sample_end; ++sample_current ) {
70 SGSoundSample *sample = sample_current->second;
72 if ( sample->is_valid_source() && sample->is_playing() ) {
73 sample->no_valid_source();
74 _smgr->release_source( sample->get_source() );
75 _smgr->release_buffer( sample );
82 void SGSampleGroup::update( double dt ) {
84 if ( !_active || _pause ) return;
86 testForALError("start of update!!\n");
88 // Delete any OpenAL buffers that might still be in use.
89 unsigned int size = _removed_samples.size();
90 for (unsigned int i=0; i<size; ) {
91 SGSoundSample *sample = _removed_samples[i];
94 alGetSourcei( sample->get_source(), AL_SOURCE_STATE, &result );
95 if ( result == AL_STOPPED ) {
96 ALuint buffer = sample->get_buffer();
97 alDeleteBuffers( 1, &buffer );
98 testForALError("buffer remove");
99 _removed_samples.erase( _removed_samples.begin()+i );
106 sample_map_iterator sample_current = _samples.begin();
107 sample_map_iterator sample_end = _samples.end();
108 for ( ; sample_current != sample_end; ++sample_current ) {
109 SGSoundSample *sample = sample_current->second;
111 if ( !sample->is_valid_source() && sample->is_playing() ) {
113 // a request to start playing a sound has been filed.
115 if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
118 // start playing the sample
119 ALuint buffer = sample->get_buffer();
120 ALuint source = _smgr->request_source();
121 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
123 sample->set_source( source );
125 alSourcei( source, AL_BUFFER, buffer );
126 testForALError("assign buffer to source");
128 sample->set_source( source );
129 update_sample_config( sample );
131 ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE;
132 alSourcei( source, AL_LOOPING, looping );
133 alSourcef( source, AL_ROLLOFF_FACTOR, 1.0 );
134 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
135 alSourcePlay( source );
136 testForALError("sample play");
138 if (alIsBuffer(buffer) == AL_FALSE)
139 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
140 // sample->no_valid_source();
141 // sadly, no free source available at this time
144 } else if ( sample->is_valid_source() && sample->has_changed() ) {
145 if ( !sample->is_playing() ) {
146 // a request to stop playing the sound has been filed.
149 sample->no_valid_source();
150 _smgr->release_source( sample->get_source() );
152 update_sample_config( sample );
155 } else if ( sample->is_valid_source() ) {
156 // check if the sound has stopped by itself
158 unsigned int source = sample->get_source();
161 alGetSourcei( source, AL_SOURCE_STATE, &result );
162 if ( result == AL_STOPPED ) {
163 // sample is stoped because it wasn't looping
165 sample->no_valid_source();
166 _smgr->release_source( source );
167 _smgr->release_buffer( sample );
168 remove( sample->get_sample_name() );
171 testForALError("update");
175 // add a sound effect, return true if successful
176 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
178 sample_map_iterator sample_it = _samples.find( refname );
179 if ( sample_it != _samples.end() ) {
180 // sample name already exists
184 _samples[refname] = sound;
189 // remove a sound effect, return true if successful
190 bool SGSampleGroup::remove( const string &refname ) {
192 sample_map_iterator sample_it = _samples.find( refname );
193 if ( sample_it == _samples.end() ) {
194 // sample was not found
198 if ( sample_it->second->is_valid_buffer() )
199 _removed_samples.push_back( sample_it->second );
200 _samples.erase( sample_it );
206 // return true of the specified sound exists in the sound manager system
207 bool SGSampleGroup::exists( const string &refname ) {
208 sample_map_iterator sample_it = _samples.find( refname );
209 if ( sample_it == _samples.end() ) {
210 // sample was not found
218 // return a pointer to the SGSoundSample if the specified sound exists
219 // in the sound manager system, otherwise return NULL
220 SGSoundSample *SGSampleGroup::find( const string &refname ) {
221 sample_map_iterator sample_it = _samples.find( refname );
222 if ( sample_it == _samples.end() ) {
223 // sample was not found
227 return sample_it->second;
231 // stop playing all associated samples
233 SGSampleGroup::suspend ()
236 sample_map_iterator sample_current = _samples.begin();
237 sample_map_iterator sample_end = _samples.end();
238 for ( ; sample_current != sample_end; ++sample_current ) {
239 SGSoundSample *sample = sample_current->second;
241 if ( sample->is_valid_source() && sample->is_playing() ) {
242 alSourcePause( sample->get_source() );
245 testForALError("suspend");
248 // resume playing all associated samples
250 SGSampleGroup::resume ()
252 sample_map_iterator sample_current = _samples.begin();
253 sample_map_iterator sample_end = _samples.end();
254 for ( ; sample_current != sample_end; ++sample_current ) {
255 SGSoundSample *sample = sample_current->second;
257 if ( sample->is_valid_source() && sample->is_playing() ) {
258 alSourcePlay( sample->get_source() );
261 testForALError("resume");
266 // tell the scheduler to play the indexed sample in a continuous loop
267 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
268 SGSoundSample *sample = find( refname );
270 if ( sample == NULL ) {
274 sample->play( looping );
279 // return true of the specified sound is currently being played
280 bool SGSampleGroup::is_playing( const string& refname ) {
281 SGSoundSample *sample = find( refname );
283 if ( sample == NULL ) {
287 return ( sample->is_playing() ) ? true : false;
290 // immediate stop playing the sound
291 bool SGSampleGroup::stop( const string& refname ) {
292 SGSoundSample *sample = find( refname );
294 if ( sample == NULL ) {
302 // set source velocity of all managed sounds
303 void SGSampleGroup::set_velocity( const SGVec3d &vel ) {
304 if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) )
306 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
310 if (_velocity != vel) {
311 sample_map_iterator sample_current = _samples.begin();
312 sample_map_iterator sample_end = _samples.end();
313 for ( ; sample_current != sample_end; ++sample_current ) {
314 SGSoundSample *sample = sample_current->second;
315 sample->set_velocity( vel );
321 // set the source position of all managed sounds
322 void SGSampleGroup::set_position( const SGGeod& pos ) {
324 sample_map_iterator sample_current = _samples.begin();
325 sample_map_iterator sample_end = _samples.end();
326 for ( ; sample_current != sample_end; ++sample_current ) {
327 SGSoundSample *sample = sample_current->second;
328 sample->set_position( pos );
334 // set the source orientation of all managed sounds
335 void SGSampleGroup::set_orientation( const SGQuatd& ori ) {
337 if (_orientation != ori) {
338 sample_map_iterator sample_current = _samples.begin();
339 sample_map_iterator sample_end = _samples.end();
340 for ( ; sample_current != sample_end; ++sample_current ) {
341 SGSoundSample *sample = sample_current->second;
342 sample->set_orientation( ori );
348 void SGSampleGroup::set_volume( float vol )
351 if (_volume < 0.0) _volume = 0.0;
352 if (_volume > 1.0) _volume = 1.0;
354 sample_map_iterator sample_current = _samples.begin();
355 sample_map_iterator sample_end = _samples.end();
356 for ( ; sample_current != sample_end; ++sample_current ) {
357 SGSoundSample *sample = sample_current->second;
358 sample->set_master_volume( _volume );
362 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
363 if ( sample->is_valid_source() ) {
364 unsigned int source = sample->get_source();
366 float *position, *orientation, *velocity;
367 if ( _tied_to_listener ) {
368 position = _smgr->get_position().data();
369 orientation = _smgr->get_velocity().data();
370 velocity = _smgr->get_direction().data();
372 sample->update_absolute_position();
373 position = sample->get_position();
374 orientation = sample->get_velocity();
375 velocity = sample->get_orientation();
377 if (dist(_smgr->get_position(), sample->get_position_vec()) > 50000)
378 printf("source and listener distance greater than 50km!\n");
379 if (isNaN(position)) printf("NaN in source position\n");
380 if (isNaN(orientation)) printf("NaN in source orientation\n");
381 if (isNaN(velocity)) printf("NaN in source velocity\n");
383 alSourcefv( source, AL_POSITION, position );
384 alSourcefv( source, AL_VELOCITY, velocity );
385 alSourcefv( source, AL_DIRECTION, orientation );
386 testForALError("position and orientation");
388 alSourcef( source, AL_PITCH, sample->get_pitch() );
389 alSourcef( source, AL_GAIN, sample->get_volume() );
390 testForALError("pitch and gain");
392 if ( sample->has_static_data_changed() ) {
393 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
394 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
395 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
396 testForALError("audio cone");
398 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
399 alSourcef( source, AL_REFERENCE_DISTANCE,
400 sample->get_reference_dist() );
401 testForALError("distance rolloff");
406 bool SGSampleGroup::testForError(void *p, string s)
409 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
415 bool SGSampleGroup::testForALError(string s)
417 ALenum error = alGetError();
418 if (error != AL_NO_ERROR) {
419 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
420 << alGetString(error) << " at " << s);