1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
4 Vamp feature extraction plugins using Paul Brossier's Aubio library.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
26 getFrequencyForMIDIPitch(int midiPitch)
28 return 440.f * powf(2.0, (float(midiPitch) - 69.0) / 12.0);
31 Pitch::Pitch(float inputSampleRate) :
32 Plugin(inputSampleRate),
36 m_pitchtype(PitchYinFFT),
37 m_minfreq(getFrequencyForMIDIPitch(32)),
38 m_maxfreq(getFrequencyForMIDIPitch(95)),
48 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
49 if (m_ibuf) del_fvec(m_ibuf);
50 if (m_obuf) del_fvec(m_obuf);
54 Pitch::getIdentifier() const
60 Pitch::getName() const
62 return "Aubio Pitch Detector";
66 Pitch::getDescription() const
68 return "Track estimated note pitches";
72 Pitch::getMaker() const
74 return "Paul Brossier (plugin by Chris Cannam)";
78 Pitch::getPluginVersion() const
84 Pitch::getCopyright() const
90 Pitch::initialise(size_t channels, size_t stepSize, size_t blockSize)
93 std::cerr << "Pitch::initialise: channels must be 1" << std::endl;
97 m_stepSize = stepSize;
98 m_blockSize = blockSize;
100 m_ibuf = new_fvec(stepSize);
101 m_obuf = new_fvec(1);
111 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
113 m_pitchdet = new_aubio_pitch
114 (const_cast<char *>(getAubioNameForPitchType(m_pitchtype)),
117 lrintf(m_inputSampleRate));
119 aubio_pitch_set_unit(m_pitchdet, const_cast<char *>("freq"));
123 Pitch::getPreferredStepSize() const
129 Pitch::getPreferredBlockSize() const
135 Pitch::getParameterDescriptors() const
139 ParameterDescriptor desc;
140 desc.identifier = "pitchtype";
141 desc.name = "Pitch Detection Function Type";
144 desc.defaultValue = (int)PitchYinFFT;
145 desc.isQuantized = true;
146 desc.quantizeStep = 1;
147 desc.valueNames.push_back("YIN Frequency Estimator");
148 desc.valueNames.push_back("Spectral Comb");
149 desc.valueNames.push_back("Schmitt");
150 desc.valueNames.push_back("Fast Harmonic Comb");
151 desc.valueNames.push_back("YIN with FFT");
152 list.push_back(desc);
154 desc = ParameterDescriptor();
155 desc.identifier = "minfreq";
156 desc.name = "Minimum Fundamental Frequency";
158 desc.maxValue = m_inputSampleRate/2;
159 desc.defaultValue = getFrequencyForMIDIPitch(32);
161 desc.isQuantized = false;
162 list.push_back(desc);
164 desc = ParameterDescriptor();
165 desc.identifier = "maxfreq";
166 desc.name = "Maximum Fundamental Frequency";
168 desc.maxValue = m_inputSampleRate/2;
169 desc.defaultValue = getFrequencyForMIDIPitch(95);
171 desc.isQuantized = false;
172 list.push_back(desc);
174 desc = ParameterDescriptor();
175 desc.identifier = "wraprange";
176 desc.name = "Fold Higher or Lower Frequencies into Range";
179 desc.defaultValue = 0;
180 desc.isQuantized = true;
181 desc.quantizeStep = 1;
182 list.push_back(desc);
184 desc = ParameterDescriptor();
185 desc.identifier = "silencethreshold";
186 desc.name = "Silence Threshold";
187 desc.minValue = -120;
189 desc.defaultValue = -90;
191 desc.isQuantized = false;
192 list.push_back(desc);
198 Pitch::getParameter(std::string param) const
200 if (param == "pitchtype") {
202 } else if (param == "minfreq") {
204 } else if (param == "maxfreq") {
206 } else if (param == "wraprange") {
207 return m_wrapRange ? 1.0 : 0.0;
208 } else if (param == "silencethreshold") {
216 Pitch::setParameter(std::string param, float value)
218 if (param == "pitchtype") {
219 switch (lrintf(value)) {
220 case 0: m_pitchtype = PitchYin; break;
221 case 1: m_pitchtype = PitchMComb; break;
222 case 2: m_pitchtype = PitchSchmitt; break;
223 case 3: m_pitchtype = PitchFComb; break;
224 case 4: m_pitchtype = PitchYinFFT; break;
226 } else if (param == "minfreq") {
228 } else if (param == "maxfreq") {
230 } else if (param == "wraprange") {
231 m_wrapRange = (value > 0.5);
232 } else if (param == "silencethreshold") {
238 Pitch::getOutputDescriptors() const
243 d.identifier = "frequency";
244 d.name = "Fundamental Frequency";
246 d.hasFixedBinCount = true;
248 d.hasKnownExtents = false;
249 d.isQuantized = false;
250 d.sampleType = OutputDescriptor::VariableSampleRate;
252 if (m_stepSize != 0) {
253 d.sampleRate = m_inputSampleRate / m_stepSize;
261 Pitch::process(const float *const *inputBuffers,
262 Vamp::RealTime timestamp)
264 FeatureSet returnFeatures;
266 if (m_stepSize == 0) {
267 std::cerr << "Pitch::process: Pitch plugin not initialised" << std::endl;
268 return returnFeatures;
271 for (size_t i = 0; i < m_stepSize; ++i) {
272 fvec_write_sample(m_ibuf, inputBuffers[0][i], i);
275 aubio_pitch_do(m_pitchdet, m_ibuf, m_obuf);
277 float freq = m_obuf->data[0];
279 bool silent = aubio_silence_detection(m_ibuf, m_silence);
281 // std::cerr << "(silent)" << std::endl;
282 return returnFeatures;
286 while (freq > 0 && freq < m_minfreq) {
289 while (freq > m_maxfreq) {
294 if (freq < m_minfreq || freq > m_maxfreq) {
295 return returnFeatures;
299 feature.hasTimestamp = true;
300 feature.timestamp = timestamp;
301 feature.values.push_back(freq);
303 returnFeatures[0].push_back(feature);
304 return returnFeatures;
308 Pitch::getRemainingFeatures()