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