-
Notifications
You must be signed in to change notification settings - Fork 3
/
VampHost.cpp
290 lines (243 loc) · 8.91 KB
/
VampHost.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
Copyright 2014 British Broadcasting Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-----------------------------------------------------------------------
Vamp
An API for audio analysis and feature extraction plugins.
Centre for Digital Music, Queen Mary, University of London.
Copyright 2006 Chris Cannam, copyright 2007-2008 QMUL.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the names of the Centre for
Digital Music; Queen Mary, University of London; and Chris Cannam
shall not be used in advertising or otherwise to promote the sale,
use or other dealings in this Software without prior written
authorization.
*/
#include "VampHost.h"
VampHost::VampHost(SNDFILE *sndfile_in,
SF_INFO sfinfo,
string soname, // example: qm-vamp-plugins:qm-mfcc
int blockSize_in,
int stepSize_in)
{
useFrames = false;
sndfile = sndfile_in;
sampleRate = sfinfo.samplerate;
channels = sfinfo.channels;
frames = sfinfo.frames;
// parse plugin name
string plugid = "";
string::size_type sep = soname.find(':');
if (sep != string::npos) {
plugid = soname.substr(sep + 1);
soname = soname.substr(0, sep);
}
// if no plugin name is found, throw error
if (plugid == "") {
cerr << "Plugin not found!" << endl;
}
// initiate plugin loading
PluginLoader *loader = PluginLoader::getInstance();
// get key of selected plugin
PluginLoader::PluginKey key = loader->composePluginKey(soname, plugid);
// load plugin with sample rate of .wav file
plugin = loader->loadPlugin
(key, sampleRate, PluginLoader::ADAPT_ALL_SAFE);
// if plugin failed to load, throw error
if (!plugin) {
cerr << "ERROR: Failed to load plugin \"" << plugid
<< "\" from library \"" << soname << "\"" << endl;
exit(1);
}
// set up block size
blockSize = blockSize_in;
if (blockSize == 0) {
blockSize = plugin->getPreferredBlockSize();
}
if (blockSize == 0) {
blockSize = 1024;
}
// set up step size
stepSize = stepSize_in;
if (stepSize == 0) {
stepSize = plugin->getPreferredStepSize();
}
// if no preference given, set step size as half block
// size if freq analysis, otherwise block size
if (stepSize == 0) {
if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
stepSize = blockSize/2;
} else {
stepSize = blockSize;
}
}
// check step size is smaller or equal to block size
if (stepSize > blockSize) {
cerr << "WARNING: stepSize " << stepSize
<< " > blockSize " << blockSize << ", resetting blockSize to ";
if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
blockSize = stepSize * 2;
} else {
blockSize = stepSize;
}
cerr << blockSize << endl;
}
// create buffers for reading blocks
filebuf = new float[blockSize * channels];
plugbuf = new float*[channels];
for (int c = 0; c < channels; ++c) plugbuf[c] = new float[blockSize + 2];
// get list of outputs
Plugin::OutputList outputs = plugin->getOutputDescriptors();
// check for no outputs
if (outputs.empty()) {
cerr << "ERROR: Plugin has no outputs!" << endl;
exit(1);
}
}
VampHost::~VampHost()
{
// clean up
delete[] filebuf;
for (int c = 0; c < channels; ++c) delete[] plugbuf[c];
delete[] plugbuf;
delete plugin;
}
int VampHost::findOutputNumber(string outputName)
{
// get list of outputs
Plugin::OutputList outputs = plugin->getOutputDescriptors();
// return position of output name in list
for (size_t oi = 0; oi < outputs.size(); ++oi) {
if (outputs[oi].identifier == outputName) {
return oi;
}
}
// if not found, return -1
cerr << "ERROR: Non-existent output \"" << outputName
<< "\" requested" << endl;
return -1;
}
int VampHost::run(Plugin::FeatureSet& results)
{
int overlapSize = blockSize - stepSize;
sf_count_t currentStep = 0;
// at end of file, this many part-silent frames needed after we hit EOF
int finalStepsRemaining = max(1, (blockSize / stepSize) - 1);
RealTime rt;
PluginWrapper *wrapper = 0;
RealTime adjustment = RealTime::zeroTime;
// initialise plugin
if (!plugin->initialise(channels, stepSize, blockSize)) {
cerr << "Plugin initialise (channels = " << channels
<< ", stepSize = " << stepSize << ", blockSize = "
<< blockSize << ") failed." << endl;
return 1;
}
// find timestamp adjustment
wrapper = dynamic_cast<PluginWrapper *>(plugin);
if (wrapper) {
// See documentation for
// PluginInputDomainAdapter::getTimestampAdjustment
PluginInputDomainAdapter *ida =
wrapper->getWrapper<PluginInputDomainAdapter>();
if (ida) adjustment = ida->getTimestampAdjustment();
}
// Here we iterate over the frames, avoiding asking the numframes
// in case it's streaming input.
do {
int count;
// if block size matches step size, just read a full block
if ((blockSize==stepSize) || (currentStep==0)) {
if ((count = sf_readf_float(sndfile, filebuf, blockSize)) < 0) {
cerr << "sf_readf_float failed: " << sf_strerror(sndfile)
<< endl;
break;
}
if (count != blockSize) --finalStepsRemaining;
// if different, shunt exiting data and read the remainder
} else {
memmove(filebuf, filebuf + (stepSize * channels),
overlapSize * channels * sizeof(float));
if ((count = sf_readf_float(sndfile, filebuf + (overlapSize *
channels), stepSize)) < 0) {
cerr << "sf_readf_float failed: " << sf_strerror(sndfile)
<< endl;
break;
}
if (count != stepSize) --finalStepsRemaining;
count += overlapSize;
}
// copy data to plugin buffer
for (int c = 0; c < channels; ++c) {
int j = 0;
while (j < count) {
plugbuf[c][j] = filebuf[j * channels + c];
++j;
}
while (j < blockSize) {
plugbuf[c][j] = 0.0f;
++j;
}
}
// show results
rt = RealTime::frame2RealTime(currentStep * stepSize, sampleRate);
Plugin::FeatureSet tmpResults = plugin->process(plugbuf, rt);
for(Plugin::FeatureSet::iterator it = tmpResults.begin();
it != tmpResults.end(); ++it)
{
int key = it->first;
Plugin::FeatureList feats = it->second;
for (unsigned int i=0; i<feats.size(); i++)
results[key].push_back(feats[i]);
}
// count the steps
++currentStep;
} while (finalStepsRemaining > 0);
// show remaining results
rt = RealTime::frame2RealTime(currentStep * stepSize, sampleRate);
Plugin::FeatureSet tmpResults = plugin->getRemainingFeatures();
for(Plugin::FeatureSet::iterator it = tmpResults.begin();
it != tmpResults.end(); ++it)
{
int key = it->first;
Plugin::FeatureList feats = it->second;
for (unsigned int i=0; i<feats.size(); i++)
results[key].push_back(feats[i]);
}
return 0;
}
int VampHost::getBlockSize()
{
return blockSize;
}
int VampHost::getStepSize()
{
return stepSize;
}
void VampHost::setParameter(string name, float value)
{
plugin->setParameter(name, value);
}