]> git.mxchange.org Git - simgear.git/blob - simgear/sound/sample_group.cxx
Remove OpenVG dependency from header file
[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 #include <simgear/debug/logstream.hxx>
29
30 #include "soundmgr_openal.hxx"
31 #include "soundmgr_openal_private.hxx"
32 #include "sample_group.hxx"
33
34 using std::string;
35
36 bool isNaN(float *v) {
37    return (isnan(v[0]) || isnan(v[1]) || isnan(v[2]));
38 }
39
40 SGSampleGroup::SGSampleGroup () :
41     _smgr(NULL),
42     _refname(""),
43     _active(false),
44     _changed(false),
45     _pause(false),
46     _volume(1.0),
47     _tied_to_listener(false),
48     _velocity(SGVec3d::zeros()),
49     _orientation(SGQuatd::zeros())
50 {
51     _samples.clear();
52 }
53
54 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
55     _smgr(smgr),
56     _refname(refname),
57     _active(false), 
58     _changed(false),
59     _pause(false),
60     _volume(1.0),
61     _tied_to_listener(false),
62     _velocity(SGVec3d::zeros()),
63     _orientation(SGQuatd::zeros())
64 {
65     _smgr->add(this, refname);
66     _samples.clear();
67 }
68
69 SGSampleGroup::~SGSampleGroup ()
70 {
71     _active = false;
72     stop();
73     _smgr = 0;
74 }
75
76 void SGSampleGroup::update( double dt ) {
77
78     if ( !_active || _pause ) return;
79
80     testForALError("start of update!!\n");
81
82     // Delete any OpenAL buffers that might still be in use.
83     unsigned int size = _removed_samples.size();
84     for (unsigned int i=0; i<size; ) {
85         SGSoundSample *sample = _removed_samples[i];
86         ALint result = AL_STOPPED;
87
88         if ( sample->is_valid_source() ) {
89             int source = sample->get_source();
90
91             alGetSourcei( source, AL_SOURCE_STATE, &result );
92             if ( sample->is_looping() ) {
93                 if ( result != AL_STOPPED ) {
94                     alSourceStop( source );
95                     alGetSourcei( source, AL_SOURCE_STATE, &result );
96                 }
97             }
98             if ( result == AL_STOPPED ) {
99                 sample->no_valid_source();
100                 _smgr->release_source( source );
101             }
102         }
103
104         if ( result == AL_STOPPED ) {
105             sample->stop();
106             if (( !sample->is_queue() )&&
107                 (sample->is_valid_buffer()))
108             {
109                 _smgr->release_buffer(sample);
110             }
111             _removed_samples.erase( _removed_samples.begin()+i );
112             size--;
113             continue;
114         }
115         i++;
116     }
117
118     // Update the position and orientation information for all samples.
119     if ( _changed || _smgr->has_changed() ) {
120         update_pos_and_orientation();
121         _changed = false;
122     }
123
124     sample_map_iterator sample_current = _samples.begin();
125     sample_map_iterator sample_end = _samples.end();
126     for ( ; sample_current != sample_end; ++sample_current ) {
127         SGSoundSample *sample = sample_current->second;
128
129         if ( !sample->is_valid_source() && sample->is_playing() && !sample->test_out_of_range()) {
130             //
131             // a request to start playing a sound has been filed.
132             //
133             ALuint source = _smgr->request_source();
134             if (alIsSource(source) == AL_TRUE )
135             {
136                 ALboolean looping = sample->is_looping() ? AL_TRUE : AL_FALSE;
137                 if ( sample->is_queue() )
138                 {
139                     sample->set_source( source );
140                     update_sample_config( sample );
141
142                     alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
143                     alSourcei( source, AL_LOOPING, looping );
144                     alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
145                     alSourcePlay( source );
146                     testForALError("sample play");
147                 }
148                 else
149                 {
150                     ALuint buffer = _smgr->request_buffer(sample);
151                     if (buffer == SGSoundMgr::FAILED_BUFFER ||
152                         buffer == SGSoundMgr::NO_BUFFER)
153                     {
154                         _smgr->release_source(source);
155                         continue;
156                     }
157
158                     // start playing the sample
159                     buffer = sample->get_buffer();
160                     if ( alIsBuffer(buffer) == AL_TRUE )
161                     {
162                         alSourcei( source, AL_BUFFER, buffer );
163                         testForALError("assign buffer to source");
164
165                         sample->set_source( source );
166                         update_sample_config( sample );
167
168                         alSourcei( source, AL_LOOPING, looping );
169                         alSourcef( source, AL_ROLLOFF_FACTOR, 0.3 );
170                         alSourcei( source, AL_SOURCE_RELATIVE, AL_FALSE );
171                         alSourcePlay( source );
172                         testForALError("sample play");
173                     } else
174                         SG_LOG( SG_SOUND, SG_ALERT, "No such buffer!");
175                 }
176             }
177
178         } else if ( sample->is_valid_source() ) {
179             // check if the sound has stopped by itself
180
181             unsigned int source = sample->get_source();
182             int result;
183
184             alGetSourcei( source, AL_SOURCE_STATE, &result );
185             if ( result == AL_STOPPED ) {
186                 // sample is stopped because it wasn't looping
187                 sample->stop();
188                 sample->no_valid_source();
189                 _smgr->release_source( source );
190                 _smgr->release_buffer( sample );
191                 remove( sample->get_sample_name() );
192             }
193             else
194             if ( sample->has_changed() ) {
195                 if ( !sample->is_playing() ) {
196                     // a request to stop playing the sound has been filed.
197                     sample->stop();
198                     sample->no_valid_source();
199                     _smgr->release_source( sample->get_source() );
200                 } else if ( _smgr->has_changed() ) {
201                     update_sample_config( sample );
202                 }
203             }
204
205         }
206         testForALError("update");
207     }
208 }
209
210 // add a sound effect, return true if successful
211 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
212
213     sample_map_iterator sample_it = _samples.find( refname );
214     if ( sample_it != _samples.end() ) {
215         // sample name already exists
216         return false;
217     }
218
219     _samples[refname] = sound;
220     return true;
221 }
222
223
224 // remove a sound effect, return true if successful
225 bool SGSampleGroup::remove( const string &refname ) {
226
227     sample_map_iterator sample_it = _samples.find( refname );
228     if ( sample_it == _samples.end() ) {
229         // sample was not found
230         return false;
231     }
232
233     if ( sample_it->second->is_valid_buffer() )
234         _removed_samples.push_back( sample_it->second );
235
236     _samples.erase( sample_it );
237
238     return true;
239 }
240
241
242 // return true of the specified sound exists in the sound manager system
243 bool SGSampleGroup::exists( const string &refname ) {
244     sample_map_iterator sample_it = _samples.find( refname );
245     if ( sample_it == _samples.end() ) {
246         // sample was not found
247         return false;
248     }
249
250     return true;
251 }
252
253
254 // return a pointer to the SGSoundSample if the specified sound exists
255 // in the sound manager system, otherwise return NULL
256 SGSoundSample *SGSampleGroup::find( const string &refname ) {
257     sample_map_iterator sample_it = _samples.find( refname );
258     if ( sample_it == _samples.end() ) {
259         // sample was not found
260         return NULL;
261     }
262
263     return sample_it->second;
264 }
265
266
267 void
268 SGSampleGroup::stop ()
269 {
270     _pause = true;
271     sample_map_iterator sample_current = _samples.begin();
272     sample_map_iterator sample_end = _samples.end();
273     for ( ; sample_current != sample_end; ++sample_current ) {
274         SGSoundSample *sample = sample_current->second;
275
276         if ( sample->is_valid_source() ) {
277             ALint source = sample->get_source();
278             if ( sample->is_playing() ) {
279                 alSourceStop( source );
280                 testForALError("stop");
281             }
282             _smgr->release_source( source );
283             sample->no_valid_source();
284         }
285
286         if ( sample->is_valid_buffer() ) {
287             _smgr->release_buffer( sample );
288             sample->no_valid_buffer();
289         }
290     }
291 }
292
293 // stop playing all associated samples
294 void
295 SGSampleGroup::suspend ()
296 {
297     if (_active && _pause == false) {
298         _pause = true;
299         sample_map_iterator sample_current = _samples.begin();
300         sample_map_iterator sample_end = _samples.end();
301         for ( ; sample_current != sample_end; ++sample_current ) {
302             SGSoundSample *sample = sample_current->second;
303
304             if ( sample->is_valid_source() && sample->is_playing() ) {
305                 alSourcePause( sample->get_source() );
306             }
307         }
308         testForALError("suspend");
309     }
310 }
311
312 // resume playing all associated samples
313 void
314 SGSampleGroup::resume ()
315 {
316     if (_active && _pause == true) {
317         sample_map_iterator sample_current = _samples.begin();
318         sample_map_iterator sample_end = _samples.end();
319         for ( ; sample_current != sample_end; ++sample_current ) {
320             SGSoundSample *sample = sample_current->second;
321
322             if ( sample->is_valid_source() && sample->is_playing() ) {
323                 alSourcePlay( sample->get_source() );
324             }
325         }
326         testForALError("resume");
327         _pause = false;
328     }
329 }
330
331
332 // tell the scheduler to play the indexed sample in a continuous loop
333 bool SGSampleGroup::play( const string &refname, bool looping = false ) {
334     SGSoundSample *sample = find( refname );
335
336     if ( sample == NULL ) {
337         return false;
338     }
339
340     sample->play( looping );
341     return true;
342 }
343
344
345 // return true of the specified sound is currently being played
346 bool SGSampleGroup::is_playing( const string& refname ) {
347     SGSoundSample *sample = find( refname );
348
349     if ( sample == NULL ) {
350         return false;
351     }
352
353     return ( sample->is_playing() ) ? true : false;
354 }
355
356 // immediate stop playing the sound
357 bool SGSampleGroup::stop( const string& refname ) {
358     SGSoundSample *sample  = find( refname );
359
360     if ( sample == NULL ) {
361         return false;
362     }
363
364     sample->stop();
365     return true;
366 }
367
368 void SGSampleGroup::set_volume( float vol )
369 {
370     if (vol > _volume*1.01 || vol < _volume*0.99) {
371         _volume = vol;
372         if (_volume < 0.0) _volume = 0.0;
373         if (_volume > 1.0) _volume = 1.0;
374         _changed = true;
375     }
376 }
377
378 // set the source position and orientation of all managed sounds
379 void SGSampleGroup::update_pos_and_orientation() {
380  
381     SGVec3d position = SGVec3d::fromGeod(_base_pos) - _smgr->get_position();
382     SGQuatd hlOr = SGQuatd::fromLonLat(_base_pos);
383     SGQuatd ec2body = hlOr*_orientation;
384
385     SGVec3f velocity = SGVec3f::zeros();
386     if ( _velocity[0] || _velocity[1] || _velocity[2] ) {
387        velocity = toVec3f( hlOr.backTransform(_velocity*SG_FEET_TO_METER) );
388     }
389
390     sample_map_iterator sample_current = _samples.begin();
391     sample_map_iterator sample_end = _samples.end();
392     for ( ; sample_current != sample_end; ++sample_current ) {
393         SGSoundSample *sample = sample_current->second;
394         sample->set_master_volume( _volume );
395         sample->set_orientation( _orientation );
396         sample->set_rotation( ec2body );
397         sample->set_position( position );
398         sample->set_velocity( velocity );
399
400         // Test if a sample is farther away than max distance, if so
401         // stop the sound playback and free it's source.
402         if (!_tied_to_listener) {
403             float max2 = sample->get_max_dist() * sample->get_max_dist();
404             float dist2 = position[0]*position[0]
405                           + position[1]*position[1] + position[2]*position[2];
406             if ((dist2 > max2) && !sample->test_out_of_range()) {
407                 sample->set_out_of_range(true);
408             } else if ((dist2 < max2) && sample->test_out_of_range()) {
409                 sample->set_out_of_range(false);
410             }
411         }
412     }
413 }
414
415 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
416     SGVec3f orientation, velocity;
417     SGVec3d position;
418
419     if ( _tied_to_listener ) {
420         orientation = _smgr->get_direction();
421         position = SGVec3d::zeros();
422         velocity = _smgr->get_velocity();
423     } else {
424         sample->update_pos_and_orientation();
425         orientation = sample->get_orientation();
426         position = sample->get_position();
427         velocity = sample->get_velocity();
428     }
429
430     if (_smgr->bad_doppler_effect()) {
431         velocity *= 100.0f;
432     }
433
434 #if 0
435     if (length(position) > 20000)
436         printf("%s source and listener distance greater than 20km!\n",
437                _refname.c_str());
438     if (isNaN(toVec3f(position).data())) printf("NaN in source position\n");
439     if (isNaN(orientation.data())) printf("NaN in source orientation\n");
440     if (isNaN(velocity.data())) printf("NaN in source velocity\n");
441 #endif
442
443     unsigned int source = sample->get_source();
444     alSourcefv( source, AL_POSITION, toVec3f(position).data() );
445     alSourcefv( source, AL_VELOCITY, velocity.data() );
446     alSourcefv( source, AL_DIRECTION, orientation.data() );
447     testForALError("position and orientation");
448
449     alSourcef( source, AL_PITCH, sample->get_pitch() );
450     alSourcef( source, AL_GAIN, sample->get_volume() );
451     testForALError("pitch and gain");
452
453     if ( sample->has_static_data_changed() ) {
454         alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
455         alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
456         alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
457         testForALError("audio cone");
458
459         alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
460         alSourcef( source, AL_REFERENCE_DISTANCE,
461                            sample->get_reference_dist() );
462         testForALError("distance rolloff");
463     }
464 }
465
466 bool SGSampleGroup::testForError(void *p, string s)
467 {
468    if (p == NULL) {
469       SG_LOG( SG_SOUND, SG_ALERT, "Error (sample group): " << s);
470       return true;
471    }
472    return false;
473 }
474
475 bool SGSampleGroup::testForALError(string s)
476 {
477     ALenum error = alGetError();
478     if (error != AL_NO_ERROR)  {
479        SG_LOG( SG_SOUND, SG_ALERT, "AL Error (" << _refname << "): "
480                                       << alGetString(error) << " at " << s);
481        return true;
482     }
483     return false;
484 }
485