annotate plugins/Onset.cpp @ 71:649c343a4b94

plugins/Onset.cpp: added onset detection function and threshold odf
author Paul Brossier <piem@piem.org>
date Wed, 28 Jan 2015 13:27:52 +0100
parents 757e99172fa0
children f80b207ccd15
rev   line source
cannam@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@0 2
cannam@0 3 /*
cannam@0 4 Vamp feature extraction plugins using Paul Brossier's Aubio library.
cannam@0 5
cannam@0 6 Centre for Digital Music, Queen Mary, University of London.
cannam@0 7 This file copyright 2006 Chris Cannam.
cannam@0 8
cannam@0 9 This program is free software; you can redistribute it and/or
cannam@0 10 modify it under the terms of the GNU General Public License as
cannam@0 11 published by the Free Software Foundation; either version 2 of the
cannam@0 12 License, or (at your option) any later version. See the file
cannam@0 13 COPYING included with this distribution for more information.
cannam@0 14
cannam@0 15 */
cannam@0 16
piem@2 17 #include <math.h>
cannam@0 18 #include "Onset.h"
cannam@0 19
cannam@0 20 using std::string;
cannam@0 21 using std::vector;
cannam@0 22 using std::cerr;
cannam@0 23 using std::endl;
cannam@0 24
cannam@0 25 Onset::Onset(float inputSampleRate) :
cannam@0 26 Plugin(inputSampleRate),
cannam@0 27 m_ibuf(0),
cannam@0 28 m_onset(0),
cannam@0 29 m_onsetdet(0),
cannam@33 30 m_onsettype(OnsetComplex),
cannam@0 31 m_threshold(0.3),
cannam@34 32 m_silence(-70),
cannam@34 33 m_minioi(4)
cannam@0 34 {
cannam@0 35 }
cannam@0 36
cannam@0 37 Onset::~Onset()
cannam@0 38 {
cannam@33 39 if (m_onsetdet) del_aubio_onset(m_onsetdet);
cannam@0 40 if (m_ibuf) del_fvec(m_ibuf);
cannam@0 41 if (m_onset) del_fvec(m_onset);
cannam@0 42 }
cannam@0 43
cannam@0 44 string
cannam@13 45 Onset::getIdentifier() const
cannam@0 46 {
cannam@0 47 return "aubioonset";
cannam@0 48 }
cannam@0 49
cannam@0 50 string
cannam@13 51 Onset::getName() const
cannam@13 52 {
cannam@13 53 return "Aubio Onset Detector";
cannam@13 54 }
cannam@13 55
cannam@13 56 string
cannam@0 57 Onset::getDescription() const
cannam@0 58 {
cannam@13 59 return "Estimate note onset times";
cannam@0 60 }
cannam@0 61
cannam@0 62 string
cannam@0 63 Onset::getMaker() const
cannam@0 64 {
cannam@0 65 return "Paul Brossier (plugin by Chris Cannam)";
cannam@0 66 }
cannam@0 67
cannam@0 68 int
cannam@0 69 Onset::getPluginVersion() const
cannam@0 70 {
cannam@31 71 return 2;
cannam@0 72 }
cannam@0 73
cannam@0 74 string
cannam@0 75 Onset::getCopyright() const
cannam@0 76 {
cannam@0 77 return "GPL";
cannam@0 78 }
cannam@0 79
cannam@0 80 bool
cannam@0 81 Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@0 82 {
cannam@32 83 if (channels != 1) {
cannam@32 84 std::cerr << "Onset::initialise: channels must be 1" << std::endl;
cannam@32 85 return false;
cannam@32 86 }
cannam@32 87
cannam@0 88 m_stepSize = stepSize;
cannam@0 89 m_blockSize = blockSize;
cannam@0 90
cannam@32 91 m_ibuf = new_fvec(stepSize);
cannam@32 92 m_onset = new_fvec(1);
cannam@0 93
cannam@37 94 reset();
cannam@0 95
cannam@0 96 return true;
cannam@0 97 }
cannam@0 98
cannam@0 99 void
cannam@0 100 Onset::reset()
cannam@0 101 {
cannam@37 102 if (m_onsetdet) del_aubio_onset(m_onsetdet);
cannam@37 103
cannam@37 104 m_onsetdet = new_aubio_onset
cannam@37 105 (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
cannam@37 106 m_blockSize,
cannam@37 107 m_stepSize,
cannam@37 108 lrintf(m_inputSampleRate));
cannam@37 109
cannam@37 110 aubio_onset_set_threshold(m_onsetdet, m_threshold);
cannam@37 111 aubio_onset_set_silence(m_onsetdet, m_silence);
cannam@37 112 aubio_onset_set_minioi(m_onsetdet, m_minioi);
cannam@37 113
cannam@37 114 m_delay = Vamp::RealTime::frame2RealTime(4 * m_stepSize,
cannam@37 115 lrintf(m_inputSampleRate));
cannam@37 116
cannam@37 117 m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
cannam@0 118 }
cannam@0 119
cannam@0 120 size_t
cannam@0 121 Onset::getPreferredStepSize() const
cannam@0 122 {
piem@2 123 return 512;
cannam@0 124 }
cannam@0 125
cannam@0 126 size_t
cannam@0 127 Onset::getPreferredBlockSize() const
cannam@0 128 {
cannam@3 129 return 2 * getPreferredStepSize();
cannam@0 130 }
cannam@0 131
cannam@0 132 Onset::ParameterList
cannam@0 133 Onset::getParameterDescriptors() const
cannam@0 134 {
cannam@0 135 ParameterList list;
cannam@0 136
cannam@0 137 ParameterDescriptor desc;
cannam@13 138 desc.identifier = "onsettype";
cannam@13 139 desc.name = "Onset Detection Function Type";
piem@64 140 desc.description = "Type of onset detection function to use";
cannam@0 141 desc.minValue = 0;
cannam@34 142 desc.maxValue = 7;
cannam@33 143 desc.defaultValue = (int)OnsetComplex;
cannam@0 144 desc.isQuantized = true;
cannam@0 145 desc.quantizeStep = 1;
cannam@0 146 desc.valueNames.push_back("Energy Based");
cannam@0 147 desc.valueNames.push_back("Spectral Difference");
cannam@0 148 desc.valueNames.push_back("High-Frequency Content");
cannam@0 149 desc.valueNames.push_back("Complex Domain");
cannam@0 150 desc.valueNames.push_back("Phase Deviation");
cannam@0 151 desc.valueNames.push_back("Kullback-Liebler");
cannam@0 152 desc.valueNames.push_back("Modified Kullback-Liebler");
cannam@34 153 desc.valueNames.push_back("Spectral Flux");
cannam@0 154 list.push_back(desc);
cannam@0 155
cannam@0 156 desc = ParameterDescriptor();
cannam@13 157 desc.identifier = "peakpickthreshold";
cannam@13 158 desc.name = "Peak Picker Threshold";
piem@64 159 desc.description = "Threshold used for peak picking, the higher the more detections";
cannam@0 160 desc.minValue = 0;
cannam@0 161 desc.maxValue = 1;
cannam@0 162 desc.defaultValue = 0.3;
cannam@0 163 desc.isQuantized = false;
cannam@0 164 list.push_back(desc);
cannam@0 165
cannam@0 166 desc = ParameterDescriptor();
cannam@13 167 desc.identifier = "silencethreshold";
cannam@13 168 desc.name = "Silence Threshold";
piem@64 169 desc.description = "Silence threshold, the higher the least detection";
cannam@0 170 desc.minValue = -120;
cannam@0 171 desc.maxValue = 0;
cannam@34 172 desc.defaultValue = -70;
cannam@0 173 desc.unit = "dB";
cannam@0 174 desc.isQuantized = false;
cannam@0 175 list.push_back(desc);
cannam@0 176
cannam@34 177 desc = ParameterDescriptor();
cannam@34 178 desc.identifier = "minioi";
cannam@34 179 desc.name = "Minimum Inter-Onset Interval";
piem@64 180 desc.description = "Time interval below which two consecutive onsets should be merged";
cannam@34 181 desc.minValue = 0;
cannam@34 182 desc.maxValue = 40;
cannam@34 183 desc.defaultValue = 4;
cannam@34 184 desc.unit = "ms";
cannam@34 185 desc.isQuantized = true;
cannam@34 186 desc.quantizeStep = 1;
cannam@34 187 list.push_back(desc);
cannam@34 188
cannam@0 189 return list;
cannam@0 190 }
cannam@0 191
cannam@0 192 float
cannam@0 193 Onset::getParameter(std::string param) const
cannam@0 194 {
cannam@0 195 if (param == "onsettype") {
cannam@0 196 return m_onsettype;
cannam@0 197 } else if (param == "peakpickthreshold") {
cannam@0 198 return m_threshold;
cannam@0 199 } else if (param == "silencethreshold") {
cannam@0 200 return m_silence;
cannam@34 201 } else if (param == "minioi") {
cannam@34 202 return m_minioi;
cannam@0 203 } else {
cannam@0 204 return 0.0;
cannam@0 205 }
cannam@0 206 }
cannam@0 207
cannam@0 208 void
cannam@0 209 Onset::setParameter(std::string param, float value)
cannam@0 210 {
cannam@0 211 if (param == "onsettype") {
cannam@0 212 switch (lrintf(value)) {
cannam@33 213 case 0: m_onsettype = OnsetEnergy; break;
cannam@33 214 case 1: m_onsettype = OnsetSpecDiff; break;
cannam@33 215 case 2: m_onsettype = OnsetHFC; break;
cannam@33 216 case 3: m_onsettype = OnsetComplex; break;
cannam@33 217 case 4: m_onsettype = OnsetPhase; break;
cannam@33 218 case 5: m_onsettype = OnsetKL; break;
cannam@33 219 case 6: m_onsettype = OnsetMKL; break;
cannam@34 220 case 7: m_onsettype = OnsetSpecFlux; break;
cannam@0 221 }
cannam@0 222 } else if (param == "peakpickthreshold") {
cannam@0 223 m_threshold = value;
cannam@0 224 } else if (param == "silencethreshold") {
cannam@0 225 m_silence = value;
cannam@34 226 } else if (param == "minioi") {
cannam@34 227 m_minioi = value;
cannam@0 228 }
cannam@0 229 }
cannam@0 230
cannam@0 231 Onset::OutputList
cannam@0 232 Onset::getOutputDescriptors() const
cannam@0 233 {
cannam@0 234 OutputList list;
cannam@0 235
cannam@0 236 OutputDescriptor d;
cannam@13 237 d.identifier = "onsets";
cannam@13 238 d.name = "Onsets";
piem@64 239 d.description = "List of times at which a note onset was detected";
cannam@0 240 d.unit = "";
cannam@0 241 d.hasFixedBinCount = true;
cannam@0 242 d.binCount = 0;
cannam@3 243 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@3 244 d.sampleRate = 0;
cannam@0 245 list.push_back(d);
cannam@0 246
piem@71 247 d.identifier = "odf";
piem@71 248 d.name = "Onset detection function";
piem@71 249 d.description = "Output of the onset detection function";
piem@71 250 d.binCount = 1;
piem@71 251 d.isQuantized = true;
piem@71 252 d.quantizeStep = 1.0;
piem@71 253 d.sampleType = OutputDescriptor::OneSamplePerStep;
piem@71 254 list.push_back(d);
piem@71 255
piem@71 256 d.identifier = "todf";
piem@71 257 d.name = "Thresholded Onset detection function";
piem@71 258 d.description = "Output of the thresholded onset detection function";
piem@71 259 d.binCount = 1;
piem@71 260 d.isQuantized = true;
piem@71 261 d.quantizeStep = 1.0;
piem@71 262 d.sampleType = OutputDescriptor::OneSamplePerStep;
piem@71 263 list.push_back(d);
piem@71 264
cannam@0 265 return list;
cannam@0 266 }
cannam@0 267
cannam@0 268 Onset::FeatureSet
cannam@12 269 Onset::process(const float *const *inputBuffers,
cannam@12 270 Vamp::RealTime timestamp)
cannam@0 271 {
cannam@0 272 for (size_t i = 0; i < m_stepSize; ++i) {
piem@52 273 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
cannam@0 274 }
cannam@0 275
cannam@33 276 aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
cannam@0 277
cannam@34 278 bool isonset = m_onset->data[0];
cannam@0 279
cannam@0 280 FeatureSet returnFeatures;
cannam@0 281
cannam@0 282 if (isonset) {
cannam@3 283 if (timestamp - m_lastOnset >= m_delay) {
cannam@3 284 Feature onsettime;
cannam@3 285 onsettime.hasTimestamp = true;
piem@5 286 if (timestamp < m_delay) timestamp = m_delay;
cannam@3 287 onsettime.timestamp = timestamp - m_delay;
cannam@3 288 returnFeatures[0].push_back(onsettime);
cannam@3 289 m_lastOnset = timestamp;
cannam@3 290 }
cannam@0 291 }
cannam@38 292
piem@71 293 Feature odf;
piem@71 294 odf.hasTimestamp = false;
piem@71 295 odf.values.push_back(aubio_onset_get_descriptor(m_onsetdet));
piem@71 296 returnFeatures[1].push_back(odf);
piem@71 297
piem@71 298 Feature todf;
piem@71 299 todf.hasTimestamp = false;
piem@71 300 todf.values.push_back(aubio_onset_get_thresholded_descriptor(m_onsetdet));
piem@71 301 returnFeatures[2].push_back(todf);
piem@71 302
cannam@0 303 return returnFeatures;
cannam@0 304 }
cannam@0 305
cannam@0 306 Onset::FeatureSet
cannam@0 307 Onset::getRemainingFeatures()
cannam@0 308 {
cannam@0 309 return FeatureSet();
cannam@0 310 }
cannam@0 311