annotate src/vamp-sdk/PluginAdapter.cpp @ 227:6b30e064cab7 distinct-libraries

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