annotate plugins/Onset.cpp @ 198:3a76aa26b578 tip master

wscript: check for 64bit using sys.maxsize (closes #3)
author Paul Brossier <piem@piem.org>
date Mon, 04 Dec 2017 01:42:19 +0100
parents 88e01aa5b83b
children
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),
piem@145 39 m_onsettype(OnsetDefault),
cannam@0 40 m_threshold(0.3),
piem@148 41 m_silence(-90),
cannam@34 42 m_minioi(4)
cannam@0 43 {
piem@148 44
cannam@0 45 }
cannam@0 46
cannam@0 47 Onset::~Onset()
cannam@0 48 {
cannam@33 49 if (m_onsetdet) del_aubio_onset(m_onsetdet);
cannam@0 50 if (m_ibuf) del_fvec(m_ibuf);
cannam@0 51 if (m_onset) del_fvec(m_onset);
cannam@0 52 }
cannam@0 53
cannam@0 54 string
cannam@13 55 Onset::getIdentifier() const
cannam@0 56 {
cannam@0 57 return "aubioonset";
cannam@0 58 }
cannam@0 59
cannam@0 60 string
cannam@13 61 Onset::getName() const
cannam@13 62 {
cannam@13 63 return "Aubio Onset Detector";
cannam@13 64 }
cannam@13 65
cannam@13 66 string
cannam@0 67 Onset::getDescription() const
cannam@0 68 {
cannam@13 69 return "Estimate note onset times";
cannam@0 70 }
cannam@0 71
cannam@0 72 string
cannam@0 73 Onset::getMaker() const
cannam@0 74 {
cannam@0 75 return "Paul Brossier (plugin by Chris Cannam)";
cannam@0 76 }
cannam@0 77
cannam@0 78 int
cannam@0 79 Onset::getPluginVersion() const
cannam@0 80 {
cannam@31 81 return 2;
cannam@0 82 }
cannam@0 83
cannam@0 84 string
cannam@0 85 Onset::getCopyright() const
cannam@0 86 {
cannam@0 87 return "GPL";
cannam@0 88 }
cannam@0 89
cannam@0 90 bool
cannam@0 91 Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@0 92 {
cannam@32 93 if (channels != 1) {
cannam@32 94 std::cerr << "Onset::initialise: channels must be 1" << std::endl;
cannam@32 95 return false;
cannam@32 96 }
cannam@32 97
cannam@0 98 m_stepSize = stepSize;
cannam@0 99 m_blockSize = blockSize;
cannam@0 100
cannam@32 101 m_ibuf = new_fvec(stepSize);
cannam@32 102 m_onset = new_fvec(1);
cannam@0 103
cannam@37 104 reset();
cannam@0 105
cannam@0 106 return true;
cannam@0 107 }
cannam@0 108
cannam@0 109 void
cannam@0 110 Onset::reset()
cannam@0 111 {
cannam@37 112 if (m_onsetdet) del_aubio_onset(m_onsetdet);
cannam@37 113
cannam@37 114 m_onsetdet = new_aubio_onset
cannam@37 115 (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
cannam@37 116 m_blockSize,
cannam@37 117 m_stepSize,
cannam@37 118 lrintf(m_inputSampleRate));
piem@148 119
cannam@37 120 aubio_onset_set_threshold(m_onsetdet, m_threshold);
cannam@37 121 aubio_onset_set_silence(m_onsetdet, m_silence);
cannam@37 122 aubio_onset_set_minioi(m_onsetdet, m_minioi);
cannam@37 123
cannam@0 124 }
cannam@0 125
cannam@0 126 size_t
cannam@0 127 Onset::getPreferredStepSize() const
cannam@0 128 {
piem@148 129 return 256;
cannam@0 130 }
cannam@0 131
cannam@0 132 size_t
cannam@0 133 Onset::getPreferredBlockSize() const
cannam@0 134 {
cannam@3 135 return 2 * getPreferredStepSize();
cannam@0 136 }
cannam@0 137
cannam@0 138 Onset::ParameterList
cannam@0 139 Onset::getParameterDescriptors() const
cannam@0 140 {
cannam@0 141 ParameterList list;
cannam@0 142
cannam@0 143 ParameterDescriptor desc;
cannam@13 144 desc.identifier = "onsettype";
cannam@13 145 desc.name = "Onset Detection Function Type";
piem@64 146 desc.description = "Type of onset detection function to use";
cannam@0 147 desc.minValue = 0;
cannam@34 148 desc.maxValue = 7;
piem@145 149 desc.defaultValue = (int)OnsetDefault;
cannam@0 150 desc.isQuantized = true;
cannam@0 151 desc.quantizeStep = 1;
cannam@0 152 desc.valueNames.push_back("Energy Based");
cannam@0 153 desc.valueNames.push_back("Spectral Difference");
cannam@0 154 desc.valueNames.push_back("High-Frequency Content");
cannam@0 155 desc.valueNames.push_back("Complex Domain");
cannam@0 156 desc.valueNames.push_back("Phase Deviation");
cannam@0 157 desc.valueNames.push_back("Kullback-Liebler");
cannam@0 158 desc.valueNames.push_back("Modified Kullback-Liebler");
cannam@34 159 desc.valueNames.push_back("Spectral Flux");
piem@145 160 desc.valueNames.push_back("Default");
cannam@0 161 list.push_back(desc);
cannam@0 162
cannam@0 163 desc = ParameterDescriptor();
cannam@13 164 desc.identifier = "peakpickthreshold";
cannam@13 165 desc.name = "Peak Picker Threshold";
piem@64 166 desc.description = "Threshold used for peak picking, the higher the more detections";
cannam@0 167 desc.minValue = 0;
cannam@0 168 desc.maxValue = 1;
cannam@0 169 desc.defaultValue = 0.3;
cannam@0 170 desc.isQuantized = false;
cannam@0 171 list.push_back(desc);
cannam@0 172
cannam@0 173 desc = ParameterDescriptor();
cannam@13 174 desc.identifier = "silencethreshold";
cannam@13 175 desc.name = "Silence Threshold";
piem@64 176 desc.description = "Silence threshold, the higher the least detection";
cannam@0 177 desc.minValue = -120;
cannam@0 178 desc.maxValue = 0;
piem@148 179 desc.defaultValue = -90;
cannam@0 180 desc.unit = "dB";
cannam@0 181 desc.isQuantized = false;
cannam@0 182 list.push_back(desc);
cannam@0 183
cannam@34 184 desc = ParameterDescriptor();
cannam@34 185 desc.identifier = "minioi";
cannam@34 186 desc.name = "Minimum Inter-Onset Interval";
piem@64 187 desc.description = "Time interval below which two consecutive onsets should be merged";
cannam@34 188 desc.minValue = 0;
cannam@34 189 desc.maxValue = 40;
cannam@34 190 desc.defaultValue = 4;
cannam@34 191 desc.unit = "ms";
cannam@34 192 desc.isQuantized = true;
cannam@34 193 desc.quantizeStep = 1;
cannam@34 194 list.push_back(desc);
cannam@34 195
cannam@0 196 return list;
cannam@0 197 }
cannam@0 198
cannam@0 199 float
cannam@0 200 Onset::getParameter(std::string param) const
cannam@0 201 {
cannam@0 202 if (param == "onsettype") {
cannam@0 203 return m_onsettype;
cannam@0 204 } else if (param == "peakpickthreshold") {
piem@148 205 if (m_onsetdet) {
piem@148 206 return aubio_onset_get_threshold(m_onsetdet);
piem@148 207 } else {
piem@148 208 return m_threshold;
piem@148 209 }
cannam@0 210 } else if (param == "silencethreshold") {
piem@148 211 if (m_onsetdet) {
piem@148 212 return aubio_onset_get_silence(m_onsetdet);
piem@148 213 } else {
piem@148 214 return m_silence;
piem@148 215 }
cannam@34 216 } else if (param == "minioi") {
piem@148 217 if (m_onsetdet) {
piem@148 218 return aubio_onset_get_minioi(m_onsetdet);
piem@148 219 } else {
piem@148 220 return m_minioi;
piem@148 221 }
cannam@0 222 } else {
cannam@0 223 return 0.0;
cannam@0 224 }
cannam@0 225 }
cannam@0 226
cannam@0 227 void
cannam@0 228 Onset::setParameter(std::string param, float value)
cannam@0 229 {
cannam@0 230 if (param == "onsettype") {
cannam@0 231 switch (lrintf(value)) {
cannam@33 232 case 0: m_onsettype = OnsetEnergy; break;
cannam@33 233 case 1: m_onsettype = OnsetSpecDiff; break;
cannam@33 234 case 2: m_onsettype = OnsetHFC; break;
cannam@33 235 case 3: m_onsettype = OnsetComplex; break;
cannam@33 236 case 4: m_onsettype = OnsetPhase; break;
cannam@33 237 case 5: m_onsettype = OnsetKL; break;
cannam@33 238 case 6: m_onsettype = OnsetMKL; break;
cannam@34 239 case 7: m_onsettype = OnsetSpecFlux; break;
piem@148 240 case 8: m_onsettype = OnsetDefault; break;
cannam@0 241 }
piem@148 242 if (!m_onsetdet) initialise(1, 256, 512);
cannam@0 243 } else if (param == "peakpickthreshold") {
cannam@0 244 m_threshold = value;
piem@148 245 if (m_onsetdet)
piem@148 246 aubio_onset_set_threshold(m_onsetdet, m_threshold);
cannam@0 247 } else if (param == "silencethreshold") {
cannam@0 248 m_silence = value;
piem@148 249 if (m_onsetdet)
piem@148 250 aubio_onset_set_silence(m_onsetdet, m_silence);
cannam@34 251 } else if (param == "minioi") {
cannam@34 252 m_minioi = value;
piem@148 253 if (m_onsetdet)
piem@148 254 aubio_onset_set_minioi(m_onsetdet, m_minioi);
cannam@0 255 }
cannam@0 256 }
cannam@0 257
cannam@0 258 Onset::OutputList
cannam@0 259 Onset::getOutputDescriptors() const
cannam@0 260 {
cannam@0 261 OutputList list;
cannam@0 262
cannam@0 263 OutputDescriptor d;
cannam@13 264 d.identifier = "onsets";
cannam@13 265 d.name = "Onsets";
piem@64 266 d.description = "List of times at which a note onset was detected";
cannam@0 267 d.unit = "";
cannam@0 268 d.hasFixedBinCount = true;
cannam@0 269 d.binCount = 0;
cannam@3 270 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@3 271 d.sampleRate = 0;
cannam@0 272 list.push_back(d);
cannam@0 273
piem@71 274 d.identifier = "odf";
piem@71 275 d.name = "Onset detection function";
piem@71 276 d.description = "Output of the onset detection function";
piem@71 277 d.binCount = 1;
piem@71 278 d.isQuantized = true;
piem@71 279 d.quantizeStep = 1.0;
piem@71 280 d.sampleType = OutputDescriptor::OneSamplePerStep;
piem@71 281 list.push_back(d);
piem@71 282
piem@71 283 d.identifier = "todf";
piem@71 284 d.name = "Thresholded Onset detection function";
piem@71 285 d.description = "Output of the thresholded onset detection function";
piem@71 286 d.binCount = 1;
piem@71 287 d.isQuantized = true;
piem@71 288 d.quantizeStep = 1.0;
piem@71 289 d.sampleType = OutputDescriptor::OneSamplePerStep;
piem@71 290 list.push_back(d);
piem@71 291
cannam@0 292 return list;
cannam@0 293 }
cannam@0 294
cannam@0 295 Onset::FeatureSet
cannam@12 296 Onset::process(const float *const *inputBuffers,
piem@149 297 UNUSED Vamp::RealTime timestamp)
cannam@0 298 {
cannam@0 299 for (size_t i = 0; i < m_stepSize; ++i) {
piem@52 300 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
cannam@0 301 }
cannam@0 302
cannam@33 303 aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
cannam@0 304
piem@148 305 smpl_t isonset = m_onset->data[0];
cannam@0 306
cannam@0 307 FeatureSet returnFeatures;
cannam@0 308
cannam@0 309 if (isonset) {
piem@148 310 Feature onsettime;
piem@148 311 onsettime.hasTimestamp = true;
piem@148 312 onsettime.timestamp = Vamp::RealTime::fromSeconds(aubio_onset_get_last_s(m_onsetdet));
piem@148 313 returnFeatures[0].push_back(onsettime);
cannam@0 314 }
cannam@38 315
piem@71 316 Feature odf;
piem@71 317 odf.hasTimestamp = false;
piem@71 318 odf.values.push_back(aubio_onset_get_descriptor(m_onsetdet));
piem@71 319 returnFeatures[1].push_back(odf);
piem@71 320
piem@71 321 Feature todf;
piem@71 322 todf.hasTimestamp = false;
piem@71 323 todf.values.push_back(aubio_onset_get_thresholded_descriptor(m_onsetdet));
piem@71 324 returnFeatures[2].push_back(todf);
piem@71 325
cannam@0 326 return returnFeatures;
cannam@0 327 }
cannam@0 328
cannam@0 329 Onset::FeatureSet
cannam@0 330 Onset::getRemainingFeatures()
cannam@0 331 {
cannam@0 332 return FeatureSet();
cannam@0 333 }
cannam@0 334