annotate host/vamp-simple-host.cpp @ 88:d17b9ca3b8c9

* add -o <file> option to vamp-simple-host
author cannam
date Fri, 12 Oct 2007 09:52:10 +0000
parents af0d86b8b692
children 200a663bace1
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@64 231 (key, sfinfo.samplerate, PluginLoader::ADAPT_ALL);
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@83 248 if (blockSize == 0) blockSize = 1024;
cannam@83 249 if (stepSize == 0) {
cannam@83 250 if (plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
cannam@83 251 stepSize = blockSize/2;
cannam@83 252 } else {
cannam@83 253 stepSize = blockSize;
cannam@83 254 }
cannam@83 255 }
cannam@83 256
cannam@16 257 int channels = sfinfo.channels;
cannam@16 258
cannam@16 259 float *filebuf = new float[blockSize * channels];
cannam@16 260 float **plugbuf = new float*[channels];
cannam@47 261 for (int c = 0; c < channels; ++c) plugbuf[c] = new float[blockSize + 2];
cannam@16 262
cannam@16 263 cerr << "Using block size = " << blockSize << ", step size = "
cannam@16 264 << stepSize << endl;
cannam@16 265
cannam@16 266 int minch = plugin->getMinChannelCount();
cannam@16 267 int maxch = plugin->getMaxChannelCount();
cannam@16 268 cerr << "Plugin accepts " << minch << " -> " << maxch << " channel(s)" << endl;
cannam@64 269 cerr << "Sound file has " << channels << " (will mix/augment if necessary)" << endl;
cannam@16 270
cannam@16 271 Vamp::Plugin::OutputList outputs = plugin->getOutputDescriptors();
cannam@16 272 Vamp::Plugin::OutputDescriptor od;
cannam@16 273
cannam@29 274 int returnValue = 1;
cannam@88 275 int progress = 0;
cannam@29 276
cannam@16 277 if (outputs.empty()) {
cannam@64 278 cerr << "ERROR: Plugin has no outputs!" << endl;
cannam@16 279 goto done;
cannam@16 280 }
cannam@16 281
cannam@64 282 if (outputNo < 0) {
cannam@16 283
cannam@64 284 for (size_t oi = 0; oi < outputs.size(); ++oi) {
cannam@64 285 if (outputs[oi].identifier == output) {
cannam@64 286 outputNo = oi;
cannam@64 287 break;
cannam@64 288 }
cannam@64 289 }
cannam@64 290
cannam@64 291 if (outputNo < 0) {
cannam@64 292 cerr << "ERROR: Non-existent output \"" << output << "\" requested" << endl;
cannam@64 293 goto done;
cannam@64 294 }
cannam@64 295
cannam@64 296 } else {
cannam@64 297
cannam@64 298 if (int(outputs.size()) <= outputNo) {
cannam@64 299 cerr << "ERROR: Output " << outputNo << " requested, but plugin has only " << outputs.size() << " output(s)" << endl;
cannam@64 300 goto done;
cannam@64 301 }
cannam@64 302 }
cannam@64 303
cannam@64 304 od = outputs[outputNo];
cannam@64 305 cerr << "Output is: \"" << od.identifier << "\"" << endl;
cannam@16 306
cannam@29 307 if (!plugin->initialise(channels, stepSize, blockSize)) {
cannam@29 308 cerr << "ERROR: Plugin initialise (channels = " << channels
cannam@29 309 << ", stepSize = " << stepSize << ", blockSize = "
cannam@29 310 << blockSize << ") failed." << endl;
cannam@29 311 goto done;
cannam@29 312 }
cannam@16 313
cannam@16 314 for (size_t i = 0; i < sfinfo.frames; i += stepSize) {
cannam@16 315
cannam@16 316 int count;
cannam@16 317
cannam@16 318 if (sf_seek(sndfile, i, SEEK_SET) < 0) {
cannam@16 319 cerr << "ERROR: sf_seek failed: " << sf_strerror(sndfile) << endl;
cannam@16 320 break;
cannam@16 321 }
cannam@16 322
cannam@16 323 if ((count = sf_readf_float(sndfile, filebuf, blockSize)) < 0) {
cannam@16 324 cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
cannam@16 325 break;
cannam@16 326 }
cannam@16 327
cannam@16 328 for (int c = 0; c < channels; ++c) {
cannam@64 329 int j = 0;
cannam@64 330 while (j < count) {
cannam@64 331 plugbuf[c][j] = filebuf[j * sfinfo.channels + c];
cannam@64 332 ++j;
cannam@64 333 }
cannam@64 334 while (j < blockSize) {
cannam@16 335 plugbuf[c][j] = 0.0f;
cannam@64 336 ++j;
cannam@16 337 }
cannam@16 338 }
cannam@16 339
cannam@16 340 printFeatures
cannam@64 341 (i, sfinfo.samplerate, outputNo, plugin->process
cannam@88 342 (plugbuf, Vamp::RealTime::frame2RealTime(i, sfinfo.samplerate)),
cannam@88 343 out);
cannam@88 344
cannam@88 345 int pp = progress;
cannam@88 346 progress = lrintf((float(i) / sfinfo.frames) * 100.f);
cannam@88 347 if (progress != pp && out) {
cannam@88 348 cerr << "\r" << progress << "%";
cannam@88 349 }
cannam@16 350 }
cannam@88 351 if (out) cerr << "\rDone" << endl;
cannam@16 352
cannam@64 353 printFeatures(sfinfo.frames, sfinfo.samplerate, outputNo,
cannam@88 354 plugin->getRemainingFeatures(), out);
cannam@16 355
cannam@29 356 returnValue = 0;
cannam@29 357
cannam@16 358 done:
cannam@16 359 delete plugin;
cannam@88 360 if (out) {
cannam@88 361 out->close();
cannam@88 362 delete out;
cannam@88 363 }
cannam@16 364 sf_close(sndfile);
cannam@29 365 return returnValue;
cannam@1 366 }
cannam@1 367
cannam@16 368 void
cannam@64 369 printPluginPath(bool verbose)
cannam@40 370 {
cannam@64 371 if (verbose) {
cannam@64 372 cout << "\nVamp plugin search path: ";
cannam@64 373 }
cannam@64 374
cannam@40 375 vector<string> path = Vamp::PluginHostAdapter::getPluginPath();
cannam@40 376 for (size_t i = 0; i < path.size(); ++i) {
cannam@64 377 if (verbose) {
cannam@64 378 cout << "[" << path[i] << "]";
cannam@64 379 } else {
cannam@64 380 cout << path[i] << endl;
cannam@64 381 }
cannam@40 382 }
cannam@64 383
cannam@64 384 if (verbose) cout << endl;
cannam@40 385 }
cannam@40 386
cannam@40 387 void
cannam@40 388 enumeratePlugins()
cannam@40 389 {
cannam@64 390 PluginLoader *loader = PluginLoader::getInstance();
cannam@64 391
cannam@64 392 cout << "\nVamp plugin libraries found in search path:" << endl;
cannam@64 393
cannam@64 394 std::vector<PluginLoader::PluginKey> plugins = loader->listPlugins();
cannam@64 395 typedef std::multimap<std::string, PluginLoader::PluginKey>
cannam@64 396 LibraryMap;
cannam@64 397 LibraryMap libraryMap;
cannam@64 398
cannam@64 399 for (size_t i = 0; i < plugins.size(); ++i) {
cannam@64 400 std::string path = loader->getLibraryPathForPlugin(plugins[i]);
cannam@64 401 libraryMap.insert(LibraryMap::value_type(path, plugins[i]));
cannam@64 402 }
cannam@64 403
cannam@64 404 std::string prevPath = "";
cannam@64 405 int index = 0;
cannam@64 406
cannam@64 407 for (LibraryMap::iterator i = libraryMap.begin();
cannam@64 408 i != libraryMap.end(); ++i) {
cannam@64 409
cannam@64 410 std::string path = i->first;
cannam@64 411 PluginLoader::PluginKey key = i->second;
cannam@64 412
cannam@64 413 if (path != prevPath) {
cannam@64 414 prevPath = path;
cannam@64 415 index = 0;
cannam@64 416 cout << "\n " << path << ":" << endl;
cannam@40 417 }
cannam@64 418
cannam@64 419 Vamp::Plugin *plugin = loader->loadPlugin(key, 48000);
cannam@64 420 if (plugin) {
cannam@64 421
cannam@64 422 char c = char('A' + index);
cannam@64 423 if (c > 'Z') c = char('a' + (index - 26));
cannam@64 424
cannam@64 425 cout << " [" << c << "] [v"
cannam@64 426 << plugin->getVampApiVersion() << "] "
cannam@64 427 << plugin->getName() << ", \""
cannam@64 428 << plugin->getIdentifier() << "\"" << " ["
cannam@64 429 << plugin->getMaker() << "]" << endl;
cannam@64 430
cannam@64 431 PluginLoader::PluginCategoryHierarchy category =
cannam@64 432 loader->getPluginCategory(key);
cannam@64 433 if (!category.empty()) {
cannam@64 434 cout << " ";
cannam@64 435 for (size_t ci = 0; ci < category.size(); ++ci) {
cannam@64 436 cout << " > " << category[ci];
cannam@64 437 }
cannam@64 438 cout << endl;
cannam@47 439 }
cannam@64 440
cannam@64 441 if (plugin->getDescription() != "") {
cannam@64 442 cout << " - " << plugin->getDescription() << endl;
cannam@40 443 }
cannam@64 444
cannam@64 445 Vamp::Plugin::OutputList outputs =
cannam@64 446 plugin->getOutputDescriptors();
cannam@64 447
cannam@64 448 if (outputs.size() > 1) {
cannam@64 449 for (size_t j = 0; j < outputs.size(); ++j) {
cannam@64 450 cout << " (" << j << ") "
cannam@64 451 << outputs[j].name << ", \""
cannam@64 452 << outputs[j].identifier << "\"" << endl;
cannam@64 453 if (outputs[j].description != "") {
cannam@64 454 cout << " - "
cannam@64 455 << outputs[j].description << endl;
cannam@40 456 }
cannam@40 457 }
cannam@64 458 }
cannam@64 459
cannam@64 460 ++index;
cannam@64 461
cannam@64 462 delete plugin;
cannam@40 463 }
cannam@40 464 }
cannam@64 465
cannam@64 466 cout << endl;
cannam@40 467 }
cannam@40 468
cannam@40 469 void
cannam@88 470 printFeatures(int frame, int sr, int output,
cannam@88 471 Vamp::Plugin::FeatureSet features, ofstream *out)
cannam@16 472 {
cannam@16 473 for (unsigned int i = 0; i < features[output].size(); ++i) {
cannam@88 474
cannam@16 475 Vamp::RealTime rt = Vamp::RealTime::frame2RealTime(frame, sr);
cannam@88 476
cannam@16 477 if (features[output][i].hasTimestamp) {
cannam@16 478 rt = features[output][i].timestamp;
cannam@16 479 }
cannam@88 480
cannam@88 481 (out ? *out : cout) << rt.toString() << ":";
cannam@88 482
cannam@16 483 for (unsigned int j = 0; j < features[output][i].values.size(); ++j) {
cannam@88 484 (out ? *out : cout) << " " << features[output][i].values[j];
cannam@16 485 }
cannam@88 486
cannam@88 487 (out ? *out : cout) << endl;
cannam@16 488 }
cannam@16 489 }
cannam@16 490
cannam@16 491
cannam@16 492