annotate host/vamp-simple-host.cpp @ 92:c94c066a4897

* Add Mark L's PluginBufferingAdapter
author cannam
date Fri, 02 Nov 2007 14:54:04 +0000
parents 200a663bace1
children cd4425ab7b98
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@64 38 #include "vamp-sdk/PluginHostAdapter.h"
cannam@64 39 #include "vamp-sdk/hostext/PluginChannelAdapter.h"
cannam@64 40 #include "vamp-sdk/hostext/PluginInputDomainAdapter.h"
cannam@64 41 #include "vamp-sdk/hostext/PluginLoader.h"
cannam@64 42 #include "vamp/vamp.h"
cannam@1 43
cannam@16 44 #include <iostream>
cannam@88 45 #include <fstream>
cannam@16 46 #include <sndfile.h>
cannam@1 47
cannam@1 48 #include "system.h"
cannam@1 49
cannam@19 50 #include <cmath>
cannam@19 51
cannam@16 52 using std::cout;
cannam@16 53 using std::cerr;
cannam@16 54 using std::endl;
cannam@16 55 using std::string;
cannam@32 56 using std::vector;
cannam@88 57 using std::ofstream;
cannam@88 58 using std::ios;
cannam@16 59
cannam@64 60 using Vamp::HostExt::PluginLoader;
cannam@64 61
cannam@64 62 #define HOST_VERSION "1.1"
cannam@40 63
cannam@88 64 void printFeatures(int, int, int, Vamp::Plugin::FeatureSet, ofstream *);
cannam@16 65 void transformInput(float *, size_t);
cannam@16 66 void fft(unsigned int, bool, double *, double *, double *, double *);
cannam@64 67 void printPluginPath(bool verbose);
cannam@64 68 void enumeratePlugins();
cannam@64 69 void listPluginsInLibrary(string soname);
cannam@64 70 int runPlugin(string myname, string soname, string id, string output,
cannam@88 71 int outputNo, string inputFile, string outfilename);
cannam@40 72
cannam@64 73 void usage(const char *name)
cannam@64 74 {
cannam@64 75 cerr << "\n"
cannam@64 76 << name << ": A simple Vamp plugin host.\n\n"
cannam@64 77 "Centre for Digital Music, Queen Mary, University of London.\n"
cannam@64 78 "Copyright 2006-2007 Chris Cannam and QMUL.\n"
cannam@64 79 "Freely redistributable; published under a BSD-style license.\n\n"
cannam@64 80 "Usage:\n\n"
cannam@88 81 " " << name << " pluginlibrary[." << PLUGIN_SUFFIX << "]:plugin[:output] file.wav [-o outfile.txt]\n"
cannam@88 82 " " << name << " pluginlibrary[." << PLUGIN_SUFFIX << "]:plugin file.wav [outputno] [-o outfile.txt]\n\n"
cannam@64 83 " -- Load plugin id \"plugin\" from \"pluginlibrary\" and run it on the\n"
cannam@73 84 " audio data in \"file.wav\", retrieving the named \"output\", or output\n"
cannam@73 85 " number \"outputno\" (the first output by default) and dumping it to\n"
cannam@88 86 " standard output, or to \"outfile.txt\" if the -o option is given.\n\n"
cannam@73 87 " \"pluginlibrary\" should be a library name, not a file path; the\n"
cannam@73 88 " standard Vamp library search path will be used to locate it. If\n"
cannam@73 89 " a file path is supplied, the directory part(s) will be ignored.\n\n"
cannam@64 90 " " << name << " -l\n\n"
cannam@73 91 " -- List the plugin libraries and Vamp plugins in the library search path.\n\n"
cannam@64 92 " " << name << " -p\n\n"
cannam@73 93 " -- Print out the Vamp library search path.\n\n"
cannam@64 94 " " << name << " -v\n\n"
cannam@64 95 " -- Display version information only.\n\n"
cannam@64 96 << endl;
cannam@64 97 exit(2);
cannam@64 98 }
cannam@1 99
cannam@1 100 int main(int argc, char **argv)
cannam@1 101 {
cannam@64 102 char *scooter = argv[0];
cannam@64 103 char *name = 0;
cannam@64 104 while (scooter && *scooter) {
cannam@64 105 if (*scooter == '/' || *scooter == '\\') name = ++scooter;
cannam@64 106 else ++scooter;
cannam@1 107 }
cannam@64 108 if (!name || !*name) name = argv[0];
cannam@43 109
cannam@88 110 if (argc < 2) usage(name);
cannam@64 111
cannam@88 112 if (argc == 2) {
cannam@88 113
cannam@88 114 if (!strcmp(argv[1], "-v")) {
cannam@88 115
cannam@88 116 cout << "Simple Vamp plugin host version: " << HOST_VERSION << endl
cannam@88 117 << "Vamp API version: " << VAMP_API_VERSION << endl
cannam@88 118 << "Vamp SDK version: " << VAMP_SDK_VERSION << endl;
cannam@88 119 return 0;
cannam@88 120
cannam@88 121 } else if (!strcmp(argv[1], "-l")) {
cannam@88 122
cannam@88 123 printPluginPath(true);
cannam@88 124 enumeratePlugins();
cannam@88 125 return 0;
cannam@88 126
cannam@88 127 } else if (!strcmp(argv[1], "-p")) {
cannam@88 128
cannam@88 129 printPluginPath(false);
cannam@88 130 return 0;
cannam@88 131
cannam@88 132 } else usage(name);
cannam@64 133 }
cannam@64 134
cannam@88 135 if (argc < 3) usage(name);
cannam@88 136
cannam@88 137 string soname = argv[1];
cannam@88 138 string wavname = argv[2];
cannam@88 139 string plugid = "";
cannam@88 140 string output = "";
cannam@88 141 int outputNo = -1;
cannam@88 142 string outfilename;
cannam@88 143
cannam@88 144 if (argc >= 4) {
cannam@88 145
cannam@88 146 int idx = 3;
cannam@88 147
cannam@88 148 if (isdigit(*argv[idx])) {
cannam@88 149 outputNo = atoi(argv[idx++]);
cannam@88 150 }
cannam@88 151
cannam@88 152 if (argc == idx + 2) {
cannam@88 153 if (!strcmp(argv[idx], "-o")) {
cannam@88 154 outfilename = argv[idx+1];
cannam@88 155 } else usage(name);
cannam@88 156 } else if (argc != idx) {
cannam@88 157 (usage(name));
cannam@88 158 }
cannam@40 159 }
cannam@40 160
cannam@64 161 cerr << endl << name << ": Running..." << endl;
cannam@1 162
cannam@88 163 cerr << "Reading file: \"" << wavname << "\", writing to ";
cannam@88 164 if (outfilename == "") {
cannam@88 165 cerr << "standard output" << endl;
cannam@88 166 } else {
cannam@88 167 cerr << "\"" << outfilename << "\"" << endl;
cannam@88 168 }
cannam@16 169
cannam@64 170 string::size_type sep = soname.find(':');
cannam@64 171
cannam@64 172 if (sep != string::npos) {
cannam@49 173 plugid = soname.substr(sep + 1);
cannam@20 174 soname = soname.substr(0, sep);
cannam@1 175
cannam@64 176 sep = plugid.find(':');
cannam@64 177 if (sep != string::npos) {
cannam@64 178 output = plugid.substr(sep + 1);
cannam@64 179 plugid = plugid.substr(0, sep);
cannam@16 180 }
cannam@16 181 }
cannam@16 182
cannam@64 183 if (plugid == "") {
cannam@64 184 usage(name);
cannam@16 185 }
cannam@64 186
cannam@64 187 if (output != "" && outputNo != -1) {
cannam@64 188 usage(name);
cannam@64 189 }
cannam@64 190
cannam@84 191 if (output == "" && outputNo == -1) {
cannam@84 192 outputNo = 0;
cannam@84 193 }
cannam@84 194
cannam@88 195 return runPlugin(name, soname, plugid, output, outputNo,
cannam@88 196 wavname, outfilename);
cannam@64 197 }
cannam@64 198
cannam@64 199
cannam@64 200 int runPlugin(string myname, string soname, string id,
cannam@88 201 string output, int outputNo, string wavname,
cannam@88 202 string outfilename)
cannam@64 203 {
cannam@64 204 PluginLoader *loader = PluginLoader::getInstance();
cannam@64 205
cannam@64 206 PluginLoader::PluginKey key = loader->composePluginKey(soname, id);
cannam@16 207
cannam@16 208 SNDFILE *sndfile;
cannam@16 209 SF_INFO sfinfo;
cannam@16 210 memset(&sfinfo, 0, sizeof(SF_INFO));
cannam@16 211
cannam@16 212 sndfile = sf_open(wavname.c_str(), SFM_READ, &sfinfo);
cannam@16 213 if (!sndfile) {
cannam@64 214 cerr << myname << ": ERROR: Failed to open input file \""
cannam@64 215 << wavname << "\": " << sf_strerror(sndfile) << endl;
cannam@16 216 return 1;
cannam@16 217 }
cannam@16 218
cannam@88 219 ofstream *out = 0;
cannam@88 220 if (outfilename != "") {
cannam@88 221 out = new ofstream(outfilename.c_str(), ios::out);
cannam@88 222 if (!*out) {
cannam@88 223 cerr << myname << ": ERROR: Failed to open output file \""
cannam@88 224 << outfilename << "\" for writing" << endl;
cannam@88 225 delete out;
cannam@88 226 return 1;
cannam@88 227 }
cannam@88 228 }
cannam@88 229
cannam@64 230 Vamp::Plugin *plugin = loader->loadPlugin
cannam@92 231 (key, sfinfo.samplerate, PluginLoader::ADAPT_ALL_SAFE);
cannam@64 232 if (!plugin) {
cannam@64 233 cerr << myname << ": ERROR: Failed to load plugin \"" << id
cannam@64 234 << "\" from library \"" << soname << "\"" << endl;
cannam@64 235 sf_close(sndfile);
cannam@88 236 if (out) {
cannam@88 237 out->close();
cannam@88 238 delete out;
cannam@88 239 }
cannam@64 240 return 1;
cannam@64 241 }
cannam@16 242
cannam@64 243 cerr << "Running plugin: \"" << plugin->getIdentifier() << "\"..." << endl;
cannam@16 244
cannam@16 245 int blockSize = plugin->getPreferredBlockSize();
cannam@16 246 int stepSize = plugin->getPreferredStepSize();
cannam@16 247
cannam@91 248 if (blockSize == 0) {
cannam@91 249 blockSize = 1024;
cannam@91 250 }
cannam@83 251 if (stepSize == 0) {
cannam@83 252 if (plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
cannam@83 253 stepSize = blockSize/2;
cannam@83 254 } else {
cannam@83 255 stepSize = blockSize;
cannam@83 256 }
cannam@91 257 } else if (stepSize > blockSize) {
cannam@91 258 cerr << "WARNING: stepSize " << stepSize << " > blockSize " << blockSize << ", resetting blockSize to ";
cannam@91 259 if (plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
cannam@91 260 blockSize = stepSize * 2;
cannam@91 261 } else {
cannam@91 262 blockSize = stepSize;
cannam@91 263 }
cannam@91 264 cerr << blockSize << endl;
cannam@83 265 }
cannam@83 266
cannam@16 267 int channels = sfinfo.channels;
cannam@16 268
cannam@16 269 float *filebuf = new float[blockSize * channels];
cannam@16 270 float **plugbuf = new float*[channels];
cannam@47 271 for (int c = 0; c < channels; ++c) plugbuf[c] = new float[blockSize + 2];
cannam@16 272
cannam@16 273 cerr << "Using block size = " << blockSize << ", step size = "
cannam@16 274 << stepSize << endl;
cannam@16 275
cannam@16 276 int minch = plugin->getMinChannelCount();
cannam@16 277 int maxch = plugin->getMaxChannelCount();
cannam@16 278 cerr << "Plugin accepts " << minch << " -> " << maxch << " channel(s)" << endl;
cannam@64 279 cerr << "Sound file has " << channels << " (will mix/augment if necessary)" << endl;
cannam@16 280
cannam@16 281 Vamp::Plugin::OutputList outputs = plugin->getOutputDescriptors();
cannam@16 282 Vamp::Plugin::OutputDescriptor od;
cannam@16 283
cannam@29 284 int returnValue = 1;
cannam@88 285 int progress = 0;
cannam@29 286
cannam@16 287 if (outputs.empty()) {
cannam@64 288 cerr << "ERROR: Plugin has no outputs!" << endl;
cannam@16 289 goto done;
cannam@16 290 }
cannam@16 291
cannam@64 292 if (outputNo < 0) {
cannam@16 293
cannam@64 294 for (size_t oi = 0; oi < outputs.size(); ++oi) {
cannam@64 295 if (outputs[oi].identifier == output) {
cannam@64 296 outputNo = oi;
cannam@64 297 break;
cannam@64 298 }
cannam@64 299 }
cannam@64 300
cannam@64 301 if (outputNo < 0) {
cannam@64 302 cerr << "ERROR: Non-existent output \"" << output << "\" requested" << endl;
cannam@64 303 goto done;
cannam@64 304 }
cannam@64 305
cannam@64 306 } else {
cannam@64 307
cannam@64 308 if (int(outputs.size()) <= outputNo) {
cannam@64 309 cerr << "ERROR: Output " << outputNo << " requested, but plugin has only " << outputs.size() << " output(s)" << endl;
cannam@64 310 goto done;
cannam@64 311 }
cannam@64 312 }
cannam@64 313
cannam@64 314 od = outputs[outputNo];
cannam@64 315 cerr << "Output is: \"" << od.identifier << "\"" << endl;
cannam@16 316
cannam@29 317 if (!plugin->initialise(channels, stepSize, blockSize)) {
cannam@29 318 cerr << "ERROR: Plugin initialise (channels = " << channels
cannam@29 319 << ", stepSize = " << stepSize << ", blockSize = "
cannam@29 320 << blockSize << ") failed." << endl;
cannam@29 321 goto done;
cannam@29 322 }
cannam@16 323
cannam@16 324 for (size_t i = 0; i < sfinfo.frames; i += stepSize) {
cannam@16 325
cannam@16 326 int count;
cannam@16 327
cannam@16 328 if (sf_seek(sndfile, i, SEEK_SET) < 0) {
cannam@16 329 cerr << "ERROR: sf_seek failed: " << sf_strerror(sndfile) << endl;
cannam@16 330 break;
cannam@16 331 }
cannam@16 332
cannam@16 333 if ((count = sf_readf_float(sndfile, filebuf, blockSize)) < 0) {
cannam@16 334 cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
cannam@16 335 break;
cannam@16 336 }
cannam@16 337
cannam@16 338 for (int c = 0; c < channels; ++c) {
cannam@64 339 int j = 0;
cannam@64 340 while (j < count) {
cannam@64 341 plugbuf[c][j] = filebuf[j * sfinfo.channels + c];
cannam@64 342 ++j;
cannam@64 343 }
cannam@64 344 while (j < blockSize) {
cannam@16 345 plugbuf[c][j] = 0.0f;
cannam@64 346 ++j;
cannam@16 347 }
cannam@16 348 }
cannam@16 349
cannam@16 350 printFeatures
cannam@64 351 (i, sfinfo.samplerate, outputNo, plugin->process
cannam@88 352 (plugbuf, Vamp::RealTime::frame2RealTime(i, sfinfo.samplerate)),
cannam@88 353 out);
cannam@88 354
cannam@88 355 int pp = progress;
cannam@88 356 progress = lrintf((float(i) / sfinfo.frames) * 100.f);
cannam@88 357 if (progress != pp && out) {
cannam@88 358 cerr << "\r" << progress << "%";
cannam@88 359 }
cannam@16 360 }
cannam@88 361 if (out) cerr << "\rDone" << endl;
cannam@16 362
cannam@64 363 printFeatures(sfinfo.frames, sfinfo.samplerate, outputNo,
cannam@88 364 plugin->getRemainingFeatures(), out);
cannam@16 365
cannam@29 366 returnValue = 0;
cannam@29 367
cannam@16 368 done:
cannam@16 369 delete plugin;
cannam@88 370 if (out) {
cannam@88 371 out->close();
cannam@88 372 delete out;
cannam@88 373 }
cannam@16 374 sf_close(sndfile);
cannam@29 375 return returnValue;
cannam@1 376 }
cannam@1 377
cannam@16 378 void
cannam@64 379 printPluginPath(bool verbose)
cannam@40 380 {
cannam@64 381 if (verbose) {
cannam@64 382 cout << "\nVamp plugin search path: ";
cannam@64 383 }
cannam@64 384
cannam@40 385 vector<string> path = Vamp::PluginHostAdapter::getPluginPath();
cannam@40 386 for (size_t i = 0; i < path.size(); ++i) {
cannam@64 387 if (verbose) {
cannam@64 388 cout << "[" << path[i] << "]";
cannam@64 389 } else {
cannam@64 390 cout << path[i] << endl;
cannam@64 391 }
cannam@40 392 }
cannam@64 393
cannam@64 394 if (verbose) cout << endl;
cannam@40 395 }
cannam@40 396
cannam@40 397 void
cannam@40 398 enumeratePlugins()
cannam@40 399 {
cannam@64 400 PluginLoader *loader = PluginLoader::getInstance();
cannam@64 401
cannam@64 402 cout << "\nVamp plugin libraries found in search path:" << endl;
cannam@64 403
cannam@64 404 std::vector<PluginLoader::PluginKey> plugins = loader->listPlugins();
cannam@64 405 typedef std::multimap<std::string, PluginLoader::PluginKey>
cannam@64 406 LibraryMap;
cannam@64 407 LibraryMap libraryMap;
cannam@64 408
cannam@64 409 for (size_t i = 0; i < plugins.size(); ++i) {
cannam@64 410 std::string path = loader->getLibraryPathForPlugin(plugins[i]);
cannam@64 411 libraryMap.insert(LibraryMap::value_type(path, plugins[i]));
cannam@64 412 }
cannam@64 413
cannam@64 414 std::string prevPath = "";
cannam@64 415 int index = 0;
cannam@64 416
cannam@64 417 for (LibraryMap::iterator i = libraryMap.begin();
cannam@64 418 i != libraryMap.end(); ++i) {
cannam@64 419
cannam@64 420 std::string path = i->first;
cannam@64 421 PluginLoader::PluginKey key = i->second;
cannam@64 422
cannam@64 423 if (path != prevPath) {
cannam@64 424 prevPath = path;
cannam@64 425 index = 0;
cannam@64 426 cout << "\n " << path << ":" << endl;
cannam@40 427 }
cannam@64 428
cannam@64 429 Vamp::Plugin *plugin = loader->loadPlugin(key, 48000);
cannam@64 430 if (plugin) {
cannam@64 431
cannam@64 432 char c = char('A' + index);
cannam@64 433 if (c > 'Z') c = char('a' + (index - 26));
cannam@64 434
cannam@64 435 cout << " [" << c << "] [v"
cannam@64 436 << plugin->getVampApiVersion() << "] "
cannam@64 437 << plugin->getName() << ", \""
cannam@64 438 << plugin->getIdentifier() << "\"" << " ["
cannam@64 439 << plugin->getMaker() << "]" << endl;
cannam@64 440
cannam@64 441 PluginLoader::PluginCategoryHierarchy category =
cannam@64 442 loader->getPluginCategory(key);
cannam@64 443 if (!category.empty()) {
cannam@64 444 cout << " ";
cannam@64 445 for (size_t ci = 0; ci < category.size(); ++ci) {
cannam@64 446 cout << " > " << category[ci];
cannam@64 447 }
cannam@64 448 cout << endl;
cannam@47 449 }
cannam@64 450
cannam@64 451 if (plugin->getDescription() != "") {
cannam@64 452 cout << " - " << plugin->getDescription() << endl;
cannam@40 453 }
cannam@64 454
cannam@64 455 Vamp::Plugin::OutputList outputs =
cannam@64 456 plugin->getOutputDescriptors();
cannam@64 457
cannam@64 458 if (outputs.size() > 1) {
cannam@64 459 for (size_t j = 0; j < outputs.size(); ++j) {
cannam@64 460 cout << " (" << j << ") "
cannam@64 461 << outputs[j].name << ", \""
cannam@64 462 << outputs[j].identifier << "\"" << endl;
cannam@64 463 if (outputs[j].description != "") {
cannam@64 464 cout << " - "
cannam@64 465 << outputs[j].description << endl;
cannam@40 466 }
cannam@40 467 }
cannam@64 468 }
cannam@64 469
cannam@64 470 ++index;
cannam@64 471
cannam@64 472 delete plugin;
cannam@40 473 }
cannam@40 474 }
cannam@64 475
cannam@64 476 cout << endl;
cannam@40 477 }
cannam@40 478
cannam@40 479 void
cannam@88 480 printFeatures(int frame, int sr, int output,
cannam@88 481 Vamp::Plugin::FeatureSet features, ofstream *out)
cannam@16 482 {
cannam@16 483 for (unsigned int i = 0; i < features[output].size(); ++i) {
cannam@88 484
cannam@16 485 Vamp::RealTime rt = Vamp::RealTime::frame2RealTime(frame, sr);
cannam@88 486
cannam@16 487 if (features[output][i].hasTimestamp) {
cannam@16 488 rt = features[output][i].timestamp;
cannam@16 489 }
cannam@88 490
cannam@88 491 (out ? *out : cout) << rt.toString() << ":";
cannam@88 492
cannam@16 493 for (unsigned int j = 0; j < features[output][i].values.size(); ++j) {
cannam@88 494 (out ? *out : cout) << " " << features[output][i].values[j];
cannam@16 495 }
cannam@88 496
cannam@88 497 (out ? *out : cout) << endl;
cannam@16 498 }
cannam@16 499 }
cannam@16 500
cannam@16 501
cannam@16 502