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::getName() const
66 Notes::getDescription() const
68 return "Aubio Note Tracker";
72 Notes::getMaker() const
74 return "Paul Brossier (plugin by Chris Cannam)";
78 Notes::getPluginVersion() const
84 Notes::getCopyright() const
90 Notes::initialise(size_t channels, size_t stepSize, size_t blockSize)
92 m_channelCount = channels;
93 m_stepSize = stepSize;
94 m_blockSize = blockSize;
96 size_t processingBlockSize;
97 if (m_onsettype == aubio_onset_energy ||
98 m_onsettype == aubio_onset_hfc) {
99 processingBlockSize = stepSize * 2;
101 processingBlockSize = stepSize * 4;
104 m_ibuf = new_fvec(stepSize, channels);
105 m_onset = new_fvec(1, channels);
106 m_fftgrain = new_cvec(processingBlockSize, channels);
107 m_pv = new_aubio_pvoc(processingBlockSize, stepSize, channels);
108 m_peakpick = new_aubio_peakpicker(m_threshold);
110 m_onsetdet = new_aubio_onsetdetection(m_onsettype, processingBlockSize, channels);
112 m_pitchdet = new_aubio_pitchdetection(processingBlockSize * 4,
115 lrintf(m_inputSampleRate),
120 m_delay = Vamp::RealTime::frame2RealTime((4 + m_median) * m_stepSize,
121 lrintf(m_inputSampleRate));
122 m_currentOnset = Vamp::RealTime::zeroTime;
123 m_haveCurrent = false;
135 Notes::getPreferredStepSize() const
141 Notes::getPreferredBlockSize() const
143 return 4 * getPreferredStepSize();
147 Notes::getParameterDescriptors() const
151 ParameterDescriptor desc;
152 desc.name = "onsettype";
153 desc.description = "Onset Detection Function Type";
156 desc.defaultValue = (int)aubio_onset_complex;
157 desc.isQuantized = true;
158 desc.quantizeStep = 1;
159 desc.valueNames.push_back("Energy Based");
160 desc.valueNames.push_back("Spectral Difference");
161 desc.valueNames.push_back("High-Frequency Content");
162 desc.valueNames.push_back("Complex Domain");
163 desc.valueNames.push_back("Phase Deviation");
164 desc.valueNames.push_back("Kullback-Liebler");
165 desc.valueNames.push_back("Modified Kullback-Liebler");
166 list.push_back(desc);
168 desc = ParameterDescriptor();
169 desc.name = "pitchtype";
170 desc.description = "Pitch Detection Function Type";
173 desc.defaultValue = (int)aubio_pitch_yinfft;
174 desc.isQuantized = true;
175 desc.quantizeStep = 1;
176 desc.valueNames.push_back("YIN Frequency Estimator");
177 desc.valueNames.push_back("Spectral Comb");
178 desc.valueNames.push_back("Schmitt");
179 desc.valueNames.push_back("Fast Harmonic Comb");
180 desc.valueNames.push_back("YIN with FFT");
181 list.push_back(desc);
183 desc = ParameterDescriptor();
184 desc.name = "minpitch";
185 desc.description = "Minimum Pitch";
188 desc.defaultValue = 32;
189 desc.unit = "MIDI units";
190 desc.isQuantized = true;
191 desc.quantizeStep = 1;
192 list.push_back(desc);
194 desc = ParameterDescriptor();
195 desc.name = "maxpitch";
196 desc.description = "Maximum Pitch";
199 desc.defaultValue = 95;
200 desc.unit = "MIDI units";
201 desc.isQuantized = true;
202 desc.quantizeStep = 1;
203 list.push_back(desc);
205 desc = ParameterDescriptor();
206 desc.name = "wraprange";
207 desc.description = "Fold Higher or Lower Notes into Range";
210 desc.defaultValue = 0;
211 desc.isQuantized = true;
212 desc.quantizeStep = 1;
213 list.push_back(desc);
215 desc = ParameterDescriptor();
216 desc.name = "avoidleaps";
217 desc.description = "Avoid Multi-Octave Jumps";
220 desc.defaultValue = 0;
221 desc.isQuantized = true;
222 desc.quantizeStep = 1;
223 list.push_back(desc);
225 desc = ParameterDescriptor();
226 desc.name = "peakpickthreshold";
227 desc.description = "Peak Picker Threshold";
230 desc.defaultValue = 0.3;
231 desc.isQuantized = false;
232 list.push_back(desc);
234 desc = ParameterDescriptor();
235 desc.name = "silencethreshold";
236 desc.description = "Silence Threshold";
237 desc.minValue = -120;
239 desc.defaultValue = -90;
241 desc.isQuantized = false;
242 list.push_back(desc);
248 Notes::getParameter(std::string param) const
250 if (param == "onsettype") {
252 } else if (param == "pitchtype") {
254 } else if (param == "peakpickthreshold") {
256 } else if (param == "silencethreshold") {
258 } else if (param == "minpitch") {
260 } else if (param == "maxpitch") {
262 } else if (param == "wraprange") {
263 return m_wrapRange ? 1.0 : 0.0;
264 } else if (param == "avoidleaps") {
265 return m_avoidLeaps ? 1.0 : 0.0;
272 Notes::setParameter(std::string param, float value)
274 if (param == "onsettype") {
275 switch (lrintf(value)) {
276 case 0: m_onsettype = aubio_onset_energy; break;
277 case 1: m_onsettype = aubio_onset_specdiff; break;
278 case 2: m_onsettype = aubio_onset_hfc; break;
279 case 3: m_onsettype = aubio_onset_complex; break;
280 case 4: m_onsettype = aubio_onset_phase; break;
281 case 5: m_onsettype = aubio_onset_kl; break;
282 case 6: m_onsettype = aubio_onset_mkl; break;
284 } else if (param == "pitchtype") {
285 switch (lrintf(value)) {
286 case 0: m_pitchtype = aubio_pitch_yin; break;
287 case 1: m_pitchtype = aubio_pitch_mcomb; break;
288 case 2: m_pitchtype = aubio_pitch_schmitt; break;
289 case 3: m_pitchtype = aubio_pitch_fcomb; break;
290 case 4: m_pitchtype = aubio_pitch_yinfft; break;
292 } else if (param == "peakpickthreshold") {
294 } else if (param == "silencethreshold") {
296 } else if (param == "minpitch") {
297 m_minpitch = lrintf(value);
298 } else if (param == "maxpitch") {
299 m_maxpitch = lrintf(value);
300 } else if (param == "wraprange") {
301 m_wrapRange = (value > 0.5);
302 } else if (param == "avoidleaps") {
303 m_avoidLeaps = (value > 0.5);
308 Notes::getOutputDescriptors() const
315 d.description = "Notes";
316 d.hasFixedBinCount = true;
318 d.binNames.push_back("Frequency");
319 d.binNames.push_back("Duration");
320 d.binNames.push_back("Velocity");
321 d.hasKnownExtents = false;
322 d.isQuantized = false;
323 d.sampleType = OutputDescriptor::VariableSampleRate;
331 Notes::process(float **inputBuffers, Vamp::RealTime timestamp)
333 for (size_t i = 0; i < m_stepSize; ++i) {
334 for (size_t j = 0; j < m_channelCount; ++j) {
335 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
339 aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
340 aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
342 bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
344 float frequency = aubio_pitchdetection(m_pitchdet, m_ibuf);
346 m_notebuf.push_back(frequency);
347 if (m_notebuf.size() > m_median) m_notebuf.pop_front();
349 float level = aubio_level_detection(m_ibuf, m_silence);
351 FeatureSet returnFeatures;
357 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
362 if (m_count > 0) ++m_count;
363 if (m_count == m_median) {
364 if (m_haveCurrent) pushNote(returnFeatures, timestamp);
365 m_currentOnset = timestamp;
366 m_currentLevel = level;
367 m_haveCurrent = true;
371 m_lastTimeStamp = timestamp;
372 return returnFeatures;
376 Notes::getRemainingFeatures()
378 FeatureSet returnFeatures;
379 if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
380 return returnFeatures;
384 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
386 std::deque<float> toSort = m_notebuf;
387 std::sort(toSort.begin(), toSort.end());
388 float median = toSort[toSort.size()/2];
389 if (median < 45.0) return;
392 int midiPitch = (int)FLOOR(aubio_freqtomidi(freq) + 0.5);
395 if (m_prevPitch >= 0) {
396 while (midiPitch < m_prevPitch - 12) {
400 while (midiPitch > m_prevPitch + 12) {
407 while (midiPitch < m_minpitch) {
408 if (!m_wrapRange) return;
413 while (midiPitch > m_maxpitch) {
414 if (!m_wrapRange) return;
419 m_prevPitch = midiPitch;
422 feature.hasTimestamp = true;
423 if (m_currentOnset < m_delay) m_currentOnset = m_delay;
424 feature.timestamp = m_currentOnset - m_delay;
425 feature.values.push_back(freq);
426 feature.values.push_back
427 (Vamp::RealTime::realTime2Frame(offTime, lrintf(m_inputSampleRate)) -
428 Vamp::RealTime::realTime2Frame(m_currentOnset, lrintf(m_inputSampleRate)));
429 feature.values.push_back(m_currentLevel);
430 fs[0].push_back(feature);