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