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