annotate plugins/Silence.cpp @ 22:a83125c623f0

* Add hasDuration to output descriptors
author Chris Cannam <cannam@all-day-breakfast.com>
date Thu, 18 Sep 2008 17:48:54 +0000
parents 8c939fff7ee1
children eb246395f576
rev   line source
cannam@17 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@17 2
cannam@17 3 /*
cannam@17 4 Vamp feature extraction plugins using Paul Brossier's Aubio library.
cannam@17 5
cannam@17 6 Centre for Digital Music, Queen Mary, University of London.
cannam@20 7 This file copyright 2006-2008 Chris Cannam and QMUL.
cannam@17 8
cannam@17 9 This program is free software; you can redistribute it and/or
cannam@17 10 modify it under the terms of the GNU General Public License as
cannam@17 11 published by the Free Software Foundation; either version 2 of the
cannam@17 12 License, or (at your option) any later version. See the file
cannam@17 13 COPYING included with this distribution for more information.
cannam@17 14 */
cannam@17 15
cannam@17 16 #include <math.h>
cannam@17 17 #include "Silence.h"
cannam@17 18
cannam@17 19 using std::string;
cannam@17 20 using std::vector;
cannam@17 21 using std::cerr;
cannam@17 22 using std::endl;
cannam@17 23
cannam@20 24 Silence::Silence(float inputSampleRate, unsigned int apiVersion) :
cannam@17 25 Plugin(inputSampleRate),
cannam@20 26 m_apiVersion(apiVersion),
cannam@17 27 m_ibuf(0),
cannam@17 28 m_pbuf(0),
cannam@17 29 m_tmpptrs(0),
cannam@18 30 m_threshold(-80),
cannam@17 31 m_prevSilent(false),
cannam@17 32 m_first(true)
cannam@17 33 {
cannam@20 34 if (m_apiVersion == 1) {
cannam@20 35 cerr << "vamp-aubio: WARNING: using compatibility version 1 of the Vamp API for silence\n"
cannam@20 36 << "detector plugin: upgrade your host to v2 for proper duration support" << endl;
cannam@20 37 }
cannam@17 38 }
cannam@17 39
cannam@17 40 Silence::~Silence()
cannam@17 41 {
cannam@17 42 if (m_ibuf) del_fvec(m_ibuf);
cannam@17 43 if (m_pbuf) del_fvec(m_pbuf);
cannam@17 44 if (m_tmpptrs) delete[] m_tmpptrs;
cannam@17 45 }
cannam@17 46
cannam@17 47 string
cannam@17 48 Silence::getIdentifier() const
cannam@17 49 {
cannam@17 50 return "aubiosilence";
cannam@17 51 }
cannam@17 52
cannam@17 53 string
cannam@17 54 Silence::getName() const
cannam@17 55 {
cannam@17 56 return "Aubio Silence Detector";
cannam@17 57 }
cannam@17 58
cannam@17 59 string
cannam@17 60 Silence::getDescription() const
cannam@17 61 {
cannam@17 62 return "Detect levels below a certain threshold";
cannam@17 63 }
cannam@17 64
cannam@17 65 string
cannam@17 66 Silence::getMaker() const
cannam@17 67 {
cannam@17 68 return "Paul Brossier (plugin by Chris Cannam)";
cannam@17 69 }
cannam@17 70
cannam@17 71 int
cannam@17 72 Silence::getPluginVersion() const
cannam@17 73 {
cannam@20 74 if (m_apiVersion == 1) return 2;
cannam@20 75 return 3;
cannam@17 76 }
cannam@17 77
cannam@17 78 string
cannam@17 79 Silence::getCopyright() const
cannam@17 80 {
cannam@17 81 return "GPL";
cannam@17 82 }
cannam@17 83
cannam@17 84 bool
cannam@17 85 Silence::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@17 86 {
cannam@17 87 m_channelCount = channels;
cannam@17 88 m_stepSize = stepSize;
cannam@17 89 m_blockSize = blockSize;
cannam@17 90
cannam@17 91 m_ibuf = new_fvec(stepSize, channels);
cannam@17 92 m_pbuf = new_fvec(stepSize, channels);
cannam@17 93 m_tmpptrs = new smpl_t *[channels];
cannam@17 94
cannam@17 95 return true;
cannam@17 96 }
cannam@17 97
cannam@17 98 void
cannam@17 99 Silence::reset()
cannam@17 100 {
cannam@17 101 m_first = true;
cannam@17 102 }
cannam@17 103
cannam@17 104 size_t
cannam@17 105 Silence::getPreferredStepSize() const
cannam@17 106 {
cannam@17 107 return 1024;
cannam@17 108 }
cannam@17 109
cannam@17 110 size_t
cannam@17 111 Silence::getPreferredBlockSize() const
cannam@17 112 {
cannam@17 113 return 1024;
cannam@17 114 }
cannam@17 115
cannam@17 116 Silence::ParameterList
cannam@17 117 Silence::getParameterDescriptors() const
cannam@17 118 {
cannam@17 119 ParameterList list;
cannam@17 120 ParameterDescriptor desc;
cannam@17 121
cannam@17 122 desc = ParameterDescriptor();
cannam@17 123 desc.identifier = "silencethreshold";
cannam@17 124 desc.name = "Silence Threshold";
cannam@17 125 desc.minValue = -120;
cannam@17 126 desc.maxValue = 0;
cannam@18 127 desc.defaultValue = -80;
cannam@17 128 desc.unit = "dB";
cannam@17 129 desc.isQuantized = false;
cannam@17 130 list.push_back(desc);
cannam@17 131
cannam@17 132 return list;
cannam@17 133 }
cannam@17 134
cannam@17 135 float
cannam@17 136 Silence::getParameter(std::string param) const
cannam@17 137 {
cannam@17 138 if (param == "silencethreshold") {
cannam@17 139 return m_threshold;
cannam@17 140 } else {
cannam@17 141 return 0.0;
cannam@17 142 }
cannam@17 143 }
cannam@17 144
cannam@17 145 void
cannam@17 146 Silence::setParameter(std::string param, float value)
cannam@17 147 {
cannam@17 148 if (param == "silencethreshold") {
cannam@17 149 m_threshold = value;
cannam@17 150 }
cannam@17 151 }
cannam@17 152
cannam@17 153 Silence::OutputList
cannam@17 154 Silence::getOutputDescriptors() const
cannam@17 155 {
cannam@17 156 OutputList list;
cannam@17 157
cannam@17 158 OutputDescriptor d;
cannam@17 159
cannam@20 160 if (m_apiVersion == 1) {
cannam@20 161
cannam@20 162 d.identifier = "silencestart";
cannam@20 163 d.name = "Beginnings of Silent Regions";
cannam@20 164 d.description = "Return a single instant at the point where each silent region begins";
cannam@20 165 d.hasFixedBinCount = true;
cannam@20 166 d.binCount = 0;
cannam@20 167 d.hasKnownExtents = false;
cannam@20 168 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@20 169 d.sampleRate = 0;
cannam@20 170 list.push_back(d);
cannam@20 171
cannam@20 172 d.identifier = "silenceend";
cannam@20 173 d.name = "Ends of Silent Regions";
cannam@20 174 d.description = "Return a single instant at the point where each silent region ends";
cannam@20 175 d.hasFixedBinCount = true;
cannam@20 176 d.binCount = 0;
cannam@20 177 d.hasKnownExtents = false;
cannam@20 178 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@20 179 d.sampleRate = 0;
cannam@20 180 list.push_back(d);
cannam@20 181
cannam@20 182 } else {
cannam@20 183
cannam@20 184 d.identifier = "silent";
cannam@20 185 d.name = "Silent Regions";
cannam@20 186 d.description = "Return an interval covering each silent region";
cannam@20 187 d.hasFixedBinCount = true;
cannam@20 188 d.binCount = 0;
cannam@20 189 d.hasKnownExtents = false;
cannam@20 190 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@20 191 d.sampleRate = 0;
cannam@22 192 d.hasDuration = true;
cannam@20 193 list.push_back(d);
cannam@20 194
cannam@20 195 d.identifier = "noisy";
cannam@20 196 d.name = "Non-Silent Regions";
cannam@20 197 d.description = "Return an interval covering each non-silent region";
cannam@20 198 d.hasFixedBinCount = true;
cannam@20 199 d.binCount = 0;
cannam@20 200 d.hasKnownExtents = false;
cannam@20 201 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@20 202 d.sampleRate = 0;
cannam@22 203 d.hasDuration = true;
cannam@20 204 list.push_back(d);
cannam@20 205 }
cannam@17 206
cannam@17 207 d.identifier = "silencelevel";
cannam@17 208 d.name = "Silence Test";
cannam@17 209 d.description = "Return a function that switches from 1 to 0 when silence falls, and back again when it ends";
cannam@17 210 d.hasFixedBinCount = true;
cannam@17 211 d.binCount = 1;
cannam@17 212 d.hasKnownExtents = true;
cannam@17 213 d.minValue = 0;
cannam@17 214 d.maxValue = 1;
cannam@17 215 d.isQuantized = true;
cannam@17 216 d.quantizeStep = 1;
cannam@17 217 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@18 218 d.sampleRate = 0;
cannam@17 219 list.push_back(d);
cannam@17 220
cannam@17 221 return list;
cannam@17 222 }
cannam@17 223
cannam@17 224 Silence::FeatureSet
cannam@17 225 Silence::process(const float *const *inputBuffers,
cannam@17 226 Vamp::RealTime timestamp)
cannam@17 227 {
cannam@17 228 for (size_t i = 0; i < m_stepSize; ++i) {
cannam@17 229 for (size_t j = 0; j < m_channelCount; ++j) {
cannam@17 230 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
cannam@17 231 }
cannam@17 232 }
cannam@17 233
cannam@17 234 bool silent = aubio_silence_detection(m_ibuf, m_threshold);
cannam@17 235 FeatureSet returnFeatures;
cannam@17 236
cannam@17 237 if (m_first || m_prevSilent != silent) {
cannam@17 238
cannam@17 239 Vamp::RealTime featureStamp = timestamp;
cannam@17 240
cannam@17 241 if ((silent && !m_first) || !silent) {
cannam@17 242
cannam@17 243 // refine our result
cannam@17 244
cannam@17 245 long off = 0;
cannam@17 246 size_t incr = 16;
cannam@17 247 if (incr > m_stepSize/8) incr = m_stepSize/8;
cannam@17 248
cannam@17 249 fvec_t vec;
cannam@17 250 vec.length = incr * 4;
cannam@17 251 vec.channels = m_channelCount;
cannam@17 252 vec.data = m_tmpptrs;
cannam@17 253
cannam@17 254 for (size_t i = 0; i < m_stepSize - incr * 4; i += incr) {
cannam@17 255 for (size_t j = 0; j < m_channelCount; ++j) {
cannam@17 256 m_tmpptrs[j] = m_ibuf->data[j] + i;
cannam@17 257 }
cannam@17 258 bool subsilent = aubio_silence_detection(&vec, m_threshold);
cannam@17 259 if (silent == subsilent) {
cannam@17 260 off = i;
cannam@17 261 break;
cannam@17 262 }
cannam@17 263 }
cannam@17 264
cannam@17 265 if (silent && (off == 0)) {
cannam@17 266 for (size_t i = 0; i < m_stepSize - incr; i += incr) {
cannam@17 267 for (size_t j = 0; j < m_channelCount; ++j) {
cannam@17 268 m_tmpptrs[j] = m_pbuf->data[j] + m_stepSize - i - incr;
cannam@17 269 }
cannam@17 270 bool subsilent = aubio_silence_detection(&vec, m_threshold);
cannam@17 271 if (!subsilent) {
cannam@17 272 off = -(long)i;
cannam@17 273 break;
cannam@17 274 }
cannam@17 275 }
cannam@17 276 } else {
cannam@17 277 }
cannam@17 278
cannam@17 279 featureStamp = timestamp + Vamp::RealTime::frame2RealTime
cannam@17 280 (off, lrintf(m_inputSampleRate));
cannam@17 281 }
cannam@17 282
cannam@17 283 Feature feature;
cannam@17 284 feature.hasTimestamp = true;
cannam@17 285 feature.timestamp = featureStamp;
cannam@17 286 feature.values.push_back(silent ? 0 : 1);
cannam@17 287 returnFeatures[2].push_back(feature);
cannam@20 288
cannam@17 289 feature.values.clear();
cannam@17 290
cannam@20 291 if (m_apiVersion == 1) {
cannam@20 292 if (silent) {
cannam@20 293 returnFeatures[0].push_back(feature);
cannam@20 294 } else {
cannam@20 295 returnFeatures[1].push_back(feature);
cannam@20 296 }
cannam@17 297 } else {
cannam@20 298 if (!m_first) {
cannam@20 299 feature.timestamp = m_lastChange;
cannam@20 300 feature.hasDuration = true;
cannam@20 301 feature.duration = featureStamp - m_lastChange;
cannam@20 302 if (silent) {
cannam@20 303 // becoming silent, so this is a non-silent region
cannam@20 304 returnFeatures[1].push_back(feature);
cannam@20 305 } else {
cannam@20 306 // becoming non-silent, so this is a silent region
cannam@20 307 returnFeatures[0].push_back(feature);
cannam@20 308 }
cannam@20 309 }
cannam@20 310 m_lastChange = featureStamp;
cannam@20 311 }
cannam@17 312
cannam@17 313 m_prevSilent = silent;
cannam@17 314 m_first = false;
cannam@17 315 }
cannam@17 316
cannam@17 317 // swap ibuf and pbuf data pointers, so that this block's data is
cannam@17 318 // available in pbuf when processing the next block, without
cannam@17 319 // having to allocate new storage for it
cannam@17 320 smpl_t **tmpdata = m_ibuf->data;
cannam@17 321 m_ibuf->data = m_pbuf->data;
cannam@17 322 m_pbuf->data = tmpdata;
cannam@17 323
cannam@20 324 m_lastTimestamp = timestamp;
cannam@20 325
cannam@17 326 return returnFeatures;
cannam@17 327 }
cannam@17 328
cannam@17 329 Silence::FeatureSet
cannam@17 330 Silence::getRemainingFeatures()
cannam@17 331 {
cannam@20 332 FeatureSet returnFeatures;
cannam@20 333
cannam@20 334 if (m_prevSilent) {
cannam@20 335 if (m_lastTimestamp > m_lastChange) {
cannam@20 336 Feature feature;
cannam@20 337 feature.hasTimestamp = true;
cannam@20 338 feature.timestamp = m_lastChange;
cannam@20 339 feature.hasDuration = true;
cannam@20 340 feature.duration = m_lastTimestamp - m_lastChange;
cannam@20 341 if (m_prevSilent) {
cannam@20 342 returnFeatures[0].push_back(feature);
cannam@20 343 } else {
cannam@20 344 returnFeatures[1].push_back(feature);
cannam@20 345 }
cannam@20 346 }
cannam@20 347 }
cannam@20 348
cannam@17 349 return FeatureSet();
cannam@17 350 }
cannam@17 351