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@331: #include "RealTimeEffectModelTransformer.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@383: #include "TransformFactory.h" Chris@383: Chris@320: #include Chris@320: Chris@383: RealTimeEffectModelTransformer::RealTimeEffectModelTransformer(Input in, Chris@383: const Transform &transform) : Chris@383: ModelTransformer(in, transform), Chris@383: m_plugin(0) Chris@320: { Chris@383: m_units = TransformFactory::getInstance()->getTransformUnits Chris@383: (transform.getIdentifier()); Chris@383: m_outputNo = Chris@383: (transform.getOutput() == "A") ? -1 : transform.getOutput().toInt(); Chris@383: Chris@383: QString pluginId = transform.getPluginIdentifier(); Chris@383: Chris@383: if (!m_transform.getBlockSize()) m_transform.setBlockSize(1024); Chris@320: Chris@331: // std::cerr << "RealTimeEffectModelTransformer::RealTimeEffectModelTransformer: 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@331: std::cerr << "RealTimeEffectModelTransformer: No factory available for plugin id \"" Chris@320: << pluginId.toStdString() << "\"" << std::endl; Chris@320: return; Chris@320: } Chris@320: Chris@383: DenseTimeValueModel *input = getConformingInput(); Chris@320: if (!input) return; Chris@320: Chris@320: m_plugin = factory->instantiatePlugin(pluginId, 0, 0, Chris@383: input->getSampleRate(), Chris@383: m_transform.getBlockSize(), Chris@320: input->getChannelCount()); Chris@320: Chris@320: if (!m_plugin) { Chris@331: std::cerr << "RealTimeEffectModelTransformer: Failed to instantiate plugin \"" Chris@320: << pluginId.toStdString() << "\"" << std::endl; Chris@320: return; Chris@320: } Chris@320: Chris@383: TransformFactory::getInstance()->setPluginParameters(m_transform, m_plugin); Chris@320: Chris@320: if (m_outputNo >= 0 && Chris@320: m_outputNo >= int(m_plugin->getControlOutputCount())) { Chris@331: std::cerr << "RealTimeEffectModelTransformer: 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@383: (input->getSampleRate(), m_transform.getBlockSize(), 0.0, 0.0, false); Chris@320: Chris@383: if (m_units != "") model->setScaleUnits(m_units); Chris@320: Chris@320: m_output = model; Chris@320: } Chris@320: } Chris@320: Chris@331: RealTimeEffectModelTransformer::~RealTimeEffectModelTransformer() Chris@320: { Chris@320: delete m_plugin; Chris@320: } Chris@320: Chris@320: DenseTimeValueModel * Chris@383: RealTimeEffectModelTransformer::getConformingInput() Chris@320: { Chris@320: DenseTimeValueModel *dtvm = Chris@320: dynamic_cast(getInputModel()); Chris@320: if (!dtvm) { Chris@383: std::cerr << "RealTimeEffectModelTransformer::getConformingInput: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl; Chris@320: } Chris@320: return dtvm; Chris@320: } Chris@320: Chris@320: void Chris@331: RealTimeEffectModelTransformer::run() Chris@320: { Chris@383: DenseTimeValueModel *input = getConformingInput(); Chris@320: if (!input) return; Chris@320: Chris@320: while (!input->isReady()) { Chris@320: if (dynamic_cast(input)) break; // no need to wait Chris@331: std::cerr << "RealTimeEffectModelTransformer::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@383: if (!wwfm && m_input.getChannel() != -1) channelCount = 1; Chris@320: Chris@320: long blockSize = m_plugin->getBufferSize(); Chris@320: Chris@320: float **inbufs = m_plugin->getAudioInputBuffers(); Chris@320: Chris@383: long startFrame = m_input.getModel()->getStartFrame(); Chris@383: long endFrame = m_input.getModel()->getEndFrame(); Chris@320: Chris@383: RealTime contextStartRT = m_transform.getStartTime(); Chris@383: RealTime contextDurationRT = m_transform.getDuration(); Chris@383: Chris@383: long contextStart = Chris@383: RealTime::realTime2Frame(contextStartRT, sampleRate); Chris@383: Chris@383: long contextDuration = Chris@383: RealTime::realTime2Frame(contextDurationRT, sampleRate); 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@383: (m_input.getChannel(), 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@383: got = input->getData(0, channelCount - 1, Chris@383: blockFrame, blockSize, Chris@383: inbufs); Chris@383: while (got < blockSize) { Chris@383: for (size_t ch = 0; ch < channelCount; ++ch) { Chris@383: inbufs[ch][got] = 0.0; Chris@320: } Chris@383: ++got; 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: