annotate plugins/Pitch.cpp @ 23:1a7563c56d88

* Add frequency range limits, frequency-wrap option, and silence threshold to pitch detector plugin
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 24 Oct 2008 14:51:44 +0000
parents 1169d00391d8
children 2e979622bd93
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),
piem@2 35 m_pitchtype(aubio_pitch_yinfft),
cannam@23 36 m_pitchmode(aubio_pitchm_freq),
cannam@23 37 m_minfreq(getFrequencyForMIDIPitch(32)),
cannam@23 38 m_maxfreq(getFrequencyForMIDIPitch(95)),
cannam@23 39 m_silence(-90),
cannam@23 40 m_wrapRange(false),
cannam@23 41 m_stepSize(0),
cannam@23 42 m_blockSize(0),
cannam@23 43 m_channelCount(0)
cannam@0 44 {
cannam@0 45 }
cannam@0 46
cannam@0 47 Pitch::~Pitch()
cannam@0 48 {
cannam@0 49 if (m_pitchdet) del_aubio_pitchdetection(m_pitchdet);
cannam@0 50 if (m_ibuf) del_fvec(m_ibuf);
cannam@0 51 }
cannam@0 52
cannam@0 53 string
cannam@13 54 Pitch::getIdentifier() const
cannam@0 55 {
cannam@0 56 return "aubiopitch";
cannam@0 57 }
cannam@0 58
cannam@0 59 string
cannam@13 60 Pitch::getName() const
cannam@13 61 {
cannam@13 62 return "Aubio Pitch Detector";
cannam@13 63 }
cannam@13 64
cannam@13 65 string
cannam@0 66 Pitch::getDescription() const
cannam@0 67 {
cannam@13 68 return "Track estimated note pitches";
cannam@0 69 }
cannam@0 70
cannam@0 71 string
cannam@0 72 Pitch::getMaker() const
cannam@0 73 {
cannam@0 74 return "Paul Brossier (plugin by Chris Cannam)";
cannam@0 75 }
cannam@0 76
cannam@0 77 int
cannam@0 78 Pitch::getPluginVersion() const
cannam@0 79 {
cannam@23 80 return 2;
cannam@0 81 }
cannam@0 82
cannam@0 83 string
cannam@0 84 Pitch::getCopyright() const
cannam@0 85 {
cannam@0 86 return "GPL";
cannam@0 87 }
cannam@0 88
cannam@0 89 bool
cannam@0 90 Pitch::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@0 91 {
cannam@0 92 m_channelCount = channels;
cannam@0 93 m_stepSize = stepSize;
cannam@0 94 m_blockSize = blockSize;
cannam@0 95
cannam@0 96 m_ibuf = new_fvec(stepSize, channels);
cannam@0 97
piem@2 98 m_pitchdet = new_aubio_pitchdetection(blockSize,
cannam@0 99 stepSize,
cannam@0 100 channels,
cannam@0 101 lrintf(m_inputSampleRate),
cannam@0 102 m_pitchtype,
cannam@0 103 m_pitchmode);
cannam@0 104
cannam@0 105 return true;
cannam@0 106 }
cannam@0 107
cannam@0 108 void
cannam@0 109 Pitch::reset()
cannam@0 110 {
cannam@0 111 }
cannam@0 112
cannam@0 113 size_t
cannam@0 114 Pitch::getPreferredStepSize() const
cannam@0 115 {
cannam@0 116 return 512;
cannam@0 117 }
cannam@0 118
cannam@0 119 size_t
cannam@0 120 Pitch::getPreferredBlockSize() const
cannam@0 121 {
piem@2 122 return 2048;
cannam@0 123 }
cannam@0 124
cannam@0 125 Pitch::ParameterList
cannam@0 126 Pitch::getParameterDescriptors() const
cannam@0 127 {
cannam@0 128 ParameterList list;
cannam@0 129
cannam@0 130 ParameterDescriptor desc;
cannam@13 131 desc.identifier = "pitchtype";
cannam@13 132 desc.name = "Pitch Detection Function Type";
cannam@0 133 desc.minValue = 0;
cannam@0 134 desc.maxValue = 4;
piem@2 135 desc.defaultValue = (int)aubio_pitch_yinfft;
cannam@0 136 desc.isQuantized = true;
cannam@0 137 desc.quantizeStep = 1;
cannam@0 138 desc.valueNames.push_back("YIN Frequency Estimator");
cannam@0 139 desc.valueNames.push_back("Spectral Comb");
cannam@0 140 desc.valueNames.push_back("Schmitt");
cannam@0 141 desc.valueNames.push_back("Fast Harmonic Comb");
cannam@0 142 desc.valueNames.push_back("YIN with FFT");
cannam@0 143 list.push_back(desc);
cannam@0 144
cannam@23 145 desc = ParameterDescriptor();
cannam@23 146 desc.identifier = "minfreq";
cannam@23 147 desc.name = "Minimum Fundamental Frequency";
cannam@23 148 desc.minValue = 1;
cannam@23 149 desc.maxValue = m_inputSampleRate/2;
cannam@23 150 desc.defaultValue = getFrequencyForMIDIPitch(32);
cannam@23 151 desc.unit = "Hz";
cannam@23 152 desc.isQuantized = false;
cannam@23 153 list.push_back(desc);
cannam@23 154
cannam@23 155 desc = ParameterDescriptor();
cannam@23 156 desc.identifier = "maxfreq";
cannam@23 157 desc.name = "Maximum Fundamental Frequency";
cannam@23 158 desc.minValue = 1;
cannam@23 159 desc.maxValue = m_inputSampleRate/2;
cannam@23 160 desc.defaultValue = getFrequencyForMIDIPitch(95);
cannam@23 161 desc.unit = "Hz";
cannam@23 162 desc.isQuantized = false;
cannam@23 163 list.push_back(desc);
cannam@23 164
cannam@23 165 desc = ParameterDescriptor();
cannam@23 166 desc.identifier = "wraprange";
cannam@23 167 desc.name = "Fold Higher or Lower Frequencies into Range";
cannam@23 168 desc.minValue = 0;
cannam@23 169 desc.maxValue = 1;
cannam@23 170 desc.defaultValue = 0;
cannam@23 171 desc.isQuantized = true;
cannam@23 172 desc.quantizeStep = 1;
cannam@23 173 list.push_back(desc);
cannam@23 174
cannam@23 175 desc = ParameterDescriptor();
cannam@23 176 desc.identifier = "silencethreshold";
cannam@23 177 desc.name = "Silence Threshold";
cannam@23 178 desc.minValue = -120;
cannam@23 179 desc.maxValue = 0;
cannam@23 180 desc.defaultValue = -90;
cannam@23 181 desc.unit = "dB";
cannam@23 182 desc.isQuantized = false;
cannam@23 183 list.push_back(desc);
cannam@23 184
cannam@0 185 return list;
cannam@0 186 }
cannam@0 187
cannam@0 188 float
cannam@0 189 Pitch::getParameter(std::string param) const
cannam@0 190 {
cannam@0 191 if (param == "pitchtype") {
cannam@0 192 return m_pitchtype;
cannam@23 193 } else if (param == "minfreq") {
cannam@23 194 return m_minfreq;
cannam@23 195 } else if (param == "maxfreq") {
cannam@23 196 return m_maxfreq;
cannam@23 197 } else if (param == "wraprange") {
cannam@23 198 return m_wrapRange ? 1.0 : 0.0;
cannam@23 199 } else if (param == "silencethreshold") {
cannam@23 200 return m_silence;
cannam@0 201 } else {
cannam@0 202 return 0.0;
cannam@0 203 }
cannam@0 204 }
cannam@0 205
cannam@0 206 void
cannam@0 207 Pitch::setParameter(std::string param, float value)
cannam@0 208 {
cannam@0 209 if (param == "pitchtype") {
cannam@0 210 switch (lrintf(value)) {
cannam@0 211 case 0: m_pitchtype = aubio_pitch_yin; break;
cannam@0 212 case 1: m_pitchtype = aubio_pitch_mcomb; break;
cannam@0 213 case 2: m_pitchtype = aubio_pitch_schmitt; break;
cannam@0 214 case 3: m_pitchtype = aubio_pitch_fcomb; break;
cannam@0 215 case 4: m_pitchtype = aubio_pitch_yinfft; break;
cannam@0 216 }
cannam@23 217 } else if (param == "minfreq") {
cannam@23 218 m_minfreq = value;
cannam@23 219 } else if (param == "maxfreq") {
cannam@23 220 m_maxfreq = value;
cannam@23 221 } else if (param == "wraprange") {
cannam@23 222 m_wrapRange = (value > 0.5);
cannam@23 223 } else if (param == "silencethreshold") {
cannam@23 224 m_silence = value;
cannam@0 225 }
cannam@0 226 }
cannam@0 227
cannam@0 228 Pitch::OutputList
cannam@0 229 Pitch::getOutputDescriptors() const
cannam@0 230 {
cannam@0 231 OutputList list;
cannam@0 232
cannam@0 233 OutputDescriptor d;
cannam@13 234 d.identifier = "frequency";
cannam@23 235 d.name = "Fundamental Frequency";
cannam@0 236 d.unit = "Hz";
cannam@0 237 d.hasFixedBinCount = true;
cannam@0 238 d.binCount = 1;
cannam@0 239 d.hasKnownExtents = false;
cannam@0 240 d.isQuantized = false;
cannam@23 241 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@23 242 d.sampleRate = 0;
cannam@23 243 if (m_stepSize != 0) {
cannam@23 244 d.sampleRate = m_inputSampleRate / m_stepSize;
cannam@23 245 }
cannam@0 246 list.push_back(d);
cannam@0 247
cannam@0 248 return list;
cannam@0 249 }
cannam@0 250
cannam@0 251 Pitch::FeatureSet
cannam@12 252 Pitch::process(const float *const *inputBuffers,
cannam@23 253 Vamp::RealTime timestamp)
cannam@0 254 {
cannam@23 255 FeatureSet returnFeatures;
cannam@23 256
cannam@23 257 if (m_stepSize == 0) {
cannam@23 258 std::cerr << "Pitch::process: Pitch plugin not initialised" << std::endl;
cannam@23 259 return returnFeatures;
cannam@23 260 }
cannam@23 261
cannam@0 262 for (size_t i = 0; i < m_stepSize; ++i) {
cannam@0 263 for (size_t j = 0; j < m_channelCount; ++j) {
cannam@0 264 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
cannam@0 265 }
cannam@0 266 }
cannam@0 267
cannam@23 268 float freq = aubio_pitchdetection(m_pitchdet, m_ibuf);
cannam@23 269
cannam@23 270 bool silent = aubio_silence_detection(m_ibuf, m_silence);
cannam@23 271 if (silent) {
cannam@23 272 // std::cerr << "(silent)" << std::endl;
cannam@23 273 return returnFeatures;
cannam@23 274 }
cannam@23 275
cannam@23 276 if (m_wrapRange) {
cannam@23 277 while (freq > 0 && freq < m_minfreq) {
cannam@23 278 freq = freq * 2.0;
cannam@23 279 }
cannam@23 280 while (freq > m_maxfreq) {
cannam@23 281 freq = freq / 2.0;
cannam@23 282 }
cannam@23 283 }
cannam@23 284
cannam@23 285 if (freq < m_minfreq || freq > m_maxfreq) {
cannam@23 286 return returnFeatures;
cannam@23 287 }
cannam@0 288
cannam@0 289 Feature feature;
cannam@23 290 feature.hasTimestamp = true;
cannam@23 291 feature.timestamp = timestamp;
cannam@23 292 feature.values.push_back(freq);
cannam@0 293
cannam@0 294 returnFeatures[0].push_back(feature);
cannam@0 295 return returnFeatures;
cannam@0 296 }
cannam@0 297
cannam@0 298 Pitch::FeatureSet
cannam@0 299 Pitch::getRemainingFeatures()
cannam@0 300 {
cannam@0 301 return FeatureSet();
cannam@0 302 }
cannam@0 303