cf3dda4f91feb755e757c5f88147ab8eb216aab6
[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     if (channels != 1) {
82         std::cerr << "Silence::initialise: channels must be 1" << std::endl;
83         return false;
84     }
85
86     m_stepSize = stepSize;
87     m_blockSize = blockSize;
88
89     m_ibuf = new_fvec(stepSize);
90     m_pbuf = new_fvec(stepSize);
91     m_tmpptrs = new smpl_t *[channels];
92
93     return true;
94 }
95
96 void
97 Silence::reset()
98 {
99     m_first = true;
100 }
101
102 size_t
103 Silence::getPreferredStepSize() const
104 {
105     return 1024;
106 }
107
108 size_t
109 Silence::getPreferredBlockSize() const
110 {
111     return 1024;
112 }
113
114 Silence::ParameterList
115 Silence::getParameterDescriptors() const
116 {
117     ParameterList list;
118     ParameterDescriptor desc;
119
120     desc = ParameterDescriptor();
121     desc.identifier = "silencethreshold";
122     desc.name = "Silence Threshold";
123     desc.minValue = -120;
124     desc.maxValue = 0;
125     desc.defaultValue = -80;
126     desc.unit = "dB";
127     desc.isQuantized = false;
128     list.push_back(desc);
129
130     return list;
131 }
132
133 float
134 Silence::getParameter(std::string param) const
135 {
136     if (param == "silencethreshold") {
137         return m_threshold;
138     } else {
139         return 0.0;
140     }
141 }
142
143 void
144 Silence::setParameter(std::string param, float value)
145 {
146     if (param == "silencethreshold") {
147         m_threshold = value;
148     }
149 }
150
151 Silence::OutputList
152 Silence::getOutputDescriptors() const
153 {
154     OutputList list;
155
156     OutputDescriptor d;
157
158     d.identifier = "silent";
159     d.name = "Silent Regions";
160     d.description = "Return an interval covering each silent region";
161     d.hasFixedBinCount = true;
162     d.binCount = 0;
163     d.hasKnownExtents = false;
164     d.sampleType = OutputDescriptor::VariableSampleRate;
165     d.sampleRate = 0;
166     d.hasDuration = true;
167     list.push_back(d);
168
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;
173     d.binCount = 0;
174     d.hasKnownExtents = false;
175     d.sampleType = OutputDescriptor::VariableSampleRate;
176     d.sampleRate = 0;
177     d.hasDuration = true;
178     list.push_back(d);
179
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;
184     d.binCount = 1;
185     d.hasKnownExtents = true;
186     d.minValue = 0;
187     d.maxValue = 1;
188     d.isQuantized = true;
189     d.quantizeStep = 1;
190     d.sampleType = OutputDescriptor::VariableSampleRate;
191     d.sampleRate = 0;
192     list.push_back(d);
193
194     return list;
195 }
196
197 Silence::FeatureSet
198 Silence::process(const float *const *inputBuffers,
199                  Vamp::RealTime timestamp)
200 {
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);
204         }
205     }
206
207     bool silent = aubio_silence_detection(m_ibuf, m_threshold);
208     FeatureSet returnFeatures;
209
210     if (m_first || m_prevSilent != silent) {
211
212         Vamp::RealTime featureStamp = timestamp;
213
214         if ((silent && !m_first) || !silent) {
215         
216             // refine our result
217
218             long off = 0;
219             size_t incr = 16;
220             if (incr > m_stepSize/8) incr = m_stepSize/8;
221
222             fvec_t vec;
223             vec.length = incr * 4;
224             vec.channels = m_channelCount;
225             vec.data = m_tmpptrs;
226             
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;
230                 }
231                 bool subsilent = aubio_silence_detection(&vec, m_threshold);
232                 if (silent == subsilent) {
233                     off = i;
234                     break;
235                 }
236             }
237
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;
242                     }
243                     bool subsilent = aubio_silence_detection(&vec, m_threshold);
244                     if (!subsilent) {
245                         off = -(long)i;
246                         break;
247                     }
248                 }
249             } else {
250             }                
251
252             featureStamp = timestamp + Vamp::RealTime::frame2RealTime
253                 (off, lrintf(m_inputSampleRate));
254         }
255
256         Feature feature;
257         feature.hasTimestamp = true;
258         feature.timestamp = featureStamp;
259         feature.values.push_back(silent ? 0 : 1);
260         returnFeatures[2].push_back(feature);
261
262         feature.values.clear();
263
264         if (!m_first) {
265             feature.timestamp = m_lastChange;
266             feature.hasDuration = true;
267             feature.duration = featureStamp - m_lastChange;
268             if (silent) {
269                 // non-silent regions feature
270                 // (becoming silent, so this is a non-silent region)
271                 returnFeatures[1].push_back(feature);
272             } else {
273                 // silent regions feature
274                 // (becoming non-silent, so this is a silent region)
275                 returnFeatures[0].push_back(feature);
276             }                    
277         }
278         m_lastChange = featureStamp;
279
280         m_prevSilent = silent;
281         m_first = false;
282     }
283
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;
290
291     m_lastTimestamp = timestamp;
292
293     return returnFeatures;
294 }
295
296 Silence::FeatureSet
297 Silence::getRemainingFeatures()
298 {
299     FeatureSet returnFeatures;
300     
301 //    std::cerr << "Silence::getRemainingFeatures: m_lastTimestamp = " << m_lastTimestamp << ", m_lastChange = " << m_lastChange << ", m_apiVersion = " << m_apiVersion << ", m_prevSilent = " << m_prevSilent << std::endl;
302
303     if (m_lastTimestamp > m_lastChange) {
304
305         Feature feature;
306         feature.hasTimestamp = true;
307
308         feature.timestamp = m_lastChange;
309         feature.hasDuration = true;
310         feature.duration = m_lastTimestamp - m_lastChange;
311         if (m_prevSilent) {
312             // silent regions feature
313             returnFeatures[0].push_back(feature);
314         } else {
315             // non-silent regions feature
316             returnFeatures[1].push_back(feature);
317         }
318
319         if (!m_prevSilent) {
320             Feature silenceTestFeature;
321             silenceTestFeature.hasTimestamp = true;
322             silenceTestFeature.timestamp = m_lastTimestamp;
323             silenceTestFeature.values.push_back(0);
324             returnFeatures[2].push_back(silenceTestFeature);
325         }
326     }
327
328     return returnFeatures;
329 }
330