annotate host/vamp-simple-host.cpp @ 59:fa79c4ec847d host-factory-stuff

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