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