2 # include <simgear_config.h>
5 #include <simgear/compiler.h>
7 #if defined (__APPLE__)
9 # if ( __GNUC__ >= 3 ) && ( __GNUC_MINOR__ >= 3 )
11 inline int (isnan)(double r) { return !(r <= 0 || r >= 0); }
13 // any C++ header file undefines isinf and isnan
14 // so this should be included before <iostream>
15 // the functions are STILL in libm (libSystem on mac os x)
16 extern "C" int isnan (double);
17 extern "C" int isinf (double);
20 // inline int (isinf)(double r) { return isinf(r); }
21 // inline int (isnan)(double r) { return isnan(r); }
25 #if defined (__FreeBSD__)
26 # if __FreeBSD_version < 500000
28 inline int isnan(double r) { return !(r <= 0 || r >= 0); }
33 #if defined (__CYGWIN__)
37 #if defined(__MINGW32__)
38 # define isnan(x) _isnan(x)
41 #include "soundmgr_openal.hxx"
42 #include "sample_group.hxx"
44 SGSampleGroup::SGSampleGroup () :
48 _tied_to_listener(false),
49 _velocity(SGVec3d::zeros()),
51 _orientation(SGQuatd::zeros())
56 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
60 _tied_to_listener(false),
61 _velocity(SGVec3d::zeros()),
63 _orientation(SGQuatd::zeros())
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 ) return;
92 // testForALError("start of update!!\n");
94 sample_map_iterator sample_current = _samples.begin();
95 sample_map_iterator sample_end = _samples.end();
96 for ( ; sample_current != sample_end; ++sample_current ) {
97 SGSoundSample *sample = sample_current->second;
99 if ( !sample->is_valid_source() && sample->is_playing() ) {
101 // a request to start playing a sound has been filed.
103 if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
106 // start playing the sample
107 ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE;
108 ALuint buffer = sample->get_buffer();
109 ALuint source = _smgr->request_source();
110 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
112 sample->set_source( source );
114 alSourcei( source, AL_BUFFER, buffer );
115 testForALError("assign buffer to source");
117 sample->set_source( source );
118 update_sample_config( sample );
120 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
121 alSourcei( source, AL_LOOPING, looping );
122 alSourcef( source, AL_ROLLOFF_FACTOR, 1.2 );
123 alSourcePlay( source );
124 testForALError("sample play");
126 if (alIsBuffer(buffer) == AL_FALSE)
127 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
128 // sample->no_valid_source();
129 // sadly, no free source available at this time
132 } else if ( sample->is_valid_source() && sample->has_changed() ) {
133 if ( !sample->is_playing() ) {
134 // a request to stop playing the sound has been filed.
136 sample->no_valid_source();
138 _smgr->release_source( sample->get_source() );
140 update_sample_config( sample );
143 } else if ( sample->is_valid_source() ) {
144 // check if the sound has stopped by itself
146 unsigned int source = sample->get_source();
149 alGetSourcei( source, AL_SOURCE_STATE, &result );
150 if ( result == AL_STOPPED ) {
151 // sample is stoped because it wasn't looping
152 sample->no_valid_source();
154 _smgr->release_source( source );
157 testForALError("update");
161 // add a sound effect, return true if successful
162 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
164 sample_map_iterator sample_it = _samples.find( refname );
165 if ( sample_it != _samples.end() ) {
166 // sample name already exists
170 _samples[refname] = sound;
175 // remove a sound effect, return true if successful
176 bool SGSampleGroup::remove( const string &refname ) {
178 sample_map_iterator sample_it = _samples.find( refname );
179 if ( sample_it == _samples.end() ) {
180 // sample was not found
184 // remove the sources buffer
185 _smgr->release_buffer( sample_it->second );
186 _samples.erase( refname );
192 // return true of the specified sound exists in the sound manager system
193 bool SGSampleGroup::exists( const string &refname ) {
194 sample_map_iterator sample_it = _samples.find( refname );
195 if ( sample_it == _samples.end() ) {
196 // sample was not found
204 // return a pointer to the SGSoundSample if the specified sound exists
205 // in the sound manager system, otherwise return NULL
206 SGSoundSample *SGSampleGroup::find( const string &refname ) {
207 sample_map_iterator sample_it = _samples.find( refname );
208 if ( sample_it == _samples.end() ) {
209 // sample was not found
213 return sample_it->second;
217 // stop playing all associated samples
219 SGSampleGroup::suspend ()
222 sample_map_iterator sample_current = _samples.begin();
223 sample_map_iterator sample_end = _samples.end();
224 for ( ; sample_current != sample_end; ++sample_current ) {
225 SGSoundSample *sample = sample_current->second;
227 if ( sample->is_valid_source() && sample->is_playing() ) {
228 unsigned int source = sample->get_source();
229 alSourcePause( source );
232 testForALError("suspend");
235 // resume playing all associated samples
237 SGSampleGroup::resume ()
239 sample_map_iterator sample_current = _samples.begin();
240 sample_map_iterator sample_end = _samples.end();
241 for ( ; sample_current != sample_end; ++sample_current ) {
242 SGSoundSample *sample = sample_current->second;
244 if ( sample->is_valid_source() && sample->is_playing() ) {
245 unsigned int source = sample->get_source();
246 alSourcePlay( source );
249 testForALError("resume");
254 // tell the scheduler to play the indexed sample in a continuous loop
255 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
256 SGSoundSample *sample = find( refname );
258 if ( sample == NULL ) {
262 sample->play( looping );
267 // return true of the specified sound is currently being played
268 bool SGSampleGroup::is_playing( const string& refname ) {
269 SGSoundSample *sample = find( refname );
271 if ( sample == NULL ) {
275 return ( sample->is_playing() ) ? true : false;
278 // immediate stop playing the sound
279 bool SGSampleGroup::stop( const string& refname ) {
280 SGSoundSample *sample = find( refname );
282 if ( sample == NULL ) {
290 // set source velocity of all managed sounds
291 void SGSampleGroup::set_velocity( SGVec3d &vel ) {
292 if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) )
294 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
298 if (_velocity != vel) {
299 sample_map_iterator sample_current = _samples.begin();
300 sample_map_iterator sample_end = _samples.end();
301 for ( ; sample_current != sample_end; ++sample_current ) {
302 SGSoundSample *sample = sample_current->second;
303 sample->set_velocity( vel );
309 // set the source position of all managed sounds
310 void SGSampleGroup::set_position( SGGeod pos ) {
312 sample_map_iterator sample_current = _samples.begin();
313 sample_map_iterator sample_end = _samples.end();
314 for ( ; sample_current != sample_end; ++sample_current ) {
315 SGSoundSample *sample = sample_current->second;
316 sample->set_position( pos );
322 // set the source orientation of all managed sounds
323 void SGSampleGroup::set_orientation( SGQuatd ori ) {
325 if (_orientation != ori) {
326 sample_map_iterator sample_current = _samples.begin();
327 sample_map_iterator sample_end = _samples.end();
328 for ( ; sample_current != sample_end; ++sample_current ) {
329 SGSoundSample *sample = sample_current->second;
330 sample->set_orientation( ori );
336 void SGSampleGroup::set_volume( float vol )
339 if (_volume < 0.0) _volume = 0.0;
340 if (_volume > 1.0) _volume = 1.0;
342 sample_map_iterator sample_current = _samples.begin();
343 sample_map_iterator sample_end = _samples.end();
344 for ( ; sample_current != sample_end; ++sample_current ) {
345 SGSoundSample *sample = sample_current->second;
346 sample->set_master_volume( _volume );
350 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
351 if ( sample->is_valid_source() ) {
352 unsigned int source = sample->get_source();
354 if ( _tied_to_listener && _smgr->has_changed() ) {
355 alSourcefv( source, AL_POSITION, _smgr->get_position().data() );
356 alSourcefv( source, AL_DIRECTION, _smgr->get_direction().data() );
357 alSourcefv( source, AL_VELOCITY, _smgr->get_velocity().data() );
359 alSourcefv( source, AL_POSITION, sample->get_position() );
360 alSourcefv( source, AL_DIRECTION, sample->get_orientation() );
361 alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
363 testForALError("position and orientation");
365 alSourcef( source, AL_PITCH, sample->get_pitch() );
366 alSourcef( source, AL_GAIN, sample->get_volume() );
367 testForALError("pitch and gain");
369 if ( sample->has_static_data_changed() ) {
370 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
371 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
372 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
373 testForALError("audio cone");
375 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
376 alSourcef( source, AL_REFERENCE_DISTANCE,
377 sample->get_reference_dist() );
378 testForALError("distance rolloff");
383 bool SGSampleGroup::testForError(void *p, string s)
386 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
392 bool SGSampleGroup::testForALError(string s)
394 ALenum error = alGetError();
395 if (error != AL_NO_ERROR) {
396 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
397 << alGetString(error) << " at " << s);