#define FG_SOUND_SAFETY_MULT 3
#define FG_MAX_SOUND_SAFETY ( 1.0 / FG_SOUND_SAFETY_MULT )
+//
+// SimpleSound
+//
+
// constructor
-FGSimpleSound::FGSimpleSound( string file ) {
+FGSimpleSound::FGSimpleSound( string file )
+ : requests(0),
+ volume(1.0),
+ pitch(1.0)
+{
SGPath slfile( globals->get_fg_root() );
slfile.append( file );
sample = new slSample ( (char *)slfile.c_str() );
volume_envelope->setStep ( 0, 0.01, 1.0 );
}
-FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len ) {
+FGSimpleSound::FGSimpleSound( unsigned char *buffer, int len )
+ : requests(0),
+ volume(1.0),
+ pitch(1.0)
+{
sample = new slSample ( buffer, len );
pitch_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
volume_envelope = new slEnvelope( 1, SL_SAMPLE_ONE_SHOT );
delete sample;
}
+void FGSimpleSound::play( slScheduler *sched, bool looped ) {
+
+ requests++;
+ if (requests > 1)
+ return;
+
+ // sched->stopSample(sample);
+ 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 );
+}
+
+//
+// Sound Manager
+//
// constructor
FGSoundMgr::FGSoundMgr() {
// tell the scheduler to play the indexed sample in a continuous
// loop
bool FGSoundMgr::play_looped( const string& refname ) {
- sound_map_iterator it = sounds.find( refname );
- if ( it != sounds.end() ) {
- FGSimpleSound *sample = it->second;
- audio_sched->loopSample( sample->get_sample() );
- audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
- sample->get_pitch_envelope(),
- SL_PITCH_ENVELOPE );
- audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
- sample->get_volume_envelope(),
- SL_VOLUME_ENVELOPE );
-
- return true;
- } else {
- return false;
- }
+ FGSimpleSound *sample;
+
+ if ((sample = find( refname )) == NULL)
+ return false;
+
+ sample->play(audio_sched, true);
+ return true;
}
// tell the scheduler to play the indexed sample once
bool FGSoundMgr::play_once( const string& refname ) {
- sound_map_iterator it = sounds.find( refname );
- if ( it != sounds.end() ) {
- FGSimpleSound *sample = it->second;
- audio_sched->stopSample( sample->get_sample() );
- audio_sched->playSample( sample->get_sample() );
- audio_sched->addSampleEnvelope( sample->get_sample(), 0, 0,
- sample->get_pitch_envelope(),
- SL_PITCH_ENVELOPE );
- audio_sched->addSampleEnvelope( sample->get_sample(), 0, 1,
- sample->get_volume_envelope(),
- SL_VOLUME_ENVELOPE );
-
- return true;
- } else {
- return false;
- }
+ FGSimpleSound *sample;
+
+ if ((sample = find( refname )) == NULL)
+ return false;
+
+ sample->play(audio_sched, false);
+ return true;
}
// return true of the specified sound is currently being played
bool FGSoundMgr::is_playing( const string& refname ) {
- sound_map_iterator it = sounds.find( refname );
- if ( it != sounds.end() ) {
- FGSimpleSound *sample = it->second;
- return (sample->get_sample()->getPlayCount() > 0 );
- return true;
- } else {
- return false;
- }
+ FGSimpleSound *sample;
+
+ if ((sample = find( refname )) == NULL)
+ return false;
+
+ return sample->is_playing();
}
// immediate stop playing the sound
bool FGSoundMgr::stop( const string& refname ) {
- sound_map_iterator it = sounds.find( refname );
- if ( it != sounds.end() ) {
- FGSimpleSound *sample = it->second;
- audio_sched->stopSample( sample->get_sample() );
- return true;
- } else {
- return false;
- }
+ FGSimpleSound *sample;
+
+ if ((sample = find( refname )) == NULL)
+ return false;
+
+ audio_sched->stopSample( sample->get_sample() );
+ return true;
}