// constructor
FGSimpleSound::FGSimpleSound( string file )
: pitch(1.0),
- volume(1.0),
- requests(0)
+ volume(1.0)
{
SGPath slfile( globals->get_fg_root() );
slfile.append( file );
FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len )
: pitch(1.0),
- volume(1.0),
- requests(0)
+ volume(1.0)
{
sample = new slSample ( buffer, len );
pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
void FGSimpleSound::play( slScheduler *sched, bool looped ) {
- requests++;
- if (requests > 1)
- return;
-
- // sched->stopSample(sample);
- if (looped)
- sched->loopSample(sample);
- else
- sched->playSample(sample);
+ // make sure sound isn't already playing
+ if ( sample->getPlayCount() > 0 ) {
+ sched->stopSample(sample);
+ // return;
+ }
+
+ if ( looped ) {
+ sched->loopSample(sample);
+ } else {
+ sched->playSample(sample);
+ }
sched->addSampleEnvelope(sample, 0, 0, pitch_envelope, SL_PITCH_ENVELOPE);
sched->addSampleEnvelope(sample, 0, 1, volume_envelope, SL_VOLUME_ENVELOPE);
void FGSimpleSound::stop( slScheduler *sched, bool quick ) {
- if (quick)
- requests = 0;
- else
- if (--requests < 0)
- requests = 0;
-
- if (requests > 0)
- return;
-
sched->stopSample( sample );
}
sample_map_iterator sample_end = samples.end();
for ( ; sample_current != sample_end; ++sample_current ) {
sample_ref *sr = sample_current->second;
+
+ audio_sched->stopSample(sr->sample);
delete sr->sample;
delete sr;
}
sound_map_iterator sound_end = sounds.end();
for ( ; sound_current != sound_end; ++sound_current ) {
FGSimpleSound *s = sound_current->second;
+
+ audio_sched->stopSample(s->get_sample());
+ delete s->get_sample();
delete s;
}
// initialize the sound manager
void FGSoundMgr::init() {
- last.stamp();
safety = FG_MAX_SOUND_SAFETY;
// audio_mixer -> setMasterVolume ( 80 ) ; /* 80% of max volume. */
sample_map_iterator sample_end = samples.end();
for ( ; sample_current != sample_end; ++sample_current ) {
sample_ref *sr = sample_current->second;
+
+ audio_sched->stopSample(sr->sample);
delete sr->sample;
delete sr;
}
sound_map_iterator sound_end = sounds.end();
for ( ; sound_current != sound_end; ++sound_current ) {
FGSimpleSound *s = sound_current->second;
+
+ audio_sched->stopSample(s->get_sample());
delete s->get_sample();
delete s;
}
}
+
void FGSoundMgr::bind ()
{
// no properties yet
}
+
void FGSoundMgr::unbind ()
{
// no properties yet
// run the audio scheduler
-void FGSoundMgr::update(int dt) {
- SGTimeStamp current;
- current.stamp();
-
- double elapsed = (double)(current - last) / 1000000.0;
- last = current;
-
- if ( elapsed > safety ) {
- safety = elapsed;
+void FGSoundMgr::update( double dt ) {
+ if ( dt > safety ) {
+ safety = dt;
} else {
- safety = safety * 0.99 + elapsed * 0.01;
+ safety = safety * 0.99 + dt * 0.01;
}
if ( safety > FG_MAX_SOUND_SAFETY ) {
safety = FG_MAX_SOUND_SAFETY;
}
+void
+FGSoundMgr::pause ()
+{
+ audio_sched->pauseSample(0, 0);
+}
+
+
+void
+FGSoundMgr::resume ()
+{
+ audio_sched->resumeSample(0, 0);
+}
+
+
// add a sound effect, return true if successful
bool FGSoundMgr::add( FGSimpleSound *sound, const string& refname ) {
- sample_map_iterator it = samples.find(refname);
- if (it != samples.end())
- return false;
+ sound_map_iterator sound_it = sounds.find( refname );
+ if ( sound_it != sounds.end() ) {
+ // sound already exists
+ return false;
+ }
+
+ sample_map_iterator sample_it = samples.find( refname );
+ if ( sample_it != samples.end() ) {
+ // this sound has existed in the past and it's sample is still
+ // here, delete the sample so we can replace it.
+ samples.erase( sample_it );
+ }
sample_ref *sr = new sample_ref;
return true;
}
+
// add a sound from a file, return the sample if successful, else return NULL
FGSimpleSound *FGSoundMgr::add( const string& refname, const string &file ) {
FGSimpleSound *sound;
return sound;
}
+
// remove a sound effect, return true if successful
bool FGSoundMgr::remove( const string& refname ) {
// delete sample;
sounds.erase( it );
+ // cout << "sndmgr: removed -> " << refname << endl;
return true;
- } else {
- return false;
+ } else {
+ // cout << "sndmgr: failed remove -> " << refname << endl;
+ return false;
}
}
sound_map_iterator it = sounds.find( refname );
if ( it != sounds.end() ) {
return it->second;
- } else {
+ } else {
return NULL;
}
}
if ((sample = find( refname )) == NULL)
return false;
- return sample->is_playing();
+ return (sample->get_sample()->getPlayCount() > 0 );
}