annotate plugins/Pitch.cpp @ 31:2e979622bd93

Update plugin version numbers, remove API version back compatibility (API v1 is now so long out of date)
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 09 Jul 2012 15:44:07 +0100
parents 1a7563c56d88
children 8a20f3488d88
rev   line source
cannam@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@0 2
cannam@0 3 /*
cannam@0 4 Vamp feature extraction plugins using Paul Brossier's Aubio library.
cannam@0 5
cannam@0 6 Centre for Digital Music, Queen Mary, University of London.
cannam@0 7 This file copyright 2006 Chris Cannam.
cannam@0 8
cannam@0 9 This program is free software; you can redistribute it and/or
cannam@0 10 modify it under the terms of the GNU General Public License as
cannam@0 11 published by the Free Software Foundation; either version 2 of the
cannam@0 12 License, or (at your option) any later version. See the file
cannam@0 13 COPYING included with this distribution for more information.
cannam@0 14
cannam@0 15 */
cannam@0 16
piem@2 17 #include <math.h>
cannam@0 18 #include "Pitch.h"
cannam@0 19
cannam@0 20 using std::string;
cannam@0 21 using std::vector;
cannam@0 22 using std::cerr;
cannam@0 23 using std::endl;
cannam@0 24
cannam@23 25 static float
cannam@23 26 getFrequencyForMIDIPitch(int midiPitch)
cannam@23 27 {
cannam@23 28 return 440.f * powf(2.0, (float(midiPitch) - 69.0) / 12.0);
cannam@23 29 }
cannam@23 30
cannam@0 31 Pitch::Pitch(float inputSampleRate) :
cannam@0 32 Plugin(inputSampleRate),
cannam@0 33 m_ibuf(0),
cannam@0 34 m_pitchdet(0),
cannam@31 35 m_pitchtype(PitchYinFFT),
cannam@23 36 m_minfreq(getFrequencyForMIDIPitch(32)),
cannam@23 37 m_maxfreq(getFrequencyForMIDIPitch(95)),
cannam@23 38 m_silence(-90),
cannam@23 39 m_wrapRange(false),
cannam@23 40 m_stepSize(0),
cannam@23 41 m_blockSize(0),
cannam@23 42 m_channelCount(0)
cannam@0 43 {
cannam@0 44 }
cannam@0 45
cannam@0 46 Pitch::~Pitch()
cannam@0 47 {
cannam@31 48 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
cannam@0 49 if (m_ibuf) del_fvec(m_ibuf);
cannam@0 50 }
cannam@0 51
cannam@0 52 string
cannam@13 53 Pitch::getIdentifier() const
cannam@0 54 {
cannam@0 55 return "aubiopitch";
cannam@0 56 }
cannam@0 57
cannam@0 58 string
cannam@13 59 Pitch::getName() const
cannam@13 60 {
cannam@13 61 return "Aubio Pitch Detector";
cannam@13 62 }
cannam@13 63
cannam@13 64 string
cannam@0 65 Pitch::getDescription() const
cannam@0 66 {
cannam@13 67 return "Track estimated note pitches";
cannam@0 68 }
cannam@0 69
cannam@0 70 string
cannam@0 71 Pitch::getMaker() const
cannam@0 72 {
cannam@0 73 return "Paul Brossier (plugin by Chris Cannam)";
cannam@0 74 }
cannam@0 75
cannam@0 76 int
cannam@0 77 Pitch::getPluginVersion() const
cannam@0 78 {
cannam@31 79 return 3;
cannam@0 80 }
cannam@0 81
cannam@0 82 string
cannam@0 83 Pitch::getCopyright() const
cannam@0 84 {
cannam@0 85 return "GPL";
cannam@0 86 }
cannam@0 87
cannam@0 88 bool
cannam@0 89 Pitch::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@0 90 {
cannam@0 91 m_channelCount = channels;
cannam@0 92 m_stepSize = stepSize;
cannam@0 93 m_blockSize = blockSize;
cannam@0 94
cannam@0 95 m_ibuf = new_fvec(stepSize, channels);
cannam@0 96
piem@2 97 m_pitchdet = new_aubio_pitchdetection(blockSize,
cannam@0 98 stepSize,
cannam@0 99 channels,
cannam@0 100 lrintf(m_inputSampleRate),
cannam@0 101 m_pitchtype,
cannam@0 102 m_pitchmode);
cannam@0 103
cannam@0 104 return true;
cannam@0 105 }
cannam@0 106
cannam@0 107 void
cannam@0 108 Pitch::reset()
cannam@0 109 {
cannam@0 110 }
cannam@0 111
cannam@0 112 size_t
cannam@0 113 Pitch::getPreferredStepSize() const
cannam@0 114 {
cannam@0 115 return 512;
cannam@0 116 }
cannam@0 117
cannam@0 118 size_t
cannam@0 119 Pitch::getPreferredBlockSize() const
cannam@0 120 {
piem@2 121 return 2048;
cannam@0 122 }
cannam@0 123
cannam@0 124 Pitch::ParameterList
cannam@0 125 Pitch::getParameterDescriptors() const
cannam@0 126 {
cannam@0 127 ParameterList list;
cannam@0 128
cannam@0 129 ParameterDescriptor desc;
cannam@13 130 desc.identifier = "pitchtype";
cannam@13 131 desc.name = "Pitch Detection Function Type";
cannam@0 132 desc.minValue = 0;
cannam@0 133 desc.maxValue = 4;
piem@2 134 desc.defaultValue = (int)aubio_pitch_yinfft;
cannam@0 135 desc.isQuantized = true;
cannam@0 136 desc.quantizeStep = 1;
cannam@0 137 desc.valueNames.push_back("YIN Frequency Estimator");
cannam@0 138 desc.valueNames.push_back("Spectral Comb");
cannam@0 139 desc.valueNames.push_back("Schmitt");
cannam@0 140 desc.valueNames.push_back("Fast Harmonic Comb");
cannam@0 141 desc.valueNames.push_back("YIN with FFT");
cannam@0 142 list.push_back(desc);
cannam@0 143
cannam@23 144 desc = ParameterDescriptor();
cannam@23 145 desc.identifier = "minfreq";
cannam@23 146 desc.name = "Minimum Fundamental Frequency";
cannam@23 147 desc.minValue = 1;
cannam@23 148 desc.maxValue = m_inputSampleRate/2;
cannam@23 149 desc.defaultValue = getFrequencyForMIDIPitch(32);
cannam@23 150 desc.unit = "Hz";
cannam@23 151 desc.isQuantized = false;
cannam@23 152 list.push_back(desc);
cannam@23 153
cannam@23 154 desc = ParameterDescriptor();
cannam@23 155 desc.identifier = "maxfreq";
cannam@23 156 desc.name = "Maximum Fundamental Frequency";
cannam@23 157 desc.minValue = 1;
cannam@23 158 desc.maxValue = m_inputSampleRate/2;
cannam@23 159 desc.defaultValue = getFrequencyForMIDIPitch(95);
cannam@23 160 desc.unit = "Hz";
cannam@23 161 desc.isQuantized = false;
cannam@23 162 list.push_back(desc);
cannam@23 163
cannam@23 164 desc = ParameterDescriptor();
cannam@23 165 desc.identifier = "wraprange";
cannam@23 166 desc.name = "Fold Higher or Lower Frequencies into Range";
cannam@23 167 desc.minValue = 0;
cannam@23 168 desc.maxValue = 1;
cannam@23 169 desc.defaultValue = 0;
cannam@23 170 desc.isQuantized = true;
cannam@23 171 desc.quantizeStep = 1;
cannam@23 172 list.push_back(desc);
cannam@23 173
cannam@23 174 desc = ParameterDescriptor();
cannam@23 175 desc.identifier = "silencethreshold";
cannam@23 176 desc.name = "Silence Threshold";
cannam@23 177 desc.minValue = -120;
cannam@23 178 desc.maxValue = 0;
cannam@23 179 desc.defaultValue = -90;
cannam@23 180 desc.unit = "dB";
cannam@23 181 desc.isQuantized = false;
cannam@23 182 list.push_back(desc);
cannam@23 183
cannam@0 184 return list;
cannam@0 185 }
cannam@0 186
cannam@0 187 float
cannam@0 188 Pitch::getParameter(std::string param) const
cannam@0 189 {
cannam@0 190 if (param == "pitchtype") {
cannam@0 191 return m_pitchtype;
cannam@23 192 } else if (param == "minfreq") {
cannam@23 193 return m_minfreq;
cannam@23 194 } else if (param == "maxfreq") {
cannam@23 195 return m_maxfreq;
cannam@23 196 } else if (param == "wraprange") {
cannam@23 197 return m_wrapRange ? 1.0 : 0.0;
cannam@23 198 } else if (param == "silencethreshold") {
cannam@23 199 return m_silence;
cannam@0 200 } else {
cannam@0 201 return 0.0;
cannam@0 202 }
cannam@0 203 }
cannam@0 204
cannam@0 205 void
cannam@0 206 Pitch::setParameter(std::string param, float value)
cannam@0 207 {
cannam@0 208 if (param == "pitchtype") {
cannam@0 209 switch (lrintf(value)) {
cannam@0 210 case 0: m_pitchtype = aubio_pitch_yin; break;
cannam@0 211 case 1: m_pitchtype = aubio_pitch_mcomb; break;
cannam@0 212 case 2: m_pitchtype = aubio_pitch_schmitt; break;
cannam@0 213 case 3: m_pitchtype = aubio_pitch_fcomb; break;
cannam@0 214 case 4: m_pitchtype = aubio_pitch_yinfft; break;
cannam@0 215 }
cannam@23 216 } else if (param == "minfreq") {
cannam@23 217 m_minfreq = value;
cannam@23 218 } else if (param == "maxfreq") {
cannam@23 219 m_maxfreq = value;
cannam@23 220 } else if (param == "wraprange") {
cannam@23 221 m_wrapRange = (value > 0.5);
cannam@23 222 } else if (param == "silencethreshold") {
cannam@23 223 m_silence = value;
cannam@0 224 }
cannam@0 225 }
cannam@0 226
cannam@0 227 Pitch::OutputList
cannam@0 228 Pitch::getOutputDescriptors() const
cannam@0 229 {
cannam@0 230 OutputList list;
cannam@0 231
cannam@0 232 OutputDescriptor d;
cannam@13 233 d.identifier = "frequency";
cannam@23 234 d.name = "Fundamental Frequency";
cannam@0 235 d.unit = "Hz";
cannam@0 236 d.hasFixedBinCount = true;
cannam@0 237 d.binCount = 1;
cannam@0 238 d.hasKnownExtents = false;
cannam@0 239 d.isQuantized = false;
cannam@23 240 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@23 241 d.sampleRate = 0;
cannam@23 242 if (m_stepSize != 0) {
cannam@23 243 d.sampleRate = m_inputSampleRate / m_stepSize;
cannam@23 244 }
cannam@0 245 list.push_back(d);
cannam@0 246
cannam@0 247 return list;
cannam@0 248 }
cannam@0 249
cannam@0 250 Pitch::FeatureSet
cannam@12 251 Pitch::process(const float *const *inputBuffers,
cannam@23 252 Vamp::RealTime timestamp)
cannam@0 253 {
cannam@23 254 FeatureSet returnFeatures;
cannam@23 255
cannam@23 256 if (m_stepSize == 0) {
cannam@23 257 std::cerr << "Pitch::process: Pitch plugin not initialised" << std::endl;
cannam@23 258 return returnFeatures;
cannam@23 259 }
cannam@23 260
cannam@0 261 for (size_t i = 0; i < m_stepSize; ++i) {
cannam@0 262 for (size_t j = 0; j < m_channelCount; ++j) {
cannam@0 263 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
cannam@0 264 }
cannam@0 265 }
cannam@0 266
cannam@23 267 float freq = aubio_pitchdetection(m_pitchdet, m_ibuf);
cannam@23 268
cannam@23 269 bool silent = aubio_silence_detection(m_ibuf, m_silence);
cannam@23 270 if (silent) {
cannam@23 271 // std::cerr << "(silent)" << std::endl;
cannam@23 272 return returnFeatures;
cannam@23 273 }
cannam@23 274
cannam@23 275 if (m_wrapRange) {
cannam@23 276 while (freq > 0 && freq < m_minfreq) {
cannam@23 277 freq = freq * 2.0;
cannam@23 278 }
cannam@23 279 while (freq > m_maxfreq) {
cannam@23 280 freq = freq / 2.0;
cannam@23 281 }
cannam@23 282 }
cannam@23 283
cannam@23 284 if (freq < m_minfreq || freq > m_maxfreq) {
cannam@23 285 return returnFeatures;
cannam@23 286 }
cannam@0 287
cannam@0 288 Feature feature;
cannam@23 289 feature.hasTimestamp = true;
cannam@23 290 feature.timestamp = timestamp;
cannam@23 291 feature.values.push_back(freq);
cannam@0 292
cannam@0 293 returnFeatures[0].push_back(feature);
cannam@0 294 return returnFeatures;
cannam@0 295 }
cannam@0 296
cannam@0 297 Pitch::FeatureSet
cannam@0 298 Pitch::getRemainingFeatures()
cannam@0 299 {
cannam@0 300 return FeatureSet();
cannam@0 301 }
cannam@0 302