1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6 An API for audio analysis and feature extraction plugins.
8 Centre for Digital Music, Queen Mary, University of London.
9 Copyright 2006 Chris Cannam.
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:
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
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.
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
38 #include <ardourext/float_cast.h>
40 #include "PercussionOnsetDetector.h"
50 PercussionOnsetDetector::PercussionOnsetDetector(float inputSampleRate) :
51 Plugin(inputSampleRate),
62 PercussionOnsetDetector::~PercussionOnsetDetector()
64 delete[] m_priorMagnitudes;
68 PercussionOnsetDetector::getIdentifier() const
70 return "percussiononsets";
74 PercussionOnsetDetector::getName() const
76 return "Simple Percussion Onset Detector";
80 PercussionOnsetDetector::getDescription() const
82 return "Detect percussive note onsets by identifying broadband energy rises";
86 PercussionOnsetDetector::getMaker() const
88 return "Vamp SDK Example Plugins";
92 PercussionOnsetDetector::getPluginVersion() const
98 PercussionOnsetDetector::getCopyright() const
100 return "Code copyright 2006 Queen Mary, University of London, after Dan Barry et al 2005. Freely redistributable (BSD license)";
104 PercussionOnsetDetector::getPreferredStepSize() const
110 PercussionOnsetDetector::getPreferredBlockSize() const
116 PercussionOnsetDetector::initialise(size_t channels, size_t stepSize, size_t blockSize)
118 if (channels < getMinChannelCount() ||
119 channels > getMaxChannelCount()) return false;
121 m_stepSize = stepSize;
122 m_blockSize = blockSize;
124 m_priorMagnitudes = new float[m_blockSize/2];
126 for (size_t i = 0; i < m_blockSize/2; ++i) {
127 m_priorMagnitudes[i] = 0.f;
137 PercussionOnsetDetector::reset()
139 for (size_t i = 0; i < m_blockSize/2; ++i) {
140 m_priorMagnitudes[i] = 0.f;
147 PercussionOnsetDetector::ParameterList
148 PercussionOnsetDetector::getParameterDescriptors() const
152 ParameterDescriptor d;
153 d.identifier = "threshold";
154 d.name = "Energy rise threshold";
155 d.description = "Energy rise within a frequency bin necessary to count toward broadband total";
160 d.isQuantized = false;
163 d.identifier = "sensitivity";
164 d.name = "Sensitivity";
165 d.description = "Sensitivity of peak detector applied to broadband detection function";
170 d.isQuantized = false;
177 PercussionOnsetDetector::getParameter(std::string id) const
179 if (id == "threshold") return m_threshold;
180 if (id == "sensitivity") return m_sensitivity;
185 PercussionOnsetDetector::setParameter(std::string id, float value)
187 if (id == "threshold") {
188 if (value < 0) value = 0;
189 if (value > 20) value = 20;
191 } else if (id == "sensitivity") {
192 if (value < 0) value = 0;
193 if (value > 100) value = 100;
194 m_sensitivity = value;
198 PercussionOnsetDetector::OutputList
199 PercussionOnsetDetector::getOutputDescriptors() const
204 d.identifier = "onsets";
206 d.description = "Percussive note onset locations";
208 d.hasFixedBinCount = true;
210 d.hasKnownExtents = false;
211 d.isQuantized = false;
212 d.sampleType = OutputDescriptor::VariableSampleRate;
213 d.sampleRate = m_inputSampleRate;
216 d.identifier = "detectionfunction";
217 d.name = "Detection Function";
218 d.description = "Broadband energy rise detection function";
220 d.isQuantized = true;
221 d.quantizeStep = 1.0;
222 d.sampleType = OutputDescriptor::OneSamplePerStep;
228 PercussionOnsetDetector::FeatureSet
229 PercussionOnsetDetector::process(const float *const *inputBuffers,
232 if (m_stepSize == 0) {
233 cerr << "ERROR: PercussionOnsetDetector::process: "
234 << "PercussionOnsetDetector has not been initialised"
241 for (size_t i = 1; i < m_blockSize/2; ++i) {
243 float real = inputBuffers[0][i*2];
244 float imag = inputBuffers[0][i*2 + 1];
246 float sqrmag = real * real + imag * imag;
248 if (m_priorMagnitudes[i] > 0.f) {
249 float diff = 10.f * log10f(sqrmag / m_priorMagnitudes[i]);
251 // std::cout << "i=" << i << ", mag=" << mag << ", prior=" << m_priorMagnitudes[i] << ", diff=" << diff << ", threshold=" << m_threshold << std::endl;
253 if (diff >= m_threshold) ++count;
256 m_priorMagnitudes[i] = sqrmag;
259 FeatureSet returnFeatures;
261 Feature detectionFunction;
262 detectionFunction.hasTimestamp = false;
263 detectionFunction.values.push_back(count);
264 returnFeatures[1].push_back(detectionFunction);
266 if (m_dfMinus2 < m_dfMinus1 &&
267 m_dfMinus1 >= count &&
268 m_dfMinus1 > ((100 - m_sensitivity) * m_blockSize) / 200) {
271 onset.hasTimestamp = true;
272 onset.timestamp = ts - Vamp::RealTime::frame2RealTime
273 (m_stepSize, lrintf(m_inputSampleRate));
274 returnFeatures[0].push_back(onset);
277 m_dfMinus2 = m_dfMinus1;
280 return returnFeatures;
283 PercussionOnsetDetector::FeatureSet
284 PercussionOnsetDetector::getRemainingFeatures()