fix crash when copy'ing latent plugins
[ardour.git] / libs / vamp-plugins / Onset.cpp
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4     Vamp feature extraction plugins using Paul Brossier's Aubio library.
5
6     Centre for Digital Music, Queen Mary, University of London.
7     This file copyright 2006 Chris Cannam.
8
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.
14
15 */
16
17 #ifdef COMPILER_MSVC
18 #include <ardourext/float_cast.h>
19 #endif
20 #include <math.h>
21 #include "Onset.h"
22
23 using std::string;
24 using std::vector;
25 using std::cerr;
26 using std::endl;
27
28 #ifdef HAVE_AUBIO4
29 const char *getAubioNameForOnsetType(OnsetType t)
30 {
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"
34     };
35     return names[(int)t];
36 }
37 #endif
38
39 Onset::Onset(float inputSampleRate) :
40     Plugin(inputSampleRate),
41     m_ibuf(0),
42     m_onset(0),
43 #ifdef HAVE_AUBIO4
44     m_onsetdet(0),
45     m_onsettype(OnsetComplex),
46     m_minioi(4),
47     m_silence(-70),
48 #else
49     m_fftgrain(0),
50     m_pv(0),
51     m_peakpick(0),
52     m_onsetdet(0),
53     m_onsettype(aubio_onset_complex),
54     m_channelCount(1),
55     m_silence(-90),
56 #endif
57     m_threshold(0.3)
58 {
59 }
60
61 Onset::~Onset()
62 {
63 #ifdef HAVE_AUBIO4
64     if (m_onsetdet) del_aubio_onset(m_onsetdet);
65 #else
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);
70 #endif
71     if (m_ibuf) del_fvec(m_ibuf);
72     if (m_onset) del_fvec(m_onset);
73 }
74
75 string
76 Onset::getIdentifier() const
77 {
78     return "aubioonset";
79 }
80
81 string
82 Onset::getName() const
83 {
84     return "Aubio Onset Detector";
85 }
86
87 string
88 Onset::getDescription() const
89 {
90     return "Estimate note onset times";
91 }
92
93 string
94 Onset::getMaker() const
95 {
96     return "Paul Brossier (plugin by Chris Cannam)";
97 }
98
99 int
100 Onset::getPluginVersion() const
101 {
102 #ifdef HAVE_AUBIO4
103     return 2;
104 #else
105     return 1;
106 #endif
107 }
108
109 string
110 Onset::getCopyright() const
111 {
112     return "GPL";
113 }
114
115 bool
116 Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
117 {
118     m_stepSize = stepSize;
119     m_blockSize = blockSize;
120
121 #ifdef HAVE_AUBIO4
122     if (channels != 1) {
123         std::cerr << "Onset::initialise: channels must be 1" << std::endl;
124         return false;
125     }
126     m_ibuf = new_fvec(stepSize);
127     m_onset = new_fvec(1);
128     reset();
129 #else
130     m_channelCount = channels;
131
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);
137
138     m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
139
140     m_delay = Vamp::RealTime::frame2RealTime(4 * stepSize,
141                                              lrintf(m_inputSampleRate));
142
143     m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
144
145 #endif
146     return true;
147 }
148
149 void
150 Onset::reset()
151 {
152 #ifdef HAVE_AUBIO4
153     if (m_onsetdet) del_aubio_onset(m_onsetdet);
154
155     m_onsetdet = new_aubio_onset
156         (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
157          m_blockSize,
158          m_stepSize,
159          lrintf(m_inputSampleRate));
160
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);
164
165     m_delay = Vamp::RealTime::frame2RealTime(4 * m_stepSize,
166                                              lrintf(m_inputSampleRate));
167
168     m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
169 #else
170     if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
171     if (m_peakpick) del_aubio_peakpicker(m_peakpick);
172
173     m_peakpick = new_aubio_peakpicker(m_threshold);
174     m_onsetdet = new_aubio_onsetdetection(m_onsettype, m_blockSize, m_channelCount);
175 #endif
176 }
177
178 size_t
179 Onset::getPreferredStepSize() const
180 {
181     return 512;
182 }
183
184 size_t
185 Onset::getPreferredBlockSize() const
186 {
187     return 2 * getPreferredStepSize();
188 }
189
190 Onset::ParameterList
191 Onset::getParameterDescriptors() const
192 {
193     ParameterList list;
194
195     ParameterDescriptor desc;
196     desc.identifier = "onsettype";
197     desc.name = "Onset Detection Function Type";
198     desc.minValue = 0;
199 #ifdef HAVE_AUBIO4
200     desc.maxValue = 7;
201     desc.defaultValue = (int)OnsetComplex;
202 #else
203     desc.maxValue = 6;
204     desc.defaultValue = (int)aubio_onset_complex;
205 #endif
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");
215 #ifdef HAVE_AUBIO4
216     desc.valueNames.push_back("Spectral Flux");
217 #endif
218     list.push_back(desc);
219
220     desc = ParameterDescriptor();
221     desc.identifier = "peakpickthreshold";
222     desc.name = "Peak Picker Threshold";
223     desc.minValue = 0;
224     desc.maxValue = 1;
225     desc.defaultValue = 0.3;
226     desc.isQuantized = false;
227     list.push_back(desc);
228
229     desc = ParameterDescriptor();
230     desc.identifier = "silencethreshold";
231     desc.name = "Silence Threshold";
232     desc.minValue = -120;
233     desc.maxValue = 0;
234 #ifdef HAVE_AUBIO4
235     desc.defaultValue = -70;
236 #else
237     desc.defaultValue = -90;
238 #endif
239     desc.unit = "dB";
240     desc.isQuantized = false;
241     list.push_back(desc);
242
243 #ifdef HAVE_AUBIO4
244     desc = ParameterDescriptor();
245     desc.identifier = "minioi";
246     desc.name = "Minimum Inter-Onset Interval";
247     desc.minValue = 0;
248     desc.maxValue = 40;
249     desc.defaultValue = 4;
250     desc.unit = "ms";
251     desc.isQuantized = true;
252     desc.quantizeStep = 1;
253     list.push_back(desc);
254 #endif
255     return list;
256 }
257
258 float
259 Onset::getParameter(std::string param) const
260 {
261     if (param == "onsettype") {
262         return m_onsettype;
263     } else if (param == "peakpickthreshold") {
264         return m_threshold;
265     } else if (param == "silencethreshold") {
266         return m_silence;
267 #ifdef HAVE_AUBIO4
268     } else if (param == "minioi") {
269         return m_minioi;
270 #endif
271     } else {
272         return 0.0;
273     }
274 }
275
276 void
277 Onset::setParameter(std::string param, float value)
278 {
279     if (param == "onsettype") {
280         switch (lrintf(value)) {
281 #ifdef HAVE_AUBIO4
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;
290 #else
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;
298 #endif
299         }
300     } else if (param == "peakpickthreshold") {
301         m_threshold = value;
302     } else if (param == "silencethreshold") {
303         m_silence = value;
304 #ifdef HAVE_AUBIO4
305     } else if (param == "minioi") {
306         m_minioi = value;
307 #endif
308     }
309 }
310
311 Onset::OutputList
312 Onset::getOutputDescriptors() const
313 {
314     OutputList list;
315
316     OutputDescriptor d;
317     d.identifier = "onsets";
318     d.name = "Onsets";
319     d.unit = "";
320     d.hasFixedBinCount = true;
321     d.binCount = 0;
322     d.sampleType = OutputDescriptor::VariableSampleRate;
323     d.sampleRate = 0;
324     list.push_back(d);
325
326 #ifndef HAVE_AUBIO4
327     d = OutputDescriptor();
328     d.identifier = "detectionfunction";
329     d.name = "Onset Detection Function";
330     d.unit = "";
331     d.hasFixedBinCount = true;
332     d.binCount = m_channelCount;
333     d.hasKnownExtents = false;
334     d.isQuantized = false;
335     d.sampleType = OutputDescriptor::OneSamplePerStep;
336     list.push_back(d);
337 #endif
338     return list;
339 }
340
341 Onset::FeatureSet
342 Onset::process(const float *const *inputBuffers,
343                Vamp::RealTime timestamp)
344 {
345 #ifdef HAVE_AUBIO4
346     for (size_t i = 0; i < m_stepSize; ++i) {
347         fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
348     }
349
350     aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
351
352     bool isonset = m_onset->data[0];
353 #else
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);
357         }
358     }
359
360     aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
361     aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
362
363     bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
364
365     if (isonset) {
366         if (aubio_silence_detection(m_ibuf, m_silence)) {
367             isonset = false;
368         }
369     }
370 #endif
371
372     FeatureSet returnFeatures;
373
374     if (isonset) {
375         if (timestamp - m_lastOnset >= m_delay) {
376             Feature onsettime;
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;
382         }
383     }
384 #ifndef HAVE_AUBIO4
385     Feature feature;
386     for (size_t j = 0; j < m_channelCount; ++j) {
387         feature.values.push_back(m_onset->data[j][0]);
388     }
389     returnFeatures[1].push_back(feature);
390 #endif
391
392     return returnFeatures;
393 }
394
395 Onset::FeatureSet
396 Onset::getRemainingFeatures()
397 {
398     return FeatureSet();
399 }
400