comparison vamp-sdk/hostext/PluginInputDomainAdapter.cpp @ 58:0284955e31e5 host-factory-stuff

* reshuffle
author cannam
date Thu, 24 May 2007 10:05:00 +0000
parents vamp-hostsdk/PluginInputDomainAdapter.cpp@09a1aac6c362
children fa79c4ec847d
comparison
equal deleted inserted replaced
57:09a1aac6c362 58:0284955e31e5
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Vamp
5
6 An API for audio analysis and feature extraction plugins.
7
8 Centre for Digital Music, Queen Mary, University of London.
9 Copyright 2006 Chris Cannam.
10
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation
13 files (the "Software"), to deal in the Software without
14 restriction, including without limitation the rights to use, copy,
15 modify, merge, publish, distribute, sublicense, and/or sell copies
16 of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 Except as contained in this notice, the names of the Centre for
31 Digital Music; Queen Mary, University of London; and Chris Cannam
32 shall not be used in advertising or otherwise to promote the sale,
33 use or other dealings in this Software without prior written
34 authorization.
35 */
36
37 #include "PluginInputDomainAdapter.h"
38
39 #include <cmath>
40
41 namespace Vamp {
42
43 PluginInputDomainAdapter::PluginInputDomainAdapter(Plugin *plugin) :
44 PluginWrapper(plugin),
45 m_channels(0),
46 m_blockSize(0),
47 m_freqbuf(0)
48 {
49 }
50
51 PluginInputDomainAdapter::~PluginInputDomainAdapter()
52 {
53 }
54
55 bool
56 PluginInputDomainAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
57 {
58 //!!! complain and die if blocksize is not a power of 2
59
60 if (m_plugin->getInputDomain() == FrequencyDomain) {
61 if (m_channels > 0) {
62 for (size_t c = 0; c < m_channels; ++c) {
63 delete[] m_freqbuf[c];
64 }
65 delete[] m_freqbuf;
66 delete[] m_ri;
67 delete[] m_ro;
68 delete[] m_io;
69 }
70 }
71
72 m_channels = channels;
73 m_blockSize = blockSize;
74
75 if (m_plugin->getInputDomain() == FrequencyDomain) {
76 m_freqbuf = new float *[m_channels];
77 for (size_t c = 0; c < m_channels; ++c) {
78 m_freqbuf[c] = new float[m_blockSize + 2];
79 }
80 m_ri = new double[m_blockSize];
81 m_ro = new double[m_blockSize];
82 m_io = new double[m_blockSize];
83 }
84
85 return m_plugin->initialise(channels, stepSize, blockSize);
86 }
87
88 Plugin::InputDomain
89 PluginInputDomainAdapter::getInputDomain() const
90 {
91 return TimeDomain;
92 }
93
94 size_t
95 PluginInputDomainAdapter::getPreferredStepSize() const
96 {
97 size_t step = m_plugin->getPreferredStepSize();
98
99 if (step == 0 && (m_plugin->getInputDomain() == FrequencyDomain)) {
100 step = getPreferredBlockSize() / 2;
101 }
102
103 return step;
104 }
105
106 size_t
107 PluginInputDomainAdapter::getPreferredBlockSize() const
108 {
109 size_t block = m_plugin->getPreferredBlockSize();
110
111 if (block == 0 && (m_plugin->getInputDomain() == FrequencyDomain)) {
112 block = 1024;
113 }
114
115 return block;
116 }
117
118 Plugin::FeatureSet
119 PluginInputDomainAdapter::process(const float *const *inputBuffers, RealTime timestamp)
120 {
121 if (m_plugin->getInputDomain() == TimeDomain) {
122 return m_plugin->process(inputBuffers, timestamp);
123 }
124
125 for (size_t c = 0; c < m_channels; ++c) {
126
127 for (size_t i = 0; i < m_blockSize; ++i) {
128 // Hanning window
129 m_ri[i] = double(inputBuffers[c][i])
130 * (0.50 - 0.50 * cos((2 * M_PI * i)
131 / m_blockSize));
132 }
133
134 for (size_t i = 0; i < m_blockSize/2; ++i) {
135 // FFT shift
136 double value = m_ri[i];
137 m_ri[i] = m_ri[i + m_blockSize/2];
138 m_ri[i + m_blockSize/2] = value;
139 }
140
141 fft(m_blockSize, false, m_ri, 0, m_ro, m_io);
142
143 for (size_t i = 0; i < m_blockSize/2; ++i) {
144 m_freqbuf[c][i * 2] = m_ro[i];
145 m_freqbuf[c][i * 2 + 1] = m_io[i];
146 }
147 }
148
149 //!!! do we want to adjust the timestamp or anything so as to
150 // effectively centre the frame?
151
152 return m_plugin->process(m_freqbuf, timestamp);
153 }
154
155 void
156 PluginInputDomainAdapter::fft(unsigned int n, bool inverse,
157 double *ri, double *ii, double *ro, double *io)
158 {
159 if (!ri || !ro || !io) return;
160
161 unsigned int bits;
162 unsigned int i, j, k, m;
163 unsigned int blockSize, blockEnd;
164
165 double tr, ti;
166
167 if (n < 2) return;
168 if (n & (n-1)) return;
169
170 double angle = 2.0 * M_PI;
171 if (inverse) angle = -angle;
172
173 for (i = 0; ; ++i) {
174 if (n & (1 << i)) {
175 bits = i;
176 break;
177 }
178 }
179
180 static unsigned int tableSize = 0;
181 static int *table = 0;
182
183 if (tableSize != n) {
184
185 delete[] table;
186
187 table = new int[n];
188
189 for (i = 0; i < n; ++i) {
190
191 m = i;
192
193 for (j = k = 0; j < bits; ++j) {
194 k = (k << 1) | (m & 1);
195 m >>= 1;
196 }
197
198 table[i] = k;
199 }
200
201 tableSize = n;
202 }
203
204 if (ii) {
205 for (i = 0; i < n; ++i) {
206 ro[table[i]] = ri[i];
207 io[table[i]] = ii[i];
208 }
209 } else {
210 for (i = 0; i < n; ++i) {
211 ro[table[i]] = ri[i];
212 io[table[i]] = 0.0;
213 }
214 }
215
216 blockEnd = 1;
217
218 for (blockSize = 2; blockSize <= n; blockSize <<= 1) {
219
220 double delta = angle / (double)blockSize;
221 double sm2 = -sin(-2 * delta);
222 double sm1 = -sin(-delta);
223 double cm2 = cos(-2 * delta);
224 double cm1 = cos(-delta);
225 double w = 2 * cm1;
226 double ar[3], ai[3];
227
228 for (i = 0; i < n; i += blockSize) {
229
230 ar[2] = cm2;
231 ar[1] = cm1;
232
233 ai[2] = sm2;
234 ai[1] = sm1;
235
236 for (j = i, m = 0; m < blockEnd; j++, m++) {
237
238 ar[0] = w * ar[1] - ar[2];
239 ar[2] = ar[1];
240 ar[1] = ar[0];
241
242 ai[0] = w * ai[1] - ai[2];
243 ai[2] = ai[1];
244 ai[1] = ai[0];
245
246 k = j + blockEnd;
247 tr = ar[0] * ro[k] - ai[0] * io[k];
248 ti = ar[0] * io[k] + ai[0] * ro[k];
249
250 ro[k] = ro[j] - tr;
251 io[k] = io[j] - ti;
252
253 ro[j] += tr;
254 io[j] += ti;
255 }
256 }
257
258 blockEnd = blockSize;
259 }
260
261 if (inverse) {
262
263 double denom = (double)n;
264
265 for (i = 0; i < n; i++) {
266 ro[i] /= denom;
267 io[i] /= denom;
268 }
269 }
270 }
271
272
273 }
274