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@135
|
7
|
c@135
|
8 This program is free software; you can redistribute it and/or
|
c@135
|
9 modify it under the terms of the GNU General Public License as
|
c@135
|
10 published by the Free Software Foundation; either version 2 of the
|
c@135
|
11 License, or (at your option) any later version. See the file
|
c@135
|
12 COPYING included with this distribution for more information.
|
c@0
|
13 */
|
c@0
|
14
|
c@0
|
15 #include "TonalChangeDetect.h"
|
c@0
|
16
|
c@3
|
17 #include <base/Pitch.h>
|
c@3
|
18 #include <dsp/chromagram/Chromagram.h>
|
c@3
|
19 #include <dsp/tonal/ChangeDetectionFunction.h>
|
c@0
|
20
|
cannam@234
|
21 using std::cerr;
|
cannam@234
|
22 using std::endl;
|
cannam@234
|
23
|
c@0
|
24 TonalChangeDetect::TonalChangeDetect(float fInputSampleRate)
|
c@0
|
25 : Vamp::Plugin(fInputSampleRate),
|
c@0
|
26 m_chromagram(0),
|
c@0
|
27 m_step(0),
|
c@0
|
28 m_block(0),
|
c@85
|
29 m_stepDelay(0),
|
c@85
|
30 m_origin(Vamp::RealTime::zeroTime),
|
c@85
|
31 m_haveOrigin(false)
|
c@0
|
32 {
|
c@0
|
33 m_minMIDIPitch = 32;
|
c@0
|
34 m_maxMIDIPitch = 108;
|
c@0
|
35 m_tuningFrequency = 440;
|
c@0
|
36 m_iSmoothingWidth = 5;
|
c@0
|
37
|
c@0
|
38 setupConfig();
|
c@0
|
39 }
|
c@0
|
40
|
c@0
|
41 TonalChangeDetect::~TonalChangeDetect()
|
c@0
|
42 {
|
c@0
|
43 }
|
c@0
|
44
|
c@0
|
45 bool TonalChangeDetect::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
c@0
|
46 {
|
c@0
|
47 if (m_chromagram) {
|
c@0
|
48 delete m_chromagram;
|
c@0
|
49 m_chromagram = 0;
|
c@0
|
50 }
|
c@0
|
51
|
c@0
|
52 if (channels < getMinChannelCount() ||
|
c@0
|
53 channels > getMaxChannelCount()) {
|
c@0
|
54 std::cerr << "TonalChangeDetect::initialise: Given channel count " << channels << " outside acceptable range (" << getMinChannelCount() << " to " << getMaxChannelCount() << ")" << std::endl;
|
c@0
|
55 return false;
|
c@0
|
56 }
|
c@85
|
57
|
c@15
|
58 m_chromagram = new Chromagram(m_config);
|
c@15
|
59 m_step = m_chromagram->getHopSize();
|
c@15
|
60 m_block = m_chromagram->getFrameSize();
|
c@15
|
61
|
c@0
|
62 if (stepSize != m_step) {
|
c@0
|
63 std::cerr << "TonalChangeDetect::initialise: Given step size " << stepSize << " differs from only acceptable value " << m_step << std::endl;
|
c@15
|
64 delete m_chromagram;
|
c@15
|
65 m_chromagram = 0;
|
c@0
|
66 return false;
|
c@0
|
67 }
|
c@0
|
68 if (blockSize != m_block) {
|
c@0
|
69 std::cerr << "TonalChangeDetect::initialise: Given step size " << stepSize << " differs from only acceptable value " << m_step << std::endl;
|
c@15
|
70 delete m_chromagram;
|
c@15
|
71 m_chromagram = 0;
|
c@0
|
72 return false;
|
c@0
|
73 }
|
c@0
|
74
|
c@0
|
75 // m_stepDelay = (blockSize - stepSize) / 2;
|
c@0
|
76 // m_stepDelay = m_stepDelay / stepSize;
|
c@0
|
77 m_stepDelay = (blockSize - stepSize) / stepSize; //!!! why? seems about right to look at, but...
|
c@0
|
78
|
c@119
|
79 // std::cerr << "TonalChangeDetect::initialise: step " << stepSize << ", block "
|
c@119
|
80 // << blockSize << ", delay " << m_stepDelay << std::endl;
|
c@0
|
81
|
c@0
|
82 m_vaCurrentVector.resize(12, 0.0);
|
c@0
|
83
|
c@0
|
84 return true;
|
c@0
|
85
|
c@0
|
86 }
|
c@0
|
87
|
c@22
|
88 std::string TonalChangeDetect::getIdentifier() const
|
c@0
|
89 {
|
c@9
|
90 return "qm-tonalchange";
|
c@0
|
91 }
|
c@0
|
92
|
c@22
|
93 std::string TonalChangeDetect::getName() const
|
c@22
|
94 {
|
c@22
|
95 return "Tonal Change";
|
c@22
|
96 }
|
c@22
|
97
|
c@0
|
98 std::string TonalChangeDetect::getDescription() const
|
c@0
|
99 {
|
c@52
|
100 return "Detect and return the positions of harmonic changes such as chord boundaries";
|
c@0
|
101 }
|
c@0
|
102
|
c@0
|
103 std::string TonalChangeDetect::getMaker() const
|
c@0
|
104 {
|
c@50
|
105 return "Queen Mary, University of London";
|
c@0
|
106 }
|
c@0
|
107
|
c@0
|
108 int TonalChangeDetect::getPluginVersion() const
|
c@0
|
109 {
|
cannam@233
|
110 return 4;
|
c@0
|
111 }
|
c@0
|
112
|
c@0
|
113 std::string TonalChangeDetect::getCopyright() const
|
c@0
|
114 {
|
c@118
|
115 return "Plugin by Martin Gasser and Christopher Harte. Copyright (c) 2006-2009 QMUL - All Rights Reserved";
|
c@0
|
116 }
|
c@0
|
117
|
c@0
|
118 TonalChangeDetect::ParameterList TonalChangeDetect::getParameterDescriptors() const
|
c@0
|
119 {
|
c@0
|
120 ParameterList list;
|
c@0
|
121
|
c@0
|
122 ParameterDescriptor desc;
|
c@23
|
123 desc.identifier = "smoothingwidth";
|
c@23
|
124 desc.name = "Gaussian smoothing";
|
c@119
|
125 desc.description = "Window length for the internal smoothing operation, in chroma analysis frames";
|
c@0
|
126 desc.unit = "frames";
|
c@0
|
127 desc.minValue = 0;
|
c@0
|
128 desc.maxValue = 20;
|
c@0
|
129 desc.defaultValue = 5;
|
c@0
|
130 desc.isQuantized = true;
|
c@0
|
131 desc.quantizeStep = 1;
|
c@0
|
132 list.push_back(desc);
|
c@0
|
133
|
c@23
|
134 desc.identifier = "minpitch";
|
c@23
|
135 desc.name = "Chromagram minimum pitch";
|
c@0
|
136 desc.unit = "MIDI units";
|
c@119
|
137 desc.description = "Lowest pitch in MIDI units to be included in the chroma analysis";
|
c@0
|
138 desc.minValue = 0;
|
c@0
|
139 desc.maxValue = 127;
|
c@0
|
140 desc.defaultValue = 32;
|
c@0
|
141 desc.isQuantized = true;
|
c@0
|
142 desc.quantizeStep = 1;
|
c@0
|
143 list.push_back(desc);
|
c@0
|
144
|
c@23
|
145 desc.identifier = "maxpitch";
|
c@23
|
146 desc.name = "Chromagram maximum pitch";
|
c@0
|
147 desc.unit = "MIDI units";
|
c@119
|
148 desc.description = "Highest pitch in MIDI units to be included in the chroma analysis";
|
c@0
|
149 desc.minValue = 0;
|
c@0
|
150 desc.maxValue = 127;
|
c@0
|
151 desc.defaultValue = 108;
|
c@0
|
152 desc.isQuantized = true;
|
c@0
|
153 desc.quantizeStep = 1;
|
c@0
|
154 list.push_back(desc);
|
c@0
|
155
|
c@23
|
156 desc.identifier = "tuning";
|
c@23
|
157 desc.name = "Chromagram tuning frequency";
|
c@0
|
158 desc.unit = "Hz";
|
c@119
|
159 desc.description = "Frequency of concert A in the music under analysis";
|
c@0
|
160 desc.minValue = 420;
|
c@0
|
161 desc.maxValue = 460;
|
c@0
|
162 desc.defaultValue = 440;
|
c@0
|
163 desc.isQuantized = false;
|
c@0
|
164 list.push_back(desc);
|
c@0
|
165
|
c@0
|
166 return list;
|
c@0
|
167 }
|
c@0
|
168
|
c@0
|
169 float
|
c@0
|
170 TonalChangeDetect::getParameter(std::string param) const
|
c@0
|
171 {
|
c@0
|
172 if (param == "smoothingwidth") {
|
c@0
|
173 return m_iSmoothingWidth;
|
c@0
|
174 }
|
c@0
|
175 if (param == "minpitch") {
|
c@0
|
176 return m_minMIDIPitch;
|
c@0
|
177 }
|
c@0
|
178 if (param == "maxpitch") {
|
c@0
|
179 return m_maxMIDIPitch;
|
c@0
|
180 }
|
c@0
|
181 if (param == "tuning") {
|
c@0
|
182 return m_tuningFrequency;
|
c@0
|
183 }
|
c@0
|
184
|
c@0
|
185 std::cerr << "WARNING: ChromagramPlugin::getParameter: unknown parameter \""
|
c@0
|
186 << param << "\"" << std::endl;
|
c@0
|
187 return 0.0;
|
c@0
|
188 }
|
c@0
|
189
|
c@0
|
190 void
|
c@0
|
191 TonalChangeDetect::setParameter(std::string param, float value)
|
c@0
|
192 {
|
c@0
|
193 if (param == "minpitch") {
|
c@0
|
194 m_minMIDIPitch = lrintf(value);
|
c@0
|
195 } else if (param == "maxpitch") {
|
c@0
|
196 m_maxMIDIPitch = lrintf(value);
|
c@0
|
197 } else if (param == "tuning") {
|
c@0
|
198 m_tuningFrequency = value;
|
c@0
|
199 }
|
c@0
|
200 else if (param == "smoothingwidth") {
|
c@0
|
201 m_iSmoothingWidth = int(value);
|
c@0
|
202 } else {
|
c@0
|
203 std::cerr << "WARNING: ChromagramPlugin::setParameter: unknown parameter \""
|
c@0
|
204 << param << "\"" << std::endl;
|
c@0
|
205 }
|
c@0
|
206
|
c@0
|
207 setupConfig();
|
c@0
|
208 }
|
c@0
|
209
|
c@0
|
210
|
c@0
|
211 void TonalChangeDetect::setupConfig()
|
c@0
|
212 {
|
c@0
|
213 m_config.FS = lrintf(m_inputSampleRate);
|
c@0
|
214 m_config.min = Pitch::getFrequencyForPitch
|
c@0
|
215 (m_minMIDIPitch, 0, m_tuningFrequency);
|
c@0
|
216 m_config.max = Pitch::getFrequencyForPitch
|
c@0
|
217 (m_maxMIDIPitch, 0, m_tuningFrequency);
|
c@0
|
218 m_config.BPO = 12;
|
c@0
|
219 m_config.CQThresh = 0.0054;
|
c@49
|
220 m_config.normalise = MathUtilities::NormaliseNone;
|
c@0
|
221
|
c@0
|
222 m_step = 0;
|
c@0
|
223 m_block = 0;
|
c@0
|
224
|
c@0
|
225
|
c@0
|
226 }
|
c@0
|
227
|
c@0
|
228 void
|
c@0
|
229 TonalChangeDetect::reset()
|
c@0
|
230 {
|
c@0
|
231 if (m_chromagram) {
|
c@0
|
232 delete m_chromagram;
|
c@0
|
233 m_chromagram = new Chromagram(m_config);
|
c@0
|
234 }
|
c@0
|
235 while (!m_pending.empty()) m_pending.pop();
|
c@119
|
236 m_vaCurrentVector.clear();
|
c@171
|
237 m_TCSGram.clear();
|
c@85
|
238
|
c@85
|
239 m_origin = Vamp::RealTime::zeroTime;
|
c@85
|
240 m_haveOrigin = false;
|
c@0
|
241 }
|
c@0
|
242
|
c@0
|
243 size_t
|
c@0
|
244 TonalChangeDetect::getPreferredStepSize() const
|
c@0
|
245 {
|
c@0
|
246 if (!m_step) {
|
c@0
|
247 Chromagram chroma(m_config);
|
c@0
|
248 m_step = chroma.getHopSize();
|
c@0
|
249 m_block = chroma.getFrameSize();
|
c@0
|
250 }
|
c@0
|
251
|
c@0
|
252 return m_step;
|
c@0
|
253 }
|
c@0
|
254
|
c@0
|
255 size_t
|
c@0
|
256 TonalChangeDetect::getPreferredBlockSize() const
|
c@0
|
257 {
|
c@0
|
258 if (!m_step) {
|
c@0
|
259 Chromagram chroma(m_config);
|
c@0
|
260 m_step = chroma.getHopSize();
|
c@0
|
261 m_block = chroma.getFrameSize();
|
c@0
|
262 }
|
c@0
|
263
|
c@0
|
264 return m_block;
|
c@0
|
265 }
|
c@0
|
266
|
c@0
|
267 TonalChangeDetect::OutputList TonalChangeDetect::getOutputDescriptors() const
|
c@0
|
268 {
|
c@0
|
269 OutputList list;
|
c@0
|
270
|
c@0
|
271 OutputDescriptor hc;
|
c@23
|
272 hc.identifier = "tcstransform";
|
c@23
|
273 hc.name = "Transform to 6D Tonal Content Space";
|
c@0
|
274 hc.unit = "";
|
c@119
|
275 hc.description = "Representation of content in a six-dimensional tonal space";
|
c@0
|
276 hc.hasFixedBinCount = true;
|
c@0
|
277 hc.binCount = 6;
|
c@0
|
278 hc.hasKnownExtents = true;
|
c@0
|
279 hc.minValue = -1.0;
|
c@0
|
280 hc.maxValue = 1.0;
|
c@0
|
281 hc.isQuantized = false;
|
c@0
|
282 hc.sampleType = OutputDescriptor::OneSamplePerStep;
|
c@0
|
283
|
c@0
|
284 OutputDescriptor d;
|
c@23
|
285 d.identifier = "tcfunction";
|
c@23
|
286 d.name = "Tonal Change Detection Function";
|
c@0
|
287 d.unit = "";
|
c@119
|
288 d.description = "Estimate of the likelihood of a tonal change occurring within each spectral frame";
|
c@0
|
289 d.minValue = 0;
|
c@0
|
290 d.minValue = 2;
|
c@0
|
291 d.hasFixedBinCount = true;
|
c@0
|
292 d.binCount = 1;
|
c@70
|
293 d.hasKnownExtents = false;
|
c@0
|
294 d.isQuantized = false;
|
c@0
|
295 d.sampleType = OutputDescriptor::VariableSampleRate;
|
c@70
|
296 double dStepSecs = double(getPreferredStepSize()) / m_inputSampleRate;
|
c@0
|
297 d.sampleRate = 1.0f / dStepSecs;
|
c@0
|
298
|
c@0
|
299 OutputDescriptor changes;
|
c@23
|
300 changes.identifier = "changepositions";
|
c@23
|
301 changes.name = "Tonal Change Positions";
|
c@0
|
302 changes.unit = "";
|
c@119
|
303 changes.description = "Estimated locations of tonal changes";
|
c@0
|
304 changes.hasFixedBinCount = true;
|
c@0
|
305 changes.binCount = 0;
|
c@70
|
306 changes.hasKnownExtents = false;
|
c@70
|
307 changes.isQuantized = false;
|
c@0
|
308 changes.sampleType = OutputDescriptor::VariableSampleRate;
|
c@0
|
309 changes.sampleRate = 1.0 / dStepSecs;
|
c@0
|
310
|
c@0
|
311 list.push_back(hc);
|
c@0
|
312 list.push_back(d);
|
c@0
|
313 list.push_back(changes);
|
c@0
|
314
|
c@0
|
315 return list;
|
c@0
|
316 }
|
c@0
|
317
|
c@18
|
318 TonalChangeDetect::FeatureSet
|
c@18
|
319 TonalChangeDetect::process(const float *const *inputBuffers,
|
c@222
|
320
|
c@18
|
321 Vamp::RealTime timestamp)
|
c@0
|
322 {
|
c@0
|
323 if (!m_chromagram) {
|
c@0
|
324 cerr << "ERROR: TonalChangeDetect::process: "
|
c@0
|
325 << "Chromagram has not been initialised"
|
c@0
|
326 << endl;
|
c@0
|
327 return FeatureSet();
|
c@0
|
328 }
|
c@0
|
329
|
c@222
|
330 if (!m_haveOrigin) {
|
c@222
|
331 m_origin = timestamp;
|
c@222
|
332 m_haveOrigin = true;
|
c@222
|
333 }
|
c@85
|
334
|
c@0
|
335 // convert float* to double*
|
c@0
|
336 double *tempBuffer = new double[m_block];
|
c@0
|
337 for (size_t i = 0; i < m_block; ++i) {
|
c@0
|
338 tempBuffer[i] = inputBuffers[0][i];
|
c@0
|
339 }
|
c@0
|
340
|
c@0
|
341 double *output = m_chromagram->process(tempBuffer);
|
c@0
|
342 delete[] tempBuffer;
|
c@0
|
343
|
c@0
|
344 for (size_t i = 0; i < 12; i++)
|
c@0
|
345 {
|
c@0
|
346 m_vaCurrentVector[i] = output[i];
|
c@0
|
347 }
|
c@0
|
348
|
c@0
|
349
|
c@0
|
350 FeatureSet returnFeatures;
|
c@0
|
351
|
c@0
|
352 if (m_stepDelay == 0) {
|
c@0
|
353 m_vaCurrentVector.normalizeL1();
|
c@0
|
354 TCSVector tcsVector = m_TonalEstimator.transform2TCS(m_vaCurrentVector);
|
c@0
|
355 m_TCSGram.addTCSVector(tcsVector);
|
c@0
|
356
|
c@0
|
357 Feature feature;
|
c@0
|
358 feature.hasTimestamp = false;
|
c@0
|
359 for (int i = 0; i < 6; i++)
|
c@0
|
360 { feature.values.push_back(static_cast<float>(tcsVector[i])); }
|
c@0
|
361 feature.label = "";
|
c@0
|
362 returnFeatures[0].push_back(feature);
|
c@0
|
363
|
c@0
|
364 return returnFeatures;
|
c@0
|
365 }
|
c@0
|
366
|
c@0
|
367 if (m_pending.size() == m_stepDelay) {
|
c@0
|
368
|
c@0
|
369 ChromaVector v = m_pending.front();
|
c@0
|
370 v.normalizeL1();
|
c@0
|
371 TCSVector tcsVector = m_TonalEstimator.transform2TCS(v);
|
c@0
|
372 m_TCSGram.addTCSVector(tcsVector);
|
c@0
|
373
|
c@0
|
374 Feature feature;
|
c@0
|
375 feature.hasTimestamp = false;
|
c@0
|
376 for (int i = 0; i < 6; i++)
|
c@0
|
377 { feature.values.push_back(static_cast<float>(tcsVector[i])); }
|
c@0
|
378 feature.label = "";
|
c@0
|
379 returnFeatures[0].push_back(feature);
|
c@0
|
380 m_pending.pop();
|
c@0
|
381
|
c@0
|
382 } else {
|
c@0
|
383 returnFeatures[0].push_back(Feature());
|
c@0
|
384 m_TCSGram.addTCSVector(TCSVector());
|
c@0
|
385 }
|
c@0
|
386
|
c@0
|
387 m_pending.push(m_vaCurrentVector);
|
c@0
|
388
|
c@0
|
389
|
c@0
|
390 return returnFeatures;
|
c@0
|
391 }
|
c@0
|
392
|
c@0
|
393 TonalChangeDetect::FeatureSet TonalChangeDetect::getRemainingFeatures()
|
c@0
|
394 {
|
c@0
|
395 FeatureSet returnFeatures;
|
c@0
|
396
|
c@0
|
397 while (!m_pending.empty()) {
|
c@0
|
398 ChromaVector v = m_pending.front();
|
c@0
|
399 v.normalizeL1();
|
c@0
|
400 TCSVector tcsVector = m_TonalEstimator.transform2TCS(v);
|
c@0
|
401 m_TCSGram.addTCSVector(tcsVector);
|
c@0
|
402
|
c@0
|
403 Feature feature;
|
c@0
|
404 feature.hasTimestamp = false;
|
c@0
|
405 for (int i = 0; i < 6; i++)
|
c@0
|
406 { feature.values.push_back(static_cast<float>(tcsVector[i])); }
|
c@0
|
407 feature.label = "";
|
c@0
|
408 returnFeatures[0].push_back(feature);
|
c@0
|
409 m_pending.pop();
|
c@0
|
410 }
|
c@0
|
411
|
c@0
|
412 ChangeDFConfig dfc;
|
c@0
|
413 dfc.smoothingWidth = double(m_iSmoothingWidth);
|
c@0
|
414 ChangeDetectionFunction df(dfc);
|
c@0
|
415 ChangeDistance d = df.process(m_TCSGram);
|
c@0
|
416
|
c@178
|
417 for (int i = 0; i < int(d.size()); i++)
|
c@0
|
418 {
|
c@0
|
419 double dCurrent = d[i];
|
c@0
|
420 double dPrevious = d[i > 0 ? i - 1 : i];
|
c@178
|
421 double dNext = d[i < int(d.size())-1 ? i + 1 : i];
|
c@0
|
422
|
c@0
|
423 Feature feature;
|
c@0
|
424 feature.label = "";
|
c@0
|
425 feature.hasTimestamp = true;
|
c@85
|
426 feature.timestamp = m_origin +
|
c@85
|
427 Vamp::RealTime::frame2RealTime(i*m_step, m_inputSampleRate);
|
c@0
|
428 feature.values.push_back(dCurrent);
|
c@0
|
429 returnFeatures[1].push_back(feature);
|
c@0
|
430
|
c@0
|
431
|
c@0
|
432 if (dCurrent > dPrevious && dCurrent > dNext)
|
c@0
|
433 {
|
c@0
|
434 Feature featurePeak;
|
c@0
|
435 featurePeak.label = "";
|
c@0
|
436 featurePeak.hasTimestamp = true;
|
c@85
|
437 featurePeak.timestamp = m_origin +
|
c@85
|
438 Vamp::RealTime::frame2RealTime(i*m_step, m_inputSampleRate);
|
c@62
|
439 returnFeatures[2].push_back(featurePeak);
|
c@0
|
440 }
|
c@0
|
441
|
c@0
|
442 }
|
c@0
|
443
|
c@0
|
444
|
c@0
|
445 return returnFeatures;
|
c@0
|
446
|
c@0
|
447 }
|
c@0
|
448
|