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