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@39: #include "data/model/WritableWaveFileModel.h" Chris@0: Chris@0: #include Chris@0: Chris@0: RealTimePluginTransform::RealTimePluginTransform(Model *inputModel, Chris@0: QString pluginId, Chris@27: const ExecutionContext &context, Chris@0: QString configurationXml, Chris@0: QString units, Chris@27: int output) : Chris@27: PluginTransform(inputModel, context), Chris@0: m_plugin(0), Chris@27: m_outputNo(output) Chris@0: { Chris@27: if (!m_context.blockSize) m_context.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@44: m_plugin = factory->instantiatePlugin(pluginId, 0, 0, Chris@44: m_input->getSampleRate(), Chris@27: m_context.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@34: if (m_outputNo >= 0 && 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@34: Chris@34: if (m_outputNo == -1) { Chris@34: Chris@39: WritableWaveFileModel *model = new WritableWaveFileModel Chris@39: (input->getSampleRate(), input->getChannelCount()); //!!! Chris@39: Chris@39: m_output = model; Chris@34: Chris@34: } else { Chris@0: Chris@34: SparseTimeValueModel *model = new SparseTimeValueModel Chris@34: (input->getSampleRate(), m_context.blockSize, 0.0, 0.0, false); Chris@0: Chris@34: if (units != "") model->setScaleUnits(units); Chris@0: Chris@34: m_output = model; Chris@34: } 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@39: SparseTimeValueModel *stvm = dynamic_cast(m_output); Chris@39: WritableWaveFileModel *wwfm = dynamic_cast(m_output); Chris@39: if (!stvm && !wwfm) return; Chris@0: Chris@39: if (stvm && (m_outputNo >= m_plugin->getControlOutputCount())) return; Chris@0: Chris@0: size_t sampleRate = input->getSampleRate(); Chris@0: int channelCount = input->getChannelCount(); Chris@44: if (!wwfm && m_context.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@39: size_t latency = m_plugin->getLatency(); Chris@39: 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@40: if (buffers && buffers[0]) { Chris@40: got = input->getValues Chris@40: (m_context.channel, blockFrame, blockFrame + blockSize, buffers[0]); Chris@40: while (got < blockSize) { Chris@40: buffers[0][got++] = 0.0; Chris@0: } Chris@40: if (m_context.channel == -1 && channelCount > 1) { Chris@40: // use mean instead of sum, as plugin input Chris@40: for (size_t i = 0; i < got; ++i) { Chris@40: buffers[0][i] /= channelCount; Chris@40: } Chris@40: } Chris@40: } Chris@0: } else { Chris@0: for (size_t ch = 0; ch < channelCount; ++ch) { Chris@40: if (buffers && buffers[ch]) { Chris@40: got = input->getValues Chris@40: (ch, blockFrame, blockFrame + blockSize, buffers[ch]); Chris@40: while (got < blockSize) { Chris@40: buffers[ch][got++] = 0.0; Chris@40: } Chris@40: } Chris@0: } Chris@0: } Chris@0: Chris@0: m_plugin->run(Vamp::RealTime::frame2RealTime(blockFrame, sampleRate)); Chris@0: Chris@39: if (stvm) { Chris@0: Chris@39: float value = m_plugin->getControlOutputValue(m_outputNo); Chris@39: Chris@39: size_t pointFrame = blockFrame; Chris@39: if (pointFrame > latency) pointFrame -= latency; Chris@39: else pointFrame = 0; Chris@39: Chris@39: stvm->addPoint(SparseTimeValueModel::Point Chris@39: (pointFrame, value, "")); Chris@39: Chris@39: } else if (wwfm) { Chris@39: Chris@39: float **buffers = m_plugin->getAudioOutputBuffers(); Chris@39: Chris@40: if (buffers) { Chris@40: Chris@40: //!!! This will fail if any buffers[c] is null or Chris@40: //uninitialised. The plugin instance should ensure Chris@40: //that that can't happen -- but it doesn't Chris@40: Chris@40: if (blockFrame >= latency) { Chris@40: wwfm->addSamples(buffers, blockSize); Chris@40: } else if (blockFrame + blockSize >= latency) { Chris@40: size_t offset = latency - blockFrame; Chris@40: size_t count = blockSize - offset; Chris@40: float **tmp = new float *[channelCount]; Chris@40: for (size_t c = 0; c < channelCount; ++c) { Chris@40: tmp[c] = buffers[c] + offset; Chris@40: } Chris@40: wwfm->addSamples(tmp, count); Chris@40: delete[] tmp; Chris@39: } Chris@39: } Chris@39: } Chris@0: Chris@0: if (blockFrame == startFrame || completion > prevCompletion) { Chris@39: if (stvm) stvm->setCompletion(completion); Chris@0: prevCompletion = completion; Chris@0: } Chris@0: Chris@0: blockFrame += blockSize; Chris@0: } Chris@0: Chris@39: if (stvm) stvm->setCompletion(100); Chris@39: if (wwfm) wwfm->sync(); Chris@0: } Chris@0: