c@362: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@375: /* c@375: QM DSP Library c@375: c@375: Centre for Digital Music, Queen Mary, University of London. c@375: This file by Chris Cannam. c@375: c@375: This program is free software; you can redistribute it and/or c@375: modify it under the terms of the GNU General Public License as c@375: published by the Free Software Foundation; either version 2 of the c@375: License, or (at your option) any later version. See the file c@375: COPYING included with this distribution for more information. c@375: */ c@362: c@362: #include "Resampler.h" c@362: c@375: #include "maths/MathUtilities.h" c@375: #include "base/KaiserWindow.h" c@375: #include "base/SincWindow.h" c@375: #include "thread/Thread.h" c@362: c@362: #include c@363: #include c@370: #include c@372: #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@374: initialise(100, 0.02); c@374: } c@374: c@374: Resampler::Resampler(int sourceRate, int targetRate, c@374: double snr, double bandwidth) : c@374: m_sourceRate(sourceRate), c@374: m_targetRate(targetRate) c@374: { c@374: initialise(snr, bandwidth); c@362: } c@362: c@362: Resampler::~Resampler() c@362: { c@362: delete[] m_phaseData; c@362: } c@362: c@371: // peakToPole -> length -> beta -> window c@381: static map > > > c@371: knownFilters; c@371: c@371: static Mutex c@371: knownFilterMutex; c@371: c@362: void c@374: Resampler::initialise(double snr, double bandwidth) 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@381: m_peakToPole = higher / m_gcd; c@362: c@381: if (m_targetRate < m_sourceRate) { c@381: // antialiasing filter, should be slightly below nyquist c@381: m_peakToPole = m_peakToPole / (1.0 - bandwidth/2.0); c@381: } c@362: c@362: KaiserWindow::Parameters params = c@381: KaiserWindow::parametersForBandwidth(snr, bandwidth, higher / m_gcd); c@362: c@362: params.length = c@362: (params.length % 2 == 0 ? params.length + 1 : params.length); c@362: c@372: params.length = c@372: (params.length > 200001 ? 200001 : params.length); c@372: c@362: m_filterLength = params.length; c@370: c@371: vector filter; c@371: knownFilterMutex.lock(); c@362: c@381: if (knownFilters[m_peakToPole][m_filterLength].find(params.beta) == c@381: knownFilters[m_peakToPole][m_filterLength].end()) { c@371: c@371: KaiserWindow kw(params); c@381: SincWindow sw(m_filterLength, m_peakToPole * 2); c@371: c@371: filter = vector(m_filterLength, 0.0); c@371: for (int i = 0; i < m_filterLength; ++i) filter[i] = 1.0; c@371: sw.cut(filter.data()); c@371: kw.cut(filter.data()); c@371: c@381: knownFilters[m_peakToPole][m_filterLength][params.beta] = filter; c@371: } c@371: c@381: filter = knownFilters[m_peakToPole][m_filterLength][params.beta]; c@371: knownFilterMutex.unlock(); 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@372: // Now we have a filter of (odd) length flen in which the lower c@372: // sample rate corresponds to every n'th point and the higher rate c@372: // to every m'th where n and m are higher and lower rates divided c@372: // by their gcd respectively. So if x coordinates are on the same c@372: // scale as our filter resolution, then source sample i is at i * c@372: // (targetRate / gcd) and target sample j is at j * (sourceRate / c@372: // gcd). c@372: c@372: // To reconstruct a single target sample, we want a buffer (real c@372: // or virtual) of flen values formed of source samples spaced at c@372: // intervals of (targetRate / gcd), in our example case 3. This c@372: // is initially formed with the first sample at the filter peak. c@372: // c@372: // 0 0 0 0 a 0 0 b 0 c@372: // c@372: // and of course we have our filter c@372: // c@372: // f1 f2 f3 f4 f5 f6 f7 f8 f9 c@372: // c@372: // We take the sum of products of non-zero values from this buffer c@372: // with corresponding values in the filter c@372: // c@372: // a * f5 + b * f8 c@372: // c@372: // Then we drop (sourceRate / gcd) values, in our example case 4, c@372: // from the start of the buffer and fill until it has flen values c@372: // again c@372: // c@372: // a 0 0 b 0 0 c 0 0 c@372: // c@372: // repeat to reconstruct the next target sample c@372: // c@372: // a * f1 + b * f4 + c * f7 c@372: // c@372: // and so on. c@372: // c@372: // Above I said the buffer could be "real or virtual" -- ours is c@372: // virtual. We don't actually store all the zero spacing values, c@372: // except for padding at the start; normally we store only the c@372: // values that actually came from the source stream, along with a c@372: // phase value that tells us how many virtual zeroes there are at c@372: // the start of the virtual buffer. So the two examples above are c@372: // c@372: // 0 a b [ with phase 1 ] c@372: // a b c [ with phase 0 ] c@372: // c@372: // Having thus broken down the buffer so that only the elements we c@372: // need to multiply are present, we can also unzip the filter into c@372: // every-nth-element subsets at each phase, allowing us to do the c@372: // filter multiplication as a simply vector multiply. That is, rather c@372: // than store c@372: // c@372: // f1 f2 f3 f4 f5 f6 f7 f8 f9 c@372: // c@372: // we store separately c@372: // c@372: // f1 f4 f7 c@372: // f2 f5 f8 c@372: // f3 f6 f9 c@372: // c@372: // Each time we complete a multiply-and-sum, we need to work out c@372: // how many (real) samples to drop from the start of our buffer, c@372: // and how many to add at the end of it for the next multiply. We c@372: // know we want to drop enough real samples to move along by one c@372: // computed output sample, which is our outputSpacing number of c@372: // virtual buffer samples. Depending on the relationship between c@372: // input and output spacings, this may mean dropping several real c@372: // samples, one real sample, or none at all (and simply moving to c@372: // a different "phase"). c@372: 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@372: 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@373: // c@372: // We want to make sure the first "real" sample will eventually be c@372: // aligned with the centre sample in the filter (it's tidier, and c@372: // easier to do diagnostic calculations that way). So we need to c@372: // pick the initial phase and buffer fill accordingly. c@372: // c@372: // Example: if the inputSpacing is 2, outputSpacing is 3, and c@372: // filter length is 7, c@372: // c@372: // x x x x a b c ... input samples c@372: // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ... c@372: // i j k l ... output samples c@372: // [--------|--------] <- filter with centre mark c@372: // c@372: // Let h be the index of the centre mark, here 3 (generally c@372: // int(filterLength/2) for odd-length filters). c@372: // c@372: // The smallest n such that h + n * outputSpacing > filterLength c@372: // is 2 (that is, ceil((filterLength - h) / outputSpacing)), and c@372: // (h + 2 * outputSpacing) % inputSpacing == 1, so the initial c@372: // phase is 1. c@372: // c@372: // To achieve our n, we need to pre-fill the "virtual" buffer with c@372: // 4 zero samples: the x's above. This is int((h + n * c@372: // outputSpacing) / inputSpacing). It's the phase that makes this c@372: // buffer get dealt with in such a way as to give us an effective c@372: // index for sample a of 9 rather than 8 or 10 or whatever. c@372: // c@372: // This gives us output latency of 2 (== n), i.e. output samples i c@372: // and j will appear before the one in which input sample a is at c@372: // the centre of the filter. c@372: c@372: int h = int(m_filterLength / 2); c@372: int n = ceil(double(m_filterLength - h) / outputSpacing); c@366: c@372: m_phase = (h + n * outputSpacing) % inputSpacing; c@372: c@372: int fill = (h + n * outputSpacing) / inputSpacing; c@372: c@372: m_latency = n; c@372: c@372: m_buffer = vector(fill, 0); c@370: m_bufferOrigin = 0; 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@372: c@373: assert(n + m_bufferOrigin <= (int)m_buffer.size()); c@372: c@370: const double *const __restrict__ buf = m_buffer.data() + m_bufferOrigin; c@370: const double *const __restrict__ filt = pd.filter.data(); c@372: c@372: // std::cerr << "phase = " << m_phase << ", drop = " << pd.drop << ", buffer for reconstruction starts..."; c@372: // for (int i = 0; i < 20; ++i) { c@372: // if (i % 5 == 0) std::cerr << "\n" << i << " "; c@372: // std::cerr << buf[i] << " "; c@372: // } c@372: // std::cerr << std::endl; c@372: 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@374: 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@381: double scaleFactor = (double(m_targetRate) / m_gcd) / m_peakToPole; 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@373: 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@373: 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@373: int m = int(ceil((double(n) * targetRate) / sourceRate)); c@368: c@373: // std::cerr << "n = " << n << ", sourceRate = " << sourceRate << ", targetRate = " << targetRate << ", m = " << m << ", latency = " << latency << ", inputPad = " << inputPad << ", 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@372: std::cerr << "first 10 in:" << std::endl; c@372: for (int i = 0; i < 10; ++i) { c@372: std::cerr << data[i] << " "; c@372: if (i == 5) std::cerr << std::endl; c@366: } c@372: std::cerr << std::endl; c@366: #endif c@366: c@368: int toReturn = got - latency; c@368: if (toReturn > m) toReturn = m; c@368: c@372: vector sliced(out.begin() + latency, c@368: out.begin() + latency + toReturn); c@372: c@372: #ifdef DEBUG_RESAMPLER c@372: std::cerr << "all out (after latency compensation), length " << sliced.size() << ":"; c@372: for (int i = 0; i < sliced.size(); ++i) { c@372: if (i % 5 == 0) std::cerr << std::endl << i << "... "; c@372: std::cerr << sliced[i] << " "; c@372: } c@372: std::cerr << std::endl; c@372: #endif c@372: c@372: return sliced; c@363: } c@363: