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