]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.cxx
fix a typo
[simgear.git] / simgear / sound / sample_group.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <simgear_config.h>
3 #endif
4
5 #include <simgear/compiler.h>
6
7 #if defined (__APPLE__)
8 #  ifdef __GNUC__
9 #    if ( __GNUC__ >= 3 ) && ( __GNUC_MINOR__ >= 3 )
10 //  #        include <math.h>
11 inline int (isnan)(double r) { return !(r <= 0 || r >= 0); }
12 #    else
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);
18 #    endif
19 #  else
20 //    inline int (isinf)(double r) { return isinf(r); }
21 //    inline int (isnan)(double r) { return isnan(r); }
22 #  endif
23 #endif
24
25 #if defined (__FreeBSD__)
26 #  if __FreeBSD_version < 500000
27      extern "C" {
28        inline int isnan(double r) { return !(r <= 0 || r >= 0); }
29      }
30 #  endif
31 #endif
32
33 #if defined (__CYGWIN__)
34 #  include <ieeefp.h>
35 #endif
36
37 #if defined(__MINGW32__)
38 #  define isnan(x) _isnan(x)
39 #endif
40
41 #include "soundmgr_openal.hxx"
42 #include "sample_group.hxx"
43
44 SGSampleGroup::SGSampleGroup () :
45     _smgr(NULL),
46     _active(false),
47     _changed(true),
48     _position_changed(true),
49     _position(SGVec3d::zeros().data()),
50     _orientation(SGVec3f::zeros().data())
51 {
52     _samples.clear();
53 }
54
55 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
56     _smgr(smgr),
57     _active(false), 
58     _changed(true),
59     _position_changed(true),
60     _position(SGVec3d::zeros().data()),
61     _orientation(SGVec3f::zeros().data())
62 {
63     _smgr->add(this, refname);
64     _active = _smgr->is_working();
65     _refname = refname;
66     _samples.clear();
67 }
68
69 SGSampleGroup::~SGSampleGroup ()
70 {
71     _active = false;
72
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;
77
78         if ( sample->is_valid_source() && sample->is_playing() ) {
79             sample->no_valid_source();
80             _smgr->release_source( sample->get_source() );
81         }
82     }
83
84     _smgr = 0;
85 }
86
87 void SGSampleGroup::update( double dt ) {
88
89     if ( !_active ) return;
90
91     // testForALError("start of update!!\n");
92
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;
97
98         if ( !sample->is_valid_source() && sample->is_playing() ) {
99             //
100             // a request to start playing a sound has been filed.
101             //
102             ALboolean looping = sample->get_looping() ? AL_TRUE : AL_FALSE;
103
104             if ( !sample->is_valid_buffer() ) {
105                 // sample was not yet loaded or removed again
106
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.
109                 load_file(sample);
110                 if ( testForALError("load sample") ) {
111                     throw sg_exception("Failed to load sound sample.");
112                     continue;
113                 }
114
115                 // create an OpenAL buffer handle
116                 ALuint buffer;
117                 alGenBuffers(1, &buffer);
118                 if ( testForALError("generate buffer") ) {
119                     throw sg_exception("Failed to generate OpenAL buffer.");
120                     continue;
121                 }
122
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 );
129                 sample->free_data();
130                 if ( testForALError("buffer add data") ) {
131                     continue;
132                 }
133
134                 sample->set_buffer(buffer);
135             }
136
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)
141             {
142                 sample->set_source( source );
143                 
144                 alSourcei( source, AL_BUFFER, buffer );
145                 testForALError("assign buffer to source");
146
147                 sample->set_source( source );
148                 update_sample_config( sample );
149
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");
155             } else {
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
160             }
161
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.
165
166                 sample->no_valid_source();
167                 sample->stop();
168                 _smgr->release_source( sample->get_source() );
169             } else  {
170                 update_sample_config( sample );
171             }
172
173         } else if ( sample->is_valid_source() ) {
174             // check if the sound has stopped by itself
175
176             unsigned int source = sample->get_source();
177             int result;
178
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();
183                 sample->stop();
184                 _smgr->release_source( source );
185
186             }
187         }
188         testForALError("update");
189     }
190 }
191
192 // add a sound effect, return true if successful
193 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
194
195     sample_map_iterator sample_it = _samples.find( refname );
196     if ( sample_it != _samples.end() ) {
197         // sample name already exists
198         return false;
199     }
200
201     _samples[refname] = sound;
202     return true;
203 }
204
205
206 // remove a sound effect, return true if successful
207 bool SGSampleGroup::remove( const string &refname ) {
208
209     sample_map_iterator sample_it = _samples.find( refname );
210     if ( sample_it == _samples.end() ) {
211         // sample was not found
212         return false;
213     }
214
215     _samples.erase( sample_it );
216     return true;
217 }
218
219
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
225         return false;
226     }
227
228     return true;
229 }
230
231
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
238         return NULL;
239     }
240
241     return sample_it->second;
242 }
243
244
245 // stop playing all associated samples
246 void
247 SGSampleGroup::suspend ()
248 {
249     _active = false;
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;
254
255         if ( sample->is_valid_source() && sample->is_playing() ) {
256             unsigned int source = sample->get_source();
257             alSourcePause( source );
258         }
259     }
260     testForALError("suspend");
261 }
262
263 // resume playing all associated samples
264 void
265 SGSampleGroup::resume ()
266 {
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;
271
272         if ( sample->is_valid_source() && sample->is_playing() ) {
273             unsigned int source = sample->get_source();
274             alSourcePlay( source );
275         }
276     }
277     testForALError("resume");
278     _active = true;
279 }
280
281
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 );
285
286     if ( sample == NULL ) {
287         return false;
288     }
289
290     sample->play( looping );
291     return true;
292 }
293
294
295 // return true of the specified sound is currently being played
296 bool SGSampleGroup::is_playing( const string& refname ) {
297     SGSoundSample *sample = find( refname );
298
299     if ( sample == NULL ) {
300         return false;
301     }
302
303     return ( sample->is_playing() ) ? true : false;
304 }
305
306 // immediate stop playing the sound
307 bool SGSampleGroup::stop( const string& refname ) {
308     SGSoundSample *sample  = find( refname );
309
310     if ( sample == NULL ) {
311         return false;
312     }
313
314     sample->stop();
315     return true;
316 }
317
318
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]) )
322     {
323         SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup postion");
324         return;
325     }
326
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 );
332     }
333 }
334
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]) )
338     {
339         SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
340         return;
341     }
342
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 );
348     }
349 }
350
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]) )
354     {
355         SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup orientation");
356         return;
357     }
358
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 );
364     }
365 }
366
367 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
368     if ( sample->is_valid_source() ) {
369         unsigned int source = sample->get_source();
370
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");
375
376         alSourcef( source, AL_PITCH, sample->get_pitch() );
377         alSourcef( source, AL_GAIN, sample->get_volume() );
378         testForALError("pitch and gain");
379
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");
385
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");
390         }
391     }
392 }
393
394 ALvoid
395 SGSampleGroup::load_file(SGSoundSample *sample) {
396     if (sample->is_file()) {
397         unsigned int size;
398         int freq, format;
399         void *data;
400
401         string sample_name = sample->get_sample_name();
402         _smgr->load(sample_name, &data, &format, &size, &freq);
403
404         sample->set_data( (unsigned char *)data );
405         sample->set_frequency( freq );
406         sample->set_format( format );
407         sample->set_size( size );
408     }
409 }
410
411 void SGSampleGroup::set_volume( float vol )
412 {
413     _volume = vol;
414     if (_volume < 0.0) _volume = 0.0;
415     if (_volume > 1.0) _volume = 1.0;
416
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 );
422     }
423 }
424
425 bool SGSampleGroup::testForError(void *p, string s)
426 {
427    if (p == NULL) {
428       SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
429       return true;
430    }
431    return false;
432 }
433
434 bool SGSampleGroup::testForALError(string s)
435 {
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);
440        return true;
441     }
442     return false;
443 }
444