annotate src/vamp-plugin-sdk-2.5/examples/PercussionOnsetDetector.cpp @ 169:223a55898ab9 tip default

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