annotate transform/RealTimePluginTransform.cpp @ 60:3086ff194ea0

* More structural work on feature extraction plugin C <-> C++ adapter * Allow use of LADSPA/DSSI plugins with control outputs as feature extraction plugins (DSSI with MIDI output still to come) * Reorder labels on spectrogram status box * Minor tweaks in doc etc.
author Chris Cannam
date Mon, 27 Mar 2006 15:03:02 +0000
parents
children 749b5521e082
rev   line source
Chris@60 1
Chris@60 2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@60 3
Chris@60 4 /*
Chris@60 5 Sonic Visualiser
Chris@60 6 An audio file viewer and annotation editor.
Chris@60 7 Centre for Digital Music, Queen Mary, University of London.
Chris@60 8 This file copyright 2006 Chris Cannam.
Chris@60 9
Chris@60 10 This program is free software; you can redistribute it and/or
Chris@60 11 modify it under the terms of the GNU General Public License as
Chris@60 12 published by the Free Software Foundation; either version 2 of the
Chris@60 13 License, or (at your option) any later version. See the file
Chris@60 14 COPYING included with this distribution for more information.
Chris@60 15 */
Chris@60 16
Chris@60 17 #include "RealTimePluginTransform.h"
Chris@60 18
Chris@60 19 #include "plugin/RealTimePluginFactory.h"
Chris@60 20 #include "plugin/RealTimePluginInstance.h"
Chris@60 21
Chris@60 22 #include "base/Model.h"
Chris@60 23 #include "model/SparseTimeValueModel.h"
Chris@60 24 #include "model/DenseTimeValueModel.h"
Chris@60 25
Chris@60 26 #include <iostream>
Chris@60 27
Chris@60 28 RealTimePluginTransform::RealTimePluginTransform(Model *inputModel,
Chris@60 29 QString pluginId,
Chris@60 30 QString configurationXml,
Chris@60 31 int output) :
Chris@60 32 Transform(inputModel),
Chris@60 33 m_plugin(0),
Chris@60 34 m_outputNo(output)
Chris@60 35 {
Chris@60 36 std::cerr << "RealTimePluginTransform::RealTimePluginTransform: plugin " << pluginId.toStdString() << ", output " << output << std::endl;
Chris@60 37
Chris@60 38 RealTimePluginFactory *factory =
Chris@60 39 RealTimePluginFactory::instanceFor(pluginId);
Chris@60 40
Chris@60 41 if (!factory) {
Chris@60 42 std::cerr << "RealTimePluginTransform: No factory available for plugin id \""
Chris@60 43 << pluginId.toStdString() << "\"" << std::endl;
Chris@60 44 return;
Chris@60 45 }
Chris@60 46
Chris@60 47 DenseTimeValueModel *input = getInput();
Chris@60 48 if (!input) return;
Chris@60 49
Chris@60 50 m_plugin = factory->instantiatePlugin(pluginId, 0, 0, m_input->getSampleRate(),
Chris@60 51 1024, //!!! wants to be configurable
Chris@60 52 input->getChannelCount());
Chris@60 53
Chris@60 54 if (!m_plugin) {
Chris@60 55 std::cerr << "RealTimePluginTransform: Failed to instantiate plugin \""
Chris@60 56 << pluginId.toStdString() << "\"" << std::endl;
Chris@60 57 return;
Chris@60 58 }
Chris@60 59
Chris@60 60 if (configurationXml != "") {
Chris@60 61 m_plugin->setParametersFromXml(configurationXml);
Chris@60 62 }
Chris@60 63
Chris@60 64 if (m_outputNo >= m_plugin->getControlOutputCount()) {
Chris@60 65 std::cerr << "RealTimePluginTransform: Plugin has fewer than desired " << m_outputNo << " control outputs" << std::endl;
Chris@60 66 return;
Chris@60 67 }
Chris@60 68
Chris@60 69 m_output = new SparseTimeValueModel(input->getSampleRate(),
Chris@60 70 1024, //!!!
Chris@60 71 0.0, 0.0, false);
Chris@60 72 }
Chris@60 73
Chris@60 74 RealTimePluginTransform::~RealTimePluginTransform()
Chris@60 75 {
Chris@60 76 delete m_plugin;
Chris@60 77 }
Chris@60 78
Chris@60 79 DenseTimeValueModel *
Chris@60 80 RealTimePluginTransform::getInput()
Chris@60 81 {
Chris@60 82 DenseTimeValueModel *dtvm =
Chris@60 83 dynamic_cast<DenseTimeValueModel *>(getInputModel());
Chris@60 84 if (!dtvm) {
Chris@60 85 std::cerr << "RealTimePluginTransform::getInput: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl;
Chris@60 86 }
Chris@60 87 return dtvm;
Chris@60 88 }
Chris@60 89
Chris@60 90 void
Chris@60 91 RealTimePluginTransform::run()
Chris@60 92 {
Chris@60 93 DenseTimeValueModel *input = getInput();
Chris@60 94 if (!input) return;
Chris@60 95
Chris@60 96 SparseTimeValueModel *model = dynamic_cast<SparseTimeValueModel *>(m_output);
Chris@60 97 if (!model) return;
Chris@60 98
Chris@60 99 if (m_outputNo >= m_plugin->getControlOutputCount()) return;
Chris@60 100
Chris@60 101 size_t sampleRate = input->getSampleRate();
Chris@60 102 int channelCount = input->getChannelCount();
Chris@60 103 size_t blockSize = m_plugin->getBufferSize();
Chris@60 104
Chris@60 105 float **buffers = m_plugin->getAudioInputBuffers();
Chris@60 106
Chris@60 107 size_t startFrame = m_input->getStartFrame();
Chris@60 108 size_t endFrame = m_input->getEndFrame();
Chris@60 109 size_t blockFrame = startFrame;
Chris@60 110
Chris@60 111 size_t prevCompletion = 0;
Chris@60 112
Chris@60 113 int i = 0;
Chris@60 114
Chris@60 115 while (blockFrame < endFrame) {
Chris@60 116
Chris@60 117 std::cout << "RealTimePluginTransform::run: blockFrame "
Chris@60 118 << blockFrame;
Chris@60 119
Chris@60 120 size_t completion =
Chris@60 121 (((blockFrame - startFrame) / blockSize) * 99) /
Chris@60 122 ( (endFrame - startFrame) / blockSize);
Chris@60 123
Chris@60 124 size_t got = 0;
Chris@60 125
Chris@60 126 if (channelCount == 1) {
Chris@60 127 got = input->getValues
Chris@60 128 (-1, blockFrame, blockFrame + blockSize, buffers[0]);
Chris@60 129 while (got < blockSize) {
Chris@60 130 buffers[0][got++] = 0.0;
Chris@60 131 }
Chris@60 132 } else {
Chris@60 133 for (size_t ch = 0; ch < channelCount; ++ch) {
Chris@60 134 got = input->getValues
Chris@60 135 (ch, blockFrame, blockFrame + blockSize, buffers[ch]);
Chris@60 136 while (got < blockSize) {
Chris@60 137 buffers[ch][got++] = 0.0;
Chris@60 138 }
Chris@60 139 }
Chris@60 140 }
Chris@60 141
Chris@60 142 m_plugin->run(RealTime::frame2RealTime(blockFrame, sampleRate));
Chris@60 143
Chris@60 144 float value = m_plugin->getControlOutputValue(m_outputNo);
Chris@60 145
Chris@60 146 std::cout << " value " << value << std::endl;
Chris@60 147
Chris@60 148 model->addPoint(SparseTimeValueModel::Point(blockFrame, value, ""));
Chris@60 149
Chris@60 150 if (blockFrame == startFrame || completion > prevCompletion) {
Chris@60 151 model->setCompletion(completion);
Chris@60 152 prevCompletion = completion;
Chris@60 153 }
Chris@60 154
Chris@60 155 blockFrame += blockSize;
Chris@60 156 }
Chris@60 157
Chris@60 158 model->setCompletion(100);
Chris@60 159 }
Chris@60 160