annotate vamp-sdk/PluginAdapter.cpp @ 22:1eb44d33a371

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