From de53f6475726807c2fd6cb8da7ef7feaf9a822a7 Mon Sep 17 00:00:00 2001 From: James Crook Date: Sun, 25 Mar 2018 15:42:14 +0100 Subject: [PATCH] Bug 499 - Normalize includes white space in offset calculation --- src/WaveTrack.cpp | 8 ++++++-- src/WaveTrack.h | 2 +- src/effects/Normalize.cpp | 12 +++++++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/WaveTrack.cpp b/src/WaveTrack.cpp index 4f1aa8873..8fe26772f 100644 --- a/src/WaveTrack.cpp +++ b/src/WaveTrack.cpp @@ -1993,13 +1993,14 @@ float WaveTrack::GetRMS(double t0, double t1, bool mayThrow) const bool WaveTrack::Get(samplePtr buffer, sampleFormat format, sampleCount start, size_t len, fillFormat fill, - bool mayThrow) const + bool mayThrow, sampleCount * pNumCopied) const { // Simple optimization: When this buffer is completely contained within one clip, // don't clear anything (because we won't have to). Otherwise, just clear // everything to be on the safe side. bool doClear = true; bool result = true; + sampleCount samplesCopied = 0; for (const auto &clip: mClips) { if (start >= clip->GetStartSample() && start+len <= clip->GetEndSample()) @@ -2062,9 +2063,12 @@ bool WaveTrack::Get(samplePtr buffer, sampleFormat format, SAMPLE_SIZE(format)), format, inclipDelta, samplesToCopy.as_size_t(), mayThrow )) result = false; + else + samplesCopied += samplesToCopy; } } - + if( pNumCopied ) + *pNumCopied = samplesCopied; return result; } diff --git a/src/WaveTrack.h b/src/WaveTrack.h index 07afb95de..a2c93c2d4 100644 --- a/src/WaveTrack.h +++ b/src/WaveTrack.h @@ -253,7 +253,7 @@ private: /// bool Get(samplePtr buffer, sampleFormat format, sampleCount start, size_t len, - fillFormat fill = fillZero, bool mayThrow = true) const; + fillFormat fill = fillZero, bool mayThrow = true, sampleCount * pNumCopied = nullptr) const; void Set(samplePtr buffer, sampleFormat format, sampleCount start, size_t len); diff --git a/src/effects/Normalize.cpp b/src/effects/Normalize.cpp index 0c7ef6550..109bb1213 100644 --- a/src/effects/Normalize.cpp +++ b/src/effects/Normalize.cpp @@ -412,6 +412,9 @@ bool EffectNormalize::AnalyseDC(const WaveTrack * track, const wxString &msg, mSum = 0.0; // dc offset inits mCount = 0; + sampleCount blockSamples; + sampleCount totalSamples = 0; + //Go through the track one buffer at a time. s counts which //sample the current buffer starts at. auto s = start; @@ -424,7 +427,8 @@ bool EffectNormalize::AnalyseDC(const WaveTrack * track, const wxString &msg, ); //Get the samples from the track and put them in the buffer - track->Get((samplePtr) buffer.get(), floatSample, s, block); + track->Get((samplePtr) buffer.get(), floatSample, s, block, fillZero, true, &blockSamples); + totalSamples += blockSamples; //Process the buffer. AnalyzeData(buffer.get(), block); @@ -439,8 +443,10 @@ bool EffectNormalize::AnalyseDC(const WaveTrack * track, const wxString &msg, break; } } - - offset = -mSum / mCount.as_double(); // calculate actual offset (amount that needs to be added on) + if( totalSamples > 0 ) + offset = -mSum / totalSamples.as_double(); // calculate actual offset (amount that needs to be added on) + else + offset = 0.0; //Return true because the effect processing succeeded ... unless cancelled return rc;