//
// If the state changes to false, stop playing.
//
- if (check == 0) {
+ if (!check) {
_active = false;
if (mgr->is_playing(_name)) {
SG_LOG(SG_GENERAL, SG_INFO, "Stopping sound: " << _name);
} else { // FLIPFLOP, RAISE, FALL
bool check = (int)(_offset + _property->getFloatValue() * _factor);
- if ((bool)check == _active)
+ if (check == _active)
return;
//
_active = !_active;
if (_mode == FGSound::ONCE)
- mgr->play_once(_name);
+ _sample->play(mgr->get_scheduler(), false);
else
- mgr->play_looped(_name);
+ _sample->play(mgr->get_scheduler(), true);
SG_LOG(SG_GENERAL, SG_INFO, "Starting audio playback for: " << _name);
SG_LOG(SG_GENERAL, SG_BULK,
#define FG_SOUND_SAFETY_MULT 3
#define FG_MAX_SOUND_SAFETY ( 1.0 / FG_SOUND_SAFETY_MULT )
+//
+// SimpleSound
+//
+
// constructor
FGSimpleSound::FGSimpleSound( string file ) {
SGPath slfile( globals->get_fg_root() );
delete sample;
}
+void FGSimpleSound::play_once( slScheduler *sched ) {
+ sched->stopSample(sample);
+ 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::play_looped( slScheduler *sched ) {
+ sched->loopSample(sample);
+ sched->addSampleEnvelope(sample, 0, 0, pitch_envelope, SL_PITCH_ENVELOPE);
+ sched->addSampleEnvelope(sample, 0, 1, volume_envelope, SL_VOLUME_ENVELOPE);
+}
+
+//
+// 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;
}
// manages everything we need to know for an individual sound sample
class FGSimpleSound {
+private:
+
slSample *sample;
slEnvelope *pitch_envelope;
slEnvelope *volume_envelope;
double pitch;
double volume;
+ int clients;
public:
FGSimpleSound( unsigned char *buffer, int len );
~FGSimpleSound();
+ void play_once( slScheduler *sched );
+ void play_looped( slScheduler *sched );
+
+ inline void play( slScheduler *sched, bool looped ) {
+ if (looped) play_looped( sched );
+ else play_once( sched );
+ }
+ inline void stop( slScheduler *sched ) {
+ sched->stopSample( sample );
+ }
+ inline bool is_playing( ) {
+ return (sample->getPlayCount() > 0 );
+ }
+
inline double get_pitch() const { return pitch; }
inline void set_pitch( double p ) {
- pitch = p;
- pitch_envelope->setStep( 0, 0.01, pitch );
+ pitch = p;
+ pitch_envelope->setStep( 0, 0.01, pitch );
}
inline double get_volume() const { return volume; }
inline void set_volume( double v ) {
- volume = v;
- volume_envelope->setStep( 0, 0.01, volume );
+ volume = v;
+ volume_envelope->setStep( 0, 0.01, volume );
}
inline slSample *get_sample() { return sample; }
// immediate stop playing the sound
bool stop( const string& refname );
+
+ // return the audio scheduler
+ inline slScheduler *get_scheduler( ) { return audio_sched; };
};