annotate runner/JAMSFeatureWriter.cpp @ 153:ad96fd5f9cd7 jams

Output some data!
author Chris Cannam
date Tue, 14 Oct 2014 14:03:34 +0100
parents db83ea0e102d
children 4b19d824a213
rev   line source
Chris@145 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@145 2
Chris@145 3 /*
Chris@145 4 Sonic Annotator
Chris@145 5 A utility for batch feature extraction from audio files.
Chris@145 6 Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London.
Chris@145 7 Copyright 2007-2014 QMUL.
Chris@145 8
Chris@145 9 This program is free software; you can redistribute it and/or
Chris@145 10 modify it under the terms of the GNU General Public License as
Chris@145 11 published by the Free Software Foundation; either version 2 of the
Chris@145 12 License, or (at your option) any later version. See the file
Chris@145 13 COPYING included with this distribution for more information.
Chris@145 14 */
Chris@145 15
Chris@145 16 #include "JAMSFeatureWriter.h"
Chris@145 17
Chris@145 18 using namespace std;
Chris@145 19 using Vamp::Plugin;
Chris@145 20 using Vamp::PluginBase;
Chris@145 21
Chris@145 22 #include "base/Exceptions.h"
Chris@145 23 #include "rdf/PluginRDFIndexer.h"
Chris@145 24
Chris@145 25 JAMSFeatureWriter::JAMSFeatureWriter() :
Chris@145 26 FileFeatureWriter(SupportOneFilePerTrackTransform |
Chris@145 27 SupportOneFilePerTrack |
Chris@152 28 SupportOneFileTotal |
Chris@145 29 SupportStdOut,
Chris@145 30 "json"),
Chris@145 31 m_network(false),
Chris@145 32 m_networkRetrieved(false)
Chris@145 33 {
Chris@145 34 }
Chris@145 35
Chris@145 36 JAMSFeatureWriter::~JAMSFeatureWriter()
Chris@145 37 {
Chris@145 38 }
Chris@145 39
Chris@145 40 string
Chris@145 41 JAMSFeatureWriter::getDescription() const
Chris@145 42 {
Chris@145 43 return "Write features to JSON files in JAMS (JSON Annotated Music Specification) format.";
Chris@145 44 }
Chris@145 45
Chris@145 46 JAMSFeatureWriter::ParameterList
Chris@145 47 JAMSFeatureWriter::getSupportedParameters() const
Chris@145 48 {
Chris@145 49 ParameterList pl = FileFeatureWriter::getSupportedParameters();
Chris@145 50 Parameter p;
Chris@145 51
Chris@145 52 p.name = "network";
Chris@145 53 p.description = "Attempt to retrieve RDF descriptions of plugins from network, if not available locally";
Chris@145 54 p.hasArg = false;
Chris@145 55 pl.push_back(p);
Chris@145 56
Chris@145 57 return pl;
Chris@145 58 }
Chris@145 59
Chris@145 60 void
Chris@145 61 JAMSFeatureWriter::setParameters(map<string, string> &params)
Chris@145 62 {
Chris@145 63 FileFeatureWriter::setParameters(params);
Chris@145 64
Chris@145 65 for (map<string, string>::iterator i = params.begin();
Chris@145 66 i != params.end(); ++i) {
Chris@145 67 if (i->first == "network") {
Chris@145 68 m_network = true;
Chris@145 69 }
Chris@145 70 }
Chris@145 71 }
Chris@145 72
Chris@145 73 void
Chris@145 74 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata)
Chris@145 75 {
Chris@145 76 QString json
Chris@153 77 ("\n\"file_metadata\":\n"
Chris@152 78 " { \"artist\": \"%1\",\n"
Chris@152 79 " \"title\": \"%2\" },\n");
Chris@145 80 m_metadata[trackId] = json.arg(metadata.maker).arg(metadata.title);
Chris@152 81 cerr << "setTrackMetadata: metadata is: " << m_metadata[trackId] << endl;
Chris@145 82 }
Chris@145 83
Chris@153 84 static double
Chris@153 85 realTime2Sec(const Vamp::RealTime &r)
Chris@153 86 {
Chris@153 87 return r / Vamp::RealTime(1, 0);
Chris@153 88 }
Chris@153 89
Chris@145 90 void
Chris@145 91 JAMSFeatureWriter::write(QString trackId,
Chris@145 92 const Transform &transform,
Chris@145 93 const Plugin::OutputDescriptor& ,
Chris@145 94 const Plugin::FeatureList& features,
Chris@145 95 std::string /* summaryType */)
Chris@145 96 {
Chris@145 97 QString transformId = transform.getIdentifier();
Chris@145 98
Chris@145 99 QTextStream *sptr = getOutputStream(trackId, transformId);
Chris@145 100 if (!sptr) {
Chris@145 101 throw FailedToOpenOutputStream(trackId, transformId);
Chris@145 102 }
Chris@145 103
Chris@145 104 QTextStream &stream = *sptr;
Chris@145 105
Chris@152 106 TrackTransformPair tt(trackId, transformId);
Chris@152 107 TrackTransformPair targetKey = getFilenameKey(trackId, transformId);
Chris@152 108
Chris@152 109 if (m_startedTargets.find(targetKey) == m_startedTargets.end()) {
Chris@152 110 // Need to write track-level preamble
Chris@152 111 stream << "{" << m_metadata[trackId] << endl;
Chris@152 112 m_startedTargets.insert(targetKey);
Chris@152 113 }
Chris@152 114
Chris@153 115 bool justBegun = false;
Chris@153 116
Chris@152 117 if (m_data.find(tt) == m_data.end()) {
Chris@145 118
Chris@145 119 identifyTask(transform);
Chris@145 120
Chris@152 121 QString json("\"%1\": [ ");
Chris@152 122 m_data[tt] = json.arg(getTaskKey(m_tasks[transformId]));
Chris@153 123 justBegun = true;
Chris@145 124 }
Chris@145 125
Chris@153 126 QString d = m_data[tt];
Chris@153 127
Chris@145 128 for (int i = 0; i < int(features.size()); ++i) {
Chris@153 129
Chris@153 130 if (i > 0 || !justBegun) {
Chris@153 131 d += ",\n";
Chris@153 132 } else {
Chris@153 133 d += "\n";
Chris@153 134 }
Chris@153 135
Chris@153 136 d += " { ";
Chris@145 137
Chris@153 138 Plugin::Feature f(features[i]);
Chris@153 139
Chris@153 140 switch (m_tasks[transformId]) {
Chris@153 141
Chris@153 142 case ChordTask:
Chris@153 143 case SegmentTask:
Chris@153 144 case NoteTask:
Chris@153 145 case UnknownTask:
Chris@153 146 if (f.hasDuration) {
Chris@153 147 d += QString
Chris@153 148 ("\"start\": { \"value\": %1 }, "
Chris@153 149 "\"end\": { \"value\": %2 }")
Chris@153 150 .arg(realTime2Sec(f.timestamp))
Chris@153 151 .arg(realTime2Sec
Chris@153 152 (f.timestamp +
Chris@153 153 (f.hasDuration ? f.duration : Vamp::RealTime::zeroTime)));
Chris@153 154 break;
Chris@153 155 } else {
Chris@153 156 // don't break; fall through to simpler no-duration case
Chris@153 157 }
Chris@153 158
Chris@153 159 case BeatTask:
Chris@153 160 case KeyTask:
Chris@153 161 case OnsetTask:
Chris@153 162 d += QString("\"time\": { \"value\": %1 }")
Chris@153 163 .arg(realTime2Sec(f.timestamp));
Chris@153 164 break;
Chris@153 165 }
Chris@153 166
Chris@153 167 if (f.label != "") {
Chris@153 168 d += QString(", \"label\": { \"value\": \"%2\" }")
Chris@153 169 .arg(f.label.c_str());
Chris@153 170 } else if (f.values.size() > 0) {
Chris@153 171 d += QString(", \"label\": { \"value\": \"%2\" }")
Chris@153 172 .arg(f.values[0]);
Chris@153 173 }
Chris@153 174
Chris@153 175 d += " }";
Chris@145 176 }
Chris@153 177
Chris@153 178 m_data[tt] = d;
Chris@145 179 }
Chris@145 180
Chris@145 181 void
Chris@152 182 JAMSFeatureWriter::finish()
Chris@152 183 {
Chris@152 184 cerr << "Finish called on " << this << endl;
Chris@152 185
Chris@152 186 set<QTextStream *> startedStreams;
Chris@152 187
Chris@152 188 for (DataMap::const_iterator i = m_data.begin();
Chris@152 189 i != m_data.end(); ++i) {
Chris@152 190
Chris@152 191 TrackTransformPair tt = i->first;
Chris@152 192 QString data = i->second;
Chris@152 193
Chris@152 194 QTextStream *sptr = getOutputStream(tt.first, tt.second);
Chris@152 195 if (!sptr) {
Chris@152 196 throw FailedToOpenOutputStream(tt.first, tt.second);
Chris@152 197 }
Chris@152 198
Chris@152 199 if (startedStreams.find(sptr) != startedStreams.end()) {
Chris@152 200 *sptr << "," << endl;
Chris@152 201 }
Chris@152 202 startedStreams.insert(sptr);
Chris@152 203
Chris@153 204 *sptr << data << "\n ]";
Chris@152 205 }
Chris@152 206
Chris@152 207 for (FileStreamMap::const_iterator i = m_streams.begin();
Chris@152 208 i != m_streams.end(); ++i) {
Chris@152 209 *(i->second) << endl << "}" << endl;
Chris@152 210 }
Chris@152 211
Chris@152 212 m_data.clear();
Chris@152 213 m_startedTargets.clear();
Chris@152 214
Chris@152 215 FileFeatureWriter::finish();
Chris@152 216 }
Chris@152 217
Chris@152 218 void
Chris@145 219 JAMSFeatureWriter::loadRDFDescription(const Transform &transform)
Chris@145 220 {
Chris@145 221 QString pluginId = transform.getPluginIdentifier();
Chris@145 222 if (m_rdfDescriptions.find(pluginId) != m_rdfDescriptions.end()) return;
Chris@145 223
Chris@145 224 if (m_network && !m_networkRetrieved) {
Chris@145 225 PluginRDFIndexer::getInstance()->indexConfiguredURLs();
Chris@145 226 m_networkRetrieved = true;
Chris@145 227 }
Chris@145 228
Chris@145 229 m_rdfDescriptions[pluginId] = PluginRDFDescription(pluginId);
Chris@145 230
Chris@145 231 if (m_rdfDescriptions[pluginId].haveDescription()) {
Chris@145 232 cerr << "NOTE: Have RDF description for plugin ID \""
Chris@145 233 << pluginId << "\"" << endl;
Chris@145 234 } else {
Chris@145 235 cerr << "NOTE: No RDF description for plugin ID \""
Chris@145 236 << pluginId << "\"" << endl;
Chris@145 237 if (!m_network) {
Chris@145 238 cerr << " Consider using the --json-network option to retrieve plugin descriptions" << endl;
Chris@145 239 cerr << " from the network where possible." << endl;
Chris@145 240 }
Chris@145 241 }
Chris@145 242 }
Chris@145 243
Chris@145 244 void
Chris@145 245 JAMSFeatureWriter::identifyTask(const Transform &transform)
Chris@145 246 {
Chris@145 247 QString transformId = transform.getIdentifier();
Chris@145 248 if (m_tasks.find(transformId) != m_tasks.end()) return;
Chris@145 249
Chris@145 250 loadRDFDescription(transform);
Chris@145 251
Chris@145 252 Task task = UnknownTask;
Chris@145 253
Chris@145 254 QString pluginId = transform.getPluginIdentifier();
Chris@145 255 QString outputId = transform.getOutput();
Chris@145 256
Chris@145 257 const PluginRDFDescription &desc = m_rdfDescriptions[pluginId];
Chris@145 258
Chris@145 259 if (desc.haveDescription()) {
Chris@145 260
Chris@145 261 PluginRDFDescription::OutputDisposition disp =
Chris@145 262 desc.getOutputDisposition(outputId);
Chris@145 263
Chris@145 264 QString af = "http://purl.org/ontology/af/";
Chris@145 265
Chris@145 266 if (disp == PluginRDFDescription::OutputSparse) {
Chris@145 267
Chris@145 268 QString eventUri = desc.getOutputEventTypeURI(outputId);
Chris@145 269
Chris@145 270 //!!! todo: allow user to prod writer for task type
Chris@145 271
Chris@145 272 if (eventUri == af + "Note") {
Chris@145 273 task = NoteTask;
Chris@145 274 } else if (eventUri == af + "Beat") {
Chris@145 275 task = BeatTask;
Chris@145 276 } else if (eventUri == af + "ChordSegment") {
Chris@145 277 task = ChordTask;
Chris@145 278 } else if (eventUri == af + "KeyChange") {
Chris@145 279 task = KeyTask;
Chris@145 280 } else if (eventUri == af + "KeySegment") {
Chris@145 281 task = KeyTask;
Chris@145 282 } else if (eventUri == af + "Onset") {
Chris@145 283 task = OnsetTask;
Chris@145 284 } else if (eventUri == af + "NonTonalOnset") {
Chris@145 285 task = OnsetTask;
Chris@145 286 } else if (eventUri == af + "Segment") {
Chris@145 287 task = SegmentTask;
Chris@145 288 } else if (eventUri == af + "SpeechSegment") {
Chris@145 289 task = SegmentTask;
Chris@145 290 } else if (eventUri == af + "StructuralSegment") {
Chris@145 291 task = SegmentTask;
Chris@145 292 } else {
Chris@145 293 cerr << "WARNING: Unsupported event type URI <"
Chris@145 294 << eventUri << ">, proceeding with UnknownTask type"
Chris@145 295 << endl;
Chris@145 296 }
Chris@145 297
Chris@145 298 } else {
Chris@145 299
Chris@145 300 cerr << "WARNING: Cannot currently write dense or track-level outputs to JSON format (only sparse ones). Will proceed using UnknownTask type, but this probably isn't going to work" << endl;
Chris@145 301 }
Chris@145 302 }
Chris@145 303
Chris@145 304 m_tasks[transformId] = task;
Chris@145 305 }
Chris@145 306
Chris@145 307 QString
Chris@145 308 JAMSFeatureWriter::getTaskKey(Task task)
Chris@145 309 {
Chris@145 310 switch (task) {
Chris@145 311 case UnknownTask: return "unknown";
Chris@145 312 case BeatTask: return "beat";
Chris@145 313 case OnsetTask: return "onset";
Chris@145 314 case ChordTask: return "chord";
Chris@145 315 case SegmentTask: return "segment";
Chris@145 316 case KeyTask: return "key";
Chris@145 317 case NoteTask: return "note";
Chris@145 318 case MelodyTask: return "melody";
Chris@145 319 case PitchTask: return "pitch";
Chris@145 320 }
Chris@145 321 return "unknown";
Chris@145 322 }