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.
18 #include <ardourext/float_cast.h>
29 const char *getAubioNameForOnsetType(OnsetType t)
31 // In the same order as the enum elements in the header
32 static const char *const names[] = {
33 "energy", "specdiff", "hfc", "complex", "phase", "kl", "mkl", "specflux"
39 Onset::Onset(float inputSampleRate) :
40 Plugin(inputSampleRate),
45 m_onsettype(OnsetComplex),
53 m_onsettype(aubio_onset_complex),
64 if (m_onsetdet) del_aubio_onset(m_onsetdet);
66 if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
67 if (m_fftgrain) del_cvec(m_fftgrain);
68 if (m_pv) del_aubio_pvoc(m_pv);
69 if (m_peakpick) del_aubio_peakpicker(m_peakpick);
71 if (m_ibuf) del_fvec(m_ibuf);
72 if (m_onset) del_fvec(m_onset);
76 Onset::getIdentifier() const
82 Onset::getName() const
84 return "Aubio Onset Detector";
88 Onset::getDescription() const
90 return "Estimate note onset times";
94 Onset::getMaker() const
96 return "Paul Brossier (plugin by Chris Cannam)";
100 Onset::getPluginVersion() const
110 Onset::getCopyright() const
116 Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
118 m_stepSize = stepSize;
119 m_blockSize = blockSize;
123 std::cerr << "Onset::initialise: channels must be 1" << std::endl;
126 m_ibuf = new_fvec(stepSize);
127 m_onset = new_fvec(1);
130 m_channelCount = channels;
132 m_ibuf = new_fvec(stepSize, channels);
133 m_onset = new_fvec(1, channels);
134 m_fftgrain = new_cvec(blockSize, channels);
135 m_pv = new_aubio_pvoc(blockSize, stepSize, channels);
136 m_peakpick = new_aubio_peakpicker(m_threshold);
138 m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
140 m_delay = Vamp::RealTime::frame2RealTime(4 * stepSize,
141 lrintf(m_inputSampleRate));
143 m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
153 if (m_onsetdet) del_aubio_onset(m_onsetdet);
155 m_onsetdet = new_aubio_onset
156 (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
159 lrintf(m_inputSampleRate));
161 aubio_onset_set_threshold(m_onsetdet, m_threshold);
162 aubio_onset_set_silence(m_onsetdet, m_silence);
163 aubio_onset_set_minioi(m_onsetdet, m_minioi);
165 m_delay = Vamp::RealTime::frame2RealTime(4 * m_stepSize,
166 lrintf(m_inputSampleRate));
168 m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
170 if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
171 if (m_peakpick) del_aubio_peakpicker(m_peakpick);
173 m_peakpick = new_aubio_peakpicker(m_threshold);
174 m_onsetdet = new_aubio_onsetdetection(m_onsettype, m_blockSize, m_channelCount);
179 Onset::getPreferredStepSize() const
185 Onset::getPreferredBlockSize() const
187 return 2 * getPreferredStepSize();
191 Onset::getParameterDescriptors() const
195 ParameterDescriptor desc;
196 desc.identifier = "onsettype";
197 desc.name = "Onset Detection Function Type";
201 desc.defaultValue = (int)OnsetComplex;
204 desc.defaultValue = (int)aubio_onset_complex;
206 desc.isQuantized = true;
207 desc.quantizeStep = 1;
208 desc.valueNames.push_back("Energy Based");
209 desc.valueNames.push_back("Spectral Difference");
210 desc.valueNames.push_back("High-Frequency Content");
211 desc.valueNames.push_back("Complex Domain");
212 desc.valueNames.push_back("Phase Deviation");
213 desc.valueNames.push_back("Kullback-Liebler");
214 desc.valueNames.push_back("Modified Kullback-Liebler");
216 desc.valueNames.push_back("Spectral Flux");
218 list.push_back(desc);
220 desc = ParameterDescriptor();
221 desc.identifier = "peakpickthreshold";
222 desc.name = "Peak Picker Threshold";
225 desc.defaultValue = 0.3;
226 desc.isQuantized = false;
227 list.push_back(desc);
229 desc = ParameterDescriptor();
230 desc.identifier = "silencethreshold";
231 desc.name = "Silence Threshold";
232 desc.minValue = -120;
235 desc.defaultValue = -70;
237 desc.defaultValue = -90;
240 desc.isQuantized = false;
241 list.push_back(desc);
244 desc = ParameterDescriptor();
245 desc.identifier = "minioi";
246 desc.name = "Minimum Inter-Onset Interval";
249 desc.defaultValue = 4;
251 desc.isQuantized = true;
252 desc.quantizeStep = 1;
253 list.push_back(desc);
259 Onset::getParameter(std::string param) const
261 if (param == "onsettype") {
263 } else if (param == "peakpickthreshold") {
265 } else if (param == "silencethreshold") {
268 } else if (param == "minioi") {
277 Onset::setParameter(std::string param, float value)
279 if (param == "onsettype") {
280 switch (lrintf(value)) {
282 case 0: m_onsettype = OnsetEnergy; break;
283 case 1: m_onsettype = OnsetSpecDiff; break;
284 case 2: m_onsettype = OnsetHFC; break;
285 case 3: m_onsettype = OnsetComplex; break;
286 case 4: m_onsettype = OnsetPhase; break;
287 case 5: m_onsettype = OnsetKL; break;
288 case 6: m_onsettype = OnsetMKL; break;
289 case 7: m_onsettype = OnsetSpecFlux; break;
291 case 0: m_onsettype = aubio_onset_energy; break;
292 case 1: m_onsettype = aubio_onset_specdiff; break;
293 case 2: m_onsettype = aubio_onset_hfc; break;
294 case 3: m_onsettype = aubio_onset_complex; break;
295 case 4: m_onsettype = aubio_onset_phase; break;
296 case 5: m_onsettype = aubio_onset_kl; break;
297 case 6: m_onsettype = aubio_onset_mkl; break;
300 } else if (param == "peakpickthreshold") {
302 } else if (param == "silencethreshold") {
305 } else if (param == "minioi") {
312 Onset::getOutputDescriptors() const
317 d.identifier = "onsets";
320 d.hasFixedBinCount = true;
322 d.sampleType = OutputDescriptor::VariableSampleRate;
327 d = OutputDescriptor();
328 d.identifier = "detectionfunction";
329 d.name = "Onset Detection Function";
331 d.hasFixedBinCount = true;
332 d.binCount = m_channelCount;
333 d.hasKnownExtents = false;
334 d.isQuantized = false;
335 d.sampleType = OutputDescriptor::OneSamplePerStep;
342 Onset::process(const float *const *inputBuffers,
343 Vamp::RealTime timestamp)
346 for (size_t i = 0; i < m_stepSize; ++i) {
347 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
350 aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
352 bool isonset = m_onset->data[0];
354 for (size_t i = 0; i < m_stepSize; ++i) {
355 for (size_t j = 0; j < m_channelCount; ++j) {
356 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
360 aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
361 aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
363 bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
366 if (aubio_silence_detection(m_ibuf, m_silence)) {
372 FeatureSet returnFeatures;
375 if (timestamp - m_lastOnset >= m_delay) {
377 onsettime.hasTimestamp = true;
378 if (timestamp < m_delay) timestamp = m_delay;
379 onsettime.timestamp = timestamp - m_delay;
380 returnFeatures[0].push_back(onsettime);
381 m_lastOnset = timestamp;
386 for (size_t j = 0; j < m_channelCount; ++j) {
387 feature.values.push_back(m_onset->data[j][0]);
389 returnFeatures[1].push_back(feature);
392 return returnFeatures;
396 Onset::getRemainingFeatures()