annotate plugins/Pitch.cpp @ 65:3f405eec97ae

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