Chris@0
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 Sonic Visualiser
|
Chris@0
|
5 An audio file viewer and annotation editor.
|
Chris@0
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@0
|
7 This file copyright 2006 Chris Cannam.
|
Chris@0
|
8
|
Chris@0
|
9 This program is free software; you can redistribute it and/or
|
Chris@0
|
10 modify it under the terms of the GNU General Public License as
|
Chris@0
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@0
|
12 License, or (at your option) any later version. See the file
|
Chris@0
|
13 COPYING included with this distribution for more information.
|
Chris@0
|
14 */
|
Chris@0
|
15
|
Chris@0
|
16 #include "IntegerTimeStretcher.h"
|
Chris@0
|
17
|
Chris@0
|
18 #include <iostream>
|
Chris@0
|
19 #include <cassert>
|
Chris@0
|
20
|
Chris@0
|
21 //#define DEBUG_INTEGER_TIME_STRETCHER 1
|
Chris@0
|
22
|
Chris@12
|
23 IntegerTimeStretcher::IntegerTimeStretcher(float ratio,
|
Chris@0
|
24 size_t maxProcessInputBlockSize,
|
Chris@0
|
25 size_t inputIncrement,
|
Chris@0
|
26 size_t windowSize,
|
Chris@0
|
27 WindowType windowType) :
|
Chris@0
|
28 m_ratio(ratio),
|
Chris@0
|
29 m_n1(inputIncrement),
|
Chris@12
|
30 m_n2(lrintf(m_n1 * ratio)),
|
Chris@0
|
31 m_wlen(std::max(windowSize, m_n2 * 2)),
|
Chris@0
|
32 m_inbuf(m_wlen),
|
Chris@12
|
33 m_outbuf(maxProcessInputBlockSize * ratio + 1024) //!!!
|
Chris@0
|
34 {
|
Chris@0
|
35 m_window = new Window<float>(windowType, m_wlen),
|
Chris@0
|
36
|
Chris@0
|
37 m_time = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * m_wlen);
|
Chris@0
|
38 m_freq = (fftwf_complex *)fftwf_malloc(sizeof(fftwf_complex) * m_wlen);
|
Chris@0
|
39 m_dbuf = (float *)fftwf_malloc(sizeof(float) * m_wlen);
|
Chris@12
|
40 m_mashbuf = (float *)fftwf_malloc(sizeof(float) * m_wlen);
|
Chris@12
|
41 m_prevPhase = (float *)fftwf_malloc(sizeof(float) * m_wlen);
|
Chris@12
|
42 m_prevAdjustedPhase = (float *)fftwf_malloc(sizeof(float) * m_wlen);
|
Chris@0
|
43
|
Chris@0
|
44 m_plan = fftwf_plan_dft_1d(m_wlen, m_time, m_freq, FFTW_FORWARD, FFTW_ESTIMATE);
|
Chris@0
|
45 m_iplan = fftwf_plan_dft_c2r_1d(m_wlen, m_freq, m_dbuf, FFTW_ESTIMATE);
|
Chris@0
|
46
|
Chris@0
|
47 for (int i = 0; i < m_wlen; ++i) {
|
Chris@0
|
48 m_mashbuf[i] = 0.0;
|
Chris@12
|
49 m_prevPhase[i] = 0.0;
|
Chris@12
|
50 m_prevAdjustedPhase[i] = 0.0;
|
Chris@0
|
51 }
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 IntegerTimeStretcher::~IntegerTimeStretcher()
|
Chris@0
|
55 {
|
Chris@0
|
56 std::cerr << "IntegerTimeStretcher::~IntegerTimeStretcher" << std::endl;
|
Chris@0
|
57
|
Chris@0
|
58 fftwf_destroy_plan(m_plan);
|
Chris@0
|
59 fftwf_destroy_plan(m_iplan);
|
Chris@0
|
60
|
Chris@0
|
61 fftwf_free(m_time);
|
Chris@0
|
62 fftwf_free(m_freq);
|
Chris@0
|
63 fftwf_free(m_dbuf);
|
Chris@12
|
64 fftwf_free(m_mashbuf);
|
Chris@12
|
65 fftwf_free(m_prevPhase);
|
Chris@12
|
66 fftwf_free(m_prevAdjustedPhase);
|
Chris@0
|
67
|
Chris@0
|
68 delete m_window;
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 size_t
|
Chris@0
|
72 IntegerTimeStretcher::getProcessingLatency() const
|
Chris@0
|
73 {
|
Chris@0
|
74 return getWindowSize() - getInputIncrement();
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 void
|
Chris@0
|
78 IntegerTimeStretcher::process(float *input, float *output, size_t samples)
|
Chris@0
|
79 {
|
Chris@0
|
80 // We need to add samples from input to our internal buffer. When
|
Chris@0
|
81 // we have m_windowSize samples in the buffer, we can process it,
|
Chris@0
|
82 // move the samples back by m_n1 and write the output onto our
|
Chris@0
|
83 // internal output buffer. If we have (samples * ratio) samples
|
Chris@0
|
84 // in that, we can write m_n2 of them back to output and return
|
Chris@0
|
85 // (otherwise we have to write zeroes).
|
Chris@0
|
86
|
Chris@0
|
87 // When we process, we write m_wlen to our fixed output buffer
|
Chris@0
|
88 // (m_mashbuf). We then pull out the first m_n2 samples from that
|
Chris@0
|
89 // buffer, push them into the output ring buffer, and shift
|
Chris@0
|
90 // m_mashbuf left by that amount.
|
Chris@0
|
91
|
Chris@0
|
92 // The processing latency is then m_wlen - m_n2.
|
Chris@0
|
93
|
Chris@0
|
94 size_t consumed = 0;
|
Chris@0
|
95
|
Chris@0
|
96 #ifdef DEBUG_INTEGER_TIME_STRETCHER
|
Chris@0
|
97 std::cerr << "IntegerTimeStretcher::process(" << samples << ", consumed = " << consumed << "), writable " << m_inbuf.getWriteSpace() <<", readable "<< m_outbuf.getReadSpace() << std::endl;
|
Chris@0
|
98 #endif
|
Chris@0
|
99
|
Chris@0
|
100 while (consumed < samples) {
|
Chris@0
|
101
|
Chris@0
|
102 size_t writable = m_inbuf.getWriteSpace();
|
Chris@0
|
103 writable = std::min(writable, samples - consumed);
|
Chris@0
|
104
|
Chris@0
|
105 if (writable == 0) {
|
Chris@0
|
106 //!!! then what? I don't think this should happen, but
|
Chris@0
|
107 std::cerr << "WARNING: IntegerTimeStretcher::process: writable == 0" << std::endl;
|
Chris@0
|
108 break;
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 #ifdef DEBUG_INTEGER_TIME_STRETCHER
|
Chris@0
|
112 std::cerr << "writing " << writable << " from index " << consumed << " to inbuf, consumed will be " << consumed + writable << std::endl;
|
Chris@0
|
113 #endif
|
Chris@0
|
114 m_inbuf.write(input + consumed, writable);
|
Chris@0
|
115 consumed += writable;
|
Chris@0
|
116
|
Chris@0
|
117 while (m_inbuf.getReadSpace() >= m_wlen &&
|
Chris@0
|
118 m_outbuf.getWriteSpace() >= m_n2) {
|
Chris@0
|
119
|
Chris@0
|
120 // We know we have at least m_wlen samples available
|
Chris@0
|
121 // in m_inbuf. We need to peek m_wlen of them for
|
Chris@0
|
122 // processing, and then read m_n1 to advance the read
|
Chris@0
|
123 // pointer.
|
Chris@0
|
124
|
Chris@0
|
125 size_t got = m_inbuf.peek(m_dbuf, m_wlen);
|
Chris@0
|
126 assert(got == m_wlen);
|
Chris@0
|
127
|
Chris@0
|
128 processBlock(m_dbuf, m_mashbuf);
|
Chris@0
|
129
|
Chris@0
|
130 #ifdef DEBUG_INTEGER_TIME_STRETCHER
|
Chris@0
|
131 std::cerr << "writing first " << m_n2 << " from mashbuf, skipping " << m_n1 << " on inbuf " << std::endl;
|
Chris@0
|
132 #endif
|
Chris@0
|
133 m_inbuf.skip(m_n1);
|
Chris@0
|
134 m_outbuf.write(m_mashbuf, m_n2);
|
Chris@0
|
135
|
Chris@0
|
136 for (size_t i = 0; i < m_wlen - m_n2; ++i) {
|
Chris@0
|
137 m_mashbuf[i] = m_mashbuf[i + m_n2];
|
Chris@0
|
138 }
|
Chris@0
|
139 for (size_t i = m_wlen - m_n2; i < m_wlen; ++i) {
|
Chris@0
|
140 m_mashbuf[i] = 0.0f;
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 // std::cerr << "WARNING: IntegerTimeStretcher::process: writespace not enough for output increment (" << m_outbuf.getWriteSpace() << " < " << m_n2 << ")" << std::endl;
|
Chris@0
|
145 // }
|
Chris@0
|
146
|
Chris@0
|
147 #ifdef DEBUG_INTEGER_TIME_STRETCHER
|
Chris@0
|
148 std::cerr << "loop ended: inbuf read space " << m_inbuf.getReadSpace() << ", outbuf write space " << m_outbuf.getWriteSpace() << std::endl;
|
Chris@0
|
149 #endif
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@12
|
152 size_t toRead = lrintf(samples * m_ratio);
|
Chris@12
|
153
|
Chris@12
|
154 if (m_outbuf.getReadSpace() < toRead) {
|
Chris@12
|
155 std::cerr << "WARNING: IntegerTimeStretcher::process: not enough data (yet?) (" << m_outbuf.getReadSpace() << " < " << toRead << ")" << std::endl;
|
Chris@12
|
156 size_t fill = toRead - m_outbuf.getReadSpace();
|
Chris@0
|
157 for (size_t i = 0; i < fill; ++i) {
|
Chris@0
|
158 output[i] = 0.0;
|
Chris@0
|
159 }
|
Chris@0
|
160 m_outbuf.read(output + fill, m_outbuf.getReadSpace());
|
Chris@0
|
161 } else {
|
Chris@0
|
162 #ifdef DEBUG_INTEGER_TIME_STRETCHER
|
Chris@12
|
163 std::cerr << "enough data - writing " << toRead << " from outbuf" << std::endl;
|
Chris@0
|
164 #endif
|
Chris@12
|
165 m_outbuf.read(output, toRead);
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 #ifdef DEBUG_INTEGER_TIME_STRETCHER
|
Chris@0
|
169 std::cerr << "IntegerTimeStretcher::process returning" << std::endl;
|
Chris@0
|
170 #endif
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 void
|
Chris@0
|
174 IntegerTimeStretcher::processBlock(float *buf, float *out)
|
Chris@0
|
175 {
|
Chris@0
|
176 size_t i;
|
Chris@0
|
177
|
Chris@0
|
178 // buf contains m_wlen samples; out contains enough space for
|
Chris@0
|
179 // m_wlen * ratio samples (we mix into out, rather than replacing)
|
Chris@0
|
180
|
Chris@0
|
181 #ifdef DEBUG_INTEGER_TIME_STRETCHER
|
Chris@0
|
182 std::cerr << "IntegerTimeStretcher::processBlock" << std::endl;
|
Chris@0
|
183 #endif
|
Chris@0
|
184
|
Chris@0
|
185 m_window->cut(buf);
|
Chris@0
|
186
|
Chris@0
|
187 for (i = 0; i < m_wlen/2; ++i) {
|
Chris@0
|
188 float temp = buf[i];
|
Chris@0
|
189 buf[i] = buf[i + m_wlen/2];
|
Chris@0
|
190 buf[i + m_wlen/2] = temp;
|
Chris@0
|
191 }
|
Chris@0
|
192
|
Chris@0
|
193 for (i = 0; i < m_wlen; ++i) {
|
Chris@0
|
194 m_time[i][0] = buf[i];
|
Chris@0
|
195 m_time[i][1] = 0.0;
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 fftwf_execute(m_plan); // m_time -> m_freq
|
Chris@0
|
199
|
Chris@0
|
200 for (i = 0; i < m_wlen; ++i) {
|
Chris@0
|
201
|
Chris@0
|
202 float mag = sqrtf(m_freq[i][0] * m_freq[i][0] +
|
Chris@0
|
203 m_freq[i][1] * m_freq[i][1]);
|
Chris@0
|
204
|
Chris@12
|
205 float phase = princargf(atan2f(m_freq[i][1], m_freq[i][0]));
|
Chris@12
|
206
|
Chris@12
|
207 float omega = (2 * M_PI * m_n1 * i) / m_wlen;
|
Chris@0
|
208
|
Chris@12
|
209 float expectedPhase = m_prevPhase[i] + omega;
|
Chris@12
|
210
|
Chris@12
|
211 float phaseError = princargf(phase - expectedPhase);
|
Chris@12
|
212
|
Chris@12
|
213 float phaseIncrement = (omega + phaseError) / m_n1;
|
Chris@12
|
214
|
Chris@12
|
215 float adjustedPhase = m_prevAdjustedPhase[i] + m_n2 * phaseIncrement;
|
Chris@0
|
216
|
Chris@12
|
217 float real = mag * cosf(adjustedPhase);
|
Chris@12
|
218 float imag = mag * sinf(adjustedPhase);
|
Chris@0
|
219 m_freq[i][0] = real;
|
Chris@0
|
220 m_freq[i][1] = imag;
|
Chris@12
|
221
|
Chris@12
|
222 m_prevPhase[i] = phase;
|
Chris@12
|
223 m_prevAdjustedPhase[i] = adjustedPhase;
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 fftwf_execute(m_iplan); // m_freq -> in, inverse fft
|
Chris@0
|
227
|
Chris@0
|
228 for (i = 0; i < m_wlen/2; ++i) {
|
Chris@0
|
229 float temp = buf[i] / m_wlen;
|
Chris@0
|
230 buf[i] = buf[i + m_wlen/2] / m_wlen;
|
Chris@0
|
231 buf[i + m_wlen/2] = temp;
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 m_window->cut(buf);
|
Chris@0
|
235
|
Chris@0
|
236 int div = m_wlen / m_n2;
|
Chris@0
|
237 if (div > 1) div /= 2;
|
Chris@0
|
238 for (i = 0; i < m_wlen; ++i) {
|
Chris@0
|
239 buf[i] /= div;
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 for (i = 0; i < m_wlen; ++i) {
|
Chris@0
|
243 out[i] += buf[i];
|
Chris@0
|
244 }
|
Chris@0
|
245 }
|