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 () :
47 _position(SGVec3d::zeros().data()),
48 _orientation(SGVec3f::zeros().data()),
49 _tied_to_listener(false)
54 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
57 _position(SGVec3d::zeros().data()),
58 _orientation(SGVec3f::zeros().data()),
59 _tied_to_listener(false)
61 _smgr->add(this, refname);
62 _active = _smgr->is_working();
67 SGSampleGroup::~SGSampleGroup ()
71 sample_map_iterator sample_current = _samples.begin();
72 sample_map_iterator sample_end = _samples.end();
73 for ( ; sample_current != sample_end; ++sample_current ) {
74 SGSoundSample *sample = sample_current->second;
76 if ( sample->is_valid_source() && sample->is_playing() ) {
77 sample->no_valid_source();
78 _smgr->release_source( sample->get_source() );
79 _smgr->release_buffer( sample );
86 void SGSampleGroup::update( double dt ) {
88 if ( !_active ) return;
90 // testForALError("start of update!!\n");
92 sample_map_iterator sample_current = _samples.begin();
93 sample_map_iterator sample_end = _samples.end();
94 for ( ; sample_current != sample_end; ++sample_current ) {
95 SGSoundSample *sample = sample_current->second;
97 if ( !sample->is_valid_source() && sample->is_playing() ) {
99 // a request to start playing a sound has been filed.
101 if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
104 if ( _tied_to_listener && _smgr->has_changed() ) {
105 sample->set_base_position( _smgr->get_position_vec() );
106 sample->set_orientation( _smgr->get_direction() );
107 sample->set_velocity( _smgr->get_velocity() );
110 // start playing the sample
111 ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE;
112 ALuint buffer = sample->get_buffer();
113 ALuint source = _smgr->request_source();
114 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
116 sample->set_source( source );
118 alSourcei( source, AL_BUFFER, buffer );
119 testForALError("assign buffer to source");
121 sample->set_source( source );
122 update_sample_config( sample );
124 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
125 alSourcei( source, AL_LOOPING, looping );
126 alSourcef( source, AL_ROLLOFF_FACTOR, 1.2 );
127 alSourcePlay( source );
128 testForALError("sample play");
130 if (alIsBuffer(buffer) == AL_FALSE)
131 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
132 // sample->no_valid_source();
133 // sadly, no free source available at this time
136 } else if ( sample->is_valid_source() && sample->has_changed() ) {
137 if ( !sample->is_playing() ) {
138 // a request to stop playing the sound has been filed.
140 sample->no_valid_source();
142 _smgr->release_source( sample->get_source() );
144 update_sample_config( sample );
147 } else if ( sample->is_valid_source() ) {
148 // check if the sound has stopped by itself
150 unsigned int source = sample->get_source();
153 alGetSourcei( source, AL_SOURCE_STATE, &result );
154 if ( result == AL_STOPPED ) {
155 // sample is stoped because it wasn't looping
156 sample->no_valid_source();
158 _smgr->release_source( source );
161 testForALError("update");
165 // add a sound effect, return true if successful
166 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
168 sample_map_iterator sample_it = _samples.find( refname );
169 if ( sample_it != _samples.end() ) {
170 // sample name already exists
174 _samples[refname] = sound;
179 // remove a sound effect, return true if successful
180 bool SGSampleGroup::remove( const string &refname ) {
182 sample_map_iterator sample_it = _samples.find( refname );
183 if ( sample_it == _samples.end() ) {
184 // sample was not found
188 // remove the sources buffer
189 _smgr->release_buffer( sample_it->second );
190 _samples.erase( refname );
196 // return true of the specified sound exists in the sound manager system
197 bool SGSampleGroup::exists( const string &refname ) {
198 sample_map_iterator sample_it = _samples.find( refname );
199 if ( sample_it == _samples.end() ) {
200 // sample was not found
208 // return a pointer to the SGSoundSample if the specified sound exists
209 // in the sound manager system, otherwise return NULL
210 SGSoundSample *SGSampleGroup::find( const string &refname ) {
211 sample_map_iterator sample_it = _samples.find( refname );
212 if ( sample_it == _samples.end() ) {
213 // sample was not found
217 return sample_it->second;
221 // stop playing all associated samples
223 SGSampleGroup::suspend ()
226 sample_map_iterator sample_current = _samples.begin();
227 sample_map_iterator sample_end = _samples.end();
228 for ( ; sample_current != sample_end; ++sample_current ) {
229 SGSoundSample *sample = sample_current->second;
231 if ( sample->is_valid_source() && sample->is_playing() ) {
232 unsigned int source = sample->get_source();
233 alSourcePause( source );
236 testForALError("suspend");
239 // resume playing all associated samples
241 SGSampleGroup::resume ()
243 sample_map_iterator sample_current = _samples.begin();
244 sample_map_iterator sample_end = _samples.end();
245 for ( ; sample_current != sample_end; ++sample_current ) {
246 SGSoundSample *sample = sample_current->second;
248 if ( sample->is_valid_source() && sample->is_playing() ) {
249 unsigned int source = sample->get_source();
250 alSourcePlay( source );
253 testForALError("resume");
258 // tell the scheduler to play the indexed sample in a continuous loop
259 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
260 SGSoundSample *sample = find( refname );
262 if ( sample == NULL ) {
266 sample->play( looping );
271 // return true of the specified sound is currently being played
272 bool SGSampleGroup::is_playing( const string& refname ) {
273 SGSoundSample *sample = find( refname );
275 if ( sample == NULL ) {
279 return ( sample->is_playing() ) ? true : false;
282 // immediate stop playing the sound
283 bool SGSampleGroup::stop( const string& refname ) {
284 SGSoundSample *sample = find( refname );
286 if ( sample == NULL ) {
295 // set source position of all managed sounds
296 void SGSampleGroup::set_position( SGVec3d pos ) {
297 if ( isnan(pos.data()[0]) || isnan(pos.data()[1]) || isnan(pos.data()[2]) )
299 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup postion");
303 if ( !_tied_to_listener ) {
304 sample_map_iterator sample_current = _samples.begin();
305 sample_map_iterator sample_end = _samples.end();
306 for ( ; sample_current != sample_end; ++sample_current ) {
307 SGSoundSample *sample = sample_current->second;
308 sample->set_base_position( pos );
313 // set source velocity of all managed sounds
314 void SGSampleGroup::set_velocity( SGVec3f vel ) {
315 if ( isnan(vel.data()[0]) || isnan(vel.data()[1]) || isnan(vel.data()[2]) )
317 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
321 sample_map_iterator sample_current = _samples.begin();
322 sample_map_iterator sample_end = _samples.end();
323 for ( ; sample_current != sample_end; ++sample_current ) {
324 SGSoundSample *sample = sample_current->second;
325 sample->set_velocity( vel );
329 // ste the source orientation of all managed sounds
330 void SGSampleGroup::set_orientation( SGVec3f ori ) {
331 if ( isnan(ori.data()[0]) || isnan(ori.data()[1]) || isnan(ori.data()[2]) )
333 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup orientation");
337 sample_map_iterator sample_current = _samples.begin();
338 sample_map_iterator sample_end = _samples.end();
339 for ( ; sample_current != sample_end; ++sample_current ) {
340 SGSoundSample *sample = sample_current->second;
341 sample->set_orientation( ori );
345 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
346 if ( sample->is_valid_source() ) {
347 unsigned int source = sample->get_source();
349 alSourcefv( source, AL_POSITION, sample->get_position());
350 alSourcefv( source, AL_DIRECTION, sample->get_direction() );
351 alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
352 testForALError("position and orientation");
354 alSourcef( source, AL_PITCH, sample->get_pitch() );
355 alSourcef( source, AL_GAIN, sample->get_volume() );
356 testForALError("pitch and gain");
358 if ( sample->has_static_data_changed() ) {
359 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
360 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
361 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
362 testForALError("audio cone");
364 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
365 alSourcef( source, AL_REFERENCE_DISTANCE,
366 sample->get_reference_dist() );
367 testForALError("distance rolloff");
372 void SGSampleGroup::set_volume( float vol )
375 if (_volume < 0.0) _volume = 0.0;
376 if (_volume > 1.0) _volume = 1.0;
378 sample_map_iterator sample_current = _samples.begin();
379 sample_map_iterator sample_end = _samples.end();
380 for ( ; sample_current != sample_end; ++sample_current ) {
381 SGSoundSample *sample = sample_current->second;
382 sample->set_master_volume( _volume );
386 bool SGSampleGroup::testForError(void *p, string s)
389 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
395 bool SGSampleGroup::testForALError(string s)
397 ALenum error = alGetError();
398 if (error != AL_NO_ERROR) {
399 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
400 << alGetString(error) << " at " << s);