1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
4 Vamp feature extraction plugins using Paul Brossier's Aubio library.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006-2008 Chris Cannam and QMUL.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
24 Silence::Silence(float inputSampleRate) :
25 Plugin(inputSampleRate),
37 if (m_ibuf) del_fvec(m_ibuf);
38 if (m_pbuf) del_fvec(m_pbuf);
39 if (m_tmpptrs) delete[] m_tmpptrs;
43 Silence::getIdentifier() const
45 return "aubiosilence";
49 Silence::getName() const
51 return "Aubio Silence Detector";
55 Silence::getDescription() const
57 return "Detect levels below a certain threshold";
61 Silence::getMaker() const
63 return "Paul Brossier (plugin by Chris Cannam)";
67 Silence::getPluginVersion() const
73 Silence::getCopyright() const
79 Silence::initialise(size_t channels, size_t stepSize, size_t blockSize)
81 m_channelCount = channels;
82 m_stepSize = stepSize;
83 m_blockSize = blockSize;
85 m_ibuf = new_fvec(stepSize, channels);
86 m_pbuf = new_fvec(stepSize, channels);
87 m_tmpptrs = new smpl_t *[channels];
99 Silence::getPreferredStepSize() const
105 Silence::getPreferredBlockSize() const
110 Silence::ParameterList
111 Silence::getParameterDescriptors() const
114 ParameterDescriptor desc;
116 desc = ParameterDescriptor();
117 desc.identifier = "silencethreshold";
118 desc.name = "Silence Threshold";
119 desc.minValue = -120;
121 desc.defaultValue = -80;
123 desc.isQuantized = false;
124 list.push_back(desc);
130 Silence::getParameter(std::string param) const
132 if (param == "silencethreshold") {
140 Silence::setParameter(std::string param, float value)
142 if (param == "silencethreshold") {
148 Silence::getOutputDescriptors() const
154 d.identifier = "silent";
155 d.name = "Silent Regions";
156 d.description = "Return an interval covering each silent region";
157 d.hasFixedBinCount = true;
159 d.hasKnownExtents = false;
160 d.sampleType = OutputDescriptor::VariableSampleRate;
162 d.hasDuration = true;
165 d.identifier = "noisy";
166 d.name = "Non-Silent Regions";
167 d.description = "Return an interval covering each non-silent region";
168 d.hasFixedBinCount = true;
170 d.hasKnownExtents = false;
171 d.sampleType = OutputDescriptor::VariableSampleRate;
173 d.hasDuration = true;
176 d.identifier = "silencelevel";
177 d.name = "Silence Test";
178 d.description = "Return a function that switches from 1 to 0 when silence falls, and back again when it ends";
179 d.hasFixedBinCount = true;
181 d.hasKnownExtents = true;
184 d.isQuantized = true;
186 d.sampleType = OutputDescriptor::VariableSampleRate;
194 Silence::process(const float *const *inputBuffers,
195 Vamp::RealTime timestamp)
197 for (size_t i = 0; i < m_stepSize; ++i) {
198 for (size_t j = 0; j < m_channelCount; ++j) {
199 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
203 bool silent = aubio_silence_detection(m_ibuf, m_threshold);
204 FeatureSet returnFeatures;
206 if (m_first || m_prevSilent != silent) {
208 Vamp::RealTime featureStamp = timestamp;
210 if ((silent && !m_first) || !silent) {
216 if (incr > m_stepSize/8) incr = m_stepSize/8;
219 vec.length = incr * 4;
220 vec.channels = m_channelCount;
221 vec.data = m_tmpptrs;
223 for (size_t i = 0; i < m_stepSize - incr * 4; i += incr) {
224 for (size_t j = 0; j < m_channelCount; ++j) {
225 m_tmpptrs[j] = m_ibuf->data[j] + i;
227 bool subsilent = aubio_silence_detection(&vec, m_threshold);
228 if (silent == subsilent) {
234 if (silent && (off == 0)) {
235 for (size_t i = 0; i < m_stepSize - incr; i += incr) {
236 for (size_t j = 0; j < m_channelCount; ++j) {
237 m_tmpptrs[j] = m_pbuf->data[j] + m_stepSize - i - incr;
239 bool subsilent = aubio_silence_detection(&vec, m_threshold);
248 featureStamp = timestamp + Vamp::RealTime::frame2RealTime
249 (off, lrintf(m_inputSampleRate));
253 feature.hasTimestamp = true;
254 feature.timestamp = featureStamp;
255 feature.values.push_back(silent ? 0 : 1);
256 returnFeatures[2].push_back(feature);
258 feature.values.clear();
261 feature.timestamp = m_lastChange;
262 feature.hasDuration = true;
263 feature.duration = featureStamp - m_lastChange;
265 // non-silent regions feature
266 // (becoming silent, so this is a non-silent region)
267 returnFeatures[1].push_back(feature);
269 // silent regions feature
270 // (becoming non-silent, so this is a silent region)
271 returnFeatures[0].push_back(feature);
274 m_lastChange = featureStamp;
276 m_prevSilent = silent;
280 // swap ibuf and pbuf data pointers, so that this block's data is
281 // available in pbuf when processing the next block, without
282 // having to allocate new storage for it
283 smpl_t **tmpdata = m_ibuf->data;
284 m_ibuf->data = m_pbuf->data;
285 m_pbuf->data = tmpdata;
287 m_lastTimestamp = timestamp;
289 return returnFeatures;
293 Silence::getRemainingFeatures()
295 FeatureSet returnFeatures;
297 // std::cerr << "Silence::getRemainingFeatures: m_lastTimestamp = " << m_lastTimestamp << ", m_lastChange = " << m_lastChange << ", m_apiVersion = " << m_apiVersion << ", m_prevSilent = " << m_prevSilent << std::endl;
299 if (m_lastTimestamp > m_lastChange) {
302 feature.hasTimestamp = true;
304 feature.timestamp = m_lastChange;
305 feature.hasDuration = true;
306 feature.duration = m_lastTimestamp - m_lastChange;
308 // silent regions feature
309 returnFeatures[0].push_back(feature);
311 // non-silent regions feature
312 returnFeatures[1].push_back(feature);
316 Feature silenceTestFeature;
317 silenceTestFeature.hasTimestamp = true;
318 silenceTestFeature.timestamp = m_lastTimestamp;
319 silenceTestFeature.values.push_back(0);
320 returnFeatures[2].push_back(silenceTestFeature);
324 return returnFeatures;