comparison rdf/generator/template-generator.cpp @ 138:147de5e64d28

* Update vamp.n3/rdf from km-rdf * Add template generator (old jerrell version) from sv1 repository * Add provisional RDF descriptions for example plugins
author cannam
date Thu, 19 Jun 2008 09:37:31 +0000
parents
children bb46b8bd213a
comparison
equal deleted inserted replaced
137:dbab8c3a6571 138:147de5e64d28
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 #include "vamp-sdk/PluginHostAdapter.h"
4 #include "vamp-sdk/hostext/PluginChannelAdapter.h"
5 #include "vamp-sdk/hostext/PluginInputDomainAdapter.h"
6 #include "vamp-sdk/hostext/PluginLoader.h"
7 #include "vamp/vamp.h"
8
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 #include <sndfile.h>
13
14 #include <cmath>
15
16 using std::cout;
17 using std::cin;
18 using std::cerr;
19 using std::getline;
20 using std::endl;
21 using std::string;
22 using std::vector;
23 using std::ofstream;
24 using std::ios;
25
26 using Vamp::HostExt::PluginLoader;
27 using Vamp::Plugin;
28
29 /*
30
31 usage:
32
33 template-generator vamp:aubioonset:onsets
34
35 */
36
37 string programURI = "http://www.vamp-plugins.org/doap.rdf#template-generator";
38
39 void usage()
40 {
41 cerr << "usage: template-generator [PLUGIN_BASE_URI YOUR_URI] vamp:soname:plugin[:output]" << endl;
42 exit(2);
43 }
44
45 template <class T>
46 inline string to_string (const T& t)
47 {
48 std::stringstream ss;
49 ss << t;
50 return ss.str();
51 }
52
53 string describe_namespaces(Plugin* plugin, string pluginBundleBaseURI)
54 {
55 string res=\
56 "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\
57 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\
58 @prefix vamp: <http://www.vamp-plugins.org/ontology/> .\n\
59 @prefix vampex: <http://www.vamp-plugins.org/examples/> .\n\
60 @prefix plugbase: <"+pluginBundleBaseURI+"> .\n\
61 @prefix owl: <http://www.w3.org/2002/07/owl#> .\n\
62 @prefix dc: <http://purl.org/dc/elements/1.1/> .\n\
63 @prefix af: <http://purl.org/ontology/af/> .\n\
64 @prefix foaf: <http://xmlns.com/foaf/0.1/> .\n\
65 @prefix cc: <http://web.resource.org/cc/> .\n\
66 @prefix thisplug: <"+pluginBundleBaseURI+plugin->getIdentifier()+"#> .\n\
67 @prefix : <> .\n\n";
68
69 return res;
70 }
71
72 string describe_doc(Plugin* plugin, string describerURI)
73 {
74 string res=\
75 "<> a vamp:PluginDescription ;\n\
76 foaf:maker <"+describerURI+"> ;\n\
77 foaf:maker <"+programURI+"> ;\n\
78 foaf:primaryTopic plugbase:"+plugin->getIdentifier()+" .\n\n";
79 return res;
80 }
81
82
83 string describe_plugin(Plugin* plugin)
84 {
85 string res=\
86 "plugbase:"+plugin->getIdentifier()+" a vamp:Plugin ;\n\
87 dc:title \""+plugin->getName()+"\" ;\n\
88 dc:description \""+plugin->getDescription()+"\" ;\n\
89 foaf:maker [ foaf:name \""+plugin->getMaker()+"\"] ; # FIXME could give plugin author's URI here\n\
90 cc:license <FIXME license for the plugin> ; \n\
91 vamp:identifier \""+plugin->getIdentifier()+"\" ;\n\
92 vamp:vamp_API_version vamp:api_version_"+to_string(plugin->getVampApiVersion())+" ;\n\
93 owl:versionInfo \""+to_string(plugin->getPluginVersion())+"\" ;\n";
94 if (plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain)
95 res+=" vamp:input_domain vamp:FrequencyDomain ;\n\n";
96 else
97 res+=" vamp:input_domain vamp:TimeDomain ;\n\n";
98
99
100 Plugin::ParameterList params = plugin->getParameterDescriptors();
101 for (Plugin::ParameterList::const_iterator i = params.begin(); i != params.end(); i++)
102 res+=" vamp:parameter_descriptor thisplug:param_"+(*i).identifier+" ;\n";
103 res+="\n";
104
105 Plugin::OutputList outputs = plugin->getOutputDescriptors();
106 for (Plugin::OutputList::const_iterator i = outputs.begin(); i!= outputs.end(); i++)
107 res+=" vamp:output_descriptor thisplug:output_"+(*i).identifier+" ;\n";
108 res+=" .\n";
109
110 return res;
111 }
112
113 string describe_param(Plugin::ParameterDescriptor p)
114 {
115 string res=\
116 "thisplug:param_"+p.identifier+" a vamp:ParameterDescriptor ;\n\
117 vamp:identifier \""+p.identifier+"\" ;\n\
118 dc:title \""+p.name+"\" ;\n\
119 dc:format \""+p.unit+"\" ;\n\
120 vamp:minValue "+to_string(p.minValue)+" ;\n\
121 vamp:maxValue "+to_string(p.maxValue)+" ;\n\
122 vamp:defaultValue "+to_string(p.defaultValue)+" .\n\n";
123 return res;
124 }
125
126 string describe_output(Plugin::OutputDescriptor o)
127 {
128 string res=\
129 "thisplug:output_"+o.identifier+" a vamp:OutputDescriptor ;\n\
130 vamp:identifier \""+o.identifier+"\" ;\n\
131 dc:title \""+o.name+"\" ;\n\
132 vamp:fixed_bin_count \""+(o.hasFixedBinCount == 1 ? "true" : "false")+"\" ;\n";
133
134 // FIXME ? Bin names may vary based on plugin setup, so including them here might be misleading...
135 if (o.hasFixedBinCount)
136 {
137 res+=" vamp:bin_count "+to_string(o.binCount)+" ;\n";
138 res+=" vamp:bin_names (";
139
140 unsigned int i;
141 for (i=0; i+1 < o.binNames.size(); i++)
142 res+=" \""+o.binNames[i]+"\"";
143 if (i < o.binNames.size())
144 res+=" \""+o.binNames[i]+"\"";
145 res+=");\n";
146 }
147
148 if (o.sampleType == Plugin::OutputDescriptor::VariableSampleRate)
149 {
150 res+=" vamp:sample_type vamp:VariableSampleRate ;\n";
151 if (o.sampleRate > 0.0f)
152 res+=" vamp:sample_rate "+to_string(o.sampleRate)+" ;\n";
153 }
154 else if (o.sampleType == Plugin::OutputDescriptor::FixedSampleRate)
155 {
156 res+=" vamp:sample_type vamp:FixedSampleRate ;\n";
157 res+=" vamp:sample_rate "+to_string(o.sampleRate)+" ;\n";
158 }
159 else if (o.sampleType == Plugin::OutputDescriptor::OneSamplePerStep)
160 res+=" vamp:sample_type vamp:OneSamplePerStep ;\n";
161 else
162 {
163 cerr<<"Incomprehensible sampleType for output descriptor "+o.identifier<<" !"<<endl;
164 exit(1);
165 }
166
167 res+=" vamp:computes_feature_type <FIXME feature type URI> ;\n";
168 res+=" vamp:computes_event_type <FIXME event type URI> ;\n";
169 res+=" .\n";
170
171 return res;
172 }
173 string describe(Plugin* plugin, string pluginBundleBaseURI, string describerURI)
174 {
175 string res = describe_namespaces(plugin, pluginBundleBaseURI);
176
177 res += describe_doc(plugin, describerURI);
178
179 res += describe_plugin(plugin);
180
181 Plugin::ParameterList params = plugin->getParameterDescriptors();
182 for (Plugin::ParameterList::const_iterator i = params.begin(); i != params.end(); i++)
183 res += describe_param(*i);
184
185 Plugin::OutputList outputs = plugin->getOutputDescriptors();
186 for (Plugin::OutputList::const_iterator i = outputs.begin(); i!= outputs.end(); i++)
187 res += describe_output(*i);
188
189 return res;
190 }
191
192 int main(int argc, char **argv)
193 {
194 if (argc != 2 && argc != 4) usage();
195
196 std::string pluginName = argv[argc-1];
197
198 if (pluginName.substr(0, 5) == "vamp:") {
199 pluginName = pluginName.substr(5);
200 }
201
202 Vamp::Plugin *plugin = PluginLoader::getInstance()->loadPlugin
203 (pluginName, size_t(44100), PluginLoader::ADAPT_ALL_SAFE);
204
205 if (!plugin) {
206 cerr << "ERROR: Plugin \"" << pluginName << "\" could not be loaded" << endl;
207 exit(1);
208 }
209
210 string pluginBundleBaseURI, describerURI;
211
212 if (argc == 4)
213 {
214 pluginBundleBaseURI = argv[1];
215 describerURI = argv[2];
216 }
217 else
218 {
219 cerr << "Please enter the base URI for the plugin bundle : ";
220 getline(cin, pluginBundleBaseURI);
221 cerr << "Please enter your URI : ";
222 getline(cin, describerURI);
223 }
224
225 cout << describe(plugin, pluginBundleBaseURI, describerURI) << endl;
226
227 return 0;
228 }
229
230