annotate host/vamp-simple-host.cpp @ 84:af0d86b8b692

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