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() );
85 void SGSampleGroup::update( double dt ) {
87 if ( !_active ) return;
89 // testForALError("start of update!!\n");
91 sample_map_iterator sample_current = _samples.begin();
92 sample_map_iterator sample_end = _samples.end();
93 for ( ; sample_current != sample_end; ++sample_current ) {
94 SGSoundSample *sample = sample_current->second;
96 if ( !sample->is_valid_source() && sample->is_playing() ) {
98 // a request to start playing a sound has been filed.
100 ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE;
102 if ( !sample->is_valid_buffer() ) {
103 // sample was not yet loaded or removed again
105 // TODO: Create a buffer cache that checks whether a file is already present
106 // as an OpenAL buffer since buffers can be shared among sources.
108 if ( testForALError("load sample") ) {
109 throw sg_exception("Failed to load sound sample.");
113 // create an OpenAL buffer handle
115 alGenBuffers(1, &buffer);
116 if ( testForALError("generate buffer") ) {
117 throw sg_exception("Failed to generate OpenAL buffer.");
121 // Copy data to the internal OpenAL buffer
122 const ALvoid *data = sample->get_data();
123 ALenum format = sample->get_format();
124 ALsizei size = sample->get_size();
125 ALsizei freq = sample->get_frequency();
126 alBufferData( buffer, format, data, size, freq );
128 if ( testForALError("buffer add data") ) {
132 sample->set_buffer(buffer);
135 if ( _tied_to_listener && _smgr->has_changed() ) {
136 sample->set_base_position( _smgr->get_position_vec() );
137 sample->set_orientation( _smgr->get_direction() );
138 sample->set_velocity( _smgr->get_velocity() );
141 // start playing the sample
142 ALuint buffer = sample->get_buffer();
143 ALuint source = _smgr->request_source();
144 if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
146 sample->set_source( source );
148 alSourcei( source, AL_BUFFER, buffer );
149 testForALError("assign buffer to source");
151 sample->set_source( source );
152 update_sample_config( sample );
154 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
155 alSourcei( source, AL_LOOPING, looping );
156 alSourcef( source, AL_ROLLOFF_FACTOR, 1.2 );
157 alSourcePlay( source );
158 testForALError("sample play");
160 if (alIsBuffer(buffer) == AL_FALSE)
161 SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
162 // sample->no_valid_source();
163 // sadly, no free source available at this time
166 } else if ( sample->is_valid_source() && sample->has_changed() ) {
167 if ( !sample->is_playing() ) {
168 // a request to stop playing the sound has been filed.
170 sample->no_valid_source();
172 _smgr->release_source( sample->get_source() );
174 update_sample_config( sample );
177 } else if ( sample->is_valid_source() ) {
178 // check if the sound has stopped by itself
180 unsigned int source = sample->get_source();
183 alGetSourcei( source, AL_SOURCE_STATE, &result );
184 if ( result == AL_STOPPED ) {
185 // sample is stoped because it wasn't looping
186 sample->no_valid_source();
188 _smgr->release_source( source );
192 testForALError("update");
196 // add a sound effect, return true if successful
197 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
199 sample_map_iterator sample_it = _samples.find( refname );
200 if ( sample_it != _samples.end() ) {
201 // sample name already exists
205 _samples[refname] = sound;
210 // remove a sound effect, return true if successful
211 bool SGSampleGroup::remove( const string &refname ) {
213 sample_map_iterator sample_it = _samples.find( refname );
214 if ( sample_it == _samples.end() ) {
215 // sample was not found
219 _samples.erase( sample_it );
224 // return true of the specified sound exists in the sound manager system
225 bool SGSampleGroup::exists( const string &refname ) {
226 sample_map_iterator sample_it = _samples.find( refname );
227 if ( sample_it == _samples.end() ) {
228 // sample was not found
236 // return a pointer to the SGSoundSample if the specified sound exists
237 // in the sound manager system, otherwise return NULL
238 SGSoundSample *SGSampleGroup::find( const string &refname ) {
239 sample_map_iterator sample_it = _samples.find( refname );
240 if ( sample_it == _samples.end() ) {
241 // sample was not found
245 return sample_it->second;
249 // stop playing all associated samples
251 SGSampleGroup::suspend ()
254 sample_map_iterator sample_current = _samples.begin();
255 sample_map_iterator sample_end = _samples.end();
256 for ( ; sample_current != sample_end; ++sample_current ) {
257 SGSoundSample *sample = sample_current->second;
259 if ( sample->is_valid_source() && sample->is_playing() ) {
260 unsigned int source = sample->get_source();
261 alSourcePause( source );
264 testForALError("suspend");
267 // resume playing all associated samples
269 SGSampleGroup::resume ()
271 sample_map_iterator sample_current = _samples.begin();
272 sample_map_iterator sample_end = _samples.end();
273 for ( ; sample_current != sample_end; ++sample_current ) {
274 SGSoundSample *sample = sample_current->second;
276 if ( sample->is_valid_source() && sample->is_playing() ) {
277 unsigned int source = sample->get_source();
278 alSourcePlay( source );
281 testForALError("resume");
286 // tell the scheduler to play the indexed sample in a continuous loop
287 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
288 SGSoundSample *sample = find( refname );
290 if ( sample == NULL ) {
294 sample->play( looping );
299 // return true of the specified sound is currently being played
300 bool SGSampleGroup::is_playing( const string& refname ) {
301 SGSoundSample *sample = find( refname );
303 if ( sample == NULL ) {
307 return ( sample->is_playing() ) ? true : false;
310 // immediate stop playing the sound
311 bool SGSampleGroup::stop( const string& refname ) {
312 SGSoundSample *sample = find( refname );
314 if ( sample == NULL ) {
323 // set source position of all managed sounds
324 void SGSampleGroup::set_position( SGVec3d pos ) {
325 if ( isnan(pos.data()[0]) || isnan(pos.data()[1]) || isnan(pos.data()[2]) )
327 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup postion");
331 if ( !_tied_to_listener ) {
332 sample_map_iterator sample_current = _samples.begin();
333 sample_map_iterator sample_end = _samples.end();
334 for ( ; sample_current != sample_end; ++sample_current ) {
335 SGSoundSample *sample = sample_current->second;
336 sample->set_base_position( pos );
341 // set source velocity of all managed sounds
342 void SGSampleGroup::set_velocity( SGVec3f vel ) {
343 if ( isnan(vel.data()[0]) || isnan(vel.data()[1]) || isnan(vel.data()[2]) )
345 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
349 sample_map_iterator sample_current = _samples.begin();
350 sample_map_iterator sample_end = _samples.end();
351 for ( ; sample_current != sample_end; ++sample_current ) {
352 SGSoundSample *sample = sample_current->second;
353 sample->set_velocity( vel );
357 // ste the source orientation of all managed sounds
358 void SGSampleGroup::set_orientation( SGVec3f ori ) {
359 if ( isnan(ori.data()[0]) || isnan(ori.data()[1]) || isnan(ori.data()[2]) )
361 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup orientation");
365 sample_map_iterator sample_current = _samples.begin();
366 sample_map_iterator sample_end = _samples.end();
367 for ( ; sample_current != sample_end; ++sample_current ) {
368 SGSoundSample *sample = sample_current->second;
369 sample->set_orientation( ori );
373 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
374 if ( sample->is_valid_source() ) {
375 unsigned int source = sample->get_source();
377 alSourcefv( source, AL_POSITION, sample->get_position());
378 alSourcefv( source, AL_DIRECTION, sample->get_direction() );
379 alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
380 testForALError("position and orientation");
382 alSourcef( source, AL_PITCH, sample->get_pitch() );
383 alSourcef( source, AL_GAIN, sample->get_volume() );
384 testForALError("pitch and gain");
386 if ( sample->has_static_data_changed() ) {
387 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
388 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
389 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
390 testForALError("audio cone");
392 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
393 alSourcef( source, AL_REFERENCE_DISTANCE,
394 sample->get_reference_dist() );
395 testForALError("distance rolloff");
401 SGSampleGroup::load_file(SGSoundSample *sample) {
402 if (sample->is_file()) {
407 string sample_name = sample->get_sample_name();
408 _smgr->load(sample_name, &data, &format, &size, &freq);
410 sample->set_data( (unsigned char *)data );
411 sample->set_frequency( freq );
412 sample->set_format( format );
413 sample->set_size( size );
417 void SGSampleGroup::set_volume( float vol )
420 if (_volume < 0.0) _volume = 0.0;
421 if (_volume > 1.0) _volume = 1.0;
423 sample_map_iterator sample_current = _samples.begin();
424 sample_map_iterator sample_end = _samples.end();
425 for ( ; sample_current != sample_end; ++sample_current ) {
426 SGSoundSample *sample = sample_current->second;
427 sample->set_master_volume( _volume );
431 bool SGSampleGroup::testForError(void *p, string s)
434 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
440 bool SGSampleGroup::testForALError(string s)
442 ALenum error = alGetError();
443 if (error != AL_NO_ERROR) {
444 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
445 << alGetString(error) << " at " << s);