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 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.
27 Notes::Notes(float inputSampleRate) :
28 Plugin(inputSampleRate),
33 m_onsettype(OnsetComplex),
35 m_pitchtype(PitchYinFFT),
50 if (m_onsetdet) del_aubio_onset(m_onsetdet);
51 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
52 if (m_ibuf) del_fvec(m_ibuf);
53 if (m_onset) del_fvec(m_onset);
54 if (m_pitch) del_fvec(m_pitch);
58 Notes::getIdentifier() const
64 Notes::getName() const
66 return "Aubio Note Tracker";
70 Notes::getDescription() const
72 return "Estimate note onset positions, pitches and durations";
76 Notes::getMaker() const
78 return "Paul Brossier (plugin by Chris Cannam)";
82 Notes::getPluginVersion() const
88 Notes::getCopyright() const
94 Notes::initialise(size_t channels, size_t stepSize, size_t blockSize)
97 std::cerr << "Notes::initialise: channels must be 1" << std::endl;
101 m_stepSize = stepSize;
102 m_blockSize = blockSize;
104 m_ibuf = new_fvec(stepSize);
105 m_onset = new_fvec(1);
106 m_pitch = new_fvec(1);
116 if (m_onsetdet) del_aubio_onset(m_onsetdet);
117 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
119 m_onsetdet = new_aubio_onset
120 (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
123 lrintf(m_inputSampleRate));
125 aubio_onset_set_threshold(m_onsetdet, m_threshold);
126 aubio_onset_set_silence(m_onsetdet, m_silence);
127 aubio_onset_set_minioi(m_onsetdet, m_minioi);
129 m_pitchdet = new_aubio_pitch
130 (const_cast<char *>(getAubioNameForPitchType(m_pitchtype)),
133 lrintf(m_inputSampleRate));
135 aubio_pitch_set_unit(m_pitchdet, const_cast<char *>("freq"));
138 m_delay = Vamp::RealTime::frame2RealTime((4 + m_median) * m_stepSize,
139 lrintf(m_inputSampleRate));
140 m_currentOnset = Vamp::RealTime::zeroTime;
141 m_haveCurrent = false;
146 Notes::getPreferredStepSize() const
152 Notes::getPreferredBlockSize() const
154 return 4 * getPreferredStepSize();
158 Notes::getParameterDescriptors() const
162 ParameterDescriptor desc;
163 desc.identifier = "onsettype";
164 desc.name = "Onset Detection Function Type";
165 desc.description = "Type of onset detection function to use";
168 desc.defaultValue = (int)OnsetComplex;
169 desc.isQuantized = true;
170 desc.quantizeStep = 1;
171 desc.valueNames.push_back("Energy Based");
172 desc.valueNames.push_back("Spectral Difference");
173 desc.valueNames.push_back("High-Frequency Content");
174 desc.valueNames.push_back("Complex Domain");
175 desc.valueNames.push_back("Phase Deviation");
176 desc.valueNames.push_back("Kullback-Liebler");
177 desc.valueNames.push_back("Modified Kullback-Liebler");
178 desc.valueNames.push_back("Spectral Flux");
179 list.push_back(desc);
181 desc = ParameterDescriptor();
182 desc.identifier = "pitchtype";
183 desc.name = "Pitch Detection Function Type";
184 desc.description = "Type of pitch detection function to use";
187 desc.defaultValue = (int)PitchYinFFT;
188 desc.isQuantized = true;
189 desc.quantizeStep = 1;
190 desc.valueNames.push_back("YIN Frequency Estimator");
191 desc.valueNames.push_back("Spectral Comb");
192 desc.valueNames.push_back("Schmitt");
193 desc.valueNames.push_back("Fast Harmonic Comb");
194 desc.valueNames.push_back("YIN with FFT");
195 list.push_back(desc);
197 desc = ParameterDescriptor();
198 desc.identifier = "minpitch";
199 desc.name = "Minimum Pitch";
200 desc.description = "Lowest pitch value to look for";
203 desc.defaultValue = 32;
204 desc.unit = "MIDI units";
205 desc.isQuantized = true;
206 desc.quantizeStep = 1;
207 list.push_back(desc);
209 desc = ParameterDescriptor();
210 desc.identifier = "maxpitch";
211 desc.name = "Maximum Pitch";
212 desc.description = "Highest pitch value to look for";
215 desc.defaultValue = 95;
216 desc.unit = "MIDI units";
217 desc.isQuantized = true;
218 desc.quantizeStep = 1;
219 list.push_back(desc);
221 desc = ParameterDescriptor();
222 desc.identifier = "wraprange";
223 desc.name = "Fold Higher or Lower Notes into Range";
224 desc.description = "Notes detected outside the range will be transposed to higher or lower octaves";
227 desc.defaultValue = 0;
228 desc.isQuantized = true;
229 desc.quantizeStep = 1;
230 list.push_back(desc);
232 desc = ParameterDescriptor();
233 desc.identifier = "avoidleaps";
234 desc.name = "Avoid Multi-Octave Jumps";
235 desc.description = "Minimize octave jumps by transposing to the octave of the previously detected note";
238 desc.defaultValue = 0;
239 desc.isQuantized = true;
240 desc.quantizeStep = 1;
241 list.push_back(desc);
243 desc = ParameterDescriptor();
244 desc.identifier = "peakpickthreshold";
245 desc.name = "Peak Picker Threshold";
246 desc.description = "Peak picking threshold, the higher the least detection";
249 desc.defaultValue = 0.3;
250 desc.isQuantized = false;
251 list.push_back(desc);
253 desc = ParameterDescriptor();
254 desc.identifier = "silencethreshold";
255 desc.name = "Silence Threshold";
256 desc.description = "Silence threshold, the higher the least detection";
257 desc.minValue = -120;
259 desc.defaultValue = -70;
261 desc.isQuantized = false;
262 list.push_back(desc);
264 desc = ParameterDescriptor();
265 desc.identifier = "minioi";
266 desc.name = "Minimum Inter-Onset Interval";
267 desc.description = "Time interval below which two consecutive onsets should be merged";
270 desc.defaultValue = 4;
272 desc.isQuantized = true;
273 desc.quantizeStep = 1;
274 list.push_back(desc);
280 Notes::getParameter(std::string param) const
282 if (param == "onsettype") {
284 } else if (param == "pitchtype") {
286 } else if (param == "peakpickthreshold") {
288 } else if (param == "silencethreshold") {
290 } else if (param == "minpitch") {
292 } else if (param == "maxpitch") {
294 } else if (param == "wraprange") {
295 return m_wrapRange ? 1.0 : 0.0;
296 } else if (param == "avoidleaps") {
297 return m_avoidLeaps ? 1.0 : 0.0;
298 } else if (param == "minioi") {
306 Notes::setParameter(std::string param, float value)
308 if (param == "onsettype") {
309 switch (lrintf(value)) {
310 case 0: m_onsettype = OnsetEnergy; break;
311 case 1: m_onsettype = OnsetSpecDiff; break;
312 case 2: m_onsettype = OnsetHFC; break;
313 case 3: m_onsettype = OnsetComplex; break;
314 case 4: m_onsettype = OnsetPhase; break;
315 case 5: m_onsettype = OnsetKL; break;
316 case 6: m_onsettype = OnsetMKL; break;
317 case 7: m_onsettype = OnsetSpecFlux; break;
319 } else if (param == "pitchtype") {
320 switch (lrintf(value)) {
321 case 0: m_pitchtype = PitchYin; break;
322 case 1: m_pitchtype = PitchMComb; break;
323 case 2: m_pitchtype = PitchSchmitt; break;
324 case 3: m_pitchtype = PitchFComb; break;
325 case 4: m_pitchtype = PitchYinFFT; break;
327 } else if (param == "peakpickthreshold") {
329 } else if (param == "silencethreshold") {
331 } else if (param == "minpitch") {
332 m_minpitch = lrintf(value);
333 } else if (param == "maxpitch") {
334 m_maxpitch = lrintf(value);
335 } else if (param == "wraprange") {
336 m_wrapRange = (value > 0.5);
337 } else if (param == "avoidleaps") {
338 m_avoidLeaps = (value > 0.5);
339 } else if (param == "minioi") {
345 Notes::getOutputDescriptors() const
350 d.identifier = "notes";
352 d.description = "List of notes detected, with their frequency and velocity";
354 d.hasFixedBinCount = true;
357 d.binNames.push_back("Frequency");
358 d.binNames.push_back("Velocity");
359 d.hasDuration = true;
361 d.hasKnownExtents = false;
362 d.isQuantized = false;
363 d.sampleType = OutputDescriptor::VariableSampleRate;
371 Notes::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
373 for (size_t i = 0; i < m_stepSize; ++i) {
374 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
377 aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
378 aubio_pitch_do(m_pitchdet, m_ibuf, m_pitch);
380 bool isonset = m_onset->data[0];
381 float frequency = m_pitch->data[0];
383 m_notebuf.push_back(frequency);
384 if (m_notebuf.size() > m_median) m_notebuf.pop_front();
386 float level = aubio_level_detection(m_ibuf, m_silence);
388 FeatureSet returnFeatures;
394 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
399 if (m_count > 0) ++m_count;
400 if (m_count == m_median) {
401 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
402 m_currentOnset = timestamp;
403 m_currentLevel = level;
404 m_haveCurrent = true;
408 m_lastTimeStamp = timestamp;
409 return returnFeatures;
413 Notes::getRemainingFeatures()
415 FeatureSet returnFeatures;
416 if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
417 return returnFeatures;
421 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
423 std::deque<float> toSort = m_notebuf;
424 std::sort(toSort.begin(), toSort.end());
425 float median = toSort[toSort.size()/2];
426 if (median < 45.0) return;
429 int midiPitch = (int)floor(aubio_freqtomidi(freq) + 0.5);
432 if (m_prevPitch >= 0) {
433 while (midiPitch < m_prevPitch - 12) {
437 while (midiPitch > m_prevPitch + 12) {
444 while (midiPitch < m_minpitch) {
445 if (!m_wrapRange) return;
450 while (midiPitch > m_maxpitch) {
451 if (!m_wrapRange) return;
456 m_prevPitch = midiPitch;
459 feature.hasTimestamp = true;
460 if (m_currentOnset < m_delay) m_currentOnset = m_delay;
461 feature.timestamp = m_currentOnset - m_delay;
462 feature.values.push_back(freq);
464 feature.values.push_back
465 (Vamp::RealTime::realTime2Frame
466 (offTime, lrintf(m_inputSampleRate)) -
467 Vamp::RealTime::realTime2Frame
468 (m_currentOnset, lrintf(m_inputSampleRate)));
469 feature.hasDuration = false;
471 feature.values.push_back(m_currentLevel);
472 fs[0].push_back(feature);