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 alSourcePlay( source );
153 testForALError("sample play");
155 if (alIsBuffer(buffer) == AL_FALSE)
156 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
157 // sample->no_valid_source();
158 // sadly, no free source available at this time
161 } else if ( sample->is_valid_source() && sample->has_changed() ) {
162 if ( !sample->is_playing() ) {
163 // a request to stop playing the sound has been filed.
165 sample->no_valid_source();
167 _smgr->release_source( sample->get_source() );
169 update_sample_config( sample );
172 } else if ( sample->is_valid_source() ) {
173 // check if the sound has stopped by itself
175 unsigned int source = sample->get_source();
178 alGetSourcei( source, AL_SOURCE_STATE, &result );
179 if ( result == AL_STOPPED ) {
180 // sample is stoped because it wasn't looping
181 sample->no_valid_source();
183 _smgr->release_source( source );
187 testForALError("update");
191 // add a sound effect, return true if successful
192 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
194 sample_map_iterator sample_it = _samples.find( refname );
195 if ( sample_it != _samples.end() ) {
196 // sample name already exists
200 _samples[refname] = sound;
205 // remove a sound effect, return true if successful
206 bool SGSampleGroup::remove( const string &refname ) {
208 sample_map_iterator sample_it = _samples.find( refname );
209 if ( sample_it == _samples.end() ) {
210 // sample was not found
214 _samples.erase( sample_it );
219 // return true of the specified sound exists in the sound manager system
220 bool SGSampleGroup::exists( const string &refname ) {
221 sample_map_iterator sample_it = _samples.find( refname );
222 if ( sample_it == _samples.end() ) {
223 // sample was not found
231 // return a pointer to the SGSoundSample if the specified sound exists
232 // in the sound manager system, otherwise return NULL
233 SGSoundSample *SGSampleGroup::find( const string &refname ) {
234 sample_map_iterator sample_it = _samples.find( refname );
235 if ( sample_it == _samples.end() ) {
236 // sample was not found
240 return sample_it->second;
244 // stop playing all associated samples
246 SGSampleGroup::suspend ()
249 sample_map_iterator sample_current = _samples.begin();
250 sample_map_iterator sample_end = _samples.end();
251 for ( ; sample_current != sample_end; ++sample_current ) {
252 SGSoundSample *sample = sample_current->second;
254 if ( sample->is_valid_source() && sample->is_playing() ) {
255 unsigned int source = sample->get_source();
256 alSourcePause( source );
259 testForALError("suspend");
262 // resume playing all associated samples
264 SGSampleGroup::resume ()
266 sample_map_iterator sample_current = _samples.begin();
267 sample_map_iterator sample_end = _samples.end();
268 for ( ; sample_current != sample_end; ++sample_current ) {
269 SGSoundSample *sample = sample_current->second;
271 if ( sample->is_valid_source() && sample->is_playing() ) {
272 unsigned int source = sample->get_source();
273 alSourcePlay( source );
276 testForALError("resume");
281 // tell the scheduler to play the indexed sample in a continuous loop
282 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
283 SGSoundSample *sample = find( refname );
285 if ( sample == NULL ) {
289 sample->play( looping );
294 // return true of the specified sound is currently being played
295 bool SGSampleGroup::is_playing( const string& refname ) {
296 SGSoundSample *sample = find( refname );
298 if ( sample == NULL ) {
302 return ( sample->is_playing() ) ? true : false;
305 // immediate stop playing the sound
306 bool SGSampleGroup::stop( const string& refname ) {
307 SGSoundSample *sample = find( refname );
309 if ( sample == NULL ) {
318 // set source position of all managed sounds
319 void SGSampleGroup::set_position( SGVec3d pos ) {
320 if ( isnan(pos.data()[0]) || isnan(pos.data()[1]) || isnan(pos.data()[2]) )
322 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup postion");
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_base_position( pos );
334 // set source velocity of all managed sounds
335 void SGSampleGroup::set_velocity( SGVec3f vel ) {
336 if ( isnan(vel.data()[0]) || isnan(vel.data()[1]) || isnan(vel.data()[2]) )
338 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
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_velocity( vel );
350 // ste the source orientation of all managed sounds
351 void SGSampleGroup::set_orientation( SGVec3f ori ) {
352 if ( isnan(ori.data()[0]) || isnan(ori.data()[1]) || isnan(ori.data()[2]) )
354 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup orientation");
358 sample_map_iterator sample_current = _samples.begin();
359 sample_map_iterator sample_end = _samples.end();
360 for ( ; sample_current != sample_end; ++sample_current ) {
361 SGSoundSample *sample = sample_current->second;
362 sample->set_orientation( ori );
366 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
367 if ( sample->is_valid_source() ) {
368 unsigned int source = sample->get_source();
370 alSourcefv( source, AL_POSITION, sample->get_position());
371 alSourcefv( source, AL_DIRECTION, sample->get_direction() );
372 alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
373 testForALError("position and orientation");
375 alSourcef( source, AL_PITCH, sample->get_pitch() );
376 alSourcef( source, AL_GAIN, sample->get_volume() );
377 testForALError("pitch and gain");
379 if ( sample->has_static_data_changed() ) {
380 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
381 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
382 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
383 testForALError("audio cone");
385 alSourcef( source, AL_ROLLOFF_FACTOR, 1.0 );
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);