comparison vamp-sdk/PluginHostAdapter.cpp @ 3:0133b3513e2b

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