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();
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() );
87 void SGSampleGroup::update( double dt ) {
89 if ( !_active ) return;
91 // testForALError("start of update!!\n");
93 sample_map_iterator sample_current = _samples.begin();
94 sample_map_iterator sample_end = _samples.end();
95 for ( ; sample_current != sample_end; ++sample_current ) {
96 SGSoundSample *sample = sample_current->second;
98 if ( !sample->is_valid_source() && sample->is_playing() ) {
100 // a request to start playing a sound has been filed.
102 ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE;
104 if ( !sample->is_valid_buffer() ) {
105 // sample was not yet loaded or removed again
107 // TODO: Create a buffer cache that checks whether a file is already present
108 // as an OpenAL buffer since buffers can be shared among sources.
110 if ( testForALError("load sample") ) {
111 throw sg_exception("Failed to load sound sample.");
115 // create an OpenAL buffer handle
117 alGenBuffers(1, &buffer);
118 if ( testForALError("generate buffer") ) {
119 throw sg_exception("Failed to generate OpenAL buffer.");
123 // Copy data to the internal OpenAL buffer
124 const ALvoid *data = sample->get_data();
125 ALenum format = sample->get_format();
126 ALsizei size = sample->get_size();
127 ALsizei freq = sample->get_frequency();
128 alBufferData( buffer, format, data, size, freq );
130 if ( testForALError("buffer add data") ) {
134 sample->set_buffer(buffer);
137 // start playing the sample
138 ALuint buffer = sample->get_buffer();
139 ALuint source = _smgr->request_source();
140 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
142 sample->set_source( source );
144 alSourcei( source, AL_BUFFER, buffer );
145 testForALError("assign buffer to source");
147 sample->set_source( source );
148 update_sample_config( sample );
150 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
151 alSourcei( source, AL_LOOPING, looping );
152 alSourcef( source, AL_ROLLOFF_FACTOR, 1.2 )
153 alSourcePlay( source );
154 testForALError("sample play");
156 if (alIsBuffer(buffer) == AL_FALSE)
157 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
158 // sample->no_valid_source();
159 // sadly, no free source available at this time
162 } else if ( sample->is_valid_source() && sample->has_changed() ) {
163 if ( !sample->is_playing() ) {
164 // a request to stop playing the sound has been filed.
166 sample->no_valid_source();
168 _smgr->release_source( sample->get_source() );
170 update_sample_config( sample );
173 } else if ( sample->is_valid_source() ) {
174 // check if the sound has stopped by itself
176 unsigned int source = sample->get_source();
179 alGetSourcei( source, AL_SOURCE_STATE, &result );
180 if ( result == AL_STOPPED ) {
181 // sample is stoped because it wasn't looping
182 sample->no_valid_source();
184 _smgr->release_source( source );
188 testForALError("update");
192 // add a sound effect, return true if successful
193 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
195 sample_map_iterator sample_it = _samples.find( refname );
196 if ( sample_it != _samples.end() ) {
197 // sample name already exists
201 _samples[refname] = sound;
206 // remove a sound effect, return true if successful
207 bool SGSampleGroup::remove( const string &refname ) {
209 sample_map_iterator sample_it = _samples.find( refname );
210 if ( sample_it == _samples.end() ) {
211 // sample was not found
215 _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 unsigned int source = sample->get_source();
257 alSourcePause( source );
260 testForALError("suspend");
263 // resume playing all associated samples
265 SGSampleGroup::resume ()
267 sample_map_iterator sample_current = _samples.begin();
268 sample_map_iterator sample_end = _samples.end();
269 for ( ; sample_current != sample_end; ++sample_current ) {
270 SGSoundSample *sample = sample_current->second;
272 if ( sample->is_valid_source() && sample->is_playing() ) {
273 unsigned int source = sample->get_source();
274 alSourcePlay( source );
277 testForALError("resume");
282 // tell the scheduler to play the indexed sample in a continuous loop
283 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
284 SGSoundSample *sample = find( refname );
286 if ( sample == NULL ) {
290 sample->play( looping );
295 // return true of the specified sound is currently being played
296 bool SGSampleGroup::is_playing( const string& refname ) {
297 SGSoundSample *sample = find( refname );
299 if ( sample == NULL ) {
303 return ( sample->is_playing() ) ? true : false;
306 // immediate stop playing the sound
307 bool SGSampleGroup::stop( const string& refname ) {
308 SGSoundSample *sample = find( refname );
310 if ( sample == NULL ) {
319 // set source position of all managed sounds
320 void SGSampleGroup::set_position( SGVec3d pos ) {
321 if ( isnan(pos.data()[0]) || isnan(pos.data()[1]) || isnan(pos.data()[2]) )
323 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup postion");
327 sample_map_iterator sample_current = _samples.begin();
328 sample_map_iterator sample_end = _samples.end();
329 for ( ; sample_current != sample_end; ++sample_current ) {
330 SGSoundSample *sample = sample_current->second;
331 sample->set_base_position( pos );
335 // set source velocity of all managed sounds
336 void SGSampleGroup::set_velocity( SGVec3f vel ) {
337 if ( isnan(vel.data()[0]) || isnan(vel.data()[1]) || isnan(vel.data()[2]) )
339 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
343 sample_map_iterator sample_current = _samples.begin();
344 sample_map_iterator sample_end = _samples.end();
345 for ( ; sample_current != sample_end; ++sample_current ) {
346 SGSoundSample *sample = sample_current->second;
347 sample->set_velocity( vel );
351 // ste the source orientation of all managed sounds
352 void SGSampleGroup::set_orientation( SGVec3f ori ) {
353 if ( isnan(ori.data()[0]) || isnan(ori.data()[1]) || isnan(ori.data()[2]) )
355 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup orientation");
359 sample_map_iterator sample_current = _samples.begin();
360 sample_map_iterator sample_end = _samples.end();
361 for ( ; sample_current != sample_end; ++sample_current ) {
362 SGSoundSample *sample = sample_current->second;
363 sample->set_orientation( ori );
367 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
368 if ( sample->is_valid_source() ) {
369 unsigned int source = sample->get_source();
371 alSourcefv( source, AL_POSITION, sample->get_position());
372 alSourcefv( source, AL_DIRECTION, sample->get_direction() );
373 alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
374 testForALError("position and orientation");
376 alSourcef( source, AL_PITCH, sample->get_pitch() );
377 alSourcef( source, AL_GAIN, sample->get_volume() );
378 testForALError("pitch and gain");
380 if ( sample->has_static_data_changed() ) {
381 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
382 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
383 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
384 testForALError("audio cone");
386 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
387 alSourcef( source, AL_REFERENCE_DISTANCE,
388 sample->get_reference_dist() );
389 testForALError("distance rolloff");
395 SGSampleGroup::load_file(SGSoundSample *sample) {
396 if (sample->is_file()) {
401 string sample_name = sample->get_sample_name();
402 _smgr->load(sample_name, &data, &format, &size, &freq);
404 sample->set_data( (unsigned char *)data );
405 sample->set_frequency( freq );
406 sample->set_format( format );
407 sample->set_size( size );
411 void SGSampleGroup::set_volume( float vol )
414 if (_volume < 0.0) _volume = 0.0;
415 if (_volume > 1.0) _volume = 1.0;
417 sample_map_iterator sample_current = _samples.begin();
418 sample_map_iterator sample_end = _samples.end();
419 for ( ; sample_current != sample_end; ++sample_current ) {
420 SGSoundSample *sample = sample_current->second;
421 sample->set_master_volume( _volume );
425 bool SGSampleGroup::testForError(void *p, string s)
428 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
434 bool SGSampleGroup::testForALError(string s)
436 ALenum error = alGetError();
437 if (error != AL_NO_ERROR) {
438 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
439 << alGetString(error) << " at " << s);