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";
167 desc.defaultValue = (int)OnsetComplex;
168 desc.isQuantized = true;
169 desc.quantizeStep = 1;
170 desc.valueNames.push_back("Energy Based");
171 desc.valueNames.push_back("Spectral Difference");
172 desc.valueNames.push_back("High-Frequency Content");
173 desc.valueNames.push_back("Complex Domain");
174 desc.valueNames.push_back("Phase Deviation");
175 desc.valueNames.push_back("Kullback-Liebler");
176 desc.valueNames.push_back("Modified Kullback-Liebler");
177 desc.valueNames.push_back("Spectral Flux");
178 list.push_back(desc);
180 desc = ParameterDescriptor();
181 desc.identifier = "pitchtype";
182 desc.name = "Pitch Detection Function Type";
185 desc.defaultValue = (int)PitchYinFFT;
186 desc.isQuantized = true;
187 desc.quantizeStep = 1;
188 desc.valueNames.push_back("YIN Frequency Estimator");
189 desc.valueNames.push_back("Spectral Comb");
190 desc.valueNames.push_back("Schmitt");
191 desc.valueNames.push_back("Fast Harmonic Comb");
192 desc.valueNames.push_back("YIN with FFT");
193 list.push_back(desc);
195 desc = ParameterDescriptor();
196 desc.identifier = "minpitch";
197 desc.name = "Minimum Pitch";
200 desc.defaultValue = 32;
201 desc.unit = "MIDI units";
202 desc.isQuantized = true;
203 desc.quantizeStep = 1;
204 list.push_back(desc);
206 desc = ParameterDescriptor();
207 desc.identifier = "maxpitch";
208 desc.name = "Maximum Pitch";
211 desc.defaultValue = 95;
212 desc.unit = "MIDI units";
213 desc.isQuantized = true;
214 desc.quantizeStep = 1;
215 list.push_back(desc);
217 desc = ParameterDescriptor();
218 desc.identifier = "wraprange";
219 desc.name = "Fold Higher or Lower Notes into Range";
222 desc.defaultValue = 0;
223 desc.isQuantized = true;
224 desc.quantizeStep = 1;
225 list.push_back(desc);
227 desc = ParameterDescriptor();
228 desc.identifier = "avoidleaps";
229 desc.name = "Avoid Multi-Octave Jumps";
232 desc.defaultValue = 0;
233 desc.isQuantized = true;
234 desc.quantizeStep = 1;
235 list.push_back(desc);
237 desc = ParameterDescriptor();
238 desc.identifier = "peakpickthreshold";
239 desc.name = "Peak Picker Threshold";
242 desc.defaultValue = 0.3;
243 desc.isQuantized = false;
244 list.push_back(desc);
246 desc = ParameterDescriptor();
247 desc.identifier = "silencethreshold";
248 desc.name = "Silence Threshold";
249 desc.minValue = -120;
251 desc.defaultValue = -70;
253 desc.isQuantized = false;
254 list.push_back(desc);
256 desc = ParameterDescriptor();
257 desc.identifier = "minioi";
258 desc.name = "Minimum Inter-Onset Interval";
261 desc.defaultValue = 4;
263 desc.isQuantized = true;
264 desc.quantizeStep = 1;
265 list.push_back(desc);
271 Notes::getParameter(std::string param) const
273 if (param == "onsettype") {
275 } else if (param == "pitchtype") {
277 } else if (param == "peakpickthreshold") {
279 } else if (param == "silencethreshold") {
281 } else if (param == "minpitch") {
283 } else if (param == "maxpitch") {
285 } else if (param == "wraprange") {
286 return m_wrapRange ? 1.0 : 0.0;
287 } else if (param == "avoidleaps") {
288 return m_avoidLeaps ? 1.0 : 0.0;
289 } else if (param == "minioi") {
297 Notes::setParameter(std::string param, float value)
299 if (param == "onsettype") {
300 switch (lrintf(value)) {
301 case 0: m_onsettype = OnsetEnergy; break;
302 case 1: m_onsettype = OnsetSpecDiff; break;
303 case 2: m_onsettype = OnsetHFC; break;
304 case 3: m_onsettype = OnsetComplex; break;
305 case 4: m_onsettype = OnsetPhase; break;
306 case 5: m_onsettype = OnsetKL; break;
307 case 6: m_onsettype = OnsetMKL; break;
308 case 7: m_onsettype = OnsetSpecFlux; break;
310 } else if (param == "pitchtype") {
311 switch (lrintf(value)) {
312 case 0: m_pitchtype = PitchYin; break;
313 case 1: m_pitchtype = PitchMComb; break;
314 case 2: m_pitchtype = PitchSchmitt; break;
315 case 3: m_pitchtype = PitchFComb; break;
316 case 4: m_pitchtype = PitchYinFFT; break;
318 } else if (param == "peakpickthreshold") {
320 } else if (param == "silencethreshold") {
322 } else if (param == "minpitch") {
323 m_minpitch = lrintf(value);
324 } else if (param == "maxpitch") {
325 m_maxpitch = lrintf(value);
326 } else if (param == "wraprange") {
327 m_wrapRange = (value > 0.5);
328 } else if (param == "avoidleaps") {
329 m_avoidLeaps = (value > 0.5);
330 } else if (param == "minioi") {
336 Notes::getOutputDescriptors() const
341 d.identifier = "notes";
344 d.hasFixedBinCount = true;
347 d.binNames.push_back("Frequency");
348 d.binNames.push_back("Velocity");
349 d.hasDuration = true;
351 d.hasKnownExtents = false;
352 d.isQuantized = false;
353 d.sampleType = OutputDescriptor::VariableSampleRate;
361 Notes::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
363 for (size_t i = 0; i < m_stepSize; ++i) {
364 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
367 aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
368 aubio_pitch_do(m_pitchdet, m_ibuf, m_pitch);
370 bool isonset = m_onset->data[0];
371 float frequency = m_pitch->data[0];
373 m_notebuf.push_back(frequency);
374 if (m_notebuf.size() > m_median) m_notebuf.pop_front();
376 float level = aubio_level_detection(m_ibuf, m_silence);
378 FeatureSet returnFeatures;
384 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
389 if (m_count > 0) ++m_count;
390 if (m_count == m_median) {
391 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
392 m_currentOnset = timestamp;
393 m_currentLevel = level;
394 m_haveCurrent = true;
398 m_lastTimeStamp = timestamp;
399 return returnFeatures;
403 Notes::getRemainingFeatures()
405 FeatureSet returnFeatures;
406 if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
407 return returnFeatures;
411 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
413 std::deque<float> toSort = m_notebuf;
414 std::sort(toSort.begin(), toSort.end());
415 float median = toSort[toSort.size()/2];
416 if (median < 45.0) return;
419 int midiPitch = (int)floor(aubio_freqtomidi(freq) + 0.5);
422 if (m_prevPitch >= 0) {
423 while (midiPitch < m_prevPitch - 12) {
427 while (midiPitch > m_prevPitch + 12) {
434 while (midiPitch < m_minpitch) {
435 if (!m_wrapRange) return;
440 while (midiPitch > m_maxpitch) {
441 if (!m_wrapRange) return;
446 m_prevPitch = midiPitch;
449 feature.hasTimestamp = true;
450 if (m_currentOnset < m_delay) m_currentOnset = m_delay;
451 feature.timestamp = m_currentOnset - m_delay;
452 feature.values.push_back(freq);
454 feature.values.push_back
455 (Vamp::RealTime::realTime2Frame
456 (offTime, lrintf(m_inputSampleRate)) -
457 Vamp::RealTime::realTime2Frame
458 (m_currentOnset, lrintf(m_inputSampleRate)));
459 feature.hasDuration = false;
461 feature.values.push_back(m_currentLevel);
462 fs[0].push_back(feature);