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),
35 m_onsettype(OnsetComplex),
37 m_pitchtype(PitchYinFFT),
51 if (m_onsetdet) del_aubio_onset(m_onsetdet);
52 if (m_pitchdet) del_aubio_pitch(m_pitchdet);
53 if (m_ibuf) del_fvec(m_ibuf);
54 if (m_onset) del_fvec(m_onset);
55 if (m_fftgrain) del_cvec(m_fftgrain);
56 if (m_pv) del_aubio_pvoc(m_pv);
57 if (m_peakpick) del_aubio_peakpicker(m_peakpick);
61 Notes::getIdentifier() const
67 Notes::getName() const
69 return "Aubio Note Tracker";
73 Notes::getDescription() const
75 return "Estimate note onset positions, pitches and durations";
79 Notes::getMaker() const
81 return "Paul Brossier (plugin by Chris Cannam)";
85 Notes::getPluginVersion() const
91 Notes::getCopyright() const
97 Notes::initialise(size_t channels, size_t stepSize, size_t blockSize)
99 m_channelCount = channels;
100 m_stepSize = stepSize;
101 m_blockSize = blockSize;
103 size_t processingBlockSize;
104 if (m_onsettype == OnsetEnergy ||
105 m_onsettype == OnsetHFC) {
106 processingBlockSize = stepSize * 2;
108 processingBlockSize = stepSize * 4;
111 m_ibuf = new_fvec(stepSize, channels);
112 m_onset = new_fvec(1, channels);
113 m_fftgrain = new_cvec(processingBlockSize, channels);
114 m_pv = new_aubio_pvoc(processingBlockSize, stepSize, channels);
115 m_peakpick = new_aubio_peakpicker(m_threshold);
117 m_onsetdet = new_aubio_onsetdetection(m_onsettype, processingBlockSize, channels);
119 m_pitchdet = new_aubio_pitchdetection(processingBlockSize * 4,
122 lrintf(m_inputSampleRate),
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)aubio_onset_complex;
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 list.push_back(desc);
175 desc = ParameterDescriptor();
176 desc.identifier = "pitchtype";
177 desc.name = "Pitch Detection Function Type";
180 desc.defaultValue = (int)aubio_pitch_yinfft;
181 desc.isQuantized = true;
182 desc.quantizeStep = 1;
183 desc.valueNames.push_back("YIN Frequency Estimator");
184 desc.valueNames.push_back("Spectral Comb");
185 desc.valueNames.push_back("Schmitt");
186 desc.valueNames.push_back("Fast Harmonic Comb");
187 desc.valueNames.push_back("YIN with FFT");
188 list.push_back(desc);
190 desc = ParameterDescriptor();
191 desc.identifier = "minpitch";
192 desc.name = "Minimum Pitch";
195 desc.defaultValue = 32;
196 desc.unit = "MIDI units";
197 desc.isQuantized = true;
198 desc.quantizeStep = 1;
199 list.push_back(desc);
201 desc = ParameterDescriptor();
202 desc.identifier = "maxpitch";
203 desc.name = "Maximum Pitch";
206 desc.defaultValue = 95;
207 desc.unit = "MIDI units";
208 desc.isQuantized = true;
209 desc.quantizeStep = 1;
210 list.push_back(desc);
212 desc = ParameterDescriptor();
213 desc.identifier = "wraprange";
214 desc.name = "Fold Higher or Lower Notes into Range";
217 desc.defaultValue = 0;
218 desc.isQuantized = true;
219 desc.quantizeStep = 1;
220 list.push_back(desc);
222 desc = ParameterDescriptor();
223 desc.identifier = "avoidleaps";
224 desc.name = "Avoid Multi-Octave Jumps";
227 desc.defaultValue = 0;
228 desc.isQuantized = true;
229 desc.quantizeStep = 1;
230 list.push_back(desc);
232 desc = ParameterDescriptor();
233 desc.identifier = "peakpickthreshold";
234 desc.name = "Peak Picker Threshold";
237 desc.defaultValue = 0.3;
238 desc.isQuantized = false;
239 list.push_back(desc);
241 desc = ParameterDescriptor();
242 desc.identifier = "silencethreshold";
243 desc.name = "Silence Threshold";
244 desc.minValue = -120;
246 desc.defaultValue = -90;
248 desc.isQuantized = false;
249 list.push_back(desc);
255 Notes::getParameter(std::string param) const
257 if (param == "onsettype") {
259 } else if (param == "pitchtype") {
261 } else if (param == "peakpickthreshold") {
263 } else if (param == "silencethreshold") {
265 } else if (param == "minpitch") {
267 } else if (param == "maxpitch") {
269 } else if (param == "wraprange") {
270 return m_wrapRange ? 1.0 : 0.0;
271 } else if (param == "avoidleaps") {
272 return m_avoidLeaps ? 1.0 : 0.0;
279 Notes::setParameter(std::string param, float value)
281 if (param == "onsettype") {
282 switch (lrintf(value)) {
283 case 0: m_onsettype = aubio_onset_energy; break;
284 case 1: m_onsettype = aubio_onset_specdiff; break;
285 case 2: m_onsettype = aubio_onset_hfc; break;
286 case 3: m_onsettype = aubio_onset_complex; break;
287 case 4: m_onsettype = aubio_onset_phase; break;
288 case 5: m_onsettype = aubio_onset_kl; break;
289 case 6: m_onsettype = aubio_onset_mkl; break;
291 } else if (param == "pitchtype") {
292 switch (lrintf(value)) {
293 case 0: m_pitchtype = aubio_pitch_yin; break;
294 case 1: m_pitchtype = aubio_pitch_mcomb; break;
295 case 2: m_pitchtype = aubio_pitch_schmitt; break;
296 case 3: m_pitchtype = aubio_pitch_fcomb; break;
297 case 4: m_pitchtype = aubio_pitch_yinfft; break;
299 } else if (param == "peakpickthreshold") {
301 } else if (param == "silencethreshold") {
303 } else if (param == "minpitch") {
304 m_minpitch = lrintf(value);
305 } else if (param == "maxpitch") {
306 m_maxpitch = lrintf(value);
307 } else if (param == "wraprange") {
308 m_wrapRange = (value > 0.5);
309 } else if (param == "avoidleaps") {
310 m_avoidLeaps = (value > 0.5);
315 Notes::getOutputDescriptors() const
320 d.identifier = "notes";
323 d.hasFixedBinCount = true;
326 d.binNames.push_back("Frequency");
327 d.binNames.push_back("Velocity");
328 d.hasDuration = true;
330 d.hasKnownExtents = false;
331 d.isQuantized = false;
332 d.sampleType = OutputDescriptor::VariableSampleRate;
340 Notes::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
342 for (size_t i = 0; i < m_stepSize; ++i) {
343 for (size_t j = 0; j < m_channelCount; ++j) {
344 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
348 aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
349 aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
351 bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
353 float frequency = aubio_pitchdetection(m_pitchdet, m_ibuf);
355 m_notebuf.push_back(frequency);
356 if (m_notebuf.size() > m_median) m_notebuf.pop_front();
358 float level = aubio_level_detection(m_ibuf, m_silence);
360 FeatureSet returnFeatures;
366 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
371 if (m_count > 0) ++m_count;
372 if (m_count == m_median) {
373 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
374 m_currentOnset = timestamp;
375 m_currentLevel = level;
376 m_haveCurrent = true;
380 m_lastTimeStamp = timestamp;
381 return returnFeatures;
385 Notes::getRemainingFeatures()
387 FeatureSet returnFeatures;
388 if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
389 return returnFeatures;
393 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
395 std::deque<float> toSort = m_notebuf;
396 std::sort(toSort.begin(), toSort.end());
397 float median = toSort[toSort.size()/2];
398 if (median < 45.0) return;
401 int midiPitch = (int)floor(aubio_freqtomidi(freq) + 0.5);
404 if (m_prevPitch >= 0) {
405 while (midiPitch < m_prevPitch - 12) {
409 while (midiPitch > m_prevPitch + 12) {
416 while (midiPitch < m_minpitch) {
417 if (!m_wrapRange) return;
422 while (midiPitch > m_maxpitch) {
423 if (!m_wrapRange) return;
428 m_prevPitch = midiPitch;
431 feature.hasTimestamp = true;
432 if (m_currentOnset < m_delay) m_currentOnset = m_delay;
433 feature.timestamp = m_currentOnset - m_delay;
434 feature.values.push_back(freq);
436 feature.values.push_back
437 (Vamp::RealTime::realTime2Frame
438 (offTime, lrintf(m_inputSampleRate)) -
439 Vamp::RealTime::realTime2Frame
440 (m_currentOnset, lrintf(m_inputSampleRate)));
441 feature.hasDuration = false;
443 feature.values.push_back(m_currentLevel);
444 fs[0].push_back(feature);