comparison src/vamp-hostsdk/PluginChannelAdapter.cpp @ 233:521734d2b498 distinct-libraries

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