alo@223: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ alo@223: alo@223: /* alo@223: Sonic Annotator alo@223: A utility for batch feature extraction from audio files. alo@223: Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London. alo@223: Copyright 2007-2014 QMUL. alo@223: alo@223: This program is free software; you can redistribute it and/or alo@223: modify it under the terms of the GNU General Public License as alo@223: published by the Free Software Foundation; either version 2 of the alo@223: License, or (at your option) any later version. See the file alo@223: COPYING included with this distribution for more information. alo@223: */ alo@223: alo@223: #include "JsonLDFeatureWriter.h" alo@223: alo@223: using namespace std; alo@223: using Vamp::Plugin; alo@223: using Vamp::PluginBase; alo@223: alo@223: #include "base/Exceptions.h" alo@223: #include "rdf/PluginRDFIndexer.h" alo@223: alo@223: #include alo@223: #include alo@223: #include alo@223: alo@223: #include "version.h" alo@223: alo@223: JsonLDFeatureWriter::JsonLDFeatureWriter() : alo@223: FileFeatureWriter(SupportOneFilePerTrackTransform | alo@223: SupportOneFilePerTrack | alo@223: SupportOneFileTotal | alo@223: SupportStdOut, alo@223: "json"), alo@223: m_network(false), alo@223: m_networkRetrieved(false), alo@223: m_n(1), alo@223: m_m(1), alo@223: m_digits(6) alo@223: { alo@223: } alo@223: alo@223: JsonLDFeatureWriter::~JsonLDFeatureWriter() alo@223: { alo@223: } alo@223: alo@223: string alo@223: JsonLDFeatureWriter::getDescription() const alo@223: { alo@223: return "Write features to JSON files in JSON-LD format. WARNING: This is a provisional implementation! The output format may change in future releases to comply more effectively with the specification. Please report any problems you find with the current implementation."; alo@223: } alo@223: alo@223: JsonLDFeatureWriter::ParameterList alo@223: JsonLDFeatureWriter::getSupportedParameters() const alo@223: { alo@223: ParameterList pl = FileFeatureWriter::getSupportedParameters(); alo@223: Parameter p; alo@223: alo@223: p.name = "digits"; alo@223: p.description = "Specify the number of significant digits to use when printing transform outputs. Outputs are represented internally using single-precision floating-point, so digits beyond the 8th or 9th place are usually meaningless. The default is 6."; alo@223: p.hasArg = true; alo@223: pl.push_back(p); alo@223: alo@223: p.name = "network"; alo@223: p.description = "Attempt to retrieve RDF descriptions of plugins from network, if not available locally."; alo@223: p.hasArg = false; alo@223: pl.push_back(p); alo@223: alo@223: return pl; alo@223: } alo@223: alo@223: void alo@223: JsonLDFeatureWriter::setParameters(map ¶ms) alo@223: { alo@223: FileFeatureWriter::setParameters(params); alo@223: alo@223: for (map::iterator i = params.begin(); alo@223: i != params.end(); ++i) { alo@223: if (i->first == "network") { alo@223: m_network = true; alo@223: } else if (i->first == "digits") { alo@223: int digits = atoi(i->second.c_str()); alo@223: if (digits <= 0 || digits > 100) { alo@223: cerr << "JsonLDFeatureWriter: ERROR: Invalid or out-of-range value for number of significant digits: " << i->second << endl; alo@223: cerr << "JsonLDFeatureWriter: NOTE: Continuing with default settings" << endl; alo@223: } else { alo@223: m_digits = digits; alo@223: } alo@223: } alo@223: } alo@223: } alo@223: alo@223: void alo@223: JsonLDFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata) alo@223: { alo@223: m_trackMetadata[trackId] = metadata; alo@223: } alo@223: alo@223: static double alo@223: realTime2Sec(const Vamp::RealTime &r) alo@223: { alo@223: return r / Vamp::RealTime(1, 0); alo@223: } alo@223: alo@223: void alo@223: JsonLDFeatureWriter::write(QString trackId, alo@223: const Transform &transform, alo@223: const Plugin::OutputDescriptor& , alo@223: const Plugin::FeatureList& features, alo@223: std::string /* summaryType */) alo@223: { alo@223: QString transformId = transform.getIdentifier(); alo@223: alo@223: QTextStream *sptr = getOutputStream alo@223: (trackId, transformId, QTextCodec::codecForName("UTF-8")); alo@223: if (!sptr) { alo@223: throw FailedToOpenOutputStream(trackId, transformId); alo@223: } alo@223: alo@223: DataId did(trackId, transform); alo@223: alo@223: if (m_data.find(did) == m_data.end()) { alo@223: identifyTask(transform); alo@223: m_streamTracks[sptr].insert(trackId); alo@223: m_streamTasks[sptr].insert(m_tasks[transformId]); alo@223: m_streamData[sptr].insert(did); alo@223: } alo@223: alo@223: if (m_trackTimelineGuids.find(trackId) == m_trackTimelineGuids.end()) { alo@223: QUuid uuid = QUuid::createUuid(); alo@223: m_trackTimelineGuids[trackId] = QString(uuid.toString().replace("{", "").replace("}", "")); alo@223: } alo@223: alo@223: QString d = m_data[did]; alo@223: alo@223: for (int i = 0; i < int(features.size()); ++i) { alo@223: alo@223: if (d != "") { alo@223: d += ",\n"; alo@223: } alo@223: alo@223: d += "\t\t\t{ "; alo@223: alo@223: Plugin::Feature f(features[i]); alo@223: alo@223: QString timestr = f.timestamp.toString().c_str(); alo@223: timestr.replace(QRegExp("^ +"), ""); alo@223: alo@223: QString durstr = "0.0"; alo@223: if (f.hasDuration) { alo@223: durstr = f.duration.toString().c_str(); alo@223: durstr.replace(QRegExp("^ +"), ""); alo@223: d += " \"@type\": \"tl:Interval\", "; alo@223: } alo@223: else{ alo@223: d += " \"@type\": \"tl:Instant\", "; alo@223: } alo@223: alo@223: d += QString("\"tl:at\": %1 ") alo@223: .arg(timestr); alo@223: alo@223: d += QString(", \"tl:timeline\": \"%1\" ") alo@223: .arg(m_trackTimelineGuids[trackId]); alo@223: alo@223: if (f.hasDuration) { alo@223: d += QString(", \"tl:duration\": %2") alo@223: .arg(durstr); alo@223: } alo@223: alo@223: if (f.label != "") { alo@223: if (f.values.empty()) { alo@223: d += QString(", \"afo:value\": \"%2\"").arg(f.label.c_str()); alo@223: } else { alo@223: d += QString(", \"rdfs:label\": \"%2\"").arg(f.label.c_str()); alo@223: } alo@223: } alo@223: alo@223: if (!f.values.empty()) { alo@223: d += QString(", \"afo:value\": "); alo@223: if (f.values.size() > 1) { alo@223: d += "[ "; alo@223: } alo@223: for (int j = 0; j < int(f.values.size()); ++j) { alo@223: if (isnan(f.values[j])) { alo@223: d += "\"NaN\""; alo@223: } else if (isinf(f.values[j])) { alo@223: d += "\"Inf\""; alo@223: } else { alo@223: d += QString("%1").arg(f.values[j], 0, 'g', m_digits); alo@223: } alo@223: if (j + 1 < int(f.values.size())) { alo@223: d += ", "; alo@223: } alo@223: } alo@223: if (f.values.size() > 1) { alo@223: d += " ]"; alo@223: } alo@223: } alo@223: alo@223: d += " }"; alo@223: } alo@223: alo@223: m_data[did] = d; alo@223: } alo@223: alo@223: void alo@223: JsonLDFeatureWriter::setNofM(int n, int m) alo@223: { alo@223: if (m_singleFileName != "" || m_stdout) { alo@223: m_n = n; alo@223: m_m = m; alo@223: } else { alo@223: m_n = 1; alo@223: m_m = 1; alo@223: } alo@223: } alo@223: alo@223: void alo@223: JsonLDFeatureWriter::finish() alo@223: { alo@223: for (FileStreamMap::const_iterator stri = m_streams.begin(); alo@223: stri != m_streams.end(); ++stri) { alo@223: alo@223: QTextStream *sptr = stri->second; alo@223: QTextStream &stream = *sptr; alo@223: alo@223: bool firstInStream = true; alo@223: alo@223: for (TrackIds::const_iterator tri = m_streamTracks[sptr].begin(); alo@223: tri != m_streamTracks[sptr].end(); ++tri) { alo@223: alo@223: TrackId trackId = *tri; alo@223: alo@223: if (firstInStream) { alo@223: if (m_streamTracks[sptr].size() > 1 || (m_m > 1 && m_n == 1)) { alo@223: stream << "[\n"; alo@223: } alo@223: } alo@223: alo@223: if (!firstInStream || (m_m > 1 && m_n > 1)) { alo@223: stream << ",\n"; alo@223: } alo@223: alo@223: stream << "{\n" << writeContext(); alo@223: stream << "\t\"@type\": \"mo:Track\",\n" alo@223: << QString("\t\"mo:available_as\": \"%1\"").arg(QFileInfo(trackId).filePath()); alo@223: alo@223: if (m_trackMetadata.find(trackId) != m_trackMetadata.end()) { alo@223: alo@223: if (m_trackMetadata[trackId].title != "") { alo@223: stream << QString(",\n\t\"dc:title\": \"%1\"") alo@223: .arg(m_trackMetadata[trackId].title); alo@223: } alo@223: alo@223: if (m_trackMetadata[trackId].maker != "") { alo@223: stream << QString(",\n\t\"mo:artist\": { " alo@223: "\t\t\"@type\": \"mo:MusicArtist\",\n" alo@223: "\t\t\"foaf:name\": \"%1\" " alo@223: "\t}") alo@223: .arg(m_trackMetadata[trackId].maker); alo@223: } alo@223: alo@223: QString durstr = m_trackMetadata[trackId].duration.toString().c_str(); alo@223: durstr.replace(QRegExp("^ +"), ""); alo@223: stream << QString(",\n\t\"mo:encodes\": {\n" alo@223: "\t\t\"@type\": \"mo:Signal\",\n" alo@223: "\t\t\"mo:time\": {\n " alo@223: "\t\t\t\"@type\": \"tl:Interval\",\n" alo@223: "\t\t\t\"tl:duration\": \"PT%1S\",\n" alo@223: "\t\t\t\"tl:timeline\": { \"@type\": \"tl:Timeline\", \"@id\": \"%2\" } " alo@223: "\n\t\t}").arg(durstr).arg(m_trackTimelineGuids[trackId]); alo@223: } alo@223: alo@223: stream << "\n\t},\n"; alo@223: stream << "\t\"afo:features\": [\n"; alo@223: alo@223: bool firstInTrack = true; alo@223: alo@223: for (Tasks::const_iterator ti = m_streamTasks[sptr].begin(); alo@223: ti != m_streamTasks[sptr].end(); ++ti) { alo@223: alo@223: Task task = *ti; alo@223: alo@223: for (DataIds::const_iterator di = m_streamData[sptr].begin(); alo@223: di != m_streamData[sptr].end(); ++di) { alo@223: alo@223: DataId did = *di; alo@223: alo@223: QString trackId = did.first; alo@223: Transform transform = did.second; alo@223: alo@223: if (m_tasks[transform.getIdentifier()] != task) continue; alo@223: alo@223: QString data = m_data[did]; alo@223: alo@223: if (!firstInTrack) { alo@223: stream << ",\n"; alo@223: } alo@223: alo@223: stream << QString alo@223: ("\t{\n" alo@223: "\t\t\"@type\": \"afv:%1\",\n" alo@223: "\t\t\"afo:computed_by\": {\n" alo@223: "%2\t\t},\n" alo@223: "\t\t\"afo:values\": [\n") alo@223: .arg(transform.getOutput().replace(0, 1, transform.getOutput().at(0).toUpper())) alo@223: .arg(writeTransformToObjectContents(transform)); alo@223: alo@223: stream << data; alo@223: alo@223: stream << "\n\t\t]\n\t}"; alo@223: firstInTrack = false; alo@223: } alo@223: } alo@223: alo@223: stream << "\n\t]"; alo@223: alo@223: stream << "\n}"; alo@223: firstInStream = false; alo@223: } alo@223: alo@223: if (!firstInStream) { alo@223: if (m_streamTracks[sptr].size() > 1 || (m_m > 1 && m_n == m_m)) { alo@223: stream << "\n\t]"; alo@223: } alo@223: stream << "\n"; alo@223: } alo@223: } alo@223: alo@223: m_streamTracks.clear(); alo@223: m_streamTasks.clear(); alo@223: m_streamData.clear(); alo@223: m_data.clear(); alo@223: alo@223: FileFeatureWriter::finish(); alo@223: } alo@223: alo@223: void alo@223: JsonLDFeatureWriter::loadRDFDescription(const Transform &transform) alo@223: { alo@223: QString pluginId = transform.getPluginIdentifier(); alo@223: if (m_rdfDescriptions.find(pluginId) != m_rdfDescriptions.end()) return; alo@223: alo@223: if (m_network && !m_networkRetrieved) { alo@223: PluginRDFIndexer::getInstance()->indexConfiguredURLs(); alo@223: m_networkRetrieved = true; alo@223: } alo@223: alo@223: m_rdfDescriptions[pluginId] = PluginRDFDescription(pluginId); alo@223: alo@223: if (m_rdfDescriptions[pluginId].haveDescription()) { alo@223: cerr << "NOTE: Have RDF description for plugin ID \"" alo@223: << pluginId << "\"" << endl; alo@223: } else { alo@223: cerr << "NOTE: No RDF description for plugin ID \"" alo@223: << pluginId << "\"" << endl; alo@223: if (!m_network) { Chris@277: cerr << " Consider using the --jsld-network option to retrieve plugin descriptions" << endl; alo@223: cerr << " from the network where possible." << endl; alo@223: } alo@223: } alo@223: } alo@223: alo@223: void alo@223: JsonLDFeatureWriter::identifyTask(const Transform &transform) alo@223: { alo@223: QString transformId = transform.getIdentifier(); alo@223: if (m_tasks.find(transformId) != m_tasks.end()) return; alo@223: alo@223: loadRDFDescription(transform); alo@223: alo@223: Task task = UnknownTask; alo@223: alo@223: QString pluginId = transform.getPluginIdentifier(); alo@223: QString outputId = transform.getOutput(); alo@223: alo@223: const PluginRDFDescription &desc = m_rdfDescriptions[pluginId]; alo@223: alo@223: if (desc.haveDescription()) { alo@223: alo@223: PluginRDFDescription::OutputDisposition disp = alo@223: desc.getOutputDisposition(outputId); alo@223: alo@223: QString af = "http://purl.org/ontology/af/"; alo@223: alo@223: if (disp == PluginRDFDescription::OutputSparse) { alo@223: alo@223: QString eventUri = desc.getOutputEventTypeURI(outputId); alo@223: alo@223: //!!! todo: allow user to prod writer for task type alo@223: alo@223: if (eventUri == af + "Note") { alo@223: task = NoteTask; alo@223: } else if (eventUri == af + "Beat") { alo@223: task = BeatTask; alo@223: } else if (eventUri == af + "ChordSegment") { alo@223: task = ChordTask; alo@223: } else if (eventUri == af + "KeyChange") { alo@223: task = KeyTask; alo@223: } else if (eventUri == af + "KeySegment") { alo@223: task = KeyTask; alo@223: } else if (eventUri == af + "Onset") { alo@223: task = OnsetTask; alo@223: } else if (eventUri == af + "NonTonalOnset") { alo@223: task = OnsetTask; alo@223: } else if (eventUri == af + "Segment") { alo@223: task = SegmentTask; alo@223: } else if (eventUri == af + "SpeechSegment") { alo@223: task = SegmentTask; alo@223: } else if (eventUri == af + "StructuralSegment") { alo@223: task = SegmentTask; alo@223: } else { alo@223: cerr << "WARNING: Unsupported event type URI <" alo@223: << eventUri << ">, proceeding with UnknownTask type" alo@223: << endl; alo@223: } alo@223: alo@223: } else { alo@223: Chris@277: cerr << "WARNING: Cannot currently write dense or track-level outputs to JSON-LD format (only sparse ones). Will proceed using UnknownTask type, but this probably isn't going to work" << endl; alo@223: } alo@223: } alo@223: alo@223: m_tasks[transformId] = task; alo@223: } alo@223: alo@223: QString alo@223: JsonLDFeatureWriter::getTaskKey(Task task) alo@223: { alo@223: switch (task) { alo@223: case UnknownTask: return "unknown"; alo@223: case BeatTask: return "beat"; alo@223: case OnsetTask: return "onset"; alo@223: case ChordTask: return "chord"; alo@223: case SegmentTask: return "segment"; alo@223: case KeyTask: return "key"; alo@223: case NoteTask: return "note"; alo@223: case MelodyTask: return "melody"; alo@223: case PitchTask: return "pitch"; alo@223: } alo@223: return "unknown"; alo@223: } alo@223: alo@223: QString alo@223: JsonLDFeatureWriter::writeTransformToObjectContents(const Transform &t) alo@223: { alo@223: QString json; alo@223: QString stpl("\t\t\t\"%1\": \"%2\",\n"); alo@223: QString ntpl("\t\t\t\"%1\": %2,\n"); alo@223: alo@223: json += stpl.arg("@type").arg("vamp:Transform"); alo@223: json += stpl.arg("vamp:plugin_id").arg(t.getPluginIdentifier()); alo@223: json += stpl.arg("vamp:output_id").arg(t.getOutput()); alo@223: alo@223: if (t.getSummaryType() != Transform::NoSummary) { alo@223: json += stpl.arg("vamp:summary_type") alo@223: .arg(Transform::summaryTypeToString(t.getSummaryType())); alo@223: } alo@223: alo@223: if (t.getPluginVersion() != QString()) { alo@223: json += stpl.arg("vamp:plugin_version").arg(t.getPluginVersion()); alo@223: } alo@223: alo@223: if (t.getProgram() != QString()) { alo@223: json += stpl.arg("vamp:program").arg(t.getProgram()); alo@223: } alo@223: alo@223: if (t.getStepSize() != 0) { alo@223: json += ntpl.arg("vamp:step_size").arg(t.getStepSize()); alo@223: } alo@223: alo@223: if (t.getBlockSize() != 0) { alo@223: json += ntpl.arg("vamp:block_size").arg(t.getBlockSize()); alo@223: } alo@223: alo@223: if (t.getWindowType() != HanningWindow) { alo@223: json += stpl.arg("vamp:window_type") alo@223: .arg(Window::getNameForType(t.getWindowType()).c_str()); alo@223: } alo@223: alo@223: if (t.getStartTime() != RealTime::zeroTime) { alo@223: json += ntpl.arg("tl:start") alo@223: .arg(t.getStartTime().toDouble(), 0, 'g', 9); alo@223: } alo@223: alo@223: if (t.getDuration() != RealTime::zeroTime) { alo@223: json += ntpl.arg("tl:duration") alo@223: .arg(t.getDuration().toDouble(), 0, 'g', 9); alo@223: } alo@223: alo@223: if (t.getSampleRate() != 0) { alo@223: json += ntpl.arg("vamp:sample_rate").arg(t.getSampleRate()); alo@223: } alo@223: alo@223: if (!t.getParameters().empty()) { alo@223: json += QString("\t\t\t\"vamp:parameter_binding\": [\n"); alo@223: Transform::ParameterMap parameters = t.getParameters(); alo@223: for (Transform::ParameterMap::const_iterator i = parameters.begin(); alo@223: i != parameters.end(); ++i) { alo@223: if (i != parameters.begin()) { alo@223: json += ",\n"; alo@223: } alo@223: QString name = i->first; alo@223: float value = i->second; alo@223: json += QString("\t\t\t\t{\n"); alo@223: json += QString("\t\t\t\t\t\"@type\": \"vamp:Parameter\",\n"); alo@223: json += QString("\t\t\t\t\t\"vamp:identifier\": \"%1\",\n").arg(name); alo@223: json += QString("\t\t\t\t\t\"vamp:value\": %1").arg(value, 0, 'g', 8); alo@223: json += QString("\n\t\t\t\t}"); alo@223: } alo@223: json += QString("\n\t\t\t],\n"); alo@223: } alo@223: alo@223: // no trailing comma on final property: alo@223: json += QString("\t\t\t\"vamp:transform_id\": \"%1\",\n").arg(t.getIdentifier()); alo@223: json += QString("\t\t\t\"afo:implemented_in\": {\n"); alo@223: json += QString("\t\t\t\t\"@type\": \"afo:SoftwareAgent\",\n"); alo@223: json += QString("\t\t\t\t\"afo:name\": \"Sonic Annotator\",\n"); alo@223: json += QString("\t\t\t\t\"afo:version\": \"%1\" \n").arg(RUNNER_VERSION); alo@223: json += QString("\t\t\t}\n"); alo@223: alo@223: return json; alo@223: } alo@223: alo@223: QString alo@223: JsonLDFeatureWriter::writeContext() { alo@223: QString context; alo@223: context += QString("\t\"@context\": {\n"); alo@223: context += QString("\t\t\"foaf\": \"http://xmlns.com/foaf/0.1/\",\n"); alo@223: context += QString("\t\t\"afo\": \"http://sovarr.c4dm.eecs.qmul.ac.uk/af/ontology/1.1#\",\n"); alo@223: context += QString("\t\t\"afv\": \"http://sovarr.c4dm.eecs.qmul.ac.uk/af/vocabulary/1.1#\",\n"); alo@223: context += QString("\t\t\"mo\": \"http://purl.org/ontology/mo/\",\n"); alo@223: context += QString("\t\t\"dc\": \"http://purl.org/dc/elements/1.1/\",\n"); alo@223: context += QString("\t\t\"tl\": \"http://purl.org/NET/c4dm/timeline.owl#\",\n"); alo@223: context += QString("\t\t\"vamp\": \"http://purl.org/ontology/vamp/\"\n"); alo@223: context += QString("\t},\n"); alo@223: return context; alo@223: }