annotate plugins/Silence.cpp @ 17:b85fbc77677b

* Add silence detector. But I'm not happy with its results: take a look at the aubio silence detection code and see what's up
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 09 Oct 2007 15:42:36 +0000
parents
children 70472f9558da
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@17 7 This file copyright 2006 Chris Cannam.
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
cannam@17 17 #include <math.h>
cannam@17 18 #include "Silence.h"
cannam@17 19
cannam@17 20 using std::string;
cannam@17 21 using std::vector;
cannam@17 22 using std::cerr;
cannam@17 23 using std::endl;
cannam@17 24
cannam@17 25 Silence::Silence(float inputSampleRate) :
cannam@17 26 Plugin(inputSampleRate),
cannam@17 27 m_ibuf(0),
cannam@17 28 m_pbuf(0),
cannam@17 29 m_tmpptrs(0),
cannam@17 30 m_threshold(-70),
cannam@17 31 m_prevSilent(false),
cannam@17 32 m_first(true)
cannam@17 33 {
cannam@17 34 }
cannam@17 35
cannam@17 36 Silence::~Silence()
cannam@17 37 {
cannam@17 38 if (m_ibuf) del_fvec(m_ibuf);
cannam@17 39 if (m_pbuf) del_fvec(m_pbuf);
cannam@17 40 if (m_tmpptrs) delete[] m_tmpptrs;
cannam@17 41 }
cannam@17 42
cannam@17 43 string
cannam@17 44 Silence::getIdentifier() const
cannam@17 45 {
cannam@17 46 return "aubiosilence";
cannam@17 47 }
cannam@17 48
cannam@17 49 string
cannam@17 50 Silence::getName() const
cannam@17 51 {
cannam@17 52 return "Aubio Silence Detector";
cannam@17 53 }
cannam@17 54
cannam@17 55 string
cannam@17 56 Silence::getDescription() const
cannam@17 57 {
cannam@17 58 return "Detect levels below a certain threshold";
cannam@17 59 }
cannam@17 60
cannam@17 61 string
cannam@17 62 Silence::getMaker() const
cannam@17 63 {
cannam@17 64 return "Paul Brossier (plugin by Chris Cannam)";
cannam@17 65 }
cannam@17 66
cannam@17 67 int
cannam@17 68 Silence::getPluginVersion() const
cannam@17 69 {
cannam@17 70 return 1;
cannam@17 71 }
cannam@17 72
cannam@17 73 string
cannam@17 74 Silence::getCopyright() const
cannam@17 75 {
cannam@17 76 return "GPL";
cannam@17 77 }
cannam@17 78
cannam@17 79 bool
cannam@17 80 Silence::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@17 81 {
cannam@17 82 m_channelCount = channels;
cannam@17 83 m_stepSize = stepSize;
cannam@17 84 m_blockSize = blockSize;
cannam@17 85
cannam@17 86 m_ibuf = new_fvec(stepSize, channels);
cannam@17 87 m_pbuf = new_fvec(stepSize, channels);
cannam@17 88 m_tmpptrs = new smpl_t *[channels];
cannam@17 89
cannam@17 90 return true;
cannam@17 91 }
cannam@17 92
cannam@17 93 void
cannam@17 94 Silence::reset()
cannam@17 95 {
cannam@17 96 m_first = true;
cannam@17 97 }
cannam@17 98
cannam@17 99 size_t
cannam@17 100 Silence::getPreferredStepSize() const
cannam@17 101 {
cannam@17 102 return 1024;
cannam@17 103 }
cannam@17 104
cannam@17 105 size_t
cannam@17 106 Silence::getPreferredBlockSize() const
cannam@17 107 {
cannam@17 108 return 1024;
cannam@17 109 }
cannam@17 110
cannam@17 111 Silence::ParameterList
cannam@17 112 Silence::getParameterDescriptors() const
cannam@17 113 {
cannam@17 114 ParameterList list;
cannam@17 115 ParameterDescriptor desc;
cannam@17 116
cannam@17 117 desc = ParameterDescriptor();
cannam@17 118 desc.identifier = "silencethreshold";
cannam@17 119 desc.name = "Silence Threshold";
cannam@17 120 desc.minValue = -120;
cannam@17 121 desc.maxValue = 0;
cannam@17 122 desc.defaultValue = -70;
cannam@17 123 desc.unit = "dB";
cannam@17 124 desc.isQuantized = false;
cannam@17 125 list.push_back(desc);
cannam@17 126
cannam@17 127 return list;
cannam@17 128 }
cannam@17 129
cannam@17 130 float
cannam@17 131 Silence::getParameter(std::string param) const
cannam@17 132 {
cannam@17 133 if (param == "silencethreshold") {
cannam@17 134 return m_threshold;
cannam@17 135 } else {
cannam@17 136 return 0.0;
cannam@17 137 }
cannam@17 138 }
cannam@17 139
cannam@17 140 void
cannam@17 141 Silence::setParameter(std::string param, float value)
cannam@17 142 {
cannam@17 143 if (param == "silencethreshold") {
cannam@17 144 m_threshold = value;
cannam@17 145 }
cannam@17 146 }
cannam@17 147
cannam@17 148 Silence::OutputList
cannam@17 149 Silence::getOutputDescriptors() const
cannam@17 150 {
cannam@17 151 OutputList list;
cannam@17 152
cannam@17 153 OutputDescriptor d;
cannam@17 154 d.identifier = "silencestart";
cannam@17 155 d.name = "Starts of Silent Regions";
cannam@17 156 d.description = "Return a single instant at the point where each silent region begins";
cannam@17 157 d.hasFixedBinCount = true;
cannam@17 158 d.binCount = 0;
cannam@17 159 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@17 160 list.push_back(d);
cannam@17 161
cannam@17 162 d.identifier = "silenceend";
cannam@17 163 d.name = "Ends of Silent Regions";
cannam@17 164 d.description = "Return a single instant at the point where each silent region ends";
cannam@17 165 d.hasFixedBinCount = true;
cannam@17 166 d.binCount = 0;
cannam@17 167 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@17 168 list.push_back(d);
cannam@17 169
cannam@17 170 d.identifier = "silencelevel";
cannam@17 171 d.name = "Silence Test";
cannam@17 172 d.description = "Return a function that switches from 1 to 0 when silence falls, and back again when it ends";
cannam@17 173 d.hasFixedBinCount = true;
cannam@17 174 d.binCount = 1;
cannam@17 175 d.hasKnownExtents = true;
cannam@17 176 d.minValue = 0;
cannam@17 177 d.maxValue = 1;
cannam@17 178 d.isQuantized = true;
cannam@17 179 d.quantizeStep = 1;
cannam@17 180 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@17 181 list.push_back(d);
cannam@17 182
cannam@17 183 return list;
cannam@17 184 }
cannam@17 185
cannam@17 186 Silence::FeatureSet
cannam@17 187 Silence::process(const float *const *inputBuffers,
cannam@17 188 Vamp::RealTime timestamp)
cannam@17 189 {
cannam@17 190 for (size_t i = 0; i < m_stepSize; ++i) {
cannam@17 191 for (size_t j = 0; j < m_channelCount; ++j) {
cannam@17 192 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
cannam@17 193 }
cannam@17 194 }
cannam@17 195
cannam@17 196 bool silent = aubio_silence_detection(m_ibuf, m_threshold);
cannam@17 197 FeatureSet returnFeatures;
cannam@17 198
cannam@17 199 if (m_first || m_prevSilent != silent) {
cannam@17 200
cannam@17 201 Vamp::RealTime featureStamp = timestamp;
cannam@17 202
cannam@17 203 if ((silent && !m_first) || !silent) {
cannam@17 204
cannam@17 205 // refine our result
cannam@17 206
cannam@17 207 long off = 0;
cannam@17 208 size_t incr = 16;
cannam@17 209 if (incr > m_stepSize/8) incr = m_stepSize/8;
cannam@17 210
cannam@17 211 fvec_t vec;
cannam@17 212 vec.length = incr * 4;
cannam@17 213 vec.channels = m_channelCount;
cannam@17 214 vec.data = m_tmpptrs;
cannam@17 215
cannam@17 216 if (silent) {
cannam@17 217 std::cerr << "silence at " << timestamp << std::endl;
cannam@17 218 }
cannam@17 219
cannam@17 220 for (size_t i = 0; i < m_stepSize - incr * 4; i += incr) {
cannam@17 221 for (size_t j = 0; j < m_channelCount; ++j) {
cannam@17 222 m_tmpptrs[j] = m_ibuf->data[j] + i;
cannam@17 223 }
cannam@17 224 bool subsilent = aubio_silence_detection(&vec, m_threshold);
cannam@17 225 if (silent == subsilent) {
cannam@17 226 std::cerr << "silent == subsilent at " << i << " after" << std::endl;
cannam@17 227 off = i;
cannam@17 228 break;
cannam@17 229 }
cannam@17 230 }
cannam@17 231
cannam@17 232 if (silent && (off == 0)) {
cannam@17 233 for (size_t i = 0; i < m_stepSize - incr; i += incr) {
cannam@17 234 for (size_t j = 0; j < m_channelCount; ++j) {
cannam@17 235 m_tmpptrs[j] = m_pbuf->data[j] + m_stepSize - i - incr;
cannam@17 236 }
cannam@17 237 bool subsilent = aubio_silence_detection(&vec, m_threshold);
cannam@17 238 if (!subsilent) {
cannam@17 239 std::cerr << "non-silence at " << i << " samples before" << std::endl;
cannam@17 240 off = -(long)i;
cannam@17 241 break;
cannam@17 242 } else {
cannam@17 243 std::cerr << "silence at " << i << " samples before" << std::endl;
cannam@17 244 }
cannam@17 245 }
cannam@17 246 } else {
cannam@17 247 }
cannam@17 248
cannam@17 249 featureStamp = timestamp + Vamp::RealTime::frame2RealTime
cannam@17 250 (off, lrintf(m_inputSampleRate));
cannam@17 251 }
cannam@17 252
cannam@17 253 Feature feature;
cannam@17 254 feature.hasTimestamp = true;
cannam@17 255 feature.timestamp = featureStamp;
cannam@17 256 feature.values.push_back(silent ? 0 : 1);
cannam@17 257 returnFeatures[2].push_back(feature);
cannam@17 258 feature.values.clear();
cannam@17 259
cannam@17 260 if (silent) {
cannam@17 261 returnFeatures[0].push_back(feature);
cannam@17 262 } else {
cannam@17 263 returnFeatures[1].push_back(feature);
cannam@17 264 }
cannam@17 265
cannam@17 266 m_prevSilent = silent;
cannam@17 267 m_first = false;
cannam@17 268 }
cannam@17 269
cannam@17 270 // swap ibuf and pbuf data pointers, so that this block's data is
cannam@17 271 // available in pbuf when processing the next block, without
cannam@17 272 // having to allocate new storage for it
cannam@17 273 smpl_t **tmpdata = m_ibuf->data;
cannam@17 274 m_ibuf->data = m_pbuf->data;
cannam@17 275 m_pbuf->data = tmpdata;
cannam@17 276
cannam@17 277 return returnFeatures;
cannam@17 278 }
cannam@17 279
cannam@17 280 Silence::FeatureSet
cannam@17 281 Silence::getRemainingFeatures()
cannam@17 282 {
cannam@17 283 return FeatureSet();
cannam@17 284 }
cannam@17 285