annotate plugins/TonalChangeDetect.cpp @ 119:2416ef0e9319

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