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, unsigned int apiVersion) :
28 Plugin(inputSampleRate),
29 m_apiVersion(apiVersion),
36 m_onsettype(OnsetComplex),
38 m_pitchtype(PitchYinFFT),
48 if (apiVersion == 1) {
49 cerr << "vamp-aubio: WARNING: using compatibility version 1 of the Vamp API for note\n"
50 << "tracker plugin: upgrade your host to v2 for proper duration support" << endl;
52 cerr << "vamp-aubio: NOTE: using v2 API for true durations" << endl;
58 if (m_onsetdet) del_aubio_onset(m_onsetdet);
59 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
60 if (m_ibuf) del_fvec(m_ibuf);
61 if (m_onset) del_fvec(m_onset);
62 if (m_fftgrain) del_cvec(m_fftgrain);
63 if (m_pv) del_aubio_pvoc(m_pv);
64 if (m_peakpick) del_aubio_peakpicker(m_peakpick);
68 Notes::getIdentifier() const
74 Notes::getName() const
76 return "Aubio Note Tracker";
80 Notes::getDescription() const
82 return "Estimate note onset positions, pitches and durations";
86 Notes::getMaker() const
88 return "Paul Brossier (plugin by Chris Cannam)";
92 Notes::getPluginVersion() const
94 if (m_apiVersion == 1) return 2;
99 Notes::getCopyright() const
105 Notes::initialise(size_t channels, size_t stepSize, size_t blockSize)
107 m_channelCount = channels;
108 m_stepSize = stepSize;
109 m_blockSize = blockSize;
111 size_t processingBlockSize;
112 if (m_onsettype == OnsetEnergy ||
113 m_onsettype == OnsetHFC) {
114 processingBlockSize = stepSize * 2;
116 processingBlockSize = stepSize * 4;
119 m_ibuf = new_fvec(stepSize, channels);
120 m_onset = new_fvec(1, channels);
121 m_fftgrain = new_cvec(processingBlockSize, channels);
122 m_pv = new_aubio_pvoc(processingBlockSize, stepSize, channels);
123 m_peakpick = new_aubio_peakpicker(m_threshold);
125 m_onsetdet = new_aubio_onsetdetection(m_onsettype, processingBlockSize, channels);
127 m_pitchdet = new_aubio_pitchdetection(processingBlockSize * 4,
130 lrintf(m_inputSampleRate),
135 m_delay = Vamp::RealTime::frame2RealTime((4 + m_median) * m_stepSize,
136 lrintf(m_inputSampleRate));
137 m_currentOnset = Vamp::RealTime::zeroTime;
138 m_haveCurrent = false;
150 Notes::getPreferredStepSize() const
156 Notes::getPreferredBlockSize() const
158 return 4 * getPreferredStepSize();
162 Notes::getParameterDescriptors() const
166 ParameterDescriptor desc;
167 desc.identifier = "onsettype";
168 desc.name = "Onset Detection Function Type";
171 desc.defaultValue = (int)aubio_onset_complex;
172 desc.isQuantized = true;
173 desc.quantizeStep = 1;
174 desc.valueNames.push_back("Energy Based");
175 desc.valueNames.push_back("Spectral Difference");
176 desc.valueNames.push_back("High-Frequency Content");
177 desc.valueNames.push_back("Complex Domain");
178 desc.valueNames.push_back("Phase Deviation");
179 desc.valueNames.push_back("Kullback-Liebler");
180 desc.valueNames.push_back("Modified Kullback-Liebler");
181 list.push_back(desc);
183 desc = ParameterDescriptor();
184 desc.identifier = "pitchtype";
185 desc.name = "Pitch Detection Function Type";
188 desc.defaultValue = (int)aubio_pitch_yinfft;
189 desc.isQuantized = true;
190 desc.quantizeStep = 1;
191 desc.valueNames.push_back("YIN Frequency Estimator");
192 desc.valueNames.push_back("Spectral Comb");
193 desc.valueNames.push_back("Schmitt");
194 desc.valueNames.push_back("Fast Harmonic Comb");
195 desc.valueNames.push_back("YIN with FFT");
196 list.push_back(desc);
198 desc = ParameterDescriptor();
199 desc.identifier = "minpitch";
200 desc.name = "Minimum Pitch";
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";
214 desc.defaultValue = 95;
215 desc.unit = "MIDI units";
216 desc.isQuantized = true;
217 desc.quantizeStep = 1;
218 list.push_back(desc);
220 desc = ParameterDescriptor();
221 desc.identifier = "wraprange";
222 desc.name = "Fold Higher or Lower Notes into Range";
225 desc.defaultValue = 0;
226 desc.isQuantized = true;
227 desc.quantizeStep = 1;
228 list.push_back(desc);
230 desc = ParameterDescriptor();
231 desc.identifier = "avoidleaps";
232 desc.name = "Avoid Multi-Octave Jumps";
235 desc.defaultValue = 0;
236 desc.isQuantized = true;
237 desc.quantizeStep = 1;
238 list.push_back(desc);
240 desc = ParameterDescriptor();
241 desc.identifier = "peakpickthreshold";
242 desc.name = "Peak Picker Threshold";
245 desc.defaultValue = 0.3;
246 desc.isQuantized = false;
247 list.push_back(desc);
249 desc = ParameterDescriptor();
250 desc.identifier = "silencethreshold";
251 desc.name = "Silence Threshold";
252 desc.minValue = -120;
254 desc.defaultValue = -90;
256 desc.isQuantized = false;
257 list.push_back(desc);
263 Notes::getParameter(std::string param) const
265 if (param == "onsettype") {
267 } else if (param == "pitchtype") {
269 } else if (param == "peakpickthreshold") {
271 } else if (param == "silencethreshold") {
273 } else if (param == "minpitch") {
275 } else if (param == "maxpitch") {
277 } else if (param == "wraprange") {
278 return m_wrapRange ? 1.0 : 0.0;
279 } else if (param == "avoidleaps") {
280 return m_avoidLeaps ? 1.0 : 0.0;
287 Notes::setParameter(std::string param, float value)
289 if (param == "onsettype") {
290 switch (lrintf(value)) {
291 case 0: m_onsettype = aubio_onset_energy; break;
292 case 1: m_onsettype = aubio_onset_specdiff; break;
293 case 2: m_onsettype = aubio_onset_hfc; break;
294 case 3: m_onsettype = aubio_onset_complex; break;
295 case 4: m_onsettype = aubio_onset_phase; break;
296 case 5: m_onsettype = aubio_onset_kl; break;
297 case 6: m_onsettype = aubio_onset_mkl; break;
299 } else if (param == "pitchtype") {
300 switch (lrintf(value)) {
301 case 0: m_pitchtype = aubio_pitch_yin; break;
302 case 1: m_pitchtype = aubio_pitch_mcomb; break;
303 case 2: m_pitchtype = aubio_pitch_schmitt; break;
304 case 3: m_pitchtype = aubio_pitch_fcomb; break;
305 case 4: m_pitchtype = aubio_pitch_yinfft; break;
307 } else if (param == "peakpickthreshold") {
309 } else if (param == "silencethreshold") {
311 } else if (param == "minpitch") {
312 m_minpitch = lrintf(value);
313 } else if (param == "maxpitch") {
314 m_maxpitch = lrintf(value);
315 } else if (param == "wraprange") {
316 m_wrapRange = (value > 0.5);
317 } else if (param == "avoidleaps") {
318 m_avoidLeaps = (value > 0.5);
323 Notes::getOutputDescriptors() const
328 d.identifier = "notes";
331 d.hasFixedBinCount = true;
333 if (m_apiVersion == 1) {
335 d.binNames.push_back("Frequency");
336 d.binNames.push_back("Duration");
337 d.binNames.push_back("Velocity");
340 d.binNames.push_back("Frequency");
341 d.binNames.push_back("Velocity");
342 d.hasDuration = true;
345 d.hasKnownExtents = false;
346 d.isQuantized = false;
347 d.sampleType = OutputDescriptor::VariableSampleRate;
355 Notes::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
357 for (size_t i = 0; i < m_stepSize; ++i) {
358 for (size_t j = 0; j < m_channelCount; ++j) {
359 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
363 aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
364 aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
366 bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
368 float frequency = aubio_pitchdetection(m_pitchdet, m_ibuf);
370 m_notebuf.push_back(frequency);
371 if (m_notebuf.size() > m_median) m_notebuf.pop_front();
373 float level = aubio_level_detection(m_ibuf, m_silence);
375 FeatureSet returnFeatures;
381 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
386 if (m_count > 0) ++m_count;
387 if (m_count == m_median) {
388 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
389 m_currentOnset = timestamp;
390 m_currentLevel = level;
391 m_haveCurrent = true;
395 m_lastTimeStamp = timestamp;
396 return returnFeatures;
400 Notes::getRemainingFeatures()
402 FeatureSet returnFeatures;
403 if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
404 return returnFeatures;
408 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
410 std::deque<float> toSort = m_notebuf;
411 std::sort(toSort.begin(), toSort.end());
412 float median = toSort[toSort.size()/2];
413 if (median < 45.0) return;
416 int midiPitch = (int)floor(aubio_freqtomidi(freq) + 0.5);
419 if (m_prevPitch >= 0) {
420 while (midiPitch < m_prevPitch - 12) {
424 while (midiPitch > m_prevPitch + 12) {
431 while (midiPitch < m_minpitch) {
432 if (!m_wrapRange) return;
437 while (midiPitch > m_maxpitch) {
438 if (!m_wrapRange) return;
443 m_prevPitch = midiPitch;
446 feature.hasTimestamp = true;
447 if (m_currentOnset < m_delay) m_currentOnset = m_delay;
448 feature.timestamp = m_currentOnset - m_delay;
449 feature.values.push_back(freq);
451 if (m_apiVersion == 1) {
452 feature.values.push_back
453 (Vamp::RealTime::realTime2Frame
454 (offTime, lrintf(m_inputSampleRate)) -
455 Vamp::RealTime::realTime2Frame
456 (m_currentOnset, lrintf(m_inputSampleRate)));
457 feature.hasDuration = false;
459 feature.hasDuration = true;
460 feature.duration = offTime - m_currentOnset;
463 feature.values.push_back(m_currentLevel);
464 fs[0].push_back(feature);