annotate vamp/CQVamp.cpp @ 109:f5325762ff6d

Make CQVamp implement two plugins, one with MIDI pitch range and one with Hz
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 14 May 2014 12:49:10 +0100
parents fa1709ed4a3c
children fdd32f995b0d
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@56 34 #include "base/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@109 53 static const float defaultTuningFrequency = 440.f;
c@109 54
c@109 55 CQVamp::CQVamp(float inputSampleRate, bool midiPitchParameters) :
c@35 56 Vamp::Plugin(inputSampleRate),
c@109 57 m_midiPitchParameters(midiPitchParameters),
c@109 58 m_minMIDIPitch(defaultMinMIDIPitch),
c@109 59 m_maxMIDIPitch(defaultMaxMIDIPitch),
c@109 60 m_tuningFrequency(defaultTuningFrequency),
c@109 61 m_bpo(defaultBPO),
c@90 62 m_interpolation(CQSpectrogram::InterpolateLinear),
c@35 63 m_cq(0),
c@109 64 m_maxFrequency(14080),
c@109 65 m_minFrequency(100),
c@53 66 m_haveStartTime(false),
c@53 67 m_columnCount(0)
c@35 68 {
c@35 69 }
c@35 70
c@35 71 CQVamp::~CQVamp()
c@35 72 {
c@35 73 delete m_cq;
c@35 74 }
c@35 75
c@35 76 string
c@35 77 CQVamp::getIdentifier() const
c@35 78 {
c@109 79 if (m_midiPitchParameters) {
c@109 80 return "cqvampmidi";
c@109 81 } else {
c@109 82 return "cqvamp";
c@109 83 }
c@35 84 }
c@35 85
c@35 86 string
c@35 87 CQVamp::getName() const
c@35 88 {
c@109 89 if (m_midiPitchParameters) {
c@109 90 return "Constant-Q Spectrogram (MIDI pitch range)";
c@109 91 } else {
c@109 92 return "Constant-Q Spectrogram (Hz range)";
c@109 93 }
c@35 94 }
c@35 95
c@35 96 string
c@35 97 CQVamp::getDescription() const
c@35 98 {
c@109 99 if (m_midiPitchParameters) {
c@109 100 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 101 } else {
c@109 102 return "Extract a spectrogram with constant ratio of centre frequency to resolution from the input audio, specifying the frequency range in Hz.";
c@109 103 }
c@35 104 }
c@35 105
c@35 106 string
c@35 107 CQVamp::getMaker() const
c@35 108 {
c@35 109 return "Queen Mary, University of London";
c@35 110 }
c@35 111
c@35 112 int
c@35 113 CQVamp::getPluginVersion() const
c@35 114 {
c@35 115 return 1;
c@35 116 }
c@35 117
c@35 118 string
c@35 119 CQVamp::getCopyright() const
c@35 120 {
c@35 121 return "Plugin by Chris Cannam. Method by Christian Schörkhuber and Anssi Klapuri. Copyright (c) 2013 QMUL";
c@35 122 }
c@35 123
c@35 124 CQVamp::ParameterList
c@35 125 CQVamp::getParameterDescriptors() const
c@35 126 {
c@35 127 ParameterList list;
c@35 128
c@55 129 ParameterDescriptor desc;
c@55 130
c@109 131 if (m_midiPitchParameters) {
c@55 132
c@109 133 desc.identifier = "minpitch";
c@109 134 desc.name = "Minimum Pitch";
c@109 135 desc.unit = "MIDI units";
c@109 136 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 137 desc.minValue = 0;
c@109 138 desc.maxValue = 127;
c@109 139 desc.defaultValue = 36;
c@109 140 desc.isQuantized = true;
c@109 141 desc.quantizeStep = 1;
c@109 142 list.push_back(desc);
c@109 143
c@109 144 desc.identifier = "maxpitch";
c@109 145 desc.name = "Maximum Pitch";
c@109 146 desc.unit = "MIDI units";
c@109 147 desc.description = "MIDI pitch corresponding to the highest frequency to be included in the constant-Q transform";
c@109 148 desc.minValue = 0;
c@109 149 desc.maxValue = 127;
c@109 150 desc.defaultValue = 84;
c@109 151 desc.isQuantized = true;
c@109 152 desc.quantizeStep = 1;
c@109 153 list.push_back(desc);
c@109 154
c@109 155 desc.identifier = "tuning";
c@109 156 desc.name = "Tuning Frequency";
c@109 157 desc.unit = "Hz";
c@109 158 desc.description = "Frequency of concert A";
c@109 159 desc.minValue = 360;
c@109 160 desc.maxValue = 500;
c@109 161 desc.defaultValue = 440;
c@109 162 desc.isQuantized = false;
c@109 163 list.push_back(desc);
c@109 164
c@109 165 } else {
c@109 166
c@109 167 desc.identifier = "minfreq";
c@109 168 desc.name = "Minimum Frequency";
c@109 169 desc.unit = "Hz";
c@109 170 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 171 desc.minValue = 1;
c@109 172 desc.maxValue = m_inputSampleRate / 2;
c@109 173 desc.defaultValue = 100;
c@109 174 desc.isQuantized = false;
c@109 175 list.push_back(desc);
c@109 176
c@109 177 desc.identifier = "maxfreq";
c@109 178 desc.name = "Maximum Frequency";
c@109 179 desc.unit = "Hz";
c@109 180 desc.description = "MIDI pitch corresponding to the highest frequency to be included in the constant-Q transform";
c@109 181 desc.minValue = 1;
c@109 182 desc.maxValue = m_inputSampleRate / 2;
c@109 183 desc.defaultValue = 14080;
c@109 184 desc.isQuantized = false;
c@109 185 list.push_back(desc);
c@109 186 }
c@35 187
c@35 188 desc.identifier = "bpo";
c@35 189 desc.name = "Bins per Octave";
c@35 190 desc.unit = "bins";
c@35 191 desc.description = "Number of constant-Q transform bins per octave";
c@35 192 desc.minValue = 2;
c@35 193 desc.maxValue = 480;
c@109 194 desc.defaultValue = 60;
c@35 195 desc.isQuantized = true;
c@35 196 desc.quantizeStep = 1;
c@35 197 list.push_back(desc);
c@35 198
c@75 199 desc.identifier = "interpolation";
c@75 200 desc.name = "Interpolation";
c@75 201 desc.unit = "";
c@75 202 desc.description = "Interpolation method used to fill empty cells in lower octaves";
c@75 203 desc.minValue = 0;
c@75 204 desc.maxValue = 2;
c@75 205 desc.defaultValue = 2;
c@75 206 desc.isQuantized = true;
c@75 207 desc.quantizeStep = 1;
c@90 208 desc.valueNames.push_back("None, leave as zero");
c@75 209 desc.valueNames.push_back("None, repeat prior value");
c@75 210 desc.valueNames.push_back("Linear interpolation");
c@75 211 list.push_back(desc);
c@75 212
c@35 213 return list;
c@35 214 }
c@35 215
c@35 216 float
c@35 217 CQVamp::getParameter(std::string param) const
c@35 218 {
c@109 219 if (param == "minpitch" && m_midiPitchParameters) {
c@55 220 return m_minMIDIPitch;
c@55 221 }
c@109 222 if (param == "maxpitch" && m_midiPitchParameters) {
c@55 223 return m_maxMIDIPitch;
c@55 224 }
c@109 225 if (param == "tuning" && m_midiPitchParameters) {
c@55 226 return m_tuningFrequency;
c@55 227 }
c@35 228 if (param == "bpo") {
c@35 229 return m_bpo;
c@35 230 }
c@75 231 if (param == "interpolation") {
c@75 232 return (float)m_interpolation;
c@75 233 }
c@109 234 if (param == "minfreq" && !m_midiPitchParameters) {
c@109 235 return m_minFrequency;
c@109 236 }
c@109 237 if (param == "maxfreq" && !m_midiPitchParameters) {
c@109 238 return m_maxFrequency;
c@109 239 }
c@35 240 std::cerr << "WARNING: CQVamp::getParameter: unknown parameter \""
c@35 241 << param << "\"" << std::endl;
c@35 242 return 0.0;
c@35 243 }
c@35 244
c@35 245 void
c@35 246 CQVamp::setParameter(std::string param, float value)
c@35 247 {
c@109 248 if (param == "minpitch" && m_midiPitchParameters) {
c@55 249 m_minMIDIPitch = lrintf(value);
c@109 250 } else if (param == "maxpitch" && m_midiPitchParameters) {
c@55 251 m_maxMIDIPitch = lrintf(value);
c@109 252 } else if (param == "tuning" && m_midiPitchParameters) {
c@55 253 m_tuningFrequency = value;
c@75 254 } else if (param == "bpo") {
c@35 255 m_bpo = lrintf(value);
c@75 256 } else if (param == "interpolation") {
c@90 257 m_interpolation = (CQSpectrogram::Interpolation)lrintf(value);
c@109 258 } else if (param == "minfreq" && !m_midiPitchParameters) {
c@109 259 m_minFrequency = value;
c@109 260 } else if (param == "maxfreq" && !m_midiPitchParameters) {
c@109 261 m_maxFrequency = value;
c@35 262 } else {
c@35 263 std::cerr << "WARNING: CQVamp::setParameter: unknown parameter \""
c@35 264 << param << "\"" << std::endl;
c@35 265 }
c@35 266 }
c@35 267
c@35 268 bool
c@35 269 CQVamp::initialise(size_t channels, size_t stepSize, size_t blockSize)
c@35 270 {
c@35 271 if (m_cq) {
c@35 272 delete m_cq;
c@75 273 m_cq = 0;
c@35 274 }
c@35 275
c@35 276 if (channels < getMinChannelCount() ||
c@35 277 channels > getMaxChannelCount()) return false;
c@35 278
c@35 279 m_stepSize = stepSize;
c@35 280 m_blockSize = blockSize;
c@35 281
c@109 282 if (m_midiPitchParameters) {
c@109 283 m_minFrequency = Pitch::getFrequencyForPitch
c@109 284 (m_minMIDIPitch, 0, m_tuningFrequency);
c@109 285 m_maxFrequency = Pitch::getFrequencyForPitch
c@109 286 (m_maxMIDIPitch, 0, m_tuningFrequency);
c@109 287 }
c@55 288
c@90 289 m_cq = new CQSpectrogram
c@75 290 (m_inputSampleRate, m_minFrequency, m_maxFrequency, m_bpo,
c@75 291 m_interpolation);
c@35 292
c@35 293 return true;
c@35 294 }
c@35 295
c@35 296 void
c@35 297 CQVamp::reset()
c@35 298 {
c@35 299 if (m_cq) {
c@35 300 delete m_cq;
c@90 301 m_cq = new CQSpectrogram
c@75 302 (m_inputSampleRate, m_minFrequency, m_maxFrequency, m_bpo,
c@75 303 m_interpolation);
c@35 304 }
c@36 305 m_prevFeature.clear();
c@53 306 m_haveStartTime = false;
c@53 307 m_columnCount = 0;
c@35 308 }
c@35 309
c@35 310 size_t
c@35 311 CQVamp::getPreferredStepSize() const
c@35 312 {
c@35 313 return 0;
c@35 314 }
c@35 315
c@35 316 size_t
c@35 317 CQVamp::getPreferredBlockSize() const
c@35 318 {
c@35 319 return 0;
c@35 320 }
c@35 321
c@109 322 std::string
c@109 323 CQVamp::noteName(int i) const
c@109 324 {
c@109 325 static const char *names[] = {
c@109 326 "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
c@109 327 };
c@109 328
c@109 329 const char *n = names[i % 12];
c@109 330 int oct = i / 12 - 1;
c@109 331 char buf[20];
c@109 332 sprintf(buf, "%s%d", n, oct);
c@109 333
c@109 334 return buf;
c@109 335 }
c@109 336
c@109 337 float
c@109 338 CQVamp::noteFrequency(int note) const
c@109 339 {
c@109 340 return m_tuningFrequency * pow(2.0, (note - 69) / 12.0);
c@109 341 }
c@109 342
c@109 343 int
c@109 344 CQVamp::noteNumber(float freq) const
c@109 345 {
c@109 346 return int(round(57.0 + (12.0 * log(freq / (m_tuningFrequency / 2.0)) / log(2.0))));
c@109 347 }
c@109 348
c@35 349 CQVamp::OutputList
c@35 350 CQVamp::getOutputDescriptors() const
c@35 351 {
c@35 352 OutputList list;
c@35 353
c@35 354 OutputDescriptor d;
c@35 355 d.identifier = "constantq";
c@35 356 d.name = "Constant-Q Spectrogram";
c@35 357 d.unit = "";
c@35 358 d.description = "Output of constant-Q transform, as a single vector per process block";
c@35 359 d.hasFixedBinCount = true;
c@35 360 d.binCount = (m_cq ? m_cq->getTotalBins() : (9 * 24));
c@58 361
c@58 362 if (m_cq) {
c@58 363 char name[20];
c@94 364 for (int i = 0; i < (int)d.binCount; ++i) {
c@58 365 float freq = m_cq->getBinFrequency(i);
c@58 366 sprintf(name, "%.1f Hz", freq);
c@109 367 if (fabs(noteFrequency(noteNumber(freq)) - freq) < 0.01) {
c@109 368 d.binNames.push_back(name + std::string(": ") +
c@109 369 noteName(noteNumber(freq)));
c@109 370 } else {
c@109 371 d.binNames.push_back(name);
c@109 372 }
c@58 373 }
c@58 374 }
c@58 375
c@35 376 d.hasKnownExtents = false;
c@35 377 d.isQuantized = false;
c@35 378 d.sampleType = OutputDescriptor::FixedSampleRate;
c@35 379 d.sampleRate = m_inputSampleRate / (m_cq ? m_cq->getColumnHop() : 256);
c@35 380 list.push_back(d);
c@35 381
c@35 382 return list;
c@35 383 }
c@35 384
c@35 385 CQVamp::FeatureSet
c@35 386 CQVamp::process(const float *const *inputBuffers,
c@53 387 Vamp::RealTime timestamp)
c@35 388 {
c@35 389 if (!m_cq) {
c@35 390 cerr << "ERROR: CQVamp::process: "
c@35 391 << "Plugin has not been initialised"
c@35 392 << endl;
c@35 393 return FeatureSet();
c@35 394 }
c@35 395
c@53 396 if (!m_haveStartTime) {
c@53 397 m_startTime = timestamp;
c@53 398 m_haveStartTime = true;
c@53 399 }
c@53 400
c@35 401 vector<double> data;
c@35 402 for (int i = 0; i < m_blockSize; ++i) data.push_back(inputBuffers[0][i]);
c@35 403
c@35 404 vector<vector<double> > cqout = m_cq->process(data);
c@36 405 return convertToFeatures(cqout);
c@36 406 }
c@35 407
c@36 408 CQVamp::FeatureSet
c@36 409 CQVamp::getRemainingFeatures()
c@36 410 {
c@90 411 vector<vector<double> > cqout = m_cq->getRemainingOutput();
c@36 412 return convertToFeatures(cqout);
c@36 413 }
c@36 414
c@36 415 CQVamp::FeatureSet
c@36 416 CQVamp::convertToFeatures(const vector<vector<double> > &cqout)
c@36 417 {
c@35 418 FeatureSet returnFeatures;
c@35 419
c@75 420 int width = cqout.size();
c@75 421 int height = m_cq->getTotalBins();
c@35 422
c@75 423 for (int i = 0; i < width; ++i) {
c@36 424
c@75 425 vector<float> column(height, 0.f);
c@75 426 int thisHeight = cqout[i].size();
c@75 427 for (int j = 0; j < thisHeight; ++j) {
c@35 428 column[j] = cqout[i][j];
c@35 429 }
c@36 430
c@58 431 // put low frequencies at the start
c@58 432 std::reverse(column.begin(), column.end());
c@58 433
c@36 434 m_prevFeature = column;
c@35 435
c@35 436 Feature feature;
c@53 437 feature.hasTimestamp = true;
c@53 438 feature.timestamp = m_startTime + Vamp::RealTime::frame2RealTime
c@53 439 (m_columnCount * m_cq->getColumnHop() - m_cq->getLatency(),
c@53 440 m_inputSampleRate);
c@35 441 feature.values = column;
c@35 442 feature.label = "";
c@53 443
c@56 444 // cerr << "timestamp = " << feature.timestamp << " (start time = " << m_startTime << ", column count = " << m_columnCount << ", latency = " << m_cq->getLatency() << ", sample rate " << m_inputSampleRate << ")" << endl;
c@53 445
c@53 446 if (feature.timestamp >= m_startTime) {
c@53 447 returnFeatures[0].push_back(feature);
c@53 448 }
c@53 449
c@53 450 ++m_columnCount;
c@35 451 }
c@35 452
c@35 453 return returnFeatures;
c@35 454 }
c@35 455