annotate rdf/generator/template-generator.cpp @ 245:7ed04d09bc8b

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