57a0420c4172414288a5acf557fe336d022af614
[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         d.hasDuration = true;
193         list.push_back(d);
194
195         d.identifier = "noisy";
196         d.name = "Non-Silent Regions";
197         d.description = "Return an interval covering each non-silent region";
198         d.hasFixedBinCount = true;
199         d.binCount = 0;
200         d.hasKnownExtents = false;
201         d.sampleType = OutputDescriptor::VariableSampleRate;
202         d.sampleRate = 0;
203         d.hasDuration = true;
204         list.push_back(d);
205     }
206
207     d.identifier = "silencelevel";
208     d.name = "Silence Test";
209     d.description = "Return a function that switches from 1 to 0 when silence falls, and back again when it ends";
210     d.hasFixedBinCount = true;
211     d.binCount = 1;
212     d.hasKnownExtents = true;
213     d.minValue = 0;
214     d.maxValue = 1;
215     d.isQuantized = true;
216     d.quantizeStep = 1;
217     d.sampleType = OutputDescriptor::VariableSampleRate;
218     d.sampleRate = 0;
219     list.push_back(d);
220
221     return list;
222 }
223
224 Silence::FeatureSet
225 Silence::process(const float *const *inputBuffers,
226                  Vamp::RealTime timestamp)
227 {
228     for (size_t i = 0; i < m_stepSize; ++i) {
229         for (size_t j = 0; j < m_channelCount; ++j) {
230             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
231         }
232     }
233
234     bool silent = aubio_silence_detection(m_ibuf, m_threshold);
235     FeatureSet returnFeatures;
236
237     if (m_first || m_prevSilent != silent) {
238
239         Vamp::RealTime featureStamp = timestamp;
240
241         if ((silent && !m_first) || !silent) {
242         
243             // refine our result
244
245             long off = 0;
246             size_t incr = 16;
247             if (incr > m_stepSize/8) incr = m_stepSize/8;
248
249             fvec_t vec;
250             vec.length = incr * 4;
251             vec.channels = m_channelCount;
252             vec.data = m_tmpptrs;
253             
254             for (size_t i = 0; i < m_stepSize - incr * 4; i += incr) {
255                 for (size_t j = 0; j < m_channelCount; ++j) {
256                     m_tmpptrs[j] = m_ibuf->data[j] + i;
257                 }
258                 bool subsilent = aubio_silence_detection(&vec, m_threshold);
259                 if (silent == subsilent) {
260                     off = i;
261                     break;
262                 }
263             }
264
265             if (silent && (off == 0)) {
266                 for (size_t i = 0; i < m_stepSize - incr; i += incr) {
267                     for (size_t j = 0; j < m_channelCount; ++j) {
268                         m_tmpptrs[j] = m_pbuf->data[j] + m_stepSize - i - incr;
269                     }
270                     bool subsilent = aubio_silence_detection(&vec, m_threshold);
271                     if (!subsilent) {
272                         off = -(long)i;
273                         break;
274                     }
275                 }
276             } else {
277             }                
278
279             featureStamp = timestamp + Vamp::RealTime::frame2RealTime
280                 (off, lrintf(m_inputSampleRate));
281         }
282
283         Feature feature;
284         feature.hasTimestamp = true;
285         feature.timestamp = featureStamp;
286         feature.values.push_back(silent ? 0 : 1);
287         returnFeatures[2].push_back(feature);
288
289         feature.values.clear();
290
291         if (m_apiVersion == 1) {
292             if (silent) {
293                 // silencestart feature
294                 returnFeatures[0].push_back(feature);
295             } else {
296                 // silenceend feature
297                 returnFeatures[1].push_back(feature);
298             }
299         } else {
300             if (!m_first) {
301                 feature.timestamp = m_lastChange;
302                 feature.hasDuration = true;
303                 feature.duration = featureStamp - m_lastChange;
304                 if (silent) {
305                     // non-silent regions feature
306                     // (becoming silent, so this is a non-silent region)
307                     returnFeatures[1].push_back(feature);
308                 } else {
309                     // silent regions feature
310                     // (becoming non-silent, so this is a silent region)
311                     returnFeatures[0].push_back(feature);
312                 }                    
313             }
314             m_lastChange = featureStamp;
315         }
316
317         m_prevSilent = silent;
318         m_first = false;
319     }
320
321     // swap ibuf and pbuf data pointers, so that this block's data is
322     // available in pbuf when processing the next block, without
323     // having to allocate new storage for it
324     smpl_t **tmpdata = m_ibuf->data;
325     m_ibuf->data = m_pbuf->data;
326     m_pbuf->data = tmpdata;
327
328     m_lastTimestamp = timestamp;
329
330     return returnFeatures;
331 }
332
333 Silence::FeatureSet
334 Silence::getRemainingFeatures()
335 {
336     FeatureSet returnFeatures;
337     
338 //    std::cerr << "Silence::getRemainingFeatures: m_lastTimestamp = " << m_lastTimestamp << ", m_lastChange = " << m_lastChange << ", m_apiVersion = " << m_apiVersion << ", m_prevSilent = " << m_prevSilent << std::endl;
339
340     if (m_lastTimestamp > m_lastChange) {
341
342         Feature feature;
343         feature.hasTimestamp = true;
344
345         if (m_apiVersion == 1) {
346             if (m_prevSilent) {
347                 // silenceend feature
348                 feature.timestamp = m_lastTimestamp;
349                 returnFeatures[1].push_back(feature);
350             }
351         } else {
352
353             feature.timestamp = m_lastChange;
354             feature.hasDuration = true;
355             feature.duration = m_lastTimestamp - m_lastChange;
356             if (m_prevSilent) {
357                 // silent regions feature
358                 returnFeatures[0].push_back(feature);
359             } else {
360                 // non-silent regions feature
361                 returnFeatures[1].push_back(feature);
362             }                
363         }
364
365         if (!m_prevSilent) {
366             Feature silenceTestFeature;
367             silenceTestFeature.hasTimestamp = true;
368             silenceTestFeature.timestamp = m_lastTimestamp;
369             silenceTestFeature.values.push_back(0);
370             returnFeatures[2].push_back(silenceTestFeature);
371         }
372     }
373
374     return returnFeatures;
375 }
376