]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.cxx
99a5a0eb3689fa40b398e97d0fe3ad98f08d8b69
[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             if ( sample->is_looping() ) {
98                 sample->no_valid_source();
99                 _smgr->release_source( sample->get_source() );
100             }
101             else
102                 alGetSourcei( sample->get_source(), AL_SOURCE_STATE, &result );
103         }
104
105         if ( result == AL_STOPPED ) {
106             sample->stop();
107             if ( !sample->is_queue() ) {
108                 ALuint buffer = sample->get_buffer();
109                 alDeleteBuffers( 1, &buffer );
110                 testForALError("buffer remove");
111             }
112             _removed_samples.erase( _removed_samples.begin()+i );
113             size--;
114             continue;
115         }
116         i++;
117     }
118
119     // Update the position and orientation information for all samples.
120     if ( _changed || _smgr->has_changed() ) {
121         update_pos_and_orientation();
122         _changed = false;
123     }
124
125     sample_map_iterator sample_current = _samples.begin();
126     sample_map_iterator sample_end = _samples.end();
127     for ( ; sample_current != sample_end; ++sample_current ) {
128         SGSoundSample *sample = sample_current->second;
129
130         if ( !sample->is_valid_source() && sample->is_playing() ) {
131             //
132             // a request to start playing a sound has been filed.
133             //
134             ALuint source = _smgr->request_source();
135             if (alIsSource(source) == AL_TRUE )
136             {
137                 if ( sample->is_queue() )
138                 {
139                     sample->set_source( source );
140                     update_sample_config( sample );
141
142                     alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
143                     alSourcei( source, AL_LOOPING, AL_FALSE);
144                     alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
145                     alSourcePlay( source );
146                     testForALError("sample play");
147                 }
148                 else
149                 {
150                     if (_smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER)
151                         continue;
152
153                     // start playing the sample
154                     ALuint buffer = sample->get_buffer();
155                     if ( alIsBuffer(buffer) == AL_TRUE )
156                     {
157                         ALboolean looping;
158
159                         alSourcei( source, AL_BUFFER, buffer );
160                         testForALError("assign buffer to source");
161
162                         sample->set_source( source );
163                         update_sample_config( sample );
164
165                         looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
166                         alSourcei( source, AL_LOOPING, looping );
167                         alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
168                         alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
169                         alSourcePlay( source );
170                         testForALError("sample play");
171                     } else
172                         SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
173                 }
174             }
175
176         } else if ( sample->is_valid_source() ) {
177             // check if the sound has stopped by itself
178
179             unsigned int source = sample->get_source();
180             int result;
181
182             alGetSourcei( source, AL_SOURCE_STATE, &result );
183             if ( result == AL_STOPPED ) {
184                 // sample is stoped because it wasn't looping
185                 sample->stop();
186                 sample->no_valid_source();
187                 _smgr->release_source( source );
188                 _smgr->release_buffer( sample );
189                 remove( sample->get_sample_name() );
190             }
191             else
192             if ( sample->has_changed() ) {
193                 if ( !sample->is_playing() ) {
194                     // a request to stop playing the sound has been filed.
195                     sample->stop();
196                     sample->no_valid_source();
197                     _smgr->release_source( sample->get_source() );
198                 } else if ( _smgr->has_changed() ) {
199                     update_sample_config( sample );
200                 }
201             }
202
203         }
204         testForALError("update");
205     }
206 }
207
208 // add a sound effect, return true if successful
209 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
210
211     sample_map_iterator sample_it = _samples.find( refname );
212     if ( sample_it != _samples.end() ) {
213         // sample name already exists
214         return false;
215     }
216
217     _samples[refname] = sound;
218     return true;
219 }
220
221
222 // remove a sound effect, return true if successful
223 bool SGSampleGroup::remove( const string &refname ) {
224
225     sample_map_iterator sample_it = _samples.find( refname );
226     if ( sample_it == _samples.end() ) {
227         // sample was not found
228         return false;
229     }
230
231     if ( sample_it->second->is_valid_buffer() )
232         _removed_samples.push_back( sample_it->second );
233
234     _samples.erase( sample_it );
235
236     return true;
237 }
238
239
240 // return true of the specified sound exists in the sound manager system
241 bool SGSampleGroup::exists( const string &refname ) {
242     sample_map_iterator sample_it = _samples.find( refname );
243     if ( sample_it == _samples.end() ) {
244         // sample was not found
245         return false;
246     }
247
248     return true;
249 }
250
251
252 // return a pointer to the SGSoundSample if the specified sound exists
253 // in the sound manager system, otherwise return NULL
254 SGSoundSample *SGSampleGroup::find( const string &refname ) {
255     sample_map_iterator sample_it = _samples.find( refname );
256     if ( sample_it == _samples.end() ) {
257         // sample was not found
258         return NULL;
259     }
260
261     return sample_it->second;
262 }
263
264
265 void
266 SGSampleGroup::stop ()
267 {
268     _pause = true;
269     sample_map_iterator sample_current = _samples.begin();
270     sample_map_iterator sample_end = _samples.end();
271     for ( ; sample_current != sample_end; ++sample_current ) {
272         SGSoundSample *sample = sample_current->second;
273
274         if ( sample->is_valid_source() ) {
275             ALint source = sample->get_source();
276             if ( sample->is_playing() ) {
277                 alSourceStop( source );
278                 alSourcei( source, AL_BUFFER, 0 );
279             }
280             _smgr->release_source( source );
281             sample->no_valid_source();
282         }
283
284         if ( sample->is_valid_buffer() ) {
285             _smgr->release_buffer( sample );
286             sample->no_valid_buffer();
287         }
288     }
289     testForALError("stop");
290 }
291
292 // stop playing all associated samples
293 void
294 SGSampleGroup::suspend ()
295 {
296     if (_active && _pause == false) {
297         _pause = true;
298         sample_map_iterator sample_current = _samples.begin();
299         sample_map_iterator sample_end = _samples.end();
300         for ( ; sample_current != sample_end; ++sample_current ) {
301             SGSoundSample *sample = sample_current->second;
302
303             if ( sample->is_valid_source() && sample->is_playing() ) {
304                 alSourcePause( sample->get_source() );
305             }
306         }
307         testForALError("suspend");
308     }
309 }
310
311 // resume playing all associated samples
312 void
313 SGSampleGroup::resume ()
314 {
315     if (_active && _pause == true) {
316         sample_map_iterator sample_current = _samples.begin();
317         sample_map_iterator sample_end = _samples.end();
318         for ( ; sample_current != sample_end; ++sample_current ) {
319             SGSoundSample *sample = sample_current->second;
320
321             if ( sample->is_valid_source() && sample->is_playing() ) {
322                 alSourcePlay( sample->get_source() );
323             }
324         }
325         testForALError("resume");
326         _pause = false;
327     }
328 }
329
330
331 // tell the scheduler to play the indexed sample in a continuous loop
332 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
333     SGSoundSample *sample = find( refname );
334
335     if ( sample == NULL ) {
336         return false;
337     }
338
339     sample->play( looping );
340     return true;
341 }
342
343
344 // return true of the specified sound is currently being played
345 bool SGSampleGroup::is_playing( const string& refname ) {
346     SGSoundSample *sample = find( refname );
347
348     if ( sample == NULL ) {
349         return false;
350     }
351
352     return ( sample->is_playing() ) ? true : false;
353 }
354
355 // immediate stop playing the sound
356 bool SGSampleGroup::stop( const string& refname ) {
357     SGSoundSample *sample  = find( refname );
358
359     if ( sample == NULL ) {
360         return false;
361     }
362
363     sample->stop();
364     return true;
365 }
366
367 void SGSampleGroup::set_volume( float vol )
368 {
369     if (vol > _volume*1.01 || vol < _volume*0.99) {
370         _volume = vol;
371         if (_volume < 0.0) _volume = 0.0;
372         if (_volume > 1.0) _volume = 1.0;
373         _changed = true;
374     }
375 }
376
377 // set the source position and orientation of all managed sounds
378 void SGSampleGroup::update_pos_and_orientation() {
379  
380     SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
381     SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
382     SGQuatd ec2body = hlOr*_orientation;
383
384     SGVec3f velocity = SGVec3f::zeros();
385     if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
386        velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
387     }
388
389     sample_map_iterator sample_current = _samples.begin();
390     sample_map_iterator sample_end = _samples.end();
391     for ( ; sample_current != sample_end; ++sample_current ) {
392         SGSoundSample *sample = sample_current->second;
393         sample->set_master_volume( _volume );
394         sample->set_orientation( _orientation );
395         sample->set_rotation( ec2body );
396         sample->set_position( position );
397         sample->set_velocity( velocity );
398     }
399 }
400
401 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
402     SGVec3f orientation, velocity;
403     SGVec3d position;
404
405     if ( _tied_to_listener ) {
406         orientation = _smgr->get_direction();
407         position = SGVec3d::zeros();
408         velocity = _smgr->get_velocity();
409     } else {
410         sample->update_pos_and_orientation();
411         orientation = sample->get_orientation();
412         position = sample->get_position();
413         velocity = sample->get_velocity();
414     }
415
416     if (_smgr->bad_doppler_effect()) {
417         velocity *= 100.0f;
418     }
419
420 #if 0
421     if (length(position) > 20000)
422         printf("%s source and listener distance greater than 20km!\n",
423                _refname.c_str());
424     if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
425     if (isNaN(orientation.data())) printf("NaN in source orientation\n");
426     if (isNaN(velocity.data())) printf("NaN in source velocity\n");
427 #endif
428
429     unsigned int source = sample->get_source();
430     alSourcefv( source, AL_POSITION, toVec3f(position).data() );
431     alSourcefv( source, AL_VELOCITY, velocity.data() );
432     alSourcefv( source, AL_DIRECTION, orientation.data() );
433     testForALError("position and orientation");
434
435     alSourcef( source, AL_PITCH, sample->get_pitch() );
436     alSourcef( source, AL_GAIN, sample->get_volume() );
437     testForALError("pitch and gain");
438
439     if ( sample->has_static_data_changed() ) {
440         alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
441         alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
442         alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
443         testForALError("audio cone");
444
445         alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
446         alSourcef( source, AL_REFERENCE_DISTANCE,
447                            sample->get_reference_dist() );
448         testForALError("distance rolloff");
449     }
450 }
451
452 bool SGSampleGroup::testForError(void *p, string s)
453 {
454    if (p == NULL) {
455       SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
456       return true;
457    }
458    return false;
459 }
460
461 bool SGSampleGroup::testForALError(string s)
462 {
463     ALenum error = alGetError();
464     if (error != AL_NO_ERROR)  {
465        SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
466                                       << alGetString(error) << " at " << s);
467        return true;
468     }
469     return false;
470 }
471