annotate plugins/Pitch.cpp @ 198:3a76aa26b578 tip master

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