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())
49 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
55 _tied_to_listener(false),
56 _velocity(SGVec3d::zeros()),
57 _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 // Update the position and orientation information for all samples.
115 if ( _changed || _smgr->has_changed() ) {
116 update_pos_and_orientation();
120 sample_map_iterator sample_current = _samples.begin();
121 sample_map_iterator sample_end = _samples.end();
122 for ( ; sample_current != sample_end; ++sample_current ) {
123 SGSoundSample *sample = sample_current->second;
125 if ( !sample->is_valid_source() && sample->is_playing() ) {
127 // a request to start playing a sound has been filed.
129 if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
132 // start playing the sample
133 ALuint buffer = sample->get_buffer();
134 ALuint source = _smgr->request_source();
135 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
137 sample->set_source( source );
139 alSourcei( source, AL_BUFFER, buffer );
140 testForALError("assign buffer to source");
142 sample->set_source( source );
143 update_sample_config( sample );
145 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
146 alSourcei( source, AL_LOOPING, looping );
147 alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
148 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
149 alSourcePlay( source );
150 testForALError("sample play");
152 if (alIsBuffer(buffer) == AL_FALSE)
153 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
154 // sample->no_valid_source();
155 // sadly, no free source available at this time
158 } else if ( sample->is_valid_source() && sample->has_changed() ) {
159 if ( !sample->is_playing() ) {
160 // a request to stop playing the sound has been filed.
163 sample->no_valid_source();
164 _smgr->release_source( sample->get_source() );
165 } else if ( _smgr->has_changed() ) {
166 update_sample_config( sample );
169 } else if ( sample->is_valid_source() ) {
170 // check if the sound has stopped by itself
172 unsigned int source = sample->get_source();
175 alGetSourcei( source, AL_SOURCE_STATE, &result );
176 if ( result == AL_STOPPED ) {
177 // sample is stoped because it wasn't looping
179 sample->no_valid_source();
180 _smgr->release_source( source );
181 _smgr->release_buffer( sample );
182 remove( sample->get_sample_name() );
185 testForALError("update");
189 // add a sound effect, return true if successful
190 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
192 sample_map_iterator sample_it = _samples.find( refname );
193 if ( sample_it != _samples.end() ) {
194 // sample name already exists
198 _samples[refname] = sound;
203 // remove a sound effect, return true if successful
204 bool SGSampleGroup::remove( const string &refname ) {
206 sample_map_iterator sample_it = _samples.find( refname );
207 if ( sample_it == _samples.end() ) {
208 // sample was not found
212 if ( sample_it->second->is_valid_buffer() )
213 _removed_samples.push_back( sample_it->second );
214 _samples.erase( sample_it );
220 // return true of the specified sound exists in the sound manager system
221 bool SGSampleGroup::exists( const string &refname ) {
222 sample_map_iterator sample_it = _samples.find( refname );
223 if ( sample_it == _samples.end() ) {
224 // sample was not found
232 // return a pointer to the SGSoundSample if the specified sound exists
233 // in the sound manager system, otherwise return NULL
234 SGSoundSample *SGSampleGroup::find( const string &refname ) {
235 sample_map_iterator sample_it = _samples.find( refname );
236 if ( sample_it == _samples.end() ) {
237 // sample was not found
241 return sample_it->second;
245 // stop playing all associated samples
247 SGSampleGroup::suspend ()
250 sample_map_iterator sample_current = _samples.begin();
251 sample_map_iterator sample_end = _samples.end();
252 for ( ; sample_current != sample_end; ++sample_current ) {
253 SGSoundSample *sample = sample_current->second;
255 if ( sample->is_valid_source() && sample->is_playing() ) {
256 alSourcePause( sample->get_source() );
259 testForALError("suspend");
262 // resume playing all associated samples
264 SGSampleGroup::resume ()
266 sample_map_iterator sample_current = _samples.begin();
267 sample_map_iterator sample_end = _samples.end();
268 for ( ; sample_current != sample_end; ++sample_current ) {
269 SGSoundSample *sample = sample_current->second;
271 if ( sample->is_valid_source() && sample->is_playing() ) {
272 alSourcePlay( sample->get_source() );
275 testForALError("resume");
280 // tell the scheduler to play the indexed sample in a continuous loop
281 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
282 SGSoundSample *sample = find( refname );
284 if ( sample == NULL ) {
288 sample->play( looping );
293 // return true of the specified sound is currently being played
294 bool SGSampleGroup::is_playing( const string& refname ) {
295 SGSoundSample *sample = find( refname );
297 if ( sample == NULL ) {
301 return ( sample->is_playing() ) ? true : false;
304 // immediate stop playing the sound
305 bool SGSampleGroup::stop( const string& refname ) {
306 SGSoundSample *sample = find( refname );
308 if ( sample == NULL ) {
316 void SGSampleGroup::set_volume( float vol )
319 if (_volume < 0.0) _volume = 0.0;
320 if (_volume > 1.0) _volume = 1.0;
323 // set the source position and orientation of all managed sounds
324 void SGSampleGroup::update_pos_and_orientation() {
326 SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
327 SGQuatd ec2body= SGQuatd::fromLonLat(_base_pos) * _orientation;
329 SGVec3f velocity = SGVec3f::zeros();
330 if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
331 velocity = toVec3f( ec2body.backTransform(_velocity*SG_FEET_TO_METER) );
334 sample_map_iterator sample_current = _samples.begin();
335 sample_map_iterator sample_end = _samples.end();
336 for ( ; sample_current != sample_end; ++sample_current ) {
337 SGSoundSample *sample = sample_current->second;
338 sample->set_master_volume( _volume );
339 sample->set_orientation( _orientation );
340 sample->set_rotation( ec2body );
341 sample->set_position( position );
342 sample->set_velocity( velocity );
346 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
347 SGVec3f orientation, velocity;
350 if ( _tied_to_listener ) {
351 orientation = _smgr->get_direction();
352 position = SGVec3d::zeros();
353 velocity = _smgr->get_velocity();
355 sample->update_pos_and_orientation();
356 orientation = sample->get_orientation();
357 position = sample->get_position();
358 velocity = sample->get_velocity();
362 if (length(position) > 20000)
363 printf("source and listener distance greater than 20km!\n");
364 if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
365 if (isNaN(orientation.data())) printf("NaN in source orientation\n");
366 if (isNaN(velocity.data())) printf("NaN in source velocity\n");
369 unsigned int source = sample->get_source();
370 alSourcefv( source, AL_POSITION, toVec3f(position).data() );
371 alSourcefv( source, AL_VELOCITY, velocity.data() );
372 alSourcefv( source, AL_DIRECTION, orientation.data() );
373 testForALError("position and orientation");
375 alSourcef( source, AL_PITCH, sample->get_pitch() );
376 alSourcef( source, AL_GAIN, sample->get_volume() );
377 testForALError("pitch and gain");
379 if ( sample->has_static_data_changed() ) {
380 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
381 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
382 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
383 testForALError("audio cone");
385 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
386 alSourcef( source, AL_REFERENCE_DISTANCE,
387 sample->get_reference_dist() );
388 testForALError("distance rolloff");
392 bool SGSampleGroup::testForError(void *p, string s)
395 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
401 bool SGSampleGroup::testForALError(string s)
403 ALenum error = alGetError();
404 if (error != AL_NO_ERROR) {
405 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
406 << alGetString(error) << " at " << s);