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),
35 m_pitchtype(PitchYinFFT),
36 m_minfreq(getFrequencyForMIDIPitch(32)),
37 m_maxfreq(getFrequencyForMIDIPitch(95)),
47 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
48 if (m_ibuf) del_fvec(m_ibuf);
49 if (m_obuf) del_fvec(m_obuf);
53 Pitch::getIdentifier() const
59 Pitch::getName() const
61 return "Aubio Pitch Detector";
65 Pitch::getDescription() const
67 return "Track estimated note pitches";
71 Pitch::getMaker() const
73 return "Paul Brossier (plugin by Chris Cannam)";
77 Pitch::getPluginVersion() const
83 Pitch::getCopyright() const
89 Pitch::initialise(size_t channels, size_t stepSize, size_t blockSize)
92 std::cerr << "Pitch::initialise: channels must be 1" << std::endl;
96 m_stepSize = stepSize;
97 m_blockSize = blockSize;
99 m_ibuf = new_fvec(stepSize);
100 m_obuf = new_fvec(1);
102 m_pitchdet = new_aubio_pitch
103 (const_cast<char *>(getAubioNameForPitchType(m_pitchtype)),
106 lrintf(m_inputSampleRate));
108 aubio_pitch_set_unit(m_pitchdet, "freq");
119 Pitch::getPreferredStepSize() const
125 Pitch::getPreferredBlockSize() const
131 Pitch::getParameterDescriptors() const
135 ParameterDescriptor desc;
136 desc.identifier = "pitchtype";
137 desc.name = "Pitch Detection Function Type";
140 desc.defaultValue = (int)PitchYinFFT;
141 desc.isQuantized = true;
142 desc.quantizeStep = 1;
143 desc.valueNames.push_back("YIN Frequency Estimator");
144 desc.valueNames.push_back("Spectral Comb");
145 desc.valueNames.push_back("Schmitt");
146 desc.valueNames.push_back("Fast Harmonic Comb");
147 desc.valueNames.push_back("YIN with FFT");
148 list.push_back(desc);
150 desc = ParameterDescriptor();
151 desc.identifier = "minfreq";
152 desc.name = "Minimum Fundamental Frequency";
154 desc.maxValue = m_inputSampleRate/2;
155 desc.defaultValue = getFrequencyForMIDIPitch(32);
157 desc.isQuantized = false;
158 list.push_back(desc);
160 desc = ParameterDescriptor();
161 desc.identifier = "maxfreq";
162 desc.name = "Maximum Fundamental Frequency";
164 desc.maxValue = m_inputSampleRate/2;
165 desc.defaultValue = getFrequencyForMIDIPitch(95);
167 desc.isQuantized = false;
168 list.push_back(desc);
170 desc = ParameterDescriptor();
171 desc.identifier = "wraprange";
172 desc.name = "Fold Higher or Lower Frequencies into Range";
175 desc.defaultValue = 0;
176 desc.isQuantized = true;
177 desc.quantizeStep = 1;
178 list.push_back(desc);
180 desc = ParameterDescriptor();
181 desc.identifier = "silencethreshold";
182 desc.name = "Silence Threshold";
183 desc.minValue = -120;
185 desc.defaultValue = -90;
187 desc.isQuantized = false;
188 list.push_back(desc);
194 Pitch::getParameter(std::string param) const
196 if (param == "pitchtype") {
198 } else if (param == "minfreq") {
200 } else if (param == "maxfreq") {
202 } else if (param == "wraprange") {
203 return m_wrapRange ? 1.0 : 0.0;
204 } else if (param == "silencethreshold") {
212 Pitch::setParameter(std::string param, float value)
214 if (param == "pitchtype") {
215 switch (lrintf(value)) {
216 case 0: m_pitchtype = PitchYin; break;
217 case 1: m_pitchtype = PitchMComb; break;
218 case 2: m_pitchtype = PitchSchmitt; break;
219 case 3: m_pitchtype = PitchFComb; break;
220 case 4: m_pitchtype = PitchYinFFT; break;
222 } else if (param == "minfreq") {
224 } else if (param == "maxfreq") {
226 } else if (param == "wraprange") {
227 m_wrapRange = (value > 0.5);
228 } else if (param == "silencethreshold") {
234 Pitch::getOutputDescriptors() const
239 d.identifier = "frequency";
240 d.name = "Fundamental Frequency";
242 d.hasFixedBinCount = true;
244 d.hasKnownExtents = false;
245 d.isQuantized = false;
246 d.sampleType = OutputDescriptor::VariableSampleRate;
248 if (m_stepSize != 0) {
249 d.sampleRate = m_inputSampleRate / m_stepSize;
257 Pitch::process(const float *const *inputBuffers,
258 Vamp::RealTime timestamp)
260 FeatureSet returnFeatures;
262 if (m_stepSize == 0) {
263 std::cerr << "Pitch::process: Pitch plugin not initialised" << std::endl;
264 return returnFeatures;
267 for (size_t i = 0; i < m_stepSize; ++i) {
268 fvec_write_sample(m_ibuf, inputBuffers[0][i], i);
271 aubio_pitch_do(m_pitchdet, m_ibuf, m_obuf);
273 float freq = m_obuf->data[0];
275 bool silent = aubio_silence_detection(m_ibuf, m_silence);
277 // std::cerr << "(silent)" << std::endl;
278 return returnFeatures;
282 while (freq > 0 && freq < m_minfreq) {
285 while (freq > m_maxfreq) {
290 if (freq < m_minfreq || freq > m_maxfreq) {
291 return returnFeatures;
295 feature.hasTimestamp = true;
296 feature.timestamp = timestamp;
297 feature.values.push_back(freq);
299 returnFeatures[0].push_back(feature);
300 return returnFeatures;
304 Pitch::getRemainingFeatures()