annotate vamp-sdk/PluginAdapter.cpp @ 17:a67eeb9d6341

* better docs for sample rate
author cannam
date Fri, 07 Apr 2006 17:50:52 +0000
parents 61887dda7fe0
children 1eb44d33a371
rev   line source
cannam@3 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@3 2
cannam@3 3 /*
cannam@3 4 Vamp
cannam@3 5
cannam@3 6 An API for audio analysis and feature extraction plugins.
cannam@3 7
cannam@3 8 Centre for Digital Music, Queen Mary, University of London.
cannam@3 9 Copyright 2006 Chris Cannam.
cannam@3 10
cannam@3 11 Permission is hereby granted, free of charge, to any person
cannam@3 12 obtaining a copy of this software and associated documentation
cannam@3 13 files (the "Software"), to deal in the Software without
cannam@3 14 restriction, including without limitation the rights to use, copy,
cannam@3 15 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@3 16 of the Software, and to permit persons to whom the Software is
cannam@3 17 furnished to do so, subject to the following conditions:
cannam@3 18
cannam@3 19 The above copyright notice and this permission notice shall be
cannam@3 20 included in all copies or substantial portions of the Software.
cannam@3 21
cannam@3 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@3 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@3 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@6 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@3 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@3 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@3 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@3 29
cannam@3 30 Except as contained in this notice, the names of the Centre for
cannam@3 31 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@3 32 shall not be used in advertising or otherwise to promote the sale,
cannam@3 33 use or other dealings in this Software without prior written
cannam@3 34 authorization.
cannam@3 35 */
cannam@3 36
cannam@3 37 #include "PluginAdapter.h"
cannam@3 38
cannam@3 39 namespace Vamp {
cannam@3 40
cannam@3 41 PluginAdapterBase::PluginAdapterBase() :
cannam@3 42 m_populated(false)
cannam@3 43 {
cannam@3 44 }
cannam@3 45
cannam@3 46 const VampPluginDescriptor *
cannam@3 47 PluginAdapterBase::getDescriptor()
cannam@3 48 {
cannam@3 49 if (m_populated) return &m_descriptor;
cannam@3 50
cannam@3 51 Plugin *plugin = createPlugin(48000);
cannam@3 52
cannam@3 53 m_parameters = plugin->getParameterDescriptors();
cannam@3 54 m_programs = plugin->getPrograms();
cannam@3 55
cannam@3 56 m_descriptor.name = strdup(plugin->getName().c_str());
cannam@3 57 m_descriptor.description = strdup(plugin->getDescription().c_str());
cannam@3 58 m_descriptor.maker = strdup(plugin->getMaker().c_str());
cannam@3 59 m_descriptor.pluginVersion = plugin->getPluginVersion();
cannam@3 60 m_descriptor.copyright = strdup(plugin->getCopyright().c_str());
cannam@3 61
cannam@3 62 m_descriptor.parameterCount = m_parameters.size();
cannam@3 63 m_descriptor.parameters = (const VampParameterDescriptor **)
cannam@3 64 malloc(m_parameters.size() * sizeof(VampParameterDescriptor));
cannam@7 65
cannam@7 66 unsigned int i;
cannam@3 67
cannam@7 68 for (i = 0; i < m_parameters.size(); ++i) {
cannam@3 69 VampParameterDescriptor *desc = (VampParameterDescriptor *)
cannam@3 70 malloc(sizeof(VampParameterDescriptor));
cannam@3 71 desc->name = strdup(m_parameters[i].name.c_str());
cannam@3 72 desc->description = strdup(m_parameters[i].description.c_str());
cannam@3 73 desc->unit = strdup(m_parameters[i].unit.c_str());
cannam@3 74 desc->minValue = m_parameters[i].minValue;
cannam@3 75 desc->maxValue = m_parameters[i].maxValue;
cannam@3 76 desc->defaultValue = m_parameters[i].defaultValue;
cannam@3 77 desc->isQuantized = m_parameters[i].isQuantized;
cannam@3 78 desc->quantizeStep = m_parameters[i].quantizeStep;
cannam@9 79 desc->valueNames = 0;
cannam@9 80 if (desc->isQuantized && !m_parameters[i].valueNames.empty()) {
cannam@9 81 desc->valueNames = (const char **)
cannam@9 82 malloc((m_parameters[i].valueNames.size()+1) * sizeof(char *));
cannam@9 83 for (unsigned int j = 0; j < m_parameters[i].valueNames.size(); ++j) {
cannam@9 84 desc->valueNames[j] = strdup(m_parameters[i].valueNames[j].c_str());
cannam@9 85 }
cannam@9 86 desc->valueNames[m_parameters[i].valueNames.size()] = 0;
cannam@9 87 }
cannam@3 88 m_descriptor.parameters[i] = desc;
cannam@3 89 }
cannam@3 90
cannam@3 91 m_descriptor.programCount = m_programs.size();
cannam@3 92 m_descriptor.programs = (const char **)
cannam@3 93 malloc(m_programs.size() * sizeof(const char *));
cannam@3 94
cannam@7 95 for (i = 0; i < m_programs.size(); ++i) {
cannam@3 96 m_descriptor.programs[i] = strdup(m_programs[i].c_str());
cannam@3 97 }
cannam@3 98
cannam@3 99 if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
cannam@3 100 m_descriptor.inputDomain = vampFrequencyDomain;
cannam@3 101 } else {
cannam@3 102 m_descriptor.inputDomain = vampTimeDomain;
cannam@3 103 }
cannam@3 104
cannam@3 105 m_descriptor.instantiate = vampInstantiate;
cannam@3 106 m_descriptor.cleanup = vampCleanup;
cannam@3 107 m_descriptor.initialise = vampInitialise;
cannam@3 108 m_descriptor.reset = vampReset;
cannam@3 109 m_descriptor.getParameter = vampGetParameter;
cannam@3 110 m_descriptor.setParameter = vampSetParameter;
cannam@3 111 m_descriptor.getCurrentProgram = vampGetCurrentProgram;
cannam@3 112 m_descriptor.selectProgram = vampSelectProgram;
cannam@3 113 m_descriptor.getPreferredStepSize = vampGetPreferredStepSize;
cannam@3 114 m_descriptor.getPreferredBlockSize = vampGetPreferredBlockSize;
cannam@3 115 m_descriptor.getMinChannelCount = vampGetMinChannelCount;
cannam@3 116 m_descriptor.getMaxChannelCount = vampGetMaxChannelCount;
cannam@3 117 m_descriptor.getOutputCount = vampGetOutputCount;
cannam@3 118 m_descriptor.getOutputDescriptor = vampGetOutputDescriptor;
cannam@3 119 m_descriptor.releaseOutputDescriptor = vampReleaseOutputDescriptor;
cannam@3 120 m_descriptor.process = vampProcess;
cannam@3 121 m_descriptor.getRemainingFeatures = vampGetRemainingFeatures;
cannam@3 122 m_descriptor.releaseFeatureSet = vampReleaseFeatureSet;
cannam@3 123
cannam@13 124 if (!m_adapterMap) {
cannam@13 125 m_adapterMap = new AdapterMap;
cannam@13 126 }
cannam@15 127 (*m_adapterMap)[&m_descriptor] = this;
cannam@3 128
cannam@3 129 delete plugin;
cannam@3 130
cannam@3 131 m_populated = true;
cannam@3 132 return &m_descriptor;
cannam@3 133 }
cannam@3 134
cannam@3 135 PluginAdapterBase::~PluginAdapterBase()
cannam@3 136 {
cannam@3 137 if (!m_populated) return;
cannam@3 138
cannam@3 139 free((void *)m_descriptor.name);
cannam@3 140 free((void *)m_descriptor.description);
cannam@3 141 free((void *)m_descriptor.maker);
cannam@3 142 free((void *)m_descriptor.copyright);
cannam@3 143
cannam@3 144 for (unsigned int i = 0; i < m_descriptor.parameterCount; ++i) {
cannam@3 145 const VampParameterDescriptor *desc = m_descriptor.parameters[i];
cannam@3 146 free((void *)desc->name);
cannam@3 147 free((void *)desc->description);
cannam@3 148 free((void *)desc->unit);
cannam@9 149 if (desc->valueNames) {
cannam@9 150 for (unsigned int j = 0; desc->valueNames[j]; ++j) {
cannam@9 151 free((void *)desc->valueNames[j]);
cannam@9 152 }
cannam@9 153 free((void *)desc->valueNames);
cannam@9 154 }
cannam@3 155 }
cannam@3 156 free((void *)m_descriptor.parameters);
cannam@3 157
cannam@3 158 for (unsigned int i = 0; i < m_descriptor.programCount; ++i) {
cannam@3 159 free((void *)m_descriptor.programs[i]);
cannam@3 160 }
cannam@3 161 free((void *)m_descriptor.programs);
cannam@3 162
cannam@13 163 if (m_adapterMap) {
cannam@13 164
cannam@13 165 m_adapterMap->erase(&m_descriptor);
cannam@13 166
cannam@13 167 if (m_adapterMap->empty()) {
cannam@13 168 delete m_adapterMap;
cannam@13 169 m_adapterMap = 0;
cannam@13 170 }
cannam@13 171 }
cannam@3 172 }
cannam@3 173
cannam@3 174 PluginAdapterBase *
cannam@3 175 PluginAdapterBase::lookupAdapter(VampPluginHandle handle)
cannam@3 176 {
cannam@13 177 if (!m_adapterMap) return 0;
cannam@13 178 AdapterMap::const_iterator i = m_adapterMap->find(handle);
cannam@13 179 if (i == m_adapterMap->end()) return 0;
cannam@3 180 return i->second;
cannam@3 181 }
cannam@3 182
cannam@3 183 VampPluginHandle
cannam@3 184 PluginAdapterBase::vampInstantiate(const VampPluginDescriptor *desc,
cannam@3 185 float inputSampleRate)
cannam@3 186 {
cannam@15 187 if (!m_adapterMap) {
cannam@15 188 m_adapterMap = new AdapterMap();
cannam@15 189 }
cannam@15 190
cannam@15 191 if (m_adapterMap->find(desc) == m_adapterMap->end()) {
cannam@15 192 std::cerr << "WARNING: PluginAdapterBase::vampInstantiate: Descriptor " << desc << " not in adapter map" << std::endl;
cannam@15 193 return 0;
cannam@15 194 }
cannam@15 195
cannam@13 196 PluginAdapterBase *adapter = (*m_adapterMap)[desc];
cannam@3 197 if (desc != &adapter->m_descriptor) return 0;
cannam@3 198
cannam@3 199 Plugin *plugin = adapter->createPlugin(inputSampleRate);
cannam@3 200 if (plugin) {
cannam@13 201 (*m_adapterMap)[plugin] = adapter;
cannam@3 202 }
cannam@3 203
cannam@3 204 return plugin;
cannam@3 205 }
cannam@3 206
cannam@3 207 void
cannam@3 208 PluginAdapterBase::vampCleanup(VampPluginHandle handle)
cannam@3 209 {
cannam@3 210 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 211 if (!adapter) {
cannam@3 212 delete ((Plugin *)handle);
cannam@3 213 return;
cannam@3 214 }
cannam@3 215 adapter->cleanup(((Plugin *)handle));
cannam@3 216 }
cannam@3 217
cannam@3 218 int
cannam@3 219 PluginAdapterBase::vampInitialise(VampPluginHandle handle,
cannam@3 220 unsigned int channels,
cannam@3 221 unsigned int stepSize,
cannam@3 222 unsigned int blockSize)
cannam@3 223 {
cannam@3 224 bool result = ((Plugin *)handle)->initialise
cannam@3 225 (channels, stepSize, blockSize);
cannam@3 226 return result ? 1 : 0;
cannam@3 227 }
cannam@3 228
cannam@3 229 void
cannam@3 230 PluginAdapterBase::vampReset(VampPluginHandle handle)
cannam@3 231 {
cannam@3 232 ((Plugin *)handle)->reset();
cannam@3 233 }
cannam@3 234
cannam@3 235 float
cannam@3 236 PluginAdapterBase::vampGetParameter(VampPluginHandle handle,
cannam@3 237 int param)
cannam@3 238 {
cannam@3 239 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 240 if (!adapter) return 0.0;
cannam@3 241 Plugin::ParameterList &list = adapter->m_parameters;
cannam@3 242 return ((Plugin *)handle)->getParameter(list[param].name);
cannam@3 243 }
cannam@3 244
cannam@3 245 void
cannam@3 246 PluginAdapterBase::vampSetParameter(VampPluginHandle handle,
cannam@3 247 int param, float value)
cannam@3 248 {
cannam@3 249 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 250 if (!adapter) return;
cannam@3 251 Plugin::ParameterList &list = adapter->m_parameters;
cannam@3 252 ((Plugin *)handle)->setParameter(list[param].name, value);
cannam@3 253 }
cannam@3 254
cannam@3 255 unsigned int
cannam@3 256 PluginAdapterBase::vampGetCurrentProgram(VampPluginHandle handle)
cannam@3 257 {
cannam@3 258 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 259 if (!adapter) return 0;
cannam@3 260 Plugin::ProgramList &list = adapter->m_programs;
cannam@3 261 std::string program = ((Plugin *)handle)->getCurrentProgram();
cannam@3 262 for (unsigned int i = 0; i < list.size(); ++i) {
cannam@3 263 if (list[i] == program) return i;
cannam@3 264 }
cannam@3 265 return 0;
cannam@3 266 }
cannam@3 267
cannam@3 268 void
cannam@3 269 PluginAdapterBase::vampSelectProgram(VampPluginHandle handle,
cannam@3 270 unsigned int program)
cannam@3 271 {
cannam@3 272 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 273 if (!adapter) return;
cannam@3 274 Plugin::ProgramList &list = adapter->m_programs;
cannam@3 275 ((Plugin *)handle)->selectProgram(list[program]);
cannam@3 276 }
cannam@3 277
cannam@3 278 unsigned int
cannam@3 279 PluginAdapterBase::vampGetPreferredStepSize(VampPluginHandle handle)
cannam@3 280 {
cannam@3 281 return ((Plugin *)handle)->getPreferredStepSize();
cannam@3 282 }
cannam@3 283
cannam@3 284 unsigned int
cannam@3 285 PluginAdapterBase::vampGetPreferredBlockSize(VampPluginHandle handle)
cannam@3 286 {
cannam@3 287 return ((Plugin *)handle)->getPreferredBlockSize();
cannam@3 288 }
cannam@3 289
cannam@3 290 unsigned int
cannam@3 291 PluginAdapterBase::vampGetMinChannelCount(VampPluginHandle handle)
cannam@3 292 {
cannam@3 293 return ((Plugin *)handle)->getMinChannelCount();
cannam@3 294 }
cannam@3 295
cannam@3 296 unsigned int
cannam@3 297 PluginAdapterBase::vampGetMaxChannelCount(VampPluginHandle handle)
cannam@3 298 {
cannam@3 299 return ((Plugin *)handle)->getMaxChannelCount();
cannam@3 300 }
cannam@3 301
cannam@3 302 unsigned int
cannam@3 303 PluginAdapterBase::vampGetOutputCount(VampPluginHandle handle)
cannam@3 304 {
cannam@3 305 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@15 306
cannam@15 307 // std::cerr << "vampGetOutputCount: handle " << handle << " -> adapter "<< adapter << std::endl;
cannam@15 308
cannam@3 309 if (!adapter) return 0;
cannam@3 310 return adapter->getOutputCount((Plugin *)handle);
cannam@3 311 }
cannam@3 312
cannam@3 313 VampOutputDescriptor *
cannam@3 314 PluginAdapterBase::vampGetOutputDescriptor(VampPluginHandle handle,
cannam@3 315 unsigned int i)
cannam@3 316 {
cannam@3 317 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@15 318
cannam@15 319 // std::cerr << "vampGetOutputDescriptor: handle " << handle << " -> adapter "<< adapter << std::endl;
cannam@15 320
cannam@3 321 if (!adapter) return 0;
cannam@3 322 return adapter->getOutputDescriptor((Plugin *)handle, i);
cannam@3 323 }
cannam@3 324
cannam@3 325 void
cannam@3 326 PluginAdapterBase::vampReleaseOutputDescriptor(VampOutputDescriptor *desc)
cannam@3 327 {
cannam@3 328 if (desc->name) free((void *)desc->name);
cannam@3 329 if (desc->description) free((void *)desc->description);
cannam@3 330 if (desc->unit) free((void *)desc->unit);
cannam@9 331 for (unsigned int i = 0; i < desc->binCount; ++i) {
cannam@9 332 free((void *)desc->binNames[i]);
cannam@3 333 }
cannam@9 334 if (desc->binNames) free((void *)desc->binNames);
cannam@3 335 free((void *)desc);
cannam@3 336 }
cannam@3 337
cannam@12 338 VampFeatureList *
cannam@3 339 PluginAdapterBase::vampProcess(VampPluginHandle handle,
cannam@3 340 float **inputBuffers,
cannam@3 341 int sec,
cannam@3 342 int nsec)
cannam@3 343 {
cannam@3 344 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 345 if (!adapter) return 0;
cannam@3 346 return adapter->process((Plugin *)handle,
cannam@3 347 inputBuffers, sec, nsec);
cannam@3 348 }
cannam@3 349
cannam@12 350 VampFeatureList *
cannam@3 351 PluginAdapterBase::vampGetRemainingFeatures(VampPluginHandle handle)
cannam@3 352 {
cannam@3 353 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 354 if (!adapter) return 0;
cannam@3 355 return adapter->getRemainingFeatures((Plugin *)handle);
cannam@3 356 }
cannam@3 357
cannam@3 358 void
cannam@12 359 PluginAdapterBase::vampReleaseFeatureSet(VampFeatureList *fs)
cannam@3 360 {
cannam@3 361 }
cannam@3 362
cannam@3 363 void
cannam@3 364 PluginAdapterBase::cleanup(Plugin *plugin)
cannam@3 365 {
cannam@12 366 if (m_fs.find(plugin) != m_fs.end()) {
cannam@12 367 size_t outputCount = 0;
cannam@12 368 if (m_pluginOutputs[plugin]) {
cannam@12 369 outputCount = m_pluginOutputs[plugin]->size();
cannam@12 370 }
cannam@12 371 VampFeatureList *list = m_fs[plugin];
cannam@12 372 for (unsigned int i = 0; i < outputCount; ++i) {
cannam@12 373 for (unsigned int j = 0; j < m_fsizes[plugin][i]; ++j) {
cannam@12 374 if (list[i].features[j].label) {
cannam@12 375 free(list[i].features[j].label);
cannam@12 376 }
cannam@12 377 if (list[i].features[j].values) {
cannam@12 378 free(list[i].features[j].values);
cannam@12 379 }
cannam@12 380 }
cannam@12 381 if (list[i].features) free(list[i].features);
cannam@12 382 }
cannam@12 383 m_fs.erase(plugin);
cannam@12 384 m_fsizes.erase(plugin);
cannam@12 385 m_fvsizes.erase(plugin);
cannam@12 386 }
cannam@12 387
cannam@3 388 if (m_pluginOutputs.find(plugin) != m_pluginOutputs.end()) {
cannam@3 389 delete m_pluginOutputs[plugin];
cannam@3 390 m_pluginOutputs.erase(plugin);
cannam@3 391 }
cannam@13 392
cannam@13 393 if (m_adapterMap) {
cannam@13 394 m_adapterMap->erase(plugin);
cannam@13 395
cannam@13 396 if (m_adapterMap->empty()) {
cannam@13 397 delete m_adapterMap;
cannam@13 398 m_adapterMap = 0;
cannam@13 399 }
cannam@13 400 }
cannam@13 401
cannam@3 402 delete ((Plugin *)plugin);
cannam@3 403 }
cannam@3 404
cannam@3 405 void
cannam@3 406 PluginAdapterBase::checkOutputMap(Plugin *plugin)
cannam@3 407 {
cannam@15 408 if (m_pluginOutputs.find(plugin) == m_pluginOutputs.end() ||
cannam@15 409 !m_pluginOutputs[plugin]) {
cannam@3 410 m_pluginOutputs[plugin] = new Plugin::OutputList
cannam@3 411 (plugin->getOutputDescriptors());
cannam@15 412 // std::cerr << "PluginAdapterBase::checkOutputMap: Have " << m_pluginOutputs[plugin]->size() << " outputs for plugin label " << plugin->getName() << std::endl;
cannam@3 413 }
cannam@3 414 }
cannam@3 415
cannam@3 416 unsigned int
cannam@3 417 PluginAdapterBase::getOutputCount(Plugin *plugin)
cannam@3 418 {
cannam@3 419 checkOutputMap(plugin);
cannam@3 420 return m_pluginOutputs[plugin]->size();
cannam@3 421 }
cannam@3 422
cannam@3 423 VampOutputDescriptor *
cannam@3 424 PluginAdapterBase::getOutputDescriptor(Plugin *plugin,
cannam@3 425 unsigned int i)
cannam@3 426 {
cannam@3 427 checkOutputMap(plugin);
cannam@3 428 Plugin::OutputDescriptor &od =
cannam@3 429 (*m_pluginOutputs[plugin])[i];
cannam@3 430
cannam@3 431 VampOutputDescriptor *desc = (VampOutputDescriptor *)
cannam@3 432 malloc(sizeof(VampOutputDescriptor));
cannam@3 433
cannam@3 434 desc->name = strdup(od.name.c_str());
cannam@3 435 desc->description = strdup(od.description.c_str());
cannam@3 436 desc->unit = strdup(od.unit.c_str());
cannam@9 437 desc->hasFixedBinCount = od.hasFixedBinCount;
cannam@9 438 desc->binCount = od.binCount;
cannam@3 439
cannam@9 440 if (od.hasFixedBinCount && od.binCount > 0) {
cannam@9 441 desc->binNames = (const char **)
cannam@9 442 malloc(od.binCount * sizeof(const char *));
cannam@3 443
cannam@9 444 for (unsigned int i = 0; i < od.binCount; ++i) {
cannam@9 445 if (i < od.binNames.size()) {
cannam@9 446 desc->binNames[i] = strdup(od.binNames[i].c_str());
cannam@7 447 } else {
cannam@9 448 desc->binNames[i] = 0;
cannam@7 449 }
cannam@3 450 }
cannam@7 451 } else {
cannam@9 452 desc->binNames = 0;
cannam@3 453 }
cannam@3 454
cannam@3 455 desc->hasKnownExtents = od.hasKnownExtents;
cannam@3 456 desc->minValue = od.minValue;
cannam@3 457 desc->maxValue = od.maxValue;
cannam@3 458 desc->isQuantized = od.isQuantized;
cannam@3 459 desc->quantizeStep = od.quantizeStep;
cannam@3 460
cannam@3 461 switch (od.sampleType) {
cannam@3 462 case Plugin::OutputDescriptor::OneSamplePerStep:
cannam@3 463 desc->sampleType = vampOneSamplePerStep; break;
cannam@3 464 case Plugin::OutputDescriptor::FixedSampleRate:
cannam@3 465 desc->sampleType = vampFixedSampleRate; break;
cannam@3 466 case Plugin::OutputDescriptor::VariableSampleRate:
cannam@3 467 desc->sampleType = vampVariableSampleRate; break;
cannam@3 468 }
cannam@3 469
cannam@3 470 desc->sampleRate = od.sampleRate;
cannam@3 471
cannam@3 472 return desc;
cannam@3 473 }
cannam@3 474
cannam@12 475 VampFeatureList *
cannam@3 476 PluginAdapterBase::process(Plugin *plugin,
cannam@3 477 float **inputBuffers,
cannam@3 478 int sec, int nsec)
cannam@3 479 {
cannam@12 480 // std::cerr << "PluginAdapterBase::process" << std::endl;
cannam@3 481 RealTime rt(sec, nsec);
cannam@12 482 checkOutputMap(plugin);
cannam@12 483 return convertFeatures(plugin, plugin->process(inputBuffers, rt));
cannam@3 484 }
cannam@3 485
cannam@12 486 VampFeatureList *
cannam@3 487 PluginAdapterBase::getRemainingFeatures(Plugin *plugin)
cannam@3 488 {
cannam@12 489 // std::cerr << "PluginAdapterBase::getRemainingFeatures" << std::endl;
cannam@12 490 checkOutputMap(plugin);
cannam@12 491 return convertFeatures(plugin, plugin->getRemainingFeatures());
cannam@3 492 }
cannam@3 493
cannam@12 494 VampFeatureList *
cannam@12 495 PluginAdapterBase::convertFeatures(Plugin *plugin,
cannam@12 496 const Plugin::FeatureSet &features)
cannam@3 497 {
cannam@12 498 int lastN = -1;
cannam@3 499
cannam@12 500 int outputCount = 0;
cannam@12 501 if (m_pluginOutputs[plugin]) outputCount = m_pluginOutputs[plugin]->size();
cannam@12 502
cannam@12 503 resizeFS(plugin, outputCount);
cannam@12 504 VampFeatureList *fs = m_fs[plugin];
cannam@3 505
cannam@12 506 for (Plugin::FeatureSet::const_iterator fi = features.begin();
cannam@12 507 fi != features.end(); ++fi) {
cannam@3 508
cannam@12 509 int n = fi->first;
cannam@12 510
cannam@12 511 // std::cerr << "PluginAdapterBase::convertFeatures: n = " << n << std::endl;
cannam@7 512
cannam@12 513 if (n >= int(outputCount)) {
cannam@12 514 std::cerr << "WARNING: PluginAdapterBase::convertFeatures: Too many outputs from plugin (" << n+1 << ", only should be " << outputCount << ")" << std::endl;
cannam@7 515 continue;
cannam@7 516 }
cannam@7 517
cannam@12 518 if (n > lastN + 1) {
cannam@12 519 for (int i = lastN + 1; i < n; ++i) {
cannam@12 520 fs[i].featureCount = 0;
cannam@12 521 }
cannam@12 522 }
cannam@7 523
cannam@7 524 const Plugin::FeatureList &fl = fi->second;
cannam@7 525
cannam@12 526 size_t sz = fl.size();
cannam@12 527 if (sz > m_fsizes[plugin][n]) resizeFL(plugin, n, sz);
cannam@12 528 fs[n].featureCount = sz;
cannam@12 529
cannam@12 530 for (size_t j = 0; j < sz; ++j) {
cannam@7 531
cannam@12 532 // std::cerr << "PluginAdapterBase::convertFeatures: j = " << j << std::endl;
cannam@7 533
cannam@12 534 VampFeature *feature = &fs[n].features[j];
cannam@7 535
cannam@7 536 feature->hasTimestamp = fl[j].hasTimestamp;
cannam@7 537 feature->sec = fl[j].timestamp.sec;
cannam@7 538 feature->nsec = fl[j].timestamp.nsec;
cannam@7 539 feature->valueCount = fl[j].values.size();
cannam@7 540
cannam@12 541 if (feature->label) free(feature->label);
cannam@12 542
cannam@12 543 if (fl[j].label.empty()) {
cannam@12 544 feature->label = 0;
cannam@12 545 } else {
cannam@12 546 feature->label = strdup(fl[j].label.c_str());
cannam@7 547 }
cannam@7 548
cannam@12 549 if (feature->valueCount > m_fvsizes[plugin][n][j]) {
cannam@12 550 resizeFV(plugin, n, j, feature->valueCount);
cannam@12 551 }
cannam@7 552
cannam@7 553 for (unsigned int k = 0; k < feature->valueCount; ++k) {
cannam@12 554 // std::cerr << "PluginAdapterBase::convertFeatures: k = " << k << std::endl;
cannam@7 555 feature->values[k] = fl[j].values[k];
cannam@3 556 }
cannam@3 557 }
cannam@12 558
cannam@12 559 lastN = n;
cannam@3 560 }
cannam@3 561
cannam@12 562 if (lastN == -1) return 0;
cannam@12 563
cannam@12 564 if (int(outputCount) > lastN + 1) {
cannam@12 565 for (int i = lastN + 1; i < int(outputCount); ++i) {
cannam@12 566 fs[i].featureCount = 0;
cannam@12 567 }
cannam@12 568 }
cannam@3 569
cannam@3 570 return fs;
cannam@3 571 }
cannam@3 572
cannam@12 573 void
cannam@12 574 PluginAdapterBase::resizeFS(Plugin *plugin, int n)
cannam@12 575 {
cannam@12 576 // std::cerr << "PluginAdapterBase::resizeFS(" << plugin << ", " << n << ")" << std::endl;
cannam@12 577
cannam@12 578 int i = m_fsizes[plugin].size();
cannam@12 579 if (i >= n) return;
cannam@12 580
cannam@16 581 // std::cerr << "resizing from " << i << std::endl;
cannam@12 582
cannam@12 583 m_fs[plugin] = (VampFeatureList *)realloc
cannam@12 584 (m_fs[plugin], n * sizeof(VampFeatureList));
cannam@12 585
cannam@12 586 while (i < n) {
cannam@12 587 m_fs[plugin][i].featureCount = 0;
cannam@12 588 m_fs[plugin][i].features = 0;
cannam@12 589 m_fsizes[plugin].push_back(0);
cannam@12 590 m_fvsizes[plugin].push_back(std::vector<size_t>());
cannam@12 591 i++;
cannam@12 592 }
cannam@12 593 }
cannam@12 594
cannam@12 595 void
cannam@12 596 PluginAdapterBase::resizeFL(Plugin *plugin, int n, size_t sz)
cannam@12 597 {
cannam@16 598 // std::cerr << "PluginAdapterBase::resizeFL(" << plugin << ", " << n << ", "
cannam@16 599 // << sz << ")" << std::endl;
cannam@12 600
cannam@12 601 size_t i = m_fsizes[plugin][n];
cannam@12 602 if (i >= sz) return;
cannam@12 603
cannam@16 604 // std::cerr << "resizing from " << i << std::endl;
cannam@12 605
cannam@12 606 m_fs[plugin][n].features = (VampFeature *)realloc
cannam@12 607 (m_fs[plugin][n].features, sz * sizeof(VampFeature));
cannam@12 608
cannam@12 609 while (m_fsizes[plugin][n] < sz) {
cannam@12 610 m_fs[plugin][n].features[m_fsizes[plugin][n]].valueCount = 0;
cannam@12 611 m_fs[plugin][n].features[m_fsizes[plugin][n]].values = 0;
cannam@12 612 m_fs[plugin][n].features[m_fsizes[plugin][n]].label = 0;
cannam@12 613 m_fvsizes[plugin][n].push_back(0);
cannam@12 614 m_fsizes[plugin][n]++;
cannam@12 615 }
cannam@12 616 }
cannam@12 617
cannam@12 618 void
cannam@12 619 PluginAdapterBase::resizeFV(Plugin *plugin, int n, int j, size_t sz)
cannam@12 620 {
cannam@16 621 // std::cerr << "PluginAdapterBase::resizeFV(" << plugin << ", " << n << ", "
cannam@16 622 // << j << ", " << sz << ")" << std::endl;
cannam@12 623
cannam@12 624 size_t i = m_fvsizes[plugin][n][j];
cannam@12 625 if (i >= sz) return;
cannam@12 626
cannam@16 627 // std::cerr << "resizing from " << i << std::endl;
cannam@12 628
cannam@12 629 m_fs[plugin][n].features[j].values = (float *)realloc
cannam@12 630 (m_fs[plugin][n].features[j].values, sz * sizeof(float));
cannam@12 631
cannam@12 632 m_fvsizes[plugin][n][j] = sz;
cannam@12 633 }
cannam@12 634
cannam@13 635 PluginAdapterBase::AdapterMap *
cannam@13 636 PluginAdapterBase::m_adapterMap = 0;
cannam@3 637
cannam@3 638 }
cannam@3 639