1 // sample_group.cxx -- Manage a group of samples relative to a base position
3 // Written for the new SoundSystem by Erik Hofman, October 2009
5 // Copyright (C) 2009 Erik Hofman <erik@ehofman.com>
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.
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.
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.
24 # include <simgear_config.h>
27 #include <simgear/compiler.h>
29 #include "soundmgr_openal.hxx"
30 #include "sample_group.hxx"
32 SGSampleGroup::SGSampleGroup () :
37 _tied_to_listener(false),
38 _velocity(SGVec3d::zeros()),
39 _orientation(SGQuatd::zeros()),
45 SGSampleGroup::SGSampleGroup ( SGSoundMgr *smgr, const string &refname ) :
50 _tied_to_listener(false),
51 _velocity(SGVec3d::zeros()),
52 _orientation(SGQuatd::zeros()),
55 _smgr->add(this, refname);
59 SGSampleGroup::~SGSampleGroup ()
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;
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 );
78 void SGSampleGroup::update( double dt ) {
80 if ( !_active || _pause ) return;
82 testForALError("start of update!!\n");
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];
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 );
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;
107 if ( !sample->is_valid_source() && sample->is_playing() ) {
109 // a request to start playing a sound has been filed.
111 if ( _smgr->request_buffer(sample) == SGSoundMgr::NO_BUFFER )
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)
119 sample->set_source( source );
121 alSourcei( source, AL_BUFFER, buffer );
122 testForALError("assign buffer to source");
124 sample->set_source( source );
125 update_sample_config( sample );
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");
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
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.
145 sample->no_valid_source();
146 _smgr->release_source( sample->get_source() );
148 update_sample_config( sample );
151 } else if ( sample->is_valid_source() ) {
152 // check if the sound has stopped by itself
154 unsigned int source = sample->get_source();
157 alGetSourcei( source, AL_SOURCE_STATE, &result );
158 if ( result == AL_STOPPED ) {
159 // sample is stoped because it wasn't looping
161 sample->no_valid_source();
162 _smgr->release_source( source );
163 _smgr->release_buffer( sample );
164 remove( sample->get_sample_name() );
167 testForALError("update");
171 // add a sound effect, return true if successful
172 bool SGSampleGroup::add( SGSharedPtr<SGSoundSample> sound, const string& refname ) {
174 sample_map_iterator sample_it = _samples.find( refname );
175 if ( sample_it != _samples.end() ) {
176 // sample name already exists
180 _samples[refname] = sound;
185 // remove a sound effect, return true if successful
186 bool SGSampleGroup::remove( const string &refname ) {
188 sample_map_iterator sample_it = _samples.find( refname );
189 if ( sample_it == _samples.end() ) {
190 // sample was not found
194 if ( sample_it->second->is_valid_buffer() )
195 _removed_samples.push_back( sample_it->second );
196 _samples.erase( sample_it );
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
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
223 return sample_it->second;
227 // stop playing all associated samples
229 SGSampleGroup::suspend ()
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;
237 if ( sample->is_valid_source() && sample->is_playing() ) {
238 alSourcePause( sample->get_source() );
241 testForALError("suspend");
244 // resume playing all associated samples
246 SGSampleGroup::resume ()
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;
253 if ( sample->is_valid_source() && sample->is_playing() ) {
254 alSourcePlay( sample->get_source() );
257 testForALError("resume");
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 );
266 if ( sample == NULL ) {
270 sample->play( looping );
275 // return true of the specified sound is currently being played
276 bool SGSampleGroup::is_playing( const string& refname ) {
277 SGSoundSample *sample = find( refname );
279 if ( sample == NULL ) {
283 return ( sample->is_playing() ) ? true : false;
286 // immediate stop playing the sound
287 bool SGSampleGroup::stop( const string& refname ) {
288 SGSoundSample *sample = find( refname );
290 if ( sample == NULL ) {
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]) )
302 SG_LOG( SG_GENERAL, SG_ALERT, "NAN's found in SampleGroup velocity");
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 );
317 // set the source position of all managed sounds
318 void SGSampleGroup::set_position( const SGGeod& pos ) {
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 );
330 // set the source orientation of all managed sounds
331 void SGSampleGroup::set_orientation( const SGQuatd& ori ) {
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 );
344 void SGSampleGroup::set_volume( float vol )
347 if (_volume < 0.0) _volume = 0.0;
348 if (_volume > 1.0) _volume = 1.0;
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 );
358 void SGSampleGroup::update_sample_config( SGSoundSample *sample ) {
359 if ( sample->is_valid_source() ) {
360 unsigned int source = sample->get_source();
362 if ( _tied_to_listener && _smgr->has_changed() ) {
363 alSourcefv( source, AL_POSITION, _smgr->get_position().data() );
364 alSourcefv( source, AL_VELOCITY, _smgr->get_velocity().data() );
365 alSourcefv( source, AL_DIRECTION, _smgr->get_direction().data() );
367 alSourcefv( source, AL_POSITION, sample->get_position() );
368 alSourcefv( source, AL_VELOCITY, sample->get_velocity() );
369 alSourcefv( source, AL_DIRECTION, sample->get_orientation() );
371 testForALError("position and orientation");
373 alSourcef( source, AL_PITCH, sample->get_pitch() );
374 alSourcef( source, AL_GAIN, sample->get_volume() );
375 testForALError("pitch and gain");
377 if ( sample->has_static_data_changed() ) {
378 alSourcef( source, AL_CONE_INNER_ANGLE, sample->get_innerangle() );
379 alSourcef( source, AL_CONE_OUTER_ANGLE, sample->get_outerangle() );
380 alSourcef( source, AL_CONE_OUTER_GAIN, sample->get_outergain() );
381 testForALError("audio cone");
383 alSourcef( source, AL_MAX_DISTANCE, sample->get_max_dist() );
384 alSourcef( source, AL_REFERENCE_DISTANCE,
385 sample->get_reference_dist() );
386 testForALError("distance rolloff");
391 bool SGSampleGroup::testForError(void *p, string s)
394 SG_LOG( SG_GENERAL, SG_ALERT, "Error (sample group): " << s);
400 bool SGSampleGroup::testForALError(string s)
402 ALenum error = alGetError();
403 if (error != AL_NO_ERROR) {
404 SG_LOG( SG_GENERAL, SG_ALERT, "AL Error (" << _refname << "): "
405 << alGetString(error) << " at " << s);