Compensate initial buffering of anti-alias filter and intepolator.

This avoids losing first few dozen of samples from beginning of the stream.

Signed-off-by: Olli Parviainen <oparviai at iki.fi>
This commit is contained in:
Olli Parviainen 2020-06-30 14:16:03 +03:00
parent 308c3484f6
commit f382149086
7 changed files with 39 additions and 1 deletions

View File

@ -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);
};
}

View File

@ -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;
}

View File

@ -56,6 +56,11 @@ protected:
public:
InterpolateCubic();
int getLatency() const
{
return 1;
}
};
}

View File

@ -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;
}
};
}

View File

@ -61,6 +61,11 @@ protected:
public:
InterpolateShannon();
int getLatency() const
{
return 3;
}
};
}

View File

@ -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);
}

View File

@ -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();