comparison vamp-sdk/hostext/PluginChannelAdapter.cpp @ 59:fa79c4ec847d host-factory-stuff

* Put hostext stuff in the HostExt sub-namespace * Tidy up system-specific stuff in PluginLoader * Make PluginLoader return a deletion-notifying wrapper which permits the library to be unloaded when no longer in use * Add PluginChannelAdapter * Make vamp-simple-host use PluginChannelAdapter, and use the PluginLoader for plugin-running task. Also some other enhancements to host
author cannam
date Thu, 24 May 2007 15:17:07 +0000
parents
children 97c5ac99d725
comparison
equal deleted inserted replaced
58:0284955e31e5 59:fa79c4ec847d
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 (channels < minch) {
84 m_forwardPtrs = new const float *[minch];
85 if (m_inputChannels > 1) {
86 // We need a set of zero-valued buffers to add to the
87 // forwarded pointers
88 m_buffer = new float*[minch - channels];
89 for (size_t i = 0; i < minch; ++i) {
90 m_buffer[i] = new float[blockSize];
91 for (size_t j = 0; j < blockSize; ++j) {
92 m_buffer[i][j] = 0.f;
93 }
94 }
95 }
96 m_pluginChannels = minch;
97 return m_plugin->initialise(minch, stepSize, blockSize);
98 }
99
100 if (channels > maxch) {
101 // We only need m_buffer if we are mixing down to a single
102 // channel -- otherwise we can just forward the same float* as
103 // passed in to process(), expecting the excess to be ignored
104 if (maxch == 1) {
105 m_buffer = new float *[1];
106 m_buffer[0] = new float[blockSize];
107 }
108 m_pluginChannels = maxch;
109 return m_plugin->initialise(maxch, stepSize, blockSize);
110 }
111
112 m_pluginChannels = channels;
113 return m_plugin->initialise(channels, stepSize, blockSize);
114 }
115
116 PluginChannelAdapter::FeatureSet
117 PluginChannelAdapter::process(const float *const *inputBuffers,
118 RealTime timestamp)
119 {
120 if (m_inputChannels < m_pluginChannels) {
121
122 if (m_inputChannels == 1) {
123 for (size_t i = 0; i < m_pluginChannels; ++i) {
124 m_forwardPtrs[i] = inputBuffers[0];
125 }
126 } else {
127 for (size_t i = 0; i < m_inputChannels; ++i) {
128 m_forwardPtrs[i] = inputBuffers[i];
129 }
130 for (size_t i = m_inputChannels; i < m_pluginChannels; ++i) {
131 m_forwardPtrs[i] = m_buffer[i - m_inputChannels];
132 }
133 }
134
135 return m_plugin->process(m_forwardPtrs, timestamp);
136 }
137
138 if (m_inputChannels > m_pluginChannels) {
139
140 if (m_pluginChannels == 1) {
141 for (size_t j = 0; j < m_blockSize; ++j) {
142 m_buffer[0][j] = inputBuffers[0][j];
143 }
144 for (size_t i = 1; i < m_inputChannels; ++i) {
145 for (size_t j = 0; j < m_blockSize; ++j) {
146 m_buffer[0][j] += inputBuffers[i][j];
147 }
148 }
149 for (size_t j = 0; j < m_blockSize; ++j) {
150 m_buffer[0][j] /= m_inputChannels;
151 }
152 return m_plugin->process(m_buffer, timestamp);
153 } else {
154 return m_plugin->process(inputBuffers, timestamp);
155 }
156 }
157
158 return m_plugin->process(inputBuffers, timestamp);
159 }
160
161 }
162
163 }
164
165