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@363: c@363: using std::vector; c@362: 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_buffer; 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@362: c@362: KaiserWindow kw(params); c@362: SincWindow sw(m_filterLength, peakToPole * 2); c@362: c@362: double *filter = new double[m_filterLength]; c@362: for (int i = 0; i < m_filterLength; ++i) filter[i] = 1.0; c@362: sw.cut(filter); c@362: kw.cut(filter); c@362: c@362: int inputSpacing = m_targetRate / m_gcd; c@362: int outputSpacing = m_sourceRate / m_gcd; c@362: c@362: m_latency = int((m_filterLength / 2) / outputSpacing); c@362: c@362: m_bufferLength = 0; 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@362: p.drop = int(ceil(std::max(0, outputSpacing - phase) / inputSpacing)); c@362: p.take = int((outputSpacing + c@362: ((m_filterLength - 1 - phase) % inputSpacing)) c@362: / outputSpacing); c@362: c@362: int filtZipLength = int(ceil((m_filterLength - phase) / inputSpacing)); c@362: if (filtZipLength > m_bufferLength) { c@362: m_bufferLength = filtZipLength; c@362: } c@362: 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: delete[] filter; 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@362: m_phase = m_filterLength % inputSpacing; c@362: m_buffer = new double[m_bufferLength]; c@362: for (int i = 0; i < m_bufferLength; ++i) m_buffer[i] = 0.0; c@362: } c@362: c@362: double c@362: Resampler::reconstructOne(const double **srcptr) c@362: { c@362: Phase &pd = m_phaseData[m_phase]; c@362: double *filt = pd.filter.data(); c@362: int n = pd.filter.size(); c@362: double v = 0.0; c@362: for (int i = 0; i < n; ++i) { c@362: v += m_buffer[i] * filt[i]; c@362: } c@362: for (int i = pd.drop; i < n; ++i) { c@362: m_buffer[i - pd.drop] = m_buffer[i]; c@362: } c@362: for (int i = 0; i < pd.take; ++i) { c@362: m_buffer[n - pd.drop + i] = **srcptr; c@362: ++ *srcptr; c@362: } c@362: m_phase = pd.nextPhase; c@362: return v; c@362: } c@362: c@362: int c@362: Resampler::process(const double *src, double *dst, int n) c@362: { c@362: int m = 0; c@362: const double *srcptr = src; c@362: c@362: while (n > m_phaseData[m_phase].take) { c@362: std::cerr << "n = " << n << ", m = " << m << ", take = " << m_phaseData[m_phase].take << std::endl; c@362: n -= m_phaseData[m_phase].take; c@362: dst[m] = reconstructOne(&srcptr); c@362: std::cerr << "n -> " << n << std::endl; c@362: ++m; c@362: } c@362: c@362: //!!! save any excess c@362: c@362: return m; c@362: } c@362: 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@363: int m = int(ceil((n * targetRate) / sourceRate)); c@363: int m1 = m + latency; c@363: int n1 = int((m1 * sourceRate) / targetRate); c@363: c@363: vector pad(n1 - n, 0.0); c@363: vector out(m1, 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@363: return vector(out.begin() + latency, out.begin() + got); c@363: } c@363: