From 67a98234345468da1c768a78c12b7e14c1d80129 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sat, 11 Aug 2018 23:41:45 -0400 Subject: [PATCH] split out function AdvancedTrackTime; more caution about termination --- src/AudioIO.cpp | 36 ++++++++++++++++++++++-------------- src/AudioIO.h | 7 +++++++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/AudioIO.cpp b/src/AudioIO.cpp index 05422afec..64713bf37 100644 --- a/src/AudioIO.cpp +++ b/src/AudioIO.cpp @@ -5646,23 +5646,18 @@ bool AudioIO::PlaybackSchedule::Overruns( double trackTime ) const return (ReversedTime() ? trackTime <= mT1 : trackTime >= mT1); } -void AudioIO::PlaybackSchedule::TrackTimeUpdate(double realElapsed) +double AudioIO::PlaybackSchedule::AdvancedTrackTime( + double time, double realElapsed, double speed ) const { - // Update mTime within the PortAudio callback - - if (Interactive()) - return; - if (ReversedTime()) realElapsed *= -1.0; - auto time = GetTrackTime(); + // Defense against cases that might cause loops not to terminate + if ( fabs(mT0 - mT1) < 1e-9 ) + return mT0; + if (mTimeTrack) { - // Defense against a case that might cause the do-loop not to terminate - if ( fabs(mT0 - mT1) < 1e-9 ) { - SetTrackTime( mT0 ); - return; - } + wxASSERT( speed == 1.0 ); double total; bool foundTotal = false; @@ -5693,7 +5688,7 @@ void AudioIO::PlaybackSchedule::TrackTimeUpdate(double realElapsed) } while ( true ); } else { - time += realElapsed; + time += realElapsed * speed; // Wrap to start if looping if (Looping()) { @@ -5705,7 +5700,20 @@ void AudioIO::PlaybackSchedule::TrackTimeUpdate(double realElapsed) } } } - SetTrackTime( time ); + + return time; +} + +void AudioIO::PlaybackSchedule::TrackTimeUpdate(double realElapsed) +{ + // Update mTime within the PortAudio callback + + if (Interactive()) + return; + + auto time = GetTrackTime(); + auto newTime = AdvancedTrackTime( time, realElapsed, 1.0 ); + SetTrackTime( newTime ); } double AudioIO::PlaybackSchedule::TrackDuration(double realElapsed) const diff --git a/src/AudioIO.h b/src/AudioIO.h index c0e684354..7d211e951 100644 --- a/src/AudioIO.h +++ b/src/AudioIO.h @@ -954,6 +954,13 @@ private: // Returns true if time equals t1 or is on opposite side of t1, to t0 bool Overruns( double trackTime ) const; + // Compute the NEW track time for the given one and a real duration, + // taking into account whether the schedule is for looping + double AdvancedTrackTime( + double trackTime, double realElapsed, double speed) const; + + // Use the function above in the callback after consuming samples from the + // playback ring buffers, during usual straight or looping play void TrackTimeUpdate(double realElapsed); // Convert a nonnegative real duration to an increment of track time