]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.cxx
Rename update() to update_late() for the sound manager to be able to initialize it...
[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                 alSourcePlay( source );
153                 testForALError("sample play");
154             } else {
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
159             }
160
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.
164
165                 sample->no_valid_source();
166                 sample->stop();
167                 _smgr->release_source( sample->get_source() );
168             } else  {
169                 update_sample_config( sample );
170             }
171
172         } else if ( sample->is_valid_source() ) {
173             // check if the sound has stopped by itself
174
175             unsigned int source = sample->get_source();
176             int result;
177
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();
182                 sample->stop();
183                 _smgr->release_source( source );
184
185             }
186         }
187         testForALError("update");
188     }
189 }
190
191 // add a sound effect, return true if successful
192 bool SGSampleGroup::add( SGSoundSample *sound, const string& refname ) {
193
194     sample_map_iterator sample_it = _samples.find( refname );
195     if ( sample_it != _samples.end() ) {
196         // sample name already exists
197         return false;
198     }
199
200     _samples[refname] = sound;
201     return true;
202 }
203
204
205 // remove a sound effect, return true if successful
206 bool SGSampleGroup::remove( const string &refname ) {
207
208     sample_map_iterator sample_it = _samples.find( refname );
209     if ( sample_it == _samples.end() ) {
210         // sample was not found
211         return false;
212     }
213
214     _samples.erase( sample_it );
215     return true;
216 }
217
218
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
224         return false;
225     }
226
227     return true;
228 }
229
230
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
237         return NULL;
238     }
239
240     return sample_it->second;
241 }
242
243
244 // stop playing all associated samples
245 void
246 SGSampleGroup::suspend ()
247 {
248     _active = false;
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;
253
254         if ( sample->is_valid_source() && sample->is_playing() ) {
255             unsigned int source = sample->get_source();
256             alSourcePause( source );
257         }
258     }
259     testForALError("suspend");
260 }
261
262 // resume playing all associated samples
263 void
264 SGSampleGroup::resume ()
265 {
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;
270
271         if ( sample->is_valid_source() && sample->is_playing() ) {
272             unsigned int source = sample->get_source();
273             alSourcePlay( source );
274         }
275     }
276     testForALError("resume");
277     _active = true;
278 }
279
280
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 );
284
285     if ( sample == NULL ) {
286         return false;
287     }
288
289     sample->play( looping );
290     return true;
291 }
292
293
294 // return true of the specified sound is currently being played
295 bool SGSampleGroup::is_playing( const string& refname ) {
296     SGSoundSample *sample = find( refname );
297
298     if ( sample == NULL ) {
299         return false;
300     }
301
302     return ( sample->is_playing() ) ? true : false;
303 }
304
305 // immediate stop playing the sound
306 bool SGSampleGroup::stop( const string& refname ) {
307     SGSoundSample *sample  = find( refname );
308
309     if ( sample == NULL ) {
310         return false;
311     }
312
313     sample->stop();
314     return true;
315 }
316
317
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]) )
321     {
322         SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup postion");
323         return;
324     }
325
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 );
331     }
332 }
333
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]) )
337     {
338         SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
339         return;
340     }
341
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 );
347     }
348 }
349
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]) )
353     {
354         SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup orientation");
355         return;
356     }
357
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 );
363     }
364 }
365
366 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
367     if ( sample->is_valid_source() ) {
368         unsigned int source = sample->get_source();
369
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");
374
375         alSourcef( source, AL_PITCH, sample->get_pitch() );
376         alSourcef( source, AL_GAIN, sample->get_volume() );
377         testForALError("pitch and gain");
378
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");
384
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");
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