From 93bfc2ea3f7936655258ad81415703d782bd83b3 Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 24 Aug 2007 17:17:09 +0000 Subject: [PATCH] define AudioSound::set_time() to have effect only upon the next call to play() --- panda/src/audio/audioSound.h | 6 +- panda/src/audiotraits/milesAudioSample.cxx | 60 ++++++++++++-------- panda/src/audiotraits/milesAudioSample.h | 2 +- panda/src/audiotraits/milesAudioSequence.cxx | 55 +++++++++++------- panda/src/audiotraits/milesAudioSequence.h | 2 +- panda/src/audiotraits/milesAudioSound.cxx | 19 ++++++- panda/src/audiotraits/milesAudioSound.h | 7 +++ panda/src/audiotraits/milesAudioStream.cxx | 55 +++++++++++------- panda/src/audiotraits/milesAudioStream.h | 2 +- 9 files changed, 133 insertions(+), 75 deletions(-) diff --git a/panda/src/audio/audioSound.h b/panda/src/audio/audioSound.h index 3d6fc645db..eef21bc20b 100644 --- a/panda/src/audio/audioSound.h +++ b/panda/src/audio/audioSound.h @@ -56,9 +56,9 @@ PUBLISHED: // a file. // time in seconds: 0 = beginning; length() = end. // inits to 0.0. - // - Unlike the other get_* and set_* calls for a sound, the - // current time position will change while the sound is playing. - // To play the same sound from a time offset a second time, + // - The current time position will not change while the sound is + // playing; you must call play() again to effect the change. To + // play the same sound from a time offset a second time, // explicitly set the time position again. When looping, the // second and later loops will start from the beginning of the // sound. diff --git a/panda/src/audiotraits/milesAudioSample.cxx b/panda/src/audiotraits/milesAudioSample.cxx index 1f0c347dc0..6f2438b106 100644 --- a/panda/src/audiotraits/milesAudioSample.cxx +++ b/panda/src/audiotraits/milesAudioSample.cxx @@ -102,8 +102,16 @@ play() { set_volume(_volume); set_play_rate(_play_rate); AIL_set_sample_loop_count(_sample, _loop_count); - AIL_start_sample(_sample); + + if (_got_start_time) { + do_set_time(_start_time); + AIL_resume_sample(_sample); + } else { + AIL_start_sample(_sample); + } } + + _got_start_time = false; } } else { // In case _loop_count gets set to forever (zero): @@ -139,30 +147,6 @@ stop() { } } -//////////////////////////////////////////////////////////////////// -// Function: MilesAudioSample::set_time -// Access: Public, Virtual -// Description: -//////////////////////////////////////////////////////////////////// -void MilesAudioSample:: -set_time(float time) { - miles_audio_debug("set_time(time="< max_time) { - milesAudio_cat.warning() - << "set_time(" << time << ") requested for sound of length " - << max_time << "\n"; - time = max_time; - } - - S32 time_ms = (S32)(1000.0f * time); - AIL_set_sample_ms_position(_sample, time_ms); - } -} - //////////////////////////////////////////////////////////////////// // Function: MilesAudioSample::get_time // Access: Public, Virtual @@ -171,6 +155,9 @@ set_time(float time) { float MilesAudioSample:: get_time() const { if (_sample == 0) { + if (_got_start_time) { + return _start_time; + } return 0.0f; } @@ -337,4 +324,27 @@ finish_callback(HSAMPLE sample) { self->_manager->_sounds_finished = true; } +//////////////////////////////////////////////////////////////////// +// Function: MilesAudioSample::do_set_time +// Access: Private +// Description: Sets the start time of an already allocated sample. +//////////////////////////////////////////////////////////////////// +void MilesAudioSample:: +do_set_time(float time) { + miles_audio_debug("do_set_time(time="< max_time) { + milesAudio_cat.warning() + << "set_time(" << time << ") requested for sound of length " + << max_time << "\n"; + time = max_time; + } + + S32 time_ms = (S32)(1000.0f * time); + AIL_set_sample_ms_position(_sample, time_ms); +} + #endif //] diff --git a/panda/src/audiotraits/milesAudioSample.h b/panda/src/audiotraits/milesAudioSample.h index 95c626ab23..c042f88191 100644 --- a/panda/src/audiotraits/milesAudioSample.h +++ b/panda/src/audiotraits/milesAudioSample.h @@ -44,7 +44,6 @@ public: virtual void play(); virtual void stop(); - virtual void set_time(float time=0.0f); virtual float get_time() const; virtual void set_volume(float volume=1.0f); @@ -61,6 +60,7 @@ public: private: void internal_stop(); static void AILCALLBACK finish_callback(HSAMPLE sample); + void do_set_time(float time); PT(MilesAudioManager::SoundData) _sd; HSAMPLE _sample; diff --git a/panda/src/audiotraits/milesAudioSequence.cxx b/panda/src/audiotraits/milesAudioSequence.cxx index 947be878b5..7698e61287 100644 --- a/panda/src/audiotraits/milesAudioSequence.cxx +++ b/panda/src/audiotraits/milesAudioSequence.cxx @@ -98,8 +98,16 @@ play() { set_volume(_volume); set_play_rate(_play_rate); AIL_set_sequence_loop_count(_sequence, _loop_count); - AIL_start_sequence(_sequence); + + if (_got_start_time) { + do_set_time(_start_time); + AIL_resume_sequence(_sequence); + } else { + AIL_start_sequence(_sequence); + } } + + _got_start_time = false; } } else { // In case _loop_count gets set to forever (zero): @@ -135,27 +143,6 @@ stop() { } } -//////////////////////////////////////////////////////////////////// -// Function: MilesAudioSequence::set_time -// Access: Public, Virtual -// Description: -//////////////////////////////////////////////////////////////////// -void MilesAudioSequence:: -set_time(float time) { - miles_audio_debug("set_time(time="<_manager->_sounds_finished = true; } +//////////////////////////////////////////////////////////////////// +// Function: MilesAudioSequence::do_set_time +// Access: Private +// Description: Sets the start time of an already allocated stream. +//////////////////////////////////////////////////////////////////// +void MilesAudioSequence:: +do_set_time(float time) { + miles_audio_debug("do_set_time(time="<_manager->_sounds_finished = true; } +//////////////////////////////////////////////////////////////////// +// Function: MilesAudioStream::do_set_time +// Access: Private +// Description: Sets the start time of an already allocated stream. +//////////////////////////////////////////////////////////////////// +void MilesAudioStream:: +do_set_time(float time) { + nassertv(_stream != 0); + + S32 time_ms = (S32)(1000.0f * time); + + // Ensure we don't inadvertently run off the end of the sound. + S32 length_ms; + AIL_stream_ms_position(_stream, &length_ms, NULL); + time_ms = min(time_ms, length_ms); + + AIL_set_stream_ms_position(_stream, time_ms); +} + #endif //] diff --git a/panda/src/audiotraits/milesAudioStream.h b/panda/src/audiotraits/milesAudioStream.h index fc61782797..2ede11a465 100644 --- a/panda/src/audiotraits/milesAudioStream.h +++ b/panda/src/audiotraits/milesAudioStream.h @@ -44,7 +44,6 @@ public: virtual void play(); virtual void stop(); - virtual void set_time(float time=0.0f); virtual float get_time() const; virtual void set_volume(float volume=1.0f); @@ -59,6 +58,7 @@ public: private: static void AILCALLBACK finish_callback(HSTREAM stream); + void do_set_time(float time); Filename _path; HSTREAM _stream;