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