annotate transform/RealTimePluginTransform.cpp @ 61:963e3187d920

* Fix slowness in serving FFT values to feature extraction plugin transform (failure to call resume() on FFT model) * Fix failure to update completion from time/value model
author Chris Cannam
date Tue, 17 Oct 2006 13:49:31 +0000
parents ca1e3f5657d5
children bedc7517b6e8
rev   line source
Chris@0 1
Chris@0 2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 3
Chris@0 4 /*
Chris@0 5 Sonic Visualiser
Chris@0 6 An audio file viewer and annotation editor.
Chris@0 7 Centre for Digital Music, Queen Mary, University of London.
Chris@0 8 This file copyright 2006 Chris Cannam.
Chris@0 9
Chris@0 10 This program is free software; you can redistribute it and/or
Chris@0 11 modify it under the terms of the GNU General Public License as
Chris@0 12 published by the Free Software Foundation; either version 2 of the
Chris@0 13 License, or (at your option) any later version. See the file
Chris@0 14 COPYING included with this distribution for more information.
Chris@0 15 */
Chris@0 16
Chris@0 17 #include "RealTimePluginTransform.h"
Chris@0 18
Chris@0 19 #include "plugin/RealTimePluginFactory.h"
Chris@0 20 #include "plugin/RealTimePluginInstance.h"
Chris@0 21 #include "plugin/PluginXml.h"
Chris@0 22
Chris@1 23 #include "data/model/Model.h"
Chris@1 24 #include "data/model/SparseTimeValueModel.h"
Chris@1 25 #include "data/model/DenseTimeValueModel.h"
Chris@39 26 #include "data/model/WritableWaveFileModel.h"
Chris@55 27 #include "data/model/WaveFileModel.h"
Chris@0 28
Chris@0 29 #include <iostream>
Chris@0 30
Chris@0 31 RealTimePluginTransform::RealTimePluginTransform(Model *inputModel,
Chris@0 32 QString pluginId,
Chris@27 33 const ExecutionContext &context,
Chris@0 34 QString configurationXml,
Chris@0 35 QString units,
Chris@27 36 int output) :
Chris@27 37 PluginTransform(inputModel, context),
Chris@0 38 m_plugin(0),
Chris@27 39 m_outputNo(output)
Chris@0 40 {
Chris@27 41 if (!m_context.blockSize) m_context.blockSize = 1024;
Chris@26 42
Chris@0 43 std::cerr << "RealTimePluginTransform::RealTimePluginTransform: plugin " << pluginId.toStdString() << ", output " << output << std::endl;
Chris@0 44
Chris@0 45 RealTimePluginFactory *factory =
Chris@0 46 RealTimePluginFactory::instanceFor(pluginId);
Chris@0 47
Chris@0 48 if (!factory) {
Chris@0 49 std::cerr << "RealTimePluginTransform: No factory available for plugin id \""
Chris@0 50 << pluginId.toStdString() << "\"" << std::endl;
Chris@0 51 return;
Chris@0 52 }
Chris@0 53
Chris@0 54 DenseTimeValueModel *input = getInput();
Chris@0 55 if (!input) return;
Chris@0 56
Chris@44 57 m_plugin = factory->instantiatePlugin(pluginId, 0, 0,
Chris@44 58 m_input->getSampleRate(),
Chris@27 59 m_context.blockSize,
Chris@0 60 input->getChannelCount());
Chris@0 61
Chris@0 62 if (!m_plugin) {
Chris@0 63 std::cerr << "RealTimePluginTransform: Failed to instantiate plugin \""
Chris@0 64 << pluginId.toStdString() << "\"" << std::endl;
Chris@0 65 return;
Chris@0 66 }
Chris@0 67
Chris@0 68 if (configurationXml != "") {
Chris@0 69 PluginXml(m_plugin).setParametersFromXml(configurationXml);
Chris@0 70 }
Chris@0 71
Chris@34 72 if (m_outputNo >= 0 && m_outputNo >= m_plugin->getControlOutputCount()) {
Chris@0 73 std::cerr << "RealTimePluginTransform: Plugin has fewer than desired " << m_outputNo << " control outputs" << std::endl;
Chris@0 74 return;
Chris@0 75 }
Chris@34 76
Chris@34 77 if (m_outputNo == -1) {
Chris@34 78
Chris@52 79 size_t outputChannels = m_plugin->getAudioOutputCount();
Chris@52 80 if (outputChannels > input->getChannelCount()) {
Chris@52 81 outputChannels = input->getChannelCount();
Chris@52 82 }
Chris@52 83
Chris@39 84 WritableWaveFileModel *model = new WritableWaveFileModel
Chris@52 85 (input->getSampleRate(), outputChannels);
Chris@39 86
Chris@39 87 m_output = model;
Chris@34 88
Chris@34 89 } else {
Chris@0 90
Chris@34 91 SparseTimeValueModel *model = new SparseTimeValueModel
Chris@34 92 (input->getSampleRate(), m_context.blockSize, 0.0, 0.0, false);
Chris@0 93
Chris@34 94 if (units != "") model->setScaleUnits(units);
Chris@0 95
Chris@34 96 m_output = model;
Chris@34 97 }
Chris@0 98 }
Chris@0 99
Chris@0 100 RealTimePluginTransform::~RealTimePluginTransform()
Chris@0 101 {
Chris@0 102 delete m_plugin;
Chris@0 103 }
Chris@0 104
Chris@0 105 DenseTimeValueModel *
Chris@0 106 RealTimePluginTransform::getInput()
Chris@0 107 {
Chris@0 108 DenseTimeValueModel *dtvm =
Chris@0 109 dynamic_cast<DenseTimeValueModel *>(getInputModel());
Chris@0 110 if (!dtvm) {
Chris@0 111 std::cerr << "RealTimePluginTransform::getInput: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl;
Chris@0 112 }
Chris@0 113 return dtvm;
Chris@0 114 }
Chris@0 115
Chris@0 116 void
Chris@0 117 RealTimePluginTransform::run()
Chris@0 118 {
Chris@0 119 DenseTimeValueModel *input = getInput();
Chris@0 120 if (!input) return;
Chris@0 121
Chris@55 122 while (!input->isReady()) {
Chris@55 123 if (dynamic_cast<WaveFileModel *>(input)) break; // no need to wait
Chris@55 124 std::cerr << "FeatureExtractionPluginTransform::run: Waiting for input model to be ready..." << std::endl;
Chris@55 125 sleep(1);
Chris@55 126 }
Chris@55 127
Chris@39 128 SparseTimeValueModel *stvm = dynamic_cast<SparseTimeValueModel *>(m_output);
Chris@39 129 WritableWaveFileModel *wwfm = dynamic_cast<WritableWaveFileModel *>(m_output);
Chris@39 130 if (!stvm && !wwfm) return;
Chris@0 131
Chris@39 132 if (stvm && (m_outputNo >= m_plugin->getControlOutputCount())) return;
Chris@0 133
Chris@0 134 size_t sampleRate = input->getSampleRate();
Chris@0 135 int channelCount = input->getChannelCount();
Chris@44 136 if (!wwfm && m_context.channel != -1) channelCount = 1;
Chris@0 137
Chris@0 138 size_t blockSize = m_plugin->getBufferSize();
Chris@0 139
Chris@0 140 float **buffers = m_plugin->getAudioInputBuffers();
Chris@0 141
Chris@0 142 size_t startFrame = m_input->getStartFrame();
Chris@0 143 size_t endFrame = m_input->getEndFrame();
Chris@0 144 size_t blockFrame = startFrame;
Chris@0 145
Chris@0 146 size_t prevCompletion = 0;
Chris@0 147
Chris@39 148 size_t latency = m_plugin->getLatency();
Chris@39 149
Chris@0 150 int i = 0;
Chris@0 151
Chris@0 152 while (blockFrame < endFrame) {
Chris@0 153
Chris@0 154 size_t completion =
Chris@0 155 (((blockFrame - startFrame) / blockSize) * 99) /
Chris@0 156 ( (endFrame - startFrame) / blockSize);
Chris@0 157
Chris@0 158 size_t got = 0;
Chris@0 159
Chris@0 160 if (channelCount == 1) {
Chris@40 161 if (buffers && buffers[0]) {
Chris@40 162 got = input->getValues
Chris@40 163 (m_context.channel, blockFrame, blockFrame + blockSize, buffers[0]);
Chris@40 164 while (got < blockSize) {
Chris@40 165 buffers[0][got++] = 0.0;
Chris@0 166 }
Chris@40 167 if (m_context.channel == -1 && channelCount > 1) {
Chris@40 168 // use mean instead of sum, as plugin input
Chris@40 169 for (size_t i = 0; i < got; ++i) {
Chris@40 170 buffers[0][i] /= channelCount;
Chris@40 171 }
Chris@40 172 }
Chris@40 173 }
Chris@0 174 } else {
Chris@0 175 for (size_t ch = 0; ch < channelCount; ++ch) {
Chris@40 176 if (buffers && buffers[ch]) {
Chris@40 177 got = input->getValues
Chris@40 178 (ch, blockFrame, blockFrame + blockSize, buffers[ch]);
Chris@40 179 while (got < blockSize) {
Chris@40 180 buffers[ch][got++] = 0.0;
Chris@40 181 }
Chris@40 182 }
Chris@0 183 }
Chris@0 184 }
Chris@0 185
Chris@0 186 m_plugin->run(Vamp::RealTime::frame2RealTime(blockFrame, sampleRate));
Chris@0 187
Chris@39 188 if (stvm) {
Chris@0 189
Chris@39 190 float value = m_plugin->getControlOutputValue(m_outputNo);
Chris@39 191
Chris@39 192 size_t pointFrame = blockFrame;
Chris@39 193 if (pointFrame > latency) pointFrame -= latency;
Chris@39 194 else pointFrame = 0;
Chris@39 195
Chris@39 196 stvm->addPoint(SparseTimeValueModel::Point
Chris@39 197 (pointFrame, value, ""));
Chris@39 198
Chris@39 199 } else if (wwfm) {
Chris@39 200
Chris@39 201 float **buffers = m_plugin->getAudioOutputBuffers();
Chris@39 202
Chris@40 203 if (buffers) {
Chris@40 204
Chris@40 205 if (blockFrame >= latency) {
Chris@40 206 wwfm->addSamples(buffers, blockSize);
Chris@40 207 } else if (blockFrame + blockSize >= latency) {
Chris@40 208 size_t offset = latency - blockFrame;
Chris@40 209 size_t count = blockSize - offset;
Chris@40 210 float **tmp = new float *[channelCount];
Chris@40 211 for (size_t c = 0; c < channelCount; ++c) {
Chris@40 212 tmp[c] = buffers[c] + offset;
Chris@40 213 }
Chris@40 214 wwfm->addSamples(tmp, count);
Chris@40 215 delete[] tmp;
Chris@39 216 }
Chris@39 217 }
Chris@39 218 }
Chris@0 219
Chris@0 220 if (blockFrame == startFrame || completion > prevCompletion) {
Chris@39 221 if (stvm) stvm->setCompletion(completion);
Chris@55 222 if (wwfm) wwfm->setCompletion(completion);
Chris@0 223 prevCompletion = completion;
Chris@0 224 }
Chris@0 225
Chris@0 226 blockFrame += blockSize;
Chris@0 227 }
Chris@0 228
Chris@39 229 if (stvm) stvm->setCompletion(100);
Chris@55 230 if (wwfm) wwfm->setCompletion(100);
Chris@0 231 }
Chris@0 232