cannam@1
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
cannam@1
|
2
|
cannam@1
|
3 /*
|
cannam@1
|
4 Vamp
|
cannam@1
|
5
|
cannam@1
|
6 An API for audio analysis and feature extraction plugins.
|
cannam@1
|
7
|
cannam@1
|
8 Centre for Digital Music, Queen Mary, University of London.
|
cannam@1
|
9 Copyright 2006 Chris Cannam.
|
cannam@1
|
10
|
cannam@1
|
11 Permission is hereby granted, free of charge, to any person
|
cannam@1
|
12 obtaining a copy of this software and associated documentation
|
cannam@1
|
13 files (the "Software"), to deal in the Software without
|
cannam@1
|
14 restriction, including without limitation the rights to use, copy,
|
cannam@1
|
15 modify, merge, publish, distribute, sublicense, and/or sell copies
|
cannam@1
|
16 of the Software, and to permit persons to whom the Software is
|
cannam@1
|
17 furnished to do so, subject to the following conditions:
|
cannam@1
|
18
|
cannam@1
|
19 The above copyright notice and this permission notice shall be
|
cannam@1
|
20 included in all copies or substantial portions of the Software.
|
cannam@1
|
21
|
cannam@1
|
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
cannam@1
|
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
cannam@1
|
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
cannam@6
|
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
cannam@1
|
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
cannam@1
|
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
cannam@1
|
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
cannam@1
|
29
|
cannam@1
|
30 Except as contained in this notice, the names of the Centre for
|
cannam@1
|
31 Digital Music; Queen Mary, University of London; and Chris Cannam
|
cannam@1
|
32 shall not be used in advertising or otherwise to promote the sale,
|
cannam@1
|
33 use or other dealings in this Software without prior written
|
cannam@1
|
34 authorization.
|
cannam@1
|
35 */
|
cannam@1
|
36
|
cannam@1
|
37 #include "PluginHostAdapter.h"
|
cannam@1
|
38
|
cannam@1
|
39 namespace Vamp
|
cannam@1
|
40 {
|
cannam@1
|
41
|
cannam@1
|
42 PluginHostAdapter::PluginHostAdapter(const VampPluginDescriptor *descriptor,
|
cannam@1
|
43 float inputSampleRate) :
|
cannam@1
|
44 Plugin(inputSampleRate),
|
cannam@1
|
45 m_descriptor(descriptor)
|
cannam@1
|
46 {
|
cannam@1
|
47 std::cerr << "PluginHostAdapter::PluginHostAdapter (plugin = " << descriptor->name << ")" << std::endl;
|
cannam@1
|
48 m_handle = m_descriptor->instantiate(m_descriptor, inputSampleRate);
|
cannam@1
|
49 }
|
cannam@1
|
50
|
cannam@1
|
51 PluginHostAdapter::~PluginHostAdapter()
|
cannam@1
|
52 {
|
cannam@1
|
53 if (m_handle) m_descriptor->cleanup(m_handle);
|
cannam@1
|
54 }
|
cannam@1
|
55
|
cannam@1
|
56 bool
|
cannam@1
|
57 PluginHostAdapter::initialise(size_t channels,
|
cannam@1
|
58 size_t stepSize,
|
cannam@1
|
59 size_t blockSize)
|
cannam@1
|
60 {
|
cannam@1
|
61 if (!m_handle) return false;
|
cannam@1
|
62 return m_descriptor->initialise(m_handle, channels, stepSize, blockSize) ?
|
cannam@1
|
63 true : false;
|
cannam@1
|
64 }
|
cannam@1
|
65
|
cannam@1
|
66 void
|
cannam@1
|
67 PluginHostAdapter::reset()
|
cannam@1
|
68 {
|
cannam@1
|
69 if (!m_handle) return;
|
cannam@1
|
70 m_descriptor->reset(m_handle);
|
cannam@1
|
71 }
|
cannam@1
|
72
|
cannam@1
|
73 PluginHostAdapter::InputDomain
|
cannam@1
|
74 PluginHostAdapter::getInputDomain() const
|
cannam@1
|
75 {
|
cannam@1
|
76 if (m_descriptor->inputDomain == vampFrequencyDomain) {
|
cannam@1
|
77 return FrequencyDomain;
|
cannam@1
|
78 } else {
|
cannam@1
|
79 return TimeDomain;
|
cannam@1
|
80 }
|
cannam@1
|
81 }
|
cannam@1
|
82
|
cannam@1
|
83 std::string
|
cannam@1
|
84 PluginHostAdapter::getName() const
|
cannam@1
|
85 {
|
cannam@1
|
86 return m_descriptor->name;
|
cannam@1
|
87 }
|
cannam@1
|
88
|
cannam@1
|
89 std::string
|
cannam@1
|
90 PluginHostAdapter::getDescription() const
|
cannam@1
|
91 {
|
cannam@1
|
92 return m_descriptor->description;
|
cannam@1
|
93 }
|
cannam@1
|
94
|
cannam@1
|
95 std::string
|
cannam@1
|
96 PluginHostAdapter::getMaker() const
|
cannam@1
|
97 {
|
cannam@1
|
98 return m_descriptor->maker;
|
cannam@1
|
99 }
|
cannam@1
|
100
|
cannam@1
|
101 int
|
cannam@1
|
102 PluginHostAdapter::getPluginVersion() const
|
cannam@1
|
103 {
|
cannam@1
|
104 return m_descriptor->pluginVersion;
|
cannam@1
|
105 }
|
cannam@1
|
106
|
cannam@1
|
107 std::string
|
cannam@1
|
108 PluginHostAdapter::getCopyright() const
|
cannam@1
|
109 {
|
cannam@1
|
110 return m_descriptor->copyright;
|
cannam@1
|
111 }
|
cannam@1
|
112
|
cannam@1
|
113 PluginHostAdapter::ParameterList
|
cannam@1
|
114 PluginHostAdapter::getParameterDescriptors() const
|
cannam@1
|
115 {
|
cannam@1
|
116 ParameterList list;
|
cannam@1
|
117 for (unsigned int i = 0; i < m_descriptor->parameterCount; ++i) {
|
cannam@1
|
118 const VampParameterDescriptor *spd = m_descriptor->parameters[i];
|
cannam@1
|
119 ParameterDescriptor pd;
|
cannam@1
|
120 pd.name = spd->name;
|
cannam@1
|
121 pd.description = spd->description;
|
cannam@1
|
122 pd.unit = spd->unit;
|
cannam@1
|
123 pd.minValue = spd->minValue;
|
cannam@1
|
124 pd.maxValue = spd->maxValue;
|
cannam@1
|
125 pd.defaultValue = spd->defaultValue;
|
cannam@1
|
126 pd.isQuantized = spd->isQuantized;
|
cannam@1
|
127 pd.quantizeStep = spd->quantizeStep;
|
cannam@1
|
128 list.push_back(pd);
|
cannam@1
|
129 }
|
cannam@1
|
130 return list;
|
cannam@1
|
131 }
|
cannam@1
|
132
|
cannam@1
|
133 float
|
cannam@1
|
134 PluginHostAdapter::getParameter(std::string param) const
|
cannam@1
|
135 {
|
cannam@1
|
136 if (!m_handle) return 0.0;
|
cannam@1
|
137
|
cannam@1
|
138 for (unsigned int i = 0; i < m_descriptor->parameterCount; ++i) {
|
cannam@1
|
139 if (param == m_descriptor->parameters[i]->name) {
|
cannam@1
|
140 return m_descriptor->getParameter(m_handle, i);
|
cannam@1
|
141 }
|
cannam@1
|
142 }
|
cannam@1
|
143
|
cannam@1
|
144 return 0.0;
|
cannam@1
|
145 }
|
cannam@1
|
146
|
cannam@1
|
147 void
|
cannam@1
|
148 PluginHostAdapter::setParameter(std::string param,
|
cannam@1
|
149 float value)
|
cannam@1
|
150 {
|
cannam@1
|
151 if (!m_handle) return;
|
cannam@1
|
152
|
cannam@1
|
153 for (unsigned int i = 0; i < m_descriptor->parameterCount; ++i) {
|
cannam@1
|
154 if (param == m_descriptor->parameters[i]->name) {
|
cannam@1
|
155 m_descriptor->setParameter(m_handle, i, value);
|
cannam@1
|
156 return;
|
cannam@1
|
157 }
|
cannam@1
|
158 }
|
cannam@1
|
159 }
|
cannam@1
|
160
|
cannam@1
|
161 PluginHostAdapter::ProgramList
|
cannam@1
|
162 PluginHostAdapter::getPrograms() const
|
cannam@1
|
163 {
|
cannam@1
|
164 ProgramList list;
|
cannam@1
|
165
|
cannam@1
|
166 for (unsigned int i = 0; i < m_descriptor->programCount; ++i) {
|
cannam@1
|
167 list.push_back(m_descriptor->programs[i]);
|
cannam@1
|
168 }
|
cannam@1
|
169
|
cannam@1
|
170 return list;
|
cannam@1
|
171 }
|
cannam@1
|
172
|
cannam@1
|
173 std::string
|
cannam@1
|
174 PluginHostAdapter::getCurrentProgram() const
|
cannam@1
|
175 {
|
cannam@1
|
176 if (!m_handle) return "";
|
cannam@1
|
177
|
cannam@1
|
178 int pn = m_descriptor->getCurrentProgram(m_handle);
|
cannam@1
|
179 return m_descriptor->programs[pn];
|
cannam@1
|
180 }
|
cannam@1
|
181
|
cannam@1
|
182 void
|
cannam@1
|
183 PluginHostAdapter::selectProgram(std::string program)
|
cannam@1
|
184 {
|
cannam@1
|
185 if (!m_handle) return;
|
cannam@1
|
186
|
cannam@1
|
187 for (unsigned int i = 0; i < m_descriptor->programCount; ++i) {
|
cannam@1
|
188 if (program == m_descriptor->programs[i]) {
|
cannam@1
|
189 m_descriptor->selectProgram(m_handle, i);
|
cannam@1
|
190 return;
|
cannam@1
|
191 }
|
cannam@1
|
192 }
|
cannam@1
|
193 }
|
cannam@1
|
194
|
cannam@1
|
195 size_t
|
cannam@1
|
196 PluginHostAdapter::getPreferredStepSize() const
|
cannam@1
|
197 {
|
cannam@1
|
198 if (!m_handle) return 0;
|
cannam@1
|
199 return m_descriptor->getPreferredStepSize(m_handle);
|
cannam@1
|
200 }
|
cannam@1
|
201
|
cannam@1
|
202 size_t
|
cannam@1
|
203 PluginHostAdapter::getPreferredBlockSize() const
|
cannam@1
|
204 {
|
cannam@1
|
205 if (!m_handle) return 0;
|
cannam@1
|
206 return m_descriptor->getPreferredBlockSize(m_handle);
|
cannam@1
|
207 }
|
cannam@1
|
208
|
cannam@1
|
209 PluginHostAdapter::OutputList
|
cannam@1
|
210 PluginHostAdapter::getOutputDescriptors() const
|
cannam@1
|
211 {
|
cannam@1
|
212 OutputList list;
|
cannam@1
|
213 if (!m_handle) return list;
|
cannam@1
|
214
|
cannam@1
|
215 unsigned int count = m_descriptor->getOutputCount(m_handle);
|
cannam@1
|
216
|
cannam@1
|
217 for (unsigned int i = 0; i < count; ++i) {
|
cannam@1
|
218 VampOutputDescriptor *sd = m_descriptor->getOutputDescriptor(m_handle, i);
|
cannam@1
|
219 OutputDescriptor d;
|
cannam@1
|
220 d.name = sd->name;
|
cannam@1
|
221 d.description = sd->description;
|
cannam@1
|
222 d.unit = sd->unit;
|
cannam@1
|
223 d.hasFixedValueCount = sd->hasFixedValueCount;
|
cannam@1
|
224 d.valueCount = sd->valueCount;
|
cannam@1
|
225 for (unsigned int j = 0; j < sd->valueCount; ++j) {
|
cannam@7
|
226 d.valueNames.push_back(sd->valueNames[j] ? sd->valueNames[j] : "");
|
cannam@1
|
227 }
|
cannam@1
|
228 d.hasKnownExtents = sd->hasKnownExtents;
|
cannam@1
|
229 d.minValue = sd->minValue;
|
cannam@1
|
230 d.maxValue = sd->maxValue;
|
cannam@1
|
231 d.isQuantized = sd->isQuantized;
|
cannam@1
|
232 d.quantizeStep = sd->quantizeStep;
|
cannam@1
|
233
|
cannam@1
|
234 switch (sd->sampleType) {
|
cannam@1
|
235 case vampOneSamplePerStep:
|
cannam@1
|
236 d.sampleType = OutputDescriptor::OneSamplePerStep; break;
|
cannam@1
|
237 case vampFixedSampleRate:
|
cannam@1
|
238 d.sampleType = OutputDescriptor::FixedSampleRate; break;
|
cannam@1
|
239 case vampVariableSampleRate:
|
cannam@1
|
240 d.sampleType = OutputDescriptor::VariableSampleRate; break;
|
cannam@1
|
241 }
|
cannam@1
|
242
|
cannam@1
|
243 d.sampleRate = sd->sampleRate;
|
cannam@1
|
244
|
cannam@1
|
245 list.push_back(d);
|
cannam@1
|
246
|
cannam@1
|
247 m_descriptor->releaseOutputDescriptor(sd);
|
cannam@1
|
248 }
|
cannam@1
|
249
|
cannam@1
|
250 return list;
|
cannam@1
|
251 }
|
cannam@1
|
252
|
cannam@1
|
253 PluginHostAdapter::FeatureSet
|
cannam@1
|
254 PluginHostAdapter::process(float **inputBuffers,
|
cannam@1
|
255 RealTime timestamp)
|
cannam@1
|
256 {
|
cannam@1
|
257 FeatureSet fs;
|
cannam@1
|
258 if (!m_handle) return fs;
|
cannam@1
|
259
|
cannam@1
|
260 int sec = timestamp.sec;
|
cannam@1
|
261 int nsec = timestamp.nsec;
|
cannam@1
|
262
|
cannam@1
|
263 VampFeatureList **features = m_descriptor->process(m_handle,
|
cannam@1
|
264 inputBuffers,
|
cannam@1
|
265 sec, nsec);
|
cannam@1
|
266
|
cannam@1
|
267 convertFeatures(features, fs);
|
cannam@1
|
268 m_descriptor->releaseFeatureSet(features);
|
cannam@1
|
269 return fs;
|
cannam@1
|
270 }
|
cannam@1
|
271
|
cannam@1
|
272 PluginHostAdapter::FeatureSet
|
cannam@1
|
273 PluginHostAdapter::getRemainingFeatures()
|
cannam@1
|
274 {
|
cannam@1
|
275 FeatureSet fs;
|
cannam@1
|
276 if (!m_handle) return fs;
|
cannam@1
|
277
|
cannam@1
|
278 VampFeatureList **features = m_descriptor->getRemainingFeatures(m_handle);
|
cannam@1
|
279
|
cannam@1
|
280 convertFeatures(features, fs);
|
cannam@1
|
281 m_descriptor->releaseFeatureSet(features);
|
cannam@1
|
282 return fs;
|
cannam@1
|
283 }
|
cannam@1
|
284
|
cannam@1
|
285 void
|
cannam@1
|
286 PluginHostAdapter::convertFeatures(VampFeatureList **features,
|
cannam@1
|
287 FeatureSet &fs)
|
cannam@1
|
288 {
|
cannam@7
|
289 if (!features) return;
|
cannam@7
|
290
|
cannam@1
|
291 for (unsigned int i = 0; features[i]; ++i) {
|
cannam@1
|
292
|
cannam@1
|
293 VampFeatureList &list = *features[i];
|
cannam@1
|
294
|
cannam@1
|
295 if (list.featureCount > 0) {
|
cannam@1
|
296
|
cannam@1
|
297 for (unsigned int j = 0; j < list.featureCount; ++j) {
|
cannam@1
|
298
|
cannam@1
|
299 Feature feature;
|
cannam@1
|
300 feature.hasTimestamp = list.features[j].hasTimestamp;
|
cannam@1
|
301 feature.timestamp = RealTime(list.features[j].sec,
|
cannam@1
|
302 list.features[j].nsec);
|
cannam@1
|
303
|
cannam@1
|
304 for (unsigned int k = 0; k < list.features[j].valueCount; ++k) {
|
cannam@1
|
305 feature.values.push_back(list.features[j].values[k]);
|
cannam@1
|
306 }
|
cannam@7
|
307
|
cannam@7
|
308 if (list.features[j].label) {
|
cannam@7
|
309 feature.label = list.features[j].label;
|
cannam@7
|
310 }
|
cannam@1
|
311
|
cannam@1
|
312 fs[i].push_back(feature);
|
cannam@1
|
313 }
|
cannam@1
|
314 }
|
cannam@1
|
315 }
|
cannam@1
|
316 }
|
cannam@1
|
317
|
cannam@1
|
318 }
|