annotate host/vamp-simple-host.cpp @ 32:13eae6cc6bac

* Add a function to look up the Vamp plugin path for the current platform. * Add make install stage and pkgconfig files. (This bit is rather Linux-specific.)
author cannam
date Mon, 31 Jul 2006 16:12:37 +0000
parents d97aafa99828
children ae3e47e76d2d
rev   line source
cannam@1 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@1 2
cannam@1 3 /*
cannam@1 4 Vamp
cannam@1 5
cannam@1 6 An API for audio analysis and feature extraction plugins.
cannam@1 7
cannam@1 8 Centre for Digital Music, Queen Mary, University of London.
cannam@1 9 Copyright 2006 Chris Cannam.
cannam@16 10 FFT code from Don Cross's public domain FFT implementation.
cannam@1 11
cannam@1 12 Permission is hereby granted, free of charge, to any person
cannam@1 13 obtaining a copy of this software and associated documentation
cannam@1 14 files (the "Software"), to deal in the Software without
cannam@1 15 restriction, including without limitation the rights to use, copy,
cannam@1 16 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@1 17 of the Software, and to permit persons to whom the Software is
cannam@1 18 furnished to do so, subject to the following conditions:
cannam@1 19
cannam@1 20 The above copyright notice and this permission notice shall be
cannam@1 21 included in all copies or substantial portions of the Software.
cannam@1 22
cannam@1 23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@1 24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@1 25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@6 26 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@1 27 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@1 28 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@1 29 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@1 30
cannam@1 31 Except as contained in this notice, the names of the Centre for
cannam@1 32 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@1 33 shall not be used in advertising or otherwise to promote the sale,
cannam@1 34 use or other dealings in this Software without prior written
cannam@1 35 authorization.
cannam@1 36 */
cannam@1 37
cannam@16 38 #include "PluginHostAdapter.h"
cannam@1 39 #include "vamp.h"
cannam@1 40
cannam@16 41 #include <iostream>
cannam@16 42 #include <sndfile.h>
cannam@1 43
cannam@1 44 #include "system.h"
cannam@1 45
cannam@19 46 #include <cmath>
cannam@19 47
cannam@16 48 using std::cout;
cannam@16 49 using std::cerr;
cannam@16 50 using std::endl;
cannam@16 51 using std::string;
cannam@32 52 using std::vector;
cannam@16 53
cannam@16 54 void printFeatures(int, int, int, Vamp::Plugin::FeatureSet);
cannam@16 55 void transformInput(float *, size_t);
cannam@16 56 void fft(unsigned int, bool, double *, double *, double *, double *);
cannam@16 57
cannam@1 58 /*
cannam@16 59 A very simple Vamp plugin host. Given the name of a plugin
cannam@16 60 library and the name of a sound file on the command line, it loads
cannam@16 61 the first plugin in the library and runs it on the sound file,
cannam@16 62 dumping the plugin's first output to stdout.
cannam@1 63 */
cannam@1 64
cannam@1 65 int main(int argc, char **argv)
cannam@1 66 {
cannam@16 67 if (argc < 2 || argc > 4) {
cannam@16 68 cerr << "Usage: " << argv[0] << " pluginlibrary.so[:plugin] [file.wav] [outputno]" << endl;
cannam@1 69 return 2;
cannam@1 70 }
cannam@1 71
cannam@16 72 cerr << endl << argv[0] << ": Running..." << endl;
cannam@1 73
cannam@32 74 cerr << endl << "Vamp path is set to:" << endl;
cannam@32 75 vector<string> path = Vamp::PluginHostAdapter::getPluginPath();
cannam@32 76 for (size_t i = 0; i < path.size(); ++i) {
cannam@32 77 cerr << "\t" << path[i] << endl;
cannam@32 78 }
cannam@32 79 cerr << "(This program doesn't use the path; just printing it for information)" << endl << endl;
cannam@32 80
cannam@16 81 string soname = argv[1];
cannam@16 82 string plugname = "";
cannam@16 83 string wavname;
cannam@16 84 if (argc >= 3) wavname = argv[2];
cannam@16 85
cannam@20 86 int sep = soname.find(":");
cannam@20 87 if (sep >= 0 && sep < soname.length()) {
cannam@20 88 plugname = soname.substr(sep + 1);
cannam@20 89 soname = soname.substr(0, sep);
cannam@16 90 }
cannam@1 91
cannam@1 92 void *libraryHandle = DLOPEN(soname, RTLD_LAZY);
cannam@1 93
cannam@1 94 if (!libraryHandle) {
cannam@16 95 cerr << argv[0] << ": Failed to open plugin library "
cannam@16 96 << soname << ": " << DLERROR() << endl;
cannam@1 97 return 1;
cannam@1 98 }
cannam@1 99
cannam@16 100 cerr << argv[0] << ": Opened plugin library " << soname << endl;
cannam@1 101
cannam@1 102 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction)
cannam@1 103 DLSYM(libraryHandle, "vampGetPluginDescriptor");
cannam@1 104
cannam@1 105 if (!fn) {
cannam@16 106 cerr << argv[0] << ": No Vamp descriptor function in library "
cannam@16 107 << soname << endl;
cannam@1 108 DLCLOSE(libraryHandle);
cannam@1 109 return 1;
cannam@1 110 }
cannam@1 111
cannam@16 112 cerr << argv[0] << ": Found plugin descriptor function" << endl;
cannam@1 113
cannam@1 114 int index = 0;
cannam@16 115 int plugnumber = -1;
cannam@1 116 const VampPluginDescriptor *descriptor = 0;
cannam@1 117
cannam@1 118 while ((descriptor = fn(index))) {
cannam@1 119
cannam@16 120 Vamp::PluginHostAdapter plugin(descriptor, 48000);
cannam@16 121 cerr << argv[0] << ": Plugin " << (index+1)
cannam@16 122 << " is \"" << plugin.getName() << "\"" << endl;
cannam@16 123
cannam@16 124 if (plugin.getName() == plugname) plugnumber = index;
cannam@1 125
cannam@1 126 ++index;
cannam@1 127 }
cannam@1 128
cannam@16 129 cerr << argv[0] << ": Done\n" << endl;
cannam@16 130
cannam@16 131 if (wavname == "") {
cannam@16 132 DLCLOSE(libraryHandle);
cannam@16 133 return 0;
cannam@16 134 }
cannam@16 135
cannam@16 136 if (plugnumber < 0) {
cannam@16 137 if (plugname != "") {
cannam@16 138 cerr << "ERROR: No such plugin as " << plugname << " in library"
cannam@16 139 << endl;
cannam@16 140 DLCLOSE(libraryHandle);
cannam@16 141 return 0;
cannam@16 142 } else {
cannam@16 143 plugnumber = 0;
cannam@16 144 }
cannam@16 145 }
cannam@16 146
cannam@16 147 descriptor = fn(plugnumber);
cannam@16 148 if (!descriptor) {
cannam@16 149 DLCLOSE(libraryHandle);
cannam@16 150 return 0;
cannam@16 151 }
cannam@16 152
cannam@16 153 SNDFILE *sndfile;
cannam@16 154 SF_INFO sfinfo;
cannam@16 155 memset(&sfinfo, 0, sizeof(SF_INFO));
cannam@16 156
cannam@16 157 sndfile = sf_open(wavname.c_str(), SFM_READ, &sfinfo);
cannam@16 158 if (!sndfile) {
cannam@16 159 cerr << "ERROR: Failed to open input file \"" << wavname << "\": "
cannam@16 160 << sf_strerror(sndfile) << endl;
cannam@16 161 DLCLOSE(libraryHandle);
cannam@16 162 return 1;
cannam@16 163 }
cannam@16 164
cannam@16 165 Vamp::PluginHostAdapter *plugin =
cannam@16 166 new Vamp::PluginHostAdapter(descriptor, sfinfo.samplerate);
cannam@16 167
cannam@16 168 cerr << "Running " << plugin->getName() << "..." << endl;
cannam@16 169
cannam@16 170 int blockSize = plugin->getPreferredBlockSize();
cannam@16 171 int stepSize = plugin->getPreferredStepSize();
cannam@16 172
cannam@16 173 cerr << "Preferred block size = " << blockSize << ", step size = "
cannam@29 174 << stepSize << endl;
cannam@16 175
cannam@16 176 if (blockSize == 0) blockSize = 1024;
cannam@16 177 if (stepSize == 0) stepSize = blockSize;
cannam@16 178
cannam@29 179 bool rightBlockSize = true;
cannam@29 180 if (plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
cannam@29 181 int p = 1, b = blockSize;
cannam@29 182 while (b) {
cannam@29 183 p <<= 1;
cannam@29 184 b >>= 1;
cannam@29 185 }
cannam@29 186 if (p != blockSize * 2) {
cannam@29 187 cerr << "WARNING: Plugin requested non-power-of-two block size of "
cannam@29 188 << blockSize << ",\nwhich is not supported by this host. ";
cannam@29 189 blockSize = p;
cannam@29 190 cerr << "Rounding up to " << blockSize << "." << endl;
cannam@29 191 rightBlockSize = false;
cannam@29 192 }
cannam@29 193 }
cannam@29 194
cannam@16 195 int channels = sfinfo.channels;
cannam@16 196
cannam@16 197 float *filebuf = new float[blockSize * channels];
cannam@16 198 float **plugbuf = new float*[channels];
cannam@16 199 for (int c = 0; c < channels; ++c) plugbuf[c] = new float[blockSize];
cannam@16 200
cannam@16 201 cerr << "Using block size = " << blockSize << ", step size = "
cannam@16 202 << stepSize << endl;
cannam@16 203
cannam@16 204 int minch = plugin->getMinChannelCount();
cannam@16 205 int maxch = plugin->getMaxChannelCount();
cannam@16 206 cerr << "Plugin accepts " << minch << " -> " << maxch << " channel(s)" << endl;
cannam@16 207
cannam@16 208 Vamp::Plugin::OutputList outputs = plugin->getOutputDescriptors();
cannam@16 209 Vamp::Plugin::OutputDescriptor od;
cannam@16 210
cannam@29 211 int returnValue = 1;
cannam@29 212
cannam@16 213 int output = 0;
cannam@16 214 if (argc == 4) output = atoi(argv[3]);
cannam@16 215
cannam@16 216 bool mix = false;
cannam@16 217
cannam@16 218 if (minch > channels || maxch < channels) {
cannam@16 219 if (minch == 1) {
cannam@16 220 cerr << "WARNING: Sound file has " << channels << " channels, mixing down to 1" << endl;
cannam@16 221 mix = true;
cannam@16 222 channels = 1;
cannam@16 223 } else {
cannam@16 224 cerr << "ERROR: Sound file has " << channels << " channels, out of range for plugin" << endl;
cannam@16 225 goto done;
cannam@16 226 }
cannam@16 227 }
cannam@16 228
cannam@16 229 if (outputs.empty()) {
cannam@16 230 cerr << "Plugin has no outputs!" << endl;
cannam@16 231 goto done;
cannam@16 232 }
cannam@16 233
cannam@16 234 if (int(outputs.size()) <= output) {
cannam@16 235 cerr << "Output " << output << " requested, but plugin has only " << outputs.size() << " output(s)" << endl;
cannam@16 236 goto done;
cannam@16 237 }
cannam@16 238
cannam@16 239 od = outputs[output];
cannam@16 240 cerr << "Output is " << od.name << endl;
cannam@16 241
cannam@29 242 if (!plugin->initialise(channels, stepSize, blockSize)) {
cannam@29 243 cerr << "ERROR: Plugin initialise (channels = " << channels
cannam@29 244 << ", stepSize = " << stepSize << ", blockSize = "
cannam@29 245 << blockSize << ") failed." << endl;
cannam@29 246 if (!rightBlockSize) {
cannam@29 247 cerr << "(Probably because I couldn't provide the plugin's preferred block size.)" << endl;
cannam@29 248 }
cannam@29 249 goto done;
cannam@29 250 }
cannam@16 251
cannam@16 252 for (size_t i = 0; i < sfinfo.frames; i += stepSize) {
cannam@16 253
cannam@16 254 int count;
cannam@16 255
cannam@16 256 if (sf_seek(sndfile, i, SEEK_SET) < 0) {
cannam@16 257 cerr << "ERROR: sf_seek failed: " << sf_strerror(sndfile) << endl;
cannam@16 258 break;
cannam@16 259 }
cannam@16 260
cannam@16 261 if ((count = sf_readf_float(sndfile, filebuf, blockSize)) < 0) {
cannam@16 262 cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
cannam@16 263 break;
cannam@16 264 }
cannam@16 265
cannam@16 266 for (int c = 0; c < channels; ++c) {
cannam@16 267 for (int j = 0; j < blockSize; ++j) {
cannam@16 268 plugbuf[c][j] = 0.0f;
cannam@16 269 }
cannam@16 270 }
cannam@16 271
cannam@16 272 for (int c = 0; c < sfinfo.channels; ++c) {
cannam@16 273 int tc = c;
cannam@16 274 if (mix) tc = 0;
cannam@16 275 for (int j = 0; j < blockSize && j < count; ++j) {
cannam@16 276 plugbuf[tc][j] += filebuf[j * channels + c];
cannam@16 277 }
cannam@16 278
cannam@16 279 if (plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
cannam@16 280 transformInput(plugbuf[tc], blockSize);
cannam@16 281 }
cannam@16 282 }
cannam@16 283
cannam@16 284 printFeatures
cannam@16 285 (i, sfinfo.samplerate, output, plugin->process
cannam@16 286 (plugbuf, Vamp::RealTime::frame2RealTime(i, sfinfo.samplerate)));
cannam@16 287 }
cannam@16 288
cannam@16 289 printFeatures(sfinfo.frames, sfinfo.samplerate, output,
cannam@16 290 plugin->getRemainingFeatures());
cannam@16 291
cannam@29 292 returnValue = 0;
cannam@29 293
cannam@16 294 done:
cannam@16 295 delete plugin;
cannam@1 296
cannam@1 297 DLCLOSE(libraryHandle);
cannam@16 298 sf_close(sndfile);
cannam@29 299 return returnValue;
cannam@1 300 }
cannam@1 301
cannam@16 302 void
cannam@16 303 printFeatures(int frame, int sr, int output, Vamp::Plugin::FeatureSet features)
cannam@16 304 {
cannam@16 305 for (unsigned int i = 0; i < features[output].size(); ++i) {
cannam@16 306 Vamp::RealTime rt = Vamp::RealTime::frame2RealTime(frame, sr);
cannam@16 307 if (features[output][i].hasTimestamp) {
cannam@16 308 rt = features[output][i].timestamp;
cannam@16 309 }
cannam@16 310 cout << rt.toString() << ":";
cannam@16 311 for (unsigned int j = 0; j < features[output][i].values.size(); ++j) {
cannam@16 312 cout << " " << features[output][i].values[j];
cannam@16 313 }
cannam@16 314 cout << endl;
cannam@16 315 }
cannam@16 316 }
cannam@16 317
cannam@16 318 void
cannam@16 319 transformInput(float *buffer, size_t size)
cannam@16 320 {
cannam@16 321 double *inbuf = new double[size * 2];
cannam@16 322 double *outbuf = new double[size * 2];
cannam@16 323
cannam@16 324 // Copy across with Hanning window
cannam@16 325 for (size_t i = 0; i < size; ++i) {
cannam@16 326 inbuf[i] = double(buffer[i]) * (0.50 - 0.50 * cos(2 * M_PI * i / size));
cannam@16 327 inbuf[i + size] = 0.0;
cannam@16 328 }
cannam@16 329
cannam@16 330 for (size_t i = 0; i < size/2; ++i) {
cannam@16 331 double temp = inbuf[i];
cannam@16 332 inbuf[i] = inbuf[i + size/2];
cannam@16 333 inbuf[i + size/2] = temp;
cannam@16 334 }
cannam@16 335
cannam@16 336 fft(size, false, inbuf, inbuf + size, outbuf, outbuf + size);
cannam@16 337
cannam@16 338 for (size_t i = 0; i < size/2; ++i) {
cannam@16 339 buffer[i * 2] = outbuf[i];
cannam@16 340 buffer[i * 2 + 1] = outbuf[i + size];
cannam@16 341 }
cannam@16 342
cannam@16 343 delete inbuf;
cannam@16 344 delete outbuf;
cannam@16 345 }
cannam@16 346
cannam@16 347 void
cannam@16 348 fft(unsigned int n, bool inverse, double *ri, double *ii, double *ro, double *io)
cannam@16 349 {
cannam@16 350 if (!ri || !ro || !io) return;
cannam@16 351
cannam@16 352 unsigned int bits;
cannam@16 353 unsigned int i, j, k, m;
cannam@16 354 unsigned int blockSize, blockEnd;
cannam@16 355
cannam@16 356 double tr, ti;
cannam@16 357
cannam@16 358 if (n < 2) return;
cannam@16 359 if (n & (n-1)) return;
cannam@16 360
cannam@16 361 double angle = 2.0 * M_PI;
cannam@16 362 if (inverse) angle = -angle;
cannam@16 363
cannam@16 364 for (i = 0; ; ++i) {
cannam@16 365 if (n & (1 << i)) {
cannam@16 366 bits = i;
cannam@16 367 break;
cannam@16 368 }
cannam@16 369 }
cannam@16 370
cannam@16 371 static unsigned int tableSize = 0;
cannam@16 372 static int *table = 0;
cannam@16 373
cannam@16 374 if (tableSize != n) {
cannam@16 375
cannam@16 376 delete[] table;
cannam@16 377
cannam@16 378 table = new int[n];
cannam@16 379
cannam@16 380 for (i = 0; i < n; ++i) {
cannam@16 381
cannam@16 382 m = i;
cannam@16 383
cannam@16 384 for (j = k = 0; j < bits; ++j) {
cannam@16 385 k = (k << 1) | (m & 1);
cannam@16 386 m >>= 1;
cannam@16 387 }
cannam@16 388
cannam@16 389 table[i] = k;
cannam@16 390 }
cannam@16 391
cannam@16 392 tableSize = n;
cannam@16 393 }
cannam@16 394
cannam@16 395 if (ii) {
cannam@16 396 for (i = 0; i < n; ++i) {
cannam@16 397 ro[table[i]] = ri[i];
cannam@16 398 io[table[i]] = ii[i];
cannam@16 399 }
cannam@16 400 } else {
cannam@16 401 for (i = 0; i < n; ++i) {
cannam@16 402 ro[table[i]] = ri[i];
cannam@16 403 io[table[i]] = 0.0;
cannam@16 404 }
cannam@16 405 }
cannam@16 406
cannam@16 407 blockEnd = 1;
cannam@16 408
cannam@16 409 for (blockSize = 2; blockSize <= n; blockSize <<= 1) {
cannam@16 410
cannam@16 411 double delta = angle / (double)blockSize;
cannam@16 412 double sm2 = -sin(-2 * delta);
cannam@16 413 double sm1 = -sin(-delta);
cannam@16 414 double cm2 = cos(-2 * delta);
cannam@16 415 double cm1 = cos(-delta);
cannam@16 416 double w = 2 * cm1;
cannam@16 417 double ar[3], ai[3];
cannam@16 418
cannam@16 419 for (i = 0; i < n; i += blockSize) {
cannam@16 420
cannam@16 421 ar[2] = cm2;
cannam@16 422 ar[1] = cm1;
cannam@16 423
cannam@16 424 ai[2] = sm2;
cannam@16 425 ai[1] = sm1;
cannam@16 426
cannam@16 427 for (j = i, m = 0; m < blockEnd; j++, m++) {
cannam@16 428
cannam@16 429 ar[0] = w * ar[1] - ar[2];
cannam@16 430 ar[2] = ar[1];
cannam@16 431 ar[1] = ar[0];
cannam@16 432
cannam@16 433 ai[0] = w * ai[1] - ai[2];
cannam@16 434 ai[2] = ai[1];
cannam@16 435 ai[1] = ai[0];
cannam@16 436
cannam@16 437 k = j + blockEnd;
cannam@16 438 tr = ar[0] * ro[k] - ai[0] * io[k];
cannam@16 439 ti = ar[0] * io[k] + ai[0] * ro[k];
cannam@16 440
cannam@16 441 ro[k] = ro[j] - tr;
cannam@16 442 io[k] = io[j] - ti;
cannam@16 443
cannam@16 444 ro[j] += tr;
cannam@16 445 io[j] += ti;
cannam@16 446 }
cannam@16 447 }
cannam@16 448
cannam@16 449 blockEnd = blockSize;
cannam@16 450 }
cannam@16 451
cannam@16 452 if (inverse) {
cannam@16 453
cannam@16 454 double denom = (double)n;
cannam@16 455
cannam@16 456 for (i = 0; i < n; i++) {
cannam@16 457 ro[i] /= denom;
cannam@16 458 io[i] /= denom;
cannam@16 459 }
cannam@16 460 }
cannam@16 461 }
cannam@16 462
cannam@16 463