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