annotate rdf/RDFFeatureWriter.cpp @ 594:f3bb7a3ffd89

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