]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.cxx
Compile with MSVC10
[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             }
264             _smgr->release_source( source );
265             sample->no_valid_source();
266         }
267
268         if (sample->is_valid_buffer() ) {
269             _smgr->release_buffer( sample );
270             sample->no_valid_buffer();
271         }
272     }
273     testForALError("stop");
274 }
275
276 // stop playing all associated samples
277 void
278 SGSampleGroup::suspend ()
279 {
280     if (_active && _pause == false) {
281         _pause = true;
282         sample_map_iterator sample_current = _samples.begin();
283         sample_map_iterator sample_end = _samples.end();
284         for ( ; sample_current != sample_end; ++sample_current ) {
285             SGSoundSample *sample = sample_current->second;
286
287             if ( sample->is_valid_source() && sample->is_playing() ) {
288                 alSourcePause( sample->get_source() );
289             }
290         }
291         testForALError("suspend");
292     }
293 }
294
295 // resume playing all associated samples
296 void
297 SGSampleGroup::resume ()
298 {
299     if (_active && _pause == true) {
300         sample_map_iterator sample_current = _samples.begin();
301         sample_map_iterator sample_end = _samples.end();
302         for ( ; sample_current != sample_end; ++sample_current ) {
303             SGSoundSample *sample = sample_current->second;
304
305             if ( sample->is_valid_source() && sample->is_playing() ) {
306                 alSourcePlay( sample->get_source() );
307             }
308         }
309         testForALError("resume");
310         _pause = false;
311     }
312 }
313
314
315 // tell the scheduler to play the indexed sample in a continuous loop
316 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
317     SGSoundSample *sample = find( refname );
318
319     if ( sample == NULL ) {
320         return false;
321     }
322
323     sample->play( looping );
324     return true;
325 }
326
327
328 // return true of the specified sound is currently being played
329 bool SGSampleGroup::is_playing( const string& refname ) {
330     SGSoundSample *sample = find( refname );
331
332     if ( sample == NULL ) {
333         return false;
334     }
335
336     return ( sample->is_playing() ) ? true : false;
337 }
338
339 // immediate stop playing the sound
340 bool SGSampleGroup::stop( const string& refname ) {
341     SGSoundSample *sample  = find( refname );
342
343     if ( sample == NULL ) {
344         return false;
345     }
346
347     sample->stop();
348     return true;
349 }
350
351 void SGSampleGroup::set_volume( float vol )
352 {
353     if (vol > _volume*1.01 || vol < _volume*0.99) {
354         _volume = vol;
355         if (_volume < 0.0) _volume = 0.0;
356         if (_volume > 1.0) _volume = 1.0;
357         _changed = true;
358     }
359 }
360
361 // set the source position and orientation of all managed sounds
362 void SGSampleGroup::update_pos_and_orientation() {
363  
364     SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
365     SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
366     SGQuatd ec2body = hlOr*_orientation;
367
368     SGVec3f velocity = SGVec3f::zeros();
369     if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
370        velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
371     }
372
373     sample_map_iterator sample_current = _samples.begin();
374     sample_map_iterator sample_end = _samples.end();
375     for ( ; sample_current != sample_end; ++sample_current ) {
376         SGSoundSample *sample = sample_current->second;
377         sample->set_master_volume( _volume );
378         sample->set_orientation( _orientation );
379         sample->set_rotation( ec2body );
380         sample->set_position( position );
381         sample->set_velocity( velocity );
382     }
383 }
384
385 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
386     SGVec3f orientation, velocity;
387     SGVec3d position;
388
389     if ( _tied_to_listener ) {
390         orientation = _smgr->get_direction();
391         position = SGVec3d::zeros();
392         velocity = _smgr->get_velocity();
393     } else {
394         sample->update_pos_and_orientation();
395         orientation = sample->get_orientation();
396         position = sample->get_position();
397         velocity = sample->get_velocity();
398     }
399
400     if (_smgr->bad_doppler_effect()) {
401         velocity *= 100.0f;
402     }
403
404 #if 0
405     if (length(position) > 20000)
406         printf("%s source and listener distance greater than 20km!\n",
407                _refname.c_str());
408     if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
409     if (isNaN(orientation.data())) printf("NaN in source orientation\n");
410     if (isNaN(velocity.data())) printf("NaN in source velocity\n");
411 #endif
412
413     unsigned int source = sample->get_source();
414     alSourcefv( source, AL_POSITION, toVec3f(position).data() );
415     alSourcefv( source, AL_VELOCITY, velocity.data() );
416     alSourcefv( source, AL_DIRECTION, orientation.data() );
417     testForALError("position and orientation");
418
419     alSourcef( source, AL_PITCH, sample->get_pitch() );
420     alSourcef( source, AL_GAIN, sample->get_volume() );
421     testForALError("pitch and gain");
422
423     if ( sample->has_static_data_changed() ) {
424         alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
425         alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
426         alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
427         testForALError("audio cone");
428
429         alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
430         alSourcef( source, AL_REFERENCE_DISTANCE,
431                            sample->get_reference_dist() );
432         testForALError("distance rolloff");
433     }
434 }
435
436 bool SGSampleGroup::testForError(void *p, string s)
437 {
438    if (p == NULL) {
439       SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
440       return true;
441    }
442    return false;
443 }
444
445 bool SGSampleGroup::testForALError(string s)
446 {
447     ALenum error = alGetError();
448     if (error != AL_NO_ERROR)  {
449        SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
450                                       << alGetString(error) << " at " << s);
451        return true;
452     }
453     return false;
454 }
455