_smgr(NULL),
_refname(""),
_active(false),
+ _pause(false),
_tied_to_listener(false),
_velocity(SGVec3d::zeros()),
_orientation(SGQuatd::zeros()),
_smgr(smgr),
_refname(refname),
_active(false),
+ _pause(false),
_tied_to_listener(false),
_velocity(SGVec3d::zeros()),
_orientation(SGQuatd::zeros()),
void SGSampleGroup::update( double dt ) {
- if ( !_active ) return;
+ if ( !_active || _pause ) return;
testForALError("start of update!!\n");
+ // Delete any OpenAL buffers that might still be in use.
+ unsigned int size = _removed_samples.size();
+ for (unsigned int i=0; i<size; ) {
+ SGSoundSample *sample = _removed_samples[i];
+ ALint result;
+
+ alGetSourcei( sample->get_source(), AL_SOURCE_STATE, &result );
+ if ( result == AL_STOPPED ) {
+ ALuint buffer = sample->get_buffer();
+ alDeleteBuffers( 1, &buffer );
+ testForALError("buffer remove");
+ _removed_samples.erase( _removed_samples.begin()+i );
+ size--;
+ continue;
+ }
+ i++;
+ }
+
sample_map_iterator sample_current = _samples.begin();
sample_map_iterator sample_end = _samples.end();
for ( ; sample_current != sample_end; ++sample_current ) {
return false;
}
- // remove the sources buffer
- _smgr->release_buffer( sample_it->second );
- _samples.erase( refname );
+ _removed_samples.push_back( sample_it->second );
+ _samples.erase( sample_it );
return true;
}
void
SGSampleGroup::suspend ()
{
- _active = false;
+ _pause = true;
sample_map_iterator sample_current = _samples.begin();
sample_map_iterator sample_end = _samples.end();
for ( ; sample_current != sample_end; ++sample_current ) {
}
}
testForALError("resume");
- _active = true;
+ _pause = false;
}
#endif
#include <string>
+#include <vector>
#include <map>
#include <simgear/compiler.h>
using std::map;
using std::string;
-typedef map < string, SGSharedPtr<SGSoundSample> > sample_map;
+typedef SGSharedPtr<SGSoundSample> SGSoundSample_ptr;
+typedef map < string, SGSoundSample_ptr > sample_map;
typedef sample_map::iterator sample_map_iterator;
typedef sample_map::const_iterator const_sample_map_iterator;
bool _active;
private:
+ bool _pause;
float _volume;
bool _tied_to_listener;
SGGeod _position;
sample_map _samples;
+ std::vector<SGSoundSample_ptr> _removed_samples;
bool testForALError(string s);
bool testForError(void *p, string s);
if (_free_sources.size() == 0) {
SG_LOG(SG_GENERAL, SG_ALERT, "Unable to grab any OpenAL sources!");
}
+}
- sample_group_map_iterator sample_grp_current = _sample_groups.begin();
- sample_group_map_iterator sample_grp_end = _sample_groups.end();
- for ( ; sample_grp_current != sample_grp_end; ++sample_grp_current ) {
- SGSampleGroup *sgrp = sample_grp_current->second;
- sgrp->activate();
+void SGSoundMgr::activate() {
+ if ( _working ) {
+ _active = true;
+ sample_group_map_iterator sample_grp_current = _sample_groups.begin();
+ sample_group_map_iterator sample_grp_end = _sample_groups.end();
+ for ( ; sample_grp_current != sample_grp_end; ++sample_grp_current ) {
+ SGSampleGroup *sgrp = sample_grp_current->second;
+ sgrp->activate();
+ }
}
}
}
void SGSoundMgr::suspend() {
- if (_active) {
+ if (_working) {
sample_group_map_iterator sample_grp_current = _sample_groups.begin();
sample_group_map_iterator sample_grp_end = _sample_groups.end();
for ( ; sample_grp_current != sample_grp_end; ++sample_grp_current ) {
}
void SGSoundMgr::resume() {
- if (!_active) {
+ if (_working) {
sample_group_map_iterator sample_grp_current = _sample_groups.begin();
sample_group_map_iterator sample_grp_end = _sample_groups.end();
for ( ; sample_grp_current != sample_grp_end; ++sample_grp_current ) {
// delete free sources
for (unsigned int i=0; i<_free_sources.size(); i++) {
- ALuint source = _free_sources.at( i );
+ ALuint source = _free_sources[i];
alDeleteSources( 1 , &source );
}
return false;
}
- if (_working) sgrp->activate();
+ if (_active) sgrp->activate();
_sample_groups[refname] = sgrp;
return true;
return false;
}
- _sample_groups.erase( refname );
+ _sample_groups.erase( sample_grp_it );
return true;
}
alSourceStop( source );
testForALError("release source");
- _free_sources.push_back(source);
- _sources_in_use.erase(it, it+1);
+ alSourcei( source, AL_BUFFER, 0 );
+ _free_sources.push_back( source );
+ _sources_in_use.erase( it );
}
}
buffer_it->second.refctr--;
if (buffer_it->second.refctr == 0) {
ALuint buffer = buffer_it->second.id;
- _buffers.erase( sample_name );
alDeleteBuffers(1, &buffer);
+ _buffers.erase( buffer_it );
testForALError("release buffer");
}
}
/**
* Set the sound manager to a working condition.
*/
- inline void activate() { _active = true; }
+ void activate();
/**
* Test is the sound manager is in an active and working condition.
* @return true is the sound manager is active
*/
- inline bool is_active() const { return (_working && _active); }
+ inline bool is_active() const { return _active; }
/**
* Register a sample group to the sound manager.