Makefile.inc: add getwaf / expandwaf rules
[vamp-aubio-plugins.git] / plugins / SpecDesc.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 "SpecDesc.h"
27
28 using std::string;
29 using std::vector;
30 using std::cerr;
31 using std::endl;
32
33 SpecDesc::SpecDesc(float inputSampleRate) :
34     Plugin(inputSampleRate),
35     m_ibuf(0),
36     m_pvoc(0),
37     m_ispec(0),
38     m_specdesc(0),
39     m_out(0),
40     m_specdesctype(SpecDescFlux)
41 {
42 }
43
44 SpecDesc::~SpecDesc()
45 {
46     if (m_specdesc) del_aubio_specdesc(m_specdesc);
47     if (m_pvoc) del_aubio_pvoc(m_pvoc);
48     if (m_ibuf) del_fvec(m_ibuf);
49     if (m_ispec) del_cvec(m_ispec);
50     if (m_out) del_fvec(m_out);
51 }
52
53 string
54 SpecDesc::getIdentifier() const
55 {
56     return "aubiospecdesc";
57 }
58
59 string
60 SpecDesc::getName() const
61 {
62     return "Aubio Spectral Descriptor";
63 }
64
65 string
66 SpecDesc::getDescription() const
67 {
68     return "Compute spectral descriptor";
69 }
70
71 string
72 SpecDesc::getMaker() const
73 {
74     return "Paul Brossier";
75 }
76
77 int
78 SpecDesc::getPluginVersion() const
79 {
80     return 2;
81 }
82
83 string
84 SpecDesc::getCopyright() const
85 {
86     return "GPL";
87 }
88
89 bool
90 SpecDesc::initialise(size_t channels, size_t stepSize, size_t blockSize)
91 {
92     if (channels != 1) {
93         std::cerr << "SpecDesc::initialise: channels must be 1" << std::endl;
94         return false;
95     }
96
97     m_stepSize = stepSize;
98     m_blockSize = blockSize;
99
100     m_ibuf = new_fvec(stepSize);
101     m_ispec = new_cvec(blockSize);
102     m_out = new_fvec(1);
103
104     reset();
105
106     return true;
107 }
108
109 void
110 SpecDesc::reset()
111 {
112     if (m_pvoc) del_aubio_pvoc(m_pvoc);
113     if (m_specdesc) del_aubio_specdesc(m_specdesc);
114
115     m_specdesc = new_aubio_specdesc
116         (const_cast<char *>(getAubioNameForSpecDescType(m_specdesctype)),
117          m_blockSize);
118
119     m_pvoc = new_aubio_pvoc(m_blockSize, m_stepSize);
120 }
121
122 size_t
123 SpecDesc::getPreferredStepSize() const
124 {
125     return 512;
126 }
127
128 size_t
129 SpecDesc::getPreferredBlockSize() const
130 {
131     return 2 * getPreferredStepSize();
132 }
133
134 SpecDesc::ParameterList
135 SpecDesc::getParameterDescriptors() const
136 {
137     ParameterList list;
138
139     ParameterDescriptor desc;
140     desc.identifier = "specdesctype";
141     desc.name = "Spectral Descriptor Type";
142     desc.description = "Type of spectral descriptor to use";
143     desc.minValue = 0;
144     desc.maxValue = 7;
145     desc.defaultValue = (int)SpecDescFlux;
146     desc.isQuantized = true;
147     desc.quantizeStep = 1;
148     desc.valueNames.push_back("Spectral Flux");
149     desc.valueNames.push_back("Spectral Centroid");
150     desc.valueNames.push_back("Spectral Spread");
151     desc.valueNames.push_back("Spectral Skewness");
152     desc.valueNames.push_back("Spectral Kurtosis");
153     desc.valueNames.push_back("Spectral Slope");
154     desc.valueNames.push_back("Spectral Decrease");
155     desc.valueNames.push_back("Spectral Rolloff");
156     list.push_back(desc);
157
158     return list;
159 }
160
161 float
162 SpecDesc::getParameter(std::string param) const
163 {
164     if (param == "specdesctype") {
165         return m_specdesctype;
166     } else {
167         return 0.0;
168     }
169 }
170
171 void
172 SpecDesc::setParameter(std::string param, float value)
173 {
174     if (param == "specdesctype") {
175         switch (lrintf(value)) {
176         case 0: m_specdesctype = SpecDescFlux; break;
177         case 1: m_specdesctype = SpecDescCentroid; break;
178         case 2: m_specdesctype = SpecDescSpread; break;
179         case 3: m_specdesctype = SpecDescSkeweness; break;
180         case 4: m_specdesctype = SpecDescKurtosis; break;
181         case 5: m_specdesctype = SpecDescSlope; break;
182         case 6: m_specdesctype = SpecDescDecrease; break;
183         case 7: m_specdesctype = SpecDescRolloff; break;
184         }
185     }
186 }
187
188 SpecDesc::OutputList
189 SpecDesc::getOutputDescriptors() const
190 {
191     OutputList list;
192
193     OutputDescriptor d;
194     d.identifier = "specdesc";
195     d.name = "Spectral descriptor output";
196     d.description = "Output of the spectral descpriptor";
197     d.binCount = 1;
198     d.isQuantized = true;
199     d.quantizeStep = 1.0;
200     d.sampleType = OutputDescriptor::OneSamplePerStep;
201     list.push_back(d);
202
203     return list;
204 }
205
206 SpecDesc::FeatureSet
207 SpecDesc::process(const float *const *inputBuffers,
208                UNUSED Vamp::RealTime timestamp)
209 {
210     for (size_t i = 0; i < m_stepSize; ++i) {
211         fvec_set_sample(m_ibuf, inputBuffers[0][i], i);
212     }
213
214     aubio_pvoc_do(m_pvoc, m_ibuf, m_ispec);
215     aubio_specdesc_do(m_specdesc, m_ispec, m_out);
216
217     FeatureSet returnFeatures;
218
219     Feature specdesc;
220     specdesc.hasTimestamp = false;
221     specdesc.values.push_back(m_out->data[0]);
222     returnFeatures[0].push_back(specdesc);
223
224     return returnFeatures;
225 }
226
227 SpecDesc::FeatureSet
228 SpecDesc::getRemainingFeatures()
229 {
230     return FeatureSet();
231 }
232