Update plugin version numbers, remove API version back compatibility (API v1 is now...
[vamp-aubio-plugins.git] / plugins / Silence.cpp
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4     Vamp feature extraction plugins using Paul Brossier's Aubio library.
5
6     Centre for Digital Music, Queen Mary, University of London.
7     This file copyright 2006-2008 Chris Cannam and QMUL.
8     
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.
14 */
15
16 #include <math.h>
17 #include "Silence.h"
18
19 using std::string;
20 using std::vector;
21 using std::cerr;
22 using std::endl;
23
24 Silence::Silence(float inputSampleRate) :
25     Plugin(inputSampleRate),
26     m_ibuf(0),
27     m_pbuf(0),
28     m_tmpptrs(0),
29     m_threshold(-80),
30     m_prevSilent(false),
31     m_first(true)
32 {
33 }
34
35 Silence::~Silence()
36 {
37     if (m_ibuf) del_fvec(m_ibuf);
38     if (m_pbuf) del_fvec(m_pbuf);
39     if (m_tmpptrs) delete[] m_tmpptrs;
40 }
41
42 string
43 Silence::getIdentifier() const
44 {
45     return "aubiosilence";
46 }
47
48 string
49 Silence::getName() const
50 {
51     return "Aubio Silence Detector";
52 }
53
54 string
55 Silence::getDescription() const
56 {
57     return "Detect levels below a certain threshold";
58 }
59
60 string
61 Silence::getMaker() const
62 {
63     return "Paul Brossier (plugin by Chris Cannam)";
64 }
65
66 int
67 Silence::getPluginVersion() const
68 {
69     return 4;
70 }
71
72 string
73 Silence::getCopyright() const
74 {
75     return "GPL";
76 }
77
78 bool
79 Silence::initialise(size_t channels, size_t stepSize, size_t blockSize)
80 {
81     m_channelCount = channels;
82     m_stepSize = stepSize;
83     m_blockSize = blockSize;
84
85     m_ibuf = new_fvec(stepSize, channels);
86     m_pbuf = new_fvec(stepSize, channels);
87     m_tmpptrs = new smpl_t *[channels];
88
89     return true;
90 }
91
92 void
93 Silence::reset()
94 {
95     m_first = true;
96 }
97
98 size_t
99 Silence::getPreferredStepSize() const
100 {
101     return 1024;
102 }
103
104 size_t
105 Silence::getPreferredBlockSize() const
106 {
107     return 1024;
108 }
109
110 Silence::ParameterList
111 Silence::getParameterDescriptors() const
112 {
113     ParameterList list;
114     ParameterDescriptor desc;
115
116     desc = ParameterDescriptor();
117     desc.identifier = "silencethreshold";
118     desc.name = "Silence Threshold";
119     desc.minValue = -120;
120     desc.maxValue = 0;
121     desc.defaultValue = -80;
122     desc.unit = "dB";
123     desc.isQuantized = false;
124     list.push_back(desc);
125
126     return list;
127 }
128
129 float
130 Silence::getParameter(std::string param) const
131 {
132     if (param == "silencethreshold") {
133         return m_threshold;
134     } else {
135         return 0.0;
136     }
137 }
138
139 void
140 Silence::setParameter(std::string param, float value)
141 {
142     if (param == "silencethreshold") {
143         m_threshold = value;
144     }
145 }
146
147 Silence::OutputList
148 Silence::getOutputDescriptors() const
149 {
150     OutputList list;
151
152     OutputDescriptor d;
153
154     d.identifier = "silent";
155     d.name = "Silent Regions";
156     d.description = "Return an interval covering each silent region";
157     d.hasFixedBinCount = true;
158     d.binCount = 0;
159     d.hasKnownExtents = false;
160     d.sampleType = OutputDescriptor::VariableSampleRate;
161     d.sampleRate = 0;
162     d.hasDuration = true;
163     list.push_back(d);
164
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;
169     d.binCount = 0;
170     d.hasKnownExtents = false;
171     d.sampleType = OutputDescriptor::VariableSampleRate;
172     d.sampleRate = 0;
173     d.hasDuration = true;
174     list.push_back(d);
175
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;
180     d.binCount = 1;
181     d.hasKnownExtents = true;
182     d.minValue = 0;
183     d.maxValue = 1;
184     d.isQuantized = true;
185     d.quantizeStep = 1;
186     d.sampleType = OutputDescriptor::VariableSampleRate;
187     d.sampleRate = 0;
188     list.push_back(d);
189
190     return list;
191 }
192
193 Silence::FeatureSet
194 Silence::process(const float *const *inputBuffers,
195                  Vamp::RealTime timestamp)
196 {
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);
200         }
201     }
202
203     bool silent = aubio_silence_detection(m_ibuf, m_threshold);
204     FeatureSet returnFeatures;
205
206     if (m_first || m_prevSilent != silent) {
207
208         Vamp::RealTime featureStamp = timestamp;
209
210         if ((silent && !m_first) || !silent) {
211         
212             // refine our result
213
214             long off = 0;
215             size_t incr = 16;
216             if (incr > m_stepSize/8) incr = m_stepSize/8;
217
218             fvec_t vec;
219             vec.length = incr * 4;
220             vec.channels = m_channelCount;
221             vec.data = m_tmpptrs;
222             
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;
226                 }
227                 bool subsilent = aubio_silence_detection(&vec, m_threshold);
228                 if (silent == subsilent) {
229                     off = i;
230                     break;
231                 }
232             }
233
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;
238                     }
239                     bool subsilent = aubio_silence_detection(&vec, m_threshold);
240                     if (!subsilent) {
241                         off = -(long)i;
242                         break;
243                     }
244                 }
245             } else {
246             }                
247
248             featureStamp = timestamp + Vamp::RealTime::frame2RealTime
249                 (off, lrintf(m_inputSampleRate));
250         }
251
252         Feature feature;
253         feature.hasTimestamp = true;
254         feature.timestamp = featureStamp;
255         feature.values.push_back(silent ? 0 : 1);
256         returnFeatures[2].push_back(feature);
257
258         feature.values.clear();
259
260         if (!m_first) {
261             feature.timestamp = m_lastChange;
262             feature.hasDuration = true;
263             feature.duration = featureStamp - m_lastChange;
264             if (silent) {
265                 // non-silent regions feature
266                 // (becoming silent, so this is a non-silent region)
267                 returnFeatures[1].push_back(feature);
268             } else {
269                 // silent regions feature
270                 // (becoming non-silent, so this is a silent region)
271                 returnFeatures[0].push_back(feature);
272             }                    
273         }
274         m_lastChange = featureStamp;
275
276         m_prevSilent = silent;
277         m_first = false;
278     }
279
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;
286
287     m_lastTimestamp = timestamp;
288
289     return returnFeatures;
290 }
291
292 Silence::FeatureSet
293 Silence::getRemainingFeatures()
294 {
295     FeatureSet returnFeatures;
296     
297 //    std::cerr << "Silence::getRemainingFeatures: m_lastTimestamp = " << m_lastTimestamp << ", m_lastChange = " << m_lastChange << ", m_apiVersion = " << m_apiVersion << ", m_prevSilent = " << m_prevSilent << std::endl;
298
299     if (m_lastTimestamp > m_lastChange) {
300
301         Feature feature;
302         feature.hasTimestamp = true;
303
304         feature.timestamp = m_lastChange;
305         feature.hasDuration = true;
306         feature.duration = m_lastTimestamp - m_lastChange;
307         if (m_prevSilent) {
308             // silent regions feature
309             returnFeatures[0].push_back(feature);
310         } else {
311             // non-silent regions feature
312             returnFeatures[1].push_back(feature);
313         }
314
315         if (!m_prevSilent) {
316             Feature silenceTestFeature;
317             silenceTestFeature.hasTimestamp = true;
318             silenceTestFeature.timestamp = m_lastTimestamp;
319             silenceTestFeature.values.push_back(0);
320             returnFeatures[2].push_back(silenceTestFeature);
321         }
322     }
323
324     return returnFeatures;
325 }
326