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@64
|
30 int channel,
|
Chris@60
|
31 QString configurationXml,
|
Chris@63
|
32 QString units,
|
Chris@60
|
33 int output) :
|
Chris@60
|
34 Transform(inputModel),
|
Chris@60
|
35 m_plugin(0),
|
Chris@64
|
36 m_channel(channel),
|
Chris@60
|
37 m_outputNo(output)
|
Chris@60
|
38 {
|
Chris@60
|
39 std::cerr << "RealTimePluginTransform::RealTimePluginTransform: plugin " << pluginId.toStdString() << ", output " << output << std::endl;
|
Chris@60
|
40
|
Chris@60
|
41 RealTimePluginFactory *factory =
|
Chris@60
|
42 RealTimePluginFactory::instanceFor(pluginId);
|
Chris@60
|
43
|
Chris@60
|
44 if (!factory) {
|
Chris@60
|
45 std::cerr << "RealTimePluginTransform: No factory available for plugin id \""
|
Chris@60
|
46 << pluginId.toStdString() << "\"" << std::endl;
|
Chris@60
|
47 return;
|
Chris@60
|
48 }
|
Chris@60
|
49
|
Chris@60
|
50 DenseTimeValueModel *input = getInput();
|
Chris@60
|
51 if (!input) return;
|
Chris@60
|
52
|
Chris@60
|
53 m_plugin = factory->instantiatePlugin(pluginId, 0, 0, m_input->getSampleRate(),
|
Chris@60
|
54 1024, //!!! wants to be configurable
|
Chris@60
|
55 input->getChannelCount());
|
Chris@60
|
56
|
Chris@60
|
57 if (!m_plugin) {
|
Chris@60
|
58 std::cerr << "RealTimePluginTransform: Failed to instantiate plugin \""
|
Chris@60
|
59 << pluginId.toStdString() << "\"" << std::endl;
|
Chris@60
|
60 return;
|
Chris@60
|
61 }
|
Chris@60
|
62
|
Chris@60
|
63 if (configurationXml != "") {
|
Chris@60
|
64 m_plugin->setParametersFromXml(configurationXml);
|
Chris@60
|
65 }
|
Chris@60
|
66
|
Chris@60
|
67 if (m_outputNo >= m_plugin->getControlOutputCount()) {
|
Chris@60
|
68 std::cerr << "RealTimePluginTransform: Plugin has fewer than desired " << m_outputNo << " control outputs" << std::endl;
|
Chris@60
|
69 return;
|
Chris@60
|
70 }
|
Chris@60
|
71
|
Chris@63
|
72 SparseTimeValueModel *model = new SparseTimeValueModel
|
Chris@63
|
73 (input->getSampleRate(), 1024, //!!!
|
Chris@63
|
74 0.0, 0.0, false);
|
Chris@63
|
75
|
Chris@63
|
76 if (units != "") model->setScaleUnits(units);
|
Chris@63
|
77
|
Chris@63
|
78 m_output = model;
|
Chris@60
|
79 }
|
Chris@60
|
80
|
Chris@60
|
81 RealTimePluginTransform::~RealTimePluginTransform()
|
Chris@60
|
82 {
|
Chris@60
|
83 delete m_plugin;
|
Chris@60
|
84 }
|
Chris@60
|
85
|
Chris@60
|
86 DenseTimeValueModel *
|
Chris@60
|
87 RealTimePluginTransform::getInput()
|
Chris@60
|
88 {
|
Chris@60
|
89 DenseTimeValueModel *dtvm =
|
Chris@60
|
90 dynamic_cast<DenseTimeValueModel *>(getInputModel());
|
Chris@60
|
91 if (!dtvm) {
|
Chris@60
|
92 std::cerr << "RealTimePluginTransform::getInput: WARNING: Input model is not conformable to DenseTimeValueModel" << std::endl;
|
Chris@60
|
93 }
|
Chris@60
|
94 return dtvm;
|
Chris@60
|
95 }
|
Chris@60
|
96
|
Chris@60
|
97 void
|
Chris@60
|
98 RealTimePluginTransform::run()
|
Chris@60
|
99 {
|
Chris@60
|
100 DenseTimeValueModel *input = getInput();
|
Chris@60
|
101 if (!input) return;
|
Chris@60
|
102
|
Chris@60
|
103 SparseTimeValueModel *model = dynamic_cast<SparseTimeValueModel *>(m_output);
|
Chris@60
|
104 if (!model) return;
|
Chris@60
|
105
|
Chris@60
|
106 if (m_outputNo >= m_plugin->getControlOutputCount()) return;
|
Chris@60
|
107
|
Chris@60
|
108 size_t sampleRate = input->getSampleRate();
|
Chris@60
|
109 int channelCount = input->getChannelCount();
|
Chris@64
|
110 if (m_channel != -1) channelCount = 1;
|
Chris@64
|
111
|
Chris@60
|
112 size_t blockSize = m_plugin->getBufferSize();
|
Chris@60
|
113
|
Chris@60
|
114 float **buffers = m_plugin->getAudioInputBuffers();
|
Chris@60
|
115
|
Chris@60
|
116 size_t startFrame = m_input->getStartFrame();
|
Chris@60
|
117 size_t endFrame = m_input->getEndFrame();
|
Chris@60
|
118 size_t blockFrame = startFrame;
|
Chris@60
|
119
|
Chris@60
|
120 size_t prevCompletion = 0;
|
Chris@60
|
121
|
Chris@60
|
122 int i = 0;
|
Chris@60
|
123
|
Chris@60
|
124 while (blockFrame < endFrame) {
|
Chris@60
|
125
|
Chris@60
|
126 size_t completion =
|
Chris@60
|
127 (((blockFrame - startFrame) / blockSize) * 99) /
|
Chris@60
|
128 ( (endFrame - startFrame) / blockSize);
|
Chris@60
|
129
|
Chris@60
|
130 size_t got = 0;
|
Chris@60
|
131
|
Chris@60
|
132 if (channelCount == 1) {
|
Chris@60
|
133 got = input->getValues
|
Chris@64
|
134 (m_channel, blockFrame, blockFrame + blockSize, buffers[0]);
|
Chris@60
|
135 while (got < blockSize) {
|
Chris@60
|
136 buffers[0][got++] = 0.0;
|
Chris@60
|
137 }
|
Chris@60
|
138 } else {
|
Chris@60
|
139 for (size_t ch = 0; ch < channelCount; ++ch) {
|
Chris@60
|
140 got = input->getValues
|
Chris@60
|
141 (ch, blockFrame, blockFrame + blockSize, buffers[ch]);
|
Chris@60
|
142 while (got < blockSize) {
|
Chris@60
|
143 buffers[ch][got++] = 0.0;
|
Chris@60
|
144 }
|
Chris@60
|
145 }
|
Chris@60
|
146 }
|
Chris@60
|
147
|
Chris@60
|
148 m_plugin->run(RealTime::frame2RealTime(blockFrame, sampleRate));
|
Chris@60
|
149
|
Chris@60
|
150 float value = m_plugin->getControlOutputValue(m_outputNo);
|
Chris@60
|
151
|
Chris@61
|
152 model->addPoint(SparseTimeValueModel::Point
|
Chris@61
|
153 (blockFrame - m_plugin->getLatency(), value, ""));
|
Chris@60
|
154
|
Chris@60
|
155 if (blockFrame == startFrame || completion > prevCompletion) {
|
Chris@60
|
156 model->setCompletion(completion);
|
Chris@60
|
157 prevCompletion = completion;
|
Chris@60
|
158 }
|
Chris@60
|
159
|
Chris@60
|
160 blockFrame += blockSize;
|
Chris@60
|
161 }
|
Chris@60
|
162
|
Chris@60
|
163 model->setCompletion(100);
|
Chris@60
|
164 }
|
Chris@60
|
165
|