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-2008 Chris Cannam and QMUL.
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/>.
36 Notes::Notes(float inputSampleRate) :
37 Plugin(inputSampleRate),
42 m_onsettype(OnsetComplex),
44 m_pitchtype(PitchYinFFT),
59 if (m_onsetdet) del_aubio_onset(m_onsetdet);
60 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
61 if (m_ibuf) del_fvec(m_ibuf);
62 if (m_onset) del_fvec(m_onset);
63 if (m_pitch) del_fvec(m_pitch);
67 Notes::getIdentifier() const
73 Notes::getName() const
75 return "Aubio Note Tracker";
79 Notes::getDescription() const
81 return "Estimate note onset positions, pitches and durations";
85 Notes::getMaker() const
87 return "Paul Brossier (plugin by Chris Cannam)";
91 Notes::getPluginVersion() const
97 Notes::getCopyright() const
103 Notes::initialise(size_t channels, size_t stepSize, size_t blockSize)
106 std::cerr << "Notes::initialise: channels must be 1" << std::endl;
110 m_stepSize = stepSize;
111 m_blockSize = blockSize;
113 m_ibuf = new_fvec(stepSize);
114 m_onset = new_fvec(1);
115 m_pitch = new_fvec(1);
125 if (m_onsetdet) del_aubio_onset(m_onsetdet);
126 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
128 m_onsetdet = new_aubio_onset
129 (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
132 lrintf(m_inputSampleRate));
134 aubio_onset_set_threshold(m_onsetdet, m_threshold);
135 aubio_onset_set_silence(m_onsetdet, m_silence);
136 aubio_onset_set_minioi(m_onsetdet, m_minioi);
138 m_pitchdet = new_aubio_pitch
139 (const_cast<char *>(getAubioNameForPitchType(m_pitchtype)),
142 lrintf(m_inputSampleRate));
144 aubio_pitch_set_unit(m_pitchdet, const_cast<char *>("freq"));
147 m_delay = Vamp::RealTime::frame2RealTime((4 + m_median) * m_stepSize,
148 lrintf(m_inputSampleRate));
149 m_currentOnset = Vamp::RealTime::zeroTime;
150 m_haveCurrent = false;
155 Notes::getPreferredStepSize() const
161 Notes::getPreferredBlockSize() const
163 return 4 * getPreferredStepSize();
167 Notes::getParameterDescriptors() const
171 ParameterDescriptor desc;
172 desc.identifier = "onsettype";
173 desc.name = "Onset Detection Function Type";
174 desc.description = "Type of onset detection function to use";
177 desc.defaultValue = (int)OnsetComplex;
178 desc.isQuantized = true;
179 desc.quantizeStep = 1;
180 desc.valueNames.push_back("Energy Based");
181 desc.valueNames.push_back("Spectral Difference");
182 desc.valueNames.push_back("High-Frequency Content");
183 desc.valueNames.push_back("Complex Domain");
184 desc.valueNames.push_back("Phase Deviation");
185 desc.valueNames.push_back("Kullback-Liebler");
186 desc.valueNames.push_back("Modified Kullback-Liebler");
187 desc.valueNames.push_back("Spectral Flux");
188 list.push_back(desc);
190 desc = ParameterDescriptor();
191 desc.identifier = "pitchtype";
192 desc.name = "Pitch Detection Function Type";
193 desc.description = "Type of pitch detection function to use";
196 desc.defaultValue = (int)PitchYinFFT;
197 desc.isQuantized = true;
198 desc.quantizeStep = 1;
199 desc.valueNames.push_back("YIN Frequency Estimator");
200 desc.valueNames.push_back("Spectral Comb");
201 desc.valueNames.push_back("Schmitt");
202 desc.valueNames.push_back("Fast Harmonic Comb");
203 desc.valueNames.push_back("YIN with FFT");
204 list.push_back(desc);
206 desc = ParameterDescriptor();
207 desc.identifier = "minpitch";
208 desc.name = "Minimum Pitch";
209 desc.description = "Lowest pitch value to look for";
212 desc.defaultValue = 32;
213 desc.unit = "MIDI units";
214 desc.isQuantized = true;
215 desc.quantizeStep = 1;
216 list.push_back(desc);
218 desc = ParameterDescriptor();
219 desc.identifier = "maxpitch";
220 desc.name = "Maximum Pitch";
221 desc.description = "Highest pitch value to look for";
224 desc.defaultValue = 95;
225 desc.unit = "MIDI units";
226 desc.isQuantized = true;
227 desc.quantizeStep = 1;
228 list.push_back(desc);
230 desc = ParameterDescriptor();
231 desc.identifier = "wraprange";
232 desc.name = "Fold Higher or Lower Notes into Range";
233 desc.description = "Notes detected outside the range will be transposed to higher or lower octaves";
236 desc.defaultValue = 0;
237 desc.isQuantized = true;
238 desc.quantizeStep = 1;
239 list.push_back(desc);
241 desc = ParameterDescriptor();
242 desc.identifier = "avoidleaps";
243 desc.name = "Avoid Multi-Octave Jumps";
244 desc.description = "Minimize octave jumps by transposing to the octave of the previously detected note";
247 desc.defaultValue = 0;
248 desc.isQuantized = true;
249 desc.quantizeStep = 1;
250 list.push_back(desc);
252 desc = ParameterDescriptor();
253 desc.identifier = "peakpickthreshold";
254 desc.name = "Peak Picker Threshold";
255 desc.description = "Peak picking threshold, the higher the least detection";
258 desc.defaultValue = 0.3;
259 desc.isQuantized = false;
260 list.push_back(desc);
262 desc = ParameterDescriptor();
263 desc.identifier = "silencethreshold";
264 desc.name = "Silence Threshold";
265 desc.description = "Silence threshold, the higher the least detection";
266 desc.minValue = -120;
268 desc.defaultValue = -70;
270 desc.isQuantized = false;
271 list.push_back(desc);
273 desc = ParameterDescriptor();
274 desc.identifier = "minioi";
275 desc.name = "Minimum Inter-Onset Interval";
276 desc.description = "Time interval below which two consecutive onsets should be merged";
279 desc.defaultValue = 4;
281 desc.isQuantized = true;
282 desc.quantizeStep = 1;
283 list.push_back(desc);
289 Notes::getParameter(std::string param) const
291 if (param == "onsettype") {
293 } else if (param == "pitchtype") {
295 } else if (param == "peakpickthreshold") {
297 } else if (param == "silencethreshold") {
299 } else if (param == "minpitch") {
301 } else if (param == "maxpitch") {
303 } else if (param == "wraprange") {
304 return m_wrapRange ? 1.0 : 0.0;
305 } else if (param == "avoidleaps") {
306 return m_avoidLeaps ? 1.0 : 0.0;
307 } else if (param == "minioi") {
315 Notes::setParameter(std::string param, float value)
317 if (param == "onsettype") {
318 switch (lrintf(value)) {
319 case 0: m_onsettype = OnsetEnergy; break;
320 case 1: m_onsettype = OnsetSpecDiff; break;
321 case 2: m_onsettype = OnsetHFC; break;
322 case 3: m_onsettype = OnsetComplex; break;
323 case 4: m_onsettype = OnsetPhase; break;
324 case 5: m_onsettype = OnsetKL; break;
325 case 6: m_onsettype = OnsetMKL; break;
326 case 7: m_onsettype = OnsetSpecFlux; break;
328 } else if (param == "pitchtype") {
329 switch (lrintf(value)) {
330 case 0: m_pitchtype = PitchYin; break;
331 case 1: m_pitchtype = PitchMComb; break;
332 case 2: m_pitchtype = PitchSchmitt; break;
333 case 3: m_pitchtype = PitchFComb; break;
334 case 4: m_pitchtype = PitchYinFFT; break;
336 } else if (param == "peakpickthreshold") {
338 } else if (param == "silencethreshold") {
340 } else if (param == "minpitch") {
341 m_minpitch = lrintf(value);
342 } else if (param == "maxpitch") {
343 m_maxpitch = lrintf(value);
344 } else if (param == "wraprange") {
345 m_wrapRange = (value > 0.5);
346 } else if (param == "avoidleaps") {
347 m_avoidLeaps = (value > 0.5);
348 } else if (param == "minioi") {
354 Notes::getOutputDescriptors() const
359 d.identifier = "notes";
361 d.description = "List of notes detected, with their frequency and velocity";
363 d.hasFixedBinCount = true;
366 d.binNames.push_back("Frequency");
367 d.binNames.push_back("Velocity");
368 d.hasDuration = true;
370 d.hasKnownExtents = false;
371 d.isQuantized = false;
372 d.sampleType = OutputDescriptor::VariableSampleRate;
380 Notes::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
382 for (size_t i = 0; i < m_stepSize; ++i) {
383 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
386 aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
387 aubio_pitch_do(m_pitchdet, m_ibuf, m_pitch);
389 bool isonset = m_onset->data[0];
390 float frequency = m_pitch->data[0];
392 m_notebuf.push_back(frequency);
393 if (m_notebuf.size() > m_median) m_notebuf.pop_front();
395 float level = aubio_level_detection(m_ibuf, m_silence);
397 FeatureSet returnFeatures;
403 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
408 if (m_count > 0) ++m_count;
409 if (m_count == m_median) {
410 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
411 m_currentOnset = timestamp;
412 m_currentLevel = level;
413 m_haveCurrent = true;
417 m_lastTimeStamp = timestamp;
418 return returnFeatures;
422 Notes::getRemainingFeatures()
424 FeatureSet returnFeatures;
425 if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
426 return returnFeatures;
430 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
432 std::deque<float> toSort = m_notebuf;
433 std::sort(toSort.begin(), toSort.end());
434 float median = toSort[toSort.size()/2];
435 if (median < 45.0) return;
438 int midiPitch = (int)floor(aubio_freqtomidi(freq) + 0.5);
441 if (m_prevPitch >= 0) {
442 while (midiPitch < m_prevPitch - 12) {
446 while (midiPitch > m_prevPitch + 12) {
453 while (midiPitch < m_minpitch) {
454 if (!m_wrapRange) return;
459 while (midiPitch > m_maxpitch) {
460 if (!m_wrapRange) return;
465 m_prevPitch = midiPitch;
468 feature.hasTimestamp = true;
469 if (m_currentOnset < m_delay) m_currentOnset = m_delay;
470 feature.timestamp = m_currentOnset - m_delay;
471 feature.values.push_back(freq);
473 feature.values.push_back
474 (Vamp::RealTime::realTime2Frame
475 (offTime, lrintf(m_inputSampleRate)) -
476 Vamp::RealTime::realTime2Frame
477 (m_currentOnset, lrintf(m_inputSampleRate)));
478 feature.hasDuration = false;
480 feature.values.push_back(m_currentLevel);
481 fs[0].push_back(feature);