Chris@439
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@439
|
2
|
Chris@439
|
3 /*
|
Chris@439
|
4 Sonic Visualiser
|
Chris@439
|
5 An audio file viewer and annotation editor.
|
Chris@439
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@439
|
7 This file copyright 2008 QMUL.
|
Chris@439
|
8
|
Chris@439
|
9 This program is free software; you can redistribute it and/or
|
Chris@439
|
10 modify it under the terms of the GNU General Public License as
|
Chris@439
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@439
|
12 License, or (at your option) any later version. See the file
|
Chris@439
|
13 COPYING included with this distribution for more information.
|
Chris@439
|
14 */
|
Chris@439
|
15
|
Chris@439
|
16 #include "RDFImporter.h"
|
Chris@439
|
17
|
Chris@439
|
18 #include <map>
|
Chris@439
|
19 #include <vector>
|
Chris@439
|
20
|
Chris@439
|
21 #include <iostream>
|
Chris@439
|
22 #include <cmath>
|
Chris@439
|
23
|
Chris@439
|
24 #include "SimpleSPARQLQuery.h"
|
Chris@439
|
25
|
Chris@439
|
26 #include "base/ProgressReporter.h"
|
Chris@439
|
27 #include "base/RealTime.h"
|
Chris@439
|
28
|
Chris@439
|
29 #include "data/model/SparseOneDimensionalModel.h"
|
Chris@439
|
30 #include "data/model/SparseTimeValueModel.h"
|
Chris@439
|
31 #include "data/model/EditableDenseThreeDimensionalModel.h"
|
Chris@449
|
32 #include "data/model/NoteModel.h"
|
Chris@510
|
33 #include "data/model/TextModel.h"
|
Chris@449
|
34 #include "data/model/RegionModel.h"
|
Chris@499
|
35 #include "data/model/WaveFileModel.h"
|
Chris@499
|
36
|
Chris@499
|
37 #include "data/fileio/FileSource.h"
|
Chris@520
|
38 #include "data/fileio/CachedFile.h"
|
Chris@581
|
39 #include "data/fileio/FileFinder.h"
|
Chris@522
|
40
|
Chris@439
|
41 using std::cerr;
|
Chris@439
|
42 using std::endl;
|
Chris@439
|
43
|
Chris@439
|
44 class RDFImporterImpl
|
Chris@439
|
45 {
|
Chris@439
|
46 public:
|
Chris@439
|
47 RDFImporterImpl(QString url, int sampleRate);
|
Chris@439
|
48 virtual ~RDFImporterImpl();
|
Chris@490
|
49
|
Chris@490
|
50 void setSampleRate(int sampleRate) { m_sampleRate = sampleRate; }
|
Chris@439
|
51
|
Chris@439
|
52 bool isOK();
|
Chris@439
|
53 QString getErrorString() const;
|
Chris@439
|
54
|
Chris@439
|
55 std::vector<Model *> getDataModels(ProgressReporter *);
|
Chris@439
|
56
|
Chris@439
|
57 protected:
|
Chris@439
|
58 QString m_uristring;
|
Chris@439
|
59 QString m_errorString;
|
Chris@499
|
60 std::map<QString, Model *> m_audioModelMap;
|
Chris@439
|
61 int m_sampleRate;
|
Chris@439
|
62
|
Chris@520
|
63 static bool m_prefixesLoaded;
|
Chris@520
|
64 static void loadPrefixes(ProgressReporter *reporter);
|
Chris@520
|
65
|
Chris@499
|
66 void getDataModelsAudio(std::vector<Model *> &, ProgressReporter *);
|
Chris@440
|
67 void getDataModelsSparse(std::vector<Model *> &, ProgressReporter *);
|
Chris@440
|
68 void getDataModelsDense(std::vector<Model *> &, ProgressReporter *);
|
Chris@440
|
69
|
Chris@493
|
70 void getDenseModelTitle(Model *, QString, QString);
|
Chris@493
|
71
|
Chris@440
|
72 void getDenseFeatureProperties(QString featureUri,
|
Chris@440
|
73 int &sampleRate, int &windowLength,
|
Chris@440
|
74 int &hopSize, int &width, int &height);
|
Chris@440
|
75
|
Chris@449
|
76 void fillModel(Model *, long, long, bool, std::vector<float> &, QString);
|
Chris@439
|
77 };
|
Chris@439
|
78
|
Chris@520
|
79 bool RDFImporterImpl::m_prefixesLoaded = false;
|
Chris@439
|
80
|
Chris@439
|
81 QString
|
Chris@439
|
82 RDFImporter::getKnownExtensions()
|
Chris@439
|
83 {
|
Chris@439
|
84 return "*.rdf *.n3 *.ttl";
|
Chris@439
|
85 }
|
Chris@439
|
86
|
Chris@439
|
87 RDFImporter::RDFImporter(QString url, int sampleRate) :
|
Chris@439
|
88 m_d(new RDFImporterImpl(url, sampleRate))
|
Chris@439
|
89 {
|
Chris@439
|
90 }
|
Chris@439
|
91
|
Chris@439
|
92 RDFImporter::~RDFImporter()
|
Chris@439
|
93 {
|
Chris@439
|
94 delete m_d;
|
Chris@439
|
95 }
|
Chris@439
|
96
|
Chris@490
|
97 void
|
Chris@490
|
98 RDFImporter::setSampleRate(int sampleRate)
|
Chris@490
|
99 {
|
Chris@490
|
100 m_d->setSampleRate(sampleRate);
|
Chris@490
|
101 }
|
Chris@490
|
102
|
Chris@439
|
103 bool
|
Chris@439
|
104 RDFImporter::isOK()
|
Chris@439
|
105 {
|
Chris@439
|
106 return m_d->isOK();
|
Chris@439
|
107 }
|
Chris@439
|
108
|
Chris@439
|
109 QString
|
Chris@439
|
110 RDFImporter::getErrorString() const
|
Chris@439
|
111 {
|
Chris@439
|
112 return m_d->getErrorString();
|
Chris@439
|
113 }
|
Chris@439
|
114
|
Chris@439
|
115 std::vector<Model *>
|
Chris@439
|
116 RDFImporter::getDataModels(ProgressReporter *r)
|
Chris@439
|
117 {
|
Chris@439
|
118 return m_d->getDataModels(r);
|
Chris@439
|
119 }
|
Chris@439
|
120
|
Chris@439
|
121 RDFImporterImpl::RDFImporterImpl(QString uri, int sampleRate) :
|
Chris@439
|
122 m_uristring(uri),
|
Chris@439
|
123 m_sampleRate(sampleRate)
|
Chris@439
|
124 {
|
Chris@439
|
125 }
|
Chris@439
|
126
|
Chris@439
|
127 RDFImporterImpl::~RDFImporterImpl()
|
Chris@439
|
128 {
|
Chris@492
|
129 SimpleSPARQLQuery::closeSingleSource(m_uristring);
|
Chris@439
|
130 }
|
Chris@439
|
131
|
Chris@439
|
132 bool
|
Chris@439
|
133 RDFImporterImpl::isOK()
|
Chris@439
|
134 {
|
Chris@439
|
135 return (m_errorString == "");
|
Chris@439
|
136 }
|
Chris@439
|
137
|
Chris@439
|
138 QString
|
Chris@439
|
139 RDFImporterImpl::getErrorString() const
|
Chris@439
|
140 {
|
Chris@439
|
141 return m_errorString;
|
Chris@439
|
142 }
|
Chris@439
|
143
|
Chris@439
|
144 std::vector<Model *>
|
Chris@439
|
145 RDFImporterImpl::getDataModels(ProgressReporter *reporter)
|
Chris@439
|
146 {
|
Chris@520
|
147 loadPrefixes(reporter);
|
Chris@520
|
148
|
Chris@439
|
149 std::vector<Model *> models;
|
Chris@439
|
150
|
Chris@499
|
151 getDataModelsAudio(models, reporter);
|
Chris@499
|
152
|
Chris@490
|
153 if (m_sampleRate == 0) {
|
Chris@589
|
154 std::cerr << "RDFImporter::getDataModels: invalid sample rate from audio" << std::endl;
|
Chris@490
|
155 return models;
|
Chris@490
|
156 }
|
Chris@490
|
157
|
Chris@508
|
158 QString error;
|
Chris@508
|
159
|
Chris@522
|
160 if (m_errorString != "") {
|
Chris@522
|
161 error = m_errorString;
|
Chris@522
|
162 }
|
Chris@508
|
163 m_errorString = "";
|
Chris@508
|
164
|
Chris@440
|
165 getDataModelsDense(models, reporter);
|
Chris@440
|
166
|
Chris@522
|
167 if (m_errorString != "") {
|
Chris@522
|
168 error = m_errorString;
|
Chris@522
|
169 }
|
Chris@440
|
170 m_errorString = "";
|
Chris@440
|
171
|
Chris@440
|
172 getDataModelsSparse(models, reporter);
|
Chris@440
|
173
|
Chris@522
|
174 if (m_errorString == "" && error != "") {
|
Chris@522
|
175 m_errorString = error;
|
Chris@522
|
176 }
|
Chris@440
|
177
|
Chris@440
|
178 return models;
|
Chris@440
|
179 }
|
Chris@440
|
180
|
Chris@440
|
181 void
|
Chris@499
|
182 RDFImporterImpl::getDataModelsAudio(std::vector<Model *> &models,
|
Chris@499
|
183 ProgressReporter *reporter)
|
Chris@499
|
184 {
|
Chris@589
|
185 SimpleSPARQLQuery query
|
Chris@499
|
186 (SimpleSPARQLQuery::QueryFromSingleSource,
|
Chris@499
|
187 QString
|
Chris@499
|
188 (
|
Chris@499
|
189 " PREFIX mo: <http://purl.org/ontology/mo/> "
|
Chris@499
|
190 " SELECT ?signal ?source FROM <%1> "
|
Chris@589
|
191 " WHERE { ?source a mo:AudioFile . "
|
Chris@589
|
192 " ?signal a mo:Signal . "
|
Chris@589
|
193 " ?source mo:encodes ?signal } "
|
Chris@499
|
194 )
|
Chris@499
|
195 .arg(m_uristring));
|
Chris@499
|
196
|
Chris@499
|
197 SimpleSPARQLQuery::ResultList results = query.execute();
|
Chris@499
|
198
|
Chris@589
|
199 if (results.empty()) {
|
Chris@589
|
200
|
Chris@589
|
201 SimpleSPARQLQuery query2
|
Chris@589
|
202 (SimpleSPARQLQuery::QueryFromSingleSource,
|
Chris@589
|
203 QString
|
Chris@589
|
204 (
|
Chris@589
|
205 " PREFIX mo: <http://purl.org/ontology/mo/> "
|
Chris@589
|
206 " SELECT ?signal ?source FROM <%1> "
|
Chris@589
|
207 " WHERE { ?signal a mo:Signal ; mo:available_as ?source } "
|
Chris@589
|
208 )
|
Chris@589
|
209 .arg(m_uristring));
|
Chris@589
|
210
|
Chris@589
|
211 results = query.execute();
|
Chris@589
|
212 }
|
Chris@589
|
213
|
Chris@499
|
214 for (int i = 0; i < results.size(); ++i) {
|
Chris@499
|
215
|
Chris@499
|
216 QString signal = results[i]["signal"].value;
|
Chris@499
|
217 QString source = results[i]["source"].value;
|
Chris@499
|
218
|
Chris@522
|
219 FileSource *fs = new FileSource(source, reporter);
|
Chris@522
|
220 #ifdef NO_SV_GUI
|
Chris@522
|
221 if (!fs->isAvailable()) {
|
Chris@522
|
222 m_errorString = QString("Signal source \"%1\" is not available").arg(source);
|
Chris@522
|
223 delete fs;
|
Chris@522
|
224 continue;
|
Chris@522
|
225 }
|
Chris@522
|
226 #else
|
Chris@522
|
227 if (!fs->isAvailable()) {
|
Chris@522
|
228 FileFinder *ff = FileFinder::getInstance();
|
Chris@581
|
229 if (ff) {
|
Chris@581
|
230 QString path = ff->find(FileFinder::AudioFile,
|
Chris@581
|
231 fs->getLocation(),
|
Chris@581
|
232 m_uristring);
|
Chris@581
|
233 if (path != "") {
|
Chris@522
|
234 delete fs;
|
Chris@581
|
235 fs = new FileSource(path, reporter);
|
Chris@581
|
236 if (!fs->isAvailable()) {
|
Chris@581
|
237 delete fs;
|
Chris@581
|
238 m_errorString = QString("Signal source \"%1\" is not available").arg(source);
|
Chris@581
|
239 continue;
|
Chris@581
|
240 }
|
Chris@522
|
241 }
|
Chris@499
|
242 }
|
Chris@522
|
243 }
|
Chris@522
|
244 #endif
|
Chris@522
|
245
|
Chris@522
|
246 if (reporter) {
|
Chris@522
|
247 reporter->setMessage(RDFImporter::tr("Importing audio referenced in RDF..."));
|
Chris@522
|
248 }
|
Chris@522
|
249 fs->waitForData();
|
Chris@522
|
250 WaveFileModel *newModel = new WaveFileModel(*fs, m_sampleRate);
|
Chris@522
|
251 if (newModel->isOK()) {
|
Chris@522
|
252 std::cerr << "Successfully created wave file model from source at \"" << source.toStdString() << "\"" << std::endl;
|
Chris@522
|
253 models.push_back(newModel);
|
Chris@522
|
254 m_audioModelMap[signal] = newModel;
|
Chris@522
|
255 if (m_sampleRate == 0) {
|
Chris@522
|
256 m_sampleRate = newModel->getSampleRate();
|
Chris@499
|
257 }
|
Chris@508
|
258 } else {
|
Chris@522
|
259 m_errorString = QString("Failed to create wave file model from source at \"%1\"").arg(source);
|
Chris@522
|
260 delete newModel;
|
Chris@499
|
261 }
|
Chris@522
|
262 delete fs;
|
Chris@499
|
263 }
|
Chris@499
|
264 }
|
Chris@499
|
265
|
Chris@499
|
266 void
|
Chris@440
|
267 RDFImporterImpl::getDataModelsDense(std::vector<Model *> &models,
|
Chris@440
|
268 ProgressReporter *reporter)
|
Chris@440
|
269 {
|
Chris@499
|
270 if (reporter) {
|
Chris@499
|
271 reporter->setMessage(RDFImporter::tr("Importing dense signal data from RDF..."));
|
Chris@499
|
272 }
|
Chris@499
|
273
|
Chris@589
|
274 SimpleSPARQLQuery query
|
Chris@489
|
275 (SimpleSPARQLQuery::QueryFromSingleSource,
|
Chris@480
|
276 QString
|
Chris@440
|
277 (
|
Chris@440
|
278 " PREFIX mo: <http://purl.org/ontology/mo/>"
|
Chris@440
|
279 " PREFIX af: <http://purl.org/ontology/af/>"
|
Chris@440
|
280
|
Chris@493
|
281 " SELECT ?feature ?feature_signal_type ?value "
|
Chris@440
|
282 " FROM <%1> "
|
Chris@440
|
283
|
Chris@440
|
284 " WHERE { "
|
Chris@440
|
285
|
Chris@493
|
286 " ?signal af:signal_feature ?feature . "
|
Chris@440
|
287
|
Chris@440
|
288 " ?feature a ?feature_signal_type ; "
|
Chris@440
|
289 " af:value ?value . "
|
Chris@440
|
290
|
Chris@440
|
291 " } "
|
Chris@440
|
292 )
|
Chris@440
|
293 .arg(m_uristring));
|
Chris@440
|
294
|
Chris@440
|
295 SimpleSPARQLQuery::ResultList results = query.execute();
|
Chris@440
|
296
|
Chris@440
|
297 if (!query.isOK()) {
|
Chris@440
|
298 m_errorString = query.getErrorString();
|
Chris@440
|
299 return;
|
Chris@440
|
300 }
|
Chris@440
|
301
|
Chris@440
|
302 if (query.wasCancelled()) {
|
Chris@440
|
303 m_errorString = "Query cancelled";
|
Chris@440
|
304 return;
|
Chris@440
|
305 }
|
Chris@440
|
306
|
Chris@440
|
307 for (int i = 0; i < results.size(); ++i) {
|
Chris@440
|
308
|
Chris@440
|
309 QString feature = results[i]["feature"].value;
|
Chris@440
|
310 QString type = results[i]["feature_signal_type"].value;
|
Chris@440
|
311 QString value = results[i]["value"].value;
|
Chris@440
|
312
|
Chris@440
|
313 int sampleRate = 0;
|
Chris@440
|
314 int windowLength = 0;
|
Chris@440
|
315 int hopSize = 0;
|
Chris@440
|
316 int width = 0;
|
Chris@440
|
317 int height = 0;
|
Chris@440
|
318 getDenseFeatureProperties
|
Chris@440
|
319 (feature, sampleRate, windowLength, hopSize, width, height);
|
Chris@440
|
320
|
Chris@440
|
321 if (sampleRate != 0 && sampleRate != m_sampleRate) {
|
Chris@440
|
322 cerr << "WARNING: Sample rate in dense feature description does not match our underlying rate -- using rate from feature description" << endl;
|
Chris@440
|
323 }
|
Chris@440
|
324 if (sampleRate == 0) sampleRate = m_sampleRate;
|
Chris@440
|
325
|
Chris@440
|
326 if (hopSize == 0) {
|
Chris@440
|
327 cerr << "WARNING: Dense feature description does not specify a hop size -- assuming 1" << endl;
|
Chris@440
|
328 hopSize = 1;
|
Chris@440
|
329 }
|
Chris@440
|
330
|
Chris@440
|
331 if (height == 0) {
|
Chris@440
|
332 cerr << "WARNING: Dense feature description does not specify feature signal dimensions -- assuming one-dimensional (height = 1)" << endl;
|
Chris@440
|
333 height = 1;
|
Chris@440
|
334 }
|
Chris@440
|
335
|
Chris@440
|
336 QStringList values = value.split(' ', QString::SkipEmptyParts);
|
Chris@440
|
337
|
Chris@440
|
338 if (values.empty()) {
|
Chris@440
|
339 cerr << "WARNING: Dense feature description does not specify any values!" << endl;
|
Chris@440
|
340 continue;
|
Chris@440
|
341 }
|
Chris@440
|
342
|
Chris@440
|
343 if (height == 1) {
|
Chris@440
|
344
|
Chris@440
|
345 SparseTimeValueModel *m = new SparseTimeValueModel
|
Chris@440
|
346 (sampleRate, hopSize, false);
|
Chris@440
|
347
|
Chris@440
|
348 for (int j = 0; j < values.size(); ++j) {
|
Chris@440
|
349 float f = values[j].toFloat();
|
Chris@440
|
350 SparseTimeValueModel::Point point(j * hopSize, f, "");
|
Chris@440
|
351 m->addPoint(point);
|
Chris@440
|
352 }
|
Chris@493
|
353
|
Chris@493
|
354 getDenseModelTitle(m, feature, type);
|
Chris@440
|
355
|
Chris@558
|
356 m->setRDFTypeURI(type);
|
Chris@558
|
357
|
Chris@440
|
358 models.push_back(m);
|
Chris@440
|
359
|
Chris@440
|
360 } else {
|
Chris@440
|
361
|
Chris@440
|
362 EditableDenseThreeDimensionalModel *m =
|
Chris@535
|
363 new EditableDenseThreeDimensionalModel
|
Chris@535
|
364 (sampleRate, hopSize, height,
|
Chris@535
|
365 EditableDenseThreeDimensionalModel::NoCompression, false);
|
Chris@440
|
366
|
Chris@440
|
367 EditableDenseThreeDimensionalModel::Column column;
|
Chris@440
|
368
|
Chris@440
|
369 int x = 0;
|
Chris@440
|
370
|
Chris@440
|
371 for (int j = 0; j < values.size(); ++j) {
|
Chris@440
|
372 if (j % height == 0 && !column.empty()) {
|
Chris@440
|
373 m->setColumn(x++, column);
|
Chris@440
|
374 column.clear();
|
Chris@440
|
375 }
|
Chris@440
|
376 column.push_back(values[j].toFloat());
|
Chris@440
|
377 }
|
Chris@440
|
378
|
Chris@440
|
379 if (!column.empty()) {
|
Chris@440
|
380 m->setColumn(x++, column);
|
Chris@440
|
381 }
|
Chris@440
|
382
|
Chris@493
|
383 getDenseModelTitle(m, feature, type);
|
Chris@493
|
384
|
Chris@558
|
385 m->setRDFTypeURI(type);
|
Chris@558
|
386
|
Chris@440
|
387 models.push_back(m);
|
Chris@440
|
388 }
|
Chris@440
|
389 }
|
Chris@440
|
390 }
|
Chris@440
|
391
|
Chris@440
|
392 void
|
Chris@493
|
393 RDFImporterImpl::getDenseModelTitle(Model *m,
|
Chris@493
|
394 QString featureUri,
|
Chris@493
|
395 QString featureTypeUri)
|
Chris@493
|
396 {
|
Chris@493
|
397 QString titleQuery = QString
|
Chris@493
|
398 (
|
Chris@493
|
399 " PREFIX dc: <http://purl.org/dc/elements/1.1/> "
|
Chris@493
|
400 " SELECT ?title "
|
Chris@493
|
401 " FROM <%1> "
|
Chris@493
|
402 " WHERE { "
|
Chris@493
|
403 " <%2> dc:title ?title . "
|
Chris@493
|
404 " } "
|
Chris@493
|
405 ).arg(m_uristring);
|
Chris@493
|
406
|
Chris@493
|
407 SimpleSPARQLQuery::Value v;
|
Chris@493
|
408
|
Chris@493
|
409 v = SimpleSPARQLQuery::singleResultQuery
|
Chris@493
|
410 (SimpleSPARQLQuery::QueryFromSingleSource,
|
Chris@493
|
411 titleQuery.arg(featureUri),
|
Chris@493
|
412 "title");
|
Chris@493
|
413
|
Chris@493
|
414 if (v.value != "") {
|
Chris@493
|
415 std::cerr << "RDFImporterImpl::getDenseModelTitle: Title (from signal) \"" << v.value.toStdString() << "\"" << std::endl;
|
Chris@493
|
416 m->setObjectName(v.value);
|
Chris@493
|
417 return;
|
Chris@493
|
418 }
|
Chris@493
|
419
|
Chris@493
|
420 v = SimpleSPARQLQuery::singleResultQuery
|
Chris@493
|
421 (SimpleSPARQLQuery::QueryFromSingleSource,
|
Chris@493
|
422 titleQuery.arg(featureTypeUri),
|
Chris@493
|
423 "title");
|
Chris@493
|
424
|
Chris@493
|
425 if (v.value != "") {
|
Chris@493
|
426 std::cerr << "RDFImporterImpl::getDenseModelTitle: Title (from signal type) \"" << v.value.toStdString() << "\"" << std::endl;
|
Chris@493
|
427 m->setObjectName(v.value);
|
Chris@493
|
428 return;
|
Chris@493
|
429 }
|
Chris@493
|
430
|
Chris@493
|
431 std::cerr << "RDFImporterImpl::getDenseModelTitle: No title available for feature <" << featureUri.toStdString() << ">" << std::endl;
|
Chris@493
|
432 }
|
Chris@493
|
433
|
Chris@493
|
434 void
|
Chris@440
|
435 RDFImporterImpl::getDenseFeatureProperties(QString featureUri,
|
Chris@440
|
436 int &sampleRate, int &windowLength,
|
Chris@440
|
437 int &hopSize, int &width, int &height)
|
Chris@440
|
438 {
|
Chris@489
|
439 SimpleSPARQLQuery::QueryType s = SimpleSPARQLQuery::QueryFromSingleSource;
|
Chris@489
|
440
|
Chris@440
|
441 QString dimensionsQuery
|
Chris@440
|
442 (
|
Chris@440
|
443 " PREFIX mo: <http://purl.org/ontology/mo/>"
|
Chris@440
|
444 " PREFIX af: <http://purl.org/ontology/af/>"
|
Chris@440
|
445
|
Chris@440
|
446 " SELECT ?dimensions "
|
Chris@440
|
447 " FROM <%1> "
|
Chris@440
|
448
|
Chris@440
|
449 " WHERE { "
|
Chris@440
|
450
|
Chris@440
|
451 " <%2> af:dimensions ?dimensions . "
|
Chris@440
|
452
|
Chris@440
|
453 " } "
|
Chris@440
|
454 );
|
Chris@440
|
455
|
Chris@440
|
456 SimpleSPARQLQuery::Value dimensionsValue =
|
Chris@489
|
457 SimpleSPARQLQuery::singleResultQuery
|
Chris@489
|
458 (s, dimensionsQuery.arg(m_uristring).arg(featureUri), "dimensions");
|
Chris@440
|
459
|
Chris@440
|
460 cerr << "Dimensions = \"" << dimensionsValue.value.toStdString() << "\""
|
Chris@440
|
461 << endl;
|
Chris@440
|
462
|
Chris@440
|
463 if (dimensionsValue.value != "") {
|
Chris@440
|
464 QStringList dl = dimensionsValue.value.split(" ");
|
Chris@440
|
465 if (dl.empty()) dl.push_back(dimensionsValue.value);
|
Chris@440
|
466 if (dl.size() > 0) height = dl[0].toInt();
|
Chris@440
|
467 if (dl.size() > 1) width = dl[1].toInt();
|
Chris@440
|
468 }
|
Chris@440
|
469
|
Chris@440
|
470 QString queryTemplate
|
Chris@440
|
471 (
|
Chris@440
|
472 " PREFIX mo: <http://purl.org/ontology/mo/>"
|
Chris@440
|
473 " PREFIX af: <http://purl.org/ontology/af/>"
|
Chris@440
|
474 " PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>"
|
Chris@440
|
475
|
Chris@440
|
476 " SELECT ?%3 "
|
Chris@440
|
477 " FROM <%1> "
|
Chris@440
|
478
|
Chris@440
|
479 " WHERE { "
|
Chris@440
|
480
|
Chris@440
|
481 " <%2> mo:time ?time . "
|
Chris@440
|
482
|
Chris@440
|
483 " ?time a tl:Interval ; "
|
Chris@440
|
484 " tl:onTimeLine ?timeline . "
|
Chris@440
|
485
|
Chris@440
|
486 " ?map tl:rangeTimeLine ?timeline . "
|
Chris@440
|
487
|
Chris@440
|
488 " ?map tl:%3 ?%3 . "
|
Chris@440
|
489
|
Chris@440
|
490 " } "
|
Chris@440
|
491 );
|
Chris@440
|
492
|
Chris@440
|
493 // Another laborious workaround for rasqal's failure to handle
|
Chris@440
|
494 // multiple optionals properly
|
Chris@440
|
495
|
Chris@440
|
496 SimpleSPARQLQuery::Value srValue =
|
Chris@489
|
497 SimpleSPARQLQuery::singleResultQuery(s,
|
Chris@480
|
498 queryTemplate
|
Chris@440
|
499 .arg(m_uristring).arg(featureUri)
|
Chris@440
|
500 .arg("sampleRate"),
|
Chris@440
|
501 "sampleRate");
|
Chris@440
|
502 if (srValue.value != "") {
|
Chris@440
|
503 sampleRate = srValue.value.toInt();
|
Chris@440
|
504 }
|
Chris@440
|
505
|
Chris@440
|
506 SimpleSPARQLQuery::Value hopValue =
|
Chris@489
|
507 SimpleSPARQLQuery::singleResultQuery(s,
|
Chris@480
|
508 queryTemplate
|
Chris@440
|
509 .arg(m_uristring).arg(featureUri)
|
Chris@440
|
510 .arg("hopSize"),
|
Chris@440
|
511 "hopSize");
|
Chris@440
|
512 if (srValue.value != "") {
|
Chris@440
|
513 hopSize = hopValue.value.toInt();
|
Chris@440
|
514 }
|
Chris@440
|
515
|
Chris@440
|
516 SimpleSPARQLQuery::Value winValue =
|
Chris@489
|
517 SimpleSPARQLQuery::singleResultQuery(s,
|
Chris@480
|
518 queryTemplate
|
Chris@440
|
519 .arg(m_uristring).arg(featureUri)
|
Chris@440
|
520 .arg("windowLength"),
|
Chris@440
|
521 "windowLength");
|
Chris@440
|
522 if (winValue.value != "") {
|
Chris@440
|
523 windowLength = winValue.value.toInt();
|
Chris@440
|
524 }
|
Chris@440
|
525
|
Chris@440
|
526 cerr << "sr = " << sampleRate << ", hop = " << hopSize << ", win = " << windowLength << endl;
|
Chris@440
|
527 }
|
Chris@440
|
528
|
Chris@440
|
529 void
|
Chris@440
|
530 RDFImporterImpl::getDataModelsSparse(std::vector<Model *> &models,
|
Chris@440
|
531 ProgressReporter *reporter)
|
Chris@440
|
532 {
|
Chris@499
|
533 if (reporter) {
|
Chris@499
|
534 reporter->setMessage(RDFImporter::tr("Importing event data from RDF..."));
|
Chris@499
|
535 }
|
Chris@499
|
536
|
Chris@489
|
537 SimpleSPARQLQuery::QueryType s = SimpleSPARQLQuery::QueryFromSingleSource;
|
Chris@489
|
538
|
Chris@439
|
539 // Our query is intended to retrieve every thing that has a time,
|
Chris@439
|
540 // and every feature type and value associated with a thing that
|
Chris@439
|
541 // has a time.
|
Chris@439
|
542
|
Chris@439
|
543 // We will then need to refine this big bag of results into a set
|
Chris@439
|
544 // of data models.
|
Chris@439
|
545
|
Chris@439
|
546 // Results that have different source signals should go into
|
Chris@439
|
547 // different models.
|
Chris@439
|
548
|
Chris@439
|
549 // Results that have different feature types should go into
|
Chris@439
|
550 // different models.
|
Chris@439
|
551
|
Chris@439
|
552 // Results that are sparse should go into different models from
|
Chris@439
|
553 // those that are dense (we need to examine the timestamps to
|
Chris@439
|
554 // establish this -- if the timestamps are regular, the results
|
Chris@439
|
555 // are dense -- so we can't do it as we go along, only after
|
Chris@439
|
556 // collecting all results).
|
Chris@439
|
557
|
Chris@439
|
558 // Timed things that have features associated with them should not
|
Chris@439
|
559 // appear directly in any model -- their features should appear
|
Chris@439
|
560 // instead -- and these should be different models from those used
|
Chris@439
|
561 // for timed things that do not have features.
|
Chris@439
|
562
|
Chris@439
|
563 // As we load the results, we'll push them into a partially
|
Chris@439
|
564 // structured container that maps from source signal (URI as
|
Chris@439
|
565 // string) -> feature type (likewise) -> time -> list of values.
|
Chris@439
|
566 // If the source signal or feature type is unavailable, the empty
|
Chris@439
|
567 // string will do.
|
Chris@439
|
568
|
Chris@449
|
569 QString prefixes = QString(
|
Chris@439
|
570 " PREFIX event: <http://purl.org/NET/c4dm/event.owl#>"
|
Chris@449
|
571 " PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>"
|
Chris@439
|
572 " PREFIX mo: <http://purl.org/ontology/mo/>"
|
Chris@439
|
573 " PREFIX af: <http://purl.org/ontology/af/>"
|
Chris@449
|
574 " PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
|
Chris@449
|
575 );
|
Chris@439
|
576
|
Chris@449
|
577 QString queryString = prefixes + QString(
|
Chris@449
|
578
|
Chris@499
|
579 " SELECT ?signal ?timed_thing ?event_type ?value"
|
Chris@439
|
580 " FROM <%1>"
|
Chris@439
|
581
|
Chris@439
|
582 " WHERE {"
|
Chris@440
|
583
|
Chris@440
|
584 " ?signal a mo:Signal ."
|
Chris@440
|
585
|
Chris@439
|
586 " ?signal mo:time ?interval ."
|
Chris@449
|
587 " ?interval tl:onTimeLine ?tl ."
|
Chris@449
|
588 " ?time tl:onTimeLine ?tl ."
|
Chris@449
|
589 " ?timed_thing event:time ?time ."
|
Chris@440
|
590 " ?timed_thing a ?event_type ."
|
Chris@440
|
591
|
Chris@439
|
592 " OPTIONAL {"
|
Chris@440
|
593 " ?timed_thing af:feature ?value"
|
Chris@439
|
594 " }"
|
Chris@439
|
595 " }"
|
Chris@439
|
596
|
Chris@439
|
597 ).arg(m_uristring);
|
Chris@439
|
598
|
Chris@449
|
599 QString timeQueryString = prefixes + QString(
|
Chris@449
|
600
|
Chris@449
|
601 " SELECT ?time FROM <%1> "
|
Chris@449
|
602 " WHERE { "
|
Chris@449
|
603 " <%2> event:time ?t . "
|
Chris@449
|
604 " ?t tl:at ?time . "
|
Chris@449
|
605 " } "
|
Chris@449
|
606
|
Chris@449
|
607 ).arg(m_uristring);
|
Chris@449
|
608
|
Chris@449
|
609 QString rangeQueryString = prefixes + QString(
|
Chris@449
|
610
|
Chris@449
|
611 " SELECT ?time ?duration FROM <%1> "
|
Chris@449
|
612 " WHERE { "
|
Chris@449
|
613 " <%2> event:time ?t . "
|
Chris@449
|
614 " ?t tl:beginsAt ?time . "
|
Chris@449
|
615 " ?t tl:duration ?duration . "
|
Chris@449
|
616 " } "
|
Chris@449
|
617
|
Chris@449
|
618 ).arg(m_uristring);
|
Chris@449
|
619
|
Chris@449
|
620 QString labelQueryString = prefixes + QString(
|
Chris@449
|
621
|
Chris@449
|
622 " SELECT ?label FROM <%1> "
|
Chris@449
|
623 " WHERE { "
|
Chris@449
|
624 " <%2> rdfs:label ?label . "
|
Chris@449
|
625 " } "
|
Chris@449
|
626
|
Chris@449
|
627 ).arg(m_uristring);
|
Chris@449
|
628
|
Chris@510
|
629 QString textQueryString = prefixes + QString(
|
Chris@510
|
630
|
Chris@510
|
631 " SELECT ?label FROM <%1> "
|
Chris@510
|
632 " WHERE { "
|
Chris@510
|
633 " <%2> af:text ?label . "
|
Chris@510
|
634 " } "
|
Chris@510
|
635
|
Chris@510
|
636 ).arg(m_uristring);
|
Chris@510
|
637
|
Chris@489
|
638 SimpleSPARQLQuery query(s, queryString);
|
Chris@439
|
639 query.setProgressReporter(reporter);
|
Chris@439
|
640
|
Chris@500
|
641 // cerr << "Query will be: " << queryString.toStdString() << endl;
|
Chris@439
|
642
|
Chris@439
|
643 SimpleSPARQLQuery::ResultList results = query.execute();
|
Chris@439
|
644
|
Chris@439
|
645 if (!query.isOK()) {
|
Chris@439
|
646 m_errorString = query.getErrorString();
|
Chris@440
|
647 return;
|
Chris@439
|
648 }
|
Chris@439
|
649
|
Chris@439
|
650 if (query.wasCancelled()) {
|
Chris@439
|
651 m_errorString = "Query cancelled";
|
Chris@440
|
652 return;
|
Chris@439
|
653 }
|
Chris@439
|
654
|
Chris@449
|
655 /*
|
Chris@449
|
656 This function is now only used for sparse data (for dense data
|
Chris@449
|
657 we would be in getDataModelsDense instead).
|
Chris@449
|
658
|
Chris@449
|
659 For sparse data, the determining factors in deciding what model
|
Chris@449
|
660 to use are: Do the features have values? and Do the features
|
Chris@449
|
661 have duration?
|
Chris@449
|
662
|
Chris@449
|
663 We can run through the results and check off whether we find
|
Chris@449
|
664 values and duration for each of the source+type keys, and then
|
Chris@449
|
665 run through the source+type keys pushing each of the results
|
Chris@449
|
666 into a suitable model.
|
Chris@449
|
667
|
Chris@449
|
668 Unfortunately, at this point we do not yet have any actual
|
Chris@449
|
669 timing data (time/duration) -- just the time URI.
|
Chris@449
|
670
|
Chris@449
|
671 What we _could_ do is to create one of each type of model at the
|
Chris@449
|
672 start, for each of the source+type keys, and then push each
|
Chris@449
|
673 feature into the relevant model depending on what we find out
|
Chris@449
|
674 about it. Then return only non-empty models.
|
Chris@449
|
675 */
|
Chris@449
|
676
|
Chris@449
|
677 // Map from signal source to event type to dimensionality to
|
Chris@449
|
678 // presence of duration to model ptr. Whee!
|
Chris@449
|
679 std::map<QString, std::map<QString, std::map<int, std::map<bool, Model *> > > >
|
Chris@449
|
680 modelMap;
|
Chris@449
|
681
|
Chris@439
|
682 for (int i = 0; i < results.size(); ++i) {
|
Chris@439
|
683
|
Chris@499
|
684 if (i % 4 == 0) {
|
Chris@499
|
685 if (reporter) reporter->setProgress(i/4);
|
Chris@499
|
686 }
|
Chris@499
|
687
|
Chris@499
|
688 QString source = results[i]["signal"].value;
|
Chris@449
|
689 QString type = results[i]["event_type"].value;
|
Chris@449
|
690 QString thinguri = results[i]["timed_thing"].value;
|
Chris@449
|
691
|
Chris@449
|
692 RealTime time;
|
Chris@449
|
693 RealTime duration;
|
Chris@439
|
694
|
Chris@449
|
695 bool haveTime = false;
|
Chris@449
|
696 bool haveDuration = false;
|
Chris@439
|
697
|
Chris@510
|
698 QString label = "";
|
Chris@510
|
699 bool text = (type.contains("Text") || type.contains("text")); // Ha, ha
|
Chris@510
|
700
|
Chris@510
|
701 if (text) {
|
Chris@510
|
702 label = SimpleSPARQLQuery::singleResultQuery
|
Chris@510
|
703 (s, textQueryString.arg(thinguri), "label").value;
|
Chris@510
|
704 }
|
Chris@510
|
705
|
Chris@510
|
706 if (label == "") {
|
Chris@510
|
707 label = SimpleSPARQLQuery::singleResultQuery
|
Chris@510
|
708 (s, labelQueryString.arg(thinguri), "label").value;
|
Chris@510
|
709 }
|
Chris@449
|
710
|
Chris@489
|
711 SimpleSPARQLQuery rangeQuery(s, rangeQueryString.arg(thinguri));
|
Chris@450
|
712 SimpleSPARQLQuery::ResultList rangeResults = rangeQuery.execute();
|
Chris@450
|
713 if (!rangeResults.empty()) {
|
Chris@450
|
714 // std::cerr << rangeResults.size() << " range results" << std::endl;
|
Chris@450
|
715 time = RealTime::fromXsdDuration
|
Chris@450
|
716 (rangeResults[0]["time"].value.toStdString());
|
Chris@450
|
717 duration = RealTime::fromXsdDuration
|
Chris@450
|
718 (rangeResults[0]["duration"].value.toStdString());
|
Chris@450
|
719 // std::cerr << "duration string " << rangeResults[0]["duration"].value.toStdString() << std::endl;
|
Chris@449
|
720 haveTime = true;
|
Chris@450
|
721 haveDuration = true;
|
Chris@449
|
722 } else {
|
Chris@450
|
723 QString timestring = SimpleSPARQLQuery::singleResultQuery
|
Chris@489
|
724 (s, timeQueryString.arg(thinguri), "time").value;
|
Chris@520
|
725 // std::cerr << "timestring = " << timestring.toStdString() << std::endl;
|
Chris@450
|
726 if (timestring != "") {
|
Chris@450
|
727 time = RealTime::fromXsdDuration(timestring.toStdString());
|
Chris@449
|
728 haveTime = true;
|
Chris@449
|
729 }
|
Chris@449
|
730 }
|
Chris@439
|
731
|
Chris@439
|
732 QString valuestring = results[i]["value"].value;
|
Chris@449
|
733 std::vector<float> values;
|
Chris@449
|
734
|
Chris@439
|
735 if (valuestring != "") {
|
Chris@449
|
736 QStringList vsl = valuestring.split(" ", QString::SkipEmptyParts);
|
Chris@449
|
737 for (int j = 0; j < vsl.size(); ++j) {
|
Chris@449
|
738 bool success = false;
|
Chris@449
|
739 float v = vsl[j].toFloat(&success);
|
Chris@449
|
740 if (success) values.push_back(v);
|
Chris@449
|
741 }
|
Chris@439
|
742 }
|
Chris@439
|
743
|
Chris@449
|
744 int dimensions = 1;
|
Chris@449
|
745 if (values.size() == 1) dimensions = 2;
|
Chris@449
|
746 else if (values.size() > 1) dimensions = 3;
|
Chris@449
|
747
|
Chris@449
|
748 Model *model = 0;
|
Chris@449
|
749
|
Chris@449
|
750 if (modelMap[source][type][dimensions].find(haveDuration) ==
|
Chris@449
|
751 modelMap[source][type][dimensions].end()) {
|
Chris@449
|
752
|
Chris@449
|
753 /*
|
Chris@449
|
754 std::cerr << "Creating new model: source = " << source.toStdString()
|
Chris@449
|
755 << ", type = " << type.toStdString() << ", dimensions = "
|
Chris@449
|
756 << dimensions << ", haveDuration = " << haveDuration
|
Chris@449
|
757 << ", time = " << time << ", duration = " << duration
|
Chris@449
|
758 << std::endl;
|
Chris@449
|
759 */
|
Chris@449
|
760
|
Chris@449
|
761 if (!haveDuration) {
|
Chris@449
|
762
|
Chris@449
|
763 if (dimensions == 1) {
|
Chris@449
|
764
|
Chris@510
|
765 if (text) {
|
Chris@510
|
766
|
Chris@510
|
767 model = new TextModel(m_sampleRate, 1, false);
|
Chris@510
|
768
|
Chris@510
|
769 } else {
|
Chris@510
|
770
|
Chris@510
|
771 model = new SparseOneDimensionalModel(m_sampleRate, 1, false);
|
Chris@510
|
772 }
|
Chris@449
|
773
|
Chris@449
|
774 } else if (dimensions == 2) {
|
Chris@449
|
775
|
Chris@510
|
776 if (text) {
|
Chris@510
|
777
|
Chris@510
|
778 model = new TextModel(m_sampleRate, 1, false);
|
Chris@510
|
779
|
Chris@510
|
780 } else {
|
Chris@510
|
781
|
Chris@510
|
782 model = new SparseTimeValueModel(m_sampleRate, 1, false);
|
Chris@510
|
783 }
|
Chris@449
|
784
|
Chris@449
|
785 } else {
|
Chris@449
|
786
|
Chris@449
|
787 // We don't have a three-dimensional sparse model,
|
Chris@449
|
788 // so use a note model. We do have some logic (in
|
Chris@449
|
789 // extractStructure below) for guessing whether
|
Chris@449
|
790 // this should after all have been a dense model,
|
Chris@449
|
791 // but it's hard to apply it because we don't have
|
Chris@449
|
792 // all the necessary timing data yet... hmm
|
Chris@449
|
793
|
Chris@449
|
794 model = new NoteModel(m_sampleRate, 1, false);
|
Chris@449
|
795 }
|
Chris@449
|
796
|
Chris@449
|
797 } else { // haveDuration
|
Chris@449
|
798
|
Chris@449
|
799 if (dimensions == 1 || dimensions == 2) {
|
Chris@449
|
800
|
Chris@449
|
801 // If our units are frequency or midi pitch, we
|
Chris@449
|
802 // should be using a note model... hm
|
Chris@449
|
803
|
Chris@449
|
804 model = new RegionModel(m_sampleRate, 1, false);
|
Chris@449
|
805
|
Chris@449
|
806 } else {
|
Chris@449
|
807
|
Chris@449
|
808 // We don't have a three-dimensional sparse model,
|
Chris@449
|
809 // so use a note model. We do have some logic (in
|
Chris@449
|
810 // extractStructure below) for guessing whether
|
Chris@449
|
811 // this should after all have been a dense model,
|
Chris@449
|
812 // but it's hard to apply it because we don't have
|
Chris@449
|
813 // all the necessary timing data yet... hmm
|
Chris@449
|
814
|
Chris@449
|
815 model = new NoteModel(m_sampleRate, 1, false);
|
Chris@449
|
816 }
|
Chris@449
|
817 }
|
Chris@449
|
818
|
Chris@558
|
819 model->setRDFTypeURI(type);
|
Chris@558
|
820
|
Chris@499
|
821 if (m_audioModelMap.find(source) != m_audioModelMap.end()) {
|
Chris@499
|
822 std::cerr << "source model for " << model << " is " << m_audioModelMap[source] << std::endl;
|
Chris@499
|
823 model->setSourceModel(m_audioModelMap[source]);
|
Chris@499
|
824 }
|
Chris@499
|
825
|
Chris@493
|
826 QString titleQuery = QString
|
Chris@493
|
827 (
|
Chris@493
|
828 " PREFIX dc: <http://purl.org/dc/elements/1.1/> "
|
Chris@493
|
829 " SELECT ?title "
|
Chris@493
|
830 " FROM <%1> "
|
Chris@493
|
831 " WHERE { "
|
Chris@493
|
832 " <%2> dc:title ?title . "
|
Chris@493
|
833 " } "
|
Chris@493
|
834 ).arg(m_uristring).arg(type);
|
Chris@493
|
835 QString title = SimpleSPARQLQuery::singleResultQuery
|
Chris@493
|
836 (s, titleQuery, "title").value;
|
Chris@493
|
837 if (title != "") model->setObjectName(title);
|
Chris@493
|
838
|
Chris@449
|
839 modelMap[source][type][dimensions][haveDuration] = model;
|
Chris@449
|
840 models.push_back(model);
|
Chris@449
|
841 }
|
Chris@449
|
842
|
Chris@449
|
843 model = modelMap[source][type][dimensions][haveDuration];
|
Chris@449
|
844
|
Chris@449
|
845 if (model) {
|
Chris@449
|
846 long ftime = RealTime::realTime2Frame(time, m_sampleRate);
|
Chris@449
|
847 long fduration = RealTime::realTime2Frame(duration, m_sampleRate);
|
Chris@449
|
848 fillModel(model, ftime, fduration, haveDuration, values, label);
|
Chris@439
|
849 }
|
Chris@439
|
850 }
|
Chris@439
|
851 }
|
Chris@439
|
852
|
Chris@439
|
853 void
|
Chris@449
|
854 RDFImporterImpl::fillModel(Model *model,
|
Chris@449
|
855 long ftime,
|
Chris@449
|
856 long fduration,
|
Chris@449
|
857 bool haveDuration,
|
Chris@449
|
858 std::vector<float> &values,
|
Chris@449
|
859 QString label)
|
Chris@449
|
860 {
|
Chris@493
|
861 // std::cerr << "RDFImporterImpl::fillModel: adding point at frame " << ftime << std::endl;
|
Chris@492
|
862
|
Chris@449
|
863 SparseOneDimensionalModel *sodm =
|
Chris@449
|
864 dynamic_cast<SparseOneDimensionalModel *>(model);
|
Chris@449
|
865 if (sodm) {
|
Chris@449
|
866 SparseOneDimensionalModel::Point point(ftime, label);
|
Chris@449
|
867 sodm->addPoint(point);
|
Chris@449
|
868 return;
|
Chris@449
|
869 }
|
Chris@449
|
870
|
Chris@510
|
871 TextModel *tm =
|
Chris@510
|
872 dynamic_cast<TextModel *>(model);
|
Chris@510
|
873 if (tm) {
|
Chris@510
|
874 TextModel::Point point
|
Chris@510
|
875 (ftime,
|
Chris@510
|
876 values.empty() ? 0.5f : values[0] < 0.f ? 0.f : values[0] > 1.f ? 1.f : values[0], // I was young and feckless once too
|
Chris@510
|
877 label);
|
Chris@510
|
878 tm->addPoint(point);
|
Chris@510
|
879 return;
|
Chris@510
|
880 }
|
Chris@510
|
881
|
Chris@449
|
882 SparseTimeValueModel *stvm =
|
Chris@449
|
883 dynamic_cast<SparseTimeValueModel *>(model);
|
Chris@449
|
884 if (stvm) {
|
Chris@449
|
885 SparseTimeValueModel::Point point
|
Chris@449
|
886 (ftime, values.empty() ? 0.f : values[0], label);
|
Chris@449
|
887 stvm->addPoint(point);
|
Chris@449
|
888 return;
|
Chris@449
|
889 }
|
Chris@449
|
890
|
Chris@449
|
891 NoteModel *nm =
|
Chris@449
|
892 dynamic_cast<NoteModel *>(model);
|
Chris@449
|
893 if (nm) {
|
Chris@449
|
894 if (haveDuration) {
|
Chris@449
|
895 float value = 0.f, level = 1.f;
|
Chris@449
|
896 if (!values.empty()) {
|
Chris@449
|
897 value = values[0];
|
Chris@449
|
898 if (values.size() > 1) {
|
Chris@449
|
899 level = values[1];
|
Chris@449
|
900 }
|
Chris@449
|
901 }
|
Chris@449
|
902 NoteModel::Point point(ftime, value, fduration, level, label);
|
Chris@449
|
903 nm->addPoint(point);
|
Chris@449
|
904 } else {
|
Chris@449
|
905 float value = 0.f, duration = 1.f, level = 1.f;
|
Chris@449
|
906 if (!values.empty()) {
|
Chris@449
|
907 value = values[0];
|
Chris@449
|
908 if (values.size() > 1) {
|
Chris@449
|
909 duration = values[1];
|
Chris@449
|
910 if (values.size() > 2) {
|
Chris@449
|
911 level = values[2];
|
Chris@449
|
912 }
|
Chris@449
|
913 }
|
Chris@449
|
914 }
|
Chris@449
|
915 NoteModel::Point point(ftime, value, duration, level, label);
|
Chris@449
|
916 nm->addPoint(point);
|
Chris@449
|
917 }
|
Chris@449
|
918 return;
|
Chris@449
|
919 }
|
Chris@449
|
920
|
Chris@449
|
921 RegionModel *rm =
|
Chris@449
|
922 dynamic_cast<RegionModel *>(model);
|
Chris@449
|
923 if (rm) {
|
Chris@449
|
924 if (haveDuration) {
|
Chris@449
|
925 RegionModel::Point point
|
Chris@449
|
926 (ftime, values.empty() ? 0.f : values[0], fduration, label);
|
Chris@449
|
927 rm->addPoint(point);
|
Chris@449
|
928 } else {
|
Chris@449
|
929 // This won't actually happen -- we only create region models
|
Chris@449
|
930 // if we do have duration -- but just for completeness
|
Chris@449
|
931 float value = 0.f, duration = 1.f;
|
Chris@449
|
932 if (!values.empty()) {
|
Chris@449
|
933 value = values[0];
|
Chris@449
|
934 if (values.size() > 1) {
|
Chris@449
|
935 duration = values[1];
|
Chris@449
|
936 }
|
Chris@449
|
937 }
|
Chris@449
|
938 RegionModel::Point point(ftime, value, duration, label);
|
Chris@449
|
939 rm->addPoint(point);
|
Chris@449
|
940 }
|
Chris@449
|
941 return;
|
Chris@449
|
942 }
|
Chris@449
|
943
|
Chris@449
|
944 std::cerr << "WARNING: RDFImporterImpl::fillModel: Unknown or unexpected model type" << std::endl;
|
Chris@449
|
945 return;
|
Chris@449
|
946 }
|
Chris@449
|
947
|
Chris@490
|
948 RDFImporter::RDFDocumentType
|
Chris@490
|
949 RDFImporter::identifyDocumentType(QString url)
|
Chris@490
|
950 {
|
Chris@490
|
951 bool haveAudio = false;
|
Chris@490
|
952 bool haveAnnotations = false;
|
Chris@449
|
953
|
Chris@499
|
954 // This query is not expected to return any values, but if it
|
Chris@499
|
955 // executes successfully (leaving no error in the error string)
|
Chris@499
|
956 // then we know we have RDF
|
Chris@499
|
957 SimpleSPARQLQuery q(SimpleSPARQLQuery::QueryFromSingleSource,
|
Chris@499
|
958 QString(" SELECT ?x FROM <%1> WHERE { ?x <y> <z> } ")
|
Chris@499
|
959 .arg(url));
|
Chris@499
|
960
|
Chris@499
|
961 SimpleSPARQLQuery::ResultList r = q.execute();
|
Chris@499
|
962 if (!q.isOK()) {
|
Chris@542
|
963 SimpleSPARQLQuery::closeSingleSource(url);
|
Chris@499
|
964 return NotRDF;
|
Chris@499
|
965 }
|
Chris@499
|
966
|
Chris@588
|
967 // "MO-conformant" structure for audio files
|
Chris@588
|
968
|
Chris@490
|
969 SimpleSPARQLQuery::Value value =
|
Chris@490
|
970 SimpleSPARQLQuery::singleResultQuery
|
Chris@490
|
971 (SimpleSPARQLQuery::QueryFromSingleSource,
|
Chris@490
|
972 QString
|
Chris@490
|
973 (" PREFIX mo: <http://purl.org/ontology/mo/> "
|
Chris@490
|
974 " SELECT ?url FROM <%1> "
|
Chris@588
|
975 " WHERE { ?url a mo:AudioFile } "
|
Chris@490
|
976 ).arg(url),
|
Chris@490
|
977 "url");
|
Chris@490
|
978
|
Chris@490
|
979 if (value.type == SimpleSPARQLQuery::URIValue) {
|
Chris@588
|
980
|
Chris@490
|
981 haveAudio = true;
|
Chris@588
|
982
|
Chris@588
|
983 } else {
|
Chris@588
|
984
|
Chris@588
|
985 // Sonic Annotator v0.2 and below used to write this structure
|
Chris@588
|
986 // (which is not properly in conformance with the Music
|
Chris@588
|
987 // Ontology)
|
Chris@588
|
988
|
Chris@588
|
989 value =
|
Chris@588
|
990 SimpleSPARQLQuery::singleResultQuery
|
Chris@588
|
991 (SimpleSPARQLQuery::QueryFromSingleSource,
|
Chris@588
|
992 QString
|
Chris@588
|
993 (" PREFIX mo: <http://purl.org/ontology/mo/> "
|
Chris@588
|
994 " SELECT ?url FROM <%1> "
|
Chris@588
|
995 " WHERE { ?signal a mo:Signal ; mo:available_as ?url } "
|
Chris@588
|
996 ).arg(url),
|
Chris@588
|
997 "url");
|
Chris@588
|
998
|
Chris@588
|
999 if (value.type == SimpleSPARQLQuery::URIValue) {
|
Chris@588
|
1000 haveAudio = true;
|
Chris@588
|
1001 }
|
Chris@490
|
1002 }
|
Chris@490
|
1003
|
Chris@490
|
1004 value =
|
Chris@490
|
1005 SimpleSPARQLQuery::singleResultQuery
|
Chris@490
|
1006 (SimpleSPARQLQuery::QueryFromSingleSource,
|
Chris@490
|
1007 QString
|
Chris@490
|
1008 (" PREFIX event: <http://purl.org/NET/c4dm/event.owl#> "
|
Chris@490
|
1009 " SELECT ?thing FROM <%1> "
|
Chris@490
|
1010 " WHERE { ?thing event:time ?time } "
|
Chris@490
|
1011 ).arg(url),
|
Chris@490
|
1012 "thing");
|
Chris@490
|
1013
|
Chris@490
|
1014 if (value.type == SimpleSPARQLQuery::URIValue) {
|
Chris@490
|
1015 haveAnnotations = true;
|
Chris@490
|
1016 }
|
Chris@490
|
1017
|
Chris@490
|
1018 if (!haveAnnotations) {
|
Chris@490
|
1019
|
Chris@490
|
1020 value =
|
Chris@490
|
1021 SimpleSPARQLQuery::singleResultQuery
|
Chris@490
|
1022 (SimpleSPARQLQuery::QueryFromSingleSource,
|
Chris@490
|
1023 QString
|
Chris@490
|
1024 (" PREFIX af: <http://purl.org/ontology/af/> "
|
Chris@490
|
1025 " SELECT ?thing FROM <%1> "
|
Chris@490
|
1026 " WHERE { ?signal af:signal_feature ?thing } "
|
Chris@490
|
1027 ).arg(url),
|
Chris@490
|
1028 "thing");
|
Chris@490
|
1029
|
Chris@490
|
1030 if (value.type == SimpleSPARQLQuery::URIValue) {
|
Chris@490
|
1031 haveAnnotations = true;
|
Chris@490
|
1032 }
|
Chris@490
|
1033 }
|
Chris@490
|
1034
|
Chris@542
|
1035 SimpleSPARQLQuery::closeSingleSource(url);
|
Chris@542
|
1036
|
Chris@490
|
1037 if (haveAudio) {
|
Chris@490
|
1038 if (haveAnnotations) {
|
Chris@490
|
1039 return AudioRefAndAnnotations;
|
Chris@490
|
1040 } else {
|
Chris@490
|
1041 return AudioRef;
|
Chris@490
|
1042 }
|
Chris@490
|
1043 } else {
|
Chris@490
|
1044 if (haveAnnotations) {
|
Chris@490
|
1045 return Annotations;
|
Chris@490
|
1046 } else {
|
Chris@499
|
1047 return OtherRDFDocument;
|
Chris@490
|
1048 }
|
Chris@490
|
1049 }
|
Chris@492
|
1050
|
Chris@542
|
1051 return OtherRDFDocument;
|
Chris@490
|
1052 }
|
Chris@490
|
1053
|
Chris@520
|
1054 void
|
Chris@520
|
1055 RDFImporterImpl::loadPrefixes(ProgressReporter *reporter)
|
Chris@520
|
1056 {
|
Chris@520
|
1057 return;
|
Chris@520
|
1058 //!!!
|
Chris@520
|
1059 if (m_prefixesLoaded) return;
|
Chris@520
|
1060 const char *prefixes[] = {
|
Chris@520
|
1061 "http://purl.org/NET/c4dm/event.owl",
|
Chris@520
|
1062 "http://purl.org/NET/c4dm/timeline.owl",
|
Chris@520
|
1063 "http://purl.org/ontology/mo/",
|
Chris@520
|
1064 "http://purl.org/ontology/af/",
|
Chris@520
|
1065 "http://www.w3.org/2000/01/rdf-schema",
|
Chris@520
|
1066 "http://purl.org/dc/elements/1.1/",
|
Chris@520
|
1067 };
|
Chris@520
|
1068 for (size_t i = 0; i < sizeof(prefixes)/sizeof(prefixes[0]); ++i) {
|
Chris@520
|
1069 CachedFile cf(prefixes[i], reporter, "application/rdf+xml");
|
Chris@520
|
1070 if (!cf.isOK()) continue;
|
Chris@520
|
1071 SimpleSPARQLQuery::addSourceToModel
|
Chris@520
|
1072 (QUrl::fromLocalFile(cf.getLocalFilename()).toString());
|
Chris@520
|
1073 }
|
Chris@520
|
1074 m_prefixesLoaded = true;
|
Chris@520
|
1075 }
|