annotate vamp-sdk/PluginAdapter.cpp @ 48:f46bf5e0fa42

* isnan -> std::isnan, likewise isinf
author cannam
date Thu, 25 Jan 2007 13:39:31 +0000
parents be8fdfe25693
children aa64a46320d4
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@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@40 413 if (desc->hasFixedBinCount && desc->binNames) {
cannam@40 414 for (unsigned int i = 0; i < desc->binCount; ++i) {
cannam@40 415 if (desc->binNames[i]) {
cannam@40 416 free((void *)desc->binNames[i]);
cannam@40 417 }
cannam@40 418 }
cannam@3 419 }
cannam@9 420 if (desc->binNames) free((void *)desc->binNames);
cannam@3 421 free((void *)desc);
cannam@3 422 }
cannam@3 423
cannam@12 424 VampFeatureList *
cannam@3 425 PluginAdapterBase::vampProcess(VampPluginHandle handle,
cannam@47 426 const float *const *inputBuffers,
cannam@3 427 int sec,
cannam@3 428 int nsec)
cannam@3 429 {
cannam@22 430 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 431 std::cerr << "PluginAdapterBase::vampProcess(" << handle << ", " << sec << ", " << nsec << ")" << std::endl;
cannam@22 432 #endif
cannam@22 433
cannam@3 434 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 435 if (!adapter) return 0;
cannam@3 436 return adapter->process((Plugin *)handle,
cannam@3 437 inputBuffers, sec, nsec);
cannam@3 438 }
cannam@3 439
cannam@12 440 VampFeatureList *
cannam@3 441 PluginAdapterBase::vampGetRemainingFeatures(VampPluginHandle handle)
cannam@3 442 {
cannam@22 443 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 444 std::cerr << "PluginAdapterBase::vampGetRemainingFeatures(" << handle << ")" << std::endl;
cannam@22 445 #endif
cannam@22 446
cannam@3 447 PluginAdapterBase *adapter = lookupAdapter(handle);
cannam@3 448 if (!adapter) return 0;
cannam@3 449 return adapter->getRemainingFeatures((Plugin *)handle);
cannam@3 450 }
cannam@3 451
cannam@3 452 void
cannam@12 453 PluginAdapterBase::vampReleaseFeatureSet(VampFeatureList *fs)
cannam@3 454 {
cannam@22 455 #ifdef DEBUG_PLUGIN_ADAPTER
cannam@22 456 std::cerr << "PluginAdapterBase::vampReleaseFeatureSet" << std::endl;
cannam@22 457 #endif
cannam@3 458 }
cannam@3 459
cannam@3 460 void
cannam@3 461 PluginAdapterBase::cleanup(Plugin *plugin)
cannam@3 462 {
cannam@12 463 if (m_fs.find(plugin) != m_fs.end()) {
cannam@12 464 size_t outputCount = 0;
cannam@12 465 if (m_pluginOutputs[plugin]) {
cannam@12 466 outputCount = m_pluginOutputs[plugin]->size();
cannam@12 467 }
cannam@12 468 VampFeatureList *list = m_fs[plugin];
cannam@12 469 for (unsigned int i = 0; i < outputCount; ++i) {
cannam@12 470 for (unsigned int j = 0; j < m_fsizes[plugin][i]; ++j) {
cannam@12 471 if (list[i].features[j].label) {
cannam@12 472 free(list[i].features[j].label);
cannam@12 473 }
cannam@12 474 if (list[i].features[j].values) {
cannam@12 475 free(list[i].features[j].values);
cannam@12 476 }
cannam@12 477 }
cannam@12 478 if (list[i].features) free(list[i].features);
cannam@12 479 }
cannam@12 480 m_fs.erase(plugin);
cannam@12 481 m_fsizes.erase(plugin);
cannam@12 482 m_fvsizes.erase(plugin);
cannam@12 483 }
cannam@12 484
cannam@3 485 if (m_pluginOutputs.find(plugin) != m_pluginOutputs.end()) {
cannam@3 486 delete m_pluginOutputs[plugin];
cannam@3 487 m_pluginOutputs.erase(plugin);
cannam@3 488 }
cannam@13 489
cannam@13 490 if (m_adapterMap) {
cannam@13 491 m_adapterMap->erase(plugin);
cannam@13 492
cannam@13 493 if (m_adapterMap->empty()) {
cannam@13 494 delete m_adapterMap;
cannam@13 495 m_adapterMap = 0;
cannam@13 496 }
cannam@13 497 }
cannam@13 498
cannam@3 499 delete ((Plugin *)plugin);
cannam@3 500 }
cannam@3 501
cannam@3 502 void
cannam@3 503 PluginAdapterBase::checkOutputMap(Plugin *plugin)
cannam@3 504 {
cannam@15 505 if (m_pluginOutputs.find(plugin) == m_pluginOutputs.end() ||
cannam@15 506 !m_pluginOutputs[plugin]) {
cannam@3 507 m_pluginOutputs[plugin] = new Plugin::OutputList
cannam@3 508 (plugin->getOutputDescriptors());
cannam@15 509 // std::cerr << "PluginAdapterBase::checkOutputMap: Have " << m_pluginOutputs[plugin]->size() << " outputs for plugin label " << plugin->getName() << std::endl;
cannam@3 510 }
cannam@3 511 }
cannam@3 512
cannam@3 513 unsigned int
cannam@3 514 PluginAdapterBase::getOutputCount(Plugin *plugin)
cannam@3 515 {
cannam@3 516 checkOutputMap(plugin);
cannam@3 517 return m_pluginOutputs[plugin]->size();
cannam@3 518 }
cannam@3 519
cannam@3 520 VampOutputDescriptor *
cannam@3 521 PluginAdapterBase::getOutputDescriptor(Plugin *plugin,
cannam@3 522 unsigned int i)
cannam@3 523 {
cannam@3 524 checkOutputMap(plugin);
cannam@3 525 Plugin::OutputDescriptor &od =
cannam@3 526 (*m_pluginOutputs[plugin])[i];
cannam@3 527
cannam@3 528 VampOutputDescriptor *desc = (VampOutputDescriptor *)
cannam@3 529 malloc(sizeof(VampOutputDescriptor));
cannam@3 530
cannam@3 531 desc->name = strdup(od.name.c_str());
cannam@3 532 desc->description = strdup(od.description.c_str());
cannam@3 533 desc->unit = strdup(od.unit.c_str());
cannam@9 534 desc->hasFixedBinCount = od.hasFixedBinCount;
cannam@9 535 desc->binCount = od.binCount;
cannam@3 536
cannam@9 537 if (od.hasFixedBinCount && od.binCount > 0) {
cannam@9 538 desc->binNames = (const char **)
cannam@9 539 malloc(od.binCount * sizeof(const char *));
cannam@3 540
cannam@9 541 for (unsigned int i = 0; i < od.binCount; ++i) {
cannam@9 542 if (i < od.binNames.size()) {
cannam@9 543 desc->binNames[i] = strdup(od.binNames[i].c_str());
cannam@7 544 } else {
cannam@9 545 desc->binNames[i] = 0;
cannam@7 546 }
cannam@3 547 }
cannam@7 548 } else {
cannam@9 549 desc->binNames = 0;
cannam@3 550 }
cannam@3 551
cannam@3 552 desc->hasKnownExtents = od.hasKnownExtents;
cannam@3 553 desc->minValue = od.minValue;
cannam@3 554 desc->maxValue = od.maxValue;
cannam@3 555 desc->isQuantized = od.isQuantized;
cannam@3 556 desc->quantizeStep = od.quantizeStep;
cannam@3 557
cannam@3 558 switch (od.sampleType) {
cannam@3 559 case Plugin::OutputDescriptor::OneSamplePerStep:
cannam@3 560 desc->sampleType = vampOneSamplePerStep; break;
cannam@3 561 case Plugin::OutputDescriptor::FixedSampleRate:
cannam@3 562 desc->sampleType = vampFixedSampleRate; break;
cannam@3 563 case Plugin::OutputDescriptor::VariableSampleRate:
cannam@3 564 desc->sampleType = vampVariableSampleRate; break;
cannam@3 565 }
cannam@3 566
cannam@3 567 desc->sampleRate = od.sampleRate;
cannam@3 568
cannam@3 569 return desc;
cannam@3 570 }
cannam@3 571
cannam@12 572 VampFeatureList *
cannam@3 573 PluginAdapterBase::process(Plugin *plugin,
cannam@47 574 const float *const *inputBuffers,
cannam@3 575 int sec, int nsec)
cannam@3 576 {
cannam@12 577 // std::cerr << "PluginAdapterBase::process" << std::endl;
cannam@3 578 RealTime rt(sec, nsec);
cannam@12 579 checkOutputMap(plugin);
cannam@12 580 return convertFeatures(plugin, plugin->process(inputBuffers, rt));
cannam@3 581 }
cannam@3 582
cannam@12 583 VampFeatureList *
cannam@3 584 PluginAdapterBase::getRemainingFeatures(Plugin *plugin)
cannam@3 585 {
cannam@12 586 // std::cerr << "PluginAdapterBase::getRemainingFeatures" << std::endl;
cannam@12 587 checkOutputMap(plugin);
cannam@12 588 return convertFeatures(plugin, plugin->getRemainingFeatures());
cannam@3 589 }
cannam@3 590
cannam@12 591 VampFeatureList *
cannam@12 592 PluginAdapterBase::convertFeatures(Plugin *plugin,
cannam@12 593 const Plugin::FeatureSet &features)
cannam@3 594 {
cannam@12 595 int lastN = -1;
cannam@3 596
cannam@12 597 int outputCount = 0;
cannam@12 598 if (m_pluginOutputs[plugin]) outputCount = m_pluginOutputs[plugin]->size();
cannam@12 599
cannam@12 600 resizeFS(plugin, outputCount);
cannam@12 601 VampFeatureList *fs = m_fs[plugin];
cannam@3 602
cannam@12 603 for (Plugin::FeatureSet::const_iterator fi = features.begin();
cannam@12 604 fi != features.end(); ++fi) {
cannam@3 605
cannam@12 606 int n = fi->first;
cannam@12 607
cannam@12 608 // std::cerr << "PluginAdapterBase::convertFeatures: n = " << n << std::endl;
cannam@7 609
cannam@12 610 if (n >= int(outputCount)) {
cannam@12 611 std::cerr << "WARNING: PluginAdapterBase::convertFeatures: Too many outputs from plugin (" << n+1 << ", only should be " << outputCount << ")" << std::endl;
cannam@7 612 continue;
cannam@7 613 }
cannam@7 614
cannam@12 615 if (n > lastN + 1) {
cannam@12 616 for (int i = lastN + 1; i < n; ++i) {
cannam@12 617 fs[i].featureCount = 0;
cannam@12 618 }
cannam@12 619 }
cannam@7 620
cannam@7 621 const Plugin::FeatureList &fl = fi->second;
cannam@7 622
cannam@12 623 size_t sz = fl.size();
cannam@12 624 if (sz > m_fsizes[plugin][n]) resizeFL(plugin, n, sz);
cannam@12 625 fs[n].featureCount = sz;
cannam@12 626
cannam@12 627 for (size_t j = 0; j < sz; ++j) {
cannam@7 628
cannam@12 629 // std::cerr << "PluginAdapterBase::convertFeatures: j = " << j << std::endl;
cannam@7 630
cannam@12 631 VampFeature *feature = &fs[n].features[j];
cannam@7 632
cannam@7 633 feature->hasTimestamp = fl[j].hasTimestamp;
cannam@7 634 feature->sec = fl[j].timestamp.sec;
cannam@7 635 feature->nsec = fl[j].timestamp.nsec;
cannam@7 636 feature->valueCount = fl[j].values.size();
cannam@7 637
cannam@12 638 if (feature->label) free(feature->label);
cannam@12 639
cannam@12 640 if (fl[j].label.empty()) {
cannam@12 641 feature->label = 0;
cannam@12 642 } else {
cannam@12 643 feature->label = strdup(fl[j].label.c_str());
cannam@7 644 }
cannam@7 645
cannam@12 646 if (feature->valueCount > m_fvsizes[plugin][n][j]) {
cannam@12 647 resizeFV(plugin, n, j, feature->valueCount);
cannam@12 648 }
cannam@7 649
cannam@7 650 for (unsigned int k = 0; k < feature->valueCount; ++k) {
cannam@12 651 // std::cerr << "PluginAdapterBase::convertFeatures: k = " << k << std::endl;
cannam@7 652 feature->values[k] = fl[j].values[k];
cannam@3 653 }
cannam@3 654 }
cannam@12 655
cannam@12 656 lastN = n;
cannam@3 657 }
cannam@3 658
cannam@12 659 if (lastN == -1) return 0;
cannam@12 660
cannam@12 661 if (int(outputCount) > lastN + 1) {
cannam@12 662 for (int i = lastN + 1; i < int(outputCount); ++i) {
cannam@12 663 fs[i].featureCount = 0;
cannam@12 664 }
cannam@12 665 }
cannam@3 666
cannam@3 667 return fs;
cannam@3 668 }
cannam@3 669
cannam@12 670 void
cannam@12 671 PluginAdapterBase::resizeFS(Plugin *plugin, int n)
cannam@12 672 {
cannam@12 673 // std::cerr << "PluginAdapterBase::resizeFS(" << plugin << ", " << n << ")" << std::endl;
cannam@12 674
cannam@12 675 int i = m_fsizes[plugin].size();
cannam@12 676 if (i >= n) return;
cannam@12 677
cannam@16 678 // std::cerr << "resizing from " << i << std::endl;
cannam@12 679
cannam@12 680 m_fs[plugin] = (VampFeatureList *)realloc
cannam@12 681 (m_fs[plugin], n * sizeof(VampFeatureList));
cannam@12 682
cannam@12 683 while (i < n) {
cannam@12 684 m_fs[plugin][i].featureCount = 0;
cannam@12 685 m_fs[plugin][i].features = 0;
cannam@12 686 m_fsizes[plugin].push_back(0);
cannam@12 687 m_fvsizes[plugin].push_back(std::vector<size_t>());
cannam@12 688 i++;
cannam@12 689 }
cannam@12 690 }
cannam@12 691
cannam@12 692 void
cannam@12 693 PluginAdapterBase::resizeFL(Plugin *plugin, int n, size_t sz)
cannam@12 694 {
cannam@16 695 // std::cerr << "PluginAdapterBase::resizeFL(" << plugin << ", " << n << ", "
cannam@16 696 // << sz << ")" << std::endl;
cannam@12 697
cannam@12 698 size_t i = m_fsizes[plugin][n];
cannam@12 699 if (i >= sz) return;
cannam@12 700
cannam@16 701 // std::cerr << "resizing from " << i << std::endl;
cannam@12 702
cannam@12 703 m_fs[plugin][n].features = (VampFeature *)realloc
cannam@12 704 (m_fs[plugin][n].features, sz * sizeof(VampFeature));
cannam@12 705
cannam@12 706 while (m_fsizes[plugin][n] < sz) {
cannam@12 707 m_fs[plugin][n].features[m_fsizes[plugin][n]].valueCount = 0;
cannam@12 708 m_fs[plugin][n].features[m_fsizes[plugin][n]].values = 0;
cannam@12 709 m_fs[plugin][n].features[m_fsizes[plugin][n]].label = 0;
cannam@12 710 m_fvsizes[plugin][n].push_back(0);
cannam@12 711 m_fsizes[plugin][n]++;
cannam@12 712 }
cannam@12 713 }
cannam@12 714
cannam@12 715 void
cannam@12 716 PluginAdapterBase::resizeFV(Plugin *plugin, int n, int j, size_t sz)
cannam@12 717 {
cannam@16 718 // std::cerr << "PluginAdapterBase::resizeFV(" << plugin << ", " << n << ", "
cannam@16 719 // << j << ", " << sz << ")" << std::endl;
cannam@12 720
cannam@12 721 size_t i = m_fvsizes[plugin][n][j];
cannam@12 722 if (i >= sz) return;
cannam@12 723
cannam@16 724 // std::cerr << "resizing from " << i << std::endl;
cannam@12 725
cannam@12 726 m_fs[plugin][n].features[j].values = (float *)realloc
cannam@12 727 (m_fs[plugin][n].features[j].values, sz * sizeof(float));
cannam@12 728
cannam@12 729 m_fvsizes[plugin][n][j] = sz;
cannam@12 730 }
cannam@12 731
cannam@13 732 PluginAdapterBase::AdapterMap *
cannam@13 733 PluginAdapterBase::m_adapterMap = 0;
cannam@3 734
cannam@3 735 }
cannam@3 736