annotate dsp/rateconversion/Resampler.cpp @ 364:01d7da967123

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