annotate plugins/Silence.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 f80b207ccd15
children
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
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/>.
piem@112 23
cannam@17 24 */
cannam@17 25
cannam@17 26 #include <math.h>
cannam@17 27 #include "Silence.h"
cannam@17 28
cannam@17 29 using std::string;
cannam@17 30 using std::vector;
cannam@17 31 using std::cerr;
cannam@17 32 using std::endl;
cannam@17 33
cannam@31 34 Silence::Silence(float inputSampleRate) :
cannam@17 35 Plugin(inputSampleRate),
cannam@17 36 m_ibuf(0),
cannam@17 37 m_pbuf(0),
cannam@18 38 m_threshold(-80),
cannam@17 39 m_prevSilent(false),
cannam@17 40 m_first(true)
cannam@17 41 {
cannam@17 42 }
cannam@17 43
cannam@17 44 Silence::~Silence()
cannam@17 45 {
cannam@17 46 if (m_ibuf) del_fvec(m_ibuf);
cannam@17 47 if (m_pbuf) del_fvec(m_pbuf);
cannam@17 48 }
cannam@17 49
cannam@17 50 string
cannam@17 51 Silence::getIdentifier() const
cannam@17 52 {
cannam@17 53 return "aubiosilence";
cannam@17 54 }
cannam@17 55
cannam@17 56 string
cannam@17 57 Silence::getName() const
cannam@17 58 {
cannam@17 59 return "Aubio Silence Detector";
cannam@17 60 }
cannam@17 61
cannam@17 62 string
cannam@17 63 Silence::getDescription() const
cannam@17 64 {
cannam@17 65 return "Detect levels below a certain threshold";
cannam@17 66 }
cannam@17 67
cannam@17 68 string
cannam@17 69 Silence::getMaker() const
cannam@17 70 {
cannam@17 71 return "Paul Brossier (plugin by Chris Cannam)";
cannam@17 72 }
cannam@17 73
cannam@17 74 int
cannam@17 75 Silence::getPluginVersion() const
cannam@17 76 {
cannam@31 77 return 4;
cannam@17 78 }
cannam@17 79
cannam@17 80 string
cannam@17 81 Silence::getCopyright() const
cannam@17 82 {
cannam@17 83 return "GPL";
cannam@17 84 }
cannam@17 85
cannam@17 86 bool
cannam@17 87 Silence::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@17 88 {
cannam@32 89 if (channels != 1) {
cannam@32 90 std::cerr << "Silence::initialise: channels must be 1" << std::endl;
cannam@32 91 return false;
cannam@32 92 }
cannam@32 93
cannam@17 94 m_stepSize = stepSize;
cannam@17 95 m_blockSize = blockSize;
cannam@17 96
cannam@32 97 m_ibuf = new_fvec(stepSize);
cannam@32 98 m_pbuf = new_fvec(stepSize);
cannam@17 99
cannam@17 100 return true;
cannam@17 101 }
cannam@17 102
cannam@17 103 void
cannam@17 104 Silence::reset()
cannam@17 105 {
cannam@17 106 m_first = true;
cannam@17 107 }
cannam@17 108
cannam@17 109 size_t
cannam@17 110 Silence::getPreferredStepSize() const
cannam@17 111 {
cannam@17 112 return 1024;
cannam@17 113 }
cannam@17 114
cannam@17 115 size_t
cannam@17 116 Silence::getPreferredBlockSize() const
cannam@17 117 {
cannam@17 118 return 1024;
cannam@17 119 }
cannam@17 120
cannam@17 121 Silence::ParameterList
cannam@17 122 Silence::getParameterDescriptors() const
cannam@17 123 {
cannam@17 124 ParameterList list;
cannam@17 125 ParameterDescriptor desc;
cannam@17 126
cannam@17 127 desc = ParameterDescriptor();
cannam@17 128 desc.identifier = "silencethreshold";
cannam@17 129 desc.name = "Silence Threshold";
piem@66 130 desc.description = "Threshold for silence detection";
cannam@17 131 desc.minValue = -120;
cannam@17 132 desc.maxValue = 0;
cannam@18 133 desc.defaultValue = -80;
cannam@17 134 desc.unit = "dB";
cannam@17 135 desc.isQuantized = false;
cannam@17 136 list.push_back(desc);
cannam@17 137
cannam@17 138 return list;
cannam@17 139 }
cannam@17 140
cannam@17 141 float
cannam@17 142 Silence::getParameter(std::string param) const
cannam@17 143 {
cannam@17 144 if (param == "silencethreshold") {
cannam@17 145 return m_threshold;
cannam@17 146 } else {
cannam@17 147 return 0.0;
cannam@17 148 }
cannam@17 149 }
cannam@17 150
cannam@17 151 void
cannam@17 152 Silence::setParameter(std::string param, float value)
cannam@17 153 {
cannam@17 154 if (param == "silencethreshold") {
cannam@17 155 m_threshold = value;
cannam@17 156 }
cannam@17 157 }
cannam@17 158
cannam@17 159 Silence::OutputList
cannam@17 160 Silence::getOutputDescriptors() const
cannam@17 161 {
cannam@17 162 OutputList list;
cannam@17 163
cannam@17 164 OutputDescriptor d;
cannam@17 165
cannam@31 166 d.identifier = "silent";
cannam@31 167 d.name = "Silent Regions";
cannam@31 168 d.description = "Return an interval covering each silent region";
cannam@31 169 d.hasFixedBinCount = true;
cannam@31 170 d.binCount = 0;
cannam@31 171 d.hasKnownExtents = false;
cannam@31 172 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@31 173 d.sampleRate = 0;
cannam@31 174 d.hasDuration = true;
cannam@31 175 list.push_back(d);
cannam@20 176
cannam@31 177 d.identifier = "noisy";
cannam@31 178 d.name = "Non-Silent Regions";
cannam@31 179 d.description = "Return an interval covering each non-silent region";
cannam@31 180 d.hasFixedBinCount = true;
cannam@31 181 d.binCount = 0;
cannam@31 182 d.hasKnownExtents = false;
cannam@31 183 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@31 184 d.sampleRate = 0;
cannam@31 185 d.hasDuration = true;
cannam@31 186 list.push_back(d);
cannam@17 187
cannam@17 188 d.identifier = "silencelevel";
cannam@17 189 d.name = "Silence Test";
cannam@17 190 d.description = "Return a function that switches from 1 to 0 when silence falls, and back again when it ends";
cannam@17 191 d.hasFixedBinCount = true;
cannam@17 192 d.binCount = 1;
cannam@17 193 d.hasKnownExtents = true;
cannam@17 194 d.minValue = 0;
cannam@17 195 d.maxValue = 1;
cannam@17 196 d.isQuantized = true;
cannam@17 197 d.quantizeStep = 1;
cannam@17 198 d.sampleType = OutputDescriptor::VariableSampleRate;
cannam@18 199 d.sampleRate = 0;
cannam@17 200 list.push_back(d);
cannam@17 201
cannam@17 202 return list;
cannam@17 203 }
cannam@17 204
cannam@17 205 Silence::FeatureSet
cannam@17 206 Silence::process(const float *const *inputBuffers,
cannam@17 207 Vamp::RealTime timestamp)
cannam@17 208 {
cannam@17 209 for (size_t i = 0; i < m_stepSize; ++i) {
piem@52 210 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
cannam@17 211 }
cannam@17 212
cannam@17 213 bool silent = aubio_silence_detection(m_ibuf, m_threshold);
cannam@17 214 FeatureSet returnFeatures;
cannam@17 215
cannam@17 216 if (m_first || m_prevSilent != silent) {
cannam@17 217
cannam@17 218 Vamp::RealTime featureStamp = timestamp;
cannam@17 219
cannam@17 220 if ((silent && !m_first) || !silent) {
cannam@17 221
cannam@17 222 // refine our result
cannam@17 223
cannam@17 224 long off = 0;
cannam@17 225 size_t incr = 16;
cannam@17 226 if (incr > m_stepSize/8) incr = m_stepSize/8;
cannam@17 227
cannam@17 228 fvec_t vec;
cannam@17 229 vec.length = incr * 4;
cannam@17 230
cannam@17 231 for (size_t i = 0; i < m_stepSize - incr * 4; i += incr) {
cannam@35 232 vec.data = m_ibuf->data + i;
cannam@17 233 bool subsilent = aubio_silence_detection(&vec, m_threshold);
cannam@17 234 if (silent == subsilent) {
cannam@17 235 off = i;
cannam@17 236 break;
cannam@17 237 }
cannam@17 238 }
cannam@17 239
cannam@17 240 if (silent && (off == 0)) {
cannam@17 241 for (size_t i = 0; i < m_stepSize - incr; i += incr) {
cannam@35 242 vec.data = m_pbuf->data + m_stepSize - i - incr;
cannam@17 243 bool subsilent = aubio_silence_detection(&vec, m_threshold);
cannam@17 244 if (!subsilent) {
cannam@17 245 off = -(long)i;
cannam@17 246 break;
cannam@17 247 }
cannam@17 248 }
cannam@17 249 } else {
cannam@17 250 }
cannam@17 251
cannam@17 252 featureStamp = timestamp + Vamp::RealTime::frame2RealTime
cannam@17 253 (off, lrintf(m_inputSampleRate));
cannam@17 254 }
cannam@17 255
cannam@17 256 Feature feature;
cannam@17 257 feature.hasTimestamp = true;
cannam@17 258 feature.timestamp = featureStamp;
cannam@17 259 feature.values.push_back(silent ? 0 : 1);
cannam@17 260 returnFeatures[2].push_back(feature);
cannam@20 261
cannam@17 262 feature.values.clear();
cannam@17 263
cannam@31 264 if (!m_first) {
cannam@31 265 feature.timestamp = m_lastChange;
cannam@31 266 feature.hasDuration = true;
cannam@31 267 feature.duration = featureStamp - m_lastChange;
cannam@20 268 if (silent) {
cannam@31 269 // non-silent regions feature
cannam@31 270 // (becoming silent, so this is a non-silent region)
cannam@31 271 returnFeatures[1].push_back(feature);
cannam@31 272 } else {
cannam@31 273 // silent regions feature
cannam@31 274 // (becoming non-silent, so this is a silent region)
cannam@20 275 returnFeatures[0].push_back(feature);
cannam@31 276 }
cannam@20 277 }
cannam@31 278 m_lastChange = featureStamp;
cannam@17 279
cannam@17 280 m_prevSilent = silent;
cannam@17 281 m_first = false;
cannam@17 282 }
cannam@17 283
cannam@17 284 // swap ibuf and pbuf data pointers, so that this block's data is
cannam@17 285 // available in pbuf when processing the next block, without
cannam@17 286 // having to allocate new storage for it
cannam@35 287 smpl_t *tmpdata = m_ibuf->data;
cannam@17 288 m_ibuf->data = m_pbuf->data;
cannam@17 289 m_pbuf->data = tmpdata;
cannam@17 290
cannam@20 291 m_lastTimestamp = timestamp;
cannam@20 292
cannam@17 293 return returnFeatures;
cannam@17 294 }
cannam@17 295
cannam@17 296 Silence::FeatureSet
cannam@17 297 Silence::getRemainingFeatures()
cannam@17 298 {
cannam@20 299 FeatureSet returnFeatures;
cannam@20 300
cannam@24 301 // std::cerr << "Silence::getRemainingFeatures: m_lastTimestamp = " << m_lastTimestamp << ", m_lastChange = " << m_lastChange << ", m_apiVersion = " << m_apiVersion << ", m_prevSilent = " << m_prevSilent << std::endl;
cannam@24 302
cannam@24 303 if (m_lastTimestamp > m_lastChange) {
cannam@24 304
cannam@24 305 Feature feature;
cannam@24 306 feature.hasTimestamp = true;
cannam@24 307
cannam@31 308 feature.timestamp = m_lastChange;
cannam@31 309 feature.hasDuration = true;
cannam@31 310 feature.duration = m_lastTimestamp - m_lastChange;
cannam@31 311 if (m_prevSilent) {
cannam@31 312 // silent regions feature
cannam@31 313 returnFeatures[0].push_back(feature);
cannam@24 314 } else {
cannam@31 315 // non-silent regions feature
cannam@31 316 returnFeatures[1].push_back(feature);
cannam@20 317 }
cannam@24 318
cannam@24 319 if (!m_prevSilent) {
cannam@24 320 Feature silenceTestFeature;
cannam@24 321 silenceTestFeature.hasTimestamp = true;
cannam@24 322 silenceTestFeature.timestamp = m_lastTimestamp;
cannam@24 323 silenceTestFeature.values.push_back(0);
cannam@24 324 returnFeatures[2].push_back(silenceTestFeature);
cannam@24 325 }
cannam@20 326 }
cannam@20 327
cannam@24 328 return returnFeatures;
cannam@17 329 }
cannam@17 330