annotate plugins/Onset.cpp @ 112:f80b207ccd15

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