comparison examples/PercussionOnsetDetector.cpp @ 35:154f86cb8c99

* Add an implementation of Dan Barry's percussion onset detector
author cannam
date Wed, 20 Sep 2006 13:51:22 +0000
parents
children 6891b25dca1a
comparison
equal deleted inserted replaced
34:cfba7059eccf 35:154f86cb8c99
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Vamp
5
6 An API for audio analysis and feature extraction plugins.
7
8 Centre for Digital Music, Queen Mary, University of London.
9 Copyright 2006 Chris Cannam.
10
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation
13 files (the "Software"), to deal in the Software without
14 restriction, including without limitation the rights to use, copy,
15 modify, merge, publish, distribute, sublicense, and/or sell copies
16 of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 Except as contained in this notice, the names of the Centre for
31 Digital Music; Queen Mary, University of London; and Chris Cannam
32 shall not be used in advertising or otherwise to promote the sale,
33 use or other dealings in this Software without prior written
34 authorization.
35 */
36
37 #include "PercussionOnsetDetector.h"
38
39 using std::string;
40 using std::vector;
41 using std::cerr;
42 using std::endl;
43
44 #include <cmath>
45
46
47 PercussionOnsetDetector::PercussionOnsetDetector(float inputSampleRate) :
48 Plugin(inputSampleRate),
49 m_stepSize(0),
50 m_blockSize(0),
51 m_threshold(3),
52 m_sensitivity(40),
53 m_priorMagnitudes(0),
54 m_dfMinus1(0),
55 m_dfMinus2(0)
56 {
57 }
58
59 PercussionOnsetDetector::~PercussionOnsetDetector()
60 {
61 delete[] m_priorMagnitudes;
62 }
63
64 string
65 PercussionOnsetDetector::getName() const
66 {
67 return "percussiononsets";
68 }
69
70 string
71 PercussionOnsetDetector::getDescription() const
72 {
73 return "Simple Percussion Onset Detector";
74 }
75
76 string
77 PercussionOnsetDetector::getMaker() const
78 {
79 return "Queen Mary, University of London";
80 }
81
82 int
83 PercussionOnsetDetector::getPluginVersion() const
84 {
85 return 2;
86 }
87
88 string
89 PercussionOnsetDetector::getCopyright() const
90 {
91 return "Code copyright 2006 Queen Mary, University of London, after Dan Barry et al 2005. Freely redistributable (BSD license)";
92 }
93
94 size_t
95 PercussionOnsetDetector::getPreferredStepSize() const
96 {
97 return 0;
98 }
99
100 size_t
101 PercussionOnsetDetector::getPreferredBlockSize() const
102 {
103 return 1024;
104 }
105
106 bool
107 PercussionOnsetDetector::initialise(size_t channels, size_t stepSize, size_t blockSize)
108 {
109 if (channels < getMinChannelCount() ||
110 channels > getMaxChannelCount()) return false;
111
112 m_stepSize = stepSize;
113 m_blockSize = blockSize;
114
115 m_priorMagnitudes = new float[m_blockSize/2];
116
117 for (size_t i = 0; i < m_blockSize/2; ++i) {
118 m_priorMagnitudes[i] = 0.f;
119 }
120
121 m_dfMinus1 = 0.f;
122 m_dfMinus2 = 0.f;
123
124 return true;
125 }
126
127 void
128 PercussionOnsetDetector::reset()
129 {
130 for (size_t i = 0; i < m_blockSize/2; ++i) {
131 m_priorMagnitudes[i] = 0.f;
132 }
133
134 m_dfMinus1 = 0.f;
135 m_dfMinus2 = 0.f;
136 }
137
138 PercussionOnsetDetector::ParameterList
139 PercussionOnsetDetector::getParameterDescriptors() const
140 {
141 ParameterList list;
142
143 ParameterDescriptor d;
144 d.name = "threshold";
145 d.description = "Broadband energy rise threshold";
146 d.unit = "dB";
147 d.minValue = 0;
148 d.maxValue = 20;
149 d.defaultValue = 3;
150 d.isQuantized = false;
151 list.push_back(d);
152
153 d.name = "sensitivity";
154 d.description = "Peak detection sensitivity";
155 d.unit = "%";
156 d.minValue = 0;
157 d.maxValue = 100;
158 d.defaultValue = 40;
159 d.isQuantized = false;
160 list.push_back(d);
161
162 return list;
163 }
164
165 float
166 PercussionOnsetDetector::getParameter(std::string name) const
167 {
168 if (name == "threshold") return m_threshold;
169 if (name == "sensitivity") return m_sensitivity;
170 return 0.f;
171 }
172
173 void
174 PercussionOnsetDetector::setParameter(std::string name, float value)
175 {
176 if (name == "threshold") {
177 if (value < 0) value = 0;
178 if (value > 20) value = 20;
179 m_threshold = value;
180 } else if (name == "sensitivity") {
181 if (value < 0) value = 0;
182 if (value > 100) value = 100;
183 m_sensitivity = value;
184 }
185 }
186
187 PercussionOnsetDetector::OutputList
188 PercussionOnsetDetector::getOutputDescriptors() const
189 {
190 OutputList list;
191
192 OutputDescriptor d;
193 d.name = "onsets";
194 d.unit = "";
195 d.description = "Onsets";
196 d.hasFixedBinCount = true;
197 d.binCount = 0;
198 d.hasKnownExtents = false;
199 d.isQuantized = false;
200 d.sampleType = OutputDescriptor::VariableSampleRate;
201 d.sampleRate = m_inputSampleRate;
202 list.push_back(d);
203
204 d.name = "detectionfunction";
205 d.description = "Onset Detection Function";
206 d.binCount = 1;
207 d.isQuantized = true;
208 d.quantizeStep = 1.0;
209 d.sampleType = OutputDescriptor::OneSamplePerStep;
210 list.push_back(d);
211
212 return list;
213 }
214
215 PercussionOnsetDetector::FeatureSet
216 PercussionOnsetDetector::process(float **inputBuffers, Vamp::RealTime ts)
217 {
218 if (m_stepSize == 0) {
219 cerr << "ERROR: PercussionOnsetDetector::process: "
220 << "PercussionOnsetDetector has not been initialised"
221 << endl;
222 return FeatureSet();
223 }
224
225 int count = 0;
226
227 for (size_t i = 1; i < m_blockSize/2; ++i) {
228
229 float real = inputBuffers[0][i*2];
230 float imag = inputBuffers[0][i*2 + 1];
231 float sqrmag = real * real + imag * imag;
232
233 if (m_priorMagnitudes[i] > 0.f) {
234 float diff = 10.f * log10f(sqrmag / m_priorMagnitudes[i]);
235
236 // std::cout << "i=" << i << ", mag=" << mag << ", prior=" << m_priorMagnitudes[i] << ", diff=" << diff << ", threshold=" << m_threshold << std::endl;
237
238 if (diff >= m_threshold) ++count;
239 }
240
241 m_priorMagnitudes[i] = sqrmag;
242 }
243
244 FeatureSet returnFeatures;
245
246 Feature detectionFunction;
247 detectionFunction.hasTimestamp = false;
248 detectionFunction.values.push_back(count);
249 returnFeatures[1].push_back(detectionFunction);
250
251 if (m_dfMinus2 < m_dfMinus1 &&
252 m_dfMinus1 >= count &&
253 m_dfMinus1 > (m_sensitivity * m_blockSize) / 200) {
254
255 Feature onset;
256 onset.hasTimestamp = true;
257 onset.timestamp = ts - Vamp::RealTime::frame2RealTime
258 (m_stepSize, m_inputSampleRate);
259 returnFeatures[0].push_back(onset);
260 }
261
262 m_dfMinus2 = m_dfMinus1;
263 m_dfMinus1 = count;
264
265 return returnFeatures;
266 }
267
268 PercussionOnsetDetector::FeatureSet
269 PercussionOnsetDetector::getRemainingFeatures()
270 {
271 return FeatureSet();
272 }
273