diff --git a/include/FIFOSampleBuffer.h b/include/FIFOSampleBuffer.h index de298dd..d792e6a 100644 --- a/include/FIFOSampleBuffer.h +++ b/include/FIFOSampleBuffer.h @@ -170,6 +170,9 @@ public: /// allow trimming (downwards) amount of samples in pipeline. /// Returns adjusted amount of samples uint adjustAmountOfSamples(uint numSamples); + + /// Add silence to end of buffer + void addSilent(uint nSamples); }; } diff --git a/source/SoundTouch/FIFOSampleBuffer.cpp b/source/SoundTouch/FIFOSampleBuffer.cpp index 8341163..15d1fc7 100644 --- a/source/SoundTouch/FIFOSampleBuffer.cpp +++ b/source/SoundTouch/FIFOSampleBuffer.cpp @@ -265,3 +265,11 @@ uint FIFOSampleBuffer::adjustAmountOfSamples(uint numSamples) } return samplesInBuffer; } + + +/// Add silence to end of buffer +void FIFOSampleBuffer::addSilent(uint nSamples) +{ + memset(ptrEnd(nSamples), 0, sizeof(SAMPLETYPE) * nSamples * channels); + samplesInBuffer += nSamples; +} diff --git a/source/SoundTouch/InterpolateCubic.h b/source/SoundTouch/InterpolateCubic.h index 4578210..f390fda 100644 --- a/source/SoundTouch/InterpolateCubic.h +++ b/source/SoundTouch/InterpolateCubic.h @@ -56,6 +56,11 @@ protected: public: InterpolateCubic(); + + int getLatency() const + { + return 1; + } }; } diff --git a/source/SoundTouch/InterpolateLinear.h b/source/SoundTouch/InterpolateLinear.h index faa2e2c..286e800 100644 --- a/source/SoundTouch/InterpolateLinear.h +++ b/source/SoundTouch/InterpolateLinear.h @@ -60,6 +60,11 @@ public: /// Sets new target rate. Normal rate = 1.0, smaller values represent slower /// rate, larger faster rates. virtual void setRate(double newRate); + + int getLatency() const + { + return 0; + } }; @@ -81,6 +86,11 @@ protected: public: InterpolateLinearFloat(); + + int getLatency() const + { + return 0; + } }; } diff --git a/source/SoundTouch/InterpolateShannon.h b/source/SoundTouch/InterpolateShannon.h index c621cf1..fc9a947 100644 --- a/source/SoundTouch/InterpolateShannon.h +++ b/source/SoundTouch/InterpolateShannon.h @@ -61,6 +61,11 @@ protected: public: InterpolateShannon(); + + int getLatency() const + { + return 3; + } }; } diff --git a/source/SoundTouch/RateTransposer.cpp b/source/SoundTouch/RateTransposer.cpp index 2efaf04..798a1f5 100644 --- a/source/SoundTouch/RateTransposer.cpp +++ b/source/SoundTouch/RateTransposer.cpp @@ -61,6 +61,7 @@ RateTransposer::RateTransposer() : FIFOProcessor(&outputBuffer) // Instantiates the anti-alias filter pAAFilter = new AAFilter(64); pTransposer = TransposerBase::newInstance(); + clear(); } @@ -192,6 +193,10 @@ void RateTransposer::clear() outputBuffer.clear(); midBuffer.clear(); inputBuffer.clear(); + + // prefill buffer to avoid losing first samples at beginning of stream + int prefill = getLatency(); + inputBuffer.addSilent(prefill); } @@ -209,7 +214,8 @@ int RateTransposer::isEmpty() const /// Return approximate initial input-output latency int RateTransposer::getLatency() const { - return (bUseAAFilter) ? pAAFilter->getLength() : 0; + return pTransposer->getLatency() + + ((bUseAAFilter) ? (pAAFilter->getLength() / 2) : 0); } diff --git a/source/SoundTouch/RateTransposer.h b/source/SoundTouch/RateTransposer.h index 52b7441..7948d44 100644 --- a/source/SoundTouch/RateTransposer.h +++ b/source/SoundTouch/RateTransposer.h @@ -83,6 +83,7 @@ public: virtual int transpose(FIFOSampleBuffer &dest, FIFOSampleBuffer &src); virtual void setRate(double newRate); virtual void setChannels(int channels); + virtual int getLatency() const = 0; // static factory function static TransposerBase *newInstance();