]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.cxx
Improve logging classes.
[simgear.git] / simgear / sound / sample_group.cxx
1 // sample_group.cxx -- Manage a group of samples relative to a base position
2 //
3 // Written for the new SoundSystem by Erik Hofman, October 2009
4 //
5 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software Foundation,
19 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include "soundmgr_openal.hxx"
30 #include "sample_group.hxx"
31
32 bool isNaN(float *v) {
33    return (isnan(v[0]) || isnan(v[1]) || isnan(v[2]));
34 }
35
36 SGSampleGroup::SGSampleGroup () :
37     _smgr(NULL),
38     _refname(""),
39     _active(false),
40     _changed(false),
41     _pause(false),
42     _volume(1.0),
43     _tied_to_listener(false),
44     _velocity(SGVec3d::zeros()),
45     _orientation(SGQuatd::zeros())
46 {
47     _samples.clear();
48 }
49
50 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
51     _smgr(smgr),
52     _refname(refname),
53     _active(false), 
54     _changed(false),
55     _pause(false),
56     _volume(1.0),
57     _tied_to_listener(false),
58     _velocity(SGVec3d::zeros()),
59     _orientation(SGQuatd::zeros())
60 {
61     _smgr->add(this, refname);
62     _samples.clear();
63 }
64
65 SGSampleGroup::~SGSampleGroup ()
66 {
67     _active = false;
68
69     sample_map_iterator sample_current = _samples.begin();
70     sample_map_iterator sample_end = _samples.end();
71     for ( ; sample_current != sample_end; ++sample_current ) {
72         SGSoundSample *sample = sample_current->second;
73
74         if ( sample->is_valid_source() && sample->is_playing() ) {
75             sample->no_valid_source();
76             _smgr->release_source( sample->get_source() );
77             _smgr->release_buffer( sample );
78         }
79     }
80
81     _smgr = 0;
82 }
83
84 void SGSampleGroup::update( double dt ) {
85
86     if ( !_active || _pause ) return;
87
88     testForALError("start of update!!\n");
89
90     // Delete any OpenAL buffers that might still be in use.
91     unsigned int size = _removed_samples.size();
92     for (unsigned int i=0; i<size; ) {
93         SGSoundSample *sample = _removed_samples[i];
94         ALint result = AL_STOPPED;
95
96         if ( sample->is_valid_source() ) {
97             int source = sample->get_source();
98
99             alGetSourcei( source, AL_SOURCE_STATE, &result );
100             if ( sample->is_looping() ) {
101                 if ( result != AL_STOPPED ) {
102                     alSourceStop( source );
103                     alGetSourcei( source, AL_SOURCE_STATE, &result );
104                 }
105                 if ( result == AL_STOPPED ) {
106                     sample->no_valid_source();
107                     _smgr->release_source( sample->get_source() );
108                 }
109             }
110         }
111
112         if ( result == AL_STOPPED ) {
113             sample->stop();
114             if ( !sample->is_queue() ) {
115                 ALuint buffer = sample->get_buffer();
116                 // disconnect buffer from its source - otherwise it cannot be deleted
117                 alSourceUnqueueBuffers(sample->get_source(), 1, &buffer);
118                 alDeleteBuffers( 1, &buffer );
119                 testForALError("buffer remove");
120             }
121             _removed_samples.erase( _removed_samples.begin()+i );
122             size--;
123             continue;
124         }
125         i++;
126     }
127
128     // Update the position and orientation information for all samples.
129     if ( _changed || _smgr->has_changed() ) {
130         update_pos_and_orientation();
131         _changed = false;
132     }
133
134     sample_map_iterator sample_current = _samples.begin();
135     sample_map_iterator sample_end = _samples.end();
136     for ( ; sample_current != sample_end; ++sample_current ) {
137         SGSoundSample *sample = sample_current->second;
138
139         if ( !sample->is_valid_source() && sample->is_playing() ) {
140             //
141             // a request to start playing a sound has been filed.
142             //
143             ALuint source = _smgr->request_source();
144             if (alIsSource(source) == AL_TRUE )
145             {
146                 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
147                 if ( sample->is_queue() )
148                 {
149                     sample->set_source( source );
150                     update_sample_config( sample );
151
152                     alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
153                     alSourcei( source, AL_LOOPING, looping );
154                     alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
155                     alSourcePlay( source );
156                     testForALError("sample play");
157                 }
158                 else
159                 {
160                     ALuint buffer = _smgr->request_buffer(sample);
161                     if (buffer == SGSoundMgr::FAILED_BUFFER ||
162                         buffer == SGSoundMgr::NO_BUFFER)
163                     {
164                         _smgr->release_source(source);
165                         continue;
166                     }
167
168                     // start playing the sample
169                     buffer = sample->get_buffer();
170                     if ( alIsBuffer(buffer) == AL_TRUE )
171                     {
172                         alSourcei( source, AL_BUFFER, buffer );
173                         testForALError("assign buffer to source");
174
175                         sample->set_source( source );
176                         update_sample_config( sample );
177
178                         alSourcei( source, AL_LOOPING, looping );
179                         alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
180                         alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
181                         alSourcePlay( source );
182                         testForALError("sample play");
183                     } else
184                         SG_LOG( SG_SOUND, SG_ALERT, "No such buffer!\n");
185                 }
186             }
187
188         } else if ( sample->is_valid_source() ) {
189             // check if the sound has stopped by itself
190
191             unsigned int source = sample->get_source();
192             int result;
193
194             alGetSourcei( source, AL_SOURCE_STATE, &result );
195             if ( result == AL_STOPPED ) {
196                 // sample is stoped because it wasn't looping
197                 sample->stop();
198                 sample->no_valid_source();
199                 _smgr->release_source( source );
200                 _smgr->release_buffer( sample );
201                 remove( sample->get_sample_name() );
202             }
203             else
204             if ( sample->has_changed() ) {
205                 if ( !sample->is_playing() ) {
206                     // a request to stop playing the sound has been filed.
207                     sample->stop();
208                     sample->no_valid_source();
209                     _smgr->release_source( sample->get_source() );
210                 } else if ( _smgr->has_changed() ) {
211                     update_sample_config( sample );
212                 }
213             }
214
215         }
216         testForALError("update");
217     }
218 }
219
220 // add a sound effect, return true if successful
221 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
222
223     sample_map_iterator sample_it = _samples.find( refname );
224     if ( sample_it != _samples.end() ) {
225         // sample name already exists
226         return false;
227     }
228
229     _samples[refname] = sound;
230     return true;
231 }
232
233
234 // remove a sound effect, return true if successful
235 bool SGSampleGroup::remove( const string &refname ) {
236
237     sample_map_iterator sample_it = _samples.find( refname );
238     if ( sample_it == _samples.end() ) {
239         // sample was not found
240         return false;
241     }
242
243     if ( sample_it->second->is_valid_buffer() )
244         _removed_samples.push_back( sample_it->second );
245
246     _samples.erase( sample_it );
247
248     return true;
249 }
250
251
252 // return true of the specified sound exists in the sound manager system
253 bool SGSampleGroup::exists( const string &refname ) {
254     sample_map_iterator sample_it = _samples.find( refname );
255     if ( sample_it == _samples.end() ) {
256         // sample was not found
257         return false;
258     }
259
260     return true;
261 }
262
263
264 // return a pointer to the SGSoundSample if the specified sound exists
265 // in the sound manager system, otherwise return NULL
266 SGSoundSample *SGSampleGroup::find( const string &refname ) {
267     sample_map_iterator sample_it = _samples.find( refname );
268     if ( sample_it == _samples.end() ) {
269         // sample was not found
270         return NULL;
271     }
272
273     return sample_it->second;
274 }
275
276
277 void
278 SGSampleGroup::stop ()
279 {
280     _pause = true;
281     sample_map_iterator sample_current = _samples.begin();
282     sample_map_iterator sample_end = _samples.end();
283     for ( ; sample_current != sample_end; ++sample_current ) {
284         SGSoundSample *sample = sample_current->second;
285
286         if ( sample->is_valid_source() ) {
287             ALint source = sample->get_source();
288             if ( sample->is_playing() ) {
289                 alSourceStop( source );
290             }
291             _smgr->release_source( source );
292             sample->no_valid_source();
293         }
294
295         if ( sample->is_valid_buffer() ) {
296             _smgr->release_buffer( sample );
297             sample->no_valid_buffer();
298         }
299     }
300     testForALError("stop");
301 }
302
303 // stop playing all associated samples
304 void
305 SGSampleGroup::suspend ()
306 {
307     if (_active && _pause == false) {
308         _pause = true;
309         sample_map_iterator sample_current = _samples.begin();
310         sample_map_iterator sample_end = _samples.end();
311         for ( ; sample_current != sample_end; ++sample_current ) {
312             SGSoundSample *sample = sample_current->second;
313
314             if ( sample->is_valid_source() && sample->is_playing() ) {
315                 alSourcePause( sample->get_source() );
316             }
317         }
318         testForALError("suspend");
319     }
320 }
321
322 // resume playing all associated samples
323 void
324 SGSampleGroup::resume ()
325 {
326     if (_active && _pause == true) {
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
332             if ( sample->is_valid_source() && sample->is_playing() ) {
333                 alSourcePlay( sample->get_source() );
334             }
335         }
336         testForALError("resume");
337         _pause = false;
338     }
339 }
340
341
342 // tell the scheduler to play the indexed sample in a continuous loop
343 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
344     SGSoundSample *sample = find( refname );
345
346     if ( sample == NULL ) {
347         return false;
348     }
349
350     sample->play( looping );
351     return true;
352 }
353
354
355 // return true of the specified sound is currently being played
356 bool SGSampleGroup::is_playing( const string& refname ) {
357     SGSoundSample *sample = find( refname );
358
359     if ( sample == NULL ) {
360         return false;
361     }
362
363     return ( sample->is_playing() ) ? true : false;
364 }
365
366 // immediate stop playing the sound
367 bool SGSampleGroup::stop( const string& refname ) {
368     SGSoundSample *sample  = find( refname );
369
370     if ( sample == NULL ) {
371         return false;
372     }
373
374     sample->stop();
375     return true;
376 }
377
378 void SGSampleGroup::set_volume( float vol )
379 {
380     if (vol > _volume*1.01 || vol < _volume*0.99) {
381         _volume = vol;
382         if (_volume < 0.0) _volume = 0.0;
383         if (_volume > 1.0) _volume = 1.0;
384         _changed = true;
385     }
386 }
387
388 // set the source position and orientation of all managed sounds
389 void SGSampleGroup::update_pos_and_orientation() {
390  
391     SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
392     SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
393     SGQuatd ec2body = hlOr*_orientation;
394
395     SGVec3f velocity = SGVec3f::zeros();
396     if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
397        velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
398     }
399
400     sample_map_iterator sample_current = _samples.begin();
401     sample_map_iterator sample_end = _samples.end();
402     for ( ; sample_current != sample_end; ++sample_current ) {
403         SGSoundSample *sample = sample_current->second;
404         sample->set_master_volume( _volume );
405         sample->set_orientation( _orientation );
406         sample->set_rotation( ec2body );
407         sample->set_position( position );
408         sample->set_velocity( velocity );
409     }
410 }
411
412 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
413     SGVec3f orientation, velocity;
414     SGVec3d position;
415
416     if ( _tied_to_listener ) {
417         orientation = _smgr->get_direction();
418         position = SGVec3d::zeros();
419         velocity = _smgr->get_velocity();
420     } else {
421         sample->update_pos_and_orientation();
422         orientation = sample->get_orientation();
423         position = sample->get_position();
424         velocity = sample->get_velocity();
425     }
426
427     if (_smgr->bad_doppler_effect()) {
428         velocity *= 100.0f;
429     }
430
431 #if 0
432     if (length(position) > 20000)
433         printf("%s source and listener distance greater than 20km!\n",
434                _refname.c_str());
435     if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
436     if (isNaN(orientation.data())) printf("NaN in source orientation\n");
437     if (isNaN(velocity.data())) printf("NaN in source velocity\n");
438 #endif
439
440     unsigned int source = sample->get_source();
441     alSourcefv( source, AL_POSITION, toVec3f(position).data() );
442     alSourcefv( source, AL_VELOCITY, velocity.data() );
443     alSourcefv( source, AL_DIRECTION, orientation.data() );
444     testForALError("position and orientation");
445
446     alSourcef( source, AL_PITCH, sample->get_pitch() );
447     alSourcef( source, AL_GAIN, sample->get_volume() );
448     testForALError("pitch and gain");
449
450     if ( sample->has_static_data_changed() ) {
451         alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
452         alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
453         alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
454         testForALError("audio cone");
455
456         alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
457         alSourcef( source, AL_REFERENCE_DISTANCE,
458                            sample->get_reference_dist() );
459         testForALError("distance rolloff");
460     }
461 }
462
463 bool SGSampleGroup::testForError(void *p, string s)
464 {
465    if (p == NULL) {
466       SG_LOG( SG_SOUND, SG_ALERT, "Error (sample group): " << s);
467       return true;
468    }
469    return false;
470 }
471
472 bool SGSampleGroup::testForALError(string s)
473 {
474     ALenum error = alGetError();
475     if (error != AL_NO_ERROR)  {
476        SG_LOG( SG_SOUND, SG_ALERT, "AL Error (" << _refname << "): "
477                                       << alGetString(error) << " at " << s);
478        return true;
479     }
480     return false;
481 }
482