annotate rdf/RDFFeatureWriter.cpp @ 510:af7b6e55895b

* Ensure text models are exported with text properties in RDF, and imported back into text models again (instead of time/value models)
author Chris Cannam
date Mon, 08 Dec 2008 11:53:10 +0000
parents 1b8c748fd7ea
children e7a39c45a7a4
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@498 58 p.name = "signal-uri";
Chris@498 59 p.description = "Link the output RDF to the given signal URI.";
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@498 76 if (i->first == "signal-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@498 121 if (!stream) return; //!!! this is probably better handled with an exception
Chris@498 122
Chris@498 123 if (m_startedStreamTransforms.find(stream) ==
Chris@498 124 m_startedStreamTransforms.end()) {
Chris@498 125 cerr << "This stream is new, writing prefixes" << endl;
Chris@498 126 writePrefixes(stream);
Chris@498 127 if (m_singleFileName == "" && !m_stdout) {
Chris@498 128 writeSignalDescription(stream, trackId);
Chris@498 129 }
Chris@498 130 }
Chris@498 131
Chris@498 132 if (m_startedStreamTransforms[stream].find(transform) ==
Chris@498 133 m_startedStreamTransforms[stream].end()) {
Chris@498 134 m_startedStreamTransforms[stream].insert(transform);
Chris@498 135 writeLocalFeatureTypes
Chris@498 136 (stream, transform, output, m_rdfDescriptions[pluginId]);
Chris@498 137 }
Chris@498 138
Chris@498 139 if (m_singleFileName != "" || m_stdout) {
Chris@498 140 if (m_startedTrackIds.find(trackId) == m_startedTrackIds.end()) {
Chris@498 141 writeSignalDescription(stream, trackId);
Chris@498 142 m_startedTrackIds.insert(trackId);
Chris@498 143 }
Chris@498 144 }
Chris@498 145
Chris@498 146 QString timelineURI = m_trackTimelineURIs[trackId];
Chris@498 147
Chris@498 148 if (timelineURI == "") {
Chris@498 149 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing features without having established a timeline URI!" << endl;
Chris@498 150 exit(1);
Chris@498 151 }
Chris@498 152
Chris@498 153 if (summaryType != "") {
Chris@498 154
Chris@498 155 writeSparseRDF(stream, transform, output, features,
Chris@498 156 m_rdfDescriptions[pluginId], timelineURI);
Chris@498 157
Chris@498 158 } else if (m_rdfDescriptions[pluginId].haveDescription() &&
Chris@498 159 m_rdfDescriptions[pluginId].getOutputDisposition
Chris@498 160 (output.identifier.c_str()) ==
Chris@498 161 PluginRDFDescription::OutputDense) {
Chris@498 162
Chris@498 163 QString signalURI = m_trackSignalURIs[trackId];
Chris@498 164
Chris@498 165 if (signalURI == "") {
Chris@498 166 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing dense features without having established a signal URI!" << endl;
Chris@498 167 exit(1);
Chris@498 168 }
Chris@498 169
Chris@498 170 writeDenseRDF(stream, transform, output, features,
Chris@498 171 m_rdfDescriptions[pluginId], signalURI, timelineURI);
Chris@498 172
Chris@507 173 } else if (!m_plain &&
Chris@507 174 m_rdfDescriptions[pluginId].haveDescription() &&
Chris@507 175 m_rdfDescriptions[pluginId].getOutputDisposition
Chris@507 176 (output.identifier.c_str()) ==
Chris@507 177 PluginRDFDescription::OutputTrackLevel &&
Chris@507 178 m_rdfDescriptions[pluginId].getOutputFeatureAttributeURI
Chris@507 179 (output.identifier.c_str()) != "") {
Chris@507 180
Chris@507 181 QString signalURI = m_trackSignalURIs[trackId];
Chris@507 182
Chris@507 183 if (signalURI == "") {
Chris@507 184 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing track-level features without having established a signal URI!" << endl;
Chris@507 185 exit(1);
Chris@507 186 }
Chris@507 187
Chris@507 188 writeTrackLevelRDF(stream, transform, output, features,
Chris@507 189 m_rdfDescriptions[pluginId], signalURI);
Chris@507 190
Chris@498 191 } else {
Chris@498 192
Chris@498 193 writeSparseRDF(stream, transform, output, features,
Chris@498 194 m_rdfDescriptions[pluginId], timelineURI);
Chris@498 195 }
Chris@498 196 }
Chris@498 197
Chris@498 198 void
Chris@498 199 RDFFeatureWriter::writePrefixes(QTextStream *sptr)
Chris@498 200 {
Chris@498 201 QTextStream &stream = *sptr;
Chris@498 202
Chris@498 203 stream << "@prefix dc: <http://purl.org/dc/elements/1.1/> .\n"
Chris@498 204 << "@prefix mo: <http://purl.org/ontology/mo/> .\n"
Chris@498 205 << "@prefix af: <http://purl.org/ontology/af/> .\n"
Chris@504 206 << "@prefix foaf: <http://xmlns.com/foaf/0.1/> . \n"
Chris@498 207 << "@prefix event: <http://purl.org/NET/c4dm/event.owl#> .\n"
Chris@498 208 << "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n"
Chris@498 209 << "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
Chris@498 210 << "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
Chris@498 211 << "@prefix tl: <http://purl.org/NET/c4dm/timeline.owl#> .\n"
Chris@498 212 << "@prefix vamp: <http://purl.org/ontology/vamp/> .\n"
Chris@498 213 << "@prefix : <#> .\n\n";
Chris@498 214 }
Chris@498 215
Chris@498 216 void
Chris@498 217 RDFFeatureWriter::writeSignalDescription(QTextStream *sptr,
Chris@498 218 QString trackId)
Chris@498 219 {
Chris@498 220 QTextStream &stream = *sptr;
Chris@498 221
Chris@498 222 /*
Chris@498 223 * Describe signal we're analysing (AudioFile, Signal, TimeLine, etc.)
Chris@498 224 */
Chris@498 225
Chris@498 226 QUrl url(trackId);
Chris@498 227 QString scheme = url.scheme().toLower();
Chris@498 228 bool local = (scheme == "" || scheme == "file" || scheme.length() == 1);
Chris@498 229
Chris@498 230 if (local) {
Chris@498 231 if (scheme == "") {
Chris@498 232 url.setScheme("file");
Chris@508 233 url.setPath(QFileInfo(url.path()).absoluteFilePath());
Chris@498 234 } else if (scheme.length() == 1) { // DOS drive letter!
Chris@498 235 url.setScheme("file");
Chris@498 236 url.setPath(scheme + ":" + url.path());
Chris@498 237 }
Chris@498 238 }
Chris@498 239
Chris@498 240 //!!! FIX: If we are appending, we need to start counting after
Chris@498 241 //all of the existing counts that are already in the file!
Chris@498 242
Chris@498 243 uint64_t signalCount = m_count++;
Chris@498 244
Chris@498 245 if (m_trackSignalURIs.find(trackId) == m_trackSignalURIs.end()) {
Chris@498 246 m_trackSignalURIs[trackId] = QString(":signal_%1").arg(signalCount);
Chris@498 247 }
Chris@498 248
Chris@498 249 if (m_suri != NULL) {
Chris@498 250 m_trackSignalURIs[trackId] = "<" + m_suri + ">";
Chris@498 251 }
Chris@498 252 QString signalURI = m_trackSignalURIs[trackId];
Chris@498 253
Chris@498 254 if (m_trackTimelineURIs.find(trackId) == m_trackTimelineURIs.end()) {
Chris@498 255 m_trackTimelineURIs[trackId] = QString(":signal_timeline_%1").arg(signalCount);
Chris@498 256 }
Chris@498 257 QString timelineURI = m_trackTimelineURIs[trackId];
Chris@498 258
Chris@500 259 if (trackId != "") {
Chris@500 260 stream << "\n<" << url.toEncoded().data() << "> a mo:AudioFile .\n\n";
Chris@500 261 }
Chris@500 262
Chris@500 263 stream << signalURI << " a mo:Signal ;\n";
Chris@500 264
Chris@500 265 if (trackId != "") {
Chris@500 266 stream << " mo:available_as <" << url.toEncoded().data()
Chris@500 267 << "> ;\n";
Chris@500 268 }
Chris@500 269
Chris@504 270 if (m_metadata.find(trackId) != m_metadata.end()) {
Chris@504 271 TrackMetadata tm = m_metadata[trackId];
Chris@504 272 if (tm.title != "") {
Chris@504 273 stream << " dc:title \"\"\"" << tm.title << "\"\"\" ;\n";
Chris@504 274 }
Chris@504 275 if (tm.maker != "") {
Chris@507 276 stream << " foaf:maker [ a mo:MusicArtist; foaf:name \"\"\"" << tm.maker << "\"\"\" ] ;\n";
Chris@504 277 }
Chris@504 278 }
Chris@504 279
Chris@500 280 stream << " mo:time [\n"
Chris@498 281 << " a tl:Interval ;\n"
Chris@498 282 << " tl:onTimeLine "
Chris@498 283 << timelineURI << "\n ] .\n\n";
Chris@498 284 }
Chris@498 285
Chris@498 286 void
Chris@498 287 RDFFeatureWriter::writeLocalFeatureTypes(QTextStream *sptr,
Chris@498 288 const Transform &transform,
Chris@498 289 const Plugin::OutputDescriptor &od,
Chris@498 290 PluginRDFDescription &desc)
Chris@498 291 {
Chris@498 292 QString outputId = od.identifier.c_str();
Chris@498 293 QTextStream &stream = *sptr;
Chris@498 294
Chris@507 295 // There is no "needFeatureType" for track-level outputs, because
Chris@507 296 // we can't meaningfully write a feature at all if we don't know
Chris@507 297 // what property to use for it. If the output is track level but
Chris@507 298 // there is no feature type given, we have to revert to events.
Chris@507 299
Chris@498 300 bool needEventType = false;
Chris@498 301 bool needSignalType = false;
Chris@498 302
Chris@498 303 //!!! bin names, extents and so on can be written out using e.g. vamp:bin_names ( "a" "b" "c" )
Chris@498 304
Chris@498 305 if (desc.getOutputDisposition(outputId) ==
Chris@498 306 PluginRDFDescription::OutputDense) {
Chris@498 307
Chris@498 308 // no feature events, so may need signal type but won't need
Chris@498 309 // event type
Chris@498 310
Chris@498 311 if (m_plain) {
Chris@498 312
Chris@498 313 needSignalType = true;
Chris@498 314
Chris@498 315 } else if (desc.getOutputSignalTypeURI(outputId) == "") {
Chris@498 316
Chris@498 317 needSignalType = true;
Chris@498 318 }
Chris@498 319
Chris@507 320 } else if (desc.getOutputDisposition(outputId) ==
Chris@507 321 PluginRDFDescription::OutputTrackLevel) {
Chris@507 322
Chris@507 323 // see note above -- need to generate an event type if no
Chris@507 324 // feature type given, or if in plain mode
Chris@507 325
Chris@507 326 if (m_plain) {
Chris@507 327
Chris@507 328 needEventType = true;
Chris@507 329
Chris@507 330 } else if (desc.getOutputFeatureAttributeURI(outputId) == "") {
Chris@507 331
Chris@507 332 if (desc.getOutputEventTypeURI(outputId) == "") {
Chris@507 333
Chris@507 334 needEventType = true;
Chris@507 335 }
Chris@507 336 }
Chris@507 337
Chris@498 338 } else {
Chris@498 339
Chris@498 340 // may need event type but won't need signal type
Chris@498 341
Chris@498 342 if (m_plain) {
Chris@498 343
Chris@498 344 needEventType = true;
Chris@498 345
Chris@498 346 } else if (desc.getOutputEventTypeURI(outputId) == "") {
Chris@498 347
Chris@498 348 needEventType = true;
Chris@498 349 }
Chris@498 350 }
Chris@498 351
Chris@498 352 QString transformUri;
Chris@498 353 if (m_transformURIs.find(transform) != m_transformURIs.end()) {
Chris@498 354 transformUri = m_transformURIs[transform];
Chris@498 355 } else {
Chris@498 356 transformUri = QString(":transform_%1_%2").arg(m_count++).arg(outputId);
Chris@498 357 m_transformURIs[transform] = transformUri;
Chris@498 358 }
Chris@498 359
Chris@500 360 if (transform.getIdentifier() != "") {
Chris@508 361 stream << endl
Chris@508 362 << RDFTransformFactory::writeTransformToRDF(transform, transformUri)
Chris@500 363 << endl;
Chris@500 364 }
Chris@498 365
Chris@510 366 if (needEventType && m_fixedEventTypeURI == "") {
Chris@498 367
Chris@498 368 QString uri;
Chris@498 369 if (m_syntheticEventTypeURIs.find(transform) !=
Chris@498 370 m_syntheticEventTypeURIs.end()) {
Chris@498 371 uri = m_syntheticEventTypeURIs[transform];
Chris@498 372 } else {
Chris@498 373 uri = QString(":event_type_%1").arg(m_count++);
Chris@498 374 m_syntheticEventTypeURIs[transform] = uri;
Chris@498 375 }
Chris@498 376
Chris@498 377 stream << uri
Chris@498 378 << " rdfs:subClassOf event:Event ;" << endl
Chris@498 379 << " dc:title \"" << od.name.c_str() << "\" ;" << endl
Chris@498 380 << " dc:format \"" << od.unit.c_str() << "\" ;" << endl
Chris@498 381 << " dc:description \"" << od.description.c_str() << "\" ."
Chris@498 382 << endl << endl;
Chris@498 383 }
Chris@498 384
Chris@498 385 if (needSignalType) {
Chris@498 386
Chris@498 387 QString uri;
Chris@498 388 if (m_syntheticSignalTypeURIs.find(transform) !=
Chris@498 389 m_syntheticSignalTypeURIs.end()) {
Chris@498 390 uri = m_syntheticSignalTypeURIs[transform];
Chris@498 391 } else {
Chris@498 392 uri = QString(":signal_type_%1").arg(m_count++);
Chris@498 393 m_syntheticSignalTypeURIs[transform] = uri;
Chris@498 394 }
Chris@498 395
Chris@498 396 stream << uri
Chris@498 397 << " rdfs:subClassOf af:Signal ;" << endl
Chris@498 398 << " dc:title \"" << od.name.c_str() << "\" ;" << endl
Chris@498 399 << " dc:format \"" << od.unit.c_str() << "\" ;" << endl
Chris@498 400 << " dc:description \"" << od.description.c_str() << "\" ."
Chris@498 401 << endl << endl;
Chris@498 402 }
Chris@498 403 }
Chris@498 404
Chris@498 405 void
Chris@498 406 RDFFeatureWriter::writeSparseRDF(QTextStream *sptr,
Chris@498 407 const Transform &transform,
Chris@498 408 const Plugin::OutputDescriptor& od,
Chris@498 409 const Plugin::FeatureList& featureList,
Chris@498 410 PluginRDFDescription &desc,
Chris@498 411 QString timelineURI)
Chris@498 412 {
Chris@498 413 if (featureList.empty()) return;
Chris@498 414 QTextStream &stream = *sptr;
Chris@498 415
Chris@498 416 bool plain = (m_plain || !desc.haveDescription());
Chris@498 417
Chris@498 418 QString outputId = od.identifier.c_str();
Chris@498 419
Chris@498 420 // iterate through FeatureLists
Chris@498 421
Chris@498 422 for (int i = 0; i < featureList.size(); ++i) {
Chris@498 423
Chris@498 424 const Plugin::Feature &feature = featureList[i];
Chris@498 425 uint64_t featureNumber = m_count++;
Chris@498 426
Chris@498 427 stream << ":event_" << featureNumber << " a ";
Chris@498 428
Chris@510 429 if (m_fixedEventTypeURI != "") {
Chris@510 430 stream << m_fixedEventTypeURI << " ;\n";
Chris@510 431 } else {
Chris@510 432 QString eventTypeURI = desc.getOutputEventTypeURI(outputId);
Chris@510 433 if (plain || eventTypeURI == "") {
Chris@510 434 if (m_syntheticEventTypeURIs.find(transform) !=
Chris@510 435 m_syntheticEventTypeURIs.end()) {
Chris@510 436 stream << m_syntheticEventTypeURIs[transform] << " ;\n";
Chris@510 437 } else {
Chris@510 438 stream << ":event_type_" << outputId << " ;\n";
Chris@510 439 }
Chris@498 440 } else {
Chris@510 441 stream << "<" << eventTypeURI << "> ;\n";
Chris@498 442 }
Chris@498 443 }
Chris@498 444
Chris@498 445 QString timestamp = feature.timestamp.toString().c_str();
Chris@498 446 timestamp.replace(QRegExp("^ +"), "");
Chris@498 447
Chris@498 448 if (feature.hasDuration && feature.duration > Vamp::RealTime::zeroTime) {
Chris@498 449
Chris@498 450 QString duration = feature.duration.toString().c_str();
Chris@498 451 duration.replace(QRegExp("^ +"), "");
Chris@498 452
Chris@498 453 stream << " event:time [ \n"
Chris@498 454 << " a tl:Interval ;\n"
Chris@498 455 << " tl:onTimeLine " << timelineURI << " ;\n"
Chris@498 456 << " tl:beginsAt \"PT" << timestamp
Chris@498 457 << "S\"^^xsd:duration ;\n"
Chris@498 458 << " tl:duration \"PT" << duration
Chris@498 459 << "S\"^^xsd:duration ;\n"
Chris@498 460 << " ] ";
Chris@498 461
Chris@498 462 } else {
Chris@498 463
Chris@498 464 stream << " event:time [ \n"
Chris@498 465 << " a tl:Instant ;\n" //location of the event in time
Chris@498 466 << " tl:onTimeLine " << timelineURI << " ;\n"
Chris@498 467 << " tl:at \"PT" << timestamp
Chris@498 468 << "S\"^^xsd:duration ;\n ] ";
Chris@498 469 }
Chris@498 470
Chris@500 471 if (transform.getIdentifier() != "") {
Chris@500 472 stream << ";\n";
Chris@500 473 stream << " vamp:computed_by " << m_transformURIs[transform] << " ";
Chris@500 474 }
Chris@498 475
Chris@498 476 if (feature.label.length() > 0) {
Chris@498 477 stream << ";\n";
Chris@510 478 stream << " rdfs:label \"\"\"" << feature.label.c_str() << "\"\"\" ";
Chris@498 479 }
Chris@498 480
Chris@498 481 if (!feature.values.empty()) {
Chris@498 482 stream << ";\n";
Chris@498 483 //!!! named bins?
Chris@498 484 stream << " af:feature \"" << feature.values[0];
Chris@498 485 for (int j = 1; j < feature.values.size(); ++j) {
Chris@498 486 stream << " " << feature.values[j];
Chris@498 487 }
Chris@498 488 stream << "\" ";
Chris@498 489 }
Chris@498 490
Chris@498 491 stream << ".\n";
Chris@498 492 }
Chris@498 493 }
Chris@498 494
Chris@498 495 void
Chris@507 496 RDFFeatureWriter::writeTrackLevelRDF(QTextStream *sptr,
Chris@507 497 const Transform &transform,
Chris@507 498 const Plugin::OutputDescriptor& od,
Chris@507 499 const Plugin::FeatureList& featureList,
Chris@507 500 PluginRDFDescription &desc,
Chris@507 501 QString signalURI)
Chris@507 502 {
Chris@507 503 if (featureList.empty()) return;
Chris@507 504 QTextStream &stream = *sptr;
Chris@507 505
Chris@507 506 bool plain = (m_plain || !desc.haveDescription());
Chris@507 507
Chris@507 508 QString outputId = od.identifier.c_str();
Chris@507 509 QString featureUri = desc.getOutputFeatureAttributeURI(outputId);
Chris@507 510
Chris@507 511 if (featureUri == "") {
Chris@507 512 cerr << "RDFFeatureWriter::writeTrackLevelRDF: ERROR: No feature URI available -- this function should not have been called!" << endl;
Chris@507 513 return;
Chris@507 514 }
Chris@507 515
Chris@507 516 for (int i = 0; i < featureList.size(); ++i) {
Chris@507 517
Chris@507 518 const Plugin::Feature &feature = featureList[i];
Chris@507 519
Chris@507 520 if (feature.values.empty()) {
Chris@507 521
Chris@507 522 if (feature.label == "") continue;
Chris@507 523
Chris@508 524 stream << signalURI << " " << featureUri << " \"\"\""
Chris@508 525 << feature.label.c_str() << "\"\"\" .\n";
Chris@507 526
Chris@507 527 } else {
Chris@507 528
Chris@507 529 stream << signalURI << " " << featureUri << " \""
Chris@507 530 << feature.values[0] << "\"^^xsd:float .\n";
Chris@507 531 }
Chris@507 532 }
Chris@507 533 }
Chris@507 534
Chris@507 535 void
Chris@498 536 RDFFeatureWriter::writeDenseRDF(QTextStream *sptr,
Chris@498 537 const Transform &transform,
Chris@498 538 const Plugin::OutputDescriptor& od,
Chris@498 539 const Plugin::FeatureList& featureList,
Chris@498 540 PluginRDFDescription &desc,
Chris@498 541 QString signalURI,
Chris@498 542 QString timelineURI)
Chris@498 543 {
Chris@498 544 if (featureList.empty()) return;
Chris@498 545
Chris@498 546 StringTransformPair sp(signalURI, transform);
Chris@498 547
Chris@498 548 if (m_openDenseFeatures.find(sp) == m_openDenseFeatures.end()) {
Chris@498 549
Chris@498 550 StreamBuffer b(sptr, "");
Chris@498 551 m_openDenseFeatures[sp] = b;
Chris@498 552
Chris@498 553 QString &str(m_openDenseFeatures[sp].second);
Chris@498 554 QTextStream stream(&str);
Chris@498 555
Chris@498 556 bool plain = (m_plain || !desc.haveDescription());
Chris@498 557 QString outputId = od.identifier.c_str();
Chris@498 558
Chris@498 559 uint64_t featureNumber = m_count++;
Chris@498 560
Chris@498 561 // need to write out feature timeline map -- for this we need
Chris@498 562 // the sample rate, window length and hop size from the
Chris@498 563 // transform
Chris@498 564
Chris@498 565 stream << "\n:feature_timeline_" << featureNumber << " a tl:DiscreteTimeLine .\n\n";
Chris@498 566
Chris@498 567 size_t stepSize = transform.getStepSize();
Chris@498 568 if (stepSize == 0) {
Chris@498 569 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing dense features without having set the step size properly!" << endl;
Chris@498 570 return;
Chris@498 571 }
Chris@498 572
Chris@498 573 size_t blockSize = transform.getBlockSize();
Chris@498 574 if (blockSize == 0) {
Chris@498 575 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing dense features without having set the block size properly!" << endl;
Chris@498 576 return;
Chris@498 577 }
Chris@498 578
Chris@498 579 float sampleRate = transform.getSampleRate();
Chris@498 580 if (sampleRate == 0.f) {
Chris@498 581 cerr << "RDFFeatureWriter: INTERNAL ERROR: writing dense features without having set the sample rate properly!" << endl;
Chris@498 582 return;
Chris@498 583 }
Chris@498 584
Chris@498 585 stream << ":feature_timeline_map_" << featureNumber
Chris@498 586 << " a tl:UniformSamplingWindowingMap ;\n"
Chris@498 587 << " tl:rangeTimeLine :feature_timeline_" << featureNumber << " ;\n"
Chris@498 588 << " tl:domainTimeLine " << timelineURI << " ;\n"
Chris@498 589 << " tl:sampleRate \"" << int(sampleRate) << "\"^^xsd:int ;\n"
Chris@498 590 << " tl:windowLength \"" << blockSize << "\"^^xsd:int ;\n"
Chris@498 591 << " tl:hopSize \"" << stepSize << "\"^^xsd:int .\n\n";
Chris@498 592
Chris@498 593 stream << signalURI << " af:signal_feature :feature_"
Chris@498 594 << featureNumber << " ." << endl << endl;
Chris@498 595
Chris@498 596 stream << ":feature_" << featureNumber << " a ";
Chris@498 597
Chris@498 598 QString signalTypeURI = desc.getOutputSignalTypeURI(outputId);
Chris@498 599 if (plain || signalTypeURI == "") {
Chris@498 600 if (m_syntheticSignalTypeURIs.find(transform) !=
Chris@498 601 m_syntheticSignalTypeURIs.end()) {
Chris@498 602 stream << m_syntheticSignalTypeURIs[transform] << " ;\n";
Chris@498 603 } else {
Chris@498 604 stream << ":signal_type_" << outputId << " ;\n";
Chris@498 605 }
Chris@498 606 } else {
Chris@498 607 stream << signalTypeURI << " ;\n";
Chris@498 608 }
Chris@498 609
Chris@498 610 stream << " mo:time ["
Chris@498 611 << "\n a tl:Interval ;"
Chris@498 612 << "\n tl:onTimeLine :feature_timeline_" << featureNumber << " ;";
Chris@498 613
Chris@498 614 RealTime startrt = transform.getStartTime();
Chris@498 615 RealTime durationrt = transform.getDuration();
Chris@498 616
Chris@498 617 int start = RealTime::realTime2Frame(startrt, sampleRate) / stepSize;
Chris@498 618 int duration = RealTime::realTime2Frame(durationrt, sampleRate) / stepSize;
Chris@498 619
Chris@498 620 if (start != 0) {
Chris@498 621 stream << "\n tl:start \"" << start << "\"^^xsd:int ;";
Chris@498 622 }
Chris@498 623 if (duration != 0) {
Chris@498 624 stream << "\n tl:duration \"" << duration << "\"^^xsd:int ;";
Chris@498 625 }
Chris@498 626
Chris@498 627 stream << "\n ] ;\n";
Chris@498 628
Chris@498 629 if (od.hasFixedBinCount) {
Chris@498 630 // We only know the height, so write the width as zero
Chris@498 631 stream << " af:dimensions \"" << od.binCount << " 0\" ;\n";
Chris@498 632 }
Chris@498 633
Chris@498 634 stream << " af:value \"";
Chris@498 635 }
Chris@498 636
Chris@498 637 QString &str = m_openDenseFeatures[sp].second;
Chris@498 638 QTextStream stream(&str);
Chris@498 639
Chris@498 640 for (int i = 0; i < featureList.size(); ++i) {
Chris@498 641
Chris@498 642 const Plugin::Feature &feature = featureList[i];
Chris@498 643
Chris@498 644 for (int j = 0; j < feature.values.size(); ++j) {
Chris@498 645 stream << feature.values[j] << " ";
Chris@498 646 }
Chris@498 647 }
Chris@498 648 }
Chris@498 649
Chris@498 650 void RDFFeatureWriter::finish()
Chris@498 651 {
Chris@498 652 // cerr << "RDFFeatureWriter::finish()" << endl;
Chris@498 653
Chris@498 654 // close any open dense feature literals
Chris@498 655
Chris@498 656 for (map<StringTransformPair, StreamBuffer>::iterator i =
Chris@498 657 m_openDenseFeatures.begin();
Chris@498 658 i != m_openDenseFeatures.end(); ++i) {
Chris@498 659 cerr << "closing a stream" << endl;
Chris@498 660 StreamBuffer &b = i->second;
Chris@498 661 *(b.first) << b.second << "\" ." << endl;
Chris@498 662 }
Chris@498 663
Chris@498 664 m_openDenseFeatures.clear();
Chris@498 665 }
Chris@498 666
Chris@498 667