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 file is part of vamp-aubio-plugins.
11 vamp-aubio is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 vamp-aubio is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with aubio. If not, see <http://www.gnu.org/licenses/>.
34 Pitch::Pitch(float inputSampleRate) :
35 Plugin(inputSampleRate),
39 m_pitchtype(PitchYinFFT),
40 m_minfreq(aubio_miditofreq(32)),
41 m_maxfreq(aubio_miditofreq(95)),
51 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
52 if (m_ibuf) del_fvec(m_ibuf);
53 if (m_obuf) del_fvec(m_obuf);
57 Pitch::getIdentifier() const
63 Pitch::getName() const
65 return "Aubio Pitch Detector";
69 Pitch::getDescription() const
71 return "Track estimated note pitches";
75 Pitch::getMaker() const
77 return "Paul Brossier (plugin by Chris Cannam)";
81 Pitch::getPluginVersion() const
87 Pitch::getCopyright() const
93 Pitch::initialise(size_t channels, size_t stepSize, size_t blockSize)
96 std::cerr << "Pitch::initialise: channels must be 1" << std::endl;
100 m_stepSize = stepSize;
101 m_blockSize = blockSize;
103 m_ibuf = new_fvec(stepSize);
104 m_obuf = new_fvec(1);
114 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
116 m_pitchdet = new_aubio_pitch
117 (const_cast<char *>(getAubioNameForPitchType(m_pitchtype)),
120 lrintf(m_inputSampleRate));
122 aubio_pitch_set_unit(m_pitchdet, const_cast<char *>("freq"));
126 Pitch::getPreferredStepSize() const
132 Pitch::getPreferredBlockSize() const
138 Pitch::getParameterDescriptors() const
142 ParameterDescriptor desc;
143 desc.identifier = "pitchtype";
144 desc.name = "Pitch Detection Function Type";
145 desc.description = "Type of pitch detection function to use";
148 desc.defaultValue = (int)PitchYinFFT;
149 desc.isQuantized = true;
150 desc.quantizeStep = 1;
151 desc.valueNames.push_back("YIN Frequency Estimator");
152 desc.valueNames.push_back("Spectral Comb");
153 desc.valueNames.push_back("Schmitt");
154 desc.valueNames.push_back("Fast Harmonic Comb");
155 desc.valueNames.push_back("YIN with FFT");
156 list.push_back(desc);
158 desc = ParameterDescriptor();
159 desc.identifier = "minfreq";
160 desc.name = "Minimum Fundamental Frequency";
161 desc.description = "Lowest frequency to look for";
163 desc.maxValue = m_inputSampleRate/2;
164 desc.defaultValue = aubio_miditofreq(32);
166 desc.isQuantized = false;
167 list.push_back(desc);
169 desc = ParameterDescriptor();
170 desc.identifier = "maxfreq";
171 desc.name = "Maximum Fundamental Frequency";
172 desc.description = "Highest frequency to look for";
174 desc.maxValue = m_inputSampleRate/2;
175 desc.defaultValue = aubio_miditofreq(95);
177 desc.isQuantized = false;
178 list.push_back(desc);
180 desc = ParameterDescriptor();
181 desc.identifier = "wraprange";
182 desc.name = "Fold Higher or Lower Frequencies into Range";
183 desc.description = "Frequencies detected outside the range will be transposed to higher or lower octaves";
186 desc.defaultValue = 0;
187 desc.isQuantized = true;
188 desc.quantizeStep = 1;
189 list.push_back(desc);
191 desc = ParameterDescriptor();
192 desc.identifier = "silencethreshold";
193 desc.name = "Silence Threshold";
194 desc.description = "Silence threshold, the higher the least detection";
195 desc.minValue = -120;
197 desc.defaultValue = -90;
199 desc.isQuantized = false;
200 list.push_back(desc);
206 Pitch::getParameter(std::string param) const
208 if (param == "pitchtype") {
210 } else if (param == "minfreq") {
212 } else if (param == "maxfreq") {
214 } else if (param == "wraprange") {
215 return m_wrapRange ? 1.0 : 0.0;
216 } else if (param == "silencethreshold") {
224 Pitch::setParameter(std::string param, float value)
226 if (param == "pitchtype") {
227 switch (lrintf(value)) {
228 case 0: m_pitchtype = PitchYin; break;
229 case 1: m_pitchtype = PitchMComb; break;
230 case 2: m_pitchtype = PitchSchmitt; break;
231 case 3: m_pitchtype = PitchFComb; break;
232 case 4: m_pitchtype = PitchYinFFT; break;
234 } else if (param == "minfreq") {
236 } else if (param == "maxfreq") {
238 } else if (param == "wraprange") {
239 m_wrapRange = (value > 0.5);
240 } else if (param == "silencethreshold") {
246 Pitch::getOutputDescriptors() const
251 d.identifier = "frequency";
252 d.name = "Fundamental Frequency";
253 d.description = "List of detected frequencies";
255 d.hasFixedBinCount = true;
257 d.hasKnownExtents = false;
258 d.isQuantized = false;
259 d.sampleType = OutputDescriptor::VariableSampleRate;
261 if (m_stepSize != 0) {
262 d.sampleRate = m_inputSampleRate / m_stepSize;
270 Pitch::process(const float *const *inputBuffers,
271 Vamp::RealTime timestamp)
273 FeatureSet returnFeatures;
275 if (m_stepSize == 0) {
276 std::cerr << "Pitch::process: Pitch plugin not initialised" << std::endl;
277 return returnFeatures;
280 for (size_t i = 0; i < m_stepSize; ++i) {
281 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
284 aubio_pitch_do(m_pitchdet, m_ibuf, m_obuf);
286 float freq = m_obuf->data[0];
288 bool silent = aubio_silence_detection(m_ibuf, m_silence);
290 // std::cerr << "(silent)" << std::endl;
291 return returnFeatures;
295 while (freq > 0 && freq < m_minfreq) {
298 while (freq > m_maxfreq) {
303 if (freq < m_minfreq || freq > m_maxfreq) {
304 return returnFeatures;
308 feature.hasTimestamp = true;
309 feature.timestamp = timestamp;
310 feature.values.push_back(freq);
312 returnFeatures[0].push_back(feature);
313 return returnFeatures;
317 Pitch::getRemainingFeatures()