489bbb0446624e8e79274ece95215a067db73cd0
[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.minValue = -120;
121     desc.maxValue = 0;
122     desc.defaultValue = -80;
123     desc.unit = "dB";
124     desc.isQuantized = false;
125     list.push_back(desc);
126
127     return list;
128 }
129
130 float
131 Silence::getParameter(std::string param) const
132 {
133     if (param == "silencethreshold") {
134         return m_threshold;
135     } else {
136         return 0.0;
137     }
138 }
139
140 void
141 Silence::setParameter(std::string param, float value)
142 {
143     if (param == "silencethreshold") {
144         m_threshold = value;
145     }
146 }
147
148 Silence::OutputList
149 Silence::getOutputDescriptors() const
150 {
151     OutputList list;
152
153     OutputDescriptor d;
154
155     d.identifier = "silent";
156     d.name = "Silent Regions";
157     d.description = "Return an interval covering each silent region";
158     d.hasFixedBinCount = true;
159     d.binCount = 0;
160     d.hasKnownExtents = false;
161     d.sampleType = OutputDescriptor::VariableSampleRate;
162     d.sampleRate = 0;
163     d.hasDuration = true;
164     list.push_back(d);
165
166     d.identifier = "noisy";
167     d.name = "Non-Silent Regions";
168     d.description = "Return an interval covering each non-silent region";
169     d.hasFixedBinCount = true;
170     d.binCount = 0;
171     d.hasKnownExtents = false;
172     d.sampleType = OutputDescriptor::VariableSampleRate;
173     d.sampleRate = 0;
174     d.hasDuration = true;
175     list.push_back(d);
176
177     d.identifier = "silencelevel";
178     d.name = "Silence Test";
179     d.description = "Return a function that switches from 1 to 0 when silence falls, and back again when it ends";
180     d.hasFixedBinCount = true;
181     d.binCount = 1;
182     d.hasKnownExtents = true;
183     d.minValue = 0;
184     d.maxValue = 1;
185     d.isQuantized = true;
186     d.quantizeStep = 1;
187     d.sampleType = OutputDescriptor::VariableSampleRate;
188     d.sampleRate = 0;
189     list.push_back(d);
190
191     return list;
192 }
193
194 Silence::FeatureSet
195 Silence::process(const float *const *inputBuffers,
196                  Vamp::RealTime timestamp)
197 {
198     for (size_t i = 0; i < m_stepSize; ++i) {
199         fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
200     }
201
202     bool silent = aubio_silence_detection(m_ibuf, m_threshold);
203     FeatureSet returnFeatures;
204
205     if (m_first || m_prevSilent != silent) {
206
207         Vamp::RealTime featureStamp = timestamp;
208
209         if ((silent && !m_first) || !silent) {
210         
211             // refine our result
212
213             long off = 0;
214             size_t incr = 16;
215             if (incr > m_stepSize/8) incr = m_stepSize/8;
216
217             fvec_t vec;
218             vec.length = incr * 4;
219             
220             for (size_t i = 0; i < m_stepSize - incr * 4; i += incr) {
221                 vec.data = m_ibuf->data + i;
222                 bool subsilent = aubio_silence_detection(&vec, m_threshold);
223                 if (silent == subsilent) {
224                     off = i;
225                     break;
226                 }
227             }
228
229             if (silent && (off == 0)) {
230                 for (size_t i = 0; i < m_stepSize - incr; i += incr) {
231                     vec.data = m_pbuf->data + m_stepSize - i - incr;
232                     bool subsilent = aubio_silence_detection(&vec, m_threshold);
233                     if (!subsilent) {
234                         off = -(long)i;
235                         break;
236                     }
237                 }
238             } else {
239             }                
240
241             featureStamp = timestamp + Vamp::RealTime::frame2RealTime
242                 (off, lrintf(m_inputSampleRate));
243         }
244
245         Feature feature;
246         feature.hasTimestamp = true;
247         feature.timestamp = featureStamp;
248         feature.values.push_back(silent ? 0 : 1);
249         returnFeatures[2].push_back(feature);
250
251         feature.values.clear();
252
253         if (!m_first) {
254             feature.timestamp = m_lastChange;
255             feature.hasDuration = true;
256             feature.duration = featureStamp - m_lastChange;
257             if (silent) {
258                 // non-silent regions feature
259                 // (becoming silent, so this is a non-silent region)
260                 returnFeatures[1].push_back(feature);
261             } else {
262                 // silent regions feature
263                 // (becoming non-silent, so this is a silent region)
264                 returnFeatures[0].push_back(feature);
265             }                    
266         }
267         m_lastChange = featureStamp;
268
269         m_prevSilent = silent;
270         m_first = false;
271     }
272
273     // swap ibuf and pbuf data pointers, so that this block's data is
274     // available in pbuf when processing the next block, without
275     // having to allocate new storage for it
276     smpl_t *tmpdata = m_ibuf->data;
277     m_ibuf->data = m_pbuf->data;
278     m_pbuf->data = tmpdata;
279
280     m_lastTimestamp = timestamp;
281
282     return returnFeatures;
283 }
284
285 Silence::FeatureSet
286 Silence::getRemainingFeatures()
287 {
288     FeatureSet returnFeatures;
289     
290 //    std::cerr << "Silence::getRemainingFeatures: m_lastTimestamp = " << m_lastTimestamp << ", m_lastChange = " << m_lastChange << ", m_apiVersion = " << m_apiVersion << ", m_prevSilent = " << m_prevSilent << std::endl;
291
292     if (m_lastTimestamp > m_lastChange) {
293
294         Feature feature;
295         feature.hasTimestamp = true;
296
297         feature.timestamp = m_lastChange;
298         feature.hasDuration = true;
299         feature.duration = m_lastTimestamp - m_lastChange;
300         if (m_prevSilent) {
301             // silent regions feature
302             returnFeatures[0].push_back(feature);
303         } else {
304             // non-silent regions feature
305             returnFeatures[1].push_back(feature);
306         }
307
308         if (!m_prevSilent) {
309             Feature silenceTestFeature;
310             silenceTestFeature.hasTimestamp = true;
311             silenceTestFeature.timestamp = m_lastTimestamp;
312             silenceTestFeature.values.push_back(0);
313             returnFeatures[2].push_back(silenceTestFeature);
314         }
315     }
316
317     return returnFeatures;
318 }
319