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;
173 Onset::getPreferredStepSize() const
179 Onset::getPreferredBlockSize() const
181 return 2 * getPreferredStepSize();
185 Onset::getParameterDescriptors() const
189 ParameterDescriptor desc;
190 desc.identifier = "onsettype";
191 desc.name = "Onset Detection Function Type";
195 desc.defaultValue = (int)OnsetComplex;
198 desc.defaultValue = (int)aubio_onset_complex;
200 desc.isQuantized = true;
201 desc.quantizeStep = 1;
202 desc.valueNames.push_back("Energy Based");
203 desc.valueNames.push_back("Spectral Difference");
204 desc.valueNames.push_back("High-Frequency Content");
205 desc.valueNames.push_back("Complex Domain");
206 desc.valueNames.push_back("Phase Deviation");
207 desc.valueNames.push_back("Kullback-Liebler");
208 desc.valueNames.push_back("Modified Kullback-Liebler");
210 desc.valueNames.push_back("Spectral Flux");
212 list.push_back(desc);
214 desc = ParameterDescriptor();
215 desc.identifier = "peakpickthreshold";
216 desc.name = "Peak Picker Threshold";
219 desc.defaultValue = 0.3;
220 desc.isQuantized = false;
221 list.push_back(desc);
223 desc = ParameterDescriptor();
224 desc.identifier = "silencethreshold";
225 desc.name = "Silence Threshold";
226 desc.minValue = -120;
229 desc.defaultValue = -70;
231 desc.defaultValue = -90;
234 desc.isQuantized = false;
235 list.push_back(desc);
238 desc = ParameterDescriptor();
239 desc.identifier = "minioi";
240 desc.name = "Minimum Inter-Onset Interval";
243 desc.defaultValue = 4;
245 desc.isQuantized = true;
246 desc.quantizeStep = 1;
247 list.push_back(desc);
253 Onset::getParameter(std::string param) const
255 if (param == "onsettype") {
257 } else if (param == "peakpickthreshold") {
259 } else if (param == "silencethreshold") {
262 } else if (param == "minioi") {
271 Onset::setParameter(std::string param, float value)
273 if (param == "onsettype") {
274 switch (lrintf(value)) {
276 case 0: m_onsettype = OnsetEnergy; break;
277 case 1: m_onsettype = OnsetSpecDiff; break;
278 case 2: m_onsettype = OnsetHFC; break;
279 case 3: m_onsettype = OnsetComplex; break;
280 case 4: m_onsettype = OnsetPhase; break;
281 case 5: m_onsettype = OnsetKL; break;
282 case 6: m_onsettype = OnsetMKL; break;
283 case 7: m_onsettype = OnsetSpecFlux; break;
285 case 0: m_onsettype = aubio_onset_energy; break;
286 case 1: m_onsettype = aubio_onset_specdiff; break;
287 case 2: m_onsettype = aubio_onset_hfc; break;
288 case 3: m_onsettype = aubio_onset_complex; break;
289 case 4: m_onsettype = aubio_onset_phase; break;
290 case 5: m_onsettype = aubio_onset_kl; break;
291 case 6: m_onsettype = aubio_onset_mkl; break;
294 } else if (param == "peakpickthreshold") {
296 } else if (param == "silencethreshold") {
299 } else if (param == "minioi") {
306 Onset::getOutputDescriptors() const
311 d.identifier = "onsets";
314 d.hasFixedBinCount = true;
316 d.sampleType = OutputDescriptor::VariableSampleRate;
321 d = OutputDescriptor();
322 d.identifier = "detectionfunction";
323 d.name = "Onset Detection Function";
325 d.hasFixedBinCount = true;
326 d.binCount = m_channelCount;
327 d.hasKnownExtents = false;
328 d.isQuantized = false;
329 d.sampleType = OutputDescriptor::OneSamplePerStep;
336 Onset::process(const float *const *inputBuffers,
337 Vamp::RealTime timestamp)
340 for (size_t i = 0; i < m_stepSize; ++i) {
341 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
344 aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
346 bool isonset = m_onset->data[0];
348 for (size_t i = 0; i < m_stepSize; ++i) {
349 for (size_t j = 0; j < m_channelCount; ++j) {
350 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
354 aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
355 aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
357 bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
360 if (aubio_silence_detection(m_ibuf, m_silence)) {
366 FeatureSet returnFeatures;
369 if (timestamp - m_lastOnset >= m_delay) {
371 onsettime.hasTimestamp = true;
372 if (timestamp < m_delay) timestamp = m_delay;
373 onsettime.timestamp = timestamp - m_delay;
374 returnFeatures[0].push_back(onsettime);
375 m_lastOnset = timestamp;
380 for (size_t j = 0; j < m_channelCount; ++j) {
381 feature.values.push_back(m_onset->data[j][0]);
383 returnFeatures[1].push_back(feature);
386 return returnFeatures;
390 Onset::getRemainingFeatures()