annotate plugins/ChromagramPlugin.cpp @ 45:5d7ce1d87301

* Add MFCC plugin * Add means output to Chromagram plugin * Update similarity plugin for MFCC changes
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 18 Jan 2008 13:30:56 +0000
parents 6d014fb538db
children fc88b465548a
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 "ChromagramPlugin.h"
c@0 11
c@3 12 #include <base/Pitch.h>
c@3 13 #include <dsp/chromagram/Chromagram.h>
c@0 14
c@0 15 using std::string;
c@0 16 using std::vector;
c@0 17 using std::cerr;
c@0 18 using std::endl;
c@0 19
c@0 20 ChromagramPlugin::ChromagramPlugin(float inputSampleRate) :
c@0 21 Vamp::Plugin(inputSampleRate),
c@0 22 m_chromagram(0),
c@0 23 m_step(0),
c@7 24 m_block(0)
c@0 25 {
c@0 26 m_minMIDIPitch = 12;
c@0 27 m_maxMIDIPitch = 96;
c@0 28 m_tuningFrequency = 440;
c@0 29 m_normalized = true;
c@0 30 m_bpo = 12;
c@0 31
c@0 32 setupConfig();
c@0 33 }
c@0 34
c@0 35 void
c@0 36 ChromagramPlugin::setupConfig()
c@0 37 {
c@0 38 m_config.FS = lrintf(m_inputSampleRate);
c@0 39 m_config.min = Pitch::getFrequencyForPitch
c@0 40 (m_minMIDIPitch, 0, m_tuningFrequency);
c@0 41 m_config.max = Pitch::getFrequencyForPitch
c@0 42 (m_maxMIDIPitch, 0, m_tuningFrequency);
c@0 43 m_config.BPO = m_bpo;
c@0 44 m_config.CQThresh = 0.0054;
c@0 45 m_config.isNormalised = m_normalized;
c@0 46
c@0 47 m_step = 0;
c@0 48 m_block = 0;
c@0 49 }
c@0 50
c@0 51 ChromagramPlugin::~ChromagramPlugin()
c@0 52 {
c@0 53 delete m_chromagram;
c@0 54 }
c@0 55
c@0 56 string
c@22 57 ChromagramPlugin::getIdentifier() const
c@0 58 {
c@9 59 return "qm-chromagram";
c@0 60 }
c@0 61
c@0 62 string
c@22 63 ChromagramPlugin::getName() const
c@22 64 {
c@22 65 return "Chromagram";
c@22 66 }
c@22 67
c@22 68 string
c@0 69 ChromagramPlugin::getDescription() const
c@0 70 {
c@22 71 //!!!
c@22 72 return "";
c@0 73 }
c@0 74
c@0 75 string
c@0 76 ChromagramPlugin::getMaker() const
c@0 77 {
c@5 78 return "Queen Mary, University of London";
c@0 79 }
c@0 80
c@0 81 int
c@0 82 ChromagramPlugin::getPluginVersion() const
c@0 83 {
c@45 84 return 3;
c@0 85 }
c@0 86
c@0 87 string
c@0 88 ChromagramPlugin::getCopyright() const
c@0 89 {
c@45 90 //!!! update
c@0 91 return "Copyright (c) 2006 - All Rights Reserved";
c@0 92 }
c@0 93
c@0 94 ChromagramPlugin::ParameterList
c@0 95 ChromagramPlugin::getParameterDescriptors() const
c@0 96 {
c@0 97 ParameterList list;
c@0 98
c@0 99 ParameterDescriptor desc;
c@22 100 desc.identifier = "minpitch";
c@22 101 desc.name = "Minimum Pitch";
c@0 102 desc.unit = "MIDI units";
c@45 103 //!!! descriptions
c@0 104 desc.minValue = 0;
c@0 105 desc.maxValue = 127;
c@0 106 desc.defaultValue = 12;
c@0 107 desc.isQuantized = true;
c@0 108 desc.quantizeStep = 1;
c@0 109 list.push_back(desc);
c@0 110
c@22 111 desc.identifier = "maxpitch";
c@22 112 desc.name = "Maximum Pitch";
c@0 113 desc.unit = "MIDI units";
c@0 114 desc.minValue = 0;
c@0 115 desc.maxValue = 127;
c@0 116 desc.defaultValue = 96;
c@0 117 desc.isQuantized = true;
c@0 118 desc.quantizeStep = 1;
c@0 119 list.push_back(desc);
c@0 120
c@22 121 desc.identifier = "tuning";
c@22 122 desc.name = "Tuning Frequency";
c@0 123 desc.unit = "Hz";
c@0 124 desc.minValue = 420;
c@0 125 desc.maxValue = 460;
c@0 126 desc.defaultValue = 440;
c@0 127 desc.isQuantized = false;
c@0 128 list.push_back(desc);
c@0 129
c@22 130 desc.identifier = "bpo";
c@22 131 desc.name = "Bins per Octave";
c@0 132 desc.unit = "bins";
c@0 133 desc.minValue = 2;
c@0 134 desc.maxValue = 36;
c@0 135 desc.defaultValue = 12;
c@0 136 desc.isQuantized = true;
c@0 137 desc.quantizeStep = 1;
c@0 138 list.push_back(desc);
c@0 139
c@22 140 desc.identifier = "normalized";
c@22 141 desc.name = "Normalized";
c@0 142 desc.unit = "";
c@0 143 desc.minValue = 0;
c@0 144 desc.maxValue = 1;
c@0 145 desc.defaultValue = 1;
c@0 146 desc.isQuantized = true;
c@0 147 desc.quantizeStep = 1;
c@0 148 list.push_back(desc);
c@0 149
c@0 150 return list;
c@0 151 }
c@0 152
c@0 153 float
c@0 154 ChromagramPlugin::getParameter(std::string param) const
c@0 155 {
c@0 156 if (param == "minpitch") {
c@0 157 return m_minMIDIPitch;
c@0 158 }
c@0 159 if (param == "maxpitch") {
c@0 160 return m_maxMIDIPitch;
c@0 161 }
c@0 162 if (param == "tuning") {
c@0 163 return m_tuningFrequency;
c@0 164 }
c@0 165 if (param == "bpo") {
c@0 166 return m_bpo;
c@0 167 }
c@0 168 if (param == "normalized") {
c@0 169 return m_normalized;
c@0 170 }
c@0 171 std::cerr << "WARNING: ChromagramPlugin::getParameter: unknown parameter \""
c@0 172 << param << "\"" << std::endl;
c@0 173 return 0.0;
c@0 174 }
c@0 175
c@0 176 void
c@0 177 ChromagramPlugin::setParameter(std::string param, float value)
c@0 178 {
c@0 179 if (param == "minpitch") {
c@0 180 m_minMIDIPitch = lrintf(value);
c@0 181 } else if (param == "maxpitch") {
c@0 182 m_maxMIDIPitch = lrintf(value);
c@0 183 } else if (param == "tuning") {
c@0 184 m_tuningFrequency = value;
c@0 185 } else if (param == "bpo") {
c@0 186 m_bpo = lrintf(value);
c@0 187 } else if (param == "normalized") {
c@0 188 m_normalized = (value > 0.0001);
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 bool
c@0 199 ChromagramPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize)
c@0 200 {
c@0 201 if (m_chromagram) {
c@0 202 delete m_chromagram;
c@0 203 m_chromagram = 0;
c@0 204 }
c@0 205
c@0 206 if (channels < getMinChannelCount() ||
c@0 207 channels > getMaxChannelCount()) return false;
c@0 208
c@0 209 std::cerr << "ChromagramPlugin::initialise: step " << stepSize << ", block "
c@7 210 << blockSize << std::endl;
c@0 211
c@0 212 m_chromagram = new Chromagram(m_config);
c@45 213 m_binsums = vector<double>(m_config.BPO);
c@45 214
c@45 215 for (int i = 0; i < m_config.BPO; ++i) {
c@45 216 m_binsums[i] = 0.0;
c@45 217 }
c@45 218
c@45 219 m_count = 0;
c@13 220
c@13 221 m_step = m_chromagram->getHopSize();
c@13 222 m_block = m_chromagram->getFrameSize();
c@13 223
c@13 224 if (stepSize != m_step ||
c@13 225 blockSize != m_block) {
c@13 226 delete m_chromagram;
c@13 227 m_chromagram = 0;
c@13 228 return false;
c@13 229 }
c@13 230
c@0 231 return true;
c@0 232 }
c@0 233
c@0 234 void
c@0 235 ChromagramPlugin::reset()
c@0 236 {
c@0 237 if (m_chromagram) {
c@0 238 delete m_chromagram;
c@0 239 m_chromagram = new Chromagram(m_config);
c@0 240 }
c@0 241 }
c@0 242
c@0 243 size_t
c@0 244 ChromagramPlugin::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 ChromagramPlugin::getPreferredBlockSize() const
c@0 257 {
c@0 258 if (!m_block) {
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 ChromagramPlugin::OutputList
c@0 268 ChromagramPlugin::getOutputDescriptors() const
c@0 269 {
c@0 270 OutputList list;
c@0 271
c@0 272 OutputDescriptor d;
c@22 273 d.identifier = "chromagram";
c@22 274 d.name = "Chromagram";
c@0 275 d.unit = "";
c@0 276 d.hasFixedBinCount = true;
c@0 277 d.binCount = m_config.BPO;
c@0 278
c@0 279 const char *names[] =
c@0 280 { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
c@0 281
c@0 282 if (d.binCount % 12 == 0) {
c@0 283 for (int i = 0; i < 12; ++i) {
c@0 284 int ipc = m_minMIDIPitch % 12;
c@0 285 int index = (i + ipc) % 12;
c@0 286 d.binNames.push_back(names[index]);
c@17 287 for (int j = 0; j < int(d.binCount) / 12 - 1; ++j) {
c@0 288 d.binNames.push_back("");
c@0 289 }
c@0 290 }
c@0 291 } else {
c@9 292 d.binNames.push_back(names[m_minMIDIPitch % 12]);
c@0 293 }
c@0 294
c@0 295 d.hasKnownExtents = m_normalized;
c@0 296 d.minValue = 0.0;
c@0 297 d.maxValue = (m_normalized ? 1.0 : 0.0);
c@0 298 d.isQuantized = false;
c@0 299 d.sampleType = OutputDescriptor::OneSamplePerStep;
c@0 300 list.push_back(d);
c@9 301
c@45 302 d.identifier = "chromameans";
c@45 303 d.name = "Chroma Means";
c@45 304 //!!! descriptions
c@45 305 d.sampleType = OutputDescriptor::FixedSampleRate;
c@45 306 d.sampleRate = 1;
c@45 307 list.push_back(d);
c@45 308
c@0 309 return list;
c@0 310 }
c@0 311
c@0 312 ChromagramPlugin::Feature
c@0 313 ChromagramPlugin::normalize(const Feature &feature)
c@0 314 {
c@0 315 float min = 0.0, max = 0.0;
c@0 316
c@0 317 for (size_t i = 0; i < feature.values.size(); ++i) {
c@0 318 if (i == 0 || feature.values[i] < min) min = feature.values[i];
c@0 319 if (i == 0 || feature.values[i] > max) max = feature.values[i];
c@0 320 }
c@0 321
c@0 322 if (max == 0.0 || max == min) return feature;
c@0 323
c@0 324 Feature normalized;
c@0 325 normalized.hasTimestamp = false;
c@0 326
c@0 327 for (size_t i = 0; i < feature.values.size(); ++i) {
c@0 328 normalized.values.push_back((feature.values[i] - min) / (max - min));
c@0 329 }
c@0 330
c@0 331 return normalized;
c@0 332 }
c@0 333
c@0 334 ChromagramPlugin::FeatureSet
c@18 335 ChromagramPlugin::process(const float *const *inputBuffers,
c@18 336 Vamp::RealTime /* timestamp */)
c@0 337 {
c@0 338 if (!m_chromagram) {
c@0 339 cerr << "ERROR: ChromagramPlugin::process: "
c@0 340 << "Chromagram has not been initialised"
c@0 341 << endl;
c@0 342 return FeatureSet();
c@0 343 }
c@0 344
c@7 345 double *real = new double[m_block];
c@7 346 double *imag = new double[m_block];
c@7 347
c@7 348 for (size_t i = 0; i < m_block/2; ++i) {
c@7 349 real[i] = inputBuffers[0][i*2];
c@17 350 if (i > 0) real[m_block - i] = real[i];
c@7 351 imag[i] = inputBuffers[0][i*2+1];
c@17 352 if (i > 0) imag[m_block - i] = imag[i];
c@0 353 }
c@0 354
c@7 355 double *output = m_chromagram->process(real, imag);
c@7 356
c@7 357 delete[] real;
c@7 358 delete[] imag;
c@7 359
c@0 360 Feature feature;
c@0 361 feature.hasTimestamp = false;
c@0 362 for (size_t i = 0; i < m_config.BPO; ++i) {
c@16 363 double value = output[i];
c@16 364 if (isnan(value)) value = 0.0;
c@45 365 m_binsums[i] += value;
c@16 366 feature.values.push_back(value);
c@0 367 }
c@0 368 feature.label = "";
c@45 369 ++m_count;
c@0 370
c@0 371 FeatureSet returnFeatures;
c@7 372 returnFeatures[0].push_back(feature);
c@0 373 return returnFeatures;
c@0 374 }
c@0 375
c@0 376 ChromagramPlugin::FeatureSet
c@0 377 ChromagramPlugin::getRemainingFeatures()
c@0 378 {
c@45 379 Feature feature;
c@45 380 feature.hasTimestamp = true;
c@45 381 feature.timestamp = Vamp::RealTime::zeroTime;
c@45 382
c@45 383 for (size_t i = 0; i < m_config.BPO; ++i) {
c@45 384 double v = m_binsums[i];
c@45 385 if (m_count > 0) v /= m_count;
c@45 386 feature.values.push_back(v);
c@45 387 }
c@45 388 feature.label = "Chromagram bin means";
c@45 389
c@45 390 FeatureSet returnFeatures;
c@45 391 returnFeatures[1].push_back(feature);
c@45 392 return returnFeatures;
c@0 393 }
c@0 394