annotate rdf/RDFFeatureWriter.cpp @ 590:063da8955f65

* Try to avoid URI clashes with existing URIs in the file, when writing with --rdf-append
author Chris Cannam
date Thu, 14 May 2009 15:47:16 +0000
parents 94054c97c76a
children f3bb7a3ffd89
rev   line source
Chris@498 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@498 2
Chris@498 3 /*
Chris@498 4 Sonic Annotator
Chris@498 5 A utility for batch feature extraction from audio files.
Chris@498 6 Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London.
Chris@498 7 Copyright 2007-2008 QMUL.
Chris@498 8
Chris@498 9 This program is free software; you can redistribute it and/or
Chris@498 10 modify it under the terms of the GNU General Public License as
Chris@498 11 published by the Free Software Foundation; either version 2 of the
Chris@498 12 License, or (at your option) any later version. See the file
Chris@498 13 COPYING included with this distribution for more information.
Chris@498 14 */
Chris@498 15
Chris@498 16 #include <fstream>
Chris@498 17
Chris@498 18 #include "vamp-hostsdk/PluginHostAdapter.h"
Chris@498 19 #include "vamp-hostsdk/PluginLoader.h"
Chris@498 20
Chris@498 21 #include "RDFFeatureWriter.h"
Chris@498 22 #include "RDFTransformFactory.h"
Chris@498 23
Chris@498 24 #include <QTextStream>
Chris@498 25 #include <QUrl>
Chris@508 26 #include <QFileInfo>
Chris@498 27 #include <QRegExp>
Chris@498 28
Chris@498 29 using namespace std;
Chris@498 30 using Vamp::Plugin;
Chris@498 31 using Vamp::PluginBase;
Chris@498 32
Chris@498 33 RDFFeatureWriter::RDFFeatureWriter() :
Chris@498 34 FileFeatureWriter(SupportOneFilePerTrackTransform |
Chris@498 35 SupportOneFilePerTrack |
Chris@498 36 SupportOneFileTotal,
Chris@498 37 "n3"),
Chris@498 38 m_plain(false),
Chris@498 39 m_count(0)
Chris@498 40 {
Chris@498 41 }
Chris@498 42
Chris@498 43 RDFFeatureWriter::~RDFFeatureWriter()
Chris@498 44 {
Chris@498 45 }
Chris@498 46
Chris@498 47 RDFFeatureWriter::ParameterList
Chris@498 48 RDFFeatureWriter::getSupportedParameters() const
Chris@498 49 {
Chris@498 50 ParameterList pl = FileFeatureWriter::getSupportedParameters();
Chris@498 51 Parameter p;
Chris@498 52
Chris@498 53 p.name = "plain";
Chris@498 54 p.description = "Use \"plain\" RDF even if transform metadata is available.";
Chris@498 55 p.hasArg = false;
Chris@498 56 pl.push_back(p);
Chris@498 57
Chris@586 58 p.name = "audiofile-uri";
Chris@586 59 p.description = "Link the output RDF to the given audio file URI instead of its actual location.";
Chris@498 60 p.hasArg = true;
Chris@498 61 pl.push_back(p);
Chris@498 62
Chris@498 63 return pl;
Chris@498 64 }
Chris@498 65
Chris@498 66 void
Chris@498 67 RDFFeatureWriter::setParameters(map<string, string> &params)
Chris@498 68 {
Chris@498 69 FileFeatureWriter::setParameters(params);
Chris@498 70
Chris@498 71 for (map<string, string>::iterator i = params.begin();
Chris@498 72 i != params.end(); ++i) {
Chris@498 73 if (i->first == "plain") {
Chris@498 74 m_plain = true;
Chris@498 75 }
Chris@586 76 if (i->first == "audiofile-uri") {
Chris@498 77 m_suri = i->second.c_str();
Chris@498 78 }
Chris@498 79 }
Chris@498 80 }
Chris@498 81
Chris@504 82 void
Chris@504 83 RDFFeatureWriter::setTrackMetadata(QString trackId,
Chris@504 84 TrackMetadata metadata)
Chris@504 85 {
Chris@504 86 m_metadata[trackId] = metadata;
Chris@504 87 }
Chris@504 88
Chris@504 89 void
Chris@510 90 RDFFeatureWriter::setFixedEventTypeURI(QString uri)
Chris@510 91 {
Chris@510 92 m_fixedEventTypeURI = uri;
Chris@510 93 }
Chris@510 94
Chris@510 95 void
Chris@504 96 RDFFeatureWriter::write(QString trackId,
Chris@504 97 const Transform &transform,
Chris@504 98 const Plugin::OutputDescriptor& output,
Chris@504 99 const Plugin::FeatureList& features,
Chris@504 100 std::string summaryType)
Chris@498 101 {
Chris@498 102 QString pluginId = transform.getPluginIdentifier();
Chris@498 103
Chris@498 104 if (m_rdfDescriptions.find(pluginId) == m_rdfDescriptions.end()) {
Chris@498 105
Chris@498 106 m_rdfDescriptions[pluginId] = PluginRDFDescription(pluginId);
Chris@498 107
Chris@498 108 if (m_rdfDescriptions[pluginId].haveDescription()) {
Chris@498 109 cerr << "NOTE: Have RDF description for plugin ID \""
Chris@498 110 << pluginId.toStdString() << "\"" << endl;
Chris@498 111 } else {
Chris@498 112 cerr << "NOTE: Do not have RDF description for plugin ID \""
Chris@498 113 << pluginId.toStdString() << "\"" << endl;
Chris@498 114 }
Chris@498 115 }
Chris@498 116
Chris@498 117 // Need to select appropriate output file for our track/transform
Chris@498 118 // combination
Chris@498 119
Chris@498 120 QTextStream *stream = getOutputStream(trackId, transform.getIdentifier());
Chris@512 121 if (!stream) {
Chris@512 122 std::cerr << "RDFFeatureWriter::write: ERROR: No output stream for track id \""
Chris@512 123 << trackId.toStdString() << "\" and transform \""
Chris@512 124 << transform.getIdentifier().toStdString() << "\"" << std::endl;
Chris@513 125 exit(1);
Chris@512 126 }
Chris@498 127
Chris@498 128 if (m_startedStreamTransforms.find(stream) ==
Chris@498 129 m_startedStreamTransforms.end()) {
Chris@530 130 cerr << "This stream is new, writing prefixes" << endl;
Chris@498 131 writePrefixes(stream);
Chris@498 132 if (m_singleFileName == "" && !m_stdout) {
Chris@498 133 writeSignalDescription(stream, trackId);
Chris@498 134 }
Chris@498 135 }
Chris@498 136
Chris@498 137 if (m_startedStreamTransforms[stream].find(transform) ==
Chris@498 138 m_startedStreamTransforms[stream].end()) {
Chris@498 139 m_startedStreamTransforms[stream].insert(transform);
Chris@498 140 writeLocalFeatureTypes
Chris@498 141 (stream, transform, output, m_rdfDescriptions[pluginId]);
Chris@498 142 }
Chris@498 143
Chris@498 144 if (m_singleFileName != "" || m_stdout) {
Chris@498 145 if (m_startedTrackIds.find(trackId) == m_startedTrackIds.end()) {
Chris@498 146 writeSignalDescription(stream, trackId);
Chris@498 147 m_startedTrackIds.insert(trackId);
Chris@498 148 }
Chris@498 149 }
Chris@498 150
Chris@498 151 QString timelineURI = m_trackTimelineURIs[trackId];
Chris@498 152
Chris@498 153 if (timelineURI == "") {
Chris@498 154 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing features without having established a timeline URI!" << endl;
Chris@498 155 exit(1);
Chris@498 156 }
Chris@498 157
Chris@498 158 if (summaryType != "") {
Chris@498 159
Chris@498 160 writeSparseRDF(stream, transform, output, features,
Chris@498 161 m_rdfDescriptions[pluginId], timelineURI);
Chris@498 162
Chris@498 163 } else if (m_rdfDescriptions[pluginId].haveDescription() &&
Chris@498 164 m_rdfDescriptions[pluginId].getOutputDisposition
Chris@498 165 (output.identifier.c_str()) ==
Chris@498 166 PluginRDFDescription::OutputDense) {
Chris@498 167
Chris@498 168 QString signalURI = m_trackSignalURIs[trackId];
Chris@498 169
Chris@498 170 if (signalURI == "") {
Chris@498 171 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing dense features without having established a signal URI!" << endl;
Chris@498 172 exit(1);
Chris@498 173 }
Chris@498 174
Chris@498 175 writeDenseRDF(stream, transform, output, features,
Chris@498 176 m_rdfDescriptions[pluginId], signalURI, timelineURI);
Chris@498 177
Chris@507 178 } else if (!m_plain &&
Chris@507 179 m_rdfDescriptions[pluginId].haveDescription() &&
Chris@507 180 m_rdfDescriptions[pluginId].getOutputDisposition
Chris@507 181 (output.identifier.c_str()) ==
Chris@507 182 PluginRDFDescription::OutputTrackLevel &&
Chris@507 183 m_rdfDescriptions[pluginId].getOutputFeatureAttributeURI
Chris@507 184 (output.identifier.c_str()) != "") {
Chris@507 185
Chris@507 186 QString signalURI = m_trackSignalURIs[trackId];
Chris@507 187
Chris@507 188 if (signalURI == "") {
Chris@507 189 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing track-level features without having established a signal URI!" << endl;
Chris@507 190 exit(1);
Chris@507 191 }
Chris@507 192
Chris@507 193 writeTrackLevelRDF(stream, transform, output, features,
Chris@507 194 m_rdfDescriptions[pluginId], signalURI);
Chris@507 195
Chris@498 196 } else {
Chris@498 197
Chris@498 198 writeSparseRDF(stream, transform, output, features,
Chris@498 199 m_rdfDescriptions[pluginId], timelineURI);
Chris@498 200 }
Chris@498 201 }
Chris@498 202
Chris@498 203 void
Chris@498 204 RDFFeatureWriter::writePrefixes(QTextStream *sptr)
Chris@498 205 {
Chris@498 206 QTextStream &stream = *sptr;
Chris@498 207
Chris@498 208 stream << "@prefix dc: <http://purl.org/dc/elements/1.1/> .\n"
Chris@498 209 << "@prefix mo: <http://purl.org/ontology/mo/> .\n"
Chris@498 210 << "@prefix af: <http://purl.org/ontology/af/> .\n"
Chris@504 211 << "@prefix foaf: <http://xmlns.com/foaf/0.1/> . \n"
Chris@498 212 << "@prefix event: <http://purl.org/NET/c4dm/event.owl#> .\n"
Chris@498 213 << "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n"
Chris@498 214 << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
Chris@498 215 << "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
Chris@498 216 << "@prefix tl: <http://purl.org/NET/c4dm/timeline.owl#> .\n"
Chris@498 217 << "@prefix vamp: <http://purl.org/ontology/vamp/> .\n"
Chris@498 218 << "@prefix : <#> .\n\n";
Chris@498 219 }
Chris@498 220
Chris@498 221 void
Chris@590 222 RDFFeatureWriter::reviewFileForAppending(QString filename)
Chris@590 223 {
Chris@590 224 // Appending to an RDF file is tricky, because we need to ensure
Chris@590 225 // that our URIs differ from any already in the file. This is a
Chris@590 226 // dirty grubby low-rent way of doing that. This function is
Chris@590 227 // called by FileFeatureWriter::getOutputFile when in append mode.
Chris@590 228
Chris@590 229 std::cerr << "reviewFileForAppending(" << filename.toStdString() << ")" << std::endl;
Chris@590 230
Chris@590 231 QFile file(filename);
Chris@590 232
Chris@590 233 // just return, don't report failure -- function that called us will do that
Chris@590 234 if (!file.open(QIODevice::ReadOnly)) return;
Chris@590 235
Chris@590 236 QTextStream in(&file);
Chris@590 237
Chris@590 238 QRegExp localObjectUriWithDigits(":[^ ]+_([0-9]+) a ");
Chris@590 239
Chris@590 240 while (!in.atEnd()) {
Chris@590 241 QString line = in.readLine();
Chris@590 242 if (line.length() > 120) { // probably data
Chris@590 243 continue;
Chris@590 244 }
Chris@590 245 if (localObjectUriWithDigits.indexIn(line) > -1) {
Chris@590 246 QString numeric = localObjectUriWithDigits.cap(1);
Chris@590 247 int number = numeric.toInt();
Chris@590 248 if (number >= m_count) m_count = number + 1;
Chris@590 249 }
Chris@590 250 }
Chris@590 251
Chris@590 252 file.close();
Chris@590 253 }
Chris@590 254
Chris@590 255 void
Chris@498 256 RDFFeatureWriter::writeSignalDescription(QTextStream *sptr,
Chris@498 257 QString trackId)
Chris@498 258 {
Chris@530 259 std::cerr << "RDFFeatureWriter::writeSignalDescription" << std::endl;
Chris@530 260
Chris@498 261 QTextStream &stream = *sptr;
Chris@498 262
Chris@498 263 /*
Chris@498 264 * Describe signal we're analysing (AudioFile, Signal, TimeLine, etc.)
Chris@498 265 */
Chris@498 266
Chris@498 267 QUrl url(trackId);
Chris@498 268 QString scheme = url.scheme().toLower();
Chris@498 269 bool local = (scheme == "" || scheme == "file" || scheme.length() == 1);
Chris@498 270
Chris@498 271 if (local) {
Chris@498 272 if (scheme == "") {
Chris@498 273 url.setScheme("file");
Chris@508 274 url.setPath(QFileInfo(url.path()).absoluteFilePath());
Chris@498 275 } else if (scheme.length() == 1) { // DOS drive letter!
Chris@498 276 url.setScheme("file");
Chris@498 277 url.setPath(scheme + ":" + url.path());
Chris@498 278 }
Chris@498 279 }
Chris@498 280
Chris@590 281 // Note reviewFileForAppending above (when opening in append mode)
Chris@498 282
Chris@498 283 uint64_t signalCount = m_count++;
Chris@498 284
Chris@498 285 if (m_trackSignalURIs.find(trackId) == m_trackSignalURIs.end()) {
Chris@498 286 m_trackSignalURIs[trackId] = QString(":signal_%1").arg(signalCount);
Chris@498 287 }
Chris@498 288 QString signalURI = m_trackSignalURIs[trackId];
Chris@498 289
Chris@585 290 if (m_trackTrackURIs.find(trackId) == m_trackTrackURIs.end()) {
Chris@585 291 m_trackTrackURIs[trackId] = QString(":track_%1").arg(signalCount);
Chris@585 292 }
Chris@585 293 QString trackURI = m_trackTrackURIs[trackId];
Chris@585 294
Chris@498 295 if (m_trackTimelineURIs.find(trackId) == m_trackTimelineURIs.end()) {
Chris@498 296 m_trackTimelineURIs[trackId] = QString(":signal_timeline_%1").arg(signalCount);
Chris@498 297 }
Chris@498 298 QString timelineURI = m_trackTimelineURIs[trackId];
Chris@498 299
Chris@586 300 QString afURI = url.toEncoded().data();
Chris@586 301 if (m_suri != NULL) afURI = m_suri;
Chris@586 302
Chris@585 303 if (m_metadata.find(trackId) != m_metadata.end()) {
Chris@585 304 TrackMetadata tm = m_metadata[trackId];
Chris@585 305 if (tm.title != "" || tm.maker != "") {
Chris@585 306 // We only write a Track at all if we have some
Chris@585 307 // title/artist metadata to put in it. Otherwise we can't
Chris@585 308 // be sure that what we have is a Track, in the
Chris@585 309 // publication sense -- it may just be a fragment, a test
Chris@585 310 // file, whatever. Since we'd have no metadata to
Chris@585 311 // associate with our Track, the only effect of including
Chris@585 312 // a Track would be to assert that this was one, which is
Chris@585 313 // the one thing we wouldn't know...
Chris@585 314 stream << trackURI << " a mo:Track ";
Chris@585 315 if (tm.title != "") {
Chris@585 316 stream << ";\n dc:title \"\"\"" << tm.title << "\"\"\" ";
Chris@585 317 }
Chris@585 318 if (tm.maker != "") {
Chris@585 319 stream << ";\n foaf:maker [ a mo:MusicArtist; foaf:name \"\"\"" << tm.maker << "\"\"\" ] ";
Chris@585 320 }
Chris@585 321 if (trackId != "") {
Chris@586 322 stream << ";\n mo:available_as <" << afURI << "> ";
Chris@585 323 }
Chris@585 324 stream << ".\n\n";
Chris@585 325 }
Chris@585 326 }
Chris@585 327
Chris@500 328 if (trackId != "") {
Chris@586 329 stream << "<" << afURI << "> a mo:AudioFile ;\n";
Chris@585 330 stream << " mo:encodes " << signalURI << ".\n\n";
Chris@500 331 }
Chris@500 332
Chris@500 333 stream << signalURI << " a mo:Signal ;\n";
Chris@500 334
Chris@500 335 stream << " mo:time [\n"
Chris@498 336 << " a tl:Interval ;\n"
Chris@498 337 << " tl:onTimeLine "
Chris@498 338 << timelineURI << "\n ] .\n\n";
Chris@585 339
Chris@585 340 stream << timelineURI << " a tl:Timeline .\n";
Chris@498 341 }
Chris@498 342
Chris@498 343 void
Chris@498 344 RDFFeatureWriter::writeLocalFeatureTypes(QTextStream *sptr,
Chris@498 345 const Transform &transform,
Chris@498 346 const Plugin::OutputDescriptor &od,
Chris@498 347 PluginRDFDescription &desc)
Chris@498 348 {
Chris@498 349 QString outputId = od.identifier.c_str();
Chris@498 350 QTextStream &stream = *sptr;
Chris@498 351
Chris@507 352 // There is no "needFeatureType" for track-level outputs, because
Chris@507 353 // we can't meaningfully write a feature at all if we don't know
Chris@507 354 // what property to use for it. If the output is track level but
Chris@507 355 // there is no feature type given, we have to revert to events.
Chris@507 356
Chris@498 357 bool needEventType = false;
Chris@498 358 bool needSignalType = false;
Chris@498 359
Chris@498 360 //!!! bin names, extents and so on can be written out using e.g. vamp:bin_names ( "a" "b" "c" )
Chris@498 361
Chris@498 362 if (desc.getOutputDisposition(outputId) ==
Chris@498 363 PluginRDFDescription::OutputDense) {
Chris@498 364
Chris@498 365 // no feature events, so may need signal type but won't need
Chris@498 366 // event type
Chris@498 367
Chris@498 368 if (m_plain) {
Chris@498 369
Chris@498 370 needSignalType = true;
Chris@498 371
Chris@498 372 } else if (desc.getOutputSignalTypeURI(outputId) == "") {
Chris@498 373
Chris@498 374 needSignalType = true;
Chris@498 375 }
Chris@498 376
Chris@507 377 } else if (desc.getOutputDisposition(outputId) ==
Chris@507 378 PluginRDFDescription::OutputTrackLevel) {
Chris@507 379
Chris@507 380 // see note above -- need to generate an event type if no
Chris@507 381 // feature type given, or if in plain mode
Chris@507 382
Chris@507 383 if (m_plain) {
Chris@507 384
Chris@507 385 needEventType = true;
Chris@507 386
Chris@507 387 } else if (desc.getOutputFeatureAttributeURI(outputId) == "") {
Chris@507 388
Chris@507 389 if (desc.getOutputEventTypeURI(outputId) == "") {
Chris@507 390
Chris@507 391 needEventType = true;
Chris@507 392 }
Chris@507 393 }
Chris@507 394
Chris@498 395 } else {
Chris@498 396
Chris@498 397 // may need event type but won't need signal type
Chris@498 398
Chris@498 399 if (m_plain) {
Chris@498 400
Chris@498 401 needEventType = true;
Chris@498 402
Chris@498 403 } else if (desc.getOutputEventTypeURI(outputId) == "") {
Chris@498 404
Chris@498 405 needEventType = true;
Chris@498 406 }
Chris@498 407 }
Chris@498 408
Chris@498 409 QString transformUri;
Chris@498 410 if (m_transformURIs.find(transform) != m_transformURIs.end()) {
Chris@498 411 transformUri = m_transformURIs[transform];
Chris@498 412 } else {
Chris@498 413 transformUri = QString(":transform_%1_%2").arg(m_count++).arg(outputId);
Chris@498 414 m_transformURIs[transform] = transformUri;
Chris@498 415 }
Chris@498 416
Chris@500 417 if (transform.getIdentifier() != "") {
Chris@508 418 stream << endl
Chris@508 419 << RDFTransformFactory::writeTransformToRDF(transform, transformUri)
Chris@500 420 << endl;
Chris@500 421 }
Chris@498 422
Chris@510 423 if (needEventType && m_fixedEventTypeURI == "") {
Chris@498 424
Chris@498 425 QString uri;
Chris@498 426 if (m_syntheticEventTypeURIs.find(transform) !=
Chris@498 427 m_syntheticEventTypeURIs.end()) {
Chris@498 428 uri = m_syntheticEventTypeURIs[transform];
Chris@498 429 } else {
Chris@498 430 uri = QString(":event_type_%1").arg(m_count++);
Chris@498 431 m_syntheticEventTypeURIs[transform] = uri;
Chris@498 432 }
Chris@498 433
Chris@498 434 stream << uri
Chris@498 435 << " rdfs:subClassOf event:Event ;" << endl
Chris@498 436 << " dc:title \"" << od.name.c_str() << "\" ;" << endl
Chris@498 437 << " dc:format \"" << od.unit.c_str() << "\" ;" << endl
Chris@498 438 << " dc:description \"" << od.description.c_str() << "\" ."
Chris@498 439 << endl << endl;
Chris@498 440 }
Chris@498 441
Chris@498 442 if (needSignalType) {
Chris@498 443
Chris@498 444 QString uri;
Chris@498 445 if (m_syntheticSignalTypeURIs.find(transform) !=
Chris@498 446 m_syntheticSignalTypeURIs.end()) {
Chris@498 447 uri = m_syntheticSignalTypeURIs[transform];
Chris@498 448 } else {
Chris@498 449 uri = QString(":signal_type_%1").arg(m_count++);
Chris@498 450 m_syntheticSignalTypeURIs[transform] = uri;
Chris@498 451 }
Chris@498 452
Chris@498 453 stream << uri
Chris@498 454 << " rdfs:subClassOf af:Signal ;" << endl
Chris@498 455 << " dc:title \"" << od.name.c_str() << "\" ;" << endl
Chris@498 456 << " dc:format \"" << od.unit.c_str() << "\" ;" << endl
Chris@498 457 << " dc:description \"" << od.description.c_str() << "\" ."
Chris@498 458 << endl << endl;
Chris@498 459 }
Chris@498 460 }
Chris@498 461
Chris@498 462 void
Chris@498 463 RDFFeatureWriter::writeSparseRDF(QTextStream *sptr,
Chris@498 464 const Transform &transform,
Chris@498 465 const Plugin::OutputDescriptor& od,
Chris@498 466 const Plugin::FeatureList& featureList,
Chris@498 467 PluginRDFDescription &desc,
Chris@498 468 QString timelineURI)
Chris@498 469 {
Chris@512 470 // std::cerr << "RDFFeatureWriter::writeSparseRDF: have " << featureList.size() << " features" << std::endl;
Chris@512 471
Chris@498 472 if (featureList.empty()) return;
Chris@498 473 QTextStream &stream = *sptr;
Chris@498 474
Chris@498 475 bool plain = (m_plain || !desc.haveDescription());
Chris@498 476
Chris@498 477 QString outputId = od.identifier.c_str();
Chris@498 478
Chris@498 479 // iterate through FeatureLists
Chris@498 480
Chris@498 481 for (int i = 0; i < featureList.size(); ++i) {
Chris@498 482
Chris@498 483 const Plugin::Feature &feature = featureList[i];
Chris@498 484 uint64_t featureNumber = m_count++;
Chris@498 485
Chris@498 486 stream << ":event_" << featureNumber << " a ";
Chris@498 487
Chris@510 488 if (m_fixedEventTypeURI != "") {
Chris@510 489 stream << m_fixedEventTypeURI << " ;\n";
Chris@510 490 } else {
Chris@510 491 QString eventTypeURI = desc.getOutputEventTypeURI(outputId);
Chris@510 492 if (plain || eventTypeURI == "") {
Chris@510 493 if (m_syntheticEventTypeURIs.find(transform) !=
Chris@510 494 m_syntheticEventTypeURIs.end()) {
Chris@510 495 stream << m_syntheticEventTypeURIs[transform] << " ;\n";
Chris@510 496 } else {
Chris@510 497 stream << ":event_type_" << outputId << " ;\n";
Chris@510 498 }
Chris@498 499 } else {
Chris@510 500 stream << "<" << eventTypeURI << "> ;\n";
Chris@498 501 }
Chris@498 502 }
Chris@498 503
Chris@498 504 QString timestamp = feature.timestamp.toString().c_str();
Chris@498 505 timestamp.replace(QRegExp("^ +"), "");
Chris@498 506
Chris@498 507 if (feature.hasDuration && feature.duration > Vamp::RealTime::zeroTime) {
Chris@498 508
Chris@498 509 QString duration = feature.duration.toString().c_str();
Chris@498 510 duration.replace(QRegExp("^ +"), "");
Chris@498 511
Chris@498 512 stream << " event:time [ \n"
Chris@498 513 << " a tl:Interval ;\n"
Chris@498 514 << " tl:onTimeLine " << timelineURI << " ;\n"
Chris@498 515 << " tl:beginsAt \"PT" << timestamp
Chris@498 516 << "S\"^^xsd:duration ;\n"
Chris@498 517 << " tl:duration \"PT" << duration
Chris@498 518 << "S\"^^xsd:duration ;\n"
Chris@498 519 << " ] ";
Chris@498 520
Chris@498 521 } else {
Chris@498 522
Chris@498 523 stream << " event:time [ \n"
Chris@498 524 << " a tl:Instant ;\n" //location of the event in time
Chris@498 525 << " tl:onTimeLine " << timelineURI << " ;\n"
Chris@498 526 << " tl:at \"PT" << timestamp
Chris@498 527 << "S\"^^xsd:duration ;\n ] ";
Chris@498 528 }
Chris@498 529
Chris@500 530 if (transform.getIdentifier() != "") {
Chris@500 531 stream << ";\n";
Chris@500 532 stream << " vamp:computed_by " << m_transformURIs[transform] << " ";
Chris@500 533 }
Chris@498 534
Chris@498 535 if (feature.label.length() > 0) {
Chris@498 536 stream << ";\n";
Chris@510 537 stream << " rdfs:label \"\"\"" << feature.label.c_str() << "\"\"\" ";
Chris@498 538 }
Chris@498 539
Chris@498 540 if (!feature.values.empty()) {
Chris@498 541 stream << ";\n";
Chris@498 542 //!!! named bins?
Chris@498 543 stream << " af:feature \"" << feature.values[0];
Chris@498 544 for (int j = 1; j < feature.values.size(); ++j) {
Chris@498 545 stream << " " << feature.values[j];
Chris@498 546 }
Chris@498 547 stream << "\" ";
Chris@498 548 }
Chris@498 549
Chris@498 550 stream << ".\n";
Chris@498 551 }
Chris@498 552 }
Chris@498 553
Chris@498 554 void
Chris@507 555 RDFFeatureWriter::writeTrackLevelRDF(QTextStream *sptr,
Chris@507 556 const Transform &transform,
Chris@507 557 const Plugin::OutputDescriptor& od,
Chris@507 558 const Plugin::FeatureList& featureList,
Chris@507 559 PluginRDFDescription &desc,
Chris@507 560 QString signalURI)
Chris@507 561 {
Chris@507 562 if (featureList.empty()) return;
Chris@507 563 QTextStream &stream = *sptr;
Chris@507 564
Chris@507 565 bool plain = (m_plain || !desc.haveDescription());
Chris@507 566
Chris@507 567 QString outputId = od.identifier.c_str();
Chris@507 568 QString featureUri = desc.getOutputFeatureAttributeURI(outputId);
Chris@507 569
Chris@507 570 if (featureUri == "") {
Chris@507 571 cerr << "RDFFeatureWriter::writeTrackLevelRDF: ERROR: No feature URI available -- this function should not have been called!" << endl;
Chris@507 572 return;
Chris@507 573 }
Chris@507 574
Chris@507 575 for (int i = 0; i < featureList.size(); ++i) {
Chris@507 576
Chris@507 577 const Plugin::Feature &feature = featureList[i];
Chris@507 578
Chris@507 579 if (feature.values.empty()) {
Chris@507 580
Chris@507 581 if (feature.label == "") continue;
Chris@507 582
Chris@508 583 stream << signalURI << " " << featureUri << " \"\"\""
Chris@508 584 << feature.label.c_str() << "\"\"\" .\n";
Chris@507 585
Chris@507 586 } else {
Chris@507 587
Chris@507 588 stream << signalURI << " " << featureUri << " \""
Chris@507 589 << feature.values[0] << "\"^^xsd:float .\n";
Chris@507 590 }
Chris@507 591 }
Chris@507 592 }
Chris@507 593
Chris@507 594 void
Chris@498 595 RDFFeatureWriter::writeDenseRDF(QTextStream *sptr,
Chris@498 596 const Transform &transform,
Chris@498 597 const Plugin::OutputDescriptor& od,
Chris@498 598 const Plugin::FeatureList& featureList,
Chris@498 599 PluginRDFDescription &desc,
Chris@498 600 QString signalURI,
Chris@498 601 QString timelineURI)
Chris@498 602 {
Chris@498 603 if (featureList.empty()) return;
Chris@498 604
Chris@498 605 StringTransformPair sp(signalURI, transform);
Chris@498 606
Chris@498 607 if (m_openDenseFeatures.find(sp) == m_openDenseFeatures.end()) {
Chris@498 608
Chris@498 609 StreamBuffer b(sptr, "");
Chris@498 610 m_openDenseFeatures[sp] = b;
Chris@498 611
Chris@498 612 QString &str(m_openDenseFeatures[sp].second);
Chris@498 613 QTextStream stream(&str);
Chris@498 614
Chris@498 615 bool plain = (m_plain || !desc.haveDescription());
Chris@498 616 QString outputId = od.identifier.c_str();
Chris@498 617
Chris@498 618 uint64_t featureNumber = m_count++;
Chris@498 619
Chris@498 620 // need to write out feature timeline map -- for this we need
Chris@498 621 // the sample rate, window length and hop size from the
Chris@498 622 // transform
Chris@498 623
Chris@498 624 stream << "\n:feature_timeline_" << featureNumber << " a tl:DiscreteTimeLine .\n\n";
Chris@498 625
Chris@498 626 size_t stepSize = transform.getStepSize();
Chris@498 627 if (stepSize == 0) {
Chris@498 628 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing dense features without having set the step size properly!" << endl;
Chris@498 629 return;
Chris@498 630 }
Chris@498 631
Chris@498 632 size_t blockSize = transform.getBlockSize();
Chris@498 633 if (blockSize == 0) {
Chris@498 634 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing dense features without having set the block size properly!" << endl;
Chris@498 635 return;
Chris@498 636 }
Chris@498 637
Chris@498 638 float sampleRate = transform.getSampleRate();
Chris@498 639 if (sampleRate == 0.f) {
Chris@498 640 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing dense features without having set the sample rate properly!" << endl;
Chris@498 641 return;
Chris@498 642 }
Chris@498 643
Chris@498 644 stream << ":feature_timeline_map_" << featureNumber
Chris@498 645 << " a tl:UniformSamplingWindowingMap ;\n"
Chris@498 646 << " tl:rangeTimeLine :feature_timeline_" << featureNumber << " ;\n"
Chris@498 647 << " tl:domainTimeLine " << timelineURI << " ;\n"
Chris@498 648 << " tl:sampleRate \"" << int(sampleRate) << "\"^^xsd:int ;\n"
Chris@498 649 << " tl:windowLength \"" << blockSize << "\"^^xsd:int ;\n"
Chris@498 650 << " tl:hopSize \"" << stepSize << "\"^^xsd:int .\n\n";
Chris@498 651
Chris@498 652 stream << signalURI << " af:signal_feature :feature_"
Chris@498 653 << featureNumber << " ." << endl << endl;
Chris@498 654
Chris@498 655 stream << ":feature_" << featureNumber << " a ";
Chris@498 656
Chris@498 657 QString signalTypeURI = desc.getOutputSignalTypeURI(outputId);
Chris@498 658 if (plain || signalTypeURI == "") {
Chris@498 659 if (m_syntheticSignalTypeURIs.find(transform) !=
Chris@498 660 m_syntheticSignalTypeURIs.end()) {
Chris@498 661 stream << m_syntheticSignalTypeURIs[transform] << " ;\n";
Chris@498 662 } else {
Chris@498 663 stream << ":signal_type_" << outputId << " ;\n";
Chris@498 664 }
Chris@498 665 } else {
Chris@587 666 stream << "<" << signalTypeURI << "> ;\n";
Chris@498 667 }
Chris@498 668
Chris@498 669 stream << " mo:time ["
Chris@498 670 << "\n a tl:Interval ;"
Chris@498 671 << "\n tl:onTimeLine :feature_timeline_" << featureNumber << " ;";
Chris@498 672
Chris@498 673 RealTime startrt = transform.getStartTime();
Chris@498 674 RealTime durationrt = transform.getDuration();
Chris@498 675
Chris@498 676 int start = RealTime::realTime2Frame(startrt, sampleRate) / stepSize;
Chris@498 677 int duration = RealTime::realTime2Frame(durationrt, sampleRate) / stepSize;
Chris@498 678
Chris@498 679 if (start != 0) {
Chris@498 680 stream << "\n tl:start \"" << start << "\"^^xsd:int ;";
Chris@498 681 }
Chris@498 682 if (duration != 0) {
Chris@498 683 stream << "\n tl:duration \"" << duration << "\"^^xsd:int ;";
Chris@498 684 }
Chris@498 685
Chris@498 686 stream << "\n ] ;\n";
Chris@498 687
Chris@584 688 if (transform.getIdentifier() != "") {
Chris@584 689 stream << " vamp:computed_by " << m_transformURIs[transform] << " ;\n";
Chris@584 690 }
Chris@584 691
Chris@498 692 if (od.hasFixedBinCount) {
Chris@498 693 // We only know the height, so write the width as zero
Chris@498 694 stream << " af:dimensions \"" << od.binCount << " 0\" ;\n";
Chris@498 695 }
Chris@498 696
Chris@498 697 stream << " af:value \"";
Chris@498 698 }
Chris@498 699
Chris@498 700 QString &str = m_openDenseFeatures[sp].second;
Chris@498 701 QTextStream stream(&str);
Chris@498 702
Chris@498 703 for (int i = 0; i < featureList.size(); ++i) {
Chris@498 704
Chris@498 705 const Plugin::Feature &feature = featureList[i];
Chris@498 706
Chris@498 707 for (int j = 0; j < feature.values.size(); ++j) {
Chris@498 708 stream << feature.values[j] << " ";
Chris@498 709 }
Chris@498 710 }
Chris@498 711 }
Chris@498 712
Chris@498 713 void RDFFeatureWriter::finish()
Chris@498 714 {
Chris@578 715 // cerr << "RDFFeatureWriter::finish()" << endl;
Chris@498 716
Chris@498 717 // close any open dense feature literals
Chris@498 718
Chris@498 719 for (map<StringTransformPair, StreamBuffer>::iterator i =
Chris@498 720 m_openDenseFeatures.begin();
Chris@498 721 i != m_openDenseFeatures.end(); ++i) {
Chris@512 722 // cerr << "closing a stream" << endl;
Chris@498 723 StreamBuffer &b = i->second;
Chris@498 724 *(b.first) << b.second << "\" ." << endl;
Chris@498 725 }
Chris@498 726
Chris@498 727 m_openDenseFeatures.clear();
Chris@530 728 m_startedStreamTransforms.clear();
Chris@530 729
Chris@530 730 FileFeatureWriter::finish();
Chris@498 731 }
Chris@498 732
Chris@498 733