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@3: #include "PluginAdapter.h" cannam@3: cannam@3: namespace Vamp { cannam@3: cannam@3: PluginAdapterBase::PluginAdapterBase() : cannam@3: m_populated(false) cannam@3: { cannam@3: } cannam@3: cannam@3: const VampPluginDescriptor * cannam@3: PluginAdapterBase::getDescriptor() cannam@3: { cannam@3: if (m_populated) return &m_descriptor; cannam@3: cannam@3: Plugin *plugin = createPlugin(48000); cannam@3: cannam@3: m_parameters = plugin->getParameterDescriptors(); cannam@3: m_programs = plugin->getPrograms(); cannam@3: 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@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@3: 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@3: PluginAdapterBase::~PluginAdapterBase() cannam@3: { cannam@3: if (!m_populated) return; cannam@3: 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@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@3: m_adapterMap.erase(&m_descriptor); cannam@3: } cannam@3: cannam@3: PluginAdapterBase * cannam@3: PluginAdapterBase::lookupAdapter(VampPluginHandle handle) cannam@3: { cannam@3: AdapterMap::const_iterator i = m_adapterMap.find(handle); cannam@3: if (i == m_adapterMap.end()) return 0; cannam@3: return i->second; cannam@3: } cannam@3: cannam@3: VampPluginHandle cannam@3: PluginAdapterBase::vampInstantiate(const VampPluginDescriptor *desc, cannam@3: float inputSampleRate) cannam@3: { cannam@3: if (m_adapterMap.find(desc) == m_adapterMap.end()) return 0; cannam@3: PluginAdapterBase *adapter = m_adapterMap[desc]; cannam@3: if (desc != &adapter->m_descriptor) return 0; cannam@3: cannam@3: Plugin *plugin = adapter->createPlugin(inputSampleRate); cannam@3: if (plugin) { cannam@3: m_adapterMap[plugin] = adapter; cannam@3: } cannam@3: cannam@3: return plugin; cannam@3: } cannam@3: cannam@3: void cannam@3: PluginAdapterBase::vampCleanup(VampPluginHandle handle) cannam@3: { cannam@3: PluginAdapterBase *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@3: PluginAdapterBase::vampInitialise(VampPluginHandle handle, cannam@3: unsigned int channels, cannam@3: unsigned int stepSize, cannam@3: unsigned int blockSize) cannam@3: { cannam@3: bool result = ((Plugin *)handle)->initialise cannam@3: (channels, stepSize, blockSize); cannam@3: return result ? 1 : 0; cannam@3: } cannam@3: cannam@3: void cannam@3: PluginAdapterBase::vampReset(VampPluginHandle handle) cannam@3: { cannam@3: ((Plugin *)handle)->reset(); cannam@3: } cannam@3: cannam@3: float cannam@3: PluginAdapterBase::vampGetParameter(VampPluginHandle handle, cannam@3: int param) cannam@3: { cannam@3: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return 0.0; cannam@3: Plugin::ParameterList &list = adapter->m_parameters; cannam@3: return ((Plugin *)handle)->getParameter(list[param].name); cannam@3: } cannam@3: cannam@3: void cannam@3: PluginAdapterBase::vampSetParameter(VampPluginHandle handle, cannam@3: int param, float value) cannam@3: { cannam@3: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return; cannam@3: Plugin::ParameterList &list = adapter->m_parameters; cannam@3: ((Plugin *)handle)->setParameter(list[param].name, value); cannam@3: } cannam@3: cannam@3: unsigned int cannam@3: PluginAdapterBase::vampGetCurrentProgram(VampPluginHandle handle) cannam@3: { cannam@3: PluginAdapterBase *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@3: PluginAdapterBase::vampSelectProgram(VampPluginHandle handle, cannam@3: unsigned int program) cannam@3: { cannam@3: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return; cannam@3: Plugin::ProgramList &list = adapter->m_programs; cannam@3: ((Plugin *)handle)->selectProgram(list[program]); cannam@3: } cannam@3: cannam@3: unsigned int cannam@3: PluginAdapterBase::vampGetPreferredStepSize(VampPluginHandle handle) cannam@3: { cannam@3: return ((Plugin *)handle)->getPreferredStepSize(); cannam@3: } cannam@3: cannam@3: unsigned int cannam@3: PluginAdapterBase::vampGetPreferredBlockSize(VampPluginHandle handle) cannam@3: { cannam@3: return ((Plugin *)handle)->getPreferredBlockSize(); cannam@3: } cannam@3: cannam@3: unsigned int cannam@3: PluginAdapterBase::vampGetMinChannelCount(VampPluginHandle handle) cannam@3: { cannam@3: return ((Plugin *)handle)->getMinChannelCount(); cannam@3: } cannam@3: cannam@3: unsigned int cannam@3: PluginAdapterBase::vampGetMaxChannelCount(VampPluginHandle handle) cannam@3: { cannam@3: return ((Plugin *)handle)->getMaxChannelCount(); cannam@3: } cannam@3: cannam@3: unsigned int cannam@3: PluginAdapterBase::vampGetOutputCount(VampPluginHandle handle) cannam@3: { cannam@3: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return 0; cannam@3: return adapter->getOutputCount((Plugin *)handle); cannam@3: } cannam@3: cannam@3: VampOutputDescriptor * cannam@3: PluginAdapterBase::vampGetOutputDescriptor(VampPluginHandle handle, cannam@3: unsigned int i) cannam@3: { cannam@3: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return 0; cannam@3: return adapter->getOutputDescriptor((Plugin *)handle, i); cannam@3: } cannam@3: cannam@3: void cannam@3: PluginAdapterBase::vampReleaseOutputDescriptor(VampOutputDescriptor *desc) cannam@3: { 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@9: for (unsigned int i = 0; i < desc->binCount; ++i) { cannam@9: free((void *)desc->binNames[i]); cannam@3: } cannam@9: if (desc->binNames) free((void *)desc->binNames); cannam@3: free((void *)desc); cannam@3: } cannam@3: cannam@3: VampFeatureList ** cannam@3: PluginAdapterBase::vampProcess(VampPluginHandle handle, cannam@3: float **inputBuffers, cannam@3: int sec, cannam@3: int nsec) cannam@3: { cannam@3: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return 0; cannam@3: return adapter->process((Plugin *)handle, cannam@3: inputBuffers, sec, nsec); cannam@3: } cannam@3: cannam@3: VampFeatureList ** cannam@3: PluginAdapterBase::vampGetRemainingFeatures(VampPluginHandle handle) cannam@3: { cannam@3: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@3: if (!adapter) return 0; cannam@3: return adapter->getRemainingFeatures((Plugin *)handle); cannam@3: } cannam@3: cannam@3: void cannam@3: PluginAdapterBase::vampReleaseFeatureSet(VampFeatureList **fs) cannam@3: { cannam@3: if (!fs) return; cannam@7: cannam@3: for (unsigned int i = 0; fs[i]; ++i) { cannam@7: cannam@3: for (unsigned int j = 0; j < fs[i]->featureCount; ++j) { cannam@3: VampFeature *feature = &fs[i]->features[j]; cannam@3: if (feature->values) free((void *)feature->values); cannam@3: if (feature->label) free((void *)feature->label); cannam@3: } cannam@7: cannam@3: if (fs[i]->features) free((void *)fs[i]->features); cannam@3: free((void *)fs[i]); cannam@3: } cannam@7: cannam@3: free((void *)fs); cannam@3: } cannam@3: cannam@3: void cannam@3: PluginAdapterBase::cleanup(Plugin *plugin) cannam@3: { 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@3: m_adapterMap.erase(plugin); cannam@3: delete ((Plugin *)plugin); cannam@3: } cannam@3: cannam@3: void cannam@3: PluginAdapterBase::checkOutputMap(Plugin *plugin) cannam@3: { cannam@3: if (!m_pluginOutputs[plugin]) { cannam@3: m_pluginOutputs[plugin] = new Plugin::OutputList cannam@3: (plugin->getOutputDescriptors()); cannam@3: } cannam@3: } cannam@3: cannam@3: unsigned int cannam@3: PluginAdapterBase::getOutputCount(Plugin *plugin) cannam@3: { cannam@3: checkOutputMap(plugin); cannam@3: return m_pluginOutputs[plugin]->size(); cannam@3: } cannam@3: cannam@3: VampOutputDescriptor * cannam@3: PluginAdapterBase::getOutputDescriptor(Plugin *plugin, cannam@3: unsigned int i) cannam@3: { cannam@3: checkOutputMap(plugin); 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@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@9: if (od.hasFixedBinCount && od.binCount > 0) { 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@3: cannam@3: return desc; cannam@3: } cannam@3: cannam@3: VampFeatureList ** cannam@3: PluginAdapterBase::process(Plugin *plugin, cannam@3: float **inputBuffers, cannam@3: int sec, int nsec) cannam@3: { cannam@3: RealTime rt(sec, nsec); cannam@3: return convertFeatures(plugin->process(inputBuffers, rt)); cannam@3: } cannam@3: cannam@3: VampFeatureList ** cannam@3: PluginAdapterBase::getRemainingFeatures(Plugin *plugin) cannam@3: { cannam@3: return convertFeatures(plugin->getRemainingFeatures()); cannam@3: } cannam@3: cannam@3: VampFeatureList ** cannam@3: PluginAdapterBase::convertFeatures(const Plugin::FeatureSet &features) cannam@3: { cannam@3: unsigned int n = 0; cannam@3: if (features.begin() != features.end()) { cannam@3: Plugin::FeatureSet::const_iterator i = features.end(); cannam@3: --i; cannam@3: n = i->first + 1; cannam@3: } cannam@3: cannam@3: if (!n) return 0; cannam@3: cannam@3: VampFeatureList **fs = (VampFeatureList **) cannam@3: malloc((n + 1) * sizeof(VampFeatureList *)); cannam@3: cannam@3: for (unsigned int i = 0; i < n; ++i) { cannam@7: cannam@3: fs[i] = (VampFeatureList *)malloc(sizeof(VampFeatureList)); cannam@7: cannam@3: if (features.find(i) == features.end()) { cannam@7: cannam@3: fs[i]->featureCount = 0; cannam@3: fs[i]->features = 0; cannam@7: continue; cannam@7: } cannam@7: cannam@7: Plugin::FeatureSet::const_iterator fi = features.find(i); cannam@7: cannam@7: const Plugin::FeatureList &fl = fi->second; cannam@7: cannam@7: fs[i]->featureCount = fl.size(); cannam@7: cannam@7: if (fs[i]->featureCount == 0) { cannam@7: fs[i]->features = 0; cannam@7: continue; cannam@7: } cannam@7: cannam@7: fs[i]->features = (VampFeature *)malloc(fl.size() * sizeof(VampFeature)); cannam@7: cannam@7: for (unsigned int j = 0; j < fl.size(); ++j) { cannam@7: cannam@7: VampFeature *feature = &fs[i]->features[j]; 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: feature->label = strdup(fl[j].label.c_str()); cannam@7: cannam@7: if (feature->valueCount == 0) { cannam@7: feature->values = 0; cannam@7: continue; cannam@7: } cannam@7: cannam@7: feature->values = (float *)malloc cannam@7: (feature->valueCount * sizeof(float)); cannam@7: cannam@7: for (unsigned int k = 0; k < feature->valueCount; ++k) { cannam@7: feature->values[k] = fl[j].values[k]; cannam@3: } cannam@3: } cannam@3: } cannam@3: cannam@3: fs[n] = 0; cannam@3: cannam@3: return fs; cannam@3: } cannam@3: cannam@3: PluginAdapterBase::AdapterMap cannam@3: PluginAdapterBase::m_adapterMap; cannam@3: cannam@3: } cannam@3: