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 _position_changed(true),
49 _position(SGVec3d::zeros().data()),
50 _orientation(SGVec3f::zeros().data())
55 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
59 _position_changed(true),
60 _position(SGVec3d::zeros().data()),
61 _orientation(SGVec3f::zeros().data())
63 _smgr->add(this, refname);
64 _active = _smgr->is_working();
68 SGSampleGroup::~SGSampleGroup ()
72 sample_map_iterator sample_current = _samples.begin();
73 sample_map_iterator sample_end = _samples.end();
74 for ( ; sample_current != sample_end; ++sample_current ) {
75 SGSoundSample *sample = sample_current->second;
77 if ( sample->is_valid_source() && sample->is_playing() ) {
78 sample->no_valid_source();
79 _smgr->release_source( sample->get_source() );
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 ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE;
103 if ( !sample->is_valid_buffer() ) {
104 // sample was not yet loaded or removed again
106 // TODO: Create a buffer cache that checks whether a file is already present
107 // as an OpenAL buffer since buffers can be shared among sources.
109 if ( testForALError("load sample") ) {
110 throw sg_exception("Failed to load sound sample.");
114 // create an OpenAL buffer handle
116 alGenBuffers(1, &buffer);
117 if ( testForALError("generate buffer") ) {
118 throw sg_exception("Failed to generate OpenAL buffer.");
122 // Copy data to the internal OpenAL buffer
123 const ALvoid *data = sample->get_data();
124 ALenum format = sample->get_format();
125 ALsizei size = sample->get_size();
126 ALsizei freq = sample->get_frequency();
127 alBufferData( buffer, format, data, size, freq );
129 if ( testForALError("buffer add data") ) {
133 sample->set_buffer(buffer);
136 // start playing the sample
137 ALuint buffer = sample->get_buffer();
138 ALuint source = _smgr->request_source();
139 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
141 sample->set_source( source );
143 alSourcei( source, AL_BUFFER, buffer );
144 testForALError("assign buffer to source");
146 sample->set_source( source );
147 update_sample_config( sample );
149 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
150 alSourcei( source, AL_LOOPING, looping );
151 alSourcePlay( source );
152 testForALError("sample play");
154 if (alIsBuffer(buffer) == AL_FALSE)
155 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
156 // sample->no_valid_source();
157 // sadly, no free source available at this time
160 } else if ( sample->is_valid_source() && sample->has_changed() ) {
161 if ( !sample->is_playing() ) {
162 // a request to stop playing the sound has been filed.
164 sample->no_valid_source();
166 _smgr->release_source( sample->get_source() );
168 update_sample_config( sample );
171 } else if ( sample->is_valid_source() ) {
172 // check if the sound has stopped by itself
174 unsigned int source = sample->get_source();
177 alGetSourcei( source, AL_SOURCE_STATE, &result );
178 if ( result == AL_STOPPED ) {
179 // sample is stoped because it wasn't looping
180 sample->no_valid_source();
182 _smgr->release_source( source );
186 testForALError("update");
190 // add a sound effect, return true if successful
191 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
193 sample_map_iterator sample_it = _samples.find( refname );
194 if ( sample_it != _samples.end() ) {
195 // sample name already exists
199 _samples[refname] = sound;
204 // remove a sound effect, return true if successful
205 bool SGSampleGroup::remove( const string &refname ) {
207 sample_map_iterator sample_it = _samples.find( refname );
208 if ( sample_it == _samples.end() ) {
209 // sample was not found
213 _samples.erase( sample_it );
218 // return true of the specified sound exists in the sound manager system
219 bool SGSampleGroup::exists( const string &refname ) {
220 sample_map_iterator sample_it = _samples.find( refname );
221 if ( sample_it == _samples.end() ) {
222 // sample was not found
230 // return a pointer to the SGSoundSample if the specified sound exists
231 // in the sound manager system, otherwise return NULL
232 SGSoundSample *SGSampleGroup::find( const string &refname ) {
233 sample_map_iterator sample_it = _samples.find( refname );
234 if ( sample_it == _samples.end() ) {
235 // sample was not found
239 return sample_it->second;
243 // stop playing all associated samples
245 SGSampleGroup::suspend ()
248 sample_map_iterator sample_current = _samples.begin();
249 sample_map_iterator sample_end = _samples.end();
250 for ( ; sample_current != sample_end; ++sample_current ) {
251 SGSoundSample *sample = sample_current->second;
253 if ( sample->is_valid_source() && sample->is_playing() ) {
254 unsigned int source = sample->get_source();
255 alSourcePause( source );
258 testForALError("suspend");
261 // resume playing all associated samples
263 SGSampleGroup::resume ()
265 sample_map_iterator sample_current = _samples.begin();
266 sample_map_iterator sample_end = _samples.end();
267 for ( ; sample_current != sample_end; ++sample_current ) {
268 SGSoundSample *sample = sample_current->second;
270 if ( sample->is_valid_source() && sample->is_playing() ) {
271 unsigned int source = sample->get_source();
272 alSourcePlay( 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 ) {
317 // set source position of all managed sounds
318 void SGSampleGroup::set_position( SGVec3d pos ) {
319 if ( isnan(pos.data()[0]) || isnan(pos.data()[1]) || isnan(pos.data()[2]) )
321 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup postion");
325 sample_map_iterator sample_current = _samples.begin();
326 sample_map_iterator sample_end = _samples.end();
327 for ( ; sample_current != sample_end; ++sample_current ) {
328 SGSoundSample *sample = sample_current->second;
329 sample->set_base_position( pos );
333 // set source velocity of all managed sounds
334 void SGSampleGroup::set_velocity( SGVec3f vel ) {
335 if ( isnan(vel.data()[0]) || isnan(vel.data()[1]) || isnan(vel.data()[2]) )
337 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
341 sample_map_iterator sample_current = _samples.begin();
342 sample_map_iterator sample_end = _samples.end();
343 for ( ; sample_current != sample_end; ++sample_current ) {
344 SGSoundSample *sample = sample_current->second;
345 sample->set_velocity( vel );
349 // ste the source orientation of all managed sounds
350 void SGSampleGroup::set_orientation( SGVec3f ori ) {
351 if ( isnan(ori.data()[0]) || isnan(ori.data()[1]) || isnan(ori.data()[2]) )
353 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup orientation");
357 sample_map_iterator sample_current = _samples.begin();
358 sample_map_iterator sample_end = _samples.end();
359 for ( ; sample_current != sample_end; ++sample_current ) {
360 SGSoundSample *sample = sample_current->second;
361 sample->set_orientation( ori );
365 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
366 if ( sample->is_valid_source() ) {
367 unsigned int source = sample->get_source();
369 alSourcefv( source, AL_POSITION, sample->get_position());
370 alSourcefv( source, AL_DIRECTION, sample->get_direction() );
371 alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
372 testForALError("position and orientation");
374 alSourcef( source, AL_PITCH, sample->get_pitch() );
375 alSourcef( source, AL_GAIN, sample->get_volume() );
376 testForALError("pitch and gain");
378 if ( sample->has_static_data_changed() ) {
379 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
380 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
381 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
382 testForALError("audio cone");
384 alSourcef( source, AL_ROLLOFF_FACTOR, 1.0 );
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");
394 SGSampleGroup::load_file(SGSoundSample *sample) {
395 if (sample->is_file()) {
400 string sample_name = sample->get_sample_name();
401 _smgr->load(sample_name, &data, &format, &size, &freq);
403 sample->set_data( (unsigned char *)data );
404 sample->set_frequency( freq );
405 sample->set_format( format );
406 sample->set_size( size );
410 void SGSampleGroup::set_volume( float vol )
413 if (_volume < 0.0) _volume = 0.0;
414 if (_volume > 1.0) _volume = 1.0;
416 sample_map_iterator sample_current = _samples.begin();
417 sample_map_iterator sample_end = _samples.end();
418 for ( ; sample_current != sample_end; ++sample_current ) {
419 SGSoundSample *sample = sample_current->second;
420 sample->set_master_volume( _volume );
424 bool SGSampleGroup::testForError(void *p, string s)
427 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
433 bool SGSampleGroup::testForALError(string s)
435 ALenum error = alGetError();
436 if (error != AL_NO_ERROR) {
437 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (sample group): "
438 << alGetString(error) << " at " << s);