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