annotate rdf/generator/template-generator.cpp @ 217:f9b4f60280db

* improve usage printout & command line parsing a bit
author cannam
date Wed, 29 Oct 2008 15:03:32 +0000
parents 27cfae2a4155
children 81ffb843c8cc
rev   line source
cannam@138 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@138 2
cannam@138 3 #include "vamp-sdk/PluginHostAdapter.h"
cannam@138 4 #include "vamp-sdk/hostext/PluginChannelAdapter.h"
cannam@138 5 #include "vamp-sdk/hostext/PluginInputDomainAdapter.h"
cannam@138 6 #include "vamp-sdk/hostext/PluginLoader.h"
cannam@138 7 #include "vamp/vamp.h"
cannam@138 8
cannam@138 9 #include <iostream>
cannam@138 10 #include <fstream>
cannam@138 11 #include <sstream>
cannam@138 12 #include <sndfile.h>
cannam@138 13
cannam@138 14 #include <cmath>
cannam@138 15
cannam@138 16 using std::cout;
cannam@138 17 using std::cin;
cannam@138 18 using std::cerr;
cannam@138 19 using std::getline;
cannam@138 20 using std::endl;
cannam@138 21 using std::string;
cannam@138 22 using std::vector;
cannam@138 23 using std::ofstream;
cannam@138 24 using std::ios;
cannam@138 25
cannam@138 26 using Vamp::HostExt::PluginLoader;
cannam@138 27 using Vamp::Plugin;
cannam@138 28
cannam@217 29 //???
cannam@138 30 string programURI = "http://www.vamp-plugins.org/doap.rdf#template-generator";
cannam@138 31
cannam@138 32 void usage()
cannam@138 33 {
cannam@217 34 cerr << endl;
cannam@217 35 cerr << "template-generator: Create a skeleton RDF description file describing a Vamp" << endl;
cannam@217 36 cerr << "plugin library using the Vamp ontology." << endl;
cannam@217 37 cerr << endl;
cannam@217 38 cerr << "Usage:" << endl;
cannam@217 39 cerr << " template-generator -i vamp:soname[:plugin] [vamp:soname[:plugin] ...]" << endl;
cannam@217 40 cerr << " template-generator PLUGIN_BASE_URI [ -m YOUR_URI ] [vamp:]soname[:plugin] [[vamp:]soname[:plugin] ...]" << endl;
cannam@217 41 cerr << endl;
cannam@217 42 cerr << "Example:" << endl;
cannam@217 43 cerr << " template-generator http://vamp-plugins.org/rdf/plugins/ vamp-example-plugins" << endl;
cannam@217 44 cerr << endl;
cannam@138 45 exit(2);
cannam@138 46 }
cannam@138 47
cannam@138 48 template <class T>
cannam@138 49 inline string to_string (const T& t)
cannam@138 50 {
cannam@138 51 std::stringstream ss;
cannam@138 52 ss << t;
cannam@138 53 return ss.str();
cannam@138 54 }
cannam@138 55
cannam@144 56 string describe_namespaces(string pluginBundleBaseURI, string libname)
cannam@138 57 {
cannam@142 58 string res=\
cannam@142 59 "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\
cannam@138 60 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\
cannam@147 61 @prefix vamp: <http://purl.org/ontology/vamp/> .\n\
cannam@144 62 @prefix plugbase: <"+pluginBundleBaseURI+libname+"#> .\n\
cannam@138 63 @prefix owl: <http://www.w3.org/2002/07/owl#> .\n\
cannam@138 64 @prefix dc: <http://purl.org/dc/elements/1.1/> .\n\
cannam@138 65 @prefix af: <http://purl.org/ontology/af/> .\n\
cannam@138 66 @prefix foaf: <http://xmlns.com/foaf/0.1/> .\n\
cannam@138 67 @prefix cc: <http://web.resource.org/cc/> .\n\
cannam@138 68 @prefix : <> .\n\n";
cannam@138 69
cannam@142 70 return res;
cannam@138 71 }
cannam@138 72
cannam@144 73 string describe_doc(string describerURI, string pluginBundleBaseURI,
cannam@144 74 string libname)
cannam@138 75 {
cannam@142 76 string res=\
cannam@217 77 "<> a vamp:PluginDescription ;\n";
cannam@217 78 if (describerURI != "") {
cannam@217 79 res += " foaf:maker <"+describerURI+"> ;\n";
cannam@217 80 }
cannam@217 81 res += "\
cannam@144 82 foaf:maker <"+programURI+"> ;\n\
cannam@144 83 foaf:primaryTopic <"+pluginBundleBaseURI+libname+"> .\n\n";
cannam@142 84 return res;
cannam@138 85 }
cannam@138 86
cannam@138 87
cannam@144 88 string describe_library(string libname, vector<Plugin *> plugins)
cannam@144 89 {
cannam@144 90 string res=\
cannam@144 91 ":"+libname+" a vamp:PluginLibrary ;\n\
cannam@144 92 vamp:identifier \""+libname+"\" ";
cannam@144 93
cannam@144 94 for (size_t i = 0; i < plugins.size(); ++i) {
cannam@144 95 res += "; \n\
cannam@145 96 vamp:available_plugin plugbase:"+plugins[i]->getIdentifier();
cannam@144 97 }
cannam@144 98
cannam@144 99 res += " .\n\n";
cannam@144 100 return res;
cannam@144 101 }
cannam@144 102
cannam@138 103 string describe_plugin(Plugin* plugin)
cannam@138 104 {
cannam@142 105 string res=\
cannam@142 106 "plugbase:"+plugin->getIdentifier()+" a vamp:Plugin ;\n\
cannam@138 107 dc:title \""+plugin->getName()+"\" ;\n\
cannam@139 108 vamp:name \""+plugin->getName()+"\" ;\n\
cannam@138 109 dc:description \""+plugin->getDescription()+"\" ;\n\
cannam@154 110 foaf:maker [ foaf:name \""+plugin->getMaker()+"\" ] ; # FIXME could give plugin author's URI here\n\
cannam@154 111 # cc:license <Place plugin license URI here and uncomment> ; \n\
cannam@138 112 vamp:identifier \""+plugin->getIdentifier()+"\" ;\n\
cannam@138 113 vamp:vamp_API_version vamp:api_version_"+to_string(plugin->getVampApiVersion())+" ;\n\
cannam@138 114 owl:versionInfo \""+to_string(plugin->getPluginVersion())+"\" ;\n";
cannam@142 115 if (plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain)
cannam@142 116 res+=" vamp:input_domain vamp:FrequencyDomain ;\n\n";
cannam@142 117 else
cannam@194 118 res+=" vamp:input_domain vamp:TimeDomain ;\n";
cannam@138 119
cannam@138 120
cannam@142 121 Plugin::ParameterList params = plugin->getParameterDescriptors();
cannam@194 122 if (!params.empty()) res+="\n";
cannam@142 123 for (Plugin::ParameterList::const_iterator i = params.begin(); i != params.end(); i++)
dpastor@156 124 res+=" vamp:parameter plugbase:"+plugin->getIdentifier()+"_param_"+(*i).identifier+" ;\n";
cannam@194 125 if (!params.empty()) res+="\n";
cannam@138 126
cannam@142 127 Plugin::OutputList outputs = plugin->getOutputDescriptors();
cannam@142 128 for (Plugin::OutputList::const_iterator i = outputs.begin(); i!= outputs.end(); i++)
cannam@158 129 res+=" vamp:output plugbase:"+plugin->getIdentifier()+"_output_"+(*i).identifier+" ;\n";
cannam@142 130 res+=" .\n";
cannam@138 131
cannam@142 132 return res;
cannam@138 133 }
cannam@138 134
cannam@144 135 string describe_param(Plugin *plugin, Plugin::ParameterDescriptor p)
cannam@138 136 {
cannam@148 137
cannam@148 138 //FIXME: dc:format and vamp:unit are the same???
dpastor@157 139 //Should be a QUantizedParameter also a Parameter??
cannam@148 140 if(p.isQuantized){
cannam@148 141 string res=\
dpastor@156 142 "plugbase:"+plugin->getIdentifier()+"_param_"+p.identifier+" a vamp:QuantizedParameter ;\n\
cannam@148 143 vamp:identifier \""+p.identifier+"\" ;\n\
cannam@148 144 dc:title \""+p.name+"\" ;\n\
cannam@148 145 dc:format \""+p.unit+"\" ;\n\
cannam@148 146 vamp:min_value "+to_string(p.minValue)+" ;\n\
cannam@148 147 vamp:max_value "+to_string(p.maxValue)+" ;\n\
cannam@148 148 vamp:unit \""+p.unit+"\" ;\n\
cannam@152 149 vamp:quantize_step "+to_string(p.quantizeStep)+" ;\n\
cannam@148 150 vamp:default_value "+to_string(p.defaultValue)+" ;\n\
cannam@148 151 vamp:value_names (";
cannam@148 152
cannam@148 153 unsigned int i;
cannam@148 154 for (i=0; i+1 < p.valueNames.size(); i++)
cannam@148 155 res+=" \""+p.valueNames[i]+"\"";
cannam@148 156 if (i < p.valueNames.size())
cannam@148 157 res+=" \""+p.valueNames[i]+"\"";
cannam@148 158 res+=");\n";
cannam@148 159
cannam@148 160 res+=" .\n";
cannam@148 161
cannam@148 162 return res;
cannam@148 163
cannam@148 164 }else{
cannam@142 165 string res=\
dpastor@156 166 "plugbase:"+plugin->getIdentifier()+"_param_"+p.identifier+" a vamp:Parameter ;\n\
cannam@138 167 vamp:identifier \""+p.identifier+"\" ;\n\
cannam@138 168 dc:title \""+p.name+"\" ;\n\
cannam@138 169 dc:format \""+p.unit+"\" ;\n\
cannam@139 170 vamp:min_value "+to_string(p.minValue)+" ;\n\
cannam@139 171 vamp:max_value "+to_string(p.maxValue)+" ;\n\
cannam@148 172 vamp:unit \""+p.unit+"\" ;\n\
cannam@148 173 vamp:default_value "+to_string(p.defaultValue)+" ;\n\
cannam@148 174 vamp:value_names (";
cannam@148 175
cannam@148 176 unsigned int i;
cannam@148 177 for (i=0; i+1 < p.valueNames.size(); i++)
cannam@148 178 res+=" \""+p.valueNames[i]+"\"";
cannam@148 179 if (i < p.valueNames.size())
cannam@148 180 res+=" \""+p.valueNames[i]+"\"";
cannam@148 181 res+=");\n";
cannam@148 182
cannam@148 183 res+=" .\n";
cannam@148 184
cannam@142 185 return res;
cannam@148 186
cannam@148 187 }
cannam@138 188 }
cannam@138 189
cannam@144 190 string describe_output(Plugin *plugin, Plugin::OutputDescriptor o)
cannam@138 191 {
cannam@138 192
cannam@142 193 //we need to distinguish here between different output types:
cannam@148 194
cannam@148 195 //Quantize or not
cannam@148 196 //KnownExtents or not
cannam@148 197 //Data output classification:
cannam@142 198 //DenseOutput
cannam@142 199 //SparseOutput
cannam@142 200 //TrackLevelOutput
cannam@139 201
cannam@139 202
cannam@168 203 // SparseOutput: variable sample rate. Events are not evenly
cannam@168 204 // spaced so we need to record the time associated with the event
cannam@168 205 // as it its not ensured that we have an event after the next one
cannam@168 206 // (but there is not time to set the duration, it has to be
cannam@168 207 // calculated as the different between 2 different events). The
cannam@168 208 // timestamp must be read.
cannam@139 209
cannam@142 210 string res;
cannam@139 211
cannam@168 212 if (o.sampleType == Plugin::OutputDescriptor::VariableSampleRate ||
cannam@168 213 !o.hasFixedBinCount)
cannam@142 214 {
cannam@139 215
cannam@142 216 res=\
cannam@144 217 "plugbase:"+plugin->getIdentifier()+"_output_"+o.identifier+" a vamp:SparseOutput ;\n\
cannam@139 218 vamp:identifier \""+o.identifier+"\" ;\n\
cannam@139 219 dc:title \""+o.name+"\" ;\n\
cannam@139 220 dc:description \""+o.description+"\" ;\n\
cannam@139 221 vamp:fixed_bin_count \""+(o.hasFixedBinCount == 1 ? "true" : "false")+"\" ;\n\
cannam@139 222 vamp:unit \""+(o.unit)+"\" ;\n";
cannam@139 223
cannam@148 224
cannam@148 225 //another type of output
cannam@148 226 if(o.isQuantized){
cannam@148 227
cannam@148 228 res+=" a vamp:QuantizedOutput ;\n";
cannam@148 229 res+=" vamp:quantize_step "+to_string(o.quantizeStep)+" ;\n";
cannam@148 230 }
cannam@148 231
cannam@148 232 //and yet another type
cannam@148 233 if(o.hasKnownExtents){
cannam@148 234
cannam@148 235 res+=" a vamp:KnownExtentsOutput ;\n";
cannam@148 236 res+=" vamp:min_value "+to_string(o.minValue)+" ;\n";
cannam@148 237 res+=" vamp:max_value "+to_string(o.maxValue)+" ;\n";
cannam@148 238 }
cannam@139 239
cannam@142 240 // FIXME ? Bin names may vary based on plugin setup, so including them here might be misleading...
cannam@142 241 if (o.hasFixedBinCount)
cannam@142 242 {
cannam@144 243 res+=" vamp:bin_count "+to_string(o.binCount)+" ;\n";
cannam@144 244 res+=" vamp:bin_names (";
cannam@138 245
cannam@142 246 unsigned int i;
cannam@142 247 for (i=0; i+1 < o.binNames.size(); i++)
cannam@142 248 res+=" \""+o.binNames[i]+"\"";
cannam@142 249 if (i < o.binNames.size())
cannam@142 250 res+=" \""+o.binNames[i]+"\"";
cannam@142 251 res+=");\n";
cannam@142 252 }
cannam@148 253
cannam@144 254 res+=" vamp:sample_type vamp:VariableSampleRate ;\n";
cannam@142 255 if (o.sampleRate > 0.0f)
cannam@144 256 res+=" vamp:sample_rate "+to_string(o.sampleRate)+" ;\n";
cannam@142 257
cannam@142 258 }
cannam@139 259
cannam@142 260 //If we do not have SparseOutput, then we have DenseOutput. TrackLevelOutput can not be inferred from the plugin directly without actually
cannam@142 261 //running the plugin.
cannam@142 262 else{
cannam@142 263
cannam@142 264 res=\
cannam@144 265 "plugbase:"+plugin->getIdentifier()+"_output_"+o.identifier+" a vamp:DenseOutput ;\n\
cannam@139 266 vamp:identifier \""+o.identifier+"\" ;\n\
cannam@139 267 dc:title \""+o.name+"\" ;\n\
cannam@139 268 dc:description \""+o.description+"\" ;\n\
cannam@139 269 vamp:fixed_bin_count \""+(o.hasFixedBinCount == 1 ? "true" : "false")+"\" ;\n\
cannam@139 270 vamp:unit \""+(o.unit)+"\" ;\n";
cannam@139 271
cannam@139 272
cannam@148 273 //another type of output
cannam@148 274 if(o.isQuantized){
cannam@148 275
cannam@148 276 res+=" a vamp:QuantizedOutput ;\n";
cannam@148 277 res+=" vamp:quantize_step "+to_string(o.quantizeStep)+" ;\n";
cannam@148 278 }
cannam@148 279
cannam@148 280 //and yet another type
cannam@148 281 if(o.hasKnownExtents){
cannam@148 282
cannam@148 283 res+=" a vamp:KnownExtentsOutput ;\n";
cannam@148 284 res+=" vamp:min_value "+to_string(o.minValue)+" ;\n";
cannam@148 285 res+=" vamp:max_value "+to_string(o.maxValue)+" ;\n";
cannam@148 286 }
cannam@139 287
cannam@142 288 // FIXME ? Bin names may vary based on plugin setup, so including them here might be misleading...
cannam@142 289 if (o.hasFixedBinCount)
cannam@142 290 {
cannam@144 291 res+=" vamp:bin_count "+to_string(o.binCount)+" ;\n";
cannam@144 292 res+=" vamp:bin_names (";
cannam@139 293
cannam@142 294 unsigned int i;
cannam@142 295 for (i=0; i+1 < o.binNames.size(); i++)
cannam@142 296 res+=" \""+o.binNames[i]+"\"";
cannam@142 297 if (i < o.binNames.size())
cannam@142 298 res+=" \""+o.binNames[i]+"\"";
cannam@142 299 res+=");\n";
cannam@139 300 }
cannam@139 301
cannam@142 302 else if (o.sampleType == Plugin::OutputDescriptor::FixedSampleRate)
cannam@142 303 {
cannam@144 304 res+=" vamp:sample_type vamp:FixedSampleRate ;\n";
cannam@144 305 res+=" vamp:sample_rate "+to_string(o.sampleRate)+" ;\n";
cannam@142 306 }
cannam@142 307 else if (o.sampleType == Plugin::OutputDescriptor::OneSamplePerStep)
cannam@144 308 res+=" vamp:sample_type vamp:OneSamplePerStep ;\n";
cannam@142 309 else
cannam@142 310 {
cannam@142 311 cerr<<"Incomprehensible sampleType for output descriptor "+o.identifier<<" !"<<endl;
cannam@142 312 exit(1);
cannam@142 313 }
cannam@142 314 }
cannam@142 315
cannam@142 316 //There is no way to know this in advance, but we can use the km a bit for this.
cannam@159 317 res+="# vamp:computes_event_type <Place event type URI here and uncomment> ;\n";
cannam@159 318 res+="# vamp:computes_feature <Place feature attribute URI here and uncomment> ;\n";
cannam@159 319 res+="# vamp:computes_signal_type <Place signal type URI here and uncomment> ;\n";
cannam@142 320 res+=" .\n";
cannam@142 321
cannam@142 322 return res;
cannam@138 323 }
cannam@139 324
cannam@144 325 string describe(vector<Plugin *> plugins, string pluginBundleBaseURI,
cannam@144 326 string describerURI, string libname)
cannam@138 327 {
cannam@144 328 string res = describe_namespaces(pluginBundleBaseURI, libname);
cannam@138 329
cannam@144 330 res += describe_doc(describerURI, pluginBundleBaseURI, libname);
cannam@138 331
cannam@144 332 res += describe_library(libname, plugins);
cannam@144 333
cannam@144 334 for (size_t i = 0; i < plugins.size(); ++i) {
cannam@144 335
cannam@144 336 Plugin *plugin = plugins[i];
cannam@144 337
cannam@144 338 res += describe_plugin(plugin);
cannam@138 339
cannam@144 340 Plugin::ParameterList params = plugin->getParameterDescriptors();
cannam@144 341 for (Plugin::ParameterList::const_iterator i = params.begin(); i != params.end(); i++)
cannam@144 342 res += describe_param(plugin, *i);
cannam@138 343
cannam@144 344 Plugin::OutputList outputs = plugin->getOutputDescriptors();
cannam@144 345 for (Plugin::OutputList::const_iterator i = outputs.begin(); i!= outputs.end(); i++)
cannam@144 346 res += describe_output(plugin, *i);
cannam@144 347 }
cannam@138 348
cannam@142 349 return res;
cannam@138 350 }
cannam@138 351
cannam@138 352 int main(int argc, char **argv)
cannam@138 353 {
cannam@144 354 if (argc < 3) usage();
cannam@138 355
cannam@144 356 bool interactive = false;
cannam@144 357 if (!strcmp(argv[1], "-i")) interactive = true;
cannam@138 358
cannam@217 359 if (!interactive && argc < 3) usage();
cannam@138 360
cannam@138 361 string pluginBundleBaseURI, describerURI;
cannam@144 362
cannam@144 363 int argidx = 2;
cannam@138 364
cannam@144 365 if (!interactive) {
cannam@138 366 pluginBundleBaseURI = argv[1];
cannam@217 367 if (!strcmp(argv[2], "-m")) {
cannam@217 368 if (argc < 5) usage();
cannam@217 369 describerURI = argv[3];
cannam@217 370 argidx = 4;
cannam@217 371 }
cannam@144 372 } else {
cannam@138 373 cerr << "Please enter the base URI for the plugin bundle : ";
cannam@138 374 getline(cin, pluginBundleBaseURI);
cannam@217 375 cerr << "Please enter your URI (empty to omit) : ";
cannam@138 376 getline(cin, describerURI);
cannam@138 377 }
cannam@144 378
cannam@144 379 vector<Plugin *> plugins;
cannam@144 380 string libname;
cannam@144 381
cannam@144 382 PluginLoader *loader = PluginLoader::getInstance();
cannam@144 383
cannam@144 384 while (argidx < argc) {
cannam@144 385
cannam@144 386 string pluginName = argv[argidx];
cannam@144 387
cannam@144 388 if (pluginName.substr(0, 5) == "vamp:") {
cannam@144 389 pluginName = pluginName.substr(5);
cannam@144 390 }
cannam@144 391
cannam@144 392 string mylibname = pluginName.substr(0, pluginName.find(':'));
cannam@144 393
cannam@144 394 if (libname == "") libname = mylibname;
cannam@144 395 else if (libname != mylibname) {
cannam@144 396 cerr << "ERROR: All plugins specified on command line must originate in the same library" << endl;
cannam@144 397 exit(1);
cannam@144 398 }
cannam@144 399
cannam@144 400 if (mylibname == pluginName) { // pluginName is a library, not a plugin
cannam@144 401
cannam@144 402 PluginLoader::PluginKeyList list = loader->listPlugins();
cannam@144 403 for (size_t i = 0; i < list.size(); ++i) {
cannam@144 404 string thislibname = list[i].substr(0, list[i].find(':'));
cannam@144 405 if (thislibname != mylibname) continue;
cannam@144 406 Plugin *plugin = loader->loadPlugin(list[i], 44100);
cannam@144 407 if (!plugin) {
cannam@144 408 cerr << "ERROR: Plugin \"" << list[i] << "\" could not be loaded" << endl;
cannam@144 409 exit(1);
cannam@144 410 }
cannam@144 411 plugins.push_back(plugin);
cannam@144 412 }
cannam@151 413
cannam@151 414 if (plugins.empty()) {
cannam@151 415 cerr << "ERROR: Plugin library \"" << mylibname << "\" does not exist, could not be opened, or contains no plugins" << endl;
cannam@151 416 exit(1);
cannam@151 417 }
cannam@151 418
cannam@144 419 } else { // pluginName is a plugin
cannam@144 420
cannam@144 421 Plugin *plugin = loader->loadPlugin(pluginName, size_t(44100));
cannam@144 422 if (!plugin) {
cannam@144 423 cerr << "ERROR: Plugin \"" << pluginName << "\" could not be loaded" << endl;
cannam@144 424 exit(1);
cannam@144 425 }
cannam@144 426 plugins.push_back(plugin);
cannam@144 427 }
cannam@144 428
cannam@144 429 ++argidx;
cannam@144 430 }
cannam@138 431
cannam@144 432 cout << describe(plugins, pluginBundleBaseURI, describerURI, libname) << endl;
cannam@138 433
cannam@138 434 return 0;
cannam@138 435 }
cannam@138 436
cannam@138 437