Makefile.linux: use same locations as for scripts/get_aubio.sh, scripts/get_deps*
[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 file is part of vamp-aubio-plugins.
10
11     vamp-aubio is free software: you can redistribute it and/or modify
12     it under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
15
16     vamp-aubio is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20
21     You should have received a copy of the GNU General Public License
22     along with aubio.  If not, see <http://www.gnu.org/licenses/>.
23
24 */
25
26 #include <math.h>
27 #include "Silence.h"
28
29 using std::string;
30 using std::vector;
31 using std::cerr;
32 using std::endl;
33
34 Silence::Silence(float inputSampleRate) :
35     Plugin(inputSampleRate),
36     m_ibuf(0),
37     m_pbuf(0),
38     m_threshold(-80),
39     m_prevSilent(false),
40     m_first(true)
41 {
42 }
43
44 Silence::~Silence()
45 {
46     if (m_ibuf) del_fvec(m_ibuf);
47     if (m_pbuf) del_fvec(m_pbuf);
48 }
49
50 string
51 Silence::getIdentifier() const
52 {
53     return "aubiosilence";
54 }
55
56 string
57 Silence::getName() const
58 {
59     return "Aubio Silence Detector";
60 }
61
62 string
63 Silence::getDescription() const
64 {
65     return "Detect levels below a certain threshold";
66 }
67
68 string
69 Silence::getMaker() const
70 {
71     return "Paul Brossier (plugin by Chris Cannam)";
72 }
73
74 int
75 Silence::getPluginVersion() const
76 {
77     return 4;
78 }
79
80 string
81 Silence::getCopyright() const
82 {
83     return "GPL";
84 }
85
86 bool
87 Silence::initialise(size_t channels, size_t stepSize, size_t blockSize)
88 {
89     if (channels != 1) {
90         std::cerr << "Silence::initialise: channels must be 1" << std::endl;
91         return false;
92     }
93
94     m_stepSize = stepSize;
95     m_blockSize = blockSize;
96
97     m_ibuf = new_fvec(stepSize);
98     m_pbuf = new_fvec(stepSize);
99
100     return true;
101 }
102
103 void
104 Silence::reset()
105 {
106     m_first = true;
107 }
108
109 size_t
110 Silence::getPreferredStepSize() const
111 {
112     return 1024;
113 }
114
115 size_t
116 Silence::getPreferredBlockSize() const
117 {
118     return 1024;
119 }
120
121 Silence::ParameterList
122 Silence::getParameterDescriptors() const
123 {
124     ParameterList list;
125     ParameterDescriptor desc;
126
127     desc = ParameterDescriptor();
128     desc.identifier = "silencethreshold";
129     desc.name = "Silence Threshold";
130     desc.description = "Threshold for silence detection";
131     desc.minValue = -120;
132     desc.maxValue = 0;
133     desc.defaultValue = -80;
134     desc.unit = "dB";
135     desc.isQuantized = false;
136     list.push_back(desc);
137
138     return list;
139 }
140
141 float
142 Silence::getParameter(std::string param) const
143 {
144     if (param == "silencethreshold") {
145         return m_threshold;
146     } else {
147         return 0.0;
148     }
149 }
150
151 void
152 Silence::setParameter(std::string param, float value)
153 {
154     if (param == "silencethreshold") {
155         m_threshold = value;
156     }
157 }
158
159 Silence::OutputList
160 Silence::getOutputDescriptors() const
161 {
162     OutputList list;
163
164     OutputDescriptor d;
165
166     d.identifier = "silent";
167     d.name = "Silent Regions";
168     d.description = "Return an interval covering each 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 = "noisy";
178     d.name = "Non-Silent Regions";
179     d.description = "Return an interval covering each non-silent region";
180     d.hasFixedBinCount = true;
181     d.binCount = 0;
182     d.hasKnownExtents = false;
183     d.sampleType = OutputDescriptor::VariableSampleRate;
184     d.sampleRate = 0;
185     d.hasDuration = true;
186     list.push_back(d);
187
188     d.identifier = "silencelevel";
189     d.name = "Silence Test";
190     d.description = "Return a function that switches from 1 to 0 when silence falls, and back again when it ends";
191     d.hasFixedBinCount = true;
192     d.binCount = 1;
193     d.hasKnownExtents = true;
194     d.minValue = 0;
195     d.maxValue = 1;
196     d.isQuantized = true;
197     d.quantizeStep = 1;
198     d.sampleType = OutputDescriptor::VariableSampleRate;
199     d.sampleRate = 0;
200     list.push_back(d);
201
202     return list;
203 }
204
205 Silence::FeatureSet
206 Silence::process(const float *const *inputBuffers,
207                  Vamp::RealTime timestamp)
208 {
209     for (size_t i = 0; i < m_stepSize; ++i) {
210         fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
211     }
212
213     bool silent = aubio_silence_detection(m_ibuf, m_threshold);
214     FeatureSet returnFeatures;
215
216     if (m_first || m_prevSilent != silent) {
217
218         Vamp::RealTime featureStamp = timestamp;
219
220         if ((silent && !m_first) || !silent) {
221         
222             // refine our result
223
224             long off = 0;
225             size_t incr = 16;
226             if (incr > m_stepSize/8) incr = m_stepSize/8;
227
228             fvec_t vec;
229             vec.length = incr * 4;
230             
231             for (size_t i = 0; i < m_stepSize - incr * 4; i += incr) {
232                 vec.data = m_ibuf->data + i;
233                 bool subsilent = aubio_silence_detection(&vec, m_threshold);
234                 if (silent == subsilent) {
235                     off = i;
236                     break;
237                 }
238             }
239
240             if (silent && (off == 0)) {
241                 for (size_t i = 0; i < m_stepSize - incr; i += incr) {
242                     vec.data = m_pbuf->data + m_stepSize - i - incr;
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