cannam@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@0: cannam@0: /* cannam@0: Vamp cannam@0: cannam@0: An API for audio analysis and feature extraction plugins. cannam@0: cannam@0: Centre for Digital Music, Queen Mary, University of London. cannam@0: Copyright 2006 Chris Cannam. cannam@0: cannam@0: Permission is hereby granted, free of charge, to any person cannam@0: obtaining a copy of this software and associated documentation cannam@0: files (the "Software"), to deal in the Software without cannam@0: restriction, including without limitation the rights to use, copy, cannam@0: modify, merge, publish, distribute, sublicense, and/or sell copies cannam@0: of the Software, and to permit persons to whom the Software is cannam@0: furnished to do so, subject to the following conditions: cannam@0: cannam@0: The above copyright notice and this permission notice shall be cannam@0: included in all copies or substantial portions of the Software. cannam@0: cannam@0: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@0: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@0: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND cannam@0: NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR cannam@0: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@0: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@0: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@0: cannam@0: Except as contained in this notice, the names of the Centre for cannam@0: Digital Music; Queen Mary, University of London; and Chris Cannam cannam@0: shall not be used in advertising or otherwise to promote the sale, cannam@0: use or other dealings in this Software without prior written cannam@0: authorization. cannam@0: */ cannam@0: cannam@0: #include "PluginAdapter.h" cannam@0: cannam@0: namespace Vamp { cannam@0: cannam@0: PluginAdapterBase::PluginAdapterBase() : cannam@0: m_populated(false) cannam@0: { cannam@0: } cannam@0: cannam@0: const VampPluginDescriptor * cannam@0: PluginAdapterBase::getDescriptor() cannam@0: { cannam@0: if (m_populated) return &m_descriptor; cannam@0: cannam@0: Plugin *plugin = createPlugin(48000); cannam@0: cannam@0: m_parameters = plugin->getParameterDescriptors(); cannam@0: m_programs = plugin->getPrograms(); cannam@0: cannam@0: m_descriptor.name = strdup(plugin->getName().c_str()); cannam@0: m_descriptor.description = strdup(plugin->getDescription().c_str()); cannam@0: m_descriptor.maker = strdup(plugin->getMaker().c_str()); cannam@0: m_descriptor.pluginVersion = plugin->getPluginVersion(); cannam@0: m_descriptor.copyright = strdup(plugin->getCopyright().c_str()); cannam@0: cannam@0: m_descriptor.parameterCount = m_parameters.size(); cannam@0: m_descriptor.parameters = (const VampParameterDescriptor **) cannam@0: malloc(m_parameters.size() * sizeof(VampParameterDescriptor)); cannam@0: cannam@0: for (unsigned int i = 0; i < m_parameters.size(); ++i) { cannam@0: VampParameterDescriptor *desc = (VampParameterDescriptor *) cannam@0: malloc(sizeof(VampParameterDescriptor)); cannam@0: desc->name = strdup(m_parameters[i].name.c_str()); cannam@0: desc->description = strdup(m_parameters[i].description.c_str()); cannam@0: desc->unit = strdup(m_parameters[i].unit.c_str()); cannam@0: desc->minValue = m_parameters[i].minValue; cannam@0: desc->maxValue = m_parameters[i].maxValue; cannam@0: desc->defaultValue = m_parameters[i].defaultValue; cannam@0: desc->isQuantized = m_parameters[i].isQuantized; cannam@0: desc->quantizeStep = m_parameters[i].quantizeStep; cannam@0: m_descriptor.parameters[i] = desc; cannam@0: } cannam@0: cannam@0: m_descriptor.programCount = m_programs.size(); cannam@0: m_descriptor.programs = (const char **) cannam@0: malloc(m_programs.size() * sizeof(const char *)); cannam@0: cannam@0: for (unsigned int i = 0; i < m_programs.size(); ++i) { cannam@0: m_descriptor.programs[i] = strdup(m_programs[i].c_str()); cannam@0: } cannam@0: cannam@0: if (plugin->getInputDomain() == Plugin::FrequencyDomain) { cannam@0: m_descriptor.inputDomain = vampFrequencyDomain; cannam@0: } else { cannam@0: m_descriptor.inputDomain = vampTimeDomain; cannam@0: } cannam@0: cannam@0: m_descriptor.instantiate = vampInstantiate; cannam@0: m_descriptor.cleanup = vampCleanup; cannam@0: m_descriptor.initialise = vampInitialise; cannam@0: m_descriptor.reset = vampReset; cannam@0: m_descriptor.getParameter = vampGetParameter; cannam@0: m_descriptor.setParameter = vampSetParameter; cannam@0: m_descriptor.getCurrentProgram = vampGetCurrentProgram; cannam@0: m_descriptor.selectProgram = vampSelectProgram; cannam@0: m_descriptor.getPreferredStepSize = vampGetPreferredStepSize; cannam@0: m_descriptor.getPreferredBlockSize = vampGetPreferredBlockSize; cannam@0: m_descriptor.getMinChannelCount = vampGetMinChannelCount; cannam@0: m_descriptor.getMaxChannelCount = vampGetMaxChannelCount; cannam@0: m_descriptor.getOutputCount = vampGetOutputCount; cannam@0: m_descriptor.getOutputDescriptor = vampGetOutputDescriptor; cannam@0: m_descriptor.releaseOutputDescriptor = vampReleaseOutputDescriptor; cannam@0: m_descriptor.process = vampProcess; cannam@0: m_descriptor.getRemainingFeatures = vampGetRemainingFeatures; cannam@0: m_descriptor.releaseFeatureSet = vampReleaseFeatureSet; cannam@0: cannam@0: m_adapterMap[&m_descriptor] = this; cannam@0: cannam@0: delete plugin; cannam@0: cannam@0: m_populated = true; cannam@0: return &m_descriptor; cannam@0: } cannam@0: cannam@0: PluginAdapterBase::~PluginAdapterBase() cannam@0: { cannam@0: if (!m_populated) return; cannam@0: cannam@0: free((void *)m_descriptor.name); cannam@0: free((void *)m_descriptor.description); cannam@0: free((void *)m_descriptor.maker); cannam@0: free((void *)m_descriptor.copyright); cannam@0: cannam@0: for (unsigned int i = 0; i < m_descriptor.parameterCount; ++i) { cannam@0: const VampParameterDescriptor *desc = m_descriptor.parameters[i]; cannam@0: free((void *)desc->name); cannam@0: free((void *)desc->description); cannam@0: free((void *)desc->unit); cannam@0: } cannam@0: free((void *)m_descriptor.parameters); cannam@0: cannam@0: for (unsigned int i = 0; i < m_descriptor.programCount; ++i) { cannam@0: free((void *)m_descriptor.programs[i]); cannam@0: } cannam@0: free((void *)m_descriptor.programs); cannam@0: cannam@0: m_adapterMap.erase(&m_descriptor); cannam@0: } cannam@0: cannam@0: PluginAdapterBase * cannam@0: PluginAdapterBase::lookupAdapter(VampPluginHandle handle) cannam@0: { cannam@0: AdapterMap::const_iterator i = m_adapterMap.find(handle); cannam@0: if (i == m_adapterMap.end()) return 0; cannam@0: return i->second; cannam@0: } cannam@0: cannam@0: VampPluginHandle cannam@0: PluginAdapterBase::vampInstantiate(const VampPluginDescriptor *desc, cannam@0: float inputSampleRate) cannam@0: { cannam@0: if (m_adapterMap.find(desc) == m_adapterMap.end()) return 0; cannam@0: PluginAdapterBase *adapter = m_adapterMap[desc]; cannam@0: if (desc != &adapter->m_descriptor) return 0; cannam@0: cannam@0: Plugin *plugin = adapter->createPlugin(inputSampleRate); cannam@0: if (plugin) { cannam@0: m_adapterMap[plugin] = adapter; cannam@0: } cannam@0: cannam@0: return plugin; cannam@0: } cannam@0: cannam@0: void cannam@0: PluginAdapterBase::vampCleanup(VampPluginHandle handle) cannam@0: { cannam@0: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@0: if (!adapter) { cannam@0: delete ((Plugin *)handle); cannam@0: return; cannam@0: } cannam@0: adapter->cleanup(((Plugin *)handle)); cannam@0: } cannam@0: cannam@0: int cannam@0: PluginAdapterBase::vampInitialise(VampPluginHandle handle, cannam@0: unsigned int channels, cannam@0: unsigned int stepSize, cannam@0: unsigned int blockSize) cannam@0: { cannam@0: bool result = ((Plugin *)handle)->initialise cannam@0: (channels, stepSize, blockSize); cannam@0: return result ? 1 : 0; cannam@0: } cannam@0: cannam@0: void cannam@0: PluginAdapterBase::vampReset(VampPluginHandle handle) cannam@0: { cannam@0: ((Plugin *)handle)->reset(); cannam@0: } cannam@0: cannam@0: float cannam@0: PluginAdapterBase::vampGetParameter(VampPluginHandle handle, cannam@0: int param) cannam@0: { cannam@0: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@0: if (!adapter) return 0.0; cannam@0: Plugin::ParameterList &list = adapter->m_parameters; cannam@0: return ((Plugin *)handle)->getParameter(list[param].name); cannam@0: } cannam@0: cannam@0: void cannam@0: PluginAdapterBase::vampSetParameter(VampPluginHandle handle, cannam@0: int param, float value) cannam@0: { cannam@0: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@0: if (!adapter) return; cannam@0: Plugin::ParameterList &list = adapter->m_parameters; cannam@0: ((Plugin *)handle)->setParameter(list[param].name, value); cannam@0: } cannam@0: cannam@0: unsigned int cannam@0: PluginAdapterBase::vampGetCurrentProgram(VampPluginHandle handle) cannam@0: { cannam@0: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@0: if (!adapter) return 0; cannam@0: Plugin::ProgramList &list = adapter->m_programs; cannam@0: std::string program = ((Plugin *)handle)->getCurrentProgram(); cannam@0: for (unsigned int i = 0; i < list.size(); ++i) { cannam@0: if (list[i] == program) return i; cannam@0: } cannam@0: return 0; cannam@0: } cannam@0: cannam@0: void cannam@0: PluginAdapterBase::vampSelectProgram(VampPluginHandle handle, cannam@0: unsigned int program) cannam@0: { cannam@0: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@0: if (!adapter) return; cannam@0: Plugin::ProgramList &list = adapter->m_programs; cannam@0: ((Plugin *)handle)->selectProgram(list[program]); cannam@0: } cannam@0: cannam@0: unsigned int cannam@0: PluginAdapterBase::vampGetPreferredStepSize(VampPluginHandle handle) cannam@0: { cannam@0: return ((Plugin *)handle)->getPreferredStepSize(); cannam@0: } cannam@0: cannam@0: unsigned int cannam@0: PluginAdapterBase::vampGetPreferredBlockSize(VampPluginHandle handle) cannam@0: { cannam@0: return ((Plugin *)handle)->getPreferredBlockSize(); cannam@0: } cannam@0: cannam@0: unsigned int cannam@0: PluginAdapterBase::vampGetMinChannelCount(VampPluginHandle handle) cannam@0: { cannam@0: return ((Plugin *)handle)->getMinChannelCount(); cannam@0: } cannam@0: cannam@0: unsigned int cannam@0: PluginAdapterBase::vampGetMaxChannelCount(VampPluginHandle handle) cannam@0: { cannam@0: return ((Plugin *)handle)->getMaxChannelCount(); cannam@0: } cannam@0: cannam@0: unsigned int cannam@0: PluginAdapterBase::vampGetOutputCount(VampPluginHandle handle) cannam@0: { cannam@0: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@0: if (!adapter) return 0; cannam@0: return adapter->getOutputCount((Plugin *)handle); cannam@0: } cannam@0: cannam@0: VampOutputDescriptor * cannam@0: PluginAdapterBase::vampGetOutputDescriptor(VampPluginHandle handle, cannam@0: unsigned int i) cannam@0: { cannam@0: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@0: if (!adapter) return 0; cannam@0: return adapter->getOutputDescriptor((Plugin *)handle, i); cannam@0: } cannam@0: cannam@0: void cannam@0: PluginAdapterBase::vampReleaseOutputDescriptor(VampOutputDescriptor *desc) cannam@0: { cannam@0: if (desc->name) free((void *)desc->name); cannam@0: if (desc->description) free((void *)desc->description); cannam@0: if (desc->unit) free((void *)desc->unit); cannam@0: for (unsigned int i = 0; i < desc->valueCount; ++i) { cannam@0: free((void *)desc->valueNames[i]); cannam@0: } cannam@0: if (desc->valueNames) free((void *)desc->valueNames); cannam@0: free((void *)desc); cannam@0: } cannam@0: cannam@0: VampFeatureList ** cannam@0: PluginAdapterBase::vampProcess(VampPluginHandle handle, cannam@0: float **inputBuffers, cannam@0: int sec, cannam@0: int nsec) cannam@0: { cannam@0: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@0: if (!adapter) return 0; cannam@0: return adapter->process((Plugin *)handle, cannam@0: inputBuffers, sec, nsec); cannam@0: } cannam@0: cannam@0: VampFeatureList ** cannam@0: PluginAdapterBase::vampGetRemainingFeatures(VampPluginHandle handle) cannam@0: { cannam@0: PluginAdapterBase *adapter = lookupAdapter(handle); cannam@0: if (!adapter) return 0; cannam@0: return adapter->getRemainingFeatures((Plugin *)handle); cannam@0: } cannam@0: cannam@0: void cannam@0: PluginAdapterBase::vampReleaseFeatureSet(VampFeatureList **fs) cannam@0: { cannam@0: if (!fs) return; cannam@0: for (unsigned int i = 0; fs[i]; ++i) { cannam@0: for (unsigned int j = 0; j < fs[i]->featureCount; ++j) { cannam@0: VampFeature *feature = &fs[i]->features[j]; cannam@0: if (feature->values) free((void *)feature->values); cannam@0: if (feature->label) free((void *)feature->label); cannam@0: free((void *)feature); cannam@0: } cannam@0: if (fs[i]->features) free((void *)fs[i]->features); cannam@0: free((void *)fs[i]); cannam@0: } cannam@0: free((void *)fs); cannam@0: } cannam@0: cannam@0: void cannam@0: PluginAdapterBase::cleanup(Plugin *plugin) cannam@0: { cannam@0: if (m_pluginOutputs.find(plugin) != m_pluginOutputs.end()) { cannam@0: delete m_pluginOutputs[plugin]; cannam@0: m_pluginOutputs.erase(plugin); cannam@0: } cannam@0: m_adapterMap.erase(plugin); cannam@0: delete ((Plugin *)plugin); cannam@0: } cannam@0: cannam@0: void cannam@0: PluginAdapterBase::checkOutputMap(Plugin *plugin) cannam@0: { cannam@0: if (!m_pluginOutputs[plugin]) { cannam@0: m_pluginOutputs[plugin] = new Plugin::OutputList cannam@0: (plugin->getOutputDescriptors()); cannam@0: } cannam@0: } cannam@0: cannam@0: unsigned int cannam@0: PluginAdapterBase::getOutputCount(Plugin *plugin) cannam@0: { cannam@0: checkOutputMap(plugin); cannam@0: return m_pluginOutputs[plugin]->size(); cannam@0: } cannam@0: cannam@0: VampOutputDescriptor * cannam@0: PluginAdapterBase::getOutputDescriptor(Plugin *plugin, cannam@0: unsigned int i) cannam@0: { cannam@0: checkOutputMap(plugin); cannam@0: Plugin::OutputDescriptor &od = cannam@0: (*m_pluginOutputs[plugin])[i]; cannam@0: cannam@0: VampOutputDescriptor *desc = (VampOutputDescriptor *) cannam@0: malloc(sizeof(VampOutputDescriptor)); cannam@0: cannam@0: desc->name = strdup(od.name.c_str()); cannam@0: desc->description = strdup(od.description.c_str()); cannam@0: desc->unit = strdup(od.unit.c_str()); cannam@0: desc->hasFixedValueCount = od.hasFixedValueCount; cannam@0: desc->valueCount = od.valueCount; cannam@0: cannam@0: desc->valueNames = (const char **) cannam@0: malloc(od.valueCount * sizeof(const char *)); cannam@0: cannam@0: for (unsigned int i = 0; i < od.valueCount; ++i) { cannam@0: if (i < od.valueNames.size()) { cannam@0: desc->valueNames[i] = strdup(od.valueNames[i].c_str()); cannam@0: } else { cannam@0: desc->valueNames[i] = 0; cannam@0: } cannam@0: } cannam@0: cannam@0: desc->hasKnownExtents = od.hasKnownExtents; cannam@0: desc->minValue = od.minValue; cannam@0: desc->maxValue = od.maxValue; cannam@0: desc->isQuantized = od.isQuantized; cannam@0: desc->quantizeStep = od.quantizeStep; cannam@0: cannam@0: switch (od.sampleType) { cannam@0: case Plugin::OutputDescriptor::OneSamplePerStep: cannam@0: desc->sampleType = vampOneSamplePerStep; break; cannam@0: case Plugin::OutputDescriptor::FixedSampleRate: cannam@0: desc->sampleType = vampFixedSampleRate; break; cannam@0: case Plugin::OutputDescriptor::VariableSampleRate: cannam@0: desc->sampleType = vampVariableSampleRate; break; cannam@0: } cannam@0: cannam@0: desc->sampleRate = od.sampleRate; cannam@0: cannam@0: return desc; cannam@0: } cannam@0: cannam@0: VampFeatureList ** cannam@0: PluginAdapterBase::process(Plugin *plugin, cannam@0: float **inputBuffers, cannam@0: int sec, int nsec) cannam@0: { cannam@0: RealTime rt(sec, nsec); cannam@0: return convertFeatures(plugin->process(inputBuffers, rt)); cannam@0: } cannam@0: cannam@0: VampFeatureList ** cannam@0: PluginAdapterBase::getRemainingFeatures(Plugin *plugin) cannam@0: { cannam@0: return convertFeatures(plugin->getRemainingFeatures()); cannam@0: } cannam@0: cannam@0: VampFeatureList ** cannam@0: PluginAdapterBase::convertFeatures(const Plugin::FeatureSet &features) cannam@0: { cannam@0: unsigned int n = 0; cannam@0: if (features.begin() != features.end()) { cannam@0: Plugin::FeatureSet::const_iterator i = features.end(); cannam@0: --i; cannam@0: n = i->first + 1; cannam@0: } cannam@0: cannam@0: if (!n) return 0; cannam@0: cannam@0: VampFeatureList **fs = (VampFeatureList **) cannam@0: malloc((n + 1) * sizeof(VampFeatureList *)); cannam@0: cannam@0: for (unsigned int i = 0; i < n; ++i) { cannam@0: fs[i] = (VampFeatureList *)malloc(sizeof(VampFeatureList)); cannam@0: if (features.find(i) == features.end()) { cannam@0: fs[i]->featureCount = 0; cannam@0: fs[i]->features = 0; cannam@0: } else { cannam@0: Plugin::FeatureSet::const_iterator fi = cannam@0: features.find(i); cannam@0: const Plugin::FeatureList &fl = fi->second; cannam@0: fs[i]->featureCount = fl.size(); cannam@0: fs[i]->features = (VampFeature *)malloc(fl.size() * cannam@0: sizeof(VampFeature)); cannam@0: for (unsigned int j = 0; j < fl.size(); ++j) { cannam@0: fs[i]->features[j].hasTimestamp = fl[j].hasTimestamp; cannam@0: fs[i]->features[j].sec = fl[j].timestamp.sec; cannam@0: fs[i]->features[j].nsec = fl[j].timestamp.nsec; cannam@0: fs[i]->features[j].valueCount = fl[j].values.size(); cannam@0: fs[i]->features[j].values = (float *)malloc cannam@0: (fs[i]->features[j].valueCount * sizeof(float)); cannam@0: for (unsigned int k = 0; k < fs[i]->features[j].valueCount; ++k) { cannam@0: fs[i]->features[j].values[k] = fl[j].values[k]; cannam@0: } cannam@0: fs[i]->features[j].label = strdup(fl[j].label.c_str()); cannam@0: } cannam@0: } cannam@0: } cannam@0: cannam@0: fs[n] = 0; cannam@0: cannam@0: return fs; cannam@0: } cannam@0: cannam@0: PluginAdapterBase::AdapterMap cannam@0: PluginAdapterBase::m_adapterMap; cannam@0: cannam@0: } cannam@0: