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 #if defined (__APPLE__)
31 # if ( __GNUC__ >= 3 ) && ( __GNUC_MINOR__ >= 3 )
33 inline int (isnan)(double r) { return !(r <= 0 || r >= 0); }
35 // any C++ header file undefines isinf and isnan
36 // so this should be included before <iostream>
37 // the functions are STILL in libm (libSystem on mac os x)
38 extern "C" int isnan (double);
39 extern "C" int isinf (double);
42 // inline int (isinf)(double r) { return isinf(r); }
43 // inline int (isnan)(double r) { return isnan(r); }
47 #if defined (__FreeBSD__)
48 # if __FreeBSD_version < 500000
50 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
55 #if defined (__CYGWIN__)
59 #if defined(__MINGW32__)
60 # define isnan(x) _isnan(x)
63 #include "soundmgr_openal.hxx"
64 #include "sample_group.hxx"
66 SGSampleGroup::SGSampleGroup () :
70 _tied_to_listener(false),
71 _velocity(SGVec3d::zeros()),
72 _orientation(SGQuatd::zeros()),
78 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
82 _tied_to_listener(false),
83 _velocity(SGVec3d::zeros()),
84 _orientation(SGQuatd::zeros()),
87 _smgr->add(this, refname);
91 SGSampleGroup::~SGSampleGroup ()
95 sample_map_iterator sample_current = _samples.begin();
96 sample_map_iterator sample_end = _samples.end();
97 for ( ; sample_current != sample_end; ++sample_current ) {
98 SGSoundSample *sample = sample_current->second;
100 if ( sample->is_valid_source() && sample->is_playing() ) {
101 sample->no_valid_source();
102 _smgr->release_source( sample->get_source() );
103 _smgr->release_buffer( sample );
110 void SGSampleGroup::update( double dt ) {
112 if ( !_active ) return;
114 testForALError("start of update!!\n");
116 sample_map_iterator sample_current = _samples.begin();
117 sample_map_iterator sample_end = _samples.end();
118 for ( ; sample_current != sample_end; ++sample_current ) {
119 SGSoundSample *sample = sample_current->second;
121 if ( !sample->is_valid_source() && sample->is_playing() ) {
123 // a request to start playing a sound has been filed.
125 if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
128 // start playing the sample
129 ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE;
130 ALuint buffer = sample->get_buffer();
131 ALuint source = _smgr->request_source();
132 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
134 sample->set_source( source );
136 alSourcei( source, AL_BUFFER, buffer );
137 testForALError("assign buffer to source");
139 sample->set_source( source );
140 update_sample_config( sample );
142 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
143 alSourcei( source, AL_LOOPING, looping );
144 alSourcef( source, AL_ROLLOFF_FACTOR, 1.0 );
145 alSourcePlay( source );
146 testForALError("sample play");
148 if (alIsBuffer(buffer) == AL_FALSE)
149 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
150 // sample->no_valid_source();
151 // sadly, no free source available at this time
154 } else if ( sample->is_valid_source() && sample->has_changed() ) {
155 if ( !sample->is_playing() ) {
156 // a request to stop playing the sound has been filed.
158 sample->no_valid_source();
160 _smgr->release_source( sample->get_source() );
162 update_sample_config( sample );
165 } else if ( sample->is_valid_source() ) {
166 // check if the sound has stopped by itself
168 unsigned int source = sample->get_source();
171 alGetSourcei( source, AL_SOURCE_STATE, &result );
172 if ( result == AL_STOPPED ) {
173 // sample is stoped because it wasn't looping
174 sample->no_valid_source();
176 _smgr->release_source( source );
179 testForALError("update");
183 // add a sound effect, return true if successful
184 bool SGSampleGroup::add( 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 // remove the sources buffer
207 _smgr->release_buffer( sample_it->second );
208 _samples.erase( refname );
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 SGVec3d &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 if ( sample->is_valid_source() ) {
372 unsigned int source = sample->get_source();
374 if ( _tied_to_listener && _smgr->has_changed() ) {
375 alSourcefv( source, AL_POSITION, _smgr->get_position().data() );
376 alSourcefv( source, AL_DIRECTION, _smgr->get_direction().data() );
377 alSourcefv( source, AL_VELOCITY, _smgr->get_velocity().data() );
379 alSourcefv( source, AL_POSITION, sample->get_position() );
380 alSourcefv( source, AL_DIRECTION, sample->get_orientation() );
381 alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
383 testForALError("position and orientation");
385 alSourcef( source, AL_PITCH, sample->get_pitch() );
386 alSourcef( source, AL_GAIN, sample->get_volume() );
387 testForALError("pitch and gain");
389 if ( sample->has_static_data_changed() ) {
390 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
391 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
392 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
393 testForALError("audio cone");
395 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
396 alSourcef( source, AL_REFERENCE_DISTANCE,
397 sample->get_reference_dist() );
398 testForALError("distance rolloff");
403 bool SGSampleGroup::testForError(void *p, string s)
406 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
412 bool SGSampleGroup::testForALError(string s)
414 ALenum error = alGetError();
415 if (error != AL_NO_ERROR) {
416 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
417 << alGetString(error) << " at " << s);