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