annotate plugins/ChromagramPlugin.cpp @ 266:d04675d44928 tip master

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