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