Chris@0: Chris@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: Sonic Visualiser Chris@0: An audio file viewer and annotation editor. Chris@0: Centre for Digital Music, Queen Mary, University of London. Chris@0: This file copyright 2006 Chris Cannam. Chris@0: Chris@0: This program is free software; you can redistribute it and/or Chris@0: modify it under the terms of the GNU General Public License as Chris@0: published by the Free Software Foundation; either version 2 of the Chris@0: License, or (at your option) any later version. See the file Chris@0: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include "RealTimePluginTransform.h" Chris@0: Chris@0: #include "plugin/RealTimePluginFactory.h" Chris@0: #include "plugin/RealTimePluginInstance.h" Chris@0: #include "plugin/PluginXml.h" Chris@0: Chris@1: #include "data/model/Model.h" Chris@1: #include "data/model/SparseTimeValueModel.h" Chris@1: #include "data/model/DenseTimeValueModel.h" Chris@0: Chris@0: #include Chris@0: Chris@0: RealTimePluginTransform::RealTimePluginTransform(Model *inputModel, Chris@0: QString pluginId, Chris@0: int channel, Chris@0: QString configurationXml, Chris@0: QString units, Chris@26: int output, Chris@26: size_t blockSize) : Chris@0: Transform(inputModel), Chris@0: m_plugin(0), Chris@0: m_channel(channel), Chris@26: m_outputNo(output), Chris@26: m_blockSize(blockSize) Chris@0: { Chris@26: if (!m_blockSize) m_blockSize = 1024; Chris@26: Chris@0: std::cerr << "RealTimePluginTransform::RealTimePluginTransform: plugin " << pluginId.toStdString() << ", output " << output << std::endl; Chris@0: Chris@0: RealTimePluginFactory *factory = Chris@0: RealTimePluginFactory::instanceFor(pluginId); Chris@0: Chris@0: if (!factory) { Chris@0: std::cerr << "RealTimePluginTransform: No factory available for plugin id \"" Chris@0: << pluginId.toStdString() << "\"" << std::endl; Chris@0: return; Chris@0: } Chris@0: Chris@0: DenseTimeValueModel *input = getInput(); Chris@0: if (!input) return; Chris@0: Chris@0: m_plugin = factory->instantiatePlugin(pluginId, 0, 0, m_input->getSampleRate(), Chris@26: m_blockSize, Chris@0: input->getChannelCount()); Chris@0: Chris@0: if (!m_plugin) { Chris@0: std::cerr << "RealTimePluginTransform: Failed to instantiate plugin \"" Chris@0: << pluginId.toStdString() << "\"" << std::endl; Chris@0: return; Chris@0: } Chris@0: Chris@0: if (configurationXml != "") { Chris@0: PluginXml(m_plugin).setParametersFromXml(configurationXml); Chris@0: } Chris@0: Chris@0: if (m_outputNo >= m_plugin->getControlOutputCount()) { Chris@0: std::cerr << "RealTimePluginTransform: Plugin has fewer than desired " << m_outputNo << " control outputs" << std::endl; Chris@0: return; Chris@0: } Chris@0: Chris@0: SparseTimeValueModel *model = new SparseTimeValueModel Chris@26: (input->getSampleRate(), m_blockSize, 0.0, 0.0, false); Chris@0: Chris@0: if (units != "") model->setScaleUnits(units); Chris@0: Chris@0: m_output = model; Chris@0: } Chris@0: Chris@0: RealTimePluginTransform::~RealTimePluginTransform() Chris@0: { Chris@0: delete m_plugin; Chris@0: } Chris@0: Chris@0: DenseTimeValueModel * Chris@0: RealTimePluginTransform::getInput() Chris@0: { Chris@0: DenseTimeValueModel *dtvm = Chris@0: dynamic_cast(getInputModel()); Chris@0: if (!dtvm) { Chris@0: std::cerr << "RealTimePluginTransform::getInput: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl; Chris@0: } Chris@0: return dtvm; Chris@0: } Chris@0: Chris@0: void Chris@0: RealTimePluginTransform::run() Chris@0: { Chris@0: DenseTimeValueModel *input = getInput(); Chris@0: if (!input) return; Chris@0: Chris@0: SparseTimeValueModel *model = dynamic_cast(m_output); Chris@0: if (!model) return; Chris@0: Chris@0: if (m_outputNo >= m_plugin->getControlOutputCount()) return; Chris@0: Chris@0: size_t sampleRate = input->getSampleRate(); Chris@0: int channelCount = input->getChannelCount(); Chris@0: if (m_channel != -1) channelCount = 1; Chris@0: Chris@0: size_t blockSize = m_plugin->getBufferSize(); Chris@0: Chris@0: float **buffers = m_plugin->getAudioInputBuffers(); Chris@0: Chris@0: size_t startFrame = m_input->getStartFrame(); Chris@0: size_t endFrame = m_input->getEndFrame(); Chris@0: size_t blockFrame = startFrame; Chris@0: Chris@0: size_t prevCompletion = 0; Chris@0: Chris@0: int i = 0; Chris@0: Chris@0: while (blockFrame < endFrame) { Chris@0: Chris@0: size_t completion = Chris@0: (((blockFrame - startFrame) / blockSize) * 99) / Chris@0: ( (endFrame - startFrame) / blockSize); Chris@0: Chris@0: size_t got = 0; Chris@0: Chris@0: if (channelCount == 1) { Chris@0: got = input->getValues Chris@0: (m_channel, blockFrame, blockFrame + blockSize, buffers[0]); Chris@0: while (got < blockSize) { Chris@0: buffers[0][got++] = 0.0; Chris@0: } Chris@0: if (m_channel == -1 && channelCount > 1) { Chris@0: // use mean instead of sum, as plugin input Chris@0: for (size_t i = 0; i < got; ++i) { Chris@0: buffers[0][i] /= channelCount; Chris@0: } Chris@0: } Chris@0: } else { Chris@0: for (size_t ch = 0; ch < channelCount; ++ch) { Chris@0: got = input->getValues Chris@0: (ch, blockFrame, blockFrame + blockSize, buffers[ch]); Chris@0: while (got < blockSize) { Chris@0: buffers[ch][got++] = 0.0; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: m_plugin->run(Vamp::RealTime::frame2RealTime(blockFrame, sampleRate)); Chris@0: Chris@0: float value = m_plugin->getControlOutputValue(m_outputNo); Chris@0: Chris@0: model->addPoint(SparseTimeValueModel::Point Chris@0: (blockFrame - m_plugin->getLatency(), value, "")); Chris@0: Chris@0: if (blockFrame == startFrame || completion > prevCompletion) { Chris@0: model->setCompletion(completion); Chris@0: prevCompletion = completion; Chris@0: } Chris@0: Chris@0: blockFrame += blockSize; Chris@0: } Chris@0: Chris@0: model->setCompletion(100); Chris@0: } Chris@0: