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