]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.cxx
eed424c10ba93bddb5e96065b005561b6a7d3799
[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     _pause(false),
41     _tied_to_listener(false),
42     _velocity(SGVec3d::zeros()),
43     _orientation(SGQuatd::zeros()),
44     _position(SGGeod())
45 {
46     _samples.clear();
47 }
48
49 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
50     _smgr(smgr),
51     _refname(refname),
52     _active(false), 
53     _pause(false),
54     _tied_to_listener(false),
55     _velocity(SGVec3d::zeros()),
56     _orientation(SGQuatd::zeros()),
57     _position(SGGeod())
58 {
59     _smgr->add(this, refname);
60     _samples.clear();
61 }
62
63 SGSampleGroup::~SGSampleGroup ()
64 {
65     _active = false;
66
67     sample_map_iterator sample_current = _samples.begin();
68     sample_map_iterator sample_end = _samples.end();
69     for ( ; sample_current != sample_end; ++sample_current ) {
70         SGSoundSample *sample = sample_current->second;
71
72         if ( sample->is_valid_source() && sample->is_playing() ) {
73             sample->no_valid_source();
74             _smgr->release_source( sample->get_source() );
75             _smgr->release_buffer( sample );
76         }
77     }
78
79     _smgr = 0;
80 }
81
82 void SGSampleGroup::update( double dt ) {
83
84     if ( !_active || _pause ) return;
85
86     testForALError("start of update!!\n");
87
88     // Delete any OpenAL buffers that might still be in use.
89     unsigned int size = _removed_samples.size();
90     for (unsigned int i=0; i<size; ) {
91         SGSoundSample *sample = _removed_samples[i];
92         ALint result = AL_STOPPED;
93
94         if ( sample->is_valid_source() ) {
95             if ( sample->is_looping() ) {
96                 sample->no_valid_source();
97                 _smgr->release_source( sample->get_source() );
98             }
99             else
100                 alGetSourcei( sample->get_source(), AL_SOURCE_STATE, &result );
101         }
102
103         if ( result == AL_STOPPED ) {
104             ALuint buffer = sample->get_buffer();
105             alDeleteBuffers( 1, &buffer );
106             testForALError("buffer remove");
107             _removed_samples.erase( _removed_samples.begin()+i );
108             size--;
109             continue;
110         }
111         i++;
112     }
113
114     sample_map_iterator sample_current = _samples.begin();
115     sample_map_iterator sample_end = _samples.end();
116     for ( ; sample_current != sample_end; ++sample_current ) {
117         SGSoundSample *sample = sample_current->second;
118
119         if ( !sample->is_valid_source() && sample->is_playing() ) {
120             //
121             // a request to start playing a sound has been filed.
122             //
123             if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
124                 continue;
125
126             // start playing the sample
127             ALuint buffer = sample->get_buffer();
128             ALuint source = _smgr->request_source();
129             if (alIsSource(source) == AL_TRUE && alIsBuffer(buffer) == AL_TRUE)
130             {
131                 sample->set_source( source );
132                 
133                 alSourcei( source, AL_BUFFER, buffer );
134                 testForALError("assign buffer to source");
135
136                 sample->set_source( source );
137                 update_sample_config( sample );
138
139                 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
140                 alSourcei( source, AL_LOOPING, looping );
141                 alSourcef( source, AL_ROLLOFF_FACTOR, 1.0 );
142                 alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
143                 alSourcePlay( source );
144                 testForALError("sample play");
145             } else {
146                 if (alIsBuffer(buffer) == AL_FALSE) 
147                    SG_LOG( SG_GENERAL, SG_ALERT, "No such buffer!\n");
148                 // sample->no_valid_source();
149                 // sadly, no free source available at this time
150             }
151
152         } else if ( sample->is_valid_source() && sample->has_changed() ) {
153             if ( !sample->is_playing() ) {
154                 // a request to stop playing the sound has been filed.
155
156                 sample->stop();
157                 sample->no_valid_source();
158                 _smgr->release_source( sample->get_source() );
159             } else if ( _smgr->has_changed() ) {
160                 update_sample_config( sample );
161             }
162
163         } else if ( sample->is_valid_source() ) {
164             // check if the sound has stopped by itself
165
166             unsigned int source = sample->get_source();
167             int result;
168
169             alGetSourcei( source, AL_SOURCE_STATE, &result );
170             if ( result == AL_STOPPED ) {
171                 // sample is stoped because it wasn't looping
172                 sample->stop();
173                 sample->no_valid_source();
174                 _smgr->release_source( source );
175                 _smgr->release_buffer( sample );
176                 remove( sample->get_sample_name() );
177             }
178         }
179         testForALError("update");
180     }
181 }
182
183 // add a sound effect, return true if successful
184 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
185
186     sample_map_iterator sample_it = _samples.find( refname );
187     if ( sample_it != _samples.end() ) {
188         // sample name already exists
189         return false;
190     }
191
192     _samples[refname] = sound;
193     return true;
194 }
195
196
197 // remove a sound effect, return true if successful
198 bool SGSampleGroup::remove( const string &refname ) {
199
200     sample_map_iterator sample_it = _samples.find( refname );
201     if ( sample_it == _samples.end() ) {
202         // sample was not found
203         return false;
204     }
205
206     if ( sample_it->second->is_valid_buffer() )
207         _removed_samples.push_back( sample_it->second );
208     _samples.erase( sample_it );
209
210     return true;
211 }
212
213
214 // return true of the specified sound exists in the sound manager system
215 bool SGSampleGroup::exists( const string &refname ) {
216     sample_map_iterator sample_it = _samples.find( refname );
217     if ( sample_it == _samples.end() ) {
218         // sample was not found
219         return false;
220     }
221
222     return true;
223 }
224
225
226 // return a pointer to the SGSoundSample if the specified sound exists
227 // in the sound manager system, otherwise return NULL
228 SGSoundSample *SGSampleGroup::find( const string &refname ) {
229     sample_map_iterator sample_it = _samples.find( refname );
230     if ( sample_it == _samples.end() ) {
231         // sample was not found
232         return NULL;
233     }
234
235     return sample_it->second;
236 }
237
238
239 // stop playing all associated samples
240 void
241 SGSampleGroup::suspend ()
242 {
243     _pause = true;
244     sample_map_iterator sample_current = _samples.begin();
245     sample_map_iterator sample_end = _samples.end();
246     for ( ; sample_current != sample_end; ++sample_current ) {
247         SGSoundSample *sample = sample_current->second;
248
249         if ( sample->is_valid_source() && sample->is_playing() ) {
250             alSourcePause( sample->get_source() );
251         }
252     }
253     testForALError("suspend");
254 }
255
256 // resume playing all associated samples
257 void
258 SGSampleGroup::resume ()
259 {
260     sample_map_iterator sample_current = _samples.begin();
261     sample_map_iterator sample_end = _samples.end();
262     for ( ; sample_current != sample_end; ++sample_current ) {
263         SGSoundSample *sample = sample_current->second;
264
265         if ( sample->is_valid_source() && sample->is_playing() ) {
266             alSourcePlay( sample->get_source() );
267         }
268     }
269     testForALError("resume");
270     _pause = false;
271 }
272
273
274 // tell the scheduler to play the indexed sample in a continuous loop
275 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
276     SGSoundSample *sample = find( refname );
277
278     if ( sample == NULL ) {
279         return false;
280     }
281
282     sample->play( looping );
283     return true;
284 }
285
286
287 // return true of the specified sound is currently being played
288 bool SGSampleGroup::is_playing( const string& refname ) {
289     SGSoundSample *sample = find( refname );
290
291     if ( sample == NULL ) {
292         return false;
293     }
294
295     return ( sample->is_playing() ) ? true : false;
296 }
297
298 // immediate stop playing the sound
299 bool SGSampleGroup::stop( const string& refname ) {
300     SGSoundSample *sample  = find( refname );
301
302     if ( sample == NULL ) {
303         return false;
304     }
305
306     sample->stop();
307     return true;
308 }
309
310 // set source velocity of all managed sounds
311 void SGSampleGroup::set_velocity( const SGVec3f &vel ) {
312     if ( isnan(vel[0]) || isnan(vel[1]) || isnan(vel[2]) )
313     {
314         SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
315         return;
316     }
317
318     if (_velocity != vel) {
319         sample_map_iterator sample_current = _samples.begin();
320         sample_map_iterator sample_end = _samples.end();
321         for ( ; sample_current != sample_end; ++sample_current ) {
322             SGSoundSample *sample = sample_current->second;
323             sample->set_velocity( vel );
324         }
325         _velocity = vel;
326     }
327 }
328
329 // set the source position of all managed sounds
330 void SGSampleGroup::set_position( const SGGeod& pos ) {
331
332     sample_map_iterator sample_current = _samples.begin();
333     sample_map_iterator sample_end = _samples.end();
334     for ( ; sample_current != sample_end; ++sample_current ) {
335         SGSoundSample *sample = sample_current->second;
336         sample->set_position( pos );
337     }
338     _position = pos;
339 }
340
341
342 // set the source orientation of all managed sounds
343 void SGSampleGroup::set_orientation( const SGQuatd& ori ) {
344
345     if (_orientation != ori) {
346         sample_map_iterator sample_current = _samples.begin();
347         sample_map_iterator sample_end = _samples.end();
348         for ( ; sample_current != sample_end; ++sample_current ) {
349             SGSoundSample *sample = sample_current->second;
350             sample->set_orientation( ori );
351         }
352         _orientation = ori;
353     }
354 }
355
356 void SGSampleGroup::set_volume( float vol )
357 {
358     _volume = vol;
359     if (_volume < 0.0) _volume = 0.0;
360     if (_volume > 1.0) _volume = 1.0;
361
362     sample_map_iterator sample_current = _samples.begin();
363     sample_map_iterator sample_end = _samples.end();
364     for ( ; sample_current != sample_end; ++sample_current ) {
365         SGSoundSample *sample = sample_current->second;
366         sample->set_master_volume( _volume );
367     }
368 }
369
370 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
371     SGVec3f orientation, velocity;
372     SGVec3d position;
373
374     if ( _tied_to_listener ) {
375         orientation = _smgr->get_direction();
376         position = _smgr->get_position();
377         velocity = _smgr->get_velocity();
378     } else {
379         sample->update_absolute_position();
380         orientation = sample->get_orientation();
381         position = sample->get_position();
382         velocity = sample->get_velocity();
383     }
384
385     if (dist(position, _smgr->get_position()) > 10000)
386         printf("source and listener distance greater than 20km!\n");
387     if (isNaN(position)) printf("NaN in source position\n");
388     if (isNaN(orientation)) printf("NaN in source orientation\n");
389     if (isNaN(velocity)) printf("NaN in source velocity\n");
390
391     unsigned int source = sample->get_source();
392     alSourcefv( source, AL_POSITION, toVec3f(position).data() );
393     alSourcefv( source, AL_VELOCITY, velocity.data() );
394     alSourcefv( source, AL_DIRECTION, orientation.data() );
395     testForALError("position and orientation");
396
397     alSourcef( source, AL_PITCH, sample->get_pitch() );
398     alSourcef( source, AL_GAIN, sample->get_volume() );
399     testForALError("pitch and gain");
400
401     if ( sample->has_static_data_changed() ) {
402         alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
403         alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
404         alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
405         testForALError("audio cone");
406
407         alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
408         alSourcef( source, AL_REFERENCE_DISTANCE,
409                            sample->get_reference_dist() );
410         testForALError("distance rolloff");
411     }
412 }
413
414 bool SGSampleGroup::testForError(void *p, string s)
415 {
416    if (p == NULL) {
417       SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
418       return true;
419    }
420    return false;
421 }
422
423 bool SGSampleGroup::testForALError(string s)
424 {
425     ALenum error = alGetError();
426     if (error != AL_NO_ERROR)  {
427        SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
428                                       << alGetString(error) << " at " << s);
429        return true;
430     }
431     return false;
432 }
433