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)
82 std::cerr << "Silence::initialise: channels must be 1" << std::endl;
86 m_stepSize = stepSize;
87 m_blockSize = blockSize;
89 m_ibuf = new_fvec(stepSize);
90 m_pbuf = new_fvec(stepSize);
91 m_tmpptrs = new smpl_t *[channels];
103 Silence::getPreferredStepSize() const
109 Silence::getPreferredBlockSize() const
114 Silence::ParameterList
115 Silence::getParameterDescriptors() const
118 ParameterDescriptor desc;
120 desc = ParameterDescriptor();
121 desc.identifier = "silencethreshold";
122 desc.name = "Silence Threshold";
123 desc.minValue = -120;
125 desc.defaultValue = -80;
127 desc.isQuantized = false;
128 list.push_back(desc);
134 Silence::getParameter(std::string param) const
136 if (param == "silencethreshold") {
144 Silence::setParameter(std::string param, float value)
146 if (param == "silencethreshold") {
152 Silence::getOutputDescriptors() const
158 d.identifier = "silent";
159 d.name = "Silent Regions";
160 d.description = "Return an interval covering each silent region";
161 d.hasFixedBinCount = true;
163 d.hasKnownExtents = false;
164 d.sampleType = OutputDescriptor::VariableSampleRate;
166 d.hasDuration = true;
169 d.identifier = "noisy";
170 d.name = "Non-Silent Regions";
171 d.description = "Return an interval covering each non-silent region";
172 d.hasFixedBinCount = true;
174 d.hasKnownExtents = false;
175 d.sampleType = OutputDescriptor::VariableSampleRate;
177 d.hasDuration = true;
180 d.identifier = "silencelevel";
181 d.name = "Silence Test";
182 d.description = "Return a function that switches from 1 to 0 when silence falls, and back again when it ends";
183 d.hasFixedBinCount = true;
185 d.hasKnownExtents = true;
188 d.isQuantized = true;
190 d.sampleType = OutputDescriptor::VariableSampleRate;
198 Silence::process(const float *const *inputBuffers,
199 Vamp::RealTime timestamp)
201 for (size_t i = 0; i < m_stepSize; ++i) {
202 for (size_t j = 0; j < m_channelCount; ++j) {
203 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
207 bool silent = aubio_silence_detection(m_ibuf, m_threshold);
208 FeatureSet returnFeatures;
210 if (m_first || m_prevSilent != silent) {
212 Vamp::RealTime featureStamp = timestamp;
214 if ((silent && !m_first) || !silent) {
220 if (incr > m_stepSize/8) incr = m_stepSize/8;
223 vec.length = incr * 4;
224 vec.channels = m_channelCount;
225 vec.data = m_tmpptrs;
227 for (size_t i = 0; i < m_stepSize - incr * 4; i += incr) {
228 for (size_t j = 0; j < m_channelCount; ++j) {
229 m_tmpptrs[j] = m_ibuf->data[j] + i;
231 bool subsilent = aubio_silence_detection(&vec, m_threshold);
232 if (silent == subsilent) {
238 if (silent && (off == 0)) {
239 for (size_t i = 0; i < m_stepSize - incr; i += incr) {
240 for (size_t j = 0; j < m_channelCount; ++j) {
241 m_tmpptrs[j] = m_pbuf->data[j] + m_stepSize - i - incr;
243 bool subsilent = aubio_silence_detection(&vec, m_threshold);
252 featureStamp = timestamp + Vamp::RealTime::frame2RealTime
253 (off, lrintf(m_inputSampleRate));
257 feature.hasTimestamp = true;
258 feature.timestamp = featureStamp;
259 feature.values.push_back(silent ? 0 : 1);
260 returnFeatures[2].push_back(feature);
262 feature.values.clear();
265 feature.timestamp = m_lastChange;
266 feature.hasDuration = true;
267 feature.duration = featureStamp - m_lastChange;
269 // non-silent regions feature
270 // (becoming silent, so this is a non-silent region)
271 returnFeatures[1].push_back(feature);
273 // silent regions feature
274 // (becoming non-silent, so this is a silent region)
275 returnFeatures[0].push_back(feature);
278 m_lastChange = featureStamp;
280 m_prevSilent = silent;
284 // swap ibuf and pbuf data pointers, so that this block's data is
285 // available in pbuf when processing the next block, without
286 // having to allocate new storage for it
287 smpl_t **tmpdata = m_ibuf->data;
288 m_ibuf->data = m_pbuf->data;
289 m_pbuf->data = tmpdata;
291 m_lastTimestamp = timestamp;
293 return returnFeatures;
297 Silence::getRemainingFeatures()
299 FeatureSet returnFeatures;
301 // std::cerr << "Silence::getRemainingFeatures: m_lastTimestamp = " << m_lastTimestamp << ", m_lastChange = " << m_lastChange << ", m_apiVersion = " << m_apiVersion << ", m_prevSilent = " << m_prevSilent << std::endl;
303 if (m_lastTimestamp > m_lastChange) {
306 feature.hasTimestamp = true;
308 feature.timestamp = m_lastChange;
309 feature.hasDuration = true;
310 feature.duration = m_lastTimestamp - m_lastChange;
312 // silent regions feature
313 returnFeatures[0].push_back(feature);
315 // non-silent regions feature
316 returnFeatures[1].push_back(feature);
320 Feature silenceTestFeature;
321 silenceTestFeature.hasTimestamp = true;
322 silenceTestFeature.timestamp = m_lastTimestamp;
323 silenceTestFeature.values.push_back(0);
324 returnFeatures[2].push_back(silenceTestFeature);
328 return returnFeatures;