cannam@64
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
cannam@64
|
2
|
cannam@64
|
3 /*
|
cannam@64
|
4 Vamp
|
cannam@64
|
5
|
cannam@64
|
6 An API for audio analysis and feature extraction plugins.
|
cannam@64
|
7
|
cannam@64
|
8 Centre for Digital Music, Queen Mary, University of London.
|
cannam@71
|
9 Copyright 2006-2007 Chris Cannam and QMUL.
|
cannam@64
|
10
|
cannam@64
|
11 Permission is hereby granted, free of charge, to any person
|
cannam@64
|
12 obtaining a copy of this software and associated documentation
|
cannam@64
|
13 files (the "Software"), to deal in the Software without
|
cannam@64
|
14 restriction, including without limitation the rights to use, copy,
|
cannam@64
|
15 modify, merge, publish, distribute, sublicense, and/or sell copies
|
cannam@64
|
16 of the Software, and to permit persons to whom the Software is
|
cannam@64
|
17 furnished to do so, subject to the following conditions:
|
cannam@64
|
18
|
cannam@64
|
19 The above copyright notice and this permission notice shall be
|
cannam@64
|
20 included in all copies or substantial portions of the Software.
|
cannam@64
|
21
|
cannam@64
|
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
cannam@64
|
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
cannam@64
|
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
cannam@64
|
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
cannam@64
|
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
cannam@64
|
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
cannam@64
|
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
cannam@64
|
29
|
cannam@64
|
30 Except as contained in this notice, the names of the Centre for
|
cannam@64
|
31 Digital Music; Queen Mary, University of London; and Chris Cannam
|
cannam@64
|
32 shall not be used in advertising or otherwise to promote the sale,
|
cannam@64
|
33 use or other dealings in this Software without prior written
|
cannam@64
|
34 authorization.
|
cannam@64
|
35 */
|
cannam@64
|
36
|
cannam@64
|
37 #include "PluginChannelAdapter.h"
|
cannam@64
|
38
|
cannam@64
|
39 namespace Vamp {
|
cannam@64
|
40
|
cannam@64
|
41 namespace HostExt {
|
cannam@64
|
42
|
cannam@70
|
43 class PluginChannelAdapter::Impl
|
cannam@70
|
44 {
|
cannam@70
|
45 public:
|
cannam@70
|
46 Impl(Plugin *plugin);
|
cannam@70
|
47 ~Impl();
|
cannam@70
|
48
|
cannam@70
|
49 bool initialise(size_t channels, size_t stepSize, size_t blockSize);
|
cannam@70
|
50
|
cannam@70
|
51 FeatureSet process(const float *const *inputBuffers, RealTime timestamp);
|
cannam@197
|
52 FeatureSet processInterleaved(const float *inputBuffers, RealTime timestamp);
|
cannam@70
|
53
|
cannam@70
|
54 protected:
|
cannam@70
|
55 Plugin *m_plugin;
|
cannam@70
|
56 size_t m_blockSize;
|
cannam@70
|
57 size_t m_inputChannels;
|
cannam@70
|
58 size_t m_pluginChannels;
|
cannam@70
|
59 float **m_buffer;
|
cannam@197
|
60 float **m_deinterleave;
|
cannam@70
|
61 const float **m_forwardPtrs;
|
cannam@70
|
62 };
|
cannam@70
|
63
|
cannam@64
|
64 PluginChannelAdapter::PluginChannelAdapter(Plugin *plugin) :
|
cannam@70
|
65 PluginWrapper(plugin)
|
cannam@70
|
66 {
|
cannam@70
|
67 m_impl = new Impl(plugin);
|
cannam@70
|
68 }
|
cannam@70
|
69
|
cannam@70
|
70 PluginChannelAdapter::~PluginChannelAdapter()
|
cannam@70
|
71 {
|
cannam@70
|
72 delete m_impl;
|
cannam@70
|
73 }
|
cannam@70
|
74
|
cannam@70
|
75 bool
|
cannam@70
|
76 PluginChannelAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
cannam@70
|
77 {
|
cannam@70
|
78 return m_impl->initialise(channels, stepSize, blockSize);
|
cannam@70
|
79 }
|
cannam@70
|
80
|
cannam@70
|
81 PluginChannelAdapter::FeatureSet
|
cannam@70
|
82 PluginChannelAdapter::process(const float *const *inputBuffers,
|
cannam@70
|
83 RealTime timestamp)
|
cannam@70
|
84 {
|
cannam@70
|
85 return m_impl->process(inputBuffers, timestamp);
|
cannam@70
|
86 }
|
cannam@70
|
87
|
cannam@197
|
88 PluginChannelAdapter::FeatureSet
|
cannam@197
|
89 PluginChannelAdapter::processInterleaved(const float *inputBuffers,
|
cannam@197
|
90 RealTime timestamp)
|
cannam@197
|
91 {
|
cannam@197
|
92 return m_impl->processInterleaved(inputBuffers, timestamp);
|
cannam@197
|
93 }
|
cannam@197
|
94
|
cannam@70
|
95 PluginChannelAdapter::Impl::Impl(Plugin *plugin) :
|
cannam@70
|
96 m_plugin(plugin),
|
cannam@64
|
97 m_blockSize(0),
|
cannam@64
|
98 m_inputChannels(0),
|
cannam@64
|
99 m_pluginChannels(0),
|
cannam@64
|
100 m_buffer(0),
|
cannam@197
|
101 m_deinterleave(0),
|
cannam@64
|
102 m_forwardPtrs(0)
|
cannam@64
|
103 {
|
cannam@64
|
104 }
|
cannam@64
|
105
|
cannam@70
|
106 PluginChannelAdapter::Impl::~Impl()
|
cannam@64
|
107 {
|
cannam@70
|
108 // the adapter will delete the plugin
|
cannam@70
|
109
|
cannam@64
|
110 if (m_buffer) {
|
cannam@64
|
111 if (m_inputChannels > m_pluginChannels) {
|
cannam@64
|
112 delete[] m_buffer[0];
|
cannam@64
|
113 } else {
|
cannam@64
|
114 for (size_t i = 0; i < m_pluginChannels - m_inputChannels; ++i) {
|
cannam@64
|
115 delete[] m_buffer[i];
|
cannam@64
|
116 }
|
cannam@64
|
117 }
|
cannam@64
|
118 delete[] m_buffer;
|
cannam@64
|
119 m_buffer = 0;
|
cannam@64
|
120 }
|
cannam@64
|
121
|
cannam@197
|
122 if (m_deinterleave) {
|
cannam@197
|
123 for (size_t i = 0; i < m_inputChannels; ++i) {
|
cannam@197
|
124 delete[] m_deinterleave[i];
|
cannam@197
|
125 }
|
cannam@197
|
126 delete[] m_deinterleave;
|
cannam@197
|
127 m_deinterleave = 0;
|
cannam@197
|
128 }
|
cannam@197
|
129
|
cannam@64
|
130 if (m_forwardPtrs) {
|
cannam@64
|
131 delete[] m_forwardPtrs;
|
cannam@64
|
132 m_forwardPtrs = 0;
|
cannam@64
|
133 }
|
cannam@64
|
134 }
|
cannam@64
|
135
|
cannam@64
|
136 bool
|
cannam@70
|
137 PluginChannelAdapter::Impl::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
cannam@64
|
138 {
|
cannam@64
|
139 m_blockSize = blockSize;
|
cannam@64
|
140
|
cannam@64
|
141 size_t minch = m_plugin->getMinChannelCount();
|
cannam@64
|
142 size_t maxch = m_plugin->getMaxChannelCount();
|
cannam@64
|
143
|
cannam@64
|
144 m_inputChannels = channels;
|
cannam@64
|
145
|
cannam@64
|
146 if (m_inputChannels < minch) {
|
cannam@64
|
147
|
cannam@64
|
148 m_forwardPtrs = new const float *[minch];
|
cannam@64
|
149
|
cannam@64
|
150 if (m_inputChannels > 1) {
|
cannam@64
|
151 // We need a set of zero-valued buffers to add to the
|
cannam@64
|
152 // forwarded pointers
|
cannam@64
|
153 m_buffer = new float*[minch - channels];
|
cannam@64
|
154 for (size_t i = 0; i < minch; ++i) {
|
cannam@64
|
155 m_buffer[i] = new float[blockSize];
|
cannam@64
|
156 for (size_t j = 0; j < blockSize; ++j) {
|
cannam@64
|
157 m_buffer[i][j] = 0.f;
|
cannam@64
|
158 }
|
cannam@64
|
159 }
|
cannam@64
|
160 }
|
cannam@64
|
161
|
cannam@64
|
162 m_pluginChannels = minch;
|
cannam@64
|
163
|
cannam@64
|
164 std::cerr << "PluginChannelAdapter::initialise: expanding " << m_inputChannels << " to " << m_pluginChannels << " for plugin" << std::endl;
|
cannam@64
|
165
|
cannam@64
|
166 } else if (m_inputChannels > maxch) {
|
cannam@64
|
167
|
cannam@64
|
168 // We only need m_buffer if we are mixing down to a single
|
cannam@64
|
169 // channel -- otherwise we can just forward the same float* as
|
cannam@64
|
170 // passed in to process(), expecting the excess to be ignored
|
cannam@64
|
171
|
cannam@64
|
172 if (maxch == 1) {
|
cannam@64
|
173 m_buffer = new float *[1];
|
cannam@64
|
174 m_buffer[0] = new float[blockSize];
|
cannam@64
|
175
|
cannam@64
|
176 std::cerr << "PluginChannelAdapter::initialise: mixing " << m_inputChannels << " to mono for plugin" << std::endl;
|
cannam@64
|
177
|
cannam@64
|
178 } else {
|
cannam@64
|
179
|
cannam@64
|
180 std::cerr << "PluginChannelAdapter::initialise: reducing " << m_inputChannels << " to " << m_pluginChannels << " for plugin" << std::endl;
|
cannam@64
|
181 }
|
cannam@64
|
182
|
cannam@64
|
183 m_pluginChannels = maxch;
|
cannam@64
|
184
|
cannam@64
|
185 } else {
|
cannam@64
|
186
|
cannam@64
|
187 std::cerr << "PluginChannelAdapter::initialise: accepting given number of channels (" << m_inputChannels << ")" << std::endl;
|
cannam@64
|
188 m_pluginChannels = m_inputChannels;
|
cannam@64
|
189 }
|
cannam@64
|
190
|
cannam@64
|
191 return m_plugin->initialise(m_pluginChannels, stepSize, blockSize);
|
cannam@64
|
192 }
|
cannam@64
|
193
|
cannam@64
|
194 PluginChannelAdapter::FeatureSet
|
cannam@197
|
195 PluginChannelAdapter::Impl::processInterleaved(const float *inputBuffers,
|
cannam@197
|
196 RealTime timestamp)
|
cannam@197
|
197 {
|
cannam@197
|
198 if (!m_deinterleave) {
|
cannam@197
|
199 m_deinterleave = new float *[m_inputChannels];
|
cannam@197
|
200 for (size_t i = 0; i < m_inputChannels; ++i) {
|
cannam@197
|
201 m_deinterleave[i] = new float[m_blockSize];
|
cannam@197
|
202 }
|
cannam@197
|
203 }
|
cannam@197
|
204
|
cannam@197
|
205 for (size_t i = 0; i < m_inputChannels; ++i) {
|
cannam@197
|
206 for (size_t j = 0; j < m_blockSize; ++j) {
|
cannam@197
|
207 m_deinterleave[i][j] = inputBuffers[j * m_inputChannels + i];
|
cannam@197
|
208 }
|
cannam@197
|
209 }
|
cannam@197
|
210
|
cannam@197
|
211 return process(m_deinterleave, timestamp);
|
cannam@197
|
212 }
|
cannam@197
|
213
|
cannam@197
|
214 PluginChannelAdapter::FeatureSet
|
cannam@70
|
215 PluginChannelAdapter::Impl::process(const float *const *inputBuffers,
|
cannam@70
|
216 RealTime timestamp)
|
cannam@64
|
217 {
|
cannam@65
|
218 // std::cerr << "PluginChannelAdapter::process: " << m_inputChannels << " -> " << m_pluginChannels << " channels" << std::endl;
|
cannam@64
|
219
|
cannam@64
|
220 if (m_inputChannels < m_pluginChannels) {
|
cannam@64
|
221
|
cannam@64
|
222 if (m_inputChannels == 1) {
|
cannam@64
|
223 for (size_t i = 0; i < m_pluginChannels; ++i) {
|
cannam@64
|
224 m_forwardPtrs[i] = inputBuffers[0];
|
cannam@64
|
225 }
|
cannam@64
|
226 } else {
|
cannam@64
|
227 for (size_t i = 0; i < m_inputChannels; ++i) {
|
cannam@64
|
228 m_forwardPtrs[i] = inputBuffers[i];
|
cannam@64
|
229 }
|
cannam@64
|
230 for (size_t i = m_inputChannels; i < m_pluginChannels; ++i) {
|
cannam@64
|
231 m_forwardPtrs[i] = m_buffer[i - m_inputChannels];
|
cannam@64
|
232 }
|
cannam@64
|
233 }
|
cannam@64
|
234
|
cannam@64
|
235 return m_plugin->process(m_forwardPtrs, timestamp);
|
cannam@64
|
236
|
cannam@64
|
237 } else if (m_inputChannels > m_pluginChannels) {
|
cannam@64
|
238
|
cannam@64
|
239 if (m_pluginChannels == 1) {
|
cannam@64
|
240 for (size_t j = 0; j < m_blockSize; ++j) {
|
cannam@64
|
241 m_buffer[0][j] = inputBuffers[0][j];
|
cannam@64
|
242 }
|
cannam@64
|
243 for (size_t i = 1; i < m_inputChannels; ++i) {
|
cannam@64
|
244 for (size_t j = 0; j < m_blockSize; ++j) {
|
cannam@64
|
245 m_buffer[0][j] += inputBuffers[i][j];
|
cannam@64
|
246 }
|
cannam@64
|
247 }
|
cannam@64
|
248 for (size_t j = 0; j < m_blockSize; ++j) {
|
cannam@64
|
249 m_buffer[0][j] /= m_inputChannels;
|
cannam@64
|
250 }
|
cannam@64
|
251 return m_plugin->process(m_buffer, timestamp);
|
cannam@64
|
252 } else {
|
cannam@64
|
253 return m_plugin->process(inputBuffers, timestamp);
|
cannam@64
|
254 }
|
cannam@64
|
255
|
cannam@64
|
256 } else {
|
cannam@64
|
257
|
cannam@64
|
258 return m_plugin->process(inputBuffers, timestamp);
|
cannam@64
|
259 }
|
cannam@64
|
260 }
|
cannam@64
|
261
|
cannam@64
|
262 }
|
cannam@64
|
263
|
cannam@64
|
264 }
|
cannam@64
|
265
|
cannam@64
|
266
|