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