annotate host/vamp-simple-host.cpp @ 58:0284955e31e5 host-factory-stuff

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