From 8d5052bf4efa0b90e68e21446ad42d53e8fe5097 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Tue, 2 Jun 2015 22:38:08 -0400 Subject: [PATCH] Avoid assertion violation while scrolling --- src/WaveClip.cpp | 50 +++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/WaveClip.cpp b/src/WaveClip.cpp index a46f91766..c42e75ba5 100644 --- a/src/WaveClip.cpp +++ b/src/WaveClip.cpp @@ -499,7 +499,7 @@ namespace { inline void findCorrection(const sampleCount oldWhere[], int oldLen, int newLen, double t0, double rate, double samplesPerPixel, - double &oldWhere0, double &denom, int &oldX0, int &oldXLast, double &correction) + double &oldWhere0, double &denom, long &oldX0, long &oldXLast, double &correction) { // Mitigate the accumulation of location errors // in copies of copies of ... of caches. @@ -511,25 +511,35 @@ void findCorrection(const sampleCount oldWhere[], int oldLen, int newLen, // Find the length in samples of the old cache. denom = oldWhereLast - oldWhere0; - // Skip unless denom rounds off to at least 1. - if (denom >= 0.5) + // What sample would go in where[0] with no correction? + const double guessWhere0 = t0 * rate; + + // What integer position in the old cache array does that map to? + // (even if it is out of bounds) + oldX0 = floor(0.5 + oldLen * (guessWhere0 - oldWhere0) / denom); + // What sample count would the old cache have put there? + const double where0 = oldWhere0 + double(oldX0) * samplesPerPixel; + // What integer position in the old cache array does our last column + // map to? (even if out of bounds) + oldXLast = floor(0.5 + oldLen * ( + (where0 + double(newLen) * samplesPerPixel - oldWhere0) + / denom + )); + + if ( // Skip if old and new are disjoint: + oldWhereLast <= guessWhere0 || + guessWhere0 + newLen * samplesPerPixel <= oldWhere0 || + // Skip unless denom rounds off to at least 1. + denom < 0.5) + { + correction = 0.0; + } + else { - // What sample would go in where[0] with no correction? - const double guessWhere0 = t0 * rate; - // What integer position in the old cache array does that map to? - // (even if it is out of bounds) - oldX0 = floor(0.5 + oldLen * (guessWhere0 - oldWhere0) / denom); - // What sample count would the old cache have put there? - const double where0 = oldWhere0 + double(oldX0) * samplesPerPixel; // What correction is needed to align the new cache with the old? - correction = where0 - guessWhere0; - wxASSERT(-samplesPerPixel <= correction && correction <= samplesPerPixel); - // What integer position in the old cache array does our last column - // map to? (even if out of bounds) - oldXLast = floor(0.5 + oldLen * ( - (where0 + double(newLen) * samplesPerPixel - oldWhere0) - / denom - )); + const double correction0 = where0 - guessWhere0; + correction = std::max(-samplesPerPixel, std::min(samplesPerPixel, correction0)); + wxASSERT(correction == correction0); } } @@ -599,7 +609,7 @@ bool WaveClip::GetWaveDisplay(WaveDisplay &display, double t0, double oldWhere0 = 0; double denom = 0; - int oldX0 = 0, oldXLast = 0; + long oldX0 = 0, oldXLast = 0; double correction = 0.0; if (match && oldCache->len > 0) { @@ -918,7 +928,7 @@ bool WaveClip::GetSpectrogram(WaveTrackCache &waveTrackCache, // reuse and finding corrections double oldWhere0 = 0; double denom = 0; - int oldX0 = 0, oldXLast = 0; + long oldX0 = 0, oldXLast = 0; double correction = 0.0; if (match &&