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