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];
92 ALint result = AL_STOPPED;
94 if ( sample->is_valid_source() ) {
95 if ( sample->is_looping() ) {
96 sample->no_valid_source();
97 _smgr->release_source( sample->get_source() );
100 alGetSourcei( sample->get_source(), AL_SOURCE_STATE, &result );
103 if ( result == AL_STOPPED ) {
104 ALuint buffer = sample->get_buffer();
105 alDeleteBuffers( 1, &buffer );
106 testForALError("buffer remove");
107 _removed_samples.erase( _removed_samples.begin()+i );
114 sample_map_iterator sample_current = _samples.begin();
115 sample_map_iterator sample_end = _samples.end();
116 for ( ; sample_current != sample_end; ++sample_current ) {
117 SGSoundSample *sample = sample_current->second;
119 if ( !sample->is_valid_source() && sample->is_playing() ) {
121 // a request to start playing a sound has been filed.
123 if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
126 // start playing the sample
127 ALuint buffer = sample->get_buffer();
128 ALuint source = _smgr->request_source();
129 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
131 sample->set_source( source );
133 alSourcei( source, AL_BUFFER, buffer );
134 testForALError("assign buffer to source");
136 sample->set_source( source );
137 update_sample_config( sample );
139 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
140 alSourcei( source, AL_LOOPING, looping );
141 alSourcef( source, AL_ROLLOFF_FACTOR, 1.0 );
142 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
143 alSourcePlay( source );
144 testForALError("sample play");
146 if (alIsBuffer(buffer) == AL_FALSE)
147 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
148 // sample->no_valid_source();
149 // sadly, no free source available at this time
152 } else if ( sample->is_valid_source() && sample->has_changed() ) {
153 if ( !sample->is_playing() ) {
154 // a request to stop playing the sound has been filed.
157 sample->no_valid_source();
158 _smgr->release_source( sample->get_source() );
159 } else if ( _smgr->has_changed() ) {
160 update_sample_config( sample );
163 } else if ( sample->is_valid_source() ) {
164 // check if the sound has stopped by itself
166 unsigned int source = sample->get_source();
169 alGetSourcei( source, AL_SOURCE_STATE, &result );
170 if ( result == AL_STOPPED ) {
171 // sample is stoped because it wasn't looping
173 sample->no_valid_source();
174 _smgr->release_source( source );
175 _smgr->release_buffer( sample );
176 remove( sample->get_sample_name() );
179 testForALError("update");
183 // add a sound effect, return true if successful
184 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
186 sample_map_iterator sample_it = _samples.find( refname );
187 if ( sample_it != _samples.end() ) {
188 // sample name already exists
192 _samples[refname] = sound;
197 // remove a sound effect, return true if successful
198 bool SGSampleGroup::remove( const string &refname ) {
200 sample_map_iterator sample_it = _samples.find( refname );
201 if ( sample_it == _samples.end() ) {
202 // sample was not found
206 if ( sample_it->second->is_valid_buffer() )
207 _removed_samples.push_back( sample_it->second );
208 _samples.erase( sample_it );
214 // return true of the specified sound exists in the sound manager system
215 bool SGSampleGroup::exists( const string &refname ) {
216 sample_map_iterator sample_it = _samples.find( refname );
217 if ( sample_it == _samples.end() ) {
218 // sample was not found
226 // return a pointer to the SGSoundSample if the specified sound exists
227 // in the sound manager system, otherwise return NULL
228 SGSoundSample *SGSampleGroup::find( const string &refname ) {
229 sample_map_iterator sample_it = _samples.find( refname );
230 if ( sample_it == _samples.end() ) {
231 // sample was not found
235 return sample_it->second;
239 // stop playing all associated samples
241 SGSampleGroup::suspend ()
244 sample_map_iterator sample_current = _samples.begin();
245 sample_map_iterator sample_end = _samples.end();
246 for ( ; sample_current != sample_end; ++sample_current ) {
247 SGSoundSample *sample = sample_current->second;
249 if ( sample->is_valid_source() && sample->is_playing() ) {
250 alSourcePause( sample->get_source() );
253 testForALError("suspend");
256 // resume playing all associated samples
258 SGSampleGroup::resume ()
260 sample_map_iterator sample_current = _samples.begin();
261 sample_map_iterator sample_end = _samples.end();
262 for ( ; sample_current != sample_end; ++sample_current ) {
263 SGSoundSample *sample = sample_current->second;
265 if ( sample->is_valid_source() && sample->is_playing() ) {
266 alSourcePlay( sample->get_source() );
269 testForALError("resume");
274 // tell the scheduler to play the indexed sample in a continuous loop
275 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
276 SGSoundSample *sample = find( refname );
278 if ( sample == NULL ) {
282 sample->play( looping );
287 // return true of the specified sound is currently being played
288 bool SGSampleGroup::is_playing( const string& refname ) {
289 SGSoundSample *sample = find( refname );
291 if ( sample == NULL ) {
295 return ( sample->is_playing() ) ? true : false;
298 // immediate stop playing the sound
299 bool SGSampleGroup::stop( const string& refname ) {
300 SGSoundSample *sample = find( refname );
302 if ( sample == NULL ) {
310 // set source velocity of all managed sounds
311 void SGSampleGroup::set_velocity( const SGVec3f &vel ) {
312 if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) )
314 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
318 if (_velocity != vel) {
319 sample_map_iterator sample_current = _samples.begin();
320 sample_map_iterator sample_end = _samples.end();
321 for ( ; sample_current != sample_end; ++sample_current ) {
322 SGSoundSample *sample = sample_current->second;
323 sample->set_velocity( vel );
329 // set the source position of all managed sounds
330 void SGSampleGroup::set_position( const SGGeod& pos ) {
332 sample_map_iterator sample_current = _samples.begin();
333 sample_map_iterator sample_end = _samples.end();
334 for ( ; sample_current != sample_end; ++sample_current ) {
335 SGSoundSample *sample = sample_current->second;
336 sample->set_position( pos );
342 // set the source orientation of all managed sounds
343 void SGSampleGroup::set_orientation( const SGQuatd& ori ) {
345 if (_orientation != ori) {
346 sample_map_iterator sample_current = _samples.begin();
347 sample_map_iterator sample_end = _samples.end();
348 for ( ; sample_current != sample_end; ++sample_current ) {
349 SGSoundSample *sample = sample_current->second;
350 sample->set_orientation( ori );
356 void SGSampleGroup::set_volume( float vol )
359 if (_volume < 0.0) _volume = 0.0;
360 if (_volume > 1.0) _volume = 1.0;
362 sample_map_iterator sample_current = _samples.begin();
363 sample_map_iterator sample_end = _samples.end();
364 for ( ; sample_current != sample_end; ++sample_current ) {
365 SGSoundSample *sample = sample_current->second;
366 sample->set_master_volume( _volume );
370 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
371 SGVec3f orientation, velocity;
374 if ( _tied_to_listener ) {
375 orientation = _smgr->get_direction();
376 position = _smgr->get_position();
377 velocity = _smgr->get_velocity();
379 sample->update_absolute_position();
380 orientation = sample->get_orientation();
381 position = sample->get_position();
382 velocity = sample->get_velocity();
385 if (dist(position, _smgr->get_position()) > 10000)
386 printf("source and listener distance greater than 20km!\n");
387 if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
388 if (isNaN(orientation.data())) printf("NaN in source orientation\n");
389 if (isNaN(velocity.data())) printf("NaN in source velocity\n");
391 unsigned int source = sample->get_source();
392 alSourcefv( source, AL_POSITION, toVec3f(position).data() );
393 alSourcefv( source, AL_VELOCITY, velocity.data() );
394 alSourcefv( source, AL_DIRECTION, orientation.data() );
395 testForALError("position and orientation");
397 alSourcef( source, AL_PITCH, sample->get_pitch() );
398 alSourcef( source, AL_GAIN, sample->get_volume() );
399 testForALError("pitch and gain");
401 if ( sample->has_static_data_changed() ) {
402 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
403 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
404 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
405 testForALError("audio cone");
407 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
408 alSourcef( source, AL_REFERENCE_DISTANCE,
409 sample->get_reference_dist() );
410 testForALError("distance rolloff");
414 bool SGSampleGroup::testForError(void *p, string s)
417 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
423 bool SGSampleGroup::testForALError(string s)
425 ALenum error = alGetError();
426 if (error != AL_NO_ERROR) {
427 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
428 << alGetString(error) << " at " << s);