comparison vamp-sdk/hostext/PluginChannelAdapter.cpp @ 64:9d3272c7db60

* Merge from host-factory-stuff branch: this adds several helper classes in the hostext directory that should make a host's life much easier. This will become version 1.1 of the SDK, eventually.
author cannam
date Fri, 01 Jun 2007 15:10:17 +0000
parents
children a712ed15d158
comparison
equal deleted inserted replaced
54:933fee59d33a 64:9d3272c7db60
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 Chris Cannam.
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 "PluginChannelAdapter.h"
38
39 namespace Vamp {
40
41 namespace HostExt {
42
43 PluginChannelAdapter::PluginChannelAdapter(Plugin *plugin) :
44 PluginWrapper(plugin),
45 m_blockSize(0),
46 m_inputChannels(0),
47 m_pluginChannels(0),
48 m_buffer(0),
49 m_forwardPtrs(0)
50 {
51 }
52
53 PluginChannelAdapter::~PluginChannelAdapter()
54 {
55 if (m_buffer) {
56 if (m_inputChannels > m_pluginChannels) {
57 delete[] m_buffer[0];
58 } else {
59 for (size_t i = 0; i < m_pluginChannels - m_inputChannels; ++i) {
60 delete[] m_buffer[i];
61 }
62 }
63 delete[] m_buffer;
64 m_buffer = 0;
65 }
66
67 if (m_forwardPtrs) {
68 delete[] m_forwardPtrs;
69 m_forwardPtrs = 0;
70 }
71 }
72
73 bool
74 PluginChannelAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
75 {
76 m_blockSize = blockSize;
77
78 size_t minch = m_plugin->getMinChannelCount();
79 size_t maxch = m_plugin->getMaxChannelCount();
80
81 m_inputChannels = channels;
82
83 if (m_inputChannels < minch) {
84
85 m_forwardPtrs = new const float *[minch];
86
87 if (m_inputChannels > 1) {
88 // We need a set of zero-valued buffers to add to the
89 // forwarded pointers
90 m_buffer = new float*[minch - channels];
91 for (size_t i = 0; i < minch; ++i) {
92 m_buffer[i] = new float[blockSize];
93 for (size_t j = 0; j < blockSize; ++j) {
94 m_buffer[i][j] = 0.f;
95 }
96 }
97 }
98
99 m_pluginChannels = minch;
100
101 std::cerr << "PluginChannelAdapter::initialise: expanding " << m_inputChannels << " to " << m_pluginChannels << " for plugin" << std::endl;
102
103 } else if (m_inputChannels > maxch) {
104
105 // We only need m_buffer if we are mixing down to a single
106 // channel -- otherwise we can just forward the same float* as
107 // passed in to process(), expecting the excess to be ignored
108
109 if (maxch == 1) {
110 m_buffer = new float *[1];
111 m_buffer[0] = new float[blockSize];
112
113 std::cerr << "PluginChannelAdapter::initialise: mixing " << m_inputChannels << " to mono for plugin" << std::endl;
114
115 } else {
116
117 std::cerr << "PluginChannelAdapter::initialise: reducing " << m_inputChannels << " to " << m_pluginChannels << " for plugin" << std::endl;
118 }
119
120 m_pluginChannels = maxch;
121
122 } else {
123
124 std::cerr << "PluginChannelAdapter::initialise: accepting given number of channels (" << m_inputChannels << ")" << std::endl;
125 m_pluginChannels = m_inputChannels;
126 }
127
128 return m_plugin->initialise(m_pluginChannels, stepSize, blockSize);
129 }
130
131 PluginChannelAdapter::FeatureSet
132 PluginChannelAdapter::process(const float *const *inputBuffers,
133 RealTime timestamp)
134 {
135 std::cerr << "PluginChannelAdapter::process: " << m_inputChannels << " -> " << m_pluginChannels << " channels" << std::endl;
136
137 if (m_inputChannels < m_pluginChannels) {
138
139 if (m_inputChannels == 1) {
140 for (size_t i = 0; i < m_pluginChannels; ++i) {
141 m_forwardPtrs[i] = inputBuffers[0];
142 }
143 } else {
144 for (size_t i = 0; i < m_inputChannels; ++i) {
145 m_forwardPtrs[i] = inputBuffers[i];
146 }
147 for (size_t i = m_inputChannels; i < m_pluginChannels; ++i) {
148 m_forwardPtrs[i] = m_buffer[i - m_inputChannels];
149 }
150 }
151
152 return m_plugin->process(m_forwardPtrs, timestamp);
153
154 } else if (m_inputChannels > m_pluginChannels) {
155
156 if (m_pluginChannels == 1) {
157 for (size_t j = 0; j < m_blockSize; ++j) {
158 m_buffer[0][j] = inputBuffers[0][j];
159 }
160 for (size_t i = 1; i < m_inputChannels; ++i) {
161 for (size_t j = 0; j < m_blockSize; ++j) {
162 m_buffer[0][j] += inputBuffers[i][j];
163 }
164 }
165 for (size_t j = 0; j < m_blockSize; ++j) {
166 m_buffer[0][j] /= m_inputChannels;
167 }
168 return m_plugin->process(m_buffer, timestamp);
169 } else {
170 return m_plugin->process(inputBuffers, timestamp);
171 }
172
173 } else {
174
175 return m_plugin->process(inputBuffers, timestamp);
176 }
177 }
178
179 }
180
181 }
182
183