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