Chris@320: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@320: Chris@320: /* Chris@320: Sonic Visualiser Chris@320: An audio file viewer and annotation editor. Chris@320: Centre for Digital Music, Queen Mary, University of London. Chris@320: This file copyright 2006 Chris Cannam and QMUL. Chris@320: Chris@320: This program is free software; you can redistribute it and/or Chris@320: modify it under the terms of the GNU General Public License as Chris@320: published by the Free Software Foundation; either version 2 of the Chris@320: License, or (at your option) any later version. See the file Chris@320: COPYING included with this distribution for more information. Chris@320: */ Chris@320: Chris@320: #include "RealTimePluginTransform.h" Chris@320: Chris@320: #include "plugin/RealTimePluginFactory.h" Chris@320: #include "plugin/RealTimePluginInstance.h" Chris@320: #include "plugin/PluginXml.h" Chris@320: Chris@320: #include "data/model/Model.h" Chris@320: #include "data/model/SparseTimeValueModel.h" Chris@320: #include "data/model/DenseTimeValueModel.h" Chris@320: #include "data/model/WritableWaveFileModel.h" Chris@320: #include "data/model/WaveFileModel.h" Chris@320: Chris@320: #include Chris@320: Chris@320: RealTimePluginTransform::RealTimePluginTransform(Model *inputModel, Chris@320: QString pluginId, Chris@320: const ExecutionContext &context, Chris@320: QString configurationXml, Chris@320: QString units, Chris@320: int output) : Chris@320: PluginTransform(inputModel, context), Chris@320: m_pluginId(pluginId), Chris@320: m_configurationXml(configurationXml), Chris@320: m_units(units), Chris@320: m_plugin(0), Chris@320: m_outputNo(output) Chris@320: { Chris@320: if (!m_context.blockSize) m_context.blockSize = 1024; Chris@320: Chris@320: // std::cerr << "RealTimePluginTransform::RealTimePluginTransform: plugin " << pluginId.toStdString() << ", output " << output << std::endl; Chris@320: Chris@320: RealTimePluginFactory *factory = Chris@320: RealTimePluginFactory::instanceFor(pluginId); Chris@320: Chris@320: if (!factory) { Chris@320: std::cerr << "RealTimePluginTransform: No factory available for plugin id \"" Chris@320: << pluginId.toStdString() << "\"" << std::endl; Chris@320: return; Chris@320: } Chris@320: Chris@320: DenseTimeValueModel *input = getInput(); Chris@320: if (!input) return; Chris@320: Chris@320: m_plugin = factory->instantiatePlugin(pluginId, 0, 0, Chris@320: m_input->getSampleRate(), Chris@320: m_context.blockSize, Chris@320: input->getChannelCount()); Chris@320: Chris@320: if (!m_plugin) { Chris@320: std::cerr << "RealTimePluginTransform: Failed to instantiate plugin \"" Chris@320: << pluginId.toStdString() << "\"" << std::endl; Chris@320: return; Chris@320: } Chris@320: Chris@320: if (configurationXml != "") { Chris@320: PluginXml(m_plugin).setParametersFromXml(configurationXml); Chris@320: } Chris@320: Chris@320: if (m_outputNo >= 0 && Chris@320: m_outputNo >= int(m_plugin->getControlOutputCount())) { Chris@320: std::cerr << "RealTimePluginTransform: Plugin has fewer than desired " << m_outputNo << " control outputs" << std::endl; Chris@320: return; Chris@320: } Chris@320: Chris@320: if (m_outputNo == -1) { Chris@320: Chris@320: size_t outputChannels = m_plugin->getAudioOutputCount(); Chris@320: if (outputChannels > input->getChannelCount()) { Chris@320: outputChannels = input->getChannelCount(); Chris@320: } Chris@320: Chris@320: WritableWaveFileModel *model = new WritableWaveFileModel Chris@320: (input->getSampleRate(), outputChannels); Chris@320: Chris@320: m_output = model; Chris@320: Chris@320: } else { Chris@320: Chris@320: SparseTimeValueModel *model = new SparseTimeValueModel Chris@320: (input->getSampleRate(), m_context.blockSize, 0.0, 0.0, false); Chris@320: Chris@320: if (units != "") model->setScaleUnits(units); Chris@320: Chris@320: m_output = model; Chris@320: } Chris@320: } Chris@320: Chris@320: RealTimePluginTransform::~RealTimePluginTransform() Chris@320: { Chris@320: delete m_plugin; Chris@320: } Chris@320: Chris@320: DenseTimeValueModel * Chris@320: RealTimePluginTransform::getInput() Chris@320: { Chris@320: DenseTimeValueModel *dtvm = Chris@320: dynamic_cast(getInputModel()); Chris@320: if (!dtvm) { Chris@320: std::cerr << "RealTimePluginTransform::getInput: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl; Chris@320: } Chris@320: return dtvm; Chris@320: } Chris@320: Chris@320: void Chris@320: RealTimePluginTransform::run() Chris@320: { Chris@320: DenseTimeValueModel *input = getInput(); Chris@320: if (!input) return; Chris@320: Chris@320: while (!input->isReady()) { Chris@320: if (dynamic_cast(input)) break; // no need to wait Chris@320: std::cerr << "RealTimePluginTransform::run: Waiting for input model to be ready..." << std::endl; Chris@320: sleep(1); Chris@320: } Chris@320: Chris@320: SparseTimeValueModel *stvm = dynamic_cast(m_output); Chris@320: WritableWaveFileModel *wwfm = dynamic_cast(m_output); Chris@320: if (!stvm && !wwfm) return; Chris@320: Chris@320: if (stvm && (m_outputNo >= int(m_plugin->getControlOutputCount()))) return; Chris@320: Chris@320: size_t sampleRate = input->getSampleRate(); Chris@320: size_t channelCount = input->getChannelCount(); Chris@320: if (!wwfm && m_context.channel != -1) channelCount = 1; Chris@320: Chris@320: long blockSize = m_plugin->getBufferSize(); Chris@320: Chris@320: float **inbufs = m_plugin->getAudioInputBuffers(); Chris@320: Chris@320: long startFrame = m_input->getStartFrame(); Chris@320: long endFrame = m_input->getEndFrame(); Chris@320: Chris@320: long contextStart = m_context.startFrame; Chris@320: long contextDuration = m_context.duration; Chris@320: Chris@320: if (contextStart == 0 || contextStart < startFrame) { Chris@320: contextStart = startFrame; Chris@320: } Chris@320: Chris@320: if (contextDuration == 0) { Chris@320: contextDuration = endFrame - contextStart; Chris@320: } Chris@320: if (contextStart + contextDuration > endFrame) { Chris@320: contextDuration = endFrame - contextStart; Chris@320: } Chris@320: Chris@320: wwfm->setStartFrame(contextStart); Chris@320: Chris@320: long blockFrame = contextStart; Chris@320: Chris@320: long prevCompletion = 0; Chris@320: Chris@320: long latency = m_plugin->getLatency(); Chris@320: Chris@320: while (blockFrame < contextStart + contextDuration + latency && Chris@320: !m_abandoned) { Chris@320: Chris@320: long completion = Chris@320: (((blockFrame - contextStart) / blockSize) * 99) / Chris@320: ((contextDuration) / blockSize); Chris@320: Chris@320: long got = 0; Chris@320: Chris@320: if (channelCount == 1) { Chris@320: if (inbufs && inbufs[0]) { Chris@320: got = input->getData Chris@320: (m_context.channel, blockFrame, blockSize, inbufs[0]); Chris@320: while (got < blockSize) { Chris@320: inbufs[0][got++] = 0.0; Chris@320: } Chris@320: } Chris@320: for (size_t ch = 1; ch < m_plugin->getAudioInputCount(); ++ch) { Chris@320: for (long i = 0; i < blockSize; ++i) { Chris@320: inbufs[ch][i] = inbufs[0][i]; Chris@320: } Chris@320: } Chris@320: } else { Chris@320: for (size_t ch = 0; ch < channelCount; ++ch) { Chris@320: if (inbufs && inbufs[ch]) { Chris@320: got = input->getData Chris@320: (ch, blockFrame, blockSize, inbufs[ch]); Chris@320: while (got < blockSize) { Chris@320: inbufs[ch][got++] = 0.0; Chris@320: } Chris@320: } Chris@320: } Chris@320: for (size_t ch = channelCount; ch < m_plugin->getAudioInputCount(); ++ch) { Chris@320: for (long i = 0; i < blockSize; ++i) { Chris@320: inbufs[ch][i] = inbufs[ch % channelCount][i]; Chris@320: } Chris@320: } Chris@320: } Chris@320: Chris@320: /* Chris@320: std::cerr << "Input for plugin: " << m_plugin->getAudioInputCount() << " channels "<< std::endl; Chris@320: Chris@320: for (size_t ch = 0; ch < m_plugin->getAudioInputCount(); ++ch) { Chris@320: std::cerr << "Input channel " << ch << std::endl; Chris@320: for (size_t i = 0; i < 100; ++i) { Chris@320: std::cerr << inbufs[ch][i] << " "; Chris@320: if (isnan(inbufs[ch][i])) { Chris@320: std::cerr << "\n\nWARNING: NaN in audio input" << std::endl; Chris@320: } Chris@320: } Chris@320: } Chris@320: */ Chris@320: Chris@320: m_plugin->run(Vamp::RealTime::frame2RealTime(blockFrame, sampleRate)); Chris@320: Chris@320: if (stvm) { Chris@320: Chris@320: float value = m_plugin->getControlOutputValue(m_outputNo); Chris@320: Chris@320: long pointFrame = blockFrame; Chris@320: if (pointFrame > latency) pointFrame -= latency; Chris@320: else pointFrame = 0; Chris@320: Chris@320: stvm->addPoint(SparseTimeValueModel::Point Chris@320: (pointFrame, value, "")); Chris@320: Chris@320: } else if (wwfm) { Chris@320: Chris@320: float **outbufs = m_plugin->getAudioOutputBuffers(); Chris@320: Chris@320: if (outbufs) { Chris@320: Chris@320: if (blockFrame >= latency) { Chris@320: long writeSize = std::min Chris@320: (blockSize, Chris@320: contextStart + contextDuration + latency - blockFrame); Chris@320: wwfm->addSamples(outbufs, writeSize); Chris@320: } else if (blockFrame + blockSize >= latency) { Chris@320: long offset = latency - blockFrame; Chris@320: long count = blockSize - offset; Chris@320: float **tmp = new float *[channelCount]; Chris@320: for (size_t c = 0; c < channelCount; ++c) { Chris@320: tmp[c] = outbufs[c] + offset; Chris@320: } Chris@320: wwfm->addSamples(tmp, count); Chris@320: delete[] tmp; Chris@320: } Chris@320: } Chris@320: } Chris@320: Chris@320: if (blockFrame == contextStart || completion > prevCompletion) { Chris@320: if (stvm) stvm->setCompletion(completion); Chris@320: if (wwfm) wwfm->setCompletion(completion); Chris@320: prevCompletion = completion; Chris@320: } Chris@320: Chris@320: blockFrame += blockSize; Chris@320: } Chris@320: Chris@320: if (m_abandoned) return; Chris@320: Chris@320: if (stvm) stvm->setCompletion(100); Chris@320: if (wwfm) wwfm->setCompletion(100); Chris@320: } Chris@320: