From c5f43ca68835d6e35fafc75b9c32e3d78b9b5533 Mon Sep 17 00:00:00 2001 From: curt Date: Mon, 11 Mar 2002 22:55:52 +0000 Subject: [PATCH] Patches from Erik. --- src/Sound/fg_sound.cxx | 8 ++-- src/Sound/soundmgr.cxx | 94 +++++++++++++++++++++--------------------- src/Sound/soundmgr.hxx | 28 +++++++++++-- 3 files changed, 75 insertions(+), 55 deletions(-) diff --git a/src/Sound/fg_sound.cxx b/src/Sound/fg_sound.cxx index 9d193cc0c..6fe6d37a7 100644 --- a/src/Sound/fg_sound.cxx +++ b/src/Sound/fg_sound.cxx @@ -279,7 +279,7 @@ FGSound::update (int dt) // // 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); @@ -298,7 +298,7 @@ FGSound::update (int dt) } else { // FLIPFLOP, RAISE, FALL bool check = (int)(_offset + _property->getFloatValue() * _factor); - if ((bool)check == _active) + if (check == _active) return; // @@ -390,9 +390,9 @@ FGSound::update (int dt) _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, diff --git a/src/Sound/soundmgr.cxx b/src/Sound/soundmgr.cxx index aa4b6f4f0..eb51afac6 100644 --- a/src/Sound/soundmgr.cxx +++ b/src/Sound/soundmgr.cxx @@ -34,6 +34,10 @@ #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() ); @@ -60,6 +64,22 @@ FGSimpleSound::~FGSimpleSound() { 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() { @@ -294,66 +314,46 @@ FGSimpleSound *FGSoundMgr::find( const string& refname ) { // 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; } diff --git a/src/Sound/soundmgr.hxx b/src/Sound/soundmgr.hxx index 2c7563905..d47e9730a 100644 --- a/src/Sound/soundmgr.hxx +++ b/src/Sound/soundmgr.hxx @@ -50,11 +50,14 @@ SG_USING_STD(string); // 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: @@ -62,15 +65,29 @@ 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; } @@ -167,6 +184,9 @@ public: // immediate stop playing the sound bool stop( const string& refname ); + + // return the audio scheduler + inline slScheduler *get_scheduler( ) { return audio_sched; }; };