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);
108 m_onsetdet = new_aubio_onset
109 (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
112 lrintf(m_inputSampleRate));
114 aubio_onset_set_threshold(m_onsetdet, m_threshold);
115 aubio_onset_set_silence(m_onsetdet, m_silence);
116 aubio_onset_set_minioi(m_onsetdet, m_minioi);
118 m_pitchdet = new_aubio_pitch
119 (const_cast<char *>(getAubioNameForPitchType(m_pitchtype)),
122 lrintf(m_inputSampleRate));
124 aubio_pitch_set_unit(m_pitchdet, const_cast<char *>("freq"));
127 m_delay = Vamp::RealTime::frame2RealTime((4 + m_median) * m_stepSize,
128 lrintf(m_inputSampleRate));
129 m_currentOnset = Vamp::RealTime::zeroTime;
130 m_haveCurrent = false;
142 Notes::getPreferredStepSize() const
148 Notes::getPreferredBlockSize() const
150 return 4 * getPreferredStepSize();
154 Notes::getParameterDescriptors() const
158 ParameterDescriptor desc;
159 desc.identifier = "onsettype";
160 desc.name = "Onset Detection Function Type";
163 desc.defaultValue = (int)OnsetComplex;
164 desc.isQuantized = true;
165 desc.quantizeStep = 1;
166 desc.valueNames.push_back("Energy Based");
167 desc.valueNames.push_back("Spectral Difference");
168 desc.valueNames.push_back("High-Frequency Content");
169 desc.valueNames.push_back("Complex Domain");
170 desc.valueNames.push_back("Phase Deviation");
171 desc.valueNames.push_back("Kullback-Liebler");
172 desc.valueNames.push_back("Modified Kullback-Liebler");
173 desc.valueNames.push_back("Spectral Flux");
174 list.push_back(desc);
176 desc = ParameterDescriptor();
177 desc.identifier = "pitchtype";
178 desc.name = "Pitch Detection Function Type";
181 desc.defaultValue = (int)PitchYinFFT;
182 desc.isQuantized = true;
183 desc.quantizeStep = 1;
184 desc.valueNames.push_back("YIN Frequency Estimator");
185 desc.valueNames.push_back("Spectral Comb");
186 desc.valueNames.push_back("Schmitt");
187 desc.valueNames.push_back("Fast Harmonic Comb");
188 desc.valueNames.push_back("YIN with FFT");
189 list.push_back(desc);
191 desc = ParameterDescriptor();
192 desc.identifier = "minpitch";
193 desc.name = "Minimum Pitch";
196 desc.defaultValue = 32;
197 desc.unit = "MIDI units";
198 desc.isQuantized = true;
199 desc.quantizeStep = 1;
200 list.push_back(desc);
202 desc = ParameterDescriptor();
203 desc.identifier = "maxpitch";
204 desc.name = "Maximum Pitch";
207 desc.defaultValue = 95;
208 desc.unit = "MIDI units";
209 desc.isQuantized = true;
210 desc.quantizeStep = 1;
211 list.push_back(desc);
213 desc = ParameterDescriptor();
214 desc.identifier = "wraprange";
215 desc.name = "Fold Higher or Lower Notes into Range";
218 desc.defaultValue = 0;
219 desc.isQuantized = true;
220 desc.quantizeStep = 1;
221 list.push_back(desc);
223 desc = ParameterDescriptor();
224 desc.identifier = "avoidleaps";
225 desc.name = "Avoid Multi-Octave Jumps";
228 desc.defaultValue = 0;
229 desc.isQuantized = true;
230 desc.quantizeStep = 1;
231 list.push_back(desc);
233 desc = ParameterDescriptor();
234 desc.identifier = "peakpickthreshold";
235 desc.name = "Peak Picker Threshold";
238 desc.defaultValue = 0.3;
239 desc.isQuantized = false;
240 list.push_back(desc);
242 desc = ParameterDescriptor();
243 desc.identifier = "silencethreshold";
244 desc.name = "Silence Threshold";
245 desc.minValue = -120;
247 desc.defaultValue = -70;
249 desc.isQuantized = false;
250 list.push_back(desc);
252 desc = ParameterDescriptor();
253 desc.identifier = "minioi";
254 desc.name = "Minimum Inter-Onset Interval";
257 desc.defaultValue = 4;
259 desc.isQuantized = true;
260 desc.quantizeStep = 1;
261 list.push_back(desc);
267 Notes::getParameter(std::string param) const
269 if (param == "onsettype") {
271 } else if (param == "pitchtype") {
273 } else if (param == "peakpickthreshold") {
275 } else if (param == "silencethreshold") {
277 } else if (param == "minpitch") {
279 } else if (param == "maxpitch") {
281 } else if (param == "wraprange") {
282 return m_wrapRange ? 1.0 : 0.0;
283 } else if (param == "avoidleaps") {
284 return m_avoidLeaps ? 1.0 : 0.0;
285 } else if (param == "minioi") {
293 Notes::setParameter(std::string param, float value)
295 if (param == "onsettype") {
296 switch (lrintf(value)) {
297 case 0: m_onsettype = OnsetEnergy; break;
298 case 1: m_onsettype = OnsetSpecDiff; break;
299 case 2: m_onsettype = OnsetHFC; break;
300 case 3: m_onsettype = OnsetComplex; break;
301 case 4: m_onsettype = OnsetPhase; break;
302 case 5: m_onsettype = OnsetKL; break;
303 case 6: m_onsettype = OnsetMKL; break;
304 case 7: m_onsettype = OnsetSpecFlux; break;
306 } else if (param == "pitchtype") {
307 switch (lrintf(value)) {
308 case 0: m_pitchtype = PitchYin; break;
309 case 1: m_pitchtype = PitchMComb; break;
310 case 2: m_pitchtype = PitchSchmitt; break;
311 case 3: m_pitchtype = PitchFComb; break;
312 case 4: m_pitchtype = PitchYinFFT; break;
314 } else if (param == "peakpickthreshold") {
316 } else if (param == "silencethreshold") {
318 } else if (param == "minpitch") {
319 m_minpitch = lrintf(value);
320 } else if (param == "maxpitch") {
321 m_maxpitch = lrintf(value);
322 } else if (param == "wraprange") {
323 m_wrapRange = (value > 0.5);
324 } else if (param == "avoidleaps") {
325 m_avoidLeaps = (value > 0.5);
326 } else if (param == "minioi") {
332 Notes::getOutputDescriptors() const
337 d.identifier = "notes";
340 d.hasFixedBinCount = true;
343 d.binNames.push_back("Frequency");
344 d.binNames.push_back("Velocity");
345 d.hasDuration = true;
347 d.hasKnownExtents = false;
348 d.isQuantized = false;
349 d.sampleType = OutputDescriptor::VariableSampleRate;
357 Notes::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
359 for (size_t i = 0; i < m_stepSize; ++i) {
360 fvec_write_sample(m_ibuf, inputBuffers[0][i], i);
363 aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
364 aubio_pitch_do(m_pitchdet, m_ibuf, m_pitch);
366 bool isonset = m_onset->data[0];
367 float frequency = m_pitch->data[0];
369 m_notebuf.push_back(frequency);
370 if (m_notebuf.size() > m_median) m_notebuf.pop_front();
372 float level = aubio_level_detection(m_ibuf, m_silence);
374 FeatureSet returnFeatures;
380 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
385 if (m_count > 0) ++m_count;
386 if (m_count == m_median) {
387 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
388 m_currentOnset = timestamp;
389 m_currentLevel = level;
390 m_haveCurrent = true;
394 m_lastTimeStamp = timestamp;
395 return returnFeatures;
399 Notes::getRemainingFeatures()
401 FeatureSet returnFeatures;
402 if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
403 return returnFeatures;
407 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
409 std::deque<float> toSort = m_notebuf;
410 std::sort(toSort.begin(), toSort.end());
411 float median = toSort[toSort.size()/2];
412 if (median < 45.0) return;
415 int midiPitch = (int)floor(aubio_freqtomidi(freq) + 0.5);
418 if (m_prevPitch >= 0) {
419 while (midiPitch < m_prevPitch - 12) {
423 while (midiPitch > m_prevPitch + 12) {
430 while (midiPitch < m_minpitch) {
431 if (!m_wrapRange) return;
436 while (midiPitch > m_maxpitch) {
437 if (!m_wrapRange) return;
442 m_prevPitch = midiPitch;
445 feature.hasTimestamp = true;
446 if (m_currentOnset < m_delay) m_currentOnset = m_delay;
447 feature.timestamp = m_currentOnset - m_delay;
448 feature.values.push_back(freq);
450 feature.values.push_back
451 (Vamp::RealTime::realTime2Frame
452 (offTime, lrintf(m_inputSampleRate)) -
453 Vamp::RealTime::realTime2Frame
454 (m_currentOnset, lrintf(m_inputSampleRate)));
455 feature.hasDuration = false;
457 feature.values.push_back(m_currentLevel);
458 fs[0].push_back(feature);