annotate vamp/CQVamp.cpp @ 196:da283326bcd3 tip master

Update plugin versions in RDF
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 28 Feb 2020 09:43:02 +0000
parents ba2a5f0ae66a
children
rev   line source
c@35 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@69 2 /*
c@69 3 Constant-Q library
c@69 4 Copyright (c) 2013-2014 Queen Mary, University of London
c@69 5
c@69 6 Permission is hereby granted, free of charge, to any person
c@69 7 obtaining a copy of this software and associated documentation
c@69 8 files (the "Software"), to deal in the Software without
c@69 9 restriction, including without limitation the rights to use, copy,
c@69 10 modify, merge, publish, distribute, sublicense, and/or sell copies
c@69 11 of the Software, and to permit persons to whom the Software is
c@69 12 furnished to do so, subject to the following conditions:
c@69 13
c@69 14 The above copyright notice and this permission notice shall be
c@69 15 included in all copies or substantial portions of the Software.
c@69 16
c@69 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c@69 18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c@69 19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c@69 20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
c@69 21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
c@69 22 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
c@69 23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
c@69 24
c@69 25 Except as contained in this notice, the names of the Centre for
c@69 26 Digital Music; Queen Mary, University of London; and Chris Cannam
c@69 27 shall not be used in advertising or otherwise to promote the sale,
c@69 28 use or other dealings in this Software without prior written
c@69 29 authorization.
c@69 30 */
c@35 31
c@35 32 #include "CQVamp.h"
c@35 33
c@121 34 #include "Pitch.h"
c@55 35
c@58 36 #include <algorithm>
c@58 37 #include <cstdio>
c@58 38
c@35 39 using std::string;
c@35 40 using std::vector;
c@35 41 using std::cerr;
c@35 42 using std::endl;
c@35 43
c@109 44 // The plugin offers either MIDI pitch or frequency range parameters,
c@109 45 // depending on the midiPitchParameters option given to the
c@109 46 // constructor. It never offers both. So they can have different
c@109 47 // defaults; if we're using MIDI pitch, the min and max frequencies
c@109 48 // will come from those rather than from the m_minFrequency and
c@109 49 // m_maxFrequency members.
c@109 50 static const int defaultMinMIDIPitch = 36;
c@109 51 static const int defaultMaxMIDIPitch = 96;
c@109 52 static const int defaultBPO = 36;
c@154 53 static const float defaultMinFrequency = 110;
c@154 54 static const float defaultMaxFrequency = 14700;
c@109 55 static const float defaultTuningFrequency = 440.f;
c@109 56
c@109 57 CQVamp::CQVamp(float inputSampleRate, bool midiPitchParameters) :
c@35 58 Vamp::Plugin(inputSampleRate),
c@109 59 m_midiPitchParameters(midiPitchParameters),
c@109 60 m_minMIDIPitch(defaultMinMIDIPitch),
c@109 61 m_maxMIDIPitch(defaultMaxMIDIPitch),
c@109 62 m_tuningFrequency(defaultTuningFrequency),
c@109 63 m_bpo(defaultBPO),
c@190 64 m_atomOverlap(4),
c@190 65 m_useDraftDecimator(false),
c@90 66 m_interpolation(CQSpectrogram::InterpolateLinear),
c@35 67 m_cq(0),
c@154 68 m_maxFrequency(defaultMaxFrequency),
c@154 69 m_minFrequency(defaultMinFrequency),
c@53 70 m_haveStartTime(false),
c@53 71 m_columnCount(0)
c@35 72 {
c@35 73 }
c@35 74
c@35 75 CQVamp::~CQVamp()
c@35 76 {
c@35 77 delete m_cq;
c@35 78 }
c@35 79
c@35 80 string
c@35 81 CQVamp::getIdentifier() const
c@35 82 {
c@109 83 if (m_midiPitchParameters) {
c@109 84 return "cqvampmidi";
c@109 85 } else {
c@109 86 return "cqvamp";
c@109 87 }
c@35 88 }
c@35 89
c@35 90 string
c@35 91 CQVamp::getName() const
c@35 92 {
c@109 93 if (m_midiPitchParameters) {
c@169 94 return "CQ Constant-Q Spectrogram (MIDI pitch range)";
c@109 95 } else {
c@169 96 return "CQ Constant-Q Spectrogram (Hz range)";
c@109 97 }
c@35 98 }
c@35 99
c@35 100 string
c@35 101 CQVamp::getDescription() const
c@35 102 {
c@109 103 if (m_midiPitchParameters) {
c@109 104 return "Extract a spectrogram with constant ratio of centre frequency to resolution from the input audio, specifying the frequency range in MIDI pitch units.";
c@109 105 } else {
c@109 106 return "Extract a spectrogram with constant ratio of centre frequency to resolution from the input audio, specifying the frequency range in Hz.";
c@109 107 }
c@35 108 }
c@35 109
c@35 110 string
c@35 111 CQVamp::getMaker() const
c@35 112 {
c@35 113 return "Queen Mary, University of London";
c@35 114 }
c@35 115
c@35 116 int
c@35 117 CQVamp::getPluginVersion() const
c@35 118 {
c@190 119 return 3;
c@35 120 }
c@35 121
c@35 122 string
c@35 123 CQVamp::getCopyright() const
c@35 124 {
c@190 125 return "Plugin by Chris Cannam. Method by Christian Schörkhuber and Anssi Klapuri. Copyright (c) 2015-2017 QMUL. BSD/MIT licence.";
c@35 126 }
c@35 127
c@35 128 CQVamp::ParameterList
c@35 129 CQVamp::getParameterDescriptors() const
c@35 130 {
c@35 131 ParameterList list;
c@35 132
c@55 133 ParameterDescriptor desc;
c@55 134
c@109 135 if (m_midiPitchParameters) {
c@55 136
c@109 137 desc.identifier = "minpitch";
c@109 138 desc.name = "Minimum Pitch";
c@109 139 desc.unit = "MIDI units";
c@109 140 desc.description = "MIDI pitch corresponding to the lowest frequency to be included in the constant-Q transform. (The actual minimum frequency may be lower, as the range always covers an integral number of octaves below the highest frequency.)";
c@109 141 desc.minValue = 0;
c@109 142 desc.maxValue = 127;
c@151 143 desc.defaultValue = defaultMinMIDIPitch;
c@109 144 desc.isQuantized = true;
c@109 145 desc.quantizeStep = 1;
c@109 146 list.push_back(desc);
c@109 147
c@109 148 desc.identifier = "maxpitch";
c@109 149 desc.name = "Maximum Pitch";
c@109 150 desc.unit = "MIDI units";
c@109 151 desc.description = "MIDI pitch corresponding to the highest frequency to be included in the constant-Q transform";
c@109 152 desc.minValue = 0;
c@109 153 desc.maxValue = 127;
c@151 154 desc.defaultValue = defaultMaxMIDIPitch;
c@109 155 desc.isQuantized = true;
c@109 156 desc.quantizeStep = 1;
c@109 157 list.push_back(desc);
c@109 158
c@109 159 desc.identifier = "tuning";
c@109 160 desc.name = "Tuning Frequency";
c@109 161 desc.unit = "Hz";
c@109 162 desc.description = "Frequency of concert A";
c@109 163 desc.minValue = 360;
c@109 164 desc.maxValue = 500;
c@151 165 desc.defaultValue = defaultTuningFrequency;
c@109 166 desc.isQuantized = false;
c@109 167 list.push_back(desc);
c@109 168
c@109 169 } else {
c@109 170
c@109 171 desc.identifier = "minfreq";
c@109 172 desc.name = "Minimum Frequency";
c@109 173 desc.unit = "Hz";
c@109 174 desc.description = "Lowest frequency to be included in the constant-Q transform. (The actual minimum frequency may be lower, as the range always covers an integral number of octaves below the highest frequency.)";
c@109 175 desc.minValue = 1;
c@153 176 desc.maxValue = 22050;
c@154 177 desc.defaultValue = defaultMinFrequency;
c@109 178 desc.isQuantized = false;
c@109 179 list.push_back(desc);
c@109 180
c@109 181 desc.identifier = "maxfreq";
c@109 182 desc.name = "Maximum Frequency";
c@109 183 desc.unit = "Hz";
c@109 184 desc.description = "MIDI pitch corresponding to the highest frequency to be included in the constant-Q transform";
c@109 185 desc.minValue = 1;
c@153 186 desc.maxValue = 22050;
c@154 187 desc.defaultValue = defaultMaxFrequency;
c@109 188 desc.isQuantized = false;
c@109 189 list.push_back(desc);
c@109 190 }
c@35 191
c@35 192 desc.identifier = "bpo";
c@35 193 desc.name = "Bins per Octave";
c@35 194 desc.unit = "bins";
c@35 195 desc.description = "Number of constant-Q transform bins per octave";
c@35 196 desc.minValue = 2;
c@35 197 desc.maxValue = 480;
c@110 198 desc.defaultValue = defaultBPO;
c@35 199 desc.isQuantized = true;
c@35 200 desc.quantizeStep = 1;
c@35 201 list.push_back(desc);
c@35 202
c@190 203 desc.identifier = "atomoverlap";
c@190 204 desc.name = "Overlap";
c@190 205 desc.unit = "";
c@190 206 desc.description = "Overlap factor for CQ kernel atoms (higher = more output values per unit time)";
c@190 207 desc.minValue = 1;
c@190 208 desc.maxValue = 8;
c@190 209 desc.defaultValue = 4;
c@190 210 desc.isQuantized = true;
c@190 211 desc.quantizeStep = 1;
c@190 212 list.push_back(desc);
c@190 213
c@190 214 desc.identifier = "draftdecimator";
c@190 215 desc.name = "Use Draft Decimator";
c@190 216 desc.unit = "";
c@190 217 desc.description = "Trade off some decimator quality for faster speed";
c@190 218 desc.minValue = 0;
c@190 219 desc.maxValue = 1;
c@190 220 desc.defaultValue = 0;
c@190 221 desc.isQuantized = true;
c@190 222 desc.quantizeStep = 1;
c@190 223 list.push_back(desc);
c@190 224
c@75 225 desc.identifier = "interpolation";
c@75 226 desc.name = "Interpolation";
c@75 227 desc.unit = "";
c@75 228 desc.description = "Interpolation method used to fill empty cells in lower octaves";
c@75 229 desc.minValue = 0;
c@75 230 desc.maxValue = 2;
c@75 231 desc.defaultValue = 2;
c@75 232 desc.isQuantized = true;
c@75 233 desc.quantizeStep = 1;
c@90 234 desc.valueNames.push_back("None, leave as zero");
c@75 235 desc.valueNames.push_back("None, repeat prior value");
c@75 236 desc.valueNames.push_back("Linear interpolation");
c@75 237 list.push_back(desc);
c@75 238
c@35 239 return list;
c@35 240 }
c@35 241
c@35 242 float
c@35 243 CQVamp::getParameter(std::string param) const
c@35 244 {
c@109 245 if (param == "minpitch" && m_midiPitchParameters) {
c@55 246 return m_minMIDIPitch;
c@55 247 }
c@109 248 if (param == "maxpitch" && m_midiPitchParameters) {
c@55 249 return m_maxMIDIPitch;
c@55 250 }
c@109 251 if (param == "tuning" && m_midiPitchParameters) {
c@55 252 return m_tuningFrequency;
c@55 253 }
c@35 254 if (param == "bpo") {
c@35 255 return m_bpo;
c@35 256 }
c@75 257 if (param == "interpolation") {
c@75 258 return (float)m_interpolation;
c@75 259 }
c@109 260 if (param == "minfreq" && !m_midiPitchParameters) {
c@109 261 return m_minFrequency;
c@109 262 }
c@109 263 if (param == "maxfreq" && !m_midiPitchParameters) {
c@109 264 return m_maxFrequency;
c@109 265 }
c@190 266 if (param == "atomoverlap") {
c@190 267 return m_atomOverlap;
c@190 268 }
c@190 269 if (param == "draftdecimator") {
c@190 270 return m_useDraftDecimator ? 1.f : 0.f;
c@190 271 }
c@35 272 std::cerr << "WARNING: CQVamp::getParameter: unknown parameter \""
c@35 273 << param << "\"" << std::endl;
c@35 274 return 0.0;
c@35 275 }
c@35 276
c@35 277 void
c@35 278 CQVamp::setParameter(std::string param, float value)
c@35 279 {
c@109 280 if (param == "minpitch" && m_midiPitchParameters) {
c@164 281 m_minMIDIPitch = int(value + 0.5f);
c@109 282 } else if (param == "maxpitch" && m_midiPitchParameters) {
c@164 283 m_maxMIDIPitch = int(value + 0.5f);
c@109 284 } else if (param == "tuning" && m_midiPitchParameters) {
c@55 285 m_tuningFrequency = value;
c@75 286 } else if (param == "bpo") {
c@164 287 m_bpo = int(value + 0.5f);
c@75 288 } else if (param == "interpolation") {
c@164 289 m_interpolation = (CQSpectrogram::Interpolation)int(value + 0.5f);
c@109 290 } else if (param == "minfreq" && !m_midiPitchParameters) {
c@109 291 m_minFrequency = value;
c@109 292 } else if (param == "maxfreq" && !m_midiPitchParameters) {
c@109 293 m_maxFrequency = value;
c@190 294 } else if (param == "atomoverlap") {
c@190 295 m_atomOverlap = int(value + 0.5f);
c@190 296 } else if (param == "draftdecimator") {
c@190 297 m_useDraftDecimator = (value > 0.5f);
c@35 298 } else {
c@35 299 std::cerr << "WARNING: CQVamp::setParameter: unknown parameter \""
c@35 300 << param << "\"" << std::endl;
c@35 301 }
c@35 302 }
c@35 303
c@35 304 bool
c@35 305 CQVamp::initialise(size_t channels, size_t stepSize, size_t blockSize)
c@35 306 {
c@35 307 if (m_cq) {
c@35 308 delete m_cq;
c@75 309 m_cq = 0;
c@35 310 }
c@35 311
c@35 312 if (channels < getMinChannelCount() ||
c@35 313 channels > getMaxChannelCount()) return false;
c@35 314
c@35 315 m_stepSize = stepSize;
c@35 316 m_blockSize = blockSize;
c@35 317
c@109 318 if (m_midiPitchParameters) {
c@109 319 m_minFrequency = Pitch::getFrequencyForPitch
c@109 320 (m_minMIDIPitch, 0, m_tuningFrequency);
c@109 321 m_maxFrequency = Pitch::getFrequencyForPitch
c@109 322 (m_maxMIDIPitch, 0, m_tuningFrequency);
c@109 323 }
c@55 324
c@147 325 reset();
c@147 326
c@147 327 if (!m_cq || !m_cq->isValid()) {
c@147 328 cerr << "CQVamp::initialise: Constant-Q parameters not valid! Not initialising" << endl;
c@147 329 return false;
c@147 330 }
c@35 331
c@35 332 return true;
c@35 333 }
c@35 334
c@35 335 void
c@35 336 CQVamp::reset()
c@35 337 {
c@147 338 delete m_cq;
c@147 339 CQParameters p(m_inputSampleRate, m_minFrequency, m_maxFrequency, m_bpo);
c@190 340 p.atomHopFactor = 1.0 / double(m_atomOverlap);
c@190 341 p.decimator = (m_useDraftDecimator ?
c@190 342 CQParameters::FasterDecimator :
c@190 343 CQParameters::BetterDecimator);
c@147 344 m_cq = new CQSpectrogram(p, m_interpolation);
c@53 345 m_haveStartTime = false;
c@53 346 m_columnCount = 0;
c@35 347 }
c@35 348
c@35 349 size_t
c@35 350 CQVamp::getPreferredStepSize() const
c@35 351 {
c@35 352 return 0;
c@35 353 }
c@35 354
c@35 355 size_t
c@35 356 CQVamp::getPreferredBlockSize() const
c@35 357 {
c@35 358 return 0;
c@35 359 }
c@35 360
c@109 361 std::string
c@109 362 CQVamp::noteName(int i) const
c@109 363 {
c@109 364 static const char *names[] = {
c@109 365 "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
c@109 366 };
c@109 367
c@109 368 const char *n = names[i % 12];
c@109 369 int oct = i / 12 - 1;
c@109 370 char buf[20];
c@110 371 sprintf(buf, "%d %s%d", i, n, oct);
c@109 372
c@109 373 return buf;
c@109 374 }
c@109 375
c@35 376 CQVamp::OutputList
c@35 377 CQVamp::getOutputDescriptors() const
c@35 378 {
c@35 379 OutputList list;
c@35 380
c@35 381 OutputDescriptor d;
c@35 382 d.identifier = "constantq";
c@35 383 d.name = "Constant-Q Spectrogram";
c@35 384 d.unit = "";
c@35 385 d.description = "Output of constant-Q transform, as a single vector per process block";
c@35 386 d.hasFixedBinCount = true;
c@35 387 d.binCount = (m_cq ? m_cq->getTotalBins() : (9 * 24));
c@58 388
c@58 389 if (m_cq) {
c@58 390 char name[20];
c@94 391 for (int i = 0; i < (int)d.binCount; ++i) {
c@168 392 float freq = m_cq->getBinFrequency(d.binCount - i - 1);
c@58 393 sprintf(name, "%.1f Hz", freq);
c@110 394 int note = Pitch::getPitchForFrequency(freq, 0, m_tuningFrequency);
c@110 395 float nearestFreq =
c@110 396 Pitch::getFrequencyForPitch(note, 0, m_tuningFrequency);
c@110 397 if (fabs(freq - nearestFreq) < 0.01) {
c@110 398 d.binNames.push_back(name + std::string(" ") + noteName(note));
c@109 399 } else {
c@109 400 d.binNames.push_back(name);
c@109 401 }
c@58 402 }
c@58 403 }
c@58 404
c@35 405 d.hasKnownExtents = false;
c@35 406 d.isQuantized = false;
c@35 407 d.sampleType = OutputDescriptor::FixedSampleRate;
c@35 408 d.sampleRate = m_inputSampleRate / (m_cq ? m_cq->getColumnHop() : 256);
c@35 409 list.push_back(d);
c@35 410
c@35 411 return list;
c@35 412 }
c@35 413
c@35 414 CQVamp::FeatureSet
c@35 415 CQVamp::process(const float *const *inputBuffers,
c@53 416 Vamp::RealTime timestamp)
c@35 417 {
c@35 418 if (!m_cq) {
c@35 419 cerr << "ERROR: CQVamp::process: "
c@35 420 << "Plugin has not been initialised"
c@35 421 << endl;
c@35 422 return FeatureSet();
c@35 423 }
c@35 424
c@53 425 if (!m_haveStartTime) {
c@53 426 m_startTime = timestamp;
c@53 427 m_haveStartTime = true;
c@53 428 }
c@53 429
c@35 430 vector<double> data;
c@35 431 for (int i = 0; i < m_blockSize; ++i) data.push_back(inputBuffers[0][i]);
c@35 432
c@35 433 vector<vector<double> > cqout = m_cq->process(data);
c@36 434 return convertToFeatures(cqout);
c@36 435 }
c@35 436
c@36 437 CQVamp::FeatureSet
c@36 438 CQVamp::getRemainingFeatures()
c@36 439 {
c@90 440 vector<vector<double> > cqout = m_cq->getRemainingOutput();
c@36 441 return convertToFeatures(cqout);
c@36 442 }
c@36 443
c@36 444 CQVamp::FeatureSet
c@36 445 CQVamp::convertToFeatures(const vector<vector<double> > &cqout)
c@36 446 {
c@35 447 FeatureSet returnFeatures;
c@35 448
c@75 449 int width = cqout.size();
c@75 450 int height = m_cq->getTotalBins();
c@35 451
c@75 452 for (int i = 0; i < width; ++i) {
c@36 453
c@75 454 vector<float> column(height, 0.f);
c@75 455 int thisHeight = cqout[i].size();
c@75 456 for (int j = 0; j < thisHeight; ++j) {
c@35 457 column[j] = cqout[i][j];
c@35 458 }
c@36 459
c@58 460 // put low frequencies at the start
c@58 461 std::reverse(column.begin(), column.end());
c@58 462
c@35 463 Feature feature;
c@53 464 feature.hasTimestamp = true;
c@53 465 feature.timestamp = m_startTime + Vamp::RealTime::frame2RealTime
c@53 466 (m_columnCount * m_cq->getColumnHop() - m_cq->getLatency(),
c@53 467 m_inputSampleRate);
c@35 468 feature.values = column;
c@35 469 feature.label = "";
c@53 470
c@56 471 // cerr << "timestamp = " << feature.timestamp << " (start time = " << m_startTime << ", column count = " << m_columnCount << ", latency = " << m_cq->getLatency() << ", sample rate " << m_inputSampleRate << ")" << endl;
c@53 472
c@53 473 if (feature.timestamp >= m_startTime) {
c@53 474 returnFeatures[0].push_back(feature);
c@53 475 }
c@53 476
c@53 477 ++m_columnCount;
c@35 478 }
c@35 479
c@35 480 return returnFeatures;
c@35 481 }
c@35 482