annotate vamp-sdk/PluginAdapter.cpp @ 64:9d3272c7db60

* Merge from host-factory-stuff branch: this adds several helper classes in the hostext directory that should make a host's life much easier. This will become version 1.1 of the SDK, eventually.
author cannam
date Fri, 01 Jun 2007 15:10:17 +0000
parents b907557b2fb9
children 6683f99107cf
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@23 39 //#define DEBUG_PLUGIN_ADAPTER 1
cannam@22 40
cannam@22 41
cannam@3 42 namespace Vamp {
cannam@3 43
cannam@3 44 PluginAdapterBase::PluginAdapterBase() :
cannam@3 45 m_populated(false)
cannam@3 46 {
cannam@22 47 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 48 std::cerr << "PluginAdapterBase[" << this << "]::PluginAdapterBase" << std::endl;
cannam@22 49 #endif
cannam@3 50 }
cannam@3 51
cannam@3 52 const VampPluginDescriptor *
cannam@3 53 PluginAdapterBase::getDescriptor()
cannam@3 54 {
cannam@22 55 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 56 std::cerr << "PluginAdapterBase[" << this << "]::getDescriptor" << std::endl;
cannam@22 57 #endif
cannam@22 58
cannam@3 59 if (m_populated) return &m_descriptor;
cannam@3 60
cannam@3 61 Plugin *plugin = createPlugin(48000);
cannam@3 62
cannam@50 63 if (plugin->getVampApiVersion() != VAMP_API_VERSION) {
cannam@50 64 std::cerr << "Vamp::PluginAdapterBase::getDescriptor: ERROR: "
cannam@50 65 << "Plugin object API version "
cannam@50 66 << plugin->getVampApiVersion()
cannam@50 67 << " does not match actual API version "
cannam@50 68 << VAMP_API_VERSION << std::endl;
cannam@50 69 delete plugin;
cannam@50 70 return 0;
cannam@50 71 }
cannam@50 72
cannam@3 73 m_parameters = plugin->getParameterDescriptors();
cannam@3 74 m_programs = plugin->getPrograms();
cannam@50 75
cannam@50 76 m_descriptor.vampApiVersion = plugin->getVampApiVersion();
cannam@49 77 m_descriptor.identifier = strdup(plugin->getIdentifier().c_str());
cannam@3 78 m_descriptor.name = strdup(plugin->getName().c_str());
cannam@3 79 m_descriptor.description = strdup(plugin->getDescription().c_str());
cannam@3 80 m_descriptor.maker = strdup(plugin->getMaker().c_str());
cannam@3 81 m_descriptor.pluginVersion = plugin->getPluginVersion();
cannam@3 82 m_descriptor.copyright = strdup(plugin->getCopyright().c_str());
cannam@3 83
cannam@3 84 m_descriptor.parameterCount = m_parameters.size();
cannam@3 85 m_descriptor.parameters = (const VampParameterDescriptor **)
cannam@3 86 malloc(m_parameters.size() * sizeof(VampParameterDescriptor));
cannam@7 87
cannam@7 88 unsigned int i;
cannam@3 89
cannam@7 90 for (i = 0; i < m_parameters.size(); ++i) {
cannam@3 91 VampParameterDescriptor *desc = (VampParameterDescriptor *)
cannam@3 92 malloc(sizeof(VampParameterDescriptor));
cannam@49 93 desc->identifier = strdup(m_parameters[i].identifier.c_str());
cannam@3 94 desc->name = strdup(m_parameters[i].name.c_str());
cannam@3 95 desc->description = strdup(m_parameters[i].description.c_str());
cannam@3 96 desc->unit = strdup(m_parameters[i].unit.c_str());
cannam@3 97 desc->minValue = m_parameters[i].minValue;
cannam@3 98 desc->maxValue = m_parameters[i].maxValue;
cannam@3 99 desc->defaultValue = m_parameters[i].defaultValue;
cannam@3 100 desc->isQuantized = m_parameters[i].isQuantized;
cannam@3 101 desc->quantizeStep = m_parameters[i].quantizeStep;
cannam@9 102 desc->valueNames = 0;
cannam@9 103 if (desc->isQuantized && !m_parameters[i].valueNames.empty()) {
cannam@9 104 desc->valueNames = (const char **)
cannam@9 105 malloc((m_parameters[i].valueNames.size()+1) * sizeof(char *));
cannam@9 106 for (unsigned int j = 0; j < m_parameters[i].valueNames.size(); ++j) {
cannam@9 107 desc->valueNames[j] = strdup(m_parameters[i].valueNames[j].c_str());
cannam@9 108 }
cannam@9 109 desc->valueNames[m_parameters[i].valueNames.size()] = 0;
cannam@9 110 }
cannam@3 111 m_descriptor.parameters[i] = desc;
cannam@3 112 }
cannam@3 113
cannam@3 114 m_descriptor.programCount = m_programs.size();
cannam@3 115 m_descriptor.programs = (const char **)
cannam@3 116 malloc(m_programs.size() * sizeof(const char *));
cannam@3 117
cannam@7 118 for (i = 0; i < m_programs.size(); ++i) {
cannam@3 119 m_descriptor.programs[i] = strdup(m_programs[i].c_str());
cannam@3 120 }
cannam@3 121
cannam@3 122 if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
cannam@3 123 m_descriptor.inputDomain = vampFrequencyDomain;
cannam@3 124 } else {
cannam@3 125 m_descriptor.inputDomain = vampTimeDomain;
cannam@3 126 }
cannam@3 127
cannam@3 128 m_descriptor.instantiate = vampInstantiate;
cannam@3 129 m_descriptor.cleanup = vampCleanup;
cannam@3 130 m_descriptor.initialise = vampInitialise;
cannam@3 131 m_descriptor.reset = vampReset;
cannam@3 132 m_descriptor.getParameter = vampGetParameter;
cannam@3 133 m_descriptor.setParameter = vampSetParameter;
cannam@3 134 m_descriptor.getCurrentProgram = vampGetCurrentProgram;
cannam@3 135 m_descriptor.selectProgram = vampSelectProgram;
cannam@3 136 m_descriptor.getPreferredStepSize = vampGetPreferredStepSize;
cannam@3 137 m_descriptor.getPreferredBlockSize = vampGetPreferredBlockSize;
cannam@3 138 m_descriptor.getMinChannelCount = vampGetMinChannelCount;
cannam@3 139 m_descriptor.getMaxChannelCount = vampGetMaxChannelCount;
cannam@3 140 m_descriptor.getOutputCount = vampGetOutputCount;
cannam@3 141 m_descriptor.getOutputDescriptor = vampGetOutputDescriptor;
cannam@3 142 m_descriptor.releaseOutputDescriptor = vampReleaseOutputDescriptor;
cannam@3 143 m_descriptor.process = vampProcess;
cannam@3 144 m_descriptor.getRemainingFeatures = vampGetRemainingFeatures;
cannam@3 145 m_descriptor.releaseFeatureSet = vampReleaseFeatureSet;
cannam@3 146
cannam@13 147 if (!m_adapterMap) {
cannam@13 148 m_adapterMap = new AdapterMap;
cannam@13 149 }
cannam@15 150 (*m_adapterMap)[&m_descriptor] = this;
cannam@3 151
cannam@3 152 delete plugin;
cannam@3 153
cannam@3 154 m_populated = true;
cannam@3 155 return &m_descriptor;
cannam@3 156 }
cannam@3 157
cannam@3 158 PluginAdapterBase::~PluginAdapterBase()
cannam@3 159 {
cannam@22 160 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 161 std::cerr << "PluginAdapterBase[" << this << "]::~PluginAdapterBase" << std::endl;
cannam@22 162 #endif
cannam@22 163
cannam@3 164 if (!m_populated) return;
cannam@3 165
cannam@49 166 free((void *)m_descriptor.identifier);
cannam@3 167 free((void *)m_descriptor.name);
cannam@3 168 free((void *)m_descriptor.description);
cannam@3 169 free((void *)m_descriptor.maker);
cannam@3 170 free((void *)m_descriptor.copyright);
cannam@3 171
cannam@3 172 for (unsigned int i = 0; i < m_descriptor.parameterCount; ++i) {
cannam@3 173 const VampParameterDescriptor *desc = m_descriptor.parameters[i];
cannam@49 174 free((void *)desc->identifier);
cannam@3 175 free((void *)desc->name);
cannam@3 176 free((void *)desc->description);
cannam@3 177 free((void *)desc->unit);
cannam@9 178 if (desc->valueNames) {
cannam@9 179 for (unsigned int j = 0; desc->valueNames[j]; ++j) {
cannam@9 180 free((void *)desc->valueNames[j]);
cannam@9 181 }
cannam@9 182 free((void *)desc->valueNames);
cannam@9 183 }
cannam@3 184 }
cannam@3 185 free((void *)m_descriptor.parameters);
cannam@3 186
cannam@3 187 for (unsigned int i = 0; i < m_descriptor.programCount; ++i) {
cannam@3 188 free((void *)m_descriptor.programs[i]);
cannam@3 189 }
cannam@3 190 free((void *)m_descriptor.programs);
cannam@3 191
cannam@13 192 if (m_adapterMap) {
cannam@13 193
cannam@13 194 m_adapterMap->erase(&m_descriptor);
cannam@13 195
cannam@13 196 if (m_adapterMap->empty()) {
cannam@13 197 delete m_adapterMap;
cannam@13 198 m_adapterMap = 0;
cannam@13 199 }
cannam@13 200 }
cannam@3 201 }
cannam@3 202
cannam@3 203 PluginAdapterBase *
cannam@3 204 PluginAdapterBase::lookupAdapter(VampPluginHandle handle)
cannam@3 205 {
cannam@22 206 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 207 std::cerr << "PluginAdapterBase::lookupAdapter(" << handle << ")" << std::endl;
cannam@22 208 #endif
cannam@22 209
cannam@13 210 if (!m_adapterMap) return 0;
cannam@13 211 AdapterMap::const_iterator i = m_adapterMap->find(handle);
cannam@13 212 if (i == m_adapterMap->end()) return 0;
cannam@3 213 return i->second;
cannam@3 214 }
cannam@3 215
cannam@3 216 VampPluginHandle
cannam@3 217 PluginAdapterBase::vampInstantiate(const VampPluginDescriptor *desc,
cannam@3 218 float inputSampleRate)
cannam@3 219 {
cannam@22 220 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 221 std::cerr << "PluginAdapterBase::vampInstantiate(" << desc << ")" << std::endl;
cannam@22 222 #endif
cannam@22 223
cannam@15 224 if (!m_adapterMap) {
cannam@15 225 m_adapterMap = new AdapterMap();
cannam@15 226 }
cannam@15 227
cannam@15 228 if (m_adapterMap->find(desc) == m_adapterMap->end()) {
cannam@15 229 std::cerr << "WARNING: PluginAdapterBase::vampInstantiate: Descriptor " << desc << " not in adapter map" << std::endl;
cannam@15 230 return 0;
cannam@15 231 }
cannam@15 232
cannam@13 233 PluginAdapterBase *adapter = (*m_adapterMap)[desc];
cannam@3 234 if (desc != &adapter->m_descriptor) return 0;
cannam@3 235
cannam@3 236 Plugin *plugin = adapter->createPlugin(inputSampleRate);
cannam@3 237 if (plugin) {
cannam@13 238 (*m_adapterMap)[plugin] = adapter;
cannam@3 239 }
cannam@3 240
cannam@22 241 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 242 std::cerr << "PluginAdapterBase::vampInstantiate(" << desc << "): returning handle " << plugin << std::endl;
cannam@22 243 #endif
cannam@22 244
cannam@3 245 return plugin;
cannam@3 246 }
cannam@3 247
cannam@3 248 void
cannam@3 249 PluginAdapterBase::vampCleanup(VampPluginHandle handle)
cannam@3 250 {
cannam@22 251 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 252 std::cerr << "PluginAdapterBase::vampCleanup(" << handle << ")" << std::endl;
cannam@22 253 #endif
cannam@22 254
cannam@3 255 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 256 if (!adapter) {
cannam@3 257 delete ((Plugin *)handle);
cannam@3 258 return;
cannam@3 259 }
cannam@3 260 adapter->cleanup(((Plugin *)handle));
cannam@3 261 }
cannam@3 262
cannam@3 263 int
cannam@3 264 PluginAdapterBase::vampInitialise(VampPluginHandle handle,
cannam@3 265 unsigned int channels,
cannam@3 266 unsigned int stepSize,
cannam@3 267 unsigned int blockSize)
cannam@3 268 {
cannam@22 269 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 270 std::cerr << "PluginAdapterBase::vampInitialise(" << handle << ", " << channels << ", " << stepSize << ", " << blockSize << ")" << std::endl;
cannam@22 271 #endif
cannam@22 272
cannam@3 273 bool result = ((Plugin *)handle)->initialise
cannam@3 274 (channels, stepSize, blockSize);
cannam@3 275 return result ? 1 : 0;
cannam@3 276 }
cannam@3 277
cannam@3 278 void
cannam@3 279 PluginAdapterBase::vampReset(VampPluginHandle handle)
cannam@3 280 {
cannam@22 281 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 282 std::cerr << "PluginAdapterBase::vampReset(" << handle << ")" << std::endl;
cannam@22 283 #endif
cannam@22 284
cannam@3 285 ((Plugin *)handle)->reset();
cannam@3 286 }
cannam@3 287
cannam@3 288 float
cannam@3 289 PluginAdapterBase::vampGetParameter(VampPluginHandle handle,
cannam@3 290 int param)
cannam@3 291 {
cannam@22 292 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 293 std::cerr << "PluginAdapterBase::vampGetParameter(" << handle << ", " << param << ")" << std::endl;
cannam@22 294 #endif
cannam@22 295
cannam@3 296 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 297 if (!adapter) return 0.0;
cannam@3 298 Plugin::ParameterList &list = adapter->m_parameters;
cannam@49 299 return ((Plugin *)handle)->getParameter(list[param].identifier);
cannam@3 300 }
cannam@3 301
cannam@3 302 void
cannam@3 303 PluginAdapterBase::vampSetParameter(VampPluginHandle handle,
cannam@3 304 int param, float value)
cannam@3 305 {
cannam@22 306 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 307 std::cerr << "PluginAdapterBase::vampSetParameter(" << handle << ", " << param << ", " << value << ")" << std::endl;
cannam@22 308 #endif
cannam@22 309
cannam@3 310 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 311 if (!adapter) return;
cannam@3 312 Plugin::ParameterList &list = adapter->m_parameters;
cannam@49 313 ((Plugin *)handle)->setParameter(list[param].identifier, value);
cannam@3 314 }
cannam@3 315
cannam@3 316 unsigned int
cannam@3 317 PluginAdapterBase::vampGetCurrentProgram(VampPluginHandle handle)
cannam@3 318 {
cannam@22 319 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 320 std::cerr << "PluginAdapterBase::vampGetCurrentProgram(" << handle << ")" << std::endl;
cannam@22 321 #endif
cannam@22 322
cannam@3 323 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 324 if (!adapter) return 0;
cannam@3 325 Plugin::ProgramList &list = adapter->m_programs;
cannam@3 326 std::string program = ((Plugin *)handle)->getCurrentProgram();
cannam@3 327 for (unsigned int i = 0; i < list.size(); ++i) {
cannam@3 328 if (list[i] == program) return i;
cannam@3 329 }
cannam@3 330 return 0;
cannam@3 331 }
cannam@3 332
cannam@3 333 void
cannam@3 334 PluginAdapterBase::vampSelectProgram(VampPluginHandle handle,
cannam@3 335 unsigned int program)
cannam@3 336 {
cannam@22 337 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 338 std::cerr << "PluginAdapterBase::vampSelectProgram(" << handle << ", " << program << ")" << std::endl;
cannam@22 339 #endif
cannam@22 340
cannam@3 341 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 342 if (!adapter) return;
cannam@3 343 Plugin::ProgramList &list = adapter->m_programs;
cannam@3 344 ((Plugin *)handle)->selectProgram(list[program]);
cannam@3 345 }
cannam@3 346
cannam@3 347 unsigned int
cannam@3 348 PluginAdapterBase::vampGetPreferredStepSize(VampPluginHandle handle)
cannam@3 349 {
cannam@22 350 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 351 std::cerr << "PluginAdapterBase::vampGetPreferredStepSize(" << handle << ")" << std::endl;
cannam@22 352 #endif
cannam@22 353
cannam@3 354 return ((Plugin *)handle)->getPreferredStepSize();
cannam@3 355 }
cannam@3 356
cannam@3 357 unsigned int
cannam@3 358 PluginAdapterBase::vampGetPreferredBlockSize(VampPluginHandle handle)
cannam@3 359 {
cannam@22 360 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 361 std::cerr << "PluginAdapterBase::vampGetPreferredBlockSize(" << handle << ")" << std::endl;
cannam@22 362 #endif
cannam@22 363
cannam@3 364 return ((Plugin *)handle)->getPreferredBlockSize();
cannam@3 365 }
cannam@3 366
cannam@3 367 unsigned int
cannam@3 368 PluginAdapterBase::vampGetMinChannelCount(VampPluginHandle handle)
cannam@3 369 {
cannam@22 370 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 371 std::cerr << "PluginAdapterBase::vampGetMinChannelCount(" << handle << ")" << std::endl;
cannam@22 372 #endif
cannam@22 373
cannam@3 374 return ((Plugin *)handle)->getMinChannelCount();
cannam@3 375 }
cannam@3 376
cannam@3 377 unsigned int
cannam@3 378 PluginAdapterBase::vampGetMaxChannelCount(VampPluginHandle handle)
cannam@3 379 {
cannam@22 380 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 381 std::cerr << "PluginAdapterBase::vampGetMaxChannelCount(" << handle << ")" << std::endl;
cannam@22 382 #endif
cannam@22 383
cannam@3 384 return ((Plugin *)handle)->getMaxChannelCount();
cannam@3 385 }
cannam@3 386
cannam@3 387 unsigned int
cannam@3 388 PluginAdapterBase::vampGetOutputCount(VampPluginHandle handle)
cannam@3 389 {
cannam@22 390 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 391 std::cerr << "PluginAdapterBase::vampGetOutputCount(" << handle << ")" << std::endl;
cannam@22 392 #endif
cannam@22 393
cannam@3 394 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@15 395
cannam@15 396 // std::cerr << "vampGetOutputCount: handle " << handle << " -> adapter "<< adapter << std::endl;
cannam@15 397
cannam@3 398 if (!adapter) return 0;
cannam@3 399 return adapter->getOutputCount((Plugin *)handle);
cannam@3 400 }
cannam@3 401
cannam@3 402 VampOutputDescriptor *
cannam@3 403 PluginAdapterBase::vampGetOutputDescriptor(VampPluginHandle handle,
cannam@3 404 unsigned int i)
cannam@3 405 {
cannam@22 406 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 407 std::cerr << "PluginAdapterBase::vampGetOutputDescriptor(" << handle << ", " << i << ")" << std::endl;
cannam@22 408 #endif
cannam@22 409
cannam@3 410 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@15 411
cannam@15 412 // std::cerr << "vampGetOutputDescriptor: handle " << handle << " -> adapter "<< adapter << std::endl;
cannam@15 413
cannam@3 414 if (!adapter) return 0;
cannam@3 415 return adapter->getOutputDescriptor((Plugin *)handle, i);
cannam@3 416 }
cannam@3 417
cannam@3 418 void
cannam@3 419 PluginAdapterBase::vampReleaseOutputDescriptor(VampOutputDescriptor *desc)
cannam@3 420 {
cannam@22 421 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 422 std::cerr << "PluginAdapterBase::vampReleaseOutputDescriptor(" << desc << ")" << std::endl;
cannam@22 423 #endif
cannam@22 424
cannam@49 425 if (desc->identifier) free((void *)desc->identifier);
cannam@3 426 if (desc->name) free((void *)desc->name);
cannam@3 427 if (desc->description) free((void *)desc->description);
cannam@3 428 if (desc->unit) free((void *)desc->unit);
cannam@40 429 if (desc->hasFixedBinCount && desc->binNames) {
cannam@40 430 for (unsigned int i = 0; i < desc->binCount; ++i) {
cannam@40 431 if (desc->binNames[i]) {
cannam@40 432 free((void *)desc->binNames[i]);
cannam@40 433 }
cannam@40 434 }
cannam@3 435 }
cannam@9 436 if (desc->binNames) free((void *)desc->binNames);
cannam@3 437 free((void *)desc);
cannam@3 438 }
cannam@3 439
cannam@12 440 VampFeatureList *
cannam@3 441 PluginAdapterBase::vampProcess(VampPluginHandle handle,
cannam@47 442 const float *const *inputBuffers,
cannam@3 443 int sec,
cannam@3 444 int nsec)
cannam@3 445 {
cannam@22 446 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 447 std::cerr << "PluginAdapterBase::vampProcess(" << handle << ", " << sec << ", " << nsec << ")" << std::endl;
cannam@22 448 #endif
cannam@22 449
cannam@3 450 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 451 if (!adapter) return 0;
cannam@3 452 return adapter->process((Plugin *)handle,
cannam@3 453 inputBuffers, sec, nsec);
cannam@3 454 }
cannam@3 455
cannam@12 456 VampFeatureList *
cannam@3 457 PluginAdapterBase::vampGetRemainingFeatures(VampPluginHandle handle)
cannam@3 458 {
cannam@22 459 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 460 std::cerr << "PluginAdapterBase::vampGetRemainingFeatures(" << handle << ")" << std::endl;
cannam@22 461 #endif
cannam@22 462
cannam@3 463 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 464 if (!adapter) return 0;
cannam@3 465 return adapter->getRemainingFeatures((Plugin *)handle);
cannam@3 466 }
cannam@3 467
cannam@3 468 void
cannam@12 469 PluginAdapterBase::vampReleaseFeatureSet(VampFeatureList *fs)
cannam@3 470 {
cannam@22 471 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 472 std::cerr << "PluginAdapterBase::vampReleaseFeatureSet" << std::endl;
cannam@22 473 #endif
cannam@3 474 }
cannam@3 475
cannam@3 476 void
cannam@3 477 PluginAdapterBase::cleanup(Plugin *plugin)
cannam@3 478 {
cannam@12 479 if (m_fs.find(plugin) != m_fs.end()) {
cannam@12 480 size_t outputCount = 0;
cannam@12 481 if (m_pluginOutputs[plugin]) {
cannam@12 482 outputCount = m_pluginOutputs[plugin]->size();
cannam@12 483 }
cannam@12 484 VampFeatureList *list = m_fs[plugin];
cannam@12 485 for (unsigned int i = 0; i < outputCount; ++i) {
cannam@12 486 for (unsigned int j = 0; j < m_fsizes[plugin][i]; ++j) {
cannam@12 487 if (list[i].features[j].label) {
cannam@12 488 free(list[i].features[j].label);
cannam@12 489 }
cannam@12 490 if (list[i].features[j].values) {
cannam@12 491 free(list[i].features[j].values);
cannam@12 492 }
cannam@12 493 }
cannam@12 494 if (list[i].features) free(list[i].features);
cannam@12 495 }
cannam@12 496 m_fs.erase(plugin);
cannam@12 497 m_fsizes.erase(plugin);
cannam@12 498 m_fvsizes.erase(plugin);
cannam@12 499 }
cannam@12 500
cannam@3 501 if (m_pluginOutputs.find(plugin) != m_pluginOutputs.end()) {
cannam@3 502 delete m_pluginOutputs[plugin];
cannam@3 503 m_pluginOutputs.erase(plugin);
cannam@3 504 }
cannam@13 505
cannam@13 506 if (m_adapterMap) {
cannam@13 507 m_adapterMap->erase(plugin);
cannam@13 508
cannam@13 509 if (m_adapterMap->empty()) {
cannam@13 510 delete m_adapterMap;
cannam@13 511 m_adapterMap = 0;
cannam@13 512 }
cannam@13 513 }
cannam@13 514
cannam@3 515 delete ((Plugin *)plugin);
cannam@3 516 }
cannam@3 517
cannam@3 518 void
cannam@3 519 PluginAdapterBase::checkOutputMap(Plugin *plugin)
cannam@3 520 {
cannam@15 521 if (m_pluginOutputs.find(plugin) == m_pluginOutputs.end() ||
cannam@15 522 !m_pluginOutputs[plugin]) {
cannam@3 523 m_pluginOutputs[plugin] = new Plugin::OutputList
cannam@3 524 (plugin->getOutputDescriptors());
cannam@49 525 // std::cerr << "PluginAdapterBase::checkOutputMap: Have " << m_pluginOutputs[plugin]->size() << " outputs for plugin " << plugin->getIdentifier() << std::endl;
cannam@3 526 }
cannam@3 527 }
cannam@3 528
cannam@3 529 unsigned int
cannam@3 530 PluginAdapterBase::getOutputCount(Plugin *plugin)
cannam@3 531 {
cannam@3 532 checkOutputMap(plugin);
cannam@3 533 return m_pluginOutputs[plugin]->size();
cannam@3 534 }
cannam@3 535
cannam@3 536 VampOutputDescriptor *
cannam@3 537 PluginAdapterBase::getOutputDescriptor(Plugin *plugin,
cannam@3 538 unsigned int i)
cannam@3 539 {
cannam@3 540 checkOutputMap(plugin);
cannam@3 541 Plugin::OutputDescriptor &od =
cannam@3 542 (*m_pluginOutputs[plugin])[i];
cannam@3 543
cannam@3 544 VampOutputDescriptor *desc = (VampOutputDescriptor *)
cannam@3 545 malloc(sizeof(VampOutputDescriptor));
cannam@3 546
cannam@49 547 desc->identifier = strdup(od.identifier.c_str());
cannam@3 548 desc->name = strdup(od.name.c_str());
cannam@3 549 desc->description = strdup(od.description.c_str());
cannam@3 550 desc->unit = strdup(od.unit.c_str());
cannam@9 551 desc->hasFixedBinCount = od.hasFixedBinCount;
cannam@9 552 desc->binCount = od.binCount;
cannam@3 553
cannam@9 554 if (od.hasFixedBinCount && od.binCount > 0) {
cannam@9 555 desc->binNames = (const char **)
cannam@9 556 malloc(od.binCount * sizeof(const char *));
cannam@3 557
cannam@9 558 for (unsigned int i = 0; i < od.binCount; ++i) {
cannam@9 559 if (i < od.binNames.size()) {
cannam@9 560 desc->binNames[i] = strdup(od.binNames[i].c_str());
cannam@7 561 } else {
cannam@9 562 desc->binNames[i] = 0;
cannam@7 563 }
cannam@3 564 }
cannam@7 565 } else {
cannam@9 566 desc->binNames = 0;
cannam@3 567 }
cannam@3 568
cannam@3 569 desc->hasKnownExtents = od.hasKnownExtents;
cannam@3 570 desc->minValue = od.minValue;
cannam@3 571 desc->maxValue = od.maxValue;
cannam@3 572 desc->isQuantized = od.isQuantized;
cannam@3 573 desc->quantizeStep = od.quantizeStep;
cannam@3 574
cannam@3 575 switch (od.sampleType) {
cannam@3 576 case Plugin::OutputDescriptor::OneSamplePerStep:
cannam@3 577 desc->sampleType = vampOneSamplePerStep; break;
cannam@3 578 case Plugin::OutputDescriptor::FixedSampleRate:
cannam@3 579 desc->sampleType = vampFixedSampleRate; break;
cannam@3 580 case Plugin::OutputDescriptor::VariableSampleRate:
cannam@3 581 desc->sampleType = vampVariableSampleRate; break;
cannam@3 582 }
cannam@3 583
cannam@3 584 desc->sampleRate = od.sampleRate;
cannam@3 585
cannam@3 586 return desc;
cannam@3 587 }
cannam@3 588
cannam@12 589 VampFeatureList *
cannam@3 590 PluginAdapterBase::process(Plugin *plugin,
cannam@47 591 const float *const *inputBuffers,
cannam@3 592 int sec, int nsec)
cannam@3 593 {
cannam@12 594 // std::cerr << "PluginAdapterBase::process" << std::endl;
cannam@3 595 RealTime rt(sec, nsec);
cannam@12 596 checkOutputMap(plugin);
cannam@12 597 return convertFeatures(plugin, plugin->process(inputBuffers, rt));
cannam@3 598 }
cannam@3 599
cannam@12 600 VampFeatureList *
cannam@3 601 PluginAdapterBase::getRemainingFeatures(Plugin *plugin)
cannam@3 602 {
cannam@12 603 // std::cerr << "PluginAdapterBase::getRemainingFeatures" << std::endl;
cannam@12 604 checkOutputMap(plugin);
cannam@12 605 return convertFeatures(plugin, plugin->getRemainingFeatures());
cannam@3 606 }
cannam@3 607
cannam@12 608 VampFeatureList *
cannam@12 609 PluginAdapterBase::convertFeatures(Plugin *plugin,
cannam@12 610 const Plugin::FeatureSet &features)
cannam@3 611 {
cannam@12 612 int lastN = -1;
cannam@3 613
cannam@12 614 int outputCount = 0;
cannam@12 615 if (m_pluginOutputs[plugin]) outputCount = m_pluginOutputs[plugin]->size();
cannam@12 616
cannam@12 617 resizeFS(plugin, outputCount);
cannam@12 618 VampFeatureList *fs = m_fs[plugin];
cannam@3 619
cannam@12 620 for (Plugin::FeatureSet::const_iterator fi = features.begin();
cannam@12 621 fi != features.end(); ++fi) {
cannam@3 622
cannam@12 623 int n = fi->first;
cannam@12 624
cannam@12 625 // std::cerr << "PluginAdapterBase::convertFeatures: n = " << n << std::endl;
cannam@7 626
cannam@12 627 if (n >= int(outputCount)) {
cannam@12 628 std::cerr << "WARNING: PluginAdapterBase::convertFeatures: Too many outputs from plugin (" << n+1 << ", only should be " << outputCount << ")" << std::endl;
cannam@7 629 continue;
cannam@7 630 }
cannam@7 631
cannam@12 632 if (n > lastN + 1) {
cannam@12 633 for (int i = lastN + 1; i < n; ++i) {
cannam@12 634 fs[i].featureCount = 0;
cannam@12 635 }
cannam@12 636 }
cannam@7 637
cannam@7 638 const Plugin::FeatureList &fl = fi->second;
cannam@7 639
cannam@12 640 size_t sz = fl.size();
cannam@12 641 if (sz > m_fsizes[plugin][n]) resizeFL(plugin, n, sz);
cannam@12 642 fs[n].featureCount = sz;
cannam@12 643
cannam@12 644 for (size_t j = 0; j < sz; ++j) {
cannam@7 645
cannam@12 646 // std::cerr << "PluginAdapterBase::convertFeatures: j = " << j << std::endl;
cannam@7 647
cannam@12 648 VampFeature *feature = &fs[n].features[j];
cannam@7 649
cannam@7 650 feature->hasTimestamp = fl[j].hasTimestamp;
cannam@7 651 feature->sec = fl[j].timestamp.sec;
cannam@7 652 feature->nsec = fl[j].timestamp.nsec;
cannam@7 653 feature->valueCount = fl[j].values.size();
cannam@7 654
cannam@12 655 if (feature->label) free(feature->label);
cannam@12 656
cannam@12 657 if (fl[j].label.empty()) {
cannam@12 658 feature->label = 0;
cannam@12 659 } else {
cannam@12 660 feature->label = strdup(fl[j].label.c_str());
cannam@7 661 }
cannam@7 662
cannam@12 663 if (feature->valueCount > m_fvsizes[plugin][n][j]) {
cannam@12 664 resizeFV(plugin, n, j, feature->valueCount);
cannam@12 665 }
cannam@7 666
cannam@7 667 for (unsigned int k = 0; k < feature->valueCount; ++k) {
cannam@12 668 // std::cerr << "PluginAdapterBase::convertFeatures: k = " << k << std::endl;
cannam@7 669 feature->values[k] = fl[j].values[k];
cannam@3 670 }
cannam@3 671 }
cannam@12 672
cannam@12 673 lastN = n;
cannam@3 674 }
cannam@3 675
cannam@12 676 if (lastN == -1) return 0;
cannam@12 677
cannam@12 678 if (int(outputCount) > lastN + 1) {
cannam@12 679 for (int i = lastN + 1; i < int(outputCount); ++i) {
cannam@12 680 fs[i].featureCount = 0;
cannam@12 681 }
cannam@12 682 }
cannam@3 683
cannam@3 684 return fs;
cannam@3 685 }
cannam@3 686
cannam@12 687 void
cannam@12 688 PluginAdapterBase::resizeFS(Plugin *plugin, int n)
cannam@12 689 {
cannam@12 690 // std::cerr << "PluginAdapterBase::resizeFS(" << plugin << ", " << n << ")" << std::endl;
cannam@12 691
cannam@12 692 int i = m_fsizes[plugin].size();
cannam@12 693 if (i >= n) return;
cannam@12 694
cannam@16 695 // std::cerr << "resizing from " << i << std::endl;
cannam@12 696
cannam@12 697 m_fs[plugin] = (VampFeatureList *)realloc
cannam@12 698 (m_fs[plugin], n * sizeof(VampFeatureList));
cannam@12 699
cannam@12 700 while (i < n) {
cannam@12 701 m_fs[plugin][i].featureCount = 0;
cannam@12 702 m_fs[plugin][i].features = 0;
cannam@12 703 m_fsizes[plugin].push_back(0);
cannam@12 704 m_fvsizes[plugin].push_back(std::vector<size_t>());
cannam@12 705 i++;
cannam@12 706 }
cannam@12 707 }
cannam@12 708
cannam@12 709 void
cannam@12 710 PluginAdapterBase::resizeFL(Plugin *plugin, int n, size_t sz)
cannam@12 711 {
cannam@16 712 // std::cerr << "PluginAdapterBase::resizeFL(" << plugin << ", " << n << ", "
cannam@16 713 // << sz << ")" << std::endl;
cannam@12 714
cannam@12 715 size_t i = m_fsizes[plugin][n];
cannam@12 716 if (i >= sz) return;
cannam@12 717
cannam@16 718 // std::cerr << "resizing from " << i << std::endl;
cannam@12 719
cannam@12 720 m_fs[plugin][n].features = (VampFeature *)realloc
cannam@12 721 (m_fs[plugin][n].features, sz * sizeof(VampFeature));
cannam@12 722
cannam@12 723 while (m_fsizes[plugin][n] < sz) {
cannam@12 724 m_fs[plugin][n].features[m_fsizes[plugin][n]].valueCount = 0;
cannam@12 725 m_fs[plugin][n].features[m_fsizes[plugin][n]].values = 0;
cannam@12 726 m_fs[plugin][n].features[m_fsizes[plugin][n]].label = 0;
cannam@12 727 m_fvsizes[plugin][n].push_back(0);
cannam@12 728 m_fsizes[plugin][n]++;
cannam@12 729 }
cannam@12 730 }
cannam@12 731
cannam@12 732 void
cannam@12 733 PluginAdapterBase::resizeFV(Plugin *plugin, int n, int j, size_t sz)
cannam@12 734 {
cannam@16 735 // std::cerr << "PluginAdapterBase::resizeFV(" << plugin << ", " << n << ", "
cannam@16 736 // << j << ", " << sz << ")" << std::endl;
cannam@12 737
cannam@12 738 size_t i = m_fvsizes[plugin][n][j];
cannam@12 739 if (i >= sz) return;
cannam@12 740
cannam@16 741 // std::cerr << "resizing from " << i << std::endl;
cannam@12 742
cannam@12 743 m_fs[plugin][n].features[j].values = (float *)realloc
cannam@12 744 (m_fs[plugin][n].features[j].values, sz * sizeof(float));
cannam@12 745
cannam@12 746 m_fvsizes[plugin][n][j] = sz;
cannam@12 747 }
cannam@12 748
cannam@13 749 PluginAdapterBase::AdapterMap *
cannam@13 750 PluginAdapterBase::m_adapterMap = 0;
cannam@3 751
cannam@3 752 }
cannam@3 753