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);
103 m_pitchdet = new_aubio_pitch
104 (const_cast<char *>(getAubioNameForPitchType(m_pitchtype)),
107 lrintf(m_inputSampleRate));
109 aubio_pitch_set_unit(m_pitchdet, const_cast<char *>("freq"));
120 Pitch::getPreferredStepSize() const
126 Pitch::getPreferredBlockSize() const
132 Pitch::getParameterDescriptors() const
136 ParameterDescriptor desc;
137 desc.identifier = "pitchtype";
138 desc.name = "Pitch Detection Function Type";
141 desc.defaultValue = (int)PitchYinFFT;
142 desc.isQuantized = true;
143 desc.quantizeStep = 1;
144 desc.valueNames.push_back("YIN Frequency Estimator");
145 desc.valueNames.push_back("Spectral Comb");
146 desc.valueNames.push_back("Schmitt");
147 desc.valueNames.push_back("Fast Harmonic Comb");
148 desc.valueNames.push_back("YIN with FFT");
149 list.push_back(desc);
151 desc = ParameterDescriptor();
152 desc.identifier = "minfreq";
153 desc.name = "Minimum Fundamental Frequency";
155 desc.maxValue = m_inputSampleRate/2;
156 desc.defaultValue = getFrequencyForMIDIPitch(32);
158 desc.isQuantized = false;
159 list.push_back(desc);
161 desc = ParameterDescriptor();
162 desc.identifier = "maxfreq";
163 desc.name = "Maximum Fundamental Frequency";
165 desc.maxValue = m_inputSampleRate/2;
166 desc.defaultValue = getFrequencyForMIDIPitch(95);
168 desc.isQuantized = false;
169 list.push_back(desc);
171 desc = ParameterDescriptor();
172 desc.identifier = "wraprange";
173 desc.name = "Fold Higher or Lower Frequencies into Range";
176 desc.defaultValue = 0;
177 desc.isQuantized = true;
178 desc.quantizeStep = 1;
179 list.push_back(desc);
181 desc = ParameterDescriptor();
182 desc.identifier = "silencethreshold";
183 desc.name = "Silence Threshold";
184 desc.minValue = -120;
186 desc.defaultValue = -90;
188 desc.isQuantized = false;
189 list.push_back(desc);
195 Pitch::getParameter(std::string param) const
197 if (param == "pitchtype") {
199 } else if (param == "minfreq") {
201 } else if (param == "maxfreq") {
203 } else if (param == "wraprange") {
204 return m_wrapRange ? 1.0 : 0.0;
205 } else if (param == "silencethreshold") {
213 Pitch::setParameter(std::string param, float value)
215 if (param == "pitchtype") {
216 switch (lrintf(value)) {
217 case 0: m_pitchtype = PitchYin; break;
218 case 1: m_pitchtype = PitchMComb; break;
219 case 2: m_pitchtype = PitchSchmitt; break;
220 case 3: m_pitchtype = PitchFComb; break;
221 case 4: m_pitchtype = PitchYinFFT; break;
223 } else if (param == "minfreq") {
225 } else if (param == "maxfreq") {
227 } else if (param == "wraprange") {
228 m_wrapRange = (value > 0.5);
229 } else if (param == "silencethreshold") {
235 Pitch::getOutputDescriptors() const
240 d.identifier = "frequency";
241 d.name = "Fundamental Frequency";
243 d.hasFixedBinCount = true;
245 d.hasKnownExtents = false;
246 d.isQuantized = false;
247 d.sampleType = OutputDescriptor::VariableSampleRate;
249 if (m_stepSize != 0) {
250 d.sampleRate = m_inputSampleRate / m_stepSize;
258 Pitch::process(const float *const *inputBuffers,
259 Vamp::RealTime timestamp)
261 FeatureSet returnFeatures;
263 if (m_stepSize == 0) {
264 std::cerr << "Pitch::process: Pitch plugin not initialised" << std::endl;
265 return returnFeatures;
268 for (size_t i = 0; i < m_stepSize; ++i) {
269 fvec_write_sample(m_ibuf, inputBuffers[0][i], i);
272 aubio_pitch_do(m_pitchdet, m_ibuf, m_obuf);
274 float freq = m_obuf->data[0];
276 bool silent = aubio_silence_detection(m_ibuf, m_silence);
278 // std::cerr << "(silent)" << std::endl;
279 return returnFeatures;
283 while (freq > 0 && freq < m_minfreq) {
286 while (freq > m_maxfreq) {
291 if (freq < m_minfreq || freq > m_maxfreq) {
292 return returnFeatures;
296 feature.hasTimestamp = true;
297 feature.timestamp = timestamp;
298 feature.values.push_back(freq);
300 returnFeatures[0].push_back(feature);
301 return returnFeatures;
305 Pitch::getRemainingFeatures()