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