comparison transform/RealTimePluginTransform.cpp @ 0:cd5d7ff8ef38

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