c@9
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@9
|
2
|
c@9
|
3 /*
|
c@9
|
4 QM Vamp Plugin Set
|
c@9
|
5
|
c@9
|
6 Centre for Digital Music, Queen Mary, University of London.
|
c@9
|
7 All rights reserved.
|
c@9
|
8 */
|
c@9
|
9
|
c@9
|
10 #include "ConstantQSpectrogram.h"
|
c@9
|
11
|
c@9
|
12 #include <base/Pitch.h>
|
c@9
|
13 #include <dsp/chromagram/ConstantQ.h>
|
c@9
|
14
|
c@9
|
15 using std::string;
|
c@9
|
16 using std::vector;
|
c@9
|
17 using std::cerr;
|
c@9
|
18 using std::endl;
|
c@9
|
19
|
c@9
|
20 ConstantQSpectrogram::ConstantQSpectrogram(float inputSampleRate) :
|
c@9
|
21 Vamp::Plugin(inputSampleRate),
|
c@17
|
22 m_bins(1),
|
c@9
|
23 m_cq(0),
|
c@9
|
24 m_step(0),
|
c@17
|
25 m_block(0)
|
c@9
|
26 {
|
c@9
|
27 m_minMIDIPitch = 12;
|
c@9
|
28 m_maxMIDIPitch = 96;
|
c@9
|
29 m_tuningFrequency = 440;
|
c@9
|
30 m_normalized = true;
|
c@9
|
31 m_bpo = 12;
|
c@9
|
32
|
c@9
|
33 setupConfig();
|
c@9
|
34 }
|
c@9
|
35
|
c@9
|
36 void
|
c@9
|
37 ConstantQSpectrogram::setupConfig()
|
c@9
|
38 {
|
c@9
|
39 m_config.FS = lrintf(m_inputSampleRate);
|
c@9
|
40 m_config.min = Pitch::getFrequencyForPitch
|
c@9
|
41 (m_minMIDIPitch, 0, m_tuningFrequency);
|
c@9
|
42 m_config.max = Pitch::getFrequencyForPitch
|
c@9
|
43 (m_maxMIDIPitch, 0, m_tuningFrequency);
|
c@9
|
44 m_config.BPO = m_bpo;
|
c@9
|
45 m_config.CQThresh = 0.0054;
|
c@9
|
46
|
c@9
|
47 m_step = 0;
|
c@9
|
48 m_block = 0;
|
c@9
|
49 }
|
c@9
|
50
|
c@9
|
51 ConstantQSpectrogram::~ConstantQSpectrogram()
|
c@9
|
52 {
|
c@9
|
53 delete m_cq;
|
c@9
|
54 }
|
c@9
|
55
|
c@9
|
56 string
|
c@22
|
57 ConstantQSpectrogram::getIdentifier() const
|
c@9
|
58 {
|
c@9
|
59 return "qm-constantq";
|
c@9
|
60 }
|
c@9
|
61
|
c@9
|
62 string
|
c@22
|
63 ConstantQSpectrogram::getName() const
|
c@22
|
64 {
|
c@22
|
65 return "Constant-Q Spectrogram";
|
c@22
|
66 }
|
c@22
|
67
|
c@22
|
68 string
|
c@9
|
69 ConstantQSpectrogram::getDescription() const
|
c@9
|
70 {
|
c@51
|
71 return "Extract a spectrogram with constant ratio of centre frequency to resolution from the input audio";
|
c@9
|
72 }
|
c@9
|
73
|
c@9
|
74 string
|
c@9
|
75 ConstantQSpectrogram::getMaker() const
|
c@9
|
76 {
|
c@9
|
77 return "Queen Mary, University of London";
|
c@9
|
78 }
|
c@9
|
79
|
c@9
|
80 int
|
c@9
|
81 ConstantQSpectrogram::getPluginVersion() const
|
c@9
|
82 {
|
c@50
|
83 return 2;
|
c@9
|
84 }
|
c@9
|
85
|
c@9
|
86 string
|
c@9
|
87 ConstantQSpectrogram::getCopyright() const
|
c@9
|
88 {
|
c@50
|
89 return "Plugin by Chris Cannam and Christian Landone. Copyright (c) 2006-2008 QMUL - All Rights Reserved";
|
c@9
|
90 }
|
c@9
|
91
|
c@9
|
92 ConstantQSpectrogram::ParameterList
|
c@9
|
93 ConstantQSpectrogram::getParameterDescriptors() const
|
c@9
|
94 {
|
c@9
|
95 ParameterList list;
|
c@9
|
96
|
c@9
|
97 ParameterDescriptor desc;
|
c@22
|
98 desc.identifier = "minpitch";
|
c@22
|
99 desc.name = "Minimum Pitch";
|
c@9
|
100 desc.unit = "MIDI units";
|
c@52
|
101 desc.description = "MIDI pitch corresponding to the lowest frequency to be included in the constant-Q transform";
|
c@9
|
102 desc.minValue = 0;
|
c@9
|
103 desc.maxValue = 127;
|
c@9
|
104 desc.defaultValue = 36;
|
c@9
|
105 desc.isQuantized = true;
|
c@9
|
106 desc.quantizeStep = 1;
|
c@9
|
107 list.push_back(desc);
|
c@9
|
108
|
c@22
|
109 desc.identifier = "maxpitch";
|
c@22
|
110 desc.name = "Maximum Pitch";
|
c@9
|
111 desc.unit = "MIDI units";
|
c@52
|
112 desc.description = "MIDI pitch corresponding to the highest frequency to be included in the constant-Q transform";
|
c@9
|
113 desc.minValue = 0;
|
c@9
|
114 desc.maxValue = 127;
|
c@9
|
115 desc.defaultValue = 84;
|
c@9
|
116 desc.isQuantized = true;
|
c@9
|
117 desc.quantizeStep = 1;
|
c@9
|
118 list.push_back(desc);
|
c@9
|
119
|
c@22
|
120 desc.identifier = "tuning";
|
c@22
|
121 desc.name = "Tuning Frequency";
|
c@9
|
122 desc.unit = "Hz";
|
c@52
|
123 desc.description = "Frequency of concert A";
|
c@9
|
124 desc.minValue = 420;
|
c@9
|
125 desc.maxValue = 460;
|
c@9
|
126 desc.defaultValue = 440;
|
c@9
|
127 desc.isQuantized = false;
|
c@9
|
128 list.push_back(desc);
|
c@9
|
129
|
c@22
|
130 desc.identifier = "bpo";
|
c@22
|
131 desc.name = "Bins per Octave";
|
c@9
|
132 desc.unit = "bins";
|
c@52
|
133 desc.description = "Number of constant-Q transform bins per octave";
|
c@9
|
134 desc.minValue = 2;
|
c@9
|
135 desc.maxValue = 36;
|
c@9
|
136 desc.defaultValue = 12;
|
c@9
|
137 desc.isQuantized = true;
|
c@9
|
138 desc.quantizeStep = 1;
|
c@9
|
139 list.push_back(desc);
|
c@9
|
140
|
c@22
|
141 desc.identifier = "normalized";
|
c@22
|
142 desc.name = "Normalized";
|
c@9
|
143 desc.unit = "";
|
c@52
|
144 desc.description = "Whether to normalize each output column to unit maximum";
|
c@9
|
145 desc.minValue = 0;
|
c@9
|
146 desc.maxValue = 1;
|
c@51
|
147 desc.defaultValue = 0;
|
c@9
|
148 desc.isQuantized = true;
|
c@9
|
149 desc.quantizeStep = 1;
|
c@9
|
150 list.push_back(desc);
|
c@9
|
151
|
c@9
|
152 return list;
|
c@9
|
153 }
|
c@9
|
154
|
c@9
|
155 float
|
c@9
|
156 ConstantQSpectrogram::getParameter(std::string param) const
|
c@9
|
157 {
|
c@9
|
158 if (param == "minpitch") {
|
c@9
|
159 return m_minMIDIPitch;
|
c@9
|
160 }
|
c@9
|
161 if (param == "maxpitch") {
|
c@9
|
162 return m_maxMIDIPitch;
|
c@9
|
163 }
|
c@9
|
164 if (param == "tuning") {
|
c@9
|
165 return m_tuningFrequency;
|
c@9
|
166 }
|
c@9
|
167 if (param == "bpo") {
|
c@9
|
168 return m_bpo;
|
c@9
|
169 }
|
c@9
|
170 if (param == "normalized") {
|
c@9
|
171 return m_normalized;
|
c@9
|
172 }
|
c@9
|
173 std::cerr << "WARNING: ConstantQSpectrogram::getParameter: unknown parameter \""
|
c@9
|
174 << param << "\"" << std::endl;
|
c@9
|
175 return 0.0;
|
c@9
|
176 }
|
c@9
|
177
|
c@9
|
178 void
|
c@9
|
179 ConstantQSpectrogram::setParameter(std::string param, float value)
|
c@9
|
180 {
|
c@9
|
181 if (param == "minpitch") {
|
c@9
|
182 m_minMIDIPitch = lrintf(value);
|
c@9
|
183 } else if (param == "maxpitch") {
|
c@9
|
184 m_maxMIDIPitch = lrintf(value);
|
c@9
|
185 } else if (param == "tuning") {
|
c@9
|
186 m_tuningFrequency = value;
|
c@9
|
187 } else if (param == "bpo") {
|
c@9
|
188 m_bpo = lrintf(value);
|
c@9
|
189 } else if (param == "normalized") {
|
c@9
|
190 m_normalized = (value > 0.0001);
|
c@9
|
191 } else {
|
c@9
|
192 std::cerr << "WARNING: ConstantQSpectrogram::setParameter: unknown parameter \""
|
c@9
|
193 << param << "\"" << std::endl;
|
c@9
|
194 }
|
c@9
|
195
|
c@9
|
196 setupConfig();
|
c@9
|
197 }
|
c@9
|
198
|
c@9
|
199
|
c@9
|
200 bool
|
c@9
|
201 ConstantQSpectrogram::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
c@9
|
202 {
|
c@9
|
203 if (m_cq) {
|
c@9
|
204 delete m_cq;
|
c@9
|
205 m_cq = 0;
|
c@9
|
206 }
|
c@9
|
207
|
c@9
|
208 if (channels < getMinChannelCount() ||
|
c@9
|
209 channels > getMaxChannelCount()) return false;
|
c@9
|
210
|
c@24
|
211 setupConfig();
|
c@24
|
212
|
c@9
|
213 m_cq = new ConstantQ(m_config);
|
c@50
|
214 m_bins = m_cq->getK();
|
c@9
|
215 m_cq->sparsekernel();
|
c@13
|
216 m_step = m_cq->gethop();
|
c@13
|
217 m_block = m_cq->getfftlength();
|
c@13
|
218
|
c@52
|
219 if (blockSize != m_block) {
|
c@52
|
220 std::cerr << "ConstantQSpectrogram::initialise: ERROR: supplied block size " << blockSize << " differs from required block size " << m_block << ", initialise failing" << std::endl;
|
c@13
|
221 delete m_cq;
|
c@13
|
222 m_cq = 0;
|
c@13
|
223 return false;
|
c@13
|
224 }
|
c@9
|
225
|
c@52
|
226 if (stepSize != m_step) {
|
c@52
|
227 std::cerr << "ConstantQSpectrogram::initialise: NOTE: supplied step size " << stepSize << " differs from expected step size " << m_step << " (for block size = " << m_block << ")" << std::endl;
|
c@52
|
228 }
|
c@52
|
229
|
c@9
|
230 return true;
|
c@9
|
231 }
|
c@9
|
232
|
c@9
|
233 void
|
c@9
|
234 ConstantQSpectrogram::reset()
|
c@9
|
235 {
|
c@9
|
236 if (m_cq) {
|
c@9
|
237 delete m_cq;
|
c@9
|
238 m_cq = new ConstantQ(m_config);
|
c@9
|
239 }
|
c@9
|
240 }
|
c@9
|
241
|
c@9
|
242 size_t
|
c@9
|
243 ConstantQSpectrogram::getPreferredStepSize() const
|
c@9
|
244 {
|
c@9
|
245 if (!m_step) {
|
c@9
|
246 ConstantQ cq(m_config);
|
c@9
|
247 m_step = cq.gethop();
|
c@9
|
248 m_block = cq.getfftlength();
|
c@9
|
249 }
|
c@9
|
250
|
c@9
|
251 return m_step;
|
c@9
|
252 }
|
c@9
|
253
|
c@9
|
254 size_t
|
c@9
|
255 ConstantQSpectrogram::getPreferredBlockSize() const
|
c@9
|
256 {
|
c@9
|
257 if (!m_block) {
|
c@9
|
258 ConstantQ cq(m_config);
|
c@9
|
259 m_step = cq.gethop();
|
c@9
|
260 m_block = cq.getfftlength();
|
c@9
|
261 }
|
c@9
|
262
|
c@9
|
263 return m_block;
|
c@9
|
264 }
|
c@9
|
265
|
c@9
|
266 ConstantQSpectrogram::OutputList
|
c@9
|
267 ConstantQSpectrogram::getOutputDescriptors() const
|
c@9
|
268 {
|
c@9
|
269 OutputList list;
|
c@9
|
270
|
c@9
|
271 OutputDescriptor d;
|
c@22
|
272 d.identifier = "constantq";
|
c@22
|
273 d.name = "Constant-Q Spectrogram";
|
c@9
|
274 d.unit = "";
|
c@52
|
275 d.description = "Output of constant-Q transform, as a single vector per process block";
|
c@9
|
276 d.hasFixedBinCount = true;
|
c@9
|
277 d.binCount = m_bins;
|
c@9
|
278
|
c@30
|
279 // std::cerr << "Bin count " << d.binCount << std::endl;
|
c@9
|
280
|
c@9
|
281 const char *names[] =
|
c@9
|
282 { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
|
c@9
|
283
|
c@9
|
284 if (m_bpo == 12) {
|
c@17
|
285 for (int i = 0; i < int(d.binCount); ++i) {
|
c@9
|
286 int ipc = m_minMIDIPitch % 12;
|
c@9
|
287 int index = (i + ipc) % 12;
|
c@9
|
288 d.binNames.push_back(names[index]);
|
c@9
|
289 }
|
c@9
|
290 } else {
|
c@9
|
291 d.binNames.push_back(names[m_minMIDIPitch % 12]);
|
c@9
|
292 }
|
c@9
|
293
|
c@9
|
294 d.hasKnownExtents = m_normalized;
|
c@9
|
295 d.minValue = 0.0;
|
c@9
|
296 d.maxValue = (m_normalized ? 1.0 : 0.0);
|
c@9
|
297 d.isQuantized = false;
|
c@9
|
298 d.sampleType = OutputDescriptor::OneSamplePerStep;
|
c@9
|
299 list.push_back(d);
|
c@9
|
300
|
c@9
|
301 return list;
|
c@9
|
302 }
|
c@9
|
303
|
c@9
|
304 ConstantQSpectrogram::Feature
|
c@9
|
305 ConstantQSpectrogram::normalize(const Feature &feature)
|
c@9
|
306 {
|
c@9
|
307 float min = 0.0, max = 0.0;
|
c@9
|
308
|
c@9
|
309 for (size_t i = 0; i < feature.values.size(); ++i) {
|
c@9
|
310 if (i == 0 || feature.values[i] < min) min = feature.values[i];
|
c@9
|
311 if (i == 0 || feature.values[i] > max) max = feature.values[i];
|
c@9
|
312 }
|
c@9
|
313
|
c@9
|
314 if (max == 0.0 || max == min) return feature;
|
c@9
|
315
|
c@9
|
316 Feature normalized;
|
c@9
|
317 normalized.hasTimestamp = false;
|
c@9
|
318
|
c@9
|
319 for (size_t i = 0; i < feature.values.size(); ++i) {
|
c@9
|
320 normalized.values.push_back((feature.values[i] - min) / (max - min));
|
c@9
|
321 }
|
c@9
|
322
|
c@9
|
323 return normalized;
|
c@9
|
324 }
|
c@9
|
325
|
c@9
|
326 ConstantQSpectrogram::FeatureSet
|
c@18
|
327 ConstantQSpectrogram::process(const float *const *inputBuffers,
|
c@18
|
328 Vamp::RealTime /* timestamp */)
|
c@9
|
329 {
|
c@9
|
330 if (!m_cq) {
|
c@9
|
331 cerr << "ERROR: ConstantQSpectrogram::process: "
|
c@9
|
332 << "Constant-Q has not been initialised"
|
c@9
|
333 << endl;
|
c@9
|
334 return FeatureSet();
|
c@9
|
335 }
|
c@9
|
336
|
c@9
|
337 double *real = new double[m_block];
|
c@9
|
338 double *imag = new double[m_block];
|
c@9
|
339 double *cqre = new double[m_bins];
|
c@9
|
340 double *cqim = new double[m_bins];
|
c@9
|
341
|
c@9
|
342 for (size_t i = 0; i < m_block/2; ++i) {
|
c@9
|
343 real[i] = inputBuffers[0][i*2];
|
c@17
|
344 if (i > 0) real[m_block - i] = real[i];
|
c@9
|
345 imag[i] = inputBuffers[0][i*2+1];
|
c@17
|
346 if (i > 0) imag[m_block - i] = imag[i];
|
c@9
|
347 }
|
c@9
|
348
|
c@9
|
349 m_cq->process(real, imag, cqre, cqim);
|
c@9
|
350
|
c@9
|
351 delete[] real;
|
c@9
|
352 delete[] imag;
|
c@9
|
353
|
c@9
|
354 Feature feature;
|
c@9
|
355 feature.hasTimestamp = false;
|
c@17
|
356 for (int i = 0; i < m_bins; ++i) {
|
c@16
|
357 double re = cqre[i];
|
c@16
|
358 double im = cqim[i];
|
c@16
|
359 if (isnan(re)) re = 0.0;
|
c@16
|
360 if (isnan(im)) im = 0.0;
|
c@16
|
361 double value = sqrt(re * re + im * im);
|
c@16
|
362 feature.values.push_back(value);
|
c@9
|
363 }
|
c@9
|
364 feature.label = "";
|
c@9
|
365
|
c@9
|
366 delete[] cqre;
|
c@9
|
367 delete[] cqim;
|
c@9
|
368
|
c@9
|
369 FeatureSet returnFeatures;
|
c@9
|
370 if (m_normalized) returnFeatures[0].push_back(normalize(feature));
|
c@9
|
371 else returnFeatures[0].push_back(feature);
|
c@9
|
372 return returnFeatures;
|
c@9
|
373 }
|
c@9
|
374
|
c@9
|
375 ConstantQSpectrogram::FeatureSet
|
c@9
|
376 ConstantQSpectrogram::getRemainingFeatures()
|
c@9
|
377 {
|
c@9
|
378 return FeatureSet();
|
c@9
|
379 }
|
c@9
|
380
|