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(aubio_onset_complex),
38 m_pitchtype(aubio_pitch_yinfft),
39 m_pitchmode(aubio_pitchm_freq),
49 if (apiVersion == 1) {
50 cerr << "vamp-aubio: WARNING: using compatibility version 1 of the Vamp API for note\n"
51 << "tracker plugin: upgrade your host to v2 for proper duration support" << endl;
53 cerr << "vamp-aubio: NOTE: using v2 API for true durations" << endl;
59 if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
60 if (m_pitchdet) del_aubio_pitchdetection(m_pitchdet);
61 if (m_ibuf) del_fvec(m_ibuf);
62 if (m_onset) del_fvec(m_onset);
63 if (m_fftgrain) del_cvec(m_fftgrain);
64 if (m_pv) del_aubio_pvoc(m_pv);
65 if (m_peakpick) del_aubio_peakpicker(m_peakpick);
69 Notes::getIdentifier() const
75 Notes::getName() const
77 return "Aubio Note Tracker";
81 Notes::getDescription() const
83 return "Estimate note onset positions, pitches and durations";
87 Notes::getMaker() const
89 return "Paul Brossier (plugin by Chris Cannam)";
93 Notes::getPluginVersion() const
95 if (m_apiVersion == 1) return 2;
100 Notes::getCopyright() const
106 Notes::initialise(size_t channels, size_t stepSize, size_t blockSize)
108 m_channelCount = channels;
109 m_stepSize = stepSize;
110 m_blockSize = blockSize;
112 size_t processingBlockSize;
113 if (m_onsettype == aubio_onset_energy ||
114 m_onsettype == aubio_onset_hfc) {
115 processingBlockSize = stepSize * 2;
117 processingBlockSize = stepSize * 4;
120 m_ibuf = new_fvec(stepSize, channels);
121 m_onset = new_fvec(1, channels);
122 m_fftgrain = new_cvec(processingBlockSize, channels);
123 m_pv = new_aubio_pvoc(processingBlockSize, stepSize, channels);
124 m_peakpick = new_aubio_peakpicker(m_threshold);
126 m_onsetdet = new_aubio_onsetdetection(m_onsettype, processingBlockSize, channels);
128 m_pitchdet = new_aubio_pitchdetection(processingBlockSize * 4,
131 lrintf(m_inputSampleRate),
136 m_delay = Vamp::RealTime::frame2RealTime((4 + m_median) * m_stepSize,
137 lrintf(m_inputSampleRate));
138 m_currentOnset = Vamp::RealTime::zeroTime;
139 m_haveCurrent = false;
151 Notes::getPreferredStepSize() const
157 Notes::getPreferredBlockSize() const
159 return 4 * getPreferredStepSize();
163 Notes::getParameterDescriptors() const
167 ParameterDescriptor desc;
168 desc.identifier = "onsettype";
169 desc.name = "Onset Detection Function Type";
172 desc.defaultValue = (int)aubio_onset_complex;
173 desc.isQuantized = true;
174 desc.quantizeStep = 1;
175 desc.valueNames.push_back("Energy Based");
176 desc.valueNames.push_back("Spectral Difference");
177 desc.valueNames.push_back("High-Frequency Content");
178 desc.valueNames.push_back("Complex Domain");
179 desc.valueNames.push_back("Phase Deviation");
180 desc.valueNames.push_back("Kullback-Liebler");
181 desc.valueNames.push_back("Modified Kullback-Liebler");
182 list.push_back(desc);
184 desc = ParameterDescriptor();
185 desc.identifier = "pitchtype";
186 desc.name = "Pitch Detection Function Type";
189 desc.defaultValue = (int)aubio_pitch_yinfft;
190 desc.isQuantized = true;
191 desc.quantizeStep = 1;
192 desc.valueNames.push_back("YIN Frequency Estimator");
193 desc.valueNames.push_back("Spectral Comb");
194 desc.valueNames.push_back("Schmitt");
195 desc.valueNames.push_back("Fast Harmonic Comb");
196 desc.valueNames.push_back("YIN with FFT");
197 list.push_back(desc);
199 desc = ParameterDescriptor();
200 desc.identifier = "minpitch";
201 desc.name = "Minimum Pitch";
204 desc.defaultValue = 32;
205 desc.unit = "MIDI units";
206 desc.isQuantized = true;
207 desc.quantizeStep = 1;
208 list.push_back(desc);
210 desc = ParameterDescriptor();
211 desc.identifier = "maxpitch";
212 desc.name = "Maximum Pitch";
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";
226 desc.defaultValue = 0;
227 desc.isQuantized = true;
228 desc.quantizeStep = 1;
229 list.push_back(desc);
231 desc = ParameterDescriptor();
232 desc.identifier = "avoidleaps";
233 desc.name = "Avoid Multi-Octave Jumps";
236 desc.defaultValue = 0;
237 desc.isQuantized = true;
238 desc.quantizeStep = 1;
239 list.push_back(desc);
241 desc = ParameterDescriptor();
242 desc.identifier = "peakpickthreshold";
243 desc.name = "Peak Picker Threshold";
246 desc.defaultValue = 0.3;
247 desc.isQuantized = false;
248 list.push_back(desc);
250 desc = ParameterDescriptor();
251 desc.identifier = "silencethreshold";
252 desc.name = "Silence Threshold";
253 desc.minValue = -120;
255 desc.defaultValue = -90;
257 desc.isQuantized = false;
258 list.push_back(desc);
264 Notes::getParameter(std::string param) const
266 if (param == "onsettype") {
268 } else if (param == "pitchtype") {
270 } else if (param == "peakpickthreshold") {
272 } else if (param == "silencethreshold") {
274 } else if (param == "minpitch") {
276 } else if (param == "maxpitch") {
278 } else if (param == "wraprange") {
279 return m_wrapRange ? 1.0 : 0.0;
280 } else if (param == "avoidleaps") {
281 return m_avoidLeaps ? 1.0 : 0.0;
288 Notes::setParameter(std::string param, float value)
290 if (param == "onsettype") {
291 switch (lrintf(value)) {
292 case 0: m_onsettype = aubio_onset_energy; break;
293 case 1: m_onsettype = aubio_onset_specdiff; break;
294 case 2: m_onsettype = aubio_onset_hfc; break;
295 case 3: m_onsettype = aubio_onset_complex; break;
296 case 4: m_onsettype = aubio_onset_phase; break;
297 case 5: m_onsettype = aubio_onset_kl; break;
298 case 6: m_onsettype = aubio_onset_mkl; break;
300 } else if (param == "pitchtype") {
301 switch (lrintf(value)) {
302 case 0: m_pitchtype = aubio_pitch_yin; break;
303 case 1: m_pitchtype = aubio_pitch_mcomb; break;
304 case 2: m_pitchtype = aubio_pitch_schmitt; break;
305 case 3: m_pitchtype = aubio_pitch_fcomb; break;
306 case 4: m_pitchtype = aubio_pitch_yinfft; break;
308 } else if (param == "peakpickthreshold") {
310 } else if (param == "silencethreshold") {
312 } else if (param == "minpitch") {
313 m_minpitch = lrintf(value);
314 } else if (param == "maxpitch") {
315 m_maxpitch = lrintf(value);
316 } else if (param == "wraprange") {
317 m_wrapRange = (value > 0.5);
318 } else if (param == "avoidleaps") {
319 m_avoidLeaps = (value > 0.5);
324 Notes::getOutputDescriptors() const
329 d.identifier = "notes";
332 d.hasFixedBinCount = true;
334 if (m_apiVersion == 1) {
336 d.binNames.push_back("Frequency");
337 d.binNames.push_back("Duration");
338 d.binNames.push_back("Velocity");
341 d.binNames.push_back("Frequency");
342 d.binNames.push_back("Velocity");
343 d.hasDuration = true;
346 d.hasKnownExtents = false;
347 d.isQuantized = false;
348 d.sampleType = OutputDescriptor::VariableSampleRate;
356 Notes::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
358 for (size_t i = 0; i < m_stepSize; ++i) {
359 for (size_t j = 0; j < m_channelCount; ++j) {
360 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
364 aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
365 aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
367 bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
369 float frequency = aubio_pitchdetection(m_pitchdet, m_ibuf);
371 m_notebuf.push_back(frequency);
372 if (m_notebuf.size() > m_median) m_notebuf.pop_front();
374 float level = aubio_level_detection(m_ibuf, m_silence);
376 FeatureSet returnFeatures;
382 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
387 if (m_count > 0) ++m_count;
388 if (m_count == m_median) {
389 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
390 m_currentOnset = timestamp;
391 m_currentLevel = level;
392 m_haveCurrent = true;
396 m_lastTimeStamp = timestamp;
397 return returnFeatures;
401 Notes::getRemainingFeatures()
403 FeatureSet returnFeatures;
404 if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
405 return returnFeatures;
409 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
411 std::deque<float> toSort = m_notebuf;
412 std::sort(toSort.begin(), toSort.end());
413 float median = toSort[toSort.size()/2];
414 if (median < 45.0) return;
417 int midiPitch = (int)floor(aubio_freqtomidi(freq) + 0.5);
420 if (m_prevPitch >= 0) {
421 while (midiPitch < m_prevPitch - 12) {
425 while (midiPitch > m_prevPitch + 12) {
432 while (midiPitch < m_minpitch) {
433 if (!m_wrapRange) return;
438 while (midiPitch > m_maxpitch) {
439 if (!m_wrapRange) return;
444 m_prevPitch = midiPitch;
447 feature.hasTimestamp = true;
448 if (m_currentOnset < m_delay) m_currentOnset = m_delay;
449 feature.timestamp = m_currentOnset - m_delay;
450 feature.values.push_back(freq);
452 if (m_apiVersion == 1) {
453 feature.values.push_back
454 (Vamp::RealTime::realTime2Frame
455 (offTime, lrintf(m_inputSampleRate)) -
456 Vamp::RealTime::realTime2Frame
457 (m_currentOnset, lrintf(m_inputSampleRate)));
458 feature.hasDuration = false;
460 feature.hasDuration = true;
461 feature.duration = offTime - m_currentOnset;
464 feature.values.push_back(m_currentLevel);
465 fs[0].push_back(feature);