annotate rdf/RDFImporter.cpp @ 738:547b03533375 dataquay

Some exception handling
author Chris Cannam
date Fri, 25 May 2012 14:57:14 +0100
parents 31ab733841d0
children e802e550a1f2
rev   line source
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@727 7 This file copyright 2008-2012 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 "base/ProgressReporter.h"
Chris@439 25 #include "base/RealTime.h"
Chris@439 26
Chris@439 27 #include "data/model/SparseOneDimensionalModel.h"
Chris@439 28 #include "data/model/SparseTimeValueModel.h"
Chris@439 29 #include "data/model/EditableDenseThreeDimensionalModel.h"
Chris@449 30 #include "data/model/NoteModel.h"
Chris@510 31 #include "data/model/TextModel.h"
Chris@449 32 #include "data/model/RegionModel.h"
Chris@499 33 #include "data/model/WaveFileModel.h"
Chris@499 34
Chris@499 35 #include "data/fileio/FileSource.h"
Chris@520 36 #include "data/fileio/CachedFile.h"
Chris@581 37 #include "data/fileio/FileFinder.h"
Chris@522 38
Chris@726 39 #include <dataquay/BasicStore.h>
Chris@726 40 #include <dataquay/PropertyObject.h>
Chris@726 41
Chris@726 42 using Dataquay::Uri;
Chris@726 43 using Dataquay::Node;
Chris@726 44 using Dataquay::Nodes;
Chris@726 45 using Dataquay::Triple;
Chris@726 46 using Dataquay::Triples;
Chris@726 47 using Dataquay::BasicStore;
Chris@726 48 using Dataquay::PropertyObject;
Chris@726 49
Chris@439 50 using std::cerr;
Chris@439 51 using std::endl;
Chris@439 52
Chris@439 53 class RDFImporterImpl
Chris@439 54 {
Chris@439 55 public:
Chris@439 56 RDFImporterImpl(QString url, int sampleRate);
Chris@439 57 virtual ~RDFImporterImpl();
Chris@490 58
Chris@490 59 void setSampleRate(int sampleRate) { m_sampleRate = sampleRate; }
Chris@439 60
Chris@439 61 bool isOK();
Chris@439 62 QString getErrorString() const;
Chris@439 63
Chris@439 64 std::vector<Model *> getDataModels(ProgressReporter *);
Chris@439 65
Chris@439 66 protected:
Chris@726 67 BasicStore *m_store;
Chris@730 68 Uri expand(QString s) { return m_store->expand(s); }
Chris@726 69
Chris@439 70 QString m_uristring;
Chris@439 71 QString m_errorString;
Chris@499 72 std::map<QString, Model *> m_audioModelMap;
Chris@439 73 int m_sampleRate;
Chris@439 74
Chris@617 75 std::map<Model *, std::map<QString, float> > m_labelValueMap;
Chris@617 76
Chris@499 77 void getDataModelsAudio(std::vector<Model *> &, ProgressReporter *);
Chris@440 78 void getDataModelsSparse(std::vector<Model *> &, ProgressReporter *);
Chris@440 79 void getDataModelsDense(std::vector<Model *> &, ProgressReporter *);
Chris@440 80
Chris@493 81 void getDenseModelTitle(Model *, QString, QString);
Chris@493 82
Chris@440 83 void getDenseFeatureProperties(QString featureUri,
Chris@440 84 int &sampleRate, int &windowLength,
Chris@440 85 int &hopSize, int &width, int &height);
Chris@440 86
Chris@449 87 void fillModel(Model *, long, long, bool, std::vector<float> &, QString);
Chris@439 88 };
Chris@439 89
Chris@439 90 QString
Chris@439 91 RDFImporter::getKnownExtensions()
Chris@439 92 {
Chris@439 93 return "*.rdf *.n3 *.ttl";
Chris@439 94 }
Chris@439 95
Chris@439 96 RDFImporter::RDFImporter(QString url, int sampleRate) :
Chris@439 97 m_d(new RDFImporterImpl(url, sampleRate))
Chris@439 98 {
Chris@439 99 }
Chris@439 100
Chris@439 101 RDFImporter::~RDFImporter()
Chris@439 102 {
Chris@439 103 delete m_d;
Chris@439 104 }
Chris@439 105
Chris@490 106 void
Chris@490 107 RDFImporter::setSampleRate(int sampleRate)
Chris@490 108 {
Chris@490 109 m_d->setSampleRate(sampleRate);
Chris@490 110 }
Chris@490 111
Chris@439 112 bool
Chris@439 113 RDFImporter::isOK()
Chris@439 114 {
Chris@439 115 return m_d->isOK();
Chris@439 116 }
Chris@439 117
Chris@439 118 QString
Chris@439 119 RDFImporter::getErrorString() const
Chris@439 120 {
Chris@439 121 return m_d->getErrorString();
Chris@439 122 }
Chris@439 123
Chris@439 124 std::vector<Model *>
Chris@439 125 RDFImporter::getDataModels(ProgressReporter *r)
Chris@439 126 {
Chris@439 127 return m_d->getDataModels(r);
Chris@439 128 }
Chris@439 129
Chris@439 130 RDFImporterImpl::RDFImporterImpl(QString uri, int sampleRate) :
Chris@726 131 m_store(new BasicStore),
Chris@439 132 m_uristring(uri),
Chris@439 133 m_sampleRate(sampleRate)
Chris@439 134 {
Chris@726 135 //!!! retrieve data if remote... then
Chris@726 136
Chris@726 137 m_store->addPrefix("mo", Uri("http://purl.org/ontology/mo/"));
Chris@726 138 m_store->addPrefix("af", Uri("http://purl.org/ontology/af/"));
Chris@726 139 m_store->addPrefix("dc", Uri("http://purl.org/dc/elements/1.1/"));
Chris@726 140 m_store->addPrefix("tl", Uri("http://purl.org/NET/c4dm/timeline.owl#"));
Chris@726 141 m_store->addPrefix("event", Uri("http://purl.org/NET/c4dm/event.owl#"));
Chris@726 142 m_store->addPrefix("rdfs", Uri("http://www.w3.org/2000/01/rdf-schema#"));
Chris@727 143
Chris@738 144 try {
Chris@738 145 QUrl url;
Chris@738 146 if (uri.startsWith("file:")) {
Chris@738 147 url = QUrl(uri);
Chris@738 148 } else {
Chris@738 149 url = QUrl::fromLocalFile(uri);
Chris@738 150 }
Chris@738 151 m_store->import(url, BasicStore::ImportIgnoreDuplicates);
Chris@738 152 } catch (std::exception &e) {
Chris@738 153 m_errorString = e.what();
Chris@736 154 }
Chris@439 155 }
Chris@439 156
Chris@439 157 RDFImporterImpl::~RDFImporterImpl()
Chris@439 158 {
Chris@726 159 delete m_store;
Chris@439 160 }
Chris@439 161
Chris@439 162 bool
Chris@439 163 RDFImporterImpl::isOK()
Chris@439 164 {
Chris@439 165 return (m_errorString == "");
Chris@439 166 }
Chris@439 167
Chris@439 168 QString
Chris@439 169 RDFImporterImpl::getErrorString() const
Chris@439 170 {
Chris@439 171 return m_errorString;
Chris@439 172 }
Chris@439 173
Chris@439 174 std::vector<Model *>
Chris@439 175 RDFImporterImpl::getDataModels(ProgressReporter *reporter)
Chris@439 176 {
Chris@439 177 std::vector<Model *> models;
Chris@439 178
Chris@499 179 getDataModelsAudio(models, reporter);
Chris@499 180
Chris@490 181 if (m_sampleRate == 0) {
Chris@616 182 m_errorString = QString("Invalid audio data model (is audio file format supported?)");
Chris@686 183 std::cerr << m_errorString << std::endl;
Chris@490 184 return models;
Chris@490 185 }
Chris@490 186
Chris@508 187 QString error;
Chris@508 188
Chris@522 189 if (m_errorString != "") {
Chris@522 190 error = m_errorString;
Chris@522 191 }
Chris@508 192 m_errorString = "";
Chris@508 193
Chris@440 194 getDataModelsDense(models, reporter);
Chris@440 195
Chris@522 196 if (m_errorString != "") {
Chris@522 197 error = m_errorString;
Chris@522 198 }
Chris@440 199 m_errorString = "";
Chris@440 200
Chris@440 201 getDataModelsSparse(models, reporter);
Chris@440 202
Chris@522 203 if (m_errorString == "" && error != "") {
Chris@522 204 m_errorString = error;
Chris@522 205 }
Chris@440 206
Chris@440 207 return models;
Chris@440 208 }
Chris@440 209
Chris@440 210 void
Chris@499 211 RDFImporterImpl::getDataModelsAudio(std::vector<Model *> &models,
Chris@499 212 ProgressReporter *reporter)
Chris@499 213 {
Chris@726 214 Nodes sigs = m_store->match
Chris@730 215 (Triple(Node(), Uri("a"), expand("mo:Signal"))).subjects();
Chris@499 216
Chris@726 217 foreach (Node sig, sigs) {
Chris@726 218
Chris@730 219 Node file = m_store->complete(Triple(Node(), expand("mo:encodes"), sig));
Chris@726 220 if (file == Node()) {
Chris@730 221 file = m_store->complete(Triple(sig, expand("mo:available_as"), Node()));
Chris@726 222 }
Chris@726 223 if (file == Node()) {
Chris@726 224 std::cerr << "RDFImporterImpl::getDataModelsAudio: ERROR: No source for signal " << sig << std::endl;
Chris@726 225 continue;
Chris@726 226 }
Chris@499 227
Chris@726 228 QString signal = sig.value;
Chris@726 229 QString source = file.value;
Chris@589 230
Chris@726 231 SVDEBUG << "NOTE: Seeking signal source \"" << source
Chris@726 232 << "\"..." << endl;
Chris@616 233
Chris@522 234 FileSource *fs = new FileSource(source, reporter);
Chris@616 235 if (fs->isAvailable()) {
Chris@690 236 SVDEBUG << "NOTE: Source is available: Local filename is \""
Chris@726 237 << fs->getLocalFilename()
Chris@726 238 << "\"..." << endl;
Chris@616 239 }
Chris@616 240
Chris@522 241 #ifdef NO_SV_GUI
Chris@522 242 if (!fs->isAvailable()) {
Chris@522 243 m_errorString = QString("Signal source \"%1\" is not available").arg(source);
Chris@522 244 delete fs;
Chris@522 245 continue;
Chris@522 246 }
Chris@522 247 #else
Chris@522 248 if (!fs->isAvailable()) {
Chris@726 249 SVDEBUG << "NOTE: Signal source \"" << source
Chris@726 250 << "\" is not available, using file finder..." << endl;
Chris@522 251 FileFinder *ff = FileFinder::getInstance();
Chris@581 252 if (ff) {
Chris@581 253 QString path = ff->find(FileFinder::AudioFile,
Chris@581 254 fs->getLocation(),
Chris@581 255 m_uristring);
Chris@581 256 if (path != "") {
Chris@617 257 std::cerr << "File finder returns: \"" << path.toStdString()
Chris@617 258 << "\"" << std::endl;
Chris@522 259 delete fs;
Chris@581 260 fs = new FileSource(path, reporter);
Chris@581 261 if (!fs->isAvailable()) {
Chris@581 262 delete fs;
Chris@581 263 m_errorString = QString("Signal source \"%1\" is not available").arg(source);
Chris@581 264 continue;
Chris@581 265 }
Chris@522 266 }
Chris@499 267 }
Chris@522 268 }
Chris@522 269 #endif
Chris@522 270
Chris@522 271 if (reporter) {
Chris@522 272 reporter->setMessage(RDFImporter::tr("Importing audio referenced in RDF..."));
Chris@522 273 }
Chris@522 274 fs->waitForData();
Chris@522 275 WaveFileModel *newModel = new WaveFileModel(*fs, m_sampleRate);
Chris@522 276 if (newModel->isOK()) {
Chris@686 277 std::cerr << "Successfully created wave file model from source at \"" << source << "\"" << std::endl;
Chris@522 278 models.push_back(newModel);
Chris@522 279 m_audioModelMap[signal] = newModel;
Chris@522 280 if (m_sampleRate == 0) {
Chris@522 281 m_sampleRate = newModel->getSampleRate();
Chris@499 282 }
Chris@508 283 } else {
Chris@522 284 m_errorString = QString("Failed to create wave file model from source at \"%1\"").arg(source);
Chris@522 285 delete newModel;
Chris@499 286 }
Chris@522 287 delete fs;
Chris@499 288 }
Chris@499 289 }
Chris@499 290
Chris@499 291 void
Chris@440 292 RDFImporterImpl::getDataModelsDense(std::vector<Model *> &models,
Chris@440 293 ProgressReporter *reporter)
Chris@440 294 {
Chris@499 295 if (reporter) {
Chris@499 296 reporter->setMessage(RDFImporter::tr("Importing dense signal data from RDF..."));
Chris@499 297 }
Chris@499 298
Chris@726 299 Nodes sigFeatures = m_store->match
Chris@730 300 (Triple(Node(), expand("af:signal_feature"), Node())).objects();
Chris@440 301
Chris@726 302 foreach (Node sf, sigFeatures) {
Chris@440 303
Chris@726 304 if (sf.type != Node::URI && sf.type != Node::Blank) continue;
Chris@726 305
Chris@730 306 Node t = m_store->complete(Triple(sf, expand("a"), Node()));
Chris@730 307 Node v = m_store->complete(Triple(sf, expand("af:value"), Node()));
Chris@440 308
Chris@726 309 QString feature = sf.value;
Chris@726 310 QString type = t.value;
Chris@726 311 QString value = v.value;
Chris@726 312
Chris@726 313 if (type == "" || value == "") continue;
Chris@440 314
Chris@440 315 int sampleRate = 0;
Chris@440 316 int windowLength = 0;
Chris@440 317 int hopSize = 0;
Chris@440 318 int width = 0;
Chris@440 319 int height = 0;
Chris@440 320 getDenseFeatureProperties
Chris@440 321 (feature, sampleRate, windowLength, hopSize, width, height);
Chris@440 322
Chris@440 323 if (sampleRate != 0 && sampleRate != m_sampleRate) {
Chris@440 324 cerr << "WARNING: Sample rate in dense feature description does not match our underlying rate -- using rate from feature description" << endl;
Chris@440 325 }
Chris@440 326 if (sampleRate == 0) sampleRate = m_sampleRate;
Chris@440 327
Chris@440 328 if (hopSize == 0) {
Chris@440 329 cerr << "WARNING: Dense feature description does not specify a hop size -- assuming 1" << endl;
Chris@440 330 hopSize = 1;
Chris@440 331 }
Chris@440 332
Chris@440 333 if (height == 0) {
Chris@440 334 cerr << "WARNING: Dense feature description does not specify feature signal dimensions -- assuming one-dimensional (height = 1)" << endl;
Chris@440 335 height = 1;
Chris@440 336 }
Chris@440 337
Chris@440 338 QStringList values = value.split(' ', QString::SkipEmptyParts);
Chris@440 339
Chris@440 340 if (values.empty()) {
Chris@440 341 cerr << "WARNING: Dense feature description does not specify any values!" << endl;
Chris@440 342 continue;
Chris@440 343 }
Chris@440 344
Chris@440 345 if (height == 1) {
Chris@440 346
Chris@440 347 SparseTimeValueModel *m = new SparseTimeValueModel
Chris@440 348 (sampleRate, hopSize, false);
Chris@440 349
Chris@440 350 for (int j = 0; j < values.size(); ++j) {
Chris@440 351 float f = values[j].toFloat();
Chris@440 352 SparseTimeValueModel::Point point(j * hopSize, f, "");
Chris@440 353 m->addPoint(point);
Chris@440 354 }
Chris@493 355
Chris@493 356 getDenseModelTitle(m, feature, type);
Chris@440 357
Chris@558 358 m->setRDFTypeURI(type);
Chris@558 359
Chris@440 360 models.push_back(m);
Chris@440 361
Chris@440 362 } else {
Chris@440 363
Chris@440 364 EditableDenseThreeDimensionalModel *m =
Chris@535 365 new EditableDenseThreeDimensionalModel
Chris@535 366 (sampleRate, hopSize, height,
Chris@535 367 EditableDenseThreeDimensionalModel::NoCompression, false);
Chris@440 368
Chris@440 369 EditableDenseThreeDimensionalModel::Column column;
Chris@440 370
Chris@440 371 int x = 0;
Chris@440 372
Chris@440 373 for (int j = 0; j < values.size(); ++j) {
Chris@440 374 if (j % height == 0 && !column.empty()) {
Chris@440 375 m->setColumn(x++, column);
Chris@440 376 column.clear();
Chris@440 377 }
Chris@440 378 column.push_back(values[j].toFloat());
Chris@440 379 }
Chris@440 380
Chris@440 381 if (!column.empty()) {
Chris@440 382 m->setColumn(x++, column);
Chris@440 383 }
Chris@440 384
Chris@493 385 getDenseModelTitle(m, feature, type);
Chris@493 386
Chris@558 387 m->setRDFTypeURI(type);
Chris@558 388
Chris@440 389 models.push_back(m);
Chris@440 390 }
Chris@440 391 }
Chris@440 392 }
Chris@440 393
Chris@440 394 void
Chris@493 395 RDFImporterImpl::getDenseModelTitle(Model *m,
Chris@493 396 QString featureUri,
Chris@493 397 QString featureTypeUri)
Chris@493 398 {
Chris@730 399 Node n = m_store->complete
Chris@730 400 (Triple(Uri(featureUri), expand("dc:title"), Node()));
Chris@493 401
Chris@726 402 if (n.type == Node::Literal && n.value != "") {
Chris@726 403 SVDEBUG << "RDFImporterImpl::getDenseModelTitle: Title (from signal) \"" << n.value << "\"" << endl;
Chris@726 404 m->setObjectName(n.value);
Chris@493 405 return;
Chris@493 406 }
Chris@493 407
Chris@730 408 n = m_store->complete
Chris@730 409 (Triple(Uri(featureTypeUri), expand("dc:title"), Node()));
Chris@726 410
Chris@726 411 if (n.type == Node::Literal && n.value != "") {
Chris@726 412 SVDEBUG << "RDFImporterImpl::getDenseModelTitle: Title (from signal type) \"" << n.value << "\"" << endl;
Chris@726 413 m->setObjectName(n.value);
Chris@493 414 return;
Chris@493 415 }
Chris@493 416
Chris@690 417 SVDEBUG << "RDFImporterImpl::getDenseModelTitle: No title available for feature <" << featureUri << ">" << endl;
Chris@493 418 }
Chris@493 419
Chris@493 420 void
Chris@440 421 RDFImporterImpl::getDenseFeatureProperties(QString featureUri,
Chris@440 422 int &sampleRate, int &windowLength,
Chris@440 423 int &hopSize, int &width, int &height)
Chris@440 424 {
Chris@730 425 Node dim = m_store->complete
Chris@730 426 (Triple(Uri(featureUri), expand("af:dimensions"), Node()));
Chris@489 427
Chris@726 428 cerr << "Dimensions = \"" << dim.value << "\"" << endl;
Chris@440 429
Chris@726 430 if (dim.type == Node::Literal && dim.value != "") {
Chris@726 431 QStringList dl = dim.value.split(" ");
Chris@726 432 if (dl.empty()) dl.push_back(dim.value);
Chris@440 433 if (dl.size() > 0) height = dl[0].toInt();
Chris@440 434 if (dl.size() > 1) width = dl[1].toInt();
Chris@440 435 }
Chris@726 436
Chris@726 437 // Looking for rate, hop, window from:
Chris@726 438 //
Chris@726 439 // ?feature mo:time ?time .
Chris@726 440 // ?time a tl:Interval .
Chris@726 441 // ?time tl:onTimeLine ?timeline .
Chris@726 442 // ?map tl:rangeTimeLine ?timeline .
Chris@726 443 // ?map tl:sampleRate ?rate .
Chris@726 444 // ?map tl:hopSize ?hop .
Chris@726 445 // ?map tl:windowLength ?window .
Chris@440 446
Chris@730 447 Node interval = m_store->complete(Triple(Uri(featureUri), expand("mo:time"), Node()));
Chris@440 448
Chris@730 449 if (!m_store->contains(Triple(interval, expand("a"), expand("tl:Interval")))) {
Chris@726 450 cerr << "RDFImporterImpl::getDenseFeatureProperties: Feature time node "
Chris@726 451 << interval << " is not a tl:Interval" << endl;
Chris@726 452 return;
Chris@440 453 }
Chris@440 454
Chris@730 455 Node tl = m_store->complete(Triple(interval, expand("tl:onTimeLine"), Node()));
Chris@726 456
Chris@726 457 if (tl == Node()) {
Chris@726 458 cerr << "RDFImporterImpl::getDenseFeatureProperties: Interval node "
Chris@726 459 << interval << " lacks tl:onTimeLine property" << endl;
Chris@726 460 return;
Chris@440 461 }
Chris@440 462
Chris@730 463 Node map = m_store->complete(Triple(Node(), expand("tl:rangeTimeLine"), tl));
Chris@726 464
Chris@726 465 if (map == Node()) {
Chris@726 466 cerr << "RDFImporterImpl::getDenseFeatureProperties: No map for "
Chris@726 467 << "timeline node " << tl << endl;
Chris@726 468 }
Chris@726 469
Chris@726 470 PropertyObject po(m_store, "tl:", map);
Chris@726 471
Chris@726 472 if (po.hasProperty("sampleRate")) {
Chris@726 473 sampleRate = po.getProperty("sampleRate").toInt();
Chris@726 474 }
Chris@726 475 if (po.hasProperty("hopSize")) {
Chris@726 476 hopSize = po.getProperty("hopSize").toInt();
Chris@726 477 }
Chris@726 478 if (po.hasProperty("windowLength")) {
Chris@726 479 windowLength = po.getProperty("windowLength").toInt();
Chris@440 480 }
Chris@440 481
Chris@440 482 cerr << "sr = " << sampleRate << ", hop = " << hopSize << ", win = " << windowLength << endl;
Chris@440 483 }
Chris@440 484
Chris@440 485 void
Chris@440 486 RDFImporterImpl::getDataModelsSparse(std::vector<Model *> &models,
Chris@440 487 ProgressReporter *reporter)
Chris@440 488 {
Chris@499 489 if (reporter) {
Chris@499 490 reporter->setMessage(RDFImporter::tr("Importing event data from RDF..."));
Chris@499 491 }
Chris@499 492
Chris@726 493 /*
Chris@726 494 This function is only used for sparse data (for dense data we
Chris@726 495 would be in getDataModelsDense instead).
Chris@489 496
Chris@726 497 Our query is intended to retrieve every thing that has a time,
Chris@726 498 and every feature type and value associated with a thing that
Chris@726 499 has a time.
Chris@439 500
Chris@726 501 We will then need to refine this big bag of results into a set
Chris@726 502 of data models.
Chris@439 503
Chris@726 504 Results that have different source signals should go into
Chris@726 505 different models.
Chris@439 506
Chris@726 507 Results that have different feature types should go into
Chris@726 508 different models.
Chris@726 509 */
Chris@439 510
Chris@726 511 Nodes sigs = m_store->match
Chris@730 512 (Triple(Node(), expand("a"), expand("mo:Signal"))).subjects();
Chris@449 513
Chris@616 514 // Map from timeline uri to event type to dimensionality to
Chris@449 515 // presence of duration to model ptr. Whee!
Chris@449 516 std::map<QString, std::map<QString, std::map<int, std::map<bool, Model *> > > >
Chris@449 517 modelMap;
Chris@449 518
Chris@726 519 foreach (Node sig, sigs) {
Chris@726 520
Chris@730 521 Node interval = m_store->complete(Triple(sig, expand("mo:time"), Node()));
Chris@726 522 if (interval == Node()) continue;
Chris@439 523
Chris@730 524 Node tl = m_store->complete(Triple(interval, expand("tl:onTimeLine"), Node()));
Chris@726 525 if (tl == Node()) continue;
Chris@499 526
Chris@730 527 Nodes times = m_store->match(Triple(Node(), expand("tl:onTimeLine"), tl)).subjects();
Chris@449 528
Chris@726 529 foreach (Node tn, times) {
Chris@726 530
Chris@730 531 Nodes timedThings = m_store->match(Triple(Node(), expand("event:time"), tn)).subjects();
Chris@439 532
Chris@726 533 foreach (Node thing, timedThings) {
Chris@726 534
Chris@730 535 Node typ = m_store->complete(Triple(thing, expand("a"), Node()));
Chris@726 536 if (typ == Node()) continue;
Chris@439 537
Chris@730 538 Node valu = m_store->complete(Triple(thing, expand("af:feature"), Node()));
Chris@510 539
Chris@726 540 QString source = sig.value;
Chris@726 541 QString timeline = tl.value;
Chris@726 542 QString type = typ.value;
Chris@726 543 QString thinguri = thing.value;
Chris@510 544
Chris@726 545 /*
Chris@726 546 For sparse data, the determining factors in deciding
Chris@726 547 what model to use are: Do the features have values?
Chris@726 548 and Do the features have duration?
Chris@449 549
Chris@726 550 We can run through the results and check off whether
Chris@726 551 we find values and duration for each of the
Chris@726 552 source+type keys, and then run through the
Chris@726 553 source+type keys pushing each of the results into a
Chris@726 554 suitable model.
Chris@439 555
Chris@726 556 Unfortunately, at this point we do not yet have any
Chris@726 557 actual timing data (time/duration) -- just the time
Chris@726 558 URI.
Chris@449 559
Chris@726 560 What we _could_ do is to create one of each type of
Chris@726 561 model at the start, for each of the source+type
Chris@726 562 keys, and then push each feature into the relevant
Chris@726 563 model depending on what we find out about it. Then
Chris@726 564 return only non-empty models.
Chris@726 565 */
Chris@439 566
Chris@726 567 QString label = "";
Chris@726 568 bool text = (type.contains("Text") || type.contains("text")); // Ha, ha
Chris@726 569 bool note = (type.contains("Note") || type.contains("note")); // Guffaw
Chris@449 570
Chris@726 571 if (text) {
Chris@730 572 label = m_store->complete(Triple(thing, expand("af:text"), Node())).value;
Chris@726 573 }
Chris@726 574
Chris@726 575 if (label == "") {
Chris@730 576 label = m_store->complete(Triple(thing, expand("rdfs:label"), Node())).value;
Chris@726 577 }
Chris@449 578
Chris@726 579 RealTime time;
Chris@726 580 RealTime duration;
Chris@726 581
Chris@726 582 bool haveTime = false;
Chris@726 583 bool haveDuration = false;
Chris@726 584
Chris@730 585 Node at = m_store->complete(Triple(tn, expand("tl:at"), Node()));
Chris@726 586
Chris@726 587 if (at != Node()) {
Chris@726 588 time = RealTime::fromXsdDuration(at.value.toStdString());
Chris@726 589 haveTime = true;
Chris@726 590 } else {
Chris@726 591 //!!! NB we're using rather old terminology for these things, apparently:
Chris@726 592 // beginsAt -> start
Chris@726 593 // onTimeLine -> timeline
Chris@726 594
Chris@730 595 Node start = m_store->complete(Triple(tn, expand("tl:beginsAt"), Node()));
Chris@730 596 Node dur = m_store->complete(Triple(tn, expand("tl:duration"), Node()));
Chris@726 597 if (start != Node() && dur != Node()) {
Chris@726 598 time = RealTime::fromXsdDuration
Chris@726 599 (start.value.toStdString());
Chris@726 600 duration = RealTime::fromXsdDuration
Chris@726 601 (dur.value.toStdString());
Chris@726 602 haveTime = haveDuration = true;
Chris@726 603 }
Chris@726 604 }
Chris@726 605
Chris@726 606 QString valuestring = valu.value;
Chris@726 607 std::vector<float> values;
Chris@726 608
Chris@726 609 if (valuestring != "") {
Chris@726 610 QStringList vsl = valuestring.split(" ", QString::SkipEmptyParts);
Chris@726 611 for (int j = 0; j < vsl.size(); ++j) {
Chris@726 612 bool success = false;
Chris@726 613 float v = vsl[j].toFloat(&success);
Chris@726 614 if (success) values.push_back(v);
Chris@726 615 }
Chris@726 616 }
Chris@726 617
Chris@726 618 int dimensions = 1;
Chris@726 619 if (values.size() == 1) dimensions = 2;
Chris@726 620 else if (values.size() > 1) dimensions = 3;
Chris@726 621
Chris@726 622 Model *model = 0;
Chris@726 623
Chris@726 624 if (modelMap[timeline][type][dimensions].find(haveDuration) ==
Chris@726 625 modelMap[timeline][type][dimensions].end()) {
Chris@449 626
Chris@449 627 /*
Chris@690 628 SVDEBUG << "Creating new model: source = " << source << ", type = " << type << ", dimensions = "
Chris@449 629 << dimensions << ", haveDuration = " << haveDuration
Chris@449 630 << ", time = " << time << ", duration = " << duration
Chris@687 631 << endl;
Chris@449 632 */
Chris@449 633
Chris@726 634 if (!haveDuration) {
Chris@449 635
Chris@726 636 if (dimensions == 1) {
Chris@726 637 if (text) {
Chris@726 638 model = new TextModel(m_sampleRate, 1, false);
Chris@726 639 } else {
Chris@726 640 model = new SparseOneDimensionalModel(m_sampleRate, 1, false);
Chris@726 641 }
Chris@726 642 } else if (dimensions == 2) {
Chris@726 643 if (text) {
Chris@726 644 model = new TextModel(m_sampleRate, 1, false);
Chris@726 645 } else {
Chris@726 646 model = new SparseTimeValueModel(m_sampleRate, 1, false);
Chris@726 647 }
Chris@726 648 } else {
Chris@726 649 // We don't have a three-dimensional sparse model,
Chris@726 650 // so use a note model. We do have some logic (in
Chris@726 651 // extractStructure below) for guessing whether
Chris@726 652 // this should after all have been a dense model,
Chris@726 653 // but it's hard to apply it because we don't have
Chris@726 654 // all the necessary timing data yet... hmm
Chris@726 655 model = new NoteModel(m_sampleRate, 1, false);
Chris@726 656 }
Chris@449 657
Chris@726 658 } else { // haveDuration
Chris@510 659
Chris@726 660 if (note || (dimensions > 2)) {
Chris@726 661 model = new NoteModel(m_sampleRate, 1, false);
Chris@726 662 } else {
Chris@726 663 // If our units are frequency or midi pitch, we
Chris@726 664 // should be using a note model... hm
Chris@726 665 model = new RegionModel(m_sampleRate, 1, false);
Chris@726 666 }
Chris@510 667 }
Chris@449 668
Chris@726 669 model->setRDFTypeURI(type);
Chris@449 670
Chris@726 671 if (m_audioModelMap.find(source) != m_audioModelMap.end()) {
Chris@726 672 std::cerr << "source model for " << model << " is " << m_audioModelMap[source] << std::endl;
Chris@726 673 model->setSourceModel(m_audioModelMap[source]);
Chris@510 674 }
Chris@449 675
Chris@730 676 QString title = m_store->complete
Chris@730 677 (Triple(typ, expand("dc:title"), Node())).value;
Chris@726 678 if (title == "") {
Chris@726 679 // take it from the end of the event type
Chris@726 680 title = type;
Chris@726 681 title.replace(QRegExp("^.*[/#]"), "");
Chris@726 682 }
Chris@726 683 model->setObjectName(title);
Chris@449 684
Chris@726 685 modelMap[timeline][type][dimensions][haveDuration] = model;
Chris@726 686 models.push_back(model);
Chris@449 687 }
Chris@449 688
Chris@726 689 model = modelMap[timeline][type][dimensions][haveDuration];
Chris@449 690
Chris@726 691 if (model) {
Chris@726 692 long ftime = RealTime::realTime2Frame(time, m_sampleRate);
Chris@726 693 long fduration = RealTime::realTime2Frame(duration, m_sampleRate);
Chris@726 694 fillModel(model, ftime, fduration, haveDuration, values, label);
Chris@449 695 }
Chris@449 696 }
Chris@439 697 }
Chris@439 698 }
Chris@439 699 }
Chris@439 700
Chris@439 701 void
Chris@449 702 RDFImporterImpl::fillModel(Model *model,
Chris@449 703 long ftime,
Chris@449 704 long fduration,
Chris@449 705 bool haveDuration,
Chris@449 706 std::vector<float> &values,
Chris@449 707 QString label)
Chris@449 708 {
Chris@690 709 // SVDEBUG << "RDFImporterImpl::fillModel: adding point at frame " << ftime << endl;
Chris@492 710
Chris@449 711 SparseOneDimensionalModel *sodm =
Chris@449 712 dynamic_cast<SparseOneDimensionalModel *>(model);
Chris@449 713 if (sodm) {
Chris@449 714 SparseOneDimensionalModel::Point point(ftime, label);
Chris@449 715 sodm->addPoint(point);
Chris@449 716 return;
Chris@449 717 }
Chris@449 718
Chris@510 719 TextModel *tm =
Chris@510 720 dynamic_cast<TextModel *>(model);
Chris@510 721 if (tm) {
Chris@510 722 TextModel::Point point
Chris@510 723 (ftime,
Chris@510 724 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 725 label);
Chris@510 726 tm->addPoint(point);
Chris@510 727 return;
Chris@510 728 }
Chris@510 729
Chris@449 730 SparseTimeValueModel *stvm =
Chris@449 731 dynamic_cast<SparseTimeValueModel *>(model);
Chris@449 732 if (stvm) {
Chris@449 733 SparseTimeValueModel::Point point
Chris@449 734 (ftime, values.empty() ? 0.f : values[0], label);
Chris@449 735 stvm->addPoint(point);
Chris@449 736 return;
Chris@449 737 }
Chris@449 738
Chris@449 739 NoteModel *nm =
Chris@449 740 dynamic_cast<NoteModel *>(model);
Chris@449 741 if (nm) {
Chris@449 742 if (haveDuration) {
Chris@449 743 float value = 0.f, level = 1.f;
Chris@449 744 if (!values.empty()) {
Chris@449 745 value = values[0];
Chris@449 746 if (values.size() > 1) {
Chris@449 747 level = values[1];
Chris@449 748 }
Chris@449 749 }
Chris@449 750 NoteModel::Point point(ftime, value, fduration, level, label);
Chris@449 751 nm->addPoint(point);
Chris@449 752 } else {
Chris@449 753 float value = 0.f, duration = 1.f, level = 1.f;
Chris@449 754 if (!values.empty()) {
Chris@449 755 value = values[0];
Chris@449 756 if (values.size() > 1) {
Chris@449 757 duration = values[1];
Chris@449 758 if (values.size() > 2) {
Chris@449 759 level = values[2];
Chris@449 760 }
Chris@449 761 }
Chris@449 762 }
Chris@449 763 NoteModel::Point point(ftime, value, duration, level, label);
Chris@449 764 nm->addPoint(point);
Chris@449 765 }
Chris@449 766 return;
Chris@449 767 }
Chris@449 768
Chris@449 769 RegionModel *rm =
Chris@449 770 dynamic_cast<RegionModel *>(model);
Chris@449 771 if (rm) {
Chris@617 772 float value = 0.f;
Chris@617 773 if (values.empty()) {
Chris@617 774 // no values? map each unique label to a distinct value
Chris@617 775 if (m_labelValueMap[model].find(label) == m_labelValueMap[model].end()) {
Chris@617 776 m_labelValueMap[model][label] = rm->getValueMaximum() + 1.f;
Chris@617 777 }
Chris@617 778 value = m_labelValueMap[model][label];
Chris@617 779 } else {
Chris@617 780 value = values[0];
Chris@617 781 }
Chris@449 782 if (haveDuration) {
Chris@617 783 RegionModel::Point point(ftime, value, fduration, label);
Chris@449 784 rm->addPoint(point);
Chris@449 785 } else {
Chris@449 786 // This won't actually happen -- we only create region models
Chris@449 787 // if we do have duration -- but just for completeness
Chris@617 788 float duration = 1.f;
Chris@449 789 if (!values.empty()) {
Chris@449 790 value = values[0];
Chris@449 791 if (values.size() > 1) {
Chris@449 792 duration = values[1];
Chris@449 793 }
Chris@449 794 }
Chris@449 795 RegionModel::Point point(ftime, value, duration, label);
Chris@449 796 rm->addPoint(point);
Chris@449 797 }
Chris@449 798 return;
Chris@449 799 }
Chris@449 800
Chris@449 801 std::cerr << "WARNING: RDFImporterImpl::fillModel: Unknown or unexpected model type" << std::endl;
Chris@449 802 return;
Chris@449 803 }
Chris@449 804
Chris@490 805 RDFImporter::RDFDocumentType
Chris@490 806 RDFImporter::identifyDocumentType(QString url)
Chris@490 807 {
Chris@490 808 bool haveAudio = false;
Chris@490 809 bool haveAnnotations = false;
Chris@726 810 bool haveRDF = false;
Chris@449 811
Chris@726 812 BasicStore *store = 0;
Chris@726 813
Chris@726 814 // This is not expected to return anything useful, but if it does
Chris@726 815 // anything at all then we know we have RDF
Chris@726 816 try {
Chris@738 817 //!!! non-local document?
Chris@726 818 store = BasicStore::load(QUrl(url));
Chris@730 819 Triple t = store->matchOnce(Triple());
Chris@726 820 if (t != Triple()) haveRDF = true;
Chris@738 821 } catch (std::exception &e) {
Chris@738 822 // nothing; haveRDF will be false so the next bit catches it
Chris@726 823 }
Chris@726 824
Chris@726 825 if (!haveRDF) {
Chris@726 826 delete store;
Chris@499 827 return NotRDF;
Chris@499 828 }
Chris@499 829
Chris@726 830 store->addPrefix("mo", Uri("http://purl.org/ontology/mo/"));
Chris@726 831 store->addPrefix("event", Uri("http://purl.org/NET/c4dm/event.owl#"));
Chris@726 832 store->addPrefix("af", Uri("http://purl.org/ontology/af/"));
Chris@726 833
Chris@588 834 // "MO-conformant" structure for audio files
Chris@588 835
Chris@730 836 Node n = store->complete(Triple(Node(), Uri("a"), store->expand("mo:AudioFile")));
Chris@726 837 if (n != Node() && n.type == Node::URI) {
Chris@588 838
Chris@490 839 haveAudio = true;
Chris@588 840
Chris@588 841 } else {
Chris@588 842
Chris@588 843 // Sonic Annotator v0.2 and below used to write this structure
Chris@588 844 // (which is not properly in conformance with the Music
Chris@588 845 // Ontology)
Chris@588 846
Chris@730 847 Nodes sigs = store->match(Triple(Node(), Uri("a"), store->expand("mo:Signal"))).subjects();
Chris@726 848 foreach (Node sig, sigs) {
Chris@730 849 Node aa = store->complete(Triple(sig, store->expand("mo:available_as"), Node()));
Chris@726 850 if (aa != Node()) {
Chris@726 851 haveAudio = true;
Chris@726 852 break;
Chris@726 853 }
Chris@588 854 }
Chris@490 855 }
Chris@490 856
Chris@690 857 SVDEBUG << "NOTE: RDFImporter::identifyDocumentType: haveAudio = "
Chris@687 858 << haveAudio << endl;
Chris@616 859
Chris@736 860 // can't call complete() with two Nothing nodes
Chris@736 861 n = store->matchOnce(Triple(Node(), store->expand("event:time"), Node())).c;
Chris@726 862 if (n != Node()) {
Chris@490 863 haveAnnotations = true;
Chris@490 864 }
Chris@490 865
Chris@490 866 if (!haveAnnotations) {
Chris@736 867 // can't call complete() with two Nothing nodes
Chris@736 868 n = store->matchOnce(Triple(Node(), store->expand("af:signal_feature"), Node())).c;
Chris@726 869 if (n != Node()) {
Chris@490 870 haveAnnotations = true;
Chris@490 871 }
Chris@490 872 }
Chris@490 873
Chris@690 874 SVDEBUG << "NOTE: RDFImporter::identifyDocumentType: haveAnnotations = "
Chris@687 875 << haveAnnotations << endl;
Chris@616 876
Chris@726 877 delete store;
Chris@542 878
Chris@490 879 if (haveAudio) {
Chris@490 880 if (haveAnnotations) {
Chris@490 881 return AudioRefAndAnnotations;
Chris@490 882 } else {
Chris@490 883 return AudioRef;
Chris@490 884 }
Chris@490 885 } else {
Chris@490 886 if (haveAnnotations) {
Chris@490 887 return Annotations;
Chris@490 888 } else {
Chris@499 889 return OtherRDFDocument;
Chris@490 890 }
Chris@490 891 }
Chris@492 892
Chris@542 893 return OtherRDFDocument;
Chris@490 894 }
Chris@490 895