annotate vamp/CQVamp.cpp @ 154:768c690975ea

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