Chris@320
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@320
|
2
|
Chris@320
|
3 /*
|
Chris@320
|
4 Sonic Visualiser
|
Chris@320
|
5 An audio file viewer and annotation editor.
|
Chris@320
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@320
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@320
|
8
|
Chris@320
|
9 This program is free software; you can redistribute it and/or
|
Chris@320
|
10 modify it under the terms of the GNU General Public License as
|
Chris@320
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@320
|
12 License, or (at your option) any later version. See the file
|
Chris@320
|
13 COPYING included with this distribution for more information.
|
Chris@320
|
14 */
|
Chris@320
|
15
|
Chris@331
|
16 #include "RealTimeEffectModelTransformer.h"
|
Chris@320
|
17
|
Chris@320
|
18 #include "plugin/RealTimePluginFactory.h"
|
Chris@320
|
19 #include "plugin/RealTimePluginInstance.h"
|
Chris@320
|
20 #include "plugin/PluginXml.h"
|
Chris@320
|
21
|
Chris@320
|
22 #include "data/model/Model.h"
|
Chris@320
|
23 #include "data/model/SparseTimeValueModel.h"
|
Chris@320
|
24 #include "data/model/DenseTimeValueModel.h"
|
Chris@320
|
25 #include "data/model/WritableWaveFileModel.h"
|
Chris@320
|
26 #include "data/model/WaveFileModel.h"
|
Chris@320
|
27
|
Chris@350
|
28 #include "TransformFactory.h"
|
Chris@350
|
29
|
Chris@320
|
30 #include <iostream>
|
Chris@320
|
31
|
Chris@350
|
32 RealTimeEffectModelTransformer::RealTimeEffectModelTransformer(Input in,
|
Chris@850
|
33 const Transform &t) :
|
Chris@850
|
34 ModelTransformer(in, t),
|
Chris@1582
|
35 m_plugin(nullptr)
|
Chris@320
|
36 {
|
Chris@850
|
37 Transform transform(t);
|
Chris@850
|
38 if (!transform.getBlockSize()) {
|
Chris@850
|
39 transform.setBlockSize(1024);
|
Chris@850
|
40 m_transforms[0] = transform;
|
Chris@850
|
41 }
|
Chris@850
|
42
|
Chris@350
|
43 m_units = TransformFactory::getInstance()->getTransformUnits
|
Chris@350
|
44 (transform.getIdentifier());
|
Chris@350
|
45 m_outputNo =
|
Chris@350
|
46 (transform.getOutput() == "A") ? -1 : transform.getOutput().toInt();
|
Chris@350
|
47
|
Chris@350
|
48 QString pluginId = transform.getPluginIdentifier();
|
Chris@350
|
49
|
Chris@1496
|
50 SVDEBUG << "RealTimeEffectModelTransformer::RealTimeEffectModelTransformer: plugin " << pluginId << ", output " << transform.getOutput() << endl;
|
Chris@320
|
51
|
Chris@320
|
52 RealTimePluginFactory *factory =
|
Chris@1429
|
53 RealTimePluginFactory::instanceFor(pluginId);
|
Chris@320
|
54
|
Chris@320
|
55 if (!factory) {
|
Chris@1496
|
56 SVCERR << "RealTimeEffectModelTransformer: No factory available for plugin id \""
|
Chris@1496
|
57 << pluginId << "\"" << endl;
|
Chris@1429
|
58 return;
|
Chris@320
|
59 }
|
Chris@320
|
60
|
Chris@1740
|
61 auto input = ModelById::getAs<DenseTimeValueModel>(getInputModel());
|
Chris@1740
|
62 if (!input) {
|
Chris@1740
|
63 SVCERR << "RealTimeEffectModelTransformer: Input is absent or of wrong type" << endl;
|
Chris@1740
|
64 return;
|
Chris@1740
|
65 }
|
Chris@320
|
66
|
Chris@320
|
67 m_plugin = factory->instantiatePlugin(pluginId, 0, 0,
|
Chris@350
|
68 input->getSampleRate(),
|
Chris@850
|
69 transform.getBlockSize(),
|
Chris@320
|
70 input->getChannelCount());
|
Chris@320
|
71
|
Chris@320
|
72 if (!m_plugin) {
|
Chris@1496
|
73 SVCERR << "RealTimeEffectModelTransformer: Failed to instantiate plugin \""
|
Chris@1496
|
74 << pluginId << "\"" << endl;
|
Chris@1429
|
75 return;
|
Chris@320
|
76 }
|
Chris@320
|
77
|
Chris@850
|
78 TransformFactory::getInstance()->setPluginParameters(transform, m_plugin);
|
Chris@320
|
79
|
Chris@320
|
80 if (m_outputNo >= 0 &&
|
Chris@320
|
81 m_outputNo >= int(m_plugin->getControlOutputCount())) {
|
Chris@843
|
82 cerr << "RealTimeEffectModelTransformer: Plugin has fewer than desired " << m_outputNo << " control outputs" << endl;
|
Chris@320
|
83 return;
|
Chris@320
|
84 }
|
Chris@320
|
85
|
Chris@320
|
86 if (m_outputNo == -1) {
|
Chris@320
|
87
|
Chris@1039
|
88 int outputChannels = (int)m_plugin->getAudioOutputCount();
|
Chris@320
|
89 if (outputChannels > input->getChannelCount()) {
|
Chris@320
|
90 outputChannels = input->getChannelCount();
|
Chris@320
|
91 }
|
Chris@320
|
92
|
Chris@1740
|
93 auto model = std::make_shared<WritableWaveFileModel>
|
Chris@320
|
94 (input->getSampleRate(), outputChannels);
|
Chris@320
|
95
|
Chris@1740
|
96 ModelById::add(model);
|
Chris@1740
|
97 m_outputs.push_back(model->getId());
|
Chris@320
|
98
|
Chris@320
|
99 } else {
|
Chris@1429
|
100
|
Chris@1740
|
101 auto model = std::make_shared<SparseTimeValueModel>
|
Chris@1740
|
102 (input->getSampleRate(), transform.getBlockSize(),
|
Chris@1740
|
103 0.0, 0.0, false);
|
Chris@350
|
104 if (m_units != "") model->setScaleUnits(m_units);
|
Chris@320
|
105
|
Chris@1740
|
106 ModelById::add(model);
|
Chris@1740
|
107 m_outputs.push_back(model->getId());
|
Chris@320
|
108 }
|
Chris@320
|
109 }
|
Chris@320
|
110
|
Chris@331
|
111 RealTimeEffectModelTransformer::~RealTimeEffectModelTransformer()
|
Chris@320
|
112 {
|
Chris@320
|
113 delete m_plugin;
|
Chris@320
|
114 }
|
Chris@320
|
115
|
Chris@320
|
116 void
|
Chris@331
|
117 RealTimeEffectModelTransformer::run()
|
Chris@320
|
118 {
|
Chris@1740
|
119 if (m_outputs.empty()) {
|
Chris@1740
|
120 abandon();
|
Chris@1496
|
121 return;
|
Chris@1496
|
122 }
|
Chris@1740
|
123
|
Chris@1740
|
124 bool ready = false;
|
Chris@1740
|
125 while (!ready && !m_abandoned) {
|
Chris@1740
|
126 { // scope so as to release input shared_ptr before sleeping
|
Chris@1740
|
127 auto input = ModelById::getAs<DenseTimeValueModel>(getInputModel());
|
Chris@1740
|
128 if (!input) {
|
Chris@1740
|
129 abandon();
|
Chris@1740
|
130 return;
|
Chris@1740
|
131 }
|
Chris@1740
|
132 ready = input->isReady();
|
Chris@1740
|
133 }
|
Chris@1740
|
134 if (!ready) {
|
Chris@1740
|
135 SVDEBUG << "RealTimeEffectModelTransformer::run: Waiting for input model to be ready..." << endl;
|
Chris@1740
|
136 usleep(500000);
|
Chris@1740
|
137 }
|
Chris@1740
|
138 }
|
Chris@1740
|
139 if (m_abandoned) return;
|
Chris@1740
|
140
|
Chris@1740
|
141 auto input = ModelById::getAs<DenseTimeValueModel>(getInputModel());
|
Chris@1740
|
142 if (!input) {
|
Chris@1740
|
143 abandon();
|
Chris@1496
|
144 return;
|
Chris@1496
|
145 }
|
Chris@1740
|
146
|
Chris@1740
|
147 auto stvm = ModelById::getAs<SparseTimeValueModel>(m_outputs[0]);
|
Chris@1740
|
148 auto wwfm = ModelById::getAs<WritableWaveFileModel>(m_outputs[0]);
|
Chris@320
|
149
|
Chris@1496
|
150 if (!stvm && !wwfm) {
|
Chris@1496
|
151 return;
|
Chris@1496
|
152 }
|
Chris@320
|
153
|
Chris@1496
|
154 if (stvm && (m_outputNo >= int(m_plugin->getControlOutputCount()))) {
|
Chris@1496
|
155 return;
|
Chris@1496
|
156 }
|
Chris@320
|
157
|
Chris@1040
|
158 sv_samplerate_t sampleRate = input->getSampleRate();
|
Chris@930
|
159 int channelCount = input->getChannelCount();
|
Chris@350
|
160 if (!wwfm && m_input.getChannel() != -1) channelCount = 1;
|
Chris@320
|
161
|
Chris@1039
|
162 sv_frame_t blockSize = m_plugin->getBufferSize();
|
Chris@320
|
163
|
Chris@320
|
164 float **inbufs = m_plugin->getAudioInputBuffers();
|
Chris@320
|
165
|
Chris@1740
|
166 sv_frame_t startFrame = input->getStartFrame();
|
Chris@1740
|
167 sv_frame_t endFrame = input->getEndFrame();
|
Chris@850
|
168
|
Chris@850
|
169 Transform transform = m_transforms[0];
|
Chris@320
|
170
|
Chris@850
|
171 RealTime contextStartRT = transform.getStartTime();
|
Chris@850
|
172 RealTime contextDurationRT = transform.getDuration();
|
Chris@350
|
173
|
Chris@1039
|
174 sv_frame_t contextStart =
|
Chris@350
|
175 RealTime::realTime2Frame(contextStartRT, sampleRate);
|
Chris@350
|
176
|
Chris@1039
|
177 sv_frame_t contextDuration =
|
Chris@350
|
178 RealTime::realTime2Frame(contextDurationRT, sampleRate);
|
Chris@320
|
179
|
Chris@320
|
180 if (contextStart == 0 || contextStart < startFrame) {
|
Chris@320
|
181 contextStart = startFrame;
|
Chris@320
|
182 }
|
Chris@320
|
183
|
Chris@320
|
184 if (contextDuration == 0) {
|
Chris@320
|
185 contextDuration = endFrame - contextStart;
|
Chris@320
|
186 }
|
Chris@320
|
187 if (contextStart + contextDuration > endFrame) {
|
Chris@320
|
188 contextDuration = endFrame - contextStart;
|
Chris@320
|
189 }
|
Chris@320
|
190
|
Chris@414
|
191 if (wwfm) {
|
Chris@414
|
192 wwfm->setStartFrame(contextStart);
|
Chris@414
|
193 }
|
Chris@320
|
194
|
Chris@1039
|
195 sv_frame_t blockFrame = contextStart;
|
Chris@320
|
196
|
Chris@1039
|
197 int prevCompletion = 0;
|
Chris@320
|
198
|
Chris@1039
|
199 sv_frame_t latency = m_plugin->getLatency();
|
Chris@320
|
200
|
Chris@320
|
201 while (blockFrame < contextStart + contextDuration + latency &&
|
Chris@320
|
202 !m_abandoned) {
|
Chris@320
|
203
|
Chris@1429
|
204 int completion = int
|
Chris@1429
|
205 ((((blockFrame - contextStart) / blockSize) * 99) /
|
Chris@1039
|
206 (1 + ((contextDuration) / blockSize)));
|
Chris@320
|
207
|
Chris@1429
|
208 sv_frame_t got = 0;
|
Chris@320
|
209
|
Chris@1429
|
210 if (channelCount == 1) {
|
Chris@320
|
211 if (inbufs && inbufs[0]) {
|
Chris@1096
|
212 auto data = input->getData
|
Chris@1096
|
213 (m_input.getChannel(), blockFrame, blockSize);
|
Chris@1096
|
214 got = data.size();
|
Chris@1096
|
215 for (sv_frame_t i = 0; i < got; ++i) {
|
Chris@1096
|
216 inbufs[0][i] = data[i];
|
Chris@1096
|
217 }
|
Chris@320
|
218 while (got < blockSize) {
|
Chris@1096
|
219 inbufs[0][got++] = 0.f;
|
Chris@320
|
220 }
|
Chris@975
|
221 for (int ch = 1; ch < (int)m_plugin->getAudioInputCount(); ++ch) {
|
Chris@1039
|
222 for (sv_frame_t i = 0; i < blockSize; ++i) {
|
Chris@975
|
223 inbufs[ch][i] = inbufs[0][i];
|
Chris@975
|
224 }
|
Chris@320
|
225 }
|
Chris@320
|
226 }
|
Chris@1429
|
227 } else {
|
Chris@429
|
228 if (inbufs && inbufs[0]) {
|
Chris@1096
|
229 auto data = input->getMultiChannelData
|
Chris@1096
|
230 (0, channelCount - 1, blockFrame, blockSize);
|
Chris@1096
|
231 if (!data.empty()) got = data[0].size();
|
Chris@1096
|
232 for (int ch = 0; ch < channelCount; ++ch) {
|
Chris@1096
|
233 for (sv_frame_t i = 0; i < got; ++i) {
|
Chris@1096
|
234 inbufs[ch][i] = data[ch][i];
|
Chris@1096
|
235 }
|
Chris@1096
|
236 }
|
Chris@429
|
237 while (got < blockSize) {
|
Chris@930
|
238 for (int ch = 0; ch < channelCount; ++ch) {
|
Chris@429
|
239 inbufs[ch][got] = 0.0;
|
Chris@429
|
240 }
|
Chris@429
|
241 ++got;
|
Chris@320
|
242 }
|
Chris@975
|
243 for (int ch = channelCount; ch < (int)m_plugin->getAudioInputCount(); ++ch) {
|
Chris@1039
|
244 for (sv_frame_t i = 0; i < blockSize; ++i) {
|
Chris@975
|
245 inbufs[ch][i] = inbufs[ch % channelCount][i];
|
Chris@975
|
246 }
|
Chris@320
|
247 }
|
Chris@320
|
248 }
|
Chris@1429
|
249 }
|
Chris@320
|
250
|
Chris@1040
|
251 m_plugin->run(RealTime::frame2RealTime(blockFrame, sampleRate));
|
Chris@320
|
252
|
Chris@320
|
253 if (stvm) {
|
Chris@320
|
254
|
Chris@320
|
255 float value = m_plugin->getControlOutputValue(m_outputNo);
|
Chris@320
|
256
|
Chris@1039
|
257 sv_frame_t pointFrame = blockFrame;
|
Chris@320
|
258 if (pointFrame > latency) pointFrame -= latency;
|
Chris@320
|
259 else pointFrame = 0;
|
Chris@320
|
260
|
Chris@1651
|
261 stvm->add(Event(pointFrame, value, ""));
|
Chris@320
|
262
|
Chris@320
|
263 } else if (wwfm) {
|
Chris@320
|
264
|
Chris@320
|
265 float **outbufs = m_plugin->getAudioOutputBuffers();
|
Chris@320
|
266
|
Chris@320
|
267 if (outbufs) {
|
Chris@320
|
268
|
Chris@320
|
269 if (blockFrame >= latency) {
|
Chris@1039
|
270 sv_frame_t writeSize = std::min
|
Chris@320
|
271 (blockSize,
|
Chris@320
|
272 contextStart + contextDuration + latency - blockFrame);
|
Chris@320
|
273 wwfm->addSamples(outbufs, writeSize);
|
Chris@320
|
274 } else if (blockFrame + blockSize >= latency) {
|
Chris@1039
|
275 sv_frame_t offset = latency - blockFrame;
|
Chris@1039
|
276 sv_frame_t count = blockSize - offset;
|
Chris@320
|
277 float **tmp = new float *[channelCount];
|
Chris@930
|
278 for (int c = 0; c < channelCount; ++c) {
|
Chris@320
|
279 tmp[c] = outbufs[c] + offset;
|
Chris@320
|
280 }
|
Chris@320
|
281 wwfm->addSamples(tmp, count);
|
Chris@320
|
282 delete[] tmp;
|
Chris@320
|
283 }
|
Chris@320
|
284 }
|
Chris@320
|
285 }
|
Chris@320
|
286
|
Chris@1429
|
287 if (blockFrame == contextStart || completion > prevCompletion) {
|
Chris@1133
|
288 // This setCompletion is probably misusing the completion
|
Chris@1133
|
289 // terminology, just as it was for WritableWaveFileModel
|
Chris@1429
|
290 if (stvm) stvm->setCompletion(completion);
|
Chris@1429
|
291 if (wwfm) wwfm->setWriteProportion(completion);
|
Chris@1429
|
292 prevCompletion = completion;
|
Chris@1429
|
293 }
|
Chris@320
|
294
|
Chris@1429
|
295 blockFrame += blockSize;
|
Chris@320
|
296 }
|
Chris@320
|
297
|
Chris@320
|
298 if (m_abandoned) return;
|
Chris@320
|
299
|
Chris@320
|
300 if (stvm) stvm->setCompletion(100);
|
Chris@1133
|
301 if (wwfm) wwfm->writeComplete();
|
Chris@320
|
302 }
|
Chris@320
|
303
|