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.
25 Notes::Notes(float inputSampleRate) :
26 Plugin(inputSampleRate),
33 m_onsettype(aubio_onset_complex),
35 m_pitchtype(aubio_pitch_yinfft),
36 m_pitchmode(aubio_pitchm_freq),
50 if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
51 if (m_pitchdet) del_aubio_pitchdetection(m_pitchdet);
52 if (m_ibuf) del_fvec(m_ibuf);
53 if (m_onset) del_fvec(m_onset);
54 if (m_fftgrain) del_cvec(m_fftgrain);
55 if (m_pv) del_aubio_pvoc(m_pv);
56 if (m_peakpick) del_aubio_peakpicker(m_peakpick);
60 Notes::getIdentifier() const
66 Notes::getName() const
68 return "Aubio Note Tracker";
72 Notes::getDescription() const
74 return "Estimate note onset positions, pitches and durations";
78 Notes::getMaker() const
80 return "Paul Brossier (plugin by Chris Cannam)";
84 Notes::getPluginVersion() const
90 Notes::getCopyright() const
96 Notes::initialise(size_t channels, size_t stepSize, size_t blockSize)
98 m_channelCount = channels;
99 m_stepSize = stepSize;
100 m_blockSize = blockSize;
102 size_t processingBlockSize;
103 if (m_onsettype == aubio_onset_energy ||
104 m_onsettype == aubio_onset_hfc) {
105 processingBlockSize = stepSize * 2;
107 processingBlockSize = stepSize * 4;
110 m_ibuf = new_fvec(stepSize, channels);
111 m_onset = new_fvec(1, channels);
112 m_fftgrain = new_cvec(processingBlockSize, channels);
113 m_pv = new_aubio_pvoc(processingBlockSize, stepSize, channels);
114 m_peakpick = new_aubio_peakpicker(m_threshold);
116 m_onsetdet = new_aubio_onsetdetection(m_onsettype, processingBlockSize, channels);
118 m_pitchdet = new_aubio_pitchdetection(processingBlockSize * 4,
121 lrintf(m_inputSampleRate),
126 m_delay = Vamp::RealTime::frame2RealTime((4 + m_median) * m_stepSize,
127 lrintf(m_inputSampleRate));
128 m_currentOnset = Vamp::RealTime::zeroTime;
129 m_haveCurrent = false;
141 Notes::getPreferredStepSize() const
147 Notes::getPreferredBlockSize() const
149 return 4 * getPreferredStepSize();
153 Notes::getParameterDescriptors() const
157 ParameterDescriptor desc;
158 desc.identifier = "onsettype";
159 desc.name = "Onset Detection Function Type";
162 desc.defaultValue = (int)aubio_onset_complex;
163 desc.isQuantized = true;
164 desc.quantizeStep = 1;
165 desc.valueNames.push_back("Energy Based");
166 desc.valueNames.push_back("Spectral Difference");
167 desc.valueNames.push_back("High-Frequency Content");
168 desc.valueNames.push_back("Complex Domain");
169 desc.valueNames.push_back("Phase Deviation");
170 desc.valueNames.push_back("Kullback-Liebler");
171 desc.valueNames.push_back("Modified Kullback-Liebler");
172 list.push_back(desc);
174 desc = ParameterDescriptor();
175 desc.identifier = "pitchtype";
176 desc.name = "Pitch Detection Function Type";
179 desc.defaultValue = (int)aubio_pitch_yinfft;
180 desc.isQuantized = true;
181 desc.quantizeStep = 1;
182 desc.valueNames.push_back("YIN Frequency Estimator");
183 desc.valueNames.push_back("Spectral Comb");
184 desc.valueNames.push_back("Schmitt");
185 desc.valueNames.push_back("Fast Harmonic Comb");
186 desc.valueNames.push_back("YIN with FFT");
187 list.push_back(desc);
189 desc = ParameterDescriptor();
190 desc.identifier = "minpitch";
191 desc.name = "Minimum Pitch";
194 desc.defaultValue = 32;
195 desc.unit = "MIDI units";
196 desc.isQuantized = true;
197 desc.quantizeStep = 1;
198 list.push_back(desc);
200 desc = ParameterDescriptor();
201 desc.identifier = "maxpitch";
202 desc.name = "Maximum Pitch";
205 desc.defaultValue = 95;
206 desc.unit = "MIDI units";
207 desc.isQuantized = true;
208 desc.quantizeStep = 1;
209 list.push_back(desc);
211 desc = ParameterDescriptor();
212 desc.identifier = "wraprange";
213 desc.name = "Fold Higher or Lower Notes into Range";
216 desc.defaultValue = 0;
217 desc.isQuantized = true;
218 desc.quantizeStep = 1;
219 list.push_back(desc);
221 desc = ParameterDescriptor();
222 desc.identifier = "avoidleaps";
223 desc.name = "Avoid Multi-Octave Jumps";
226 desc.defaultValue = 0;
227 desc.isQuantized = true;
228 desc.quantizeStep = 1;
229 list.push_back(desc);
231 desc = ParameterDescriptor();
232 desc.identifier = "peakpickthreshold";
233 desc.name = "Peak Picker Threshold";
236 desc.defaultValue = 0.3;
237 desc.isQuantized = false;
238 list.push_back(desc);
240 desc = ParameterDescriptor();
241 desc.identifier = "silencethreshold";
242 desc.name = "Silence Threshold";
243 desc.minValue = -120;
245 desc.defaultValue = -90;
247 desc.isQuantized = false;
248 list.push_back(desc);
254 Notes::getParameter(std::string param) const
256 if (param == "onsettype") {
258 } else if (param == "pitchtype") {
260 } else if (param == "peakpickthreshold") {
262 } else if (param == "silencethreshold") {
264 } else if (param == "minpitch") {
266 } else if (param == "maxpitch") {
268 } else if (param == "wraprange") {
269 return m_wrapRange ? 1.0 : 0.0;
270 } else if (param == "avoidleaps") {
271 return m_avoidLeaps ? 1.0 : 0.0;
278 Notes::setParameter(std::string param, float value)
280 if (param == "onsettype") {
281 switch (lrintf(value)) {
282 case 0: m_onsettype = aubio_onset_energy; break;
283 case 1: m_onsettype = aubio_onset_specdiff; break;
284 case 2: m_onsettype = aubio_onset_hfc; break;
285 case 3: m_onsettype = aubio_onset_complex; break;
286 case 4: m_onsettype = aubio_onset_phase; break;
287 case 5: m_onsettype = aubio_onset_kl; break;
288 case 6: m_onsettype = aubio_onset_mkl; break;
290 } else if (param == "pitchtype") {
291 switch (lrintf(value)) {
292 case 0: m_pitchtype = aubio_pitch_yin; break;
293 case 1: m_pitchtype = aubio_pitch_mcomb; break;
294 case 2: m_pitchtype = aubio_pitch_schmitt; break;
295 case 3: m_pitchtype = aubio_pitch_fcomb; break;
296 case 4: m_pitchtype = aubio_pitch_yinfft; break;
298 } else if (param == "peakpickthreshold") {
300 } else if (param == "silencethreshold") {
302 } else if (param == "minpitch") {
303 m_minpitch = lrintf(value);
304 } else if (param == "maxpitch") {
305 m_maxpitch = lrintf(value);
306 } else if (param == "wraprange") {
307 m_wrapRange = (value > 0.5);
308 } else if (param == "avoidleaps") {
309 m_avoidLeaps = (value > 0.5);
314 Notes::getOutputDescriptors() const
319 d.identifier = "notes";
322 d.hasFixedBinCount = true;
324 d.binNames.push_back("Frequency");
325 d.binNames.push_back("Duration");
326 d.binNames.push_back("Velocity");
327 d.hasKnownExtents = false;
328 d.isQuantized = false;
329 d.sampleType = OutputDescriptor::VariableSampleRate;
337 Notes::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
339 for (size_t i = 0; i < m_stepSize; ++i) {
340 for (size_t j = 0; j < m_channelCount; ++j) {
341 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
345 aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
346 aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
348 bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
350 float frequency = aubio_pitchdetection(m_pitchdet, m_ibuf);
352 m_notebuf.push_back(frequency);
353 if (m_notebuf.size() > m_median) m_notebuf.pop_front();
355 float level = aubio_level_detection(m_ibuf, m_silence);
357 FeatureSet returnFeatures;
363 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
368 if (m_count > 0) ++m_count;
369 if (m_count == m_median) {
370 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
371 m_currentOnset = timestamp;
372 m_currentLevel = level;
373 m_haveCurrent = true;
377 m_lastTimeStamp = timestamp;
378 return returnFeatures;
382 Notes::getRemainingFeatures()
384 FeatureSet returnFeatures;
385 if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
386 return returnFeatures;
390 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
392 std::deque<float> toSort = m_notebuf;
393 std::sort(toSort.begin(), toSort.end());
394 float median = toSort[toSort.size()/2];
395 if (median < 45.0) return;
398 int midiPitch = (int)FLOOR(aubio_freqtomidi(freq) + 0.5);
401 if (m_prevPitch >= 0) {
402 while (midiPitch < m_prevPitch - 12) {
406 while (midiPitch > m_prevPitch + 12) {
413 while (midiPitch < m_minpitch) {
414 if (!m_wrapRange) return;
419 while (midiPitch > m_maxpitch) {
420 if (!m_wrapRange) return;
425 m_prevPitch = midiPitch;
428 feature.hasTimestamp = true;
429 if (m_currentOnset < m_delay) m_currentOnset = m_delay;
430 feature.timestamp = m_currentOnset - m_delay;
431 feature.values.push_back(freq);
432 feature.values.push_back
433 (Vamp::RealTime::realTime2Frame(offTime, lrintf(m_inputSampleRate)) -
434 Vamp::RealTime::realTime2Frame(m_currentOnset, lrintf(m_inputSampleRate)));
435 feature.values.push_back(m_currentLevel);
436 fs[0].push_back(feature);