c@362: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@362: c@362: #include "Resampler.h" c@362: c@362: #include "qm-dsp/maths/MathUtilities.h" c@362: #include "qm-dsp/base/KaiserWindow.h" c@362: #include "qm-dsp/base/SincWindow.h" c@362: c@362: #include c@363: #include c@370: #include c@363: c@363: using std::vector; c@370: using std::map; c@362: c@366: //#define DEBUG_RESAMPLER 1 c@366: c@362: Resampler::Resampler(int sourceRate, int targetRate) : c@362: m_sourceRate(sourceRate), c@362: m_targetRate(targetRate) c@362: { c@362: initialise(); c@362: } c@362: c@362: Resampler::~Resampler() c@362: { c@362: delete[] m_phaseData; c@362: } c@362: c@362: void c@362: Resampler::initialise() c@362: { c@362: int higher = std::max(m_sourceRate, m_targetRate); c@362: int lower = std::min(m_sourceRate, m_targetRate); c@362: c@362: m_gcd = MathUtilities::gcd(lower, higher); c@362: c@362: int peakToPole = higher / m_gcd; c@362: c@362: KaiserWindow::Parameters params = c@362: KaiserWindow::parametersForBandwidth(100, 0.02, peakToPole); c@362: c@362: params.length = c@362: (params.length % 2 == 0 ? params.length + 1 : params.length); c@362: c@362: m_filterLength = params.length; c@370: c@362: KaiserWindow kw(params); c@362: SincWindow sw(m_filterLength, peakToPole * 2); c@362: c@370: vector filter(m_filterLength, 0.0); c@362: for (int i = 0; i < m_filterLength; ++i) filter[i] = 1.0; c@370: sw.cut(filter.data()); c@370: kw.cut(filter.data()); c@362: c@362: int inputSpacing = m_targetRate / m_gcd; c@362: int outputSpacing = m_sourceRate / m_gcd; c@362: c@366: #ifdef DEBUG_RESAMPLER c@366: std::cerr << "resample " << m_sourceRate << " -> " << m_targetRate c@366: << ": inputSpacing " << inputSpacing << ", outputSpacing " c@366: << outputSpacing << ": filter length " << m_filterLength c@366: << std::endl; c@366: #endif c@362: c@362: m_phaseData = new Phase[inputSpacing]; c@362: c@362: for (int phase = 0; phase < inputSpacing; ++phase) { c@362: c@362: Phase p; c@362: c@362: p.nextPhase = phase - outputSpacing; c@362: while (p.nextPhase < 0) p.nextPhase += inputSpacing; c@362: p.nextPhase %= inputSpacing; c@362: c@366: p.drop = int(ceil(std::max(0.0, double(outputSpacing - phase)) c@366: / inputSpacing)); c@362: c@366: int filtZipLength = int(ceil(double(m_filterLength - phase) c@366: / inputSpacing)); c@362: for (int i = 0; i < filtZipLength; ++i) { c@362: p.filter.push_back(filter[i * inputSpacing + phase]); c@362: } c@362: c@362: m_phaseData[phase] = p; c@362: } c@362: c@362: // The May implementation of this uses a pull model -- we ask the c@362: // resampler for a certain number of output samples, and it asks c@362: // its source stream for as many as it needs to calculate c@362: // those. This means (among other things) that the source stream c@362: // can be asked for enough samples up-front to fill the buffer c@362: // before the first output sample is generated. c@362: // c@362: // In this implementation we're using a push model in which a c@362: // certain number of source samples is provided and we're asked c@362: // for as many output samples as that makes available. But we c@362: // can't return any samples from the beginning until half the c@362: // filter length has been provided as input. This means we must c@362: // either return a very variable number of samples (none at all c@362: // until the filter fills, then half the filter length at once) or c@362: // else have a lengthy declared latency on the output. We do the c@362: // latter. (What do other implementations do?) c@362: c@366: m_phase = (m_filterLength/2) % inputSpacing; c@366: c@366: m_buffer = vector(m_phaseData[0].filter.size(), 0); c@370: m_bufferOrigin = 0; c@366: c@366: m_latency = c@366: ((m_buffer.size() * inputSpacing) - (m_filterLength/2)) / outputSpacing c@366: + m_phase; c@366: c@366: #ifdef DEBUG_RESAMPLER c@366: std::cerr << "initial phase " << m_phase << " (as " << (m_filterLength/2) << " % " << inputSpacing << ")" c@366: << ", latency " << m_latency << std::endl; c@366: #endif c@362: } c@362: c@362: double c@366: Resampler::reconstructOne() c@362: { c@362: Phase &pd = m_phaseData[m_phase]; c@366: double v = 0.0; c@362: int n = pd.filter.size(); c@370: const double *const __restrict__ buf = m_buffer.data() + m_bufferOrigin; c@370: const double *const __restrict__ filt = pd.filter.data(); c@362: for (int i = 0; i < n; ++i) { c@370: // NB gcc can only vectorize this with -ffast-math c@370: v += buf[i] * filt[i]; c@362: } c@370: m_bufferOrigin += pd.drop; c@366: m_phase = pd.nextPhase; c@362: return v; c@362: } c@362: c@362: int c@366: Resampler::process(const double *src, double *dst, int n) c@362: { c@366: for (int i = 0; i < n; ++i) { c@366: m_buffer.push_back(src[i]); c@362: } c@362: c@366: int maxout = int(ceil(double(n) * m_targetRate / m_sourceRate)); c@366: int outidx = 0; c@364: c@366: #ifdef DEBUG_RESAMPLER c@366: std::cerr << "process: buf siz " << m_buffer.size() << " filt siz for phase " << m_phase << " " << m_phaseData[m_phase].filter.size() << std::endl; c@366: #endif c@366: c@367: double scaleFactor = 1.0; c@367: if (m_targetRate < m_sourceRate) { c@367: scaleFactor = double(m_targetRate) / double(m_sourceRate); c@367: } c@367: c@366: while (outidx < maxout && c@370: m_buffer.size() >= m_phaseData[m_phase].filter.size() + m_bufferOrigin) { c@367: dst[outidx] = scaleFactor * reconstructOne(); c@366: outidx++; c@364: } c@370: c@370: m_buffer = vector(m_buffer.begin() + m_bufferOrigin, m_buffer.end()); c@370: m_bufferOrigin = 0; c@366: c@366: return outidx; c@362: } c@366: c@363: std::vector c@363: Resampler::resample(int sourceRate, int targetRate, const double *data, int n) c@363: { c@363: Resampler r(sourceRate, targetRate); c@363: c@363: int latency = r.getLatency(); c@363: c@368: // latency is the output latency. We need to provide enough c@368: // padding input samples at the end of input to guarantee at c@368: // *least* the latency's worth of output samples. that is, c@368: c@368: int inputPad = int(ceil(double(latency * sourceRate) / targetRate)); c@368: c@368: // that means we are providing this much input in total: c@368: c@368: int n1 = n + inputPad; c@368: c@368: // and obtaining this much output in total: c@368: c@368: int m1 = int(ceil(double(n1 * targetRate) / sourceRate)); c@368: c@368: // in order to return this much output to the user: c@368: c@366: int m = int(ceil(double(n * targetRate) / sourceRate)); c@368: c@370: // std::cerr << "n = " << n << ", sourceRate = " << sourceRate << ", targetRate = " << targetRate << ", m = " << m << ", latency = " << latency << ", m1 = " << m1 << ", n1 = " << n1 << ", n1 - n = " << n1 - n << std::endl; c@363: c@363: vector pad(n1 - n, 0.0); c@368: vector out(m1 + 1, 0.0); c@363: c@363: int got = r.process(data, out.data(), n); c@363: got += r.process(pad.data(), out.data() + got, pad.size()); c@363: c@366: #ifdef DEBUG_RESAMPLER c@366: std::cerr << "resample: " << n << " in, " << got << " out" << std::endl; c@366: for (int i = 0; i < got; ++i) { c@366: if (i % 5 == 0) std::cout << std::endl << i << "... "; c@366: std::cout << (float) out[i] << " "; c@366: } c@366: std::cout << std::endl; c@366: #endif c@366: c@368: int toReturn = got - latency; c@368: if (toReturn > m) toReturn = m; c@368: c@368: return vector(out.begin() + latency, c@368: out.begin() + latency + toReturn); c@363: } c@363: