From 9ecc601a2c14b51f406569267fcef34e989416b6 Mon Sep 17 00:00:00 2001 From: Chris Cannam Date: Fri, 27 May 2022 11:17:20 +0100 Subject: [PATCH] Implement reset --- src/finer/PhaseAdvance.h | 8 ++++++++ src/finer/R3StretcherImpl.cpp | 27 +++++++++++++++++++++------ src/finer/R3StretcherImpl.h | 18 ++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/finer/PhaseAdvance.h b/src/finer/PhaseAdvance.h index 624cfa0..0d26320 100644 --- a/src/finer/PhaseAdvance.h +++ b/src/finer/PhaseAdvance.h @@ -77,6 +77,14 @@ public: deallocate_channels(m_prevOutPhase, ch); deallocate_channels(m_unlocked, ch); } + + void reset() { + size_t ch = m_parameters.channels; + v_zero_channels(m_prevPeaks, ch, m_binCount); + v_zero_channels(m_prevInMag, ch, m_binCount); + v_zero_channels(m_prevInPhase, ch, m_binCount); + v_zero_channels(m_prevOutPhase, ch, m_binCount); + } void advance(double *const *outPhase, const double *const *mag, diff --git a/src/finer/R3StretcherImpl.cpp b/src/finer/R3StretcherImpl.cpp index 8b88fb0..9491f03 100644 --- a/src/finer/R3StretcherImpl.cpp +++ b/src/finer/R3StretcherImpl.cpp @@ -195,7 +195,19 @@ R3StretcherImpl::getChannelCount() const void R3StretcherImpl::reset() { - //!!! + m_calculator->reset(); + m_resampler->reset(); + + for (auto &it : m_scaleData) { + it.second->guided.reset(); + } + + for (auto &cd : m_channelData) { + cd->reset(); + } + + m_prevInhop = m_inhop; + m_prevOuthop = int(round(m_inhop * getEffectiveRatio())); } size_t @@ -303,8 +315,6 @@ R3StretcherImpl::consume() // advanced the input and output since the previous frame, not the // distances we are about to advance them, so they use the m_prev // values. - -// std::cout << "outhop = " << outhop << std::endl; if (inhop != m_prevInhop) { std::cout << "Note: inhop has changed from " << m_prevInhop @@ -457,7 +467,10 @@ R3StretcherImpl::analyseChannel(int c, int inhop, int prevInhop, int prevOuthop) // rather than classification) anew rather than reuse the previous // readahead. Pity... - if (inhop != prevInhop) { + bool haveValidReadahead = cd->haveReadahead; + if (inhop != prevInhop) haveValidReadahead = false; + + if (!haveValidReadahead) { m_scaleData.at(classify)->analysisWindow.cut (buf + (longest - classify) / 2, classifyScale->timeDomain.data()); @@ -477,7 +490,7 @@ R3StretcherImpl::analyseChannel(int c, int inhop, int prevInhop, int prevOuthop) v_fftshift(readahead.timeDomain.data(), classify); - if (inhop == prevInhop) { + if (haveValidReadahead) { v_copy(classifyScale->mag.data(), readahead.mag.data(), classifyScale->bufSize); @@ -520,13 +533,15 @@ R3StretcherImpl::analyseChannel(int c, int inhop, int prevInhop, int prevOuthop) } } + cd->haveReadahead = true; + // For the others (and the classify if the inhop has changed) we // operate directly in the scale data and restrict the range for // cartesian-polar conversion for (auto &it: cd->scales) { int fftSize = it.first; - if (fftSize == classify && inhop == prevInhop) continue; + if (fftSize == classify && haveValidReadahead) continue; auto &scale = it.second; v_fftshift(scale->timeDomain.data(), fftSize); diff --git a/src/finer/R3StretcherImpl.h b/src/finer/R3StretcherImpl.h index b14160c..c25bbb6 100644 --- a/src/finer/R3StretcherImpl.h +++ b/src/finer/R3StretcherImpl.h @@ -119,6 +119,11 @@ protected: accumulator(_longestFftSize, 0.f) { } + void reset() { + v_zero(prevMag.data(), prevMag.size()); + v_zero(accumulator.data(), accumulator.size()); + } + private: ChannelScaleData(const ChannelScaleData &) =delete; ChannelScaleData &operator=(const ChannelScaleData &) =delete; @@ -127,6 +132,7 @@ protected: struct ChannelData { std::map> scales; ClassificationReadaheadData readahead; + bool haveReadahead; std::unique_ptr segmenter; BinSegmenter::Segmentation segmentation; BinSegmenter::Segmentation prevSegmentation; @@ -143,6 +149,7 @@ protected: int outRingBufferSize) : scales(), readahead(segmenterParameters.fftSize), + haveReadahead(false), segmenter(new BinSegmenter(segmenterParameters, classifierParameters)), segmentation(), prevSegmentation(), nextSegmentation(), @@ -150,6 +157,17 @@ protected: resampled(outRingBufferSize, 0.f), inbuf(new RingBuffer(inRingBufferSize)), outbuf(new RingBuffer(outRingBufferSize)) { } + void reset() { + haveReadahead = false; + segmentation = BinSegmenter::Segmentation(); + prevSegmentation = BinSegmenter::Segmentation(); + nextSegmentation = BinSegmenter::Segmentation(); + inbuf->reset(); + outbuf->reset(); + for (auto &s : scales) { + s.second->reset(); + } + } }; struct ChannelAssembly {