cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: VampPluginSDK: vamp-simple-host.cpp Source File cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50:
cannam@50: cannam@50: cannam@50:
cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: cannam@50:
cannam@50:
VampPluginSDK cannam@50:  2.1 cannam@50:
cannam@50: cannam@50:
cannam@50:
cannam@50: cannam@50: cannam@50: cannam@50: cannam@50:
cannam@50:
cannam@50: cannam@50:
cannam@50:
cannam@50:
cannam@50: cannam@50:
cannam@50:
cannam@50:
cannam@50:
vamp-simple-host.cpp
cannam@50:
cannam@50:
cannam@50: Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
cannam@50: 00002 
cannam@50: 00003 /*
cannam@50: 00004     Vamp
cannam@50: 00005 
cannam@50: 00006     An API for audio analysis and feature extraction plugins.
cannam@50: 00007 
cannam@50: 00008     Centre for Digital Music, Queen Mary, University of London.
cannam@50: 00009     Copyright 2006 Chris Cannam, copyright 2007-2008 QMUL.
cannam@50: 00010   
cannam@50: 00011     Permission is hereby granted, free of charge, to any person
cannam@50: 00012     obtaining a copy of this software and associated documentation
cannam@50: 00013     files (the "Software"), to deal in the Software without
cannam@50: 00014     restriction, including without limitation the rights to use, copy,
cannam@50: 00015     modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@50: 00016     of the Software, and to permit persons to whom the Software is
cannam@50: 00017     furnished to do so, subject to the following conditions:
cannam@50: 00018 
cannam@50: 00019     The above copyright notice and this permission notice shall be
cannam@50: 00020     included in all copies or substantial portions of the Software.
cannam@50: 00021 
cannam@50: 00022     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@50: 00023     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@50: 00024     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@50: 00025     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@50: 00026     ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@50: 00027     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@50: 00028     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@50: 00029 
cannam@50: 00030     Except as contained in this notice, the names of the Centre for
cannam@50: 00031     Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@50: 00032     shall not be used in advertising or otherwise to promote the sale,
cannam@50: 00033     use or other dealings in this Software without prior written
cannam@50: 00034     authorization.
cannam@50: 00035 */
cannam@50: 00036 
cannam@50: 00037 
cannam@50: 00038 /*
cannam@50: 00039  * This "simple" Vamp plugin host is no longer as simple as it was; it
cannam@50: 00040  * now has a lot of options and includes a lot of code to handle the
cannam@50: 00041  * various useful listing modes it supports.
cannam@50: 00042  *
cannam@50: 00043  * However, the runPlugin function still contains a reasonable
cannam@50: 00044  * implementation of a fairly generic Vamp plugin host capable of
cannam@50: 00045  * evaluating a given output on a given plugin for a sound file read
cannam@50: 00046  * via libsndfile.
cannam@50: 00047  */
cannam@50: 00048 
cannam@50: 00049 #include <vamp-hostsdk/PluginHostAdapter.h>
cannam@50: 00050 #include <vamp-hostsdk/PluginInputDomainAdapter.h>
cannam@50: 00051 #include <vamp-hostsdk/PluginLoader.h>
cannam@50: 00052 
cannam@50: 00053 #include <iostream>
cannam@50: 00054 #include <fstream>
cannam@50: 00055 #include <set>
cannam@50: 00056 #include <sndfile.h>
cannam@50: 00057 
cannam@50: 00058 #include <cstring>
cannam@50: 00059 #include <cstdlib>
cannam@50: 00060 
cannam@50: 00061 #include "system.h"
cannam@50: 00062 
cannam@50: 00063 #include <cmath>
cannam@50: 00064 
cannam@50: 00065 using namespace std;
cannam@50: 00066 
cannam@50: 00067 using Vamp::Plugin;
cannam@50: 00068 using Vamp::PluginHostAdapter;
cannam@50: 00069 using Vamp::RealTime;
cannam@50: 00070 using Vamp::HostExt::PluginLoader;
cannam@50: 00071 using Vamp::HostExt::PluginWrapper;
cannam@50: 00072 using Vamp::HostExt::PluginInputDomainAdapter;
cannam@50: 00073 
cannam@50: 00074 #define HOST_VERSION "1.4"
cannam@50: 00075 
cannam@50: 00076 enum Verbosity {
cannam@50: 00077     PluginIds,
cannam@50: 00078     PluginOutputIds,
cannam@50: 00079     PluginInformation,
cannam@50: 00080     PluginInformationDetailed
cannam@50: 00081 };
cannam@50: 00082 
cannam@50: 00083 void printFeatures(int, int, int, Plugin::FeatureSet, ofstream *, bool frames);
cannam@50: 00084 void transformInput(float *, size_t);
cannam@50: 00085 void fft(unsigned int, bool, double *, double *, double *, double *);
cannam@50: 00086 void printPluginPath(bool verbose);
cannam@50: 00087 void printPluginCategoryList();
cannam@50: 00088 void enumeratePlugins(Verbosity);
cannam@50: 00089 void listPluginsInLibrary(string soname);
cannam@50: 00090 int runPlugin(string myname, string soname, string id, string output,
cannam@50: 00091               int outputNo, string inputFile, string outfilename, bool frames);
cannam@50: 00092 
cannam@50: 00093 void usage(const char *name)
cannam@50: 00094 {
cannam@50: 00095     cerr << "\n"
cannam@50: 00096          << name << ": A command-line host for Vamp audio analysis plugins.\n\n"
cannam@50: 00097         "Centre for Digital Music, Queen Mary, University of London.\n"
cannam@50: 00098         "Copyright 2006-2009 Chris Cannam and QMUL.\n"
cannam@50: 00099         "Freely redistributable; published under a BSD-style license.\n\n"
cannam@50: 00100         "Usage:\n\n"
cannam@50: 00101         "  " << name << " [-s] pluginlibrary[." << PLUGIN_SUFFIX << "]:plugin[:output] file.wav [-o out.txt]\n"
cannam@50: 00102         "  " << name << " [-s] pluginlibrary[." << PLUGIN_SUFFIX << "]:plugin file.wav [outputno] [-o out.txt]\n\n"
cannam@50: 00103         "    -- Load plugin id \"plugin\" from \"pluginlibrary\" and run it on the\n"
cannam@50: 00104         "       audio data in \"file.wav\", retrieving the named \"output\", or output\n"
cannam@50: 00105         "       number \"outputno\" (the first output by default) and dumping it to\n"
cannam@50: 00106         "       standard output, or to \"out.txt\" if the -o option is given.\n\n"
cannam@50: 00107         "       \"pluginlibrary\" should be a library name, not a file path; the\n"
cannam@50: 00108         "       standard Vamp library search path will be used to locate it.  If\n"
cannam@50: 00109         "       a file path is supplied, the directory part(s) will be ignored.\n\n"
cannam@50: 00110         "       If the -s option is given, results will be labelled with the audio\n"
cannam@50: 00111         "       sample frame at which they occur. Otherwise, they will be labelled\n"
cannam@50: 00112         "       with time in seconds.\n\n"
cannam@50: 00113         "  " << name << " -l\n"
cannam@50: 00114         "  " << name << " --list\n\n"
cannam@50: 00115         "    -- List the plugin libraries and Vamp plugins in the library search path\n"
cannam@50: 00116         "       in a verbose human-readable format.\n\n"
cannam@50: 00117         "  " << name << " --list-full\n\n"
cannam@50: 00118         "    -- List all data reported by all the Vamp plugins in the library search\n"
cannam@50: 00119         "       path in a very verbose human-readable format.\n\n"
cannam@50: 00120         "  " << name << " --list-ids\n\n"
cannam@50: 00121         "    -- List the plugins in the search path in a terse machine-readable format,\n"
cannam@50: 00122         "       in the form vamp:soname:identifier.\n\n"
cannam@50: 00123         "  " << name << " --list-outputs\n\n"
cannam@50: 00124         "    -- List the outputs for plugins in the search path in a machine-readable\n"
cannam@50: 00125         "       format, in the form vamp:soname:identifier:output.\n\n"
cannam@50: 00126         "  " << name << " --list-by-category\n\n"
cannam@50: 00127         "    -- List the plugins as a plugin index by category, in a machine-readable\n"
cannam@50: 00128         "       format.  The format may change in future releases.\n\n"
cannam@50: 00129         "  " << name << " -p\n\n"
cannam@50: 00130         "    -- Print out the Vamp library search path.\n\n"
cannam@50: 00131         "  " << name << " -v\n\n"
cannam@50: 00132         "    -- Display version information only.\n"
cannam@50: 00133          << endl;
cannam@50: 00134     exit(2);
cannam@50: 00135 }
cannam@50: 00136 
cannam@50: 00137 int main(int argc, char **argv)
cannam@50: 00138 {
cannam@50: 00139     char *scooter = argv[0];
cannam@50: 00140     char *name = 0;
cannam@50: 00141     while (scooter && *scooter) {
cannam@50: 00142         if (*scooter == '/' || *scooter == '\\') name = ++scooter;
cannam@50: 00143         else ++scooter;
cannam@50: 00144     }
cannam@50: 00145     if (!name || !*name) name = argv[0];
cannam@50: 00146     
cannam@50: 00147     if (argc < 2) usage(name);
cannam@50: 00148 
cannam@50: 00149     if (argc == 2) {
cannam@50: 00150 
cannam@50: 00151         if (!strcmp(argv[1], "-v")) {
cannam@50: 00152 
cannam@50: 00153             cout << "Simple Vamp plugin host version: " << HOST_VERSION << endl
cannam@50: 00154                  << "Vamp API version: " << VAMP_API_VERSION << endl
cannam@50: 00155                  << "Vamp SDK version: " << VAMP_SDK_VERSION << endl;
cannam@50: 00156             return 0;
cannam@50: 00157 
cannam@50: 00158         } else if (!strcmp(argv[1], "-l") || !strcmp(argv[1], "--list")) {
cannam@50: 00159 
cannam@50: 00160             printPluginPath(true);
cannam@50: 00161             enumeratePlugins(PluginInformation);
cannam@50: 00162             return 0;
cannam@50: 00163 
cannam@50: 00164         } else if (!strcmp(argv[1], "--list-full")) {
cannam@50: 00165 
cannam@50: 00166             enumeratePlugins(PluginInformationDetailed);
cannam@50: 00167             return 0;
cannam@50: 00168 
cannam@50: 00169         } else if (!strcmp(argv[1], "-p")) {
cannam@50: 00170 
cannam@50: 00171             printPluginPath(false);
cannam@50: 00172             return 0;
cannam@50: 00173 
cannam@50: 00174         } else if (!strcmp(argv[1], "--list-ids")) {
cannam@50: 00175 
cannam@50: 00176             enumeratePlugins(PluginIds);
cannam@50: 00177             return 0;
cannam@50: 00178 
cannam@50: 00179         } else if (!strcmp(argv[1], "--list-outputs")) {
cannam@50: 00180 
cannam@50: 00181             enumeratePlugins(PluginOutputIds);
cannam@50: 00182             return 0;
cannam@50: 00183 
cannam@50: 00184         } else if (!strcmp(argv[1], "--list-by-category")) {
cannam@50: 00185 
cannam@50: 00186             printPluginCategoryList();
cannam@50: 00187             return 0;
cannam@50: 00188 
cannam@50: 00189         } else usage(name);
cannam@50: 00190     }
cannam@50: 00191 
cannam@50: 00192     if (argc < 3) usage(name);
cannam@50: 00193 
cannam@50: 00194     bool useFrames = false;
cannam@50: 00195     
cannam@50: 00196     int base = 1;
cannam@50: 00197     if (!strcmp(argv[1], "-s")) {
cannam@50: 00198         useFrames = true;
cannam@50: 00199         base = 2;
cannam@50: 00200     }
cannam@50: 00201 
cannam@50: 00202     string soname = argv[base];
cannam@50: 00203     string wavname = argv[base+1];
cannam@50: 00204     string plugid = "";
cannam@50: 00205     string output = "";
cannam@50: 00206     int outputNo = -1;
cannam@50: 00207     string outfilename;
cannam@50: 00208 
cannam@50: 00209     if (argc >= base+3) {
cannam@50: 00210 
cannam@50: 00211         int idx = base+2;
cannam@50: 00212 
cannam@50: 00213         if (isdigit(*argv[idx])) {
cannam@50: 00214             outputNo = atoi(argv[idx++]);
cannam@50: 00215         }
cannam@50: 00216 
cannam@50: 00217         if (argc == idx + 2) {
cannam@50: 00218             if (!strcmp(argv[idx], "-o")) {
cannam@50: 00219                 outfilename = argv[idx+1];
cannam@50: 00220             } else usage(name);
cannam@50: 00221         } else if (argc != idx) {
cannam@50: 00222             (usage(name));
cannam@50: 00223         }
cannam@50: 00224     }
cannam@50: 00225 
cannam@50: 00226     cerr << endl << name << ": Running..." << endl;
cannam@50: 00227 
cannam@50: 00228     cerr << "Reading file: \"" << wavname << "\", writing to ";
cannam@50: 00229     if (outfilename == "") {
cannam@50: 00230         cerr << "standard output" << endl;
cannam@50: 00231     } else {
cannam@50: 00232         cerr << "\"" << outfilename << "\"" << endl;
cannam@50: 00233     }
cannam@50: 00234 
cannam@50: 00235     string::size_type sep = soname.find(':');
cannam@50: 00236 
cannam@50: 00237     if (sep != string::npos) {
cannam@50: 00238         plugid = soname.substr(sep + 1);
cannam@50: 00239         soname = soname.substr(0, sep);
cannam@50: 00240 
cannam@50: 00241         sep = plugid.find(':');
cannam@50: 00242         if (sep != string::npos) {
cannam@50: 00243             output = plugid.substr(sep + 1);
cannam@50: 00244             plugid = plugid.substr(0, sep);
cannam@50: 00245         }
cannam@50: 00246     }
cannam@50: 00247 
cannam@50: 00248     if (plugid == "") {
cannam@50: 00249         usage(name);
cannam@50: 00250     }
cannam@50: 00251 
cannam@50: 00252     if (output != "" && outputNo != -1) {
cannam@50: 00253         usage(name);
cannam@50: 00254     }
cannam@50: 00255 
cannam@50: 00256     if (output == "" && outputNo == -1) {
cannam@50: 00257         outputNo = 0;
cannam@50: 00258     }
cannam@50: 00259 
cannam@50: 00260     return runPlugin(name, soname, plugid, output, outputNo,
cannam@50: 00261                      wavname, outfilename, useFrames);
cannam@50: 00262 }
cannam@50: 00263 
cannam@50: 00264 
cannam@50: 00265 int runPlugin(string myname, string soname, string id,
cannam@50: 00266               string output, int outputNo, string wavname,
cannam@50: 00267               string outfilename, bool useFrames)
cannam@50: 00268 {
cannam@50: 00269     PluginLoader *loader = PluginLoader::getInstance();
cannam@50: 00270 
cannam@50: 00271     PluginLoader::PluginKey key = loader->composePluginKey(soname, id);
cannam@50: 00272     
cannam@50: 00273     SNDFILE *sndfile;
cannam@50: 00274     SF_INFO sfinfo;
cannam@50: 00275     memset(&sfinfo, 0, sizeof(SF_INFO));
cannam@50: 00276 
cannam@50: 00277     sndfile = sf_open(wavname.c_str(), SFM_READ, &sfinfo);
cannam@50: 00278     if (!sndfile) {
cannam@50: 00279         cerr << myname << ": ERROR: Failed to open input file \""
cannam@50: 00280              << wavname << "\": " << sf_strerror(sndfile) << endl;
cannam@50: 00281         return 1;
cannam@50: 00282     }
cannam@50: 00283 
cannam@50: 00284     ofstream *out = 0;
cannam@50: 00285     if (outfilename != "") {
cannam@50: 00286         out = new ofstream(outfilename.c_str(), ios::out);
cannam@50: 00287         if (!*out) {
cannam@50: 00288             cerr << myname << ": ERROR: Failed to open output file \""
cannam@50: 00289                  << outfilename << "\" for writing" << endl;
cannam@50: 00290             delete out;
cannam@50: 00291             return 1;
cannam@50: 00292         }
cannam@50: 00293     }
cannam@50: 00294 
cannam@50: 00295     Plugin *plugin = loader->loadPlugin
cannam@50: 00296         (key, sfinfo.samplerate, PluginLoader::ADAPT_ALL_SAFE);
cannam@50: 00297     if (!plugin) {
cannam@50: 00298         cerr << myname << ": ERROR: Failed to load plugin \"" << id
cannam@50: 00299              << "\" from library \"" << soname << "\"" << endl;
cannam@50: 00300         sf_close(sndfile);
cannam@50: 00301         if (out) {
cannam@50: 00302             out->close();
cannam@50: 00303             delete out;
cannam@50: 00304         }
cannam@50: 00305         return 1;
cannam@50: 00306     }
cannam@50: 00307 
cannam@50: 00308     cerr << "Running plugin: \"" << plugin->getIdentifier() << "\"..." << endl;
cannam@50: 00309 
cannam@50: 00310     // Note that the following would be much simpler if we used a
cannam@50: 00311     // PluginBufferingAdapter as well -- i.e. if we had passed
cannam@50: 00312     // PluginLoader::ADAPT_ALL to loader->loadPlugin() above, instead
cannam@50: 00313     // of ADAPT_ALL_SAFE.  Then we could simply specify our own block
cannam@50: 00314     // size, keep the step size equal to the block size, and ignore
cannam@50: 00315     // the plugin's bleatings.  However, there are some issues with
cannam@50: 00316     // using a PluginBufferingAdapter that make the results sometimes
cannam@50: 00317     // technically different from (if effectively the same as) the
cannam@50: 00318     // un-adapted plugin, so we aren't doing that here.  See the
cannam@50: 00319     // PluginBufferingAdapter documentation for details.
cannam@50: 00320 
cannam@50: 00321     int blockSize = plugin->getPreferredBlockSize();
cannam@50: 00322     int stepSize = plugin->getPreferredStepSize();
cannam@50: 00323 
cannam@50: 00324     if (blockSize == 0) {
cannam@50: 00325         blockSize = 1024;
cannam@50: 00326     }
cannam@50: 00327     if (stepSize == 0) {
cannam@50: 00328         if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
cannam@50: 00329             stepSize = blockSize/2;
cannam@50: 00330         } else {
cannam@50: 00331             stepSize = blockSize;
cannam@50: 00332         }
cannam@50: 00333     } else if (stepSize > blockSize) {
cannam@50: 00334         cerr << "WARNING: stepSize " << stepSize << " > blockSize " << blockSize << ", resetting blockSize to ";
cannam@50: 00335         if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
cannam@50: 00336             blockSize = stepSize * 2;
cannam@50: 00337         } else {
cannam@50: 00338             blockSize = stepSize;
cannam@50: 00339         }
cannam@50: 00340         cerr << blockSize << endl;
cannam@50: 00341     }
cannam@50: 00342     int overlapSize = blockSize - stepSize;
cannam@50: 00343     sf_count_t currentStep = 0;
cannam@50: 00344     int finalStepsRemaining = max(1, (blockSize / stepSize) - 1); // at end of file, this many part-silent frames needed after we hit EOF
cannam@50: 00345 
cannam@50: 00346     int channels = sfinfo.channels;
cannam@50: 00347 
cannam@50: 00348     float *filebuf = new float[blockSize * channels];
cannam@50: 00349     float **plugbuf = new float*[channels];
cannam@50: 00350     for (int c = 0; c < channels; ++c) plugbuf[c] = new float[blockSize + 2];
cannam@50: 00351 
cannam@50: 00352     cerr << "Using block size = " << blockSize << ", step size = "
cannam@50: 00353               << stepSize << endl;
cannam@50: 00354 
cannam@50: 00355     // The channel queries here are for informational purposes only --
cannam@50: 00356     // a PluginChannelAdapter is being used automatically behind the
cannam@50: 00357     // scenes, and it will take case of any channel mismatch
cannam@50: 00358 
cannam@50: 00359     int minch = plugin->getMinChannelCount();
cannam@50: 00360     int maxch = plugin->getMaxChannelCount();
cannam@50: 00361     cerr << "Plugin accepts " << minch << " -> " << maxch << " channel(s)" << endl;
cannam@50: 00362     cerr << "Sound file has " << channels << " (will mix/augment if necessary)" << endl;
cannam@50: 00363 
cannam@50: 00364     Plugin::OutputList outputs = plugin->getOutputDescriptors();
cannam@50: 00365     Plugin::OutputDescriptor od;
cannam@50: 00366 
cannam@50: 00367     int returnValue = 1;
cannam@50: 00368     int progress = 0;
cannam@50: 00369 
cannam@50: 00370     RealTime rt;
cannam@50: 00371     PluginWrapper *wrapper = 0;
cannam@50: 00372     RealTime adjustment = RealTime::zeroTime;
cannam@50: 00373 
cannam@50: 00374     if (outputs.empty()) {
cannam@50: 00375         cerr << "ERROR: Plugin has no outputs!" << endl;
cannam@50: 00376         goto done;
cannam@50: 00377     }
cannam@50: 00378 
cannam@50: 00379     if (outputNo < 0) {
cannam@50: 00380 
cannam@50: 00381         for (size_t oi = 0; oi < outputs.size(); ++oi) {
cannam@50: 00382             if (outputs[oi].identifier == output) {
cannam@50: 00383                 outputNo = oi;
cannam@50: 00384                 break;
cannam@50: 00385             }
cannam@50: 00386         }
cannam@50: 00387 
cannam@50: 00388         if (outputNo < 0) {
cannam@50: 00389             cerr << "ERROR: Non-existent output \"" << output << "\" requested" << endl;
cannam@50: 00390             goto done;
cannam@50: 00391         }
cannam@50: 00392 
cannam@50: 00393     } else {
cannam@50: 00394 
cannam@50: 00395         if (int(outputs.size()) <= outputNo) {
cannam@50: 00396             cerr << "ERROR: Output " << outputNo << " requested, but plugin has only " << outputs.size() << " output(s)" << endl;
cannam@50: 00397             goto done;
cannam@50: 00398         }        
cannam@50: 00399     }
cannam@50: 00400 
cannam@50: 00401     od = outputs[outputNo];
cannam@50: 00402     cerr << "Output is: \"" << od.identifier << "\"" << endl;
cannam@50: 00403 
cannam@50: 00404     if (!plugin->initialise(channels, stepSize, blockSize)) {
cannam@50: 00405         cerr << "ERROR: Plugin initialise (channels = " << channels
cannam@50: 00406              << ", stepSize = " << stepSize << ", blockSize = "
cannam@50: 00407              << blockSize << ") failed." << endl;
cannam@50: 00408         goto done;
cannam@50: 00409     }
cannam@50: 00410 
cannam@50: 00411     wrapper = dynamic_cast<PluginWrapper *>(plugin);
cannam@50: 00412     if (wrapper) {
cannam@50: 00413         // See documentation for
cannam@50: 00414         // PluginInputDomainAdapter::getTimestampAdjustment
cannam@50: 00415         PluginInputDomainAdapter *ida =
cannam@50: 00416             wrapper->getWrapper<PluginInputDomainAdapter>();
cannam@50: 00417         if (ida) adjustment = ida->getTimestampAdjustment();
cannam@50: 00418     }
cannam@50: 00419     
cannam@50: 00420     // Here we iterate over the frames, avoiding asking the numframes in case it's streaming input.
cannam@50: 00421     do {
cannam@50: 00422 
cannam@50: 00423         int count;
cannam@50: 00424 
cannam@50: 00425         if ((blockSize==stepSize) || (currentStep==0)) {
cannam@50: 00426             // read a full fresh block
cannam@50: 00427             if ((count = sf_readf_float(sndfile, filebuf, blockSize)) < 0) {
cannam@50: 00428                 cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
cannam@50: 00429                 break;
cannam@50: 00430             }
cannam@50: 00431             if (count != blockSize) --finalStepsRemaining;
cannam@50: 00432         } else {
cannam@50: 00433             //  otherwise shunt the existing data down and read the remainder.
cannam@50: 00434             memmove(filebuf, filebuf + (stepSize * channels), overlapSize * channels * sizeof(float));
cannam@50: 00435             if ((count = sf_readf_float(sndfile, filebuf + (overlapSize * channels), stepSize)) < 0) {
cannam@50: 00436                 cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
cannam@50: 00437                 break;
cannam@50: 00438             }
cannam@50: 00439             if (count != stepSize) --finalStepsRemaining;
cannam@50: 00440             count += overlapSize;
cannam@50: 00441         }
cannam@50: 00442 
cannam@50: 00443         for (int c = 0; c < channels; ++c) {
cannam@50: 00444             int j = 0;
cannam@50: 00445             while (j < count) {
cannam@50: 00446                 plugbuf[c][j] = filebuf[j * sfinfo.channels + c];
cannam@50: 00447                 ++j;
cannam@50: 00448             }
cannam@50: 00449             while (j < blockSize) {
cannam@50: 00450                 plugbuf[c][j] = 0.0f;
cannam@50: 00451                 ++j;
cannam@50: 00452             }
cannam@50: 00453         }
cannam@50: 00454 
cannam@50: 00455         rt = RealTime::frame2RealTime(currentStep * stepSize, sfinfo.samplerate);
cannam@50: 00456 
cannam@50: 00457         printFeatures
cannam@50: 00458             (RealTime::realTime2Frame(rt + adjustment, sfinfo.samplerate),
cannam@50: 00459              sfinfo.samplerate, outputNo, plugin->process(plugbuf, rt),
cannam@50: 00460              out, useFrames);
cannam@50: 00461 
cannam@50: 00462         if (sfinfo.frames > 0){
cannam@50: 00463             int pp = progress;
cannam@50: 00464             progress = lrintf((float(currentStep * stepSize) / sfinfo.frames) * 100.f);
cannam@50: 00465             if (progress != pp && out) {
cannam@50: 00466                 cerr << "\r" << progress << "%";
cannam@50: 00467             }
cannam@50: 00468         }
cannam@50: 00469 
cannam@50: 00470         ++currentStep;
cannam@50: 00471 
cannam@50: 00472     } while (finalStepsRemaining > 0);
cannam@50: 00473 
cannam@50: 00474     if (out) cerr << "\rDone" << endl;
cannam@50: 00475 
cannam@50: 00476     rt = RealTime::frame2RealTime(currentStep * stepSize, sfinfo.samplerate);
cannam@50: 00477 
cannam@50: 00478     printFeatures(RealTime::realTime2Frame(rt + adjustment, sfinfo.samplerate),
cannam@50: 00479                   sfinfo.samplerate, outputNo,
cannam@50: 00480                   plugin->getRemainingFeatures(), out, useFrames);
cannam@50: 00481 
cannam@50: 00482     returnValue = 0;
cannam@50: 00483 
cannam@50: 00484 done:
cannam@50: 00485     delete plugin;
cannam@50: 00486     if (out) {
cannam@50: 00487         out->close();
cannam@50: 00488         delete out;
cannam@50: 00489     }
cannam@50: 00490     sf_close(sndfile);
cannam@50: 00491     return returnValue;
cannam@50: 00492 }
cannam@50: 00493 
cannam@50: 00494 void
cannam@50: 00495 printFeatures(int frame, int sr, int output,
cannam@50: 00496               Plugin::FeatureSet features, ofstream *out, bool useFrames)
cannam@50: 00497 {
cannam@50: 00498     for (unsigned int i = 0; i < features[output].size(); ++i) {
cannam@50: 00499 
cannam@50: 00500         if (useFrames) {
cannam@50: 00501 
cannam@50: 00502             int displayFrame = frame;
cannam@50: 00503 
cannam@50: 00504             if (features[output][i].hasTimestamp) {
cannam@50: 00505                 displayFrame = RealTime::realTime2Frame
cannam@50: 00506                     (features[output][i].timestamp, sr);
cannam@50: 00507             }
cannam@50: 00508 
cannam@50: 00509             (out ? *out : cout) << displayFrame;
cannam@50: 00510 
cannam@50: 00511             if (features[output][i].hasDuration) {
cannam@50: 00512                 displayFrame = RealTime::realTime2Frame
cannam@50: 00513                     (features[output][i].duration, sr);
cannam@50: 00514                 (out ? *out : cout) << "," << displayFrame;
cannam@50: 00515             }
cannam@50: 00516 
cannam@50: 00517             (out ? *out : cout)  << ":";
cannam@50: 00518 
cannam@50: 00519         } else {
cannam@50: 00520 
cannam@50: 00521             RealTime rt = RealTime::frame2RealTime(frame, sr);
cannam@50: 00522 
cannam@50: 00523             if (features[output][i].hasTimestamp) {
cannam@50: 00524                 rt = features[output][i].timestamp;
cannam@50: 00525             }
cannam@50: 00526 
cannam@50: 00527             (out ? *out : cout) << rt.toString();
cannam@50: 00528 
cannam@50: 00529             if (features[output][i].hasDuration) {
cannam@50: 00530                 rt = features[output][i].duration;
cannam@50: 00531                 (out ? *out : cout) << "," << rt.toString();
cannam@50: 00532             }
cannam@50: 00533 
cannam@50: 00534             (out ? *out : cout) << ":";
cannam@50: 00535         }
cannam@50: 00536 
cannam@50: 00537         for (unsigned int j = 0; j < features[output][i].values.size(); ++j) {
cannam@50: 00538             (out ? *out : cout) << " " << features[output][i].values[j];
cannam@50: 00539         }
cannam@50: 00540         (out ? *out : cout) << " " << features[output][i].label;
cannam@50: 00541 
cannam@50: 00542         (out ? *out : cout) << endl;
cannam@50: 00543     }
cannam@50: 00544 }
cannam@50: 00545 
cannam@50: 00546 void
cannam@50: 00547 printPluginPath(bool verbose)
cannam@50: 00548 {
cannam@50: 00549     if (verbose) {
cannam@50: 00550         cout << "\nVamp plugin search path: ";
cannam@50: 00551     }
cannam@50: 00552 
cannam@50: 00553     vector<string> path = PluginHostAdapter::getPluginPath();
cannam@50: 00554     for (size_t i = 0; i < path.size(); ++i) {
cannam@50: 00555         if (verbose) {
cannam@50: 00556             cout << "[" << path[i] << "]";
cannam@50: 00557         } else {
cannam@50: 00558             cout << path[i] << endl;
cannam@50: 00559         }
cannam@50: 00560     }
cannam@50: 00561 
cannam@50: 00562     if (verbose) cout << endl;
cannam@50: 00563 }
cannam@50: 00564 
cannam@50: 00565 static
cannam@50: 00566 string
cannam@50: 00567 header(string text, int level)
cannam@50: 00568 {
cannam@50: 00569     string out = '\n' + text + '\n';
cannam@50: 00570     for (size_t i = 0; i < text.length(); ++i) {
cannam@50: 00571         out += (level == 1 ? '=' : level == 2 ? '-' : '~');
cannam@50: 00572     }
cannam@50: 00573     out += '\n';
cannam@50: 00574     return out;
cannam@50: 00575 }
cannam@50: 00576 
cannam@50: 00577 void
cannam@50: 00578 enumeratePlugins(Verbosity verbosity)
cannam@50: 00579 {
cannam@50: 00580     PluginLoader *loader = PluginLoader::getInstance();
cannam@50: 00581 
cannam@50: 00582     if (verbosity == PluginInformation) {
cannam@50: 00583         cout << "\nVamp plugin libraries found in search path:" << endl;
cannam@50: 00584     }
cannam@50: 00585 
cannam@50: 00586     vector<PluginLoader::PluginKey> plugins = loader->listPlugins();
cannam@50: 00587     typedef multimap<string, PluginLoader::PluginKey>
cannam@50: 00588         LibraryMap;
cannam@50: 00589     LibraryMap libraryMap;
cannam@50: 00590 
cannam@50: 00591     for (size_t i = 0; i < plugins.size(); ++i) {
cannam@50: 00592         string path = loader->getLibraryPathForPlugin(plugins[i]);
cannam@50: 00593         libraryMap.insert(LibraryMap::value_type(path, plugins[i]));
cannam@50: 00594     }
cannam@50: 00595 
cannam@50: 00596     string prevPath = "";
cannam@50: 00597     int index = 0;
cannam@50: 00598 
cannam@50: 00599     for (LibraryMap::iterator i = libraryMap.begin();
cannam@50: 00600          i != libraryMap.end(); ++i) {
cannam@50: 00601         
cannam@50: 00602         string path = i->first;
cannam@50: 00603         PluginLoader::PluginKey key = i->second;
cannam@50: 00604 
cannam@50: 00605         if (path != prevPath) {
cannam@50: 00606             prevPath = path;
cannam@50: 00607             index = 0;
cannam@50: 00608             if (verbosity == PluginInformation) {
cannam@50: 00609                 cout << "\n  " << path << ":" << endl;
cannam@50: 00610             } else if (verbosity == PluginInformationDetailed) {
cannam@50: 00611                 string::size_type ki = i->second.find(':');
cannam@50: 00612                 string text = "Library \"" + i->second.substr(0, ki) + "\"";
cannam@50: 00613                 cout << "\n" << header(text, 1);
cannam@50: 00614             }
cannam@50: 00615         }
cannam@50: 00616 
cannam@50: 00617         Plugin *plugin = loader->loadPlugin(key, 48000);
cannam@50: 00618         if (plugin) {
cannam@50: 00619 
cannam@50: 00620             char c = char('A' + index);
cannam@50: 00621             if (c > 'Z') c = char('a' + (index - 26));
cannam@50: 00622 
cannam@50: 00623             PluginLoader::PluginCategoryHierarchy category =
cannam@50: 00624                 loader->getPluginCategory(key);
cannam@50: 00625             string catstr;
cannam@50: 00626             if (!category.empty()) {
cannam@50: 00627                 for (size_t ci = 0; ci < category.size(); ++ci) {
cannam@50: 00628                     if (ci > 0) catstr += " > ";
cannam@50: 00629                         catstr += category[ci];
cannam@50: 00630                 }
cannam@50: 00631             }
cannam@50: 00632 
cannam@50: 00633             if (verbosity == PluginInformation) {
cannam@50: 00634 
cannam@50: 00635                 cout << "    [" << c << "] [v"
cannam@50: 00636                      << plugin->getVampApiVersion() << "] "
cannam@50: 00637                      << plugin->getName() << ", \""
cannam@50: 00638                      << plugin->getIdentifier() << "\"" << " ["
cannam@50: 00639                      << plugin->getMaker() << "]" << endl;
cannam@50: 00640                 
cannam@50: 00641                 if (catstr != "") {
cannam@50: 00642                     cout << "       > " << catstr << endl;
cannam@50: 00643                 }
cannam@50: 00644 
cannam@50: 00645                 if (plugin->getDescription() != "") {
cannam@50: 00646                     cout << "        - " << plugin->getDescription() << endl;
cannam@50: 00647                 }
cannam@50: 00648 
cannam@50: 00649             } else if (verbosity == PluginInformationDetailed) {
cannam@50: 00650 
cannam@50: 00651                 cout << header(plugin->getName(), 2);
cannam@50: 00652                 cout << " - Identifier:         "
cannam@50: 00653                      << key << endl;
cannam@50: 00654                 cout << " - Plugin Version:     " 
cannam@50: 00655                      << plugin->getPluginVersion() << endl;
cannam@50: 00656                 cout << " - Vamp API Version:   "
cannam@50: 00657                      << plugin->getVampApiVersion() << endl;
cannam@50: 00658                 cout << " - Maker:              \""
cannam@50: 00659                      << plugin->getMaker() << "\"" << endl;
cannam@50: 00660                 cout << " - Copyright:          \""
cannam@50: 00661                      << plugin->getCopyright() << "\"" << endl;
cannam@50: 00662                 cout << " - Description:        \""
cannam@50: 00663                      << plugin->getDescription() << "\"" << endl;
cannam@50: 00664                 cout << " - Input Domain:       "
cannam@50: 00665                      << (plugin->getInputDomain() == Vamp::Plugin::TimeDomain ?
cannam@50: 00666                          "Time Domain" : "Frequency Domain") << endl;
cannam@50: 00667                 cout << " - Default Step Size:  " 
cannam@50: 00668                      << plugin->getPreferredStepSize() << endl;
cannam@50: 00669                 cout << " - Default Block Size: " 
cannam@50: 00670                      << plugin->getPreferredBlockSize() << endl;
cannam@50: 00671                 cout << " - Minimum Channels:   " 
cannam@50: 00672                      << plugin->getMinChannelCount() << endl;
cannam@50: 00673                 cout << " - Maximum Channels:   " 
cannam@50: 00674                      << plugin->getMaxChannelCount() << endl;
cannam@50: 00675 
cannam@50: 00676             } else if (verbosity == PluginIds) {
cannam@50: 00677                 cout << "vamp:" << key << endl;
cannam@50: 00678             }
cannam@50: 00679             
cannam@50: 00680             Plugin::OutputList outputs =
cannam@50: 00681                 plugin->getOutputDescriptors();
cannam@50: 00682 
cannam@50: 00683             if (verbosity == PluginInformationDetailed) {
cannam@50: 00684 
cannam@50: 00685                 Plugin::ParameterList params = plugin->getParameterDescriptors();
cannam@50: 00686                 for (size_t j = 0; j < params.size(); ++j) {
cannam@50: 00687                     Plugin::ParameterDescriptor &pd(params[j]);
cannam@50: 00688                     cout << "\nParameter " << j+1 << ": \"" << pd.name << "\"" << endl;
cannam@50: 00689                     cout << " - Identifier:         " << pd.identifier << endl;
cannam@50: 00690                     cout << " - Description:        \"" << pd.description << "\"" << endl;
cannam@50: 00691                     if (pd.unit != "") {
cannam@50: 00692                         cout << " - Unit:               " << pd.unit << endl;
cannam@50: 00693                     }
cannam@50: 00694                     cout << " - Range:              ";
cannam@50: 00695                     cout << pd.minValue << " -> " << pd.maxValue << endl;
cannam@50: 00696                     cout << " - Default:            ";
cannam@50: 00697                     cout << pd.defaultValue << endl;
cannam@50: 00698                     if (pd.isQuantized) {
cannam@50: 00699                         cout << " - Quantize Step:      "
cannam@50: 00700                              << pd.quantizeStep << endl;
cannam@50: 00701                     }
cannam@50: 00702                     if (!pd.valueNames.empty()) {
cannam@50: 00703                         cout << " - Value Names:        ";
cannam@50: 00704                         for (size_t k = 0; k < pd.valueNames.size(); ++k) {
cannam@50: 00705                             if (k > 0) cout << ", ";
cannam@50: 00706                             cout << "\"" << pd.valueNames[k] << "\"";
cannam@50: 00707                         }
cannam@50: 00708                         cout << endl;
cannam@50: 00709                     }
cannam@50: 00710                 }
cannam@50: 00711 
cannam@50: 00712                 if (outputs.empty()) {
cannam@50: 00713                     cout << "\n** Note: This plugin reports no outputs!" << endl;
cannam@50: 00714                 }
cannam@50: 00715                 for (size_t j = 0; j < outputs.size(); ++j) {
cannam@50: 00716                     Plugin::OutputDescriptor &od(outputs[j]);
cannam@50: 00717                     cout << "\nOutput " << j+1 << ": \"" << od.name << "\"" << endl;
cannam@50: 00718                     cout << " - Identifier:         " << od.identifier << endl;
cannam@50: 00719                     cout << " - Description:        \"" << od.description << "\"" << endl;
cannam@50: 00720                     if (od.unit != "") {
cannam@50: 00721                         cout << " - Unit:               " << od.unit << endl;
cannam@50: 00722                     }
cannam@50: 00723                     if (od.hasFixedBinCount) {
cannam@50: 00724                         cout << " - Default Bin Count:  " << od.binCount << endl;
cannam@50: 00725                     }
cannam@50: 00726                     if (!od.binNames.empty()) {
cannam@50: 00727                         bool have = false;
cannam@50: 00728                         for (size_t k = 0; k < od.binNames.size(); ++k) {
cannam@50: 00729                             if (od.binNames[k] != "") {
cannam@50: 00730                                 have = true; break;
cannam@50: 00731                             }
cannam@50: 00732                         }
cannam@50: 00733                         if (have) {
cannam@50: 00734                             cout << " - Bin Names:          ";
cannam@50: 00735                             for (size_t k = 0; k < od.binNames.size(); ++k) {
cannam@50: 00736                                 if (k > 0) cout << ", ";
cannam@50: 00737                                 cout << "\"" << od.binNames[k] << "\"";
cannam@50: 00738                             }
cannam@50: 00739                             cout << endl;
cannam@50: 00740                         }
cannam@50: 00741                     }
cannam@50: 00742                     if (od.hasKnownExtents) {
cannam@50: 00743                         cout << " - Default Extents:    ";
cannam@50: 00744                         cout << od.minValue << " -> " << od.maxValue << endl;
cannam@50: 00745                     }
cannam@50: 00746                     if (od.isQuantized) {
cannam@50: 00747                         cout << " - Quantize Step:      "
cannam@50: 00748                              << od.quantizeStep << endl;
cannam@50: 00749                     }
cannam@50: 00750                     cout << " - Sample Type:        "
cannam@50: 00751                          << (od.sampleType ==
cannam@50: 00752                              Plugin::OutputDescriptor::OneSamplePerStep ?
cannam@50: 00753                              "One Sample Per Step" :
cannam@50: 00754                              od.sampleType ==
cannam@50: 00755                              Plugin::OutputDescriptor::FixedSampleRate ?
cannam@50: 00756                              "Fixed Sample Rate" :
cannam@50: 00757                              "Variable Sample Rate") << endl;
cannam@50: 00758                     if (od.sampleType !=
cannam@50: 00759                         Plugin::OutputDescriptor::OneSamplePerStep) {
cannam@50: 00760                         cout << " - Default Rate:       "
cannam@50: 00761                              << od.sampleRate << endl;
cannam@50: 00762                     }
cannam@50: 00763                     cout << " - Has Duration:       "
cannam@50: 00764                          << (od.hasDuration ? "Yes" : "No") << endl;
cannam@50: 00765                 }
cannam@50: 00766             }
cannam@50: 00767 
cannam@50: 00768             if (outputs.size() > 1 || verbosity == PluginOutputIds) {
cannam@50: 00769                 for (size_t j = 0; j < outputs.size(); ++j) {
cannam@50: 00770                     if (verbosity == PluginInformation) {
cannam@50: 00771                         cout << "         (" << j << ") "
cannam@50: 00772                              << outputs[j].name << ", \""
cannam@50: 00773                              << outputs[j].identifier << "\"" << endl;
cannam@50: 00774                         if (outputs[j].description != "") {
cannam@50: 00775                             cout << "             - " 
cannam@50: 00776                                  << outputs[j].description << endl;
cannam@50: 00777                         }
cannam@50: 00778                     } else if (verbosity == PluginOutputIds) {
cannam@50: 00779                         cout << "vamp:" << key << ":" << outputs[j].identifier << endl;
cannam@50: 00780                     }
cannam@50: 00781                 }
cannam@50: 00782             }
cannam@50: 00783 
cannam@50: 00784             ++index;
cannam@50: 00785 
cannam@50: 00786             delete plugin;
cannam@50: 00787         }
cannam@50: 00788     }
cannam@50: 00789 
cannam@50: 00790     if (verbosity == PluginInformation ||
cannam@50: 00791         verbosity == PluginInformationDetailed) {
cannam@50: 00792         cout << endl;
cannam@50: 00793     }
cannam@50: 00794 }
cannam@50: 00795 
cannam@50: 00796 void
cannam@50: 00797 printPluginCategoryList()
cannam@50: 00798 {
cannam@50: 00799     PluginLoader *loader = PluginLoader::getInstance();
cannam@50: 00800 
cannam@50: 00801     vector<PluginLoader::PluginKey> plugins = loader->listPlugins();
cannam@50: 00802 
cannam@50: 00803     set<string> printedcats;
cannam@50: 00804 
cannam@50: 00805     for (size_t i = 0; i < plugins.size(); ++i) {
cannam@50: 00806 
cannam@50: 00807         PluginLoader::PluginKey key = plugins[i];
cannam@50: 00808         
cannam@50: 00809         PluginLoader::PluginCategoryHierarchy category =
cannam@50: 00810             loader->getPluginCategory(key);
cannam@50: 00811 
cannam@50: 00812         Plugin *plugin = loader->loadPlugin(key, 48000);
cannam@50: 00813         if (!plugin) continue;
cannam@50: 00814 
cannam@50: 00815         string catstr = "";
cannam@50: 00816 
cannam@50: 00817         if (category.empty()) catstr = '|';
cannam@50: 00818         else {
cannam@50: 00819             for (size_t j = 0; j < category.size(); ++j) {
cannam@50: 00820                 catstr += category[j];
cannam@50: 00821                 catstr += '|';
cannam@50: 00822                 if (printedcats.find(catstr) == printedcats.end()) {
cannam@50: 00823                     std::cout << catstr << std::endl;
cannam@50: 00824                     printedcats.insert(catstr);
cannam@50: 00825                 }
cannam@50: 00826             }
cannam@50: 00827         }
cannam@50: 00828 
cannam@50: 00829         std::cout << catstr << key << ":::" << plugin->getName() << ":::" << plugin->getMaker() << ":::" << plugin->getDescription() << std::endl;
cannam@50: 00830     }
cannam@50: 00831 }
cannam@50: 00832 
cannam@50: 
cannam@50:
cannam@50: cannam@50: cannam@50: cannam@50: cannam@50: