piem@69
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
piem@69
|
2
|
piem@69
|
3 /*
|
piem@69
|
4 Vamp feature extraction plugins using Paul Brossier's Aubio library.
|
piem@69
|
5
|
piem@69
|
6 Copyright (C) 2006-2015 Paul Brossier <piem@aubio.org>
|
piem@69
|
7
|
piem@110
|
8 This file is part of vamp-aubio-plugins.
|
piem@69
|
9
|
piem@69
|
10 vamp-aubio is free software: you can redistribute it and/or modify
|
piem@69
|
11 it under the terms of the GNU General Public License as published by
|
piem@69
|
12 the Free Software Foundation, either version 3 of the License, or
|
piem@69
|
13 (at your option) any later version.
|
piem@69
|
14
|
piem@69
|
15 vamp-aubio is distributed in the hope that it will be useful,
|
piem@69
|
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
piem@69
|
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
piem@69
|
18 GNU General Public License for more details.
|
piem@69
|
19
|
piem@69
|
20 You should have received a copy of the GNU General Public License
|
piem@69
|
21 along with aubio. If not, see <http://www.gnu.org/licenses/>.
|
piem@69
|
22
|
piem@69
|
23 */
|
piem@69
|
24
|
piem@69
|
25 #include <math.h>
|
piem@69
|
26 #include "Mfcc.h"
|
piem@69
|
27
|
piem@69
|
28 using std::string;
|
piem@69
|
29 using std::vector;
|
piem@69
|
30 using std::cerr;
|
piem@69
|
31 using std::endl;
|
piem@69
|
32
|
piem@69
|
33 Mfcc::Mfcc(float inputSampleRate) :
|
piem@69
|
34 Plugin(inputSampleRate),
|
piem@69
|
35 m_ibuf(0), // input fvec_t, set in initialise
|
piem@69
|
36 m_pvoc(0), // aubio_pvoc_t, set in reset
|
piem@69
|
37 m_ispec(0), // cvec_t, set in initialise
|
piem@69
|
38 m_mfcc(0), // aubio_mfcc_t, set in reset
|
piem@69
|
39 m_ovec(0), // output fvec_t, set in initialise
|
piem@69
|
40 m_nfilters(40), // parameter
|
piem@69
|
41 m_ncoeffs(13), // parameter
|
piem@69
|
42 m_stepSize(0), // host parameter
|
piem@69
|
43 m_blockSize(0) // host parameter
|
piem@69
|
44 {
|
piem@69
|
45 }
|
piem@69
|
46
|
piem@69
|
47 Mfcc::~Mfcc()
|
piem@69
|
48 {
|
piem@69
|
49 if (m_mfcc) del_aubio_mfcc(m_mfcc);
|
piem@69
|
50 if (m_pvoc) del_aubio_pvoc(m_pvoc);
|
piem@69
|
51 if (m_ibuf) del_fvec(m_ibuf);
|
piem@69
|
52 if (m_ispec) del_cvec(m_ispec);
|
piem@69
|
53 if (m_ovec) del_fvec(m_ovec);
|
piem@69
|
54 }
|
piem@69
|
55
|
piem@69
|
56 string
|
piem@69
|
57 Mfcc::getIdentifier() const
|
piem@69
|
58 {
|
piem@69
|
59 return "aubiomfcc";
|
piem@69
|
60 }
|
piem@69
|
61
|
piem@69
|
62 string
|
piem@69
|
63 Mfcc::getName() const
|
piem@69
|
64 {
|
piem@69
|
65 return "Aubio Mfcc Extractor";
|
piem@69
|
66 }
|
piem@69
|
67
|
piem@69
|
68 string
|
piem@69
|
69 Mfcc::getDescription() const
|
piem@69
|
70 {
|
piem@69
|
71 return "Extract Mel-Frequency Cepstrum Coefficients";
|
piem@69
|
72 }
|
piem@69
|
73
|
piem@69
|
74 string
|
piem@69
|
75 Mfcc::getMaker() const
|
piem@69
|
76 {
|
piem@69
|
77 return "Paul Brossier";
|
piem@69
|
78 }
|
piem@69
|
79
|
piem@69
|
80 int
|
piem@69
|
81 Mfcc::getPluginVersion() const
|
piem@69
|
82 {
|
piem@69
|
83 return 3;
|
piem@69
|
84 }
|
piem@69
|
85
|
piem@69
|
86 string
|
piem@69
|
87 Mfcc::getCopyright() const
|
piem@69
|
88 {
|
piem@69
|
89 return "GPL";
|
piem@69
|
90 }
|
piem@69
|
91
|
piem@69
|
92 bool
|
piem@69
|
93 Mfcc::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
piem@69
|
94 {
|
piem@69
|
95 if (channels != 1) {
|
piem@69
|
96 std::cerr << "Mfcc::initialise: channels must be 1" << std::endl;
|
piem@69
|
97 return false;
|
piem@69
|
98 }
|
piem@69
|
99
|
piem@69
|
100 m_stepSize = stepSize;
|
piem@69
|
101 m_blockSize = blockSize;
|
piem@69
|
102
|
piem@69
|
103 m_ibuf = new_fvec(stepSize);
|
piem@69
|
104 m_ispec = new_cvec(blockSize);
|
piem@69
|
105 m_ovec = new_fvec(m_ncoeffs);
|
piem@69
|
106
|
piem@69
|
107 reset();
|
piem@69
|
108
|
piem@69
|
109 return true;
|
piem@69
|
110 }
|
piem@69
|
111
|
piem@69
|
112 void
|
piem@69
|
113 Mfcc::reset()
|
piem@69
|
114 {
|
piem@69
|
115 if (m_pvoc) del_aubio_pvoc(m_pvoc);
|
piem@69
|
116 if (m_mfcc) del_aubio_mfcc(m_mfcc);
|
piem@69
|
117
|
piem@69
|
118 m_pvoc = new_aubio_pvoc(m_blockSize, m_stepSize);
|
piem@69
|
119
|
piem@69
|
120 m_mfcc = new_aubio_mfcc(m_blockSize, m_nfilters, m_ncoeffs,
|
piem@69
|
121 lrintf(m_inputSampleRate));
|
piem@69
|
122
|
piem@69
|
123 }
|
piem@69
|
124
|
piem@69
|
125 size_t
|
piem@69
|
126 Mfcc::getPreferredStepSize() const
|
piem@69
|
127 {
|
piem@69
|
128 return 128;
|
piem@69
|
129 }
|
piem@69
|
130
|
piem@69
|
131 size_t
|
piem@69
|
132 Mfcc::getPreferredBlockSize() const
|
piem@69
|
133 {
|
piem@69
|
134 return 512;
|
piem@69
|
135 }
|
piem@69
|
136
|
piem@69
|
137 Mfcc::ParameterList
|
piem@69
|
138 Mfcc::getParameterDescriptors() const
|
piem@69
|
139 {
|
piem@69
|
140 ParameterList list;
|
piem@69
|
141
|
piem@69
|
142 ParameterDescriptor desc;
|
piem@69
|
143 desc.identifier = "nfilters";
|
piem@69
|
144 desc.name = "Number of filters";
|
piem@96
|
145 desc.description = "Size of mel filterbank used to compute MFCCs (fixed to 40 for now)";
|
piem@96
|
146 desc.minValue = 40;
|
piem@96
|
147 desc.maxValue = 40;
|
piem@69
|
148 desc.defaultValue = 40;
|
piem@69
|
149 desc.isQuantized = true;
|
piem@69
|
150 desc.quantizeStep = 1;
|
piem@69
|
151 list.push_back(desc);
|
piem@69
|
152
|
piem@69
|
153 desc = ParameterDescriptor();
|
piem@69
|
154 desc.identifier = "ncoeffs";
|
piem@69
|
155 desc.name = "Number of coefficients";
|
piem@69
|
156 desc.description = "Number of output coefficients to compute";
|
piem@69
|
157 desc.minValue = 1;
|
piem@69
|
158 desc.maxValue = 100;
|
piem@69
|
159 desc.defaultValue = 13;
|
piem@69
|
160 desc.isQuantized = true;
|
piem@69
|
161 desc.quantizeStep = 1;
|
piem@69
|
162 list.push_back(desc);
|
piem@69
|
163
|
piem@69
|
164 return list;
|
piem@69
|
165 }
|
piem@69
|
166
|
piem@69
|
167 float
|
piem@69
|
168 Mfcc::getParameter(std::string param) const
|
piem@69
|
169 {
|
piem@69
|
170 if (param == "ncoeffs") {
|
piem@69
|
171 return m_ncoeffs;
|
piem@69
|
172 } else if (param == "nfilters") {
|
piem@69
|
173 return m_nfilters;
|
piem@69
|
174 } else {
|
piem@69
|
175 return 0.0;
|
piem@69
|
176 }
|
piem@69
|
177 }
|
piem@69
|
178
|
piem@69
|
179 void
|
piem@69
|
180 Mfcc::setParameter(std::string param, float value)
|
piem@69
|
181 {
|
piem@69
|
182 if (param == "nfilters") {
|
piem@69
|
183 m_nfilters = lrintf(value);
|
piem@69
|
184 } else if (param == "ncoeffs") {
|
piem@69
|
185 m_ncoeffs = lrintf(value);
|
piem@69
|
186 }
|
piem@69
|
187 }
|
piem@69
|
188
|
piem@69
|
189 Mfcc::OutputList
|
piem@69
|
190 Mfcc::getOutputDescriptors() const
|
piem@69
|
191 {
|
piem@69
|
192 OutputList list;
|
piem@69
|
193
|
piem@69
|
194 OutputDescriptor d;
|
piem@69
|
195 d.identifier = "mfcc";
|
piem@79
|
196 d.name = "Mel-Frequency Cepstrum Coefficients";
|
piem@69
|
197 d.description = "List of detected Mel-Frequency Cepstrum Coefficients";
|
piem@69
|
198 d.unit = "";
|
piem@69
|
199 d.hasFixedBinCount = true;
|
piem@69
|
200 d.binCount = m_ncoeffs;
|
piem@69
|
201 d.isQuantized = true;
|
piem@143
|
202 d.quantizeStep = 1.0;
|
piem@143
|
203 d.sampleType = OutputDescriptor::OneSamplePerStep;
|
piem@69
|
204 list.push_back(d);
|
piem@69
|
205
|
piem@69
|
206 return list;
|
piem@69
|
207 }
|
piem@69
|
208
|
piem@69
|
209 Mfcc::FeatureSet
|
piem@69
|
210 Mfcc::process(const float *const *inputBuffers,
|
piem@134
|
211 UNUSED Vamp::RealTime timestamp)
|
piem@69
|
212 {
|
piem@69
|
213 FeatureSet returnFeatures;
|
piem@69
|
214
|
piem@69
|
215 if (m_stepSize == 0) {
|
piem@69
|
216 std::cerr << "Mfcc::process: Mfcc plugin not initialised" << std::endl;
|
piem@69
|
217 return returnFeatures;
|
piem@69
|
218 }
|
piem@69
|
219 if (m_ncoeffs == 0) {
|
piem@69
|
220 std::cerr << "Mfcc::process: Mfcc plugin not initialised" << std::endl;
|
piem@69
|
221 return returnFeatures;
|
piem@69
|
222 }
|
piem@69
|
223
|
piem@69
|
224 for (size_t i = 0; i < m_stepSize; ++i) {
|
piem@69
|
225 fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
|
piem@69
|
226 }
|
piem@69
|
227
|
piem@69
|
228 aubio_pvoc_do(m_pvoc, m_ibuf, m_ispec);
|
piem@69
|
229 aubio_mfcc_do(m_mfcc, m_ispec, m_ovec);
|
piem@69
|
230
|
piem@69
|
231 Feature feature;
|
piem@69
|
232 for (uint_t i = 0; i < m_ovec->length; i++) {
|
piem@69
|
233 float value = m_ovec->data[i];
|
piem@69
|
234 feature.values.push_back(value);
|
piem@69
|
235 }
|
piem@69
|
236
|
piem@69
|
237 returnFeatures[0].push_back(feature);
|
piem@69
|
238 return returnFeatures;
|
piem@69
|
239 }
|
piem@69
|
240
|
piem@69
|
241 Mfcc::FeatureSet
|
piem@69
|
242 Mfcc::getRemainingFeatures()
|
piem@69
|
243 {
|
piem@69
|
244 return FeatureSet();
|
piem@69
|
245 }
|
piem@69
|
246
|