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