annotate dsp/rateconversion/Resampler.cpp @ 363:2fe2ab316c8e

Add one-shot resample function
author Chris Cannam <c.cannam@qmul.ac.uk>
date Sun, 13 Oct 2013 12:47:50 +0100
parents 3953f3ef1b62
children 7fe0da91e9c3
rev   line source
c@362 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@362 2
c@362 3 #include "Resampler.h"
c@362 4
c@362 5 #include "qm-dsp/maths/MathUtilities.h"
c@362 6 #include "qm-dsp/base/KaiserWindow.h"
c@362 7 #include "qm-dsp/base/SincWindow.h"
c@362 8
c@362 9 #include <iostream>
c@363 10 #include <vector>
c@363 11
c@363 12 using std::vector;
c@362 13
c@362 14 Resampler::Resampler(int sourceRate, int targetRate) :
c@362 15 m_sourceRate(sourceRate),
c@362 16 m_targetRate(targetRate)
c@362 17 {
c@362 18 initialise();
c@362 19 }
c@362 20
c@362 21 Resampler::~Resampler()
c@362 22 {
c@362 23 delete[] m_buffer;
c@362 24 delete[] m_phaseData;
c@362 25 }
c@362 26
c@362 27 void
c@362 28 Resampler::initialise()
c@362 29 {
c@362 30 int higher = std::max(m_sourceRate, m_targetRate);
c@362 31 int lower = std::min(m_sourceRate, m_targetRate);
c@362 32
c@362 33 m_gcd = MathUtilities::gcd(lower, higher);
c@362 34
c@362 35 int peakToPole = higher / m_gcd;
c@362 36
c@362 37 KaiserWindow::Parameters params =
c@362 38 KaiserWindow::parametersForBandwidth(100, 0.02, peakToPole);
c@362 39
c@362 40 params.length =
c@362 41 (params.length % 2 == 0 ? params.length + 1 : params.length);
c@362 42
c@362 43 m_filterLength = params.length;
c@362 44
c@362 45 KaiserWindow kw(params);
c@362 46 SincWindow sw(m_filterLength, peakToPole * 2);
c@362 47
c@362 48 double *filter = new double[m_filterLength];
c@362 49 for (int i = 0; i < m_filterLength; ++i) filter[i] = 1.0;
c@362 50 sw.cut(filter);
c@362 51 kw.cut(filter);
c@362 52
c@362 53 int inputSpacing = m_targetRate / m_gcd;
c@362 54 int outputSpacing = m_sourceRate / m_gcd;
c@362 55
c@362 56 m_latency = int((m_filterLength / 2) / outputSpacing);
c@362 57
c@362 58 m_bufferLength = 0;
c@362 59
c@362 60 m_phaseData = new Phase[inputSpacing];
c@362 61
c@362 62 for (int phase = 0; phase < inputSpacing; ++phase) {
c@362 63
c@362 64 Phase p;
c@362 65
c@362 66 p.nextPhase = phase - outputSpacing;
c@362 67 while (p.nextPhase < 0) p.nextPhase += inputSpacing;
c@362 68 p.nextPhase %= inputSpacing;
c@362 69
c@362 70 p.drop = int(ceil(std::max(0, outputSpacing - phase) / inputSpacing));
c@362 71 p.take = int((outputSpacing +
c@362 72 ((m_filterLength - 1 - phase) % inputSpacing))
c@362 73 / outputSpacing);
c@362 74
c@362 75 int filtZipLength = int(ceil((m_filterLength - phase) / inputSpacing));
c@362 76 if (filtZipLength > m_bufferLength) {
c@362 77 m_bufferLength = filtZipLength;
c@362 78 }
c@362 79
c@362 80 for (int i = 0; i < filtZipLength; ++i) {
c@362 81 p.filter.push_back(filter[i * inputSpacing + phase]);
c@362 82 }
c@362 83
c@362 84 m_phaseData[phase] = p;
c@362 85 }
c@362 86
c@362 87 delete[] filter;
c@362 88
c@362 89 // The May implementation of this uses a pull model -- we ask the
c@362 90 // resampler for a certain number of output samples, and it asks
c@362 91 // its source stream for as many as it needs to calculate
c@362 92 // those. This means (among other things) that the source stream
c@362 93 // can be asked for enough samples up-front to fill the buffer
c@362 94 // before the first output sample is generated.
c@362 95 //
c@362 96 // In this implementation we're using a push model in which a
c@362 97 // certain number of source samples is provided and we're asked
c@362 98 // for as many output samples as that makes available. But we
c@362 99 // can't return any samples from the beginning until half the
c@362 100 // filter length has been provided as input. This means we must
c@362 101 // either return a very variable number of samples (none at all
c@362 102 // until the filter fills, then half the filter length at once) or
c@362 103 // else have a lengthy declared latency on the output. We do the
c@362 104 // latter. (What do other implementations do?)
c@362 105
c@362 106 m_phase = m_filterLength % inputSpacing;
c@362 107 m_buffer = new double[m_bufferLength];
c@362 108 for (int i = 0; i < m_bufferLength; ++i) m_buffer[i] = 0.0;
c@362 109 }
c@362 110
c@362 111 double
c@362 112 Resampler::reconstructOne(const double **srcptr)
c@362 113 {
c@362 114 Phase &pd = m_phaseData[m_phase];
c@362 115 double *filt = pd.filter.data();
c@362 116 int n = pd.filter.size();
c@362 117 double v = 0.0;
c@362 118 for (int i = 0; i < n; ++i) {
c@362 119 v += m_buffer[i] * filt[i];
c@362 120 }
c@362 121 for (int i = pd.drop; i < n; ++i) {
c@362 122 m_buffer[i - pd.drop] = m_buffer[i];
c@362 123 }
c@362 124 for (int i = 0; i < pd.take; ++i) {
c@362 125 m_buffer[n - pd.drop + i] = **srcptr;
c@362 126 ++ *srcptr;
c@362 127 }
c@362 128 m_phase = pd.nextPhase;
c@362 129 return v;
c@362 130 }
c@362 131
c@362 132 int
c@362 133 Resampler::process(const double *src, double *dst, int n)
c@362 134 {
c@362 135 int m = 0;
c@362 136 const double *srcptr = src;
c@362 137
c@362 138 while (n > m_phaseData[m_phase].take) {
c@362 139 std::cerr << "n = " << n << ", m = " << m << ", take = " << m_phaseData[m_phase].take << std::endl;
c@362 140 n -= m_phaseData[m_phase].take;
c@362 141 dst[m] = reconstructOne(&srcptr);
c@362 142 std::cerr << "n -> " << n << std::endl;
c@362 143 ++m;
c@362 144 }
c@362 145
c@362 146 //!!! save any excess
c@362 147
c@362 148 return m;
c@362 149 }
c@362 150
c@363 151 std::vector<double>
c@363 152 Resampler::resample(int sourceRate, int targetRate, const double *data, int n)
c@363 153 {
c@363 154 Resampler r(sourceRate, targetRate);
c@363 155
c@363 156 int latency = r.getLatency();
c@363 157
c@363 158 int m = int(ceil((n * targetRate) / sourceRate));
c@363 159 int m1 = m + latency;
c@363 160 int n1 = int((m1 * sourceRate) / targetRate);
c@363 161
c@363 162 vector<double> pad(n1 - n, 0.0);
c@363 163 vector<double> out(m1, 0.0);
c@363 164
c@363 165 int got = r.process(data, out.data(), n);
c@363 166 got += r.process(pad.data(), out.data() + got, pad.size());
c@363 167
c@363 168 return vector<double>(out.begin() + latency, out.begin() + got);
c@363 169 }
c@363 170