annotate vamp-sdk/PluginAdapter.cpp @ 106:7c3574540a99

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