set negative timestamps to zero
[vamp-aubio-plugins.git] / 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 #include <math.h>
18 #include "Onset.h"
19
20 using std::string;
21 using std::vector;
22 using std::cerr;
23 using std::endl;
24
25 Onset::Onset(float inputSampleRate) :
26     Plugin(inputSampleRate),
27     m_ibuf(0),
28     m_fftgrain(0),
29     m_onset(0),
30     m_pv(0),
31     m_peakpick(0),
32     m_onsetdet(0),
33     m_onsettype(aubio_onset_complex),
34     m_threshold(0.3),
35     m_silence(-90),
36     m_channelCount(1)
37 {
38 }
39
40 Onset::~Onset()
41 {
42     if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
43     if (m_ibuf) del_fvec(m_ibuf);
44     if (m_onset) del_fvec(m_onset);
45     if (m_fftgrain) del_cvec(m_fftgrain);
46     if (m_pv) del_aubio_pvoc(m_pv);
47     if (m_peakpick) del_aubio_peakpicker(m_peakpick);
48 }
49
50 string
51 Onset::getName() const
52 {
53     return "aubioonset";
54 }
55
56 string
57 Onset::getDescription() const
58 {
59     return "Aubio Onset Detector";
60 }
61
62 string
63 Onset::getMaker() const
64 {
65     return "Paul Brossier (plugin by Chris Cannam)";
66 }
67
68 int
69 Onset::getPluginVersion() const
70 {
71     return 1;
72 }
73
74 string
75 Onset::getCopyright() const
76 {
77     return "GPL";
78 }
79
80 bool
81 Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
82 {
83     m_channelCount = channels;
84     m_stepSize = stepSize;
85     m_blockSize = blockSize;
86
87     m_ibuf = new_fvec(stepSize, channels);
88     m_onset = new_fvec(1, channels);
89     m_fftgrain = new_cvec(blockSize, channels);
90     m_pv = new_aubio_pvoc(blockSize, stepSize, channels);
91     m_peakpick = new_aubio_peakpicker(m_threshold);
92
93     m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
94     
95     m_delay = Vamp::RealTime::frame2RealTime(4 * stepSize,
96                                              lrintf(m_inputSampleRate));
97
98     m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
99
100     return true;
101 }
102
103 void
104 Onset::reset()
105 {
106 }
107
108 size_t
109 Onset::getPreferredStepSize() const
110 {
111     return 512;
112 }
113
114 size_t
115 Onset::getPreferredBlockSize() const
116 {
117     return 2 * getPreferredStepSize();
118 }
119
120 Onset::ParameterList
121 Onset::getParameterDescriptors() const
122 {
123     ParameterList list;
124     
125     ParameterDescriptor desc;
126     desc.name = "onsettype";
127     desc.description = "Onset Detection Function Type";
128     desc.minValue = 0;
129     desc.maxValue = 6;
130     desc.defaultValue = (int)aubio_onset_complex;
131     desc.isQuantized = true;
132     desc.quantizeStep = 1;
133     desc.valueNames.push_back("Energy Based");
134     desc.valueNames.push_back("Spectral Difference");
135     desc.valueNames.push_back("High-Frequency Content");
136     desc.valueNames.push_back("Complex Domain");
137     desc.valueNames.push_back("Phase Deviation");
138     desc.valueNames.push_back("Kullback-Liebler");
139     desc.valueNames.push_back("Modified Kullback-Liebler");
140     list.push_back(desc);
141
142     desc = ParameterDescriptor();
143     desc.name = "peakpickthreshold";
144     desc.description = "Peak Picker Threshold";
145     desc.minValue = 0;
146     desc.maxValue = 1;
147     desc.defaultValue = 0.3;
148     desc.isQuantized = false;
149     list.push_back(desc);
150
151     desc = ParameterDescriptor();
152     desc.name = "silencethreshold";
153     desc.description = "Silence Threshold";
154     desc.minValue = -120;
155     desc.maxValue = 0;
156     desc.defaultValue = -90;
157     desc.unit = "dB";
158     desc.isQuantized = false;
159     list.push_back(desc);
160
161     return list;
162 }
163
164 float
165 Onset::getParameter(std::string param) const
166 {
167     if (param == "onsettype") {
168         return m_onsettype;
169     } else if (param == "peakpickthreshold") {
170         return m_threshold;
171     } else if (param == "silencethreshold") {
172         return m_silence;
173     } else {
174         return 0.0;
175     }
176 }
177
178 void
179 Onset::setParameter(std::string param, float value)
180 {
181     if (param == "onsettype") {
182         switch (lrintf(value)) {
183         case 0: m_onsettype = aubio_onset_energy; break;
184         case 1: m_onsettype = aubio_onset_specdiff; break;
185         case 2: m_onsettype = aubio_onset_hfc; break;
186         case 3: m_onsettype = aubio_onset_complex; break;
187         case 4: m_onsettype = aubio_onset_phase; break;
188         case 5: m_onsettype = aubio_onset_kl; break;
189         case 6: m_onsettype = aubio_onset_mkl; break;
190         }
191     } else if (param == "peakpickthreshold") {
192         m_threshold = value;
193     } else if (param == "silencethreshold") {
194         m_silence = value;
195     }
196 }
197
198 Onset::OutputList
199 Onset::getOutputDescriptors() const
200 {
201     OutputList list;
202
203     OutputDescriptor d;
204     d.name = "onsets";
205     d.unit = "";
206     d.description = "Onsets";
207     d.hasFixedBinCount = true;
208     d.binCount = 0;
209     d.sampleType = OutputDescriptor::VariableSampleRate;
210     d.sampleRate = 0;
211     list.push_back(d);
212
213     d = OutputDescriptor();
214     d.name = "detectionfunction";
215     d.unit = "";
216     d.description = "Onset Detection Function";
217     d.hasFixedBinCount = true;
218     d.binCount = m_channelCount;
219     d.hasKnownExtents = false;
220     d.isQuantized = false;
221     d.sampleType = OutputDescriptor::OneSamplePerStep;
222     list.push_back(d);
223
224     return list;
225 }
226
227 Onset::FeatureSet
228 Onset::process(float **inputBuffers, Vamp::RealTime timestamp)
229 {
230     for (size_t i = 0; i < m_stepSize; ++i) {
231         for (size_t j = 0; j < m_channelCount; ++j) {
232             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
233         }
234     }
235
236     aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
237     aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
238
239     bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
240
241     if (isonset) {
242         if (aubio_silence_detection(m_ibuf, m_silence)) {
243             isonset = false;
244         }
245     }
246
247     FeatureSet returnFeatures;
248
249     if (isonset) {
250         if (timestamp - m_lastOnset >= m_delay) {
251             Feature onsettime;
252             onsettime.hasTimestamp = true;
253             if (timestamp < m_delay) timestamp = m_delay;
254             onsettime.timestamp = timestamp - m_delay;
255             returnFeatures[0].push_back(onsettime);
256             m_lastOnset = timestamp;
257         }
258     }
259     Feature feature;
260     for (size_t j = 0; j < m_channelCount; ++j) {
261         feature.values.push_back(m_onset->data[j][0]);
262     }
263     returnFeatures[1].push_back(feature);
264
265     return returnFeatures;
266 }
267
268 Onset::FeatureSet
269 Onset::getRemainingFeatures()
270 {
271     return FeatureSet();
272 }
273