cannam@3: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@3: cannam@3: /* cannam@3: Vamp cannam@3: cannam@3: An API for audio analysis and feature extraction plugins. cannam@3: cannam@3: Centre for Digital Music, Queen Mary, University of London. cannam@3: Copyright 2006 Chris Cannam. cannam@3: cannam@3: Permission is hereby granted, free of charge, to any person cannam@3: obtaining a copy of this software and associated documentation cannam@3: files (the "Software"), to deal in the Software without cannam@3: restriction, including without limitation the rights to use, copy, cannam@3: modify, merge, publish, distribute, sublicense, and/or sell copies cannam@3: of the Software, and to permit persons to whom the Software is cannam@3: furnished to do so, subject to the following conditions: cannam@3: cannam@3: The above copyright notice and this permission notice shall be cannam@3: included in all copies or substantial portions of the Software. cannam@3: cannam@3: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@3: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@3: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND cannam@6: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR cannam@3: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@3: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@3: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@3: cannam@3: Except as contained in this notice, the names of the Centre for cannam@3: Digital Music; Queen Mary, University of London; and Chris Cannam cannam@3: shall not be used in advertising or otherwise to promote the sale, cannam@3: use or other dealings in this Software without prior written cannam@3: authorization. cannam@3: */ cannam@3: cannam@230: #include cannam@3: cannam@130: #include cannam@130: #include cannam@130: Chris@410: #if ( VAMP_SDK_MAJOR_VERSION != 2 || VAMP_SDK_MINOR_VERSION != 6 ) cannam@308: #error Unexpected version of Vamp SDK header included cannam@234: #endif cannam@234: cannam@234: cannam@23: //#define DEBUG_PLUGIN_ADAPTER 1 cannam@22: cannam@233: _VAMP_SDK_PLUGSPACE_BEGIN(PluginAdapter.cpp) cannam@230: cannam@3: namespace Vamp { cannam@3: cannam@76: class PluginAdapterBase::Impl cannam@3: { cannam@76: public: cannam@76: Impl(PluginAdapterBase *); cannam@76: ~Impl(); cannam@76: cannam@76: const VampPluginDescriptor *getDescriptor(); cannam@76: cannam@76: protected: cannam@76: PluginAdapterBase *m_base; cannam@76: cannam@76: static VampPluginHandle vampInstantiate(const VampPluginDescriptor *desc, cannam@167: float inputSampleRate); cannam@76: cannam@76: static void vampCleanup(VampPluginHandle handle); cannam@76: cannam@76: static int vampInitialise(VampPluginHandle handle, unsigned int channels, cannam@76: unsigned int stepSize, unsigned int blockSize); cannam@76: cannam@76: static void vampReset(VampPluginHandle handle); cannam@76: cannam@76: static float vampGetParameter(VampPluginHandle handle, int param); cannam@76: static void vampSetParameter(VampPluginHandle handle, int param, float value); cannam@76: cannam@76: static unsigned int vampGetCurrentProgram(VampPluginHandle handle); cannam@76: static void vampSelectProgram(VampPluginHandle handle, unsigned int program); cannam@76: cannam@76: static unsigned int vampGetPreferredStepSize(VampPluginHandle handle); cannam@76: static unsigned int vampGetPreferredBlockSize(VampPluginHandle handle); cannam@76: static unsigned int vampGetMinChannelCount(VampPluginHandle handle); cannam@76: static unsigned int vampGetMaxChannelCount(VampPluginHandle handle); cannam@76: cannam@76: static unsigned int vampGetOutputCount(VampPluginHandle handle); cannam@76: cannam@76: static VampOutputDescriptor *vampGetOutputDescriptor(VampPluginHandle handle, cannam@76: unsigned int i); cannam@76: cannam@76: static void vampReleaseOutputDescriptor(VampOutputDescriptor *desc); cannam@76: cannam@76: static VampFeatureList *vampProcess(VampPluginHandle handle, cannam@76: const float *const *inputBuffers, cannam@76: int sec, cannam@76: int nsec); cannam@76: cannam@76: static VampFeatureList *vampGetRemainingFeatures(VampPluginHandle handle); cannam@76: cannam@76: static void vampReleaseFeatureSet(VampFeatureList *fs); cannam@76: cannam@268: void checkOutputMap(Plugin *plugin); cannam@268: void markOutputsChanged(Plugin *plugin); cannam@268: cannam@76: void cleanup(Plugin *plugin); cannam@76: unsigned int getOutputCount(Plugin *plugin); cannam@76: VampOutputDescriptor *getOutputDescriptor(Plugin *plugin, cannam@76: unsigned int i); cannam@76: VampFeatureList *process(Plugin *plugin, cannam@76: const float *const *inputBuffers, cannam@76: int sec, int nsec); cannam@76: VampFeatureList *getRemainingFeatures(Plugin *plugin); cannam@76: VampFeatureList *convertFeatures(Plugin *plugin, cannam@76: const Plugin::FeatureSet &features); cannam@76: cannam@76: // maps both plugins and descriptors to adapters cannam@76: typedef std::map AdapterMap; cannam@76: static AdapterMap *m_adapterMap; cannam@76: static Impl *lookupAdapter(VampPluginHandle); cannam@76: cannam@76: bool m_populated; cannam@76: VampPluginDescriptor m_descriptor; cannam@76: Plugin::ParameterList m_parameters; cannam@76: Plugin::ProgramList m_programs; cannam@76: cannam@76: typedef std::map OutputMap; cannam@76: OutputMap m_pluginOutputs; cannam@76: cannam@76: std::map m_fs; cannam@76: std::map > m_fsizes; cannam@76: std::map > > m_fvsizes; cannam@76: void resizeFS(Plugin *plugin, int n); cannam@76: void resizeFL(Plugin *plugin, int n, size_t sz); cannam@76: void resizeFV(Plugin *plugin, int n, int j, size_t sz); cannam@76: }; cannam@76: cannam@76: PluginAdapterBase::PluginAdapterBase() cannam@76: { cannam@76: m_impl = new Impl(this); cannam@76: } cannam@76: cannam@76: PluginAdapterBase::~PluginAdapterBase() cannam@76: { cannam@76: delete m_impl; cannam@3: } cannam@3: cannam@3: const VampPluginDescriptor * cannam@3: PluginAdapterBase::getDescriptor() cannam@3: { cannam@76: return m_impl->getDescriptor(); cannam@76: } cannam@76: cannam@76: PluginAdapterBase::Impl::Impl(PluginAdapterBase *base) : cannam@76: m_base(base), cannam@76: m_populated(false) cannam@76: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl[" << this << "]::Impl" << std::endl; cannam@76: #endif cannam@76: } cannam@76: cannam@76: const VampPluginDescriptor * cannam@76: PluginAdapterBase::Impl::getDescriptor() cannam@76: { cannam@76: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl[" << this << "]::getDescriptor" << std::endl; cannam@22: #endif cannam@22: cannam@3: if (m_populated) return &m_descriptor; cannam@3: cannam@76: Plugin *plugin = m_base->createPlugin(48000); cannam@285: cannam@285: if (!plugin) { cannam@285: std::cerr << "PluginAdapterBase::Impl::getDescriptor: Failed to create plugin" << std::endl; cannam@285: return 0; cannam@285: } cannam@3: cannam@50: if (plugin->getVampApiVersion() != VAMP_API_VERSION) { cannam@76: std::cerr << "Vamp::PluginAdapterBase::Impl::getDescriptor: ERROR: " cannam@194: << "API version " << plugin->getVampApiVersion() cannam@194: << " for\nplugin \"" << plugin->getIdentifier() << "\" " cannam@194: << "differs from version " cannam@194: << VAMP_API_VERSION << " for adapter.\n" cannam@194: << "This plugin is probably linked against a different version of the Vamp SDK\n" cannam@194: << "from the version it was compiled with. It will need to be re-linked correctly\n" cannam@194: << "before it can be used." << std::endl; cannam@50: delete plugin; cannam@50: return 0; cannam@50: } cannam@50: cannam@3: m_parameters = plugin->getParameterDescriptors(); cannam@3: m_programs = plugin->getPrograms(); cannam@50: cannam@50: m_descriptor.vampApiVersion = plugin->getVampApiVersion(); cannam@49: m_descriptor.identifier = strdup(plugin->getIdentifier().c_str()); cannam@3: m_descriptor.name = strdup(plugin->getName().c_str()); cannam@3: m_descriptor.description = strdup(plugin->getDescription().c_str()); cannam@3: m_descriptor.maker = strdup(plugin->getMaker().c_str()); cannam@3: m_descriptor.pluginVersion = plugin->getPluginVersion(); cannam@3: m_descriptor.copyright = strdup(plugin->getCopyright().c_str()); cannam@3: cannam@3: m_descriptor.parameterCount = m_parameters.size(); cannam@3: m_descriptor.parameters = (const VampParameterDescriptor **) cannam@3: malloc(m_parameters.size() * sizeof(VampParameterDescriptor)); cannam@7: cannam@7: unsigned int i; cannam@3: cannam@7: for (i = 0; i < m_parameters.size(); ++i) { cannam@3: VampParameterDescriptor *desc = (VampParameterDescriptor *) cannam@3: malloc(sizeof(VampParameterDescriptor)); cannam@49: desc->identifier = strdup(m_parameters[i].identifier.c_str()); cannam@3: desc->name = strdup(m_parameters[i].name.c_str()); cannam@3: desc->description = strdup(m_parameters[i].description.c_str()); cannam@3: desc->unit = strdup(m_parameters[i].unit.c_str()); cannam@3: desc->minValue = m_parameters[i].minValue; cannam@3: desc->maxValue = m_parameters[i].maxValue; cannam@3: desc->defaultValue = m_parameters[i].defaultValue; cannam@3: desc->isQuantized = m_parameters[i].isQuantized; cannam@3: desc->quantizeStep = m_parameters[i].quantizeStep; cannam@9: desc->valueNames = 0; cannam@9: if (desc->isQuantized && !m_parameters[i].valueNames.empty()) { cannam@9: desc->valueNames = (const char **) cannam@9: malloc((m_parameters[i].valueNames.size()+1) * sizeof(char *)); cannam@9: for (unsigned int j = 0; j < m_parameters[i].valueNames.size(); ++j) { cannam@9: desc->valueNames[j] = strdup(m_parameters[i].valueNames[j].c_str()); cannam@9: } cannam@9: desc->valueNames[m_parameters[i].valueNames.size()] = 0; cannam@9: } cannam@3: m_descriptor.parameters[i] = desc; cannam@3: } cannam@3: cannam@3: m_descriptor.programCount = m_programs.size(); cannam@3: m_descriptor.programs = (const char **) cannam@3: malloc(m_programs.size() * sizeof(const char *)); cannam@3: cannam@7: for (i = 0; i < m_programs.size(); ++i) { cannam@3: m_descriptor.programs[i] = strdup(m_programs[i].c_str()); cannam@3: } cannam@3: cannam@3: if (plugin->getInputDomain() == Plugin::FrequencyDomain) { cannam@3: m_descriptor.inputDomain = vampFrequencyDomain; cannam@3: } else { cannam@3: m_descriptor.inputDomain = vampTimeDomain; cannam@3: } cannam@3: cannam@3: m_descriptor.instantiate = vampInstantiate; cannam@3: m_descriptor.cleanup = vampCleanup; cannam@3: m_descriptor.initialise = vampInitialise; cannam@3: m_descriptor.reset = vampReset; cannam@3: m_descriptor.getParameter = vampGetParameter; cannam@3: m_descriptor.setParameter = vampSetParameter; cannam@3: m_descriptor.getCurrentProgram = vampGetCurrentProgram; cannam@3: m_descriptor.selectProgram = vampSelectProgram; cannam@3: m_descriptor.getPreferredStepSize = vampGetPreferredStepSize; cannam@3: m_descriptor.getPreferredBlockSize = vampGetPreferredBlockSize; cannam@3: m_descriptor.getMinChannelCount = vampGetMinChannelCount; cannam@3: m_descriptor.getMaxChannelCount = vampGetMaxChannelCount; cannam@3: m_descriptor.getOutputCount = vampGetOutputCount; cannam@3: m_descriptor.getOutputDescriptor = vampGetOutputDescriptor; cannam@3: m_descriptor.releaseOutputDescriptor = vampReleaseOutputDescriptor; cannam@3: m_descriptor.process = vampProcess; cannam@3: m_descriptor.getRemainingFeatures = vampGetRemainingFeatures; cannam@3: m_descriptor.releaseFeatureSet = vampReleaseFeatureSet; cannam@3: cannam@13: if (!m_adapterMap) { cannam@13: m_adapterMap = new AdapterMap; cannam@13: } cannam@15: (*m_adapterMap)[&m_descriptor] = this; cannam@3: cannam@3: delete plugin; cannam@3: cannam@3: m_populated = true; cannam@3: return &m_descriptor; cannam@3: } cannam@3: cannam@76: PluginAdapterBase::Impl::~Impl() cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl[" << this << "]::~Impl" << std::endl; cannam@22: #endif cannam@22: cannam@3: if (!m_populated) return; cannam@3: cannam@49: free((void *)m_descriptor.identifier); cannam@3: free((void *)m_descriptor.name); cannam@3: free((void *)m_descriptor.description); cannam@3: free((void *)m_descriptor.maker); cannam@3: free((void *)m_descriptor.copyright); cannam@3: cannam@3: for (unsigned int i = 0; i < m_descriptor.parameterCount; ++i) { cannam@3: const VampParameterDescriptor *desc = m_descriptor.parameters[i]; cannam@49: free((void *)desc->identifier); cannam@3: free((void *)desc->name); cannam@3: free((void *)desc->description); cannam@3: free((void *)desc->unit); cannam@9: if (desc->valueNames) { cannam@9: for (unsigned int j = 0; desc->valueNames[j]; ++j) { cannam@9: free((void *)desc->valueNames[j]); cannam@9: } cannam@9: free((void *)desc->valueNames); cannam@9: } cannam@3: } cannam@3: free((void *)m_descriptor.parameters); cannam@3: cannam@3: for (unsigned int i = 0; i < m_descriptor.programCount; ++i) { cannam@3: free((void *)m_descriptor.programs[i]); cannam@3: } cannam@3: free((void *)m_descriptor.programs); cannam@3: cannam@13: if (m_adapterMap) { cannam@13: cannam@13: m_adapterMap->erase(&m_descriptor); cannam@13: cannam@13: if (m_adapterMap->empty()) { cannam@13: delete m_adapterMap; cannam@13: m_adapterMap = 0; cannam@13: } cannam@13: } cannam@3: } cannam@3: cannam@76: PluginAdapterBase::Impl * cannam@76: PluginAdapterBase::Impl::lookupAdapter(VampPluginHandle handle) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::lookupAdapter(" << handle << ")" << std::endl; cannam@22: #endif cannam@22: cannam@13: if (!m_adapterMap) return 0; cannam@13: AdapterMap::const_iterator i = m_adapterMap->find(handle); cannam@13: if (i == m_adapterMap->end()) return 0; cannam@3: return i->second; cannam@3: } cannam@3: cannam@3: VampPluginHandle cannam@76: PluginAdapterBase::Impl::vampInstantiate(const VampPluginDescriptor *desc, cannam@268: float inputSampleRate) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampInstantiate(" << desc << ")" << std::endl; cannam@22: #endif cannam@22: cannam@15: if (!m_adapterMap) { cannam@15: m_adapterMap = new AdapterMap(); cannam@15: } cannam@15: cannam@15: if (m_adapterMap->find(desc) == m_adapterMap->end()) { cannam@76: std::cerr << "WARNING: PluginAdapterBase::Impl::vampInstantiate: Descriptor " << desc << " not in adapter map" << std::endl; cannam@15: return 0; cannam@15: } cannam@15: cannam@76: Impl *adapter = (*m_adapterMap)[desc]; cannam@3: if (desc != &adapter->m_descriptor) return 0; cannam@3: cannam@76: Plugin *plugin = adapter->m_base->createPlugin(inputSampleRate); cannam@3: if (plugin) { cannam@13: (*m_adapterMap)[plugin] = adapter; cannam@3: } cannam@3: cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampInstantiate(" << desc << "): returning handle " << plugin << std::endl; cannam@22: #endif cannam@22: cannam@3: return plugin; cannam@3: } cannam@3: cannam@3: void cannam@76: PluginAdapterBase::Impl::vampCleanup(VampPluginHandle handle) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampCleanup(" << handle << ")" << std::endl; cannam@22: #endif cannam@22: cannam@76: Impl *adapter = lookupAdapter(handle); cannam@3: if (!adapter) { cannam@3: delete ((Plugin *)handle); cannam@3: return; cannam@3: } cannam@3: adapter->cleanup(((Plugin *)handle)); cannam@3: } cannam@3: cannam@3: int cannam@76: PluginAdapterBase::Impl::vampInitialise(VampPluginHandle handle, cannam@268: unsigned int channels, cannam@268: unsigned int stepSize, cannam@268: unsigned int blockSize) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampInitialise(" << handle << ", " << channels << ", " << stepSize << ", " << blockSize << ")" << std::endl; cannam@22: #endif cannam@22: cannam@268: Impl *adapter = lookupAdapter(handle); cannam@268: if (!adapter) return 0; cannam@268: bool result = ((Plugin *)handle)->initialise(channels, stepSize, blockSize); cannam@268: adapter->markOutputsChanged((Plugin *)handle); cannam@3: return result ? 1 : 0; cannam@3: } cannam@3: cannam@3: void cannam@76: PluginAdapterBase::Impl::vampReset(VampPluginHandle handle) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampReset(" << handle << ")" << std::endl; cannam@22: #endif cannam@22: cannam@3: ((Plugin *)handle)->reset(); cannam@3: } cannam@3: cannam@3: float cannam@76: PluginAdapterBase::Impl::vampGetParameter(VampPluginHandle handle, cannam@3: int param) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampGetParameter(" << handle << ", " << param << ")" << std::endl; cannam@22: #endif cannam@22: cannam@76: Impl *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return 0.0; cannam@3: Plugin::ParameterList &list = adapter->m_parameters; cannam@49: return ((Plugin *)handle)->getParameter(list[param].identifier); cannam@3: } cannam@3: cannam@3: void cannam@76: PluginAdapterBase::Impl::vampSetParameter(VampPluginHandle handle, cannam@3: int param, float value) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampSetParameter(" << handle << ", " << param << ", " << value << ")" << std::endl; cannam@22: #endif cannam@22: cannam@76: Impl *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return; cannam@3: Plugin::ParameterList &list = adapter->m_parameters; cannam@49: ((Plugin *)handle)->setParameter(list[param].identifier, value); cannam@268: adapter->markOutputsChanged((Plugin *)handle); cannam@3: } cannam@3: cannam@3: unsigned int cannam@76: PluginAdapterBase::Impl::vampGetCurrentProgram(VampPluginHandle handle) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampGetCurrentProgram(" << handle << ")" << std::endl; cannam@22: #endif cannam@22: cannam@76: Impl *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return 0; cannam@3: Plugin::ProgramList &list = adapter->m_programs; cannam@3: std::string program = ((Plugin *)handle)->getCurrentProgram(); cannam@3: for (unsigned int i = 0; i < list.size(); ++i) { cannam@3: if (list[i] == program) return i; cannam@3: } cannam@3: return 0; cannam@3: } cannam@3: cannam@3: void cannam@76: PluginAdapterBase::Impl::vampSelectProgram(VampPluginHandle handle, cannam@268: unsigned int program) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampSelectProgram(" << handle << ", " << program << ")" << std::endl; cannam@22: #endif cannam@22: cannam@76: Impl *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return; cannam@268: cannam@3: Plugin::ProgramList &list = adapter->m_programs; cannam@3: ((Plugin *)handle)->selectProgram(list[program]); cannam@268: cannam@268: adapter->markOutputsChanged((Plugin *)handle); cannam@3: } cannam@3: cannam@3: unsigned int cannam@76: PluginAdapterBase::Impl::vampGetPreferredStepSize(VampPluginHandle handle) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampGetPreferredStepSize(" << handle << ")" << std::endl; cannam@22: #endif cannam@22: cannam@3: return ((Plugin *)handle)->getPreferredStepSize(); cannam@3: } cannam@3: cannam@3: unsigned int cannam@76: PluginAdapterBase::Impl::vampGetPreferredBlockSize(VampPluginHandle handle) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampGetPreferredBlockSize(" << handle << ")" << std::endl; cannam@22: #endif cannam@22: cannam@3: return ((Plugin *)handle)->getPreferredBlockSize(); cannam@3: } cannam@3: cannam@3: unsigned int cannam@76: PluginAdapterBase::Impl::vampGetMinChannelCount(VampPluginHandle handle) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampGetMinChannelCount(" << handle << ")" << std::endl; cannam@22: #endif cannam@22: cannam@3: return ((Plugin *)handle)->getMinChannelCount(); cannam@3: } cannam@3: cannam@3: unsigned int cannam@76: PluginAdapterBase::Impl::vampGetMaxChannelCount(VampPluginHandle handle) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampGetMaxChannelCount(" << handle << ")" << std::endl; cannam@22: #endif cannam@22: cannam@3: return ((Plugin *)handle)->getMaxChannelCount(); cannam@3: } cannam@3: cannam@3: unsigned int cannam@76: PluginAdapterBase::Impl::vampGetOutputCount(VampPluginHandle handle) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampGetOutputCount(" << handle << ")" << std::endl; cannam@22: #endif cannam@22: cannam@76: Impl *adapter = lookupAdapter(handle); cannam@15: cannam@15: // std::cerr << "vampGetOutputCount: handle " << handle << " -> adapter "<< adapter << std::endl; cannam@15: cannam@3: if (!adapter) return 0; cannam@3: return adapter->getOutputCount((Plugin *)handle); cannam@3: } cannam@3: cannam@3: VampOutputDescriptor * cannam@76: PluginAdapterBase::Impl::vampGetOutputDescriptor(VampPluginHandle handle, cannam@268: unsigned int i) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampGetOutputDescriptor(" << handle << ", " << i << ")" << std::endl; cannam@22: #endif cannam@22: cannam@76: Impl *adapter = lookupAdapter(handle); cannam@15: cannam@15: // std::cerr << "vampGetOutputDescriptor: handle " << handle << " -> adapter "<< adapter << std::endl; cannam@15: cannam@3: if (!adapter) return 0; cannam@3: return adapter->getOutputDescriptor((Plugin *)handle, i); cannam@3: } cannam@3: cannam@3: void cannam@76: PluginAdapterBase::Impl::vampReleaseOutputDescriptor(VampOutputDescriptor *desc) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampReleaseOutputDescriptor(" << desc << ")" << std::endl; cannam@22: #endif cannam@22: cannam@49: if (desc->identifier) free((void *)desc->identifier); cannam@3: if (desc->name) free((void *)desc->name); cannam@3: if (desc->description) free((void *)desc->description); cannam@3: if (desc->unit) free((void *)desc->unit); cannam@40: if (desc->hasFixedBinCount && desc->binNames) { cannam@40: for (unsigned int i = 0; i < desc->binCount; ++i) { cannam@40: if (desc->binNames[i]) { cannam@40: free((void *)desc->binNames[i]); cannam@40: } cannam@40: } cannam@3: } cannam@9: if (desc->binNames) free((void *)desc->binNames); cannam@3: free((void *)desc); cannam@3: } cannam@3: cannam@12: VampFeatureList * cannam@76: PluginAdapterBase::Impl::vampProcess(VampPluginHandle handle, cannam@108: const float *const *inputBuffers, cannam@108: int sec, cannam@108: int nsec) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampProcess(" << handle << ", " << sec << ", " << nsec << ")" << std::endl; cannam@22: #endif cannam@22: cannam@76: Impl *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return 0; cannam@268: return adapter->process((Plugin *)handle, inputBuffers, sec, nsec); cannam@3: } cannam@3: cannam@12: VampFeatureList * cannam@76: PluginAdapterBase::Impl::vampGetRemainingFeatures(VampPluginHandle handle) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampGetRemainingFeatures(" << handle << ")" << std::endl; cannam@22: #endif cannam@22: cannam@76: Impl *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return 0; cannam@3: return adapter->getRemainingFeatures((Plugin *)handle); cannam@3: } cannam@3: cannam@3: void Chris@398: PluginAdapterBase::Impl::vampReleaseFeatureSet(VampFeatureList *) cannam@3: { cannam@22: #ifdef DEBUG_PLUGIN_ADAPTER cannam@76: std::cerr << "PluginAdapterBase::Impl::vampReleaseFeatureSet" << std::endl; cannam@22: #endif cannam@3: } cannam@3: cannam@3: void cannam@76: PluginAdapterBase::Impl::cleanup(Plugin *plugin) cannam@3: { cannam@12: if (m_fs.find(plugin) != m_fs.end()) { cannam@12: size_t outputCount = 0; cannam@12: if (m_pluginOutputs[plugin]) { cannam@12: outputCount = m_pluginOutputs[plugin]->size(); cannam@12: } cannam@12: VampFeatureList *list = m_fs[plugin]; cannam@12: for (unsigned int i = 0; i < outputCount; ++i) { cannam@12: for (unsigned int j = 0; j < m_fsizes[plugin][i]; ++j) { cannam@168: if (list[i].features[j].v1.label) { cannam@168: free(list[i].features[j].v1.label); cannam@12: } cannam@168: if (list[i].features[j].v1.values) { cannam@168: free(list[i].features[j].v1.values); cannam@12: } cannam@12: } cannam@12: if (list[i].features) free(list[i].features); cannam@12: } cannam@12: m_fs.erase(plugin); cannam@12: m_fsizes.erase(plugin); cannam@12: m_fvsizes.erase(plugin); cannam@12: } cannam@12: cannam@3: if (m_pluginOutputs.find(plugin) != m_pluginOutputs.end()) { cannam@3: delete m_pluginOutputs[plugin]; cannam@3: m_pluginOutputs.erase(plugin); cannam@3: } cannam@13: cannam@13: if (m_adapterMap) { cannam@13: m_adapterMap->erase(plugin); cannam@13: cannam@13: if (m_adapterMap->empty()) { cannam@13: delete m_adapterMap; cannam@13: m_adapterMap = 0; cannam@13: } cannam@13: } cannam@13: cannam@3: delete ((Plugin *)plugin); cannam@3: } cannam@3: cannam@3: void cannam@76: PluginAdapterBase::Impl::checkOutputMap(Plugin *plugin) cannam@3: { cannam@268: OutputMap::iterator i = m_pluginOutputs.find(plugin); cannam@268: cannam@268: if (i == m_pluginOutputs.end() || !i->second) { cannam@268: cannam@3: m_pluginOutputs[plugin] = new Plugin::OutputList cannam@3: (plugin->getOutputDescriptors()); cannam@268: cannam@76: // std::cerr << "PluginAdapterBase::Impl::checkOutputMap: Have " << m_pluginOutputs[plugin]->size() << " outputs for plugin " << plugin->getIdentifier() << std::endl; cannam@3: } cannam@3: } cannam@3: cannam@268: void cannam@268: PluginAdapterBase::Impl::markOutputsChanged(Plugin *plugin) cannam@268: { cannam@268: OutputMap::iterator i = m_pluginOutputs.find(plugin); cannam@268: cannam@268: // std::cerr << "PluginAdapterBase::Impl::markOutputsChanged" << std::endl; cannam@268: cannam@268: if (i != m_pluginOutputs.end()) { cannam@268: cannam@268: Plugin::OutputList *list = i->second; cannam@268: m_pluginOutputs.erase(i); cannam@268: delete list; cannam@268: } cannam@268: } cannam@268: cannam@3: unsigned int cannam@76: PluginAdapterBase::Impl::getOutputCount(Plugin *plugin) cannam@3: { cannam@3: checkOutputMap(plugin); cannam@268: cannam@3: return m_pluginOutputs[plugin]->size(); cannam@3: } cannam@3: cannam@3: VampOutputDescriptor * cannam@76: PluginAdapterBase::Impl::getOutputDescriptor(Plugin *plugin, cannam@268: unsigned int i) cannam@3: { cannam@3: checkOutputMap(plugin); cannam@268: cannam@3: Plugin::OutputDescriptor &od = cannam@3: (*m_pluginOutputs[plugin])[i]; cannam@3: cannam@3: VampOutputDescriptor *desc = (VampOutputDescriptor *) cannam@3: malloc(sizeof(VampOutputDescriptor)); cannam@3: cannam@49: desc->identifier = strdup(od.identifier.c_str()); cannam@3: desc->name = strdup(od.name.c_str()); cannam@3: desc->description = strdup(od.description.c_str()); cannam@3: desc->unit = strdup(od.unit.c_str()); cannam@9: desc->hasFixedBinCount = od.hasFixedBinCount; cannam@9: desc->binCount = od.binCount; cannam@3: cannam@240: if (od.hasFixedBinCount && od.binCount > 0 cannam@240: // We would like to do "&& !od.binNames.empty()" here -- but we cannam@240: // can't, because it will crash older versions of the host adapter cannam@240: // which try to copy the names across whenever the bin count is cannam@240: // non-zero, regardless of whether they exist or not cannam@240: ) { cannam@9: desc->binNames = (const char **) cannam@9: malloc(od.binCount * sizeof(const char *)); cannam@3: cannam@9: for (unsigned int i = 0; i < od.binCount; ++i) { cannam@9: if (i < od.binNames.size()) { cannam@9: desc->binNames[i] = strdup(od.binNames[i].c_str()); cannam@7: } else { cannam@9: desc->binNames[i] = 0; cannam@7: } cannam@3: } cannam@7: } else { cannam@9: desc->binNames = 0; cannam@3: } cannam@3: cannam@3: desc->hasKnownExtents = od.hasKnownExtents; cannam@3: desc->minValue = od.minValue; cannam@3: desc->maxValue = od.maxValue; cannam@3: desc->isQuantized = od.isQuantized; cannam@3: desc->quantizeStep = od.quantizeStep; cannam@3: cannam@3: switch (od.sampleType) { cannam@3: case Plugin::OutputDescriptor::OneSamplePerStep: cannam@3: desc->sampleType = vampOneSamplePerStep; break; cannam@3: case Plugin::OutputDescriptor::FixedSampleRate: cannam@3: desc->sampleType = vampFixedSampleRate; break; cannam@3: case Plugin::OutputDescriptor::VariableSampleRate: cannam@3: desc->sampleType = vampVariableSampleRate; break; cannam@3: } cannam@3: cannam@3: desc->sampleRate = od.sampleRate; cannam@192: desc->hasDuration = od.hasDuration; cannam@3: cannam@3: return desc; cannam@3: } cannam@3: cannam@12: VampFeatureList * cannam@76: PluginAdapterBase::Impl::process(Plugin *plugin, cannam@108: const float *const *inputBuffers, cannam@108: int sec, int nsec) cannam@3: { cannam@76: // std::cerr << "PluginAdapterBase::Impl::process" << std::endl; cannam@3: RealTime rt(sec, nsec); cannam@12: checkOutputMap(plugin); cannam@12: return convertFeatures(plugin, plugin->process(inputBuffers, rt)); cannam@3: } cannam@3: cannam@12: VampFeatureList * cannam@76: PluginAdapterBase::Impl::getRemainingFeatures(Plugin *plugin) cannam@3: { cannam@76: // std::cerr << "PluginAdapterBase::Impl::getRemainingFeatures" << std::endl; cannam@12: checkOutputMap(plugin); cannam@12: return convertFeatures(plugin, plugin->getRemainingFeatures()); cannam@3: } cannam@3: cannam@12: VampFeatureList * cannam@76: PluginAdapterBase::Impl::convertFeatures(Plugin *plugin, cannam@167: const Plugin::FeatureSet &features) cannam@3: { cannam@12: int lastN = -1; cannam@3: cannam@12: int outputCount = 0; cannam@12: if (m_pluginOutputs[plugin]) outputCount = m_pluginOutputs[plugin]->size(); cannam@12: cannam@12: resizeFS(plugin, outputCount); cannam@12: VampFeatureList *fs = m_fs[plugin]; cannam@3: cannam@168: // std::cerr << "PluginAdapter(v2)::convertFeatures: NOTE: sizeof(Feature) == " << sizeof(Plugin::Feature) << ", sizeof(VampFeature) == " << sizeof(VampFeature) << ", sizeof(VampFeatureList) == " << sizeof(VampFeatureList) << std::endl; cannam@168: cannam@12: for (Plugin::FeatureSet::const_iterator fi = features.begin(); cannam@12: fi != features.end(); ++fi) { cannam@3: cannam@12: int n = fi->first; cannam@12: cannam@76: // std::cerr << "PluginAdapterBase::Impl::convertFeatures: n = " << n << std::endl; cannam@7: cannam@12: if (n >= int(outputCount)) { cannam@76: std::cerr << "WARNING: PluginAdapterBase::Impl::convertFeatures: Too many outputs from plugin (" << n+1 << ", only should be " << outputCount << ")" << std::endl; cannam@7: continue; cannam@7: } cannam@7: cannam@12: if (n > lastN + 1) { cannam@12: for (int i = lastN + 1; i < n; ++i) { cannam@12: fs[i].featureCount = 0; cannam@12: } cannam@12: } cannam@7: cannam@7: const Plugin::FeatureList &fl = fi->second; cannam@7: cannam@12: size_t sz = fl.size(); cannam@12: if (sz > m_fsizes[plugin][n]) resizeFL(plugin, n, sz); cannam@12: fs[n].featureCount = sz; cannam@12: cannam@12: for (size_t j = 0; j < sz; ++j) { cannam@7: cannam@76: // std::cerr << "PluginAdapterBase::Impl::convertFeatures: j = " << j << std::endl; cannam@7: cannam@168: VampFeature *feature = &fs[n].features[j].v1; cannam@7: cannam@7: feature->hasTimestamp = fl[j].hasTimestamp; cannam@7: feature->sec = fl[j].timestamp.sec; cannam@7: feature->nsec = fl[j].timestamp.nsec; cannam@7: feature->valueCount = fl[j].values.size(); cannam@7: cannam@168: VampFeatureV2 *v2 = &fs[n].features[j + sz].v2; cannam@167: cannam@167: v2->hasDuration = fl[j].hasDuration; cannam@167: v2->durationSec = fl[j].duration.sec; cannam@167: v2->durationNsec = fl[j].duration.nsec; cannam@167: cannam@12: if (feature->label) free(feature->label); cannam@12: cannam@12: if (fl[j].label.empty()) { cannam@12: feature->label = 0; cannam@12: } else { cannam@12: feature->label = strdup(fl[j].label.c_str()); cannam@7: } cannam@7: cannam@12: if (feature->valueCount > m_fvsizes[plugin][n][j]) { cannam@12: resizeFV(plugin, n, j, feature->valueCount); cannam@12: } cannam@7: cannam@7: for (unsigned int k = 0; k < feature->valueCount; ++k) { cannam@76: // std::cerr << "PluginAdapterBase::Impl::convertFeatures: k = " << k << std::endl; cannam@7: feature->values[k] = fl[j].values[k]; cannam@3: } cannam@3: } cannam@12: cannam@12: lastN = n; cannam@3: } cannam@3: cannam@12: if (lastN == -1) return 0; cannam@12: cannam@12: if (int(outputCount) > lastN + 1) { cannam@12: for (int i = lastN + 1; i < int(outputCount); ++i) { cannam@12: fs[i].featureCount = 0; cannam@12: } cannam@12: } cannam@3: cannam@168: // std::cerr << "PluginAdapter(v2)::convertFeatures: NOTE: have " << outputCount << " outputs" << std::endl; cannam@168: // for (int i = 0; i < outputCount; ++i) { cannam@168: // std::cerr << "PluginAdapter(v2)::convertFeatures: NOTE: output " << i << " has " << fs[i].featureCount << " features" << std::endl; cannam@168: // } cannam@168: cannam@168: cannam@3: return fs; cannam@3: } cannam@3: cannam@12: void cannam@76: PluginAdapterBase::Impl::resizeFS(Plugin *plugin, int n) cannam@12: { cannam@76: // std::cerr << "PluginAdapterBase::Impl::resizeFS(" << plugin << ", " << n << ")" << std::endl; cannam@12: cannam@12: int i = m_fsizes[plugin].size(); cannam@12: if (i >= n) return; cannam@12: cannam@16: // std::cerr << "resizing from " << i << std::endl; cannam@12: cannam@12: m_fs[plugin] = (VampFeatureList *)realloc cannam@12: (m_fs[plugin], n * sizeof(VampFeatureList)); cannam@12: cannam@12: while (i < n) { cannam@12: m_fs[plugin][i].featureCount = 0; cannam@12: m_fs[plugin][i].features = 0; cannam@12: m_fsizes[plugin].push_back(0); cannam@12: m_fvsizes[plugin].push_back(std::vector()); cannam@12: i++; cannam@12: } cannam@12: } cannam@12: cannam@12: void cannam@76: PluginAdapterBase::Impl::resizeFL(Plugin *plugin, int n, size_t sz) cannam@12: { cannam@76: // std::cerr << "PluginAdapterBase::Impl::resizeFL(" << plugin << ", " << n << ", " cannam@16: // << sz << ")" << std::endl; cannam@12: cannam@12: size_t i = m_fsizes[plugin][n]; cannam@12: if (i >= sz) return; cannam@12: cannam@16: // std::cerr << "resizing from " << i << std::endl; cannam@12: cannam@168: m_fs[plugin][n].features = (VampFeatureUnion *)realloc cannam@168: (m_fs[plugin][n].features, 2 * sz * sizeof(VampFeatureUnion)); cannam@167: cannam@12: while (m_fsizes[plugin][n] < sz) { cannam@168: m_fs[plugin][n].features[m_fsizes[plugin][n]].v1.hasTimestamp = 0; cannam@168: m_fs[plugin][n].features[m_fsizes[plugin][n]].v1.valueCount = 0; cannam@168: m_fs[plugin][n].features[m_fsizes[plugin][n]].v1.values = 0; cannam@168: m_fs[plugin][n].features[m_fsizes[plugin][n]].v1.label = 0; cannam@189: m_fs[plugin][n].features[m_fsizes[plugin][n] + sz].v2.hasDuration = 0; cannam@12: m_fvsizes[plugin][n].push_back(0); cannam@12: m_fsizes[plugin][n]++; cannam@12: } cannam@12: } cannam@12: cannam@12: void cannam@76: PluginAdapterBase::Impl::resizeFV(Plugin *plugin, int n, int j, size_t sz) cannam@12: { cannam@76: // std::cerr << "PluginAdapterBase::Impl::resizeFV(" << plugin << ", " << n << ", " cannam@16: // << j << ", " << sz << ")" << std::endl; cannam@12: cannam@12: size_t i = m_fvsizes[plugin][n][j]; cannam@12: if (i >= sz) return; cannam@12: cannam@16: // std::cerr << "resizing from " << i << std::endl; cannam@12: cannam@168: m_fs[plugin][n].features[j].v1.values = (float *)realloc cannam@168: (m_fs[plugin][n].features[j].v1.values, sz * sizeof(float)); cannam@12: cannam@12: m_fvsizes[plugin][n][j] = sz; cannam@12: } cannam@12: cannam@76: PluginAdapterBase::Impl::AdapterMap * cannam@76: PluginAdapterBase::Impl::m_adapterMap = 0; cannam@3: cannam@3: } cannam@3: cannam@233: _VAMP_SDK_PLUGSPACE_END(PluginAdapter.cpp) cannam@230: