annotate transform/RealTimePluginTransform.cpp @ 185:f3191ab6d564

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