annotate plugins/TonalChangeDetect.cpp @ 50:df7a0bc46592

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