annotate plugins/ChromagramPlugin.cpp @ 139:bc07394fbde0

Merge
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 05 Apr 2011 11:57:05 +0100
parents dcf5800f0f00
children f96ea0e4b475
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 {
c@94 88 return 4;
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@0 219 m_chromagram = new Chromagram(m_config);
c@45 220 m_binsums = vector<double>(m_config.BPO);
c@45 221
c@45 222 for (int i = 0; i < m_config.BPO; ++i) {
c@45 223 m_binsums[i] = 0.0;
c@45 224 }
c@45 225
c@45 226 m_count = 0;
c@13 227
c@13 228 m_step = m_chromagram->getHopSize();
c@13 229 m_block = m_chromagram->getFrameSize();
c@95 230 if (m_step < 1) m_step = 1;
c@13 231
c@52 232 if (blockSize != m_block) {
c@52 233 std::cerr << "ChromagramPlugin::initialise: ERROR: supplied block size " << blockSize << " differs from required block size " << m_block << ", initialise failing" << std::endl;
c@13 234 delete m_chromagram;
c@13 235 m_chromagram = 0;
c@13 236 return false;
c@13 237 }
c@13 238
c@52 239 if (stepSize != m_step) {
c@52 240 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 241 }
c@52 242
c@0 243 return true;
c@0 244 }
c@0 245
c@0 246 void
c@0 247 ChromagramPlugin::reset()
c@0 248 {
c@0 249 if (m_chromagram) {
c@0 250 delete m_chromagram;
c@0 251 m_chromagram = new Chromagram(m_config);
c@83 252 for (int i = 0; i < m_config.BPO; ++i) {
c@83 253 m_binsums[i] = 0.0;
c@83 254 }
c@83 255 m_count = 0;
c@0 256 }
c@0 257 }
c@0 258
c@0 259 size_t
c@0 260 ChromagramPlugin::getPreferredStepSize() const
c@0 261 {
c@0 262 if (!m_step) {
c@0 263 Chromagram chroma(m_config);
c@0 264 m_step = chroma.getHopSize();
c@0 265 m_block = chroma.getFrameSize();
c@95 266 if (m_step < 1) m_step = 1;
c@0 267 }
c@0 268
c@0 269 return m_step;
c@0 270 }
c@0 271
c@0 272 size_t
c@0 273 ChromagramPlugin::getPreferredBlockSize() const
c@0 274 {
c@0 275 if (!m_block) {
c@0 276 Chromagram chroma(m_config);
c@0 277 m_step = chroma.getHopSize();
c@0 278 m_block = chroma.getFrameSize();
c@95 279 if (m_step < 1) m_step = 1;
c@0 280 }
c@0 281
c@0 282 return m_block;
c@0 283 }
c@0 284
c@0 285 ChromagramPlugin::OutputList
c@0 286 ChromagramPlugin::getOutputDescriptors() const
c@0 287 {
c@0 288 OutputList list;
c@0 289
c@0 290 OutputDescriptor d;
c@22 291 d.identifier = "chromagram";
c@22 292 d.name = "Chromagram";
c@0 293 d.unit = "";
c@52 294 d.description = "Output of chromagram, as a single vector per process block";
c@0 295 d.hasFixedBinCount = true;
c@0 296 d.binCount = m_config.BPO;
c@0 297
c@0 298 const char *names[] =
c@0 299 { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
c@0 300
c@0 301 if (d.binCount % 12 == 0) {
c@0 302 for (int i = 0; i < 12; ++i) {
c@0 303 int ipc = m_minMIDIPitch % 12;
c@0 304 int index = (i + ipc) % 12;
c@0 305 d.binNames.push_back(names[index]);
c@17 306 for (int j = 0; j < int(d.binCount) / 12 - 1; ++j) {
c@0 307 d.binNames.push_back("");
c@0 308 }
c@0 309 }
c@0 310 } else {
c@9 311 d.binNames.push_back(names[m_minMIDIPitch % 12]);
c@0 312 }
c@0 313
c@49 314 d.hasKnownExtents = (m_normalise != MathUtilities::NormaliseNone);
c@0 315 d.minValue = 0.0;
c@49 316 d.maxValue = (d.hasKnownExtents ? 1.0 : 0.0);
c@0 317 d.isQuantized = false;
c@0 318 d.sampleType = OutputDescriptor::OneSamplePerStep;
c@0 319 list.push_back(d);
c@9 320
c@45 321 d.identifier = "chromameans";
c@45 322 d.name = "Chroma Means";
c@52 323 d.description = "Mean values of chromagram bins across the duration of the input audio";
c@45 324 d.sampleType = OutputDescriptor::FixedSampleRate;
c@45 325 d.sampleRate = 1;
c@45 326 list.push_back(d);
c@45 327
c@0 328 return list;
c@0 329 }
c@0 330
c@0 331 ChromagramPlugin::FeatureSet
c@18 332 ChromagramPlugin::process(const float *const *inputBuffers,
c@75 333 Vamp::RealTime timestamp)
c@0 334 {
c@0 335 if (!m_chromagram) {
c@0 336 cerr << "ERROR: ChromagramPlugin::process: "
c@0 337 << "Chromagram has not been initialised"
c@0 338 << endl;
c@0 339 return FeatureSet();
c@0 340 }
c@0 341
c@7 342 double *real = new double[m_block];
c@7 343 double *imag = new double[m_block];
c@7 344
c@75 345 for (size_t i = 0; i <= m_block/2; ++i) {
c@7 346 real[i] = inputBuffers[0][i*2];
c@17 347 if (i > 0) real[m_block - i] = real[i];
c@7 348 imag[i] = inputBuffers[0][i*2+1];
c@17 349 if (i > 0) imag[m_block - i] = imag[i];
c@0 350 }
c@0 351
c@75 352 // cerr << "chromagram: timestamp = " << timestamp << endl;
c@75 353 /*
c@75 354 bool printThis = false;
c@75 355
c@75 356 if (timestamp.sec == 3 && timestamp.nsec < 250000000) {
c@75 357 printThis = true;
c@75 358 }
c@75 359 if (printThis) {
c@75 360 cerr << "\n\nchromagram: timestamp " << timestamp << ": input data starts:" << endl;
c@75 361 for (int i = 0; i < m_block && i < 1000; ++i) {
c@75 362 cerr << real[i] << "," << imag[i] << " ";
c@75 363 }
c@75 364 cerr << endl << "values:" << endl;
c@75 365 }
c@75 366 */
c@7 367 double *output = m_chromagram->process(real, imag);
c@7 368
c@7 369 delete[] real;
c@7 370 delete[] imag;
c@7 371
c@0 372 Feature feature;
c@0 373 feature.hasTimestamp = false;
c@0 374 for (size_t i = 0; i < m_config.BPO; ++i) {
c@16 375 double value = output[i];
c@75 376 /*
c@75 377 if (printThis) {
c@75 378 cerr << value << " ";
c@75 379 }
c@75 380 */
c@130 381 if (ISNAN(value)) value = 0.0;
c@45 382 m_binsums[i] += value;
c@16 383 feature.values.push_back(value);
c@0 384 }
c@0 385 feature.label = "";
c@45 386 ++m_count;
c@75 387 /*
c@75 388 if (printThis) {
c@75 389 cerr << endl;
c@75 390 }
c@75 391 */
c@0 392
c@0 393 FeatureSet returnFeatures;
c@7 394 returnFeatures[0].push_back(feature);
c@0 395 return returnFeatures;
c@0 396 }
c@0 397
c@0 398 ChromagramPlugin::FeatureSet
c@0 399 ChromagramPlugin::getRemainingFeatures()
c@0 400 {
c@45 401 Feature feature;
c@45 402 feature.hasTimestamp = true;
c@45 403 feature.timestamp = Vamp::RealTime::zeroTime;
c@45 404
c@45 405 for (size_t i = 0; i < m_config.BPO; ++i) {
c@45 406 double v = m_binsums[i];
c@45 407 if (m_count > 0) v /= m_count;
c@45 408 feature.values.push_back(v);
c@45 409 }
c@45 410 feature.label = "Chromagram bin means";
c@45 411
c@45 412 FeatureSet returnFeatures;
c@45 413 returnFeatures[1].push_back(feature);
c@45 414 return returnFeatures;
c@0 415 }
c@0 416