annotate examples/PercussionOnsetDetector.cpp @ 47:be8fdfe25693

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