...
[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 Chris Cannam.
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 1;
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     m_channelCount = channels;
82     m_stepSize = stepSize;
83     m_blockSize = blockSize;
84
85     m_ibuf = new_fvec(stepSize, channels);
86     m_pbuf = new_fvec(stepSize, channels);
87     m_tmpptrs = new smpl_t *[channels];
88
89     return true;
90 }
91
92 void
93 Silence::reset()
94 {
95     m_first = true;
96 }
97
98 size_t
99 Silence::getPreferredStepSize() const
100 {
101     return 1024;
102 }
103
104 size_t
105 Silence::getPreferredBlockSize() const
106 {
107     return 1024;
108 }
109
110 Silence::ParameterList
111 Silence::getParameterDescriptors() const
112 {
113     ParameterList list;
114     ParameterDescriptor desc;
115
116     desc = ParameterDescriptor();
117     desc.identifier = "silencethreshold";
118     desc.name = "Silence Threshold";
119     desc.minValue = -120;
120     desc.maxValue = 0;
121     desc.defaultValue = -80;
122     desc.unit = "dB";
123     desc.isQuantized = false;
124     list.push_back(desc);
125
126     return list;
127 }
128
129 float
130 Silence::getParameter(std::string param) const
131 {
132     if (param == "silencethreshold") {
133         return m_threshold;
134     } else {
135         return 0.0;
136     }
137 }
138
139 void
140 Silence::setParameter(std::string param, float value)
141 {
142     if (param == "silencethreshold") {
143         m_threshold = value;
144     }
145 }
146
147 Silence::OutputList
148 Silence::getOutputDescriptors() const
149 {
150     OutputList list;
151
152     OutputDescriptor d;
153     d.identifier = "silencestart";
154     d.name = "Beginnings of Silent Regions";
155     d.description = "Return a single instant at the point where each silent region begins";
156     d.hasFixedBinCount = true;
157     d.binCount = 0;
158     d.sampleType = OutputDescriptor::VariableSampleRate;
159     d.sampleRate = 0;
160     list.push_back(d);
161
162     d.identifier = "silenceend";
163     d.name = "Ends of Silent Regions";
164     d.description = "Return a single instant at the point where each silent region ends";
165     d.hasFixedBinCount = true;
166     d.binCount = 0;
167     d.sampleType = OutputDescriptor::VariableSampleRate;
168     d.sampleRate = 0;
169     list.push_back(d);
170
171     d.identifier = "silencelevel";
172     d.name = "Silence Test";
173     d.description = "Return a function that switches from 1 to 0 when silence falls, and back again when it ends";
174     d.hasFixedBinCount = true;
175     d.binCount = 1;
176     d.hasKnownExtents = true;
177     d.minValue = 0;
178     d.maxValue = 1;
179     d.isQuantized = true;
180     d.quantizeStep = 1;
181     d.sampleType = OutputDescriptor::VariableSampleRate;
182     d.sampleRate = 0;
183     list.push_back(d);
184
185     return list;
186 }
187
188 Silence::FeatureSet
189 Silence::process(const float *const *inputBuffers,
190                  Vamp::RealTime timestamp)
191 {
192     for (size_t i = 0; i < m_stepSize; ++i) {
193         for (size_t j = 0; j < m_channelCount; ++j) {
194             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
195         }
196     }
197
198     bool silent = aubio_silence_detection(m_ibuf, m_threshold);
199     FeatureSet returnFeatures;
200
201     if (m_first || m_prevSilent != silent) {
202
203         Vamp::RealTime featureStamp = timestamp;
204
205         if ((silent && !m_first) || !silent) {
206         
207             // refine our result
208
209             long off = 0;
210             size_t incr = 16;
211             if (incr > m_stepSize/8) incr = m_stepSize/8;
212
213             fvec_t vec;
214             vec.length = incr * 4;
215             vec.channels = m_channelCount;
216             vec.data = m_tmpptrs;
217             
218             for (size_t i = 0; i < m_stepSize - incr * 4; i += incr) {
219                 for (size_t j = 0; j < m_channelCount; ++j) {
220                     m_tmpptrs[j] = m_ibuf->data[j] + i;
221                 }
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                     for (size_t j = 0; j < m_channelCount; ++j) {
232                         m_tmpptrs[j] = m_pbuf->data[j] + m_stepSize - i - incr;
233                     }
234                     bool subsilent = aubio_silence_detection(&vec, m_threshold);
235                     if (!subsilent) {
236                         off = -(long)i;
237                         break;
238                     }
239                 }
240             } else {
241             }                
242
243             featureStamp = timestamp + Vamp::RealTime::frame2RealTime
244                 (off, lrintf(m_inputSampleRate));
245         }
246
247         Feature feature;
248         feature.hasTimestamp = true;
249         feature.timestamp = featureStamp;
250         feature.values.push_back(silent ? 0 : 1);
251         returnFeatures[2].push_back(feature);
252         feature.values.clear();
253
254         if (silent) {
255             returnFeatures[0].push_back(feature);
256         } else {
257             returnFeatures[1].push_back(feature);
258         }            
259
260         m_prevSilent = silent;
261         m_first = false;
262     }
263
264     // swap ibuf and pbuf data pointers, so that this block's data is
265     // available in pbuf when processing the next block, without
266     // having to allocate new storage for it
267     smpl_t **tmpdata = m_ibuf->data;
268     m_ibuf->data = m_pbuf->data;
269     m_pbuf->data = tmpdata;
270
271     return returnFeatures;
272 }
273
274 Silence::FeatureSet
275 Silence::getRemainingFeatures()
276 {
277     return FeatureSet();
278 }
279