annotate dsp/rateconversion/Resampler.cpp @ 362:3953f3ef1b62

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