annotate runner/JAMSFeatureWriter.cpp @ 165:d0be35a305cc jams

Add transform metadata to output
author Chris Cannam
date Wed, 15 Oct 2014 13:30:25 +0100
parents 447230267c0d
children e98b1abeb792
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@162 25 #include "version.h"
Chris@162 26
Chris@145 27 JAMSFeatureWriter::JAMSFeatureWriter() :
Chris@145 28 FileFeatureWriter(SupportOneFilePerTrackTransform |
Chris@145 29 SupportOneFilePerTrack |
Chris@152 30 SupportOneFileTotal |
Chris@145 31 SupportStdOut,
Chris@145 32 "json"),
Chris@145 33 m_network(false),
Chris@145 34 m_networkRetrieved(false)
Chris@145 35 {
Chris@145 36 }
Chris@145 37
Chris@145 38 JAMSFeatureWriter::~JAMSFeatureWriter()
Chris@145 39 {
Chris@145 40 }
Chris@145 41
Chris@145 42 string
Chris@145 43 JAMSFeatureWriter::getDescription() const
Chris@145 44 {
Chris@145 45 return "Write features to JSON files in JAMS (JSON Annotated Music Specification) format.";
Chris@145 46 }
Chris@145 47
Chris@145 48 JAMSFeatureWriter::ParameterList
Chris@145 49 JAMSFeatureWriter::getSupportedParameters() const
Chris@145 50 {
Chris@145 51 ParameterList pl = FileFeatureWriter::getSupportedParameters();
Chris@145 52 Parameter p;
Chris@145 53
Chris@145 54 p.name = "network";
Chris@145 55 p.description = "Attempt to retrieve RDF descriptions of plugins from network, if not available locally";
Chris@145 56 p.hasArg = false;
Chris@145 57 pl.push_back(p);
Chris@145 58
Chris@145 59 return pl;
Chris@145 60 }
Chris@145 61
Chris@145 62 void
Chris@145 63 JAMSFeatureWriter::setParameters(map<string, string> &params)
Chris@145 64 {
Chris@145 65 FileFeatureWriter::setParameters(params);
Chris@145 66
Chris@145 67 for (map<string, string>::iterator i = params.begin();
Chris@145 68 i != params.end(); ++i) {
Chris@145 69 if (i->first == "network") {
Chris@145 70 m_network = true;
Chris@145 71 }
Chris@145 72 }
Chris@145 73 }
Chris@145 74
Chris@145 75 void
Chris@145 76 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata)
Chris@145 77 {
Chris@145 78 QString json
Chris@162 79 ("\n\"file_metadata\": {\n"
Chris@162 80 " \"artist\": \"%1\",\n"
Chris@162 81 " \"title\": \"%2\"\n},\n");
Chris@145 82 m_metadata[trackId] = json.arg(metadata.maker).arg(metadata.title);
Chris@152 83 cerr << "setTrackMetadata: metadata is: " << m_metadata[trackId] << endl;
Chris@145 84 }
Chris@145 85
Chris@153 86 static double
Chris@153 87 realTime2Sec(const Vamp::RealTime &r)
Chris@153 88 {
Chris@153 89 return r / Vamp::RealTime(1, 0);
Chris@153 90 }
Chris@153 91
Chris@145 92 void
Chris@145 93 JAMSFeatureWriter::write(QString trackId,
Chris@145 94 const Transform &transform,
Chris@145 95 const Plugin::OutputDescriptor& ,
Chris@145 96 const Plugin::FeatureList& features,
Chris@145 97 std::string /* summaryType */)
Chris@145 98 {
Chris@145 99 QString transformId = transform.getIdentifier();
Chris@145 100
Chris@145 101 QTextStream *sptr = getOutputStream(trackId, transformId);
Chris@145 102 if (!sptr) {
Chris@145 103 throw FailedToOpenOutputStream(trackId, transformId);
Chris@145 104 }
Chris@145 105
Chris@145 106 QTextStream &stream = *sptr;
Chris@145 107
Chris@152 108 TrackTransformPair tt(trackId, transformId);
Chris@152 109 TrackTransformPair targetKey = getFilenameKey(trackId, transformId);
Chris@152 110
Chris@152 111 if (m_startedTargets.find(targetKey) == m_startedTargets.end()) {
Chris@152 112 // Need to write track-level preamble
Chris@152 113 stream << "{" << m_metadata[trackId] << endl;
Chris@152 114 m_startedTargets.insert(targetKey);
Chris@152 115 }
Chris@152 116
Chris@153 117 bool justBegun = false;
Chris@153 118
Chris@152 119 if (m_data.find(tt) == m_data.end()) {
Chris@145 120
Chris@145 121 identifyTask(transform);
Chris@145 122
Chris@162 123 QString json
Chris@162 124 ("\"%1\": [ { \n"
Chris@162 125 " \"annotation_metadata\": {\n"
Chris@162 126 " \"annotation_tools\": \"Sonic Annotator v%2\",\n"
Chris@162 127 " \"data_source\": \"Automatic feature extraction\",\n"
Chris@165 128 " \"annotator\": {\n"
Chris@165 129 "%3"
Chris@165 130 " },\n"
Chris@162 131 " },\n"
Chris@162 132 " \"data\": [");
Chris@162 133 m_data[tt] = json
Chris@162 134 .arg(getTaskKey(m_tasks[transformId]))
Chris@162 135 .arg(RUNNER_VERSION)
Chris@165 136 .arg(writeTransformToObjectContents(transform));
Chris@153 137 justBegun = true;
Chris@145 138 }
Chris@145 139
Chris@153 140 QString d = m_data[tt];
Chris@153 141
Chris@145 142 for (int i = 0; i < int(features.size()); ++i) {
Chris@153 143
Chris@153 144 if (i > 0 || !justBegun) {
Chris@153 145 d += ",\n";
Chris@153 146 } else {
Chris@153 147 d += "\n";
Chris@153 148 }
Chris@153 149
Chris@153 150 d += " { ";
Chris@145 151
Chris@153 152 Plugin::Feature f(features[i]);
Chris@153 153
Chris@153 154 switch (m_tasks[transformId]) {
Chris@153 155
Chris@153 156 case ChordTask:
Chris@153 157 case SegmentTask:
Chris@153 158 case NoteTask:
Chris@153 159 case UnknownTask:
Chris@153 160 if (f.hasDuration) {
Chris@153 161 d += QString
Chris@153 162 ("\"start\": { \"value\": %1 }, "
Chris@153 163 "\"end\": { \"value\": %2 }")
Chris@153 164 .arg(realTime2Sec(f.timestamp))
Chris@153 165 .arg(realTime2Sec
Chris@153 166 (f.timestamp +
Chris@153 167 (f.hasDuration ? f.duration : Vamp::RealTime::zeroTime)));
Chris@153 168 break;
Chris@153 169 } else {
Chris@153 170 // don't break; fall through to simpler no-duration case
Chris@153 171 }
Chris@153 172
Chris@153 173 case BeatTask:
Chris@153 174 case KeyTask:
Chris@153 175 case OnsetTask:
Chris@153 176 d += QString("\"time\": { \"value\": %1 }")
Chris@153 177 .arg(realTime2Sec(f.timestamp));
Chris@153 178 break;
Chris@161 179
Chris@161 180 case MelodyTask:
Chris@161 181 case PitchTask:
Chris@161 182 //!!!
Chris@161 183 break;
Chris@153 184 }
Chris@153 185
Chris@153 186 if (f.label != "") {
Chris@153 187 d += QString(", \"label\": { \"value\": \"%2\" }")
Chris@153 188 .arg(f.label.c_str());
Chris@153 189 } else if (f.values.size() > 0) {
Chris@153 190 d += QString(", \"label\": { \"value\": \"%2\" }")
Chris@153 191 .arg(f.values[0]);
Chris@153 192 }
Chris@153 193
Chris@153 194 d += " }";
Chris@145 195 }
Chris@153 196
Chris@153 197 m_data[tt] = d;
Chris@145 198 }
Chris@145 199
Chris@145 200 void
Chris@152 201 JAMSFeatureWriter::finish()
Chris@152 202 {
Chris@152 203 cerr << "Finish called on " << this << endl;
Chris@152 204
Chris@152 205 set<QTextStream *> startedStreams;
Chris@152 206
Chris@152 207 for (DataMap::const_iterator i = m_data.begin();
Chris@152 208 i != m_data.end(); ++i) {
Chris@152 209
Chris@152 210 TrackTransformPair tt = i->first;
Chris@152 211 QString data = i->second;
Chris@152 212
Chris@152 213 QTextStream *sptr = getOutputStream(tt.first, tt.second);
Chris@152 214 if (!sptr) {
Chris@152 215 throw FailedToOpenOutputStream(tt.first, tt.second);
Chris@152 216 }
Chris@152 217
Chris@152 218 if (startedStreams.find(sptr) != startedStreams.end()) {
Chris@152 219 *sptr << "," << endl;
Chris@152 220 }
Chris@152 221 startedStreams.insert(sptr);
Chris@152 222
Chris@162 223 *sptr << data << "\n ]\n} ]";
Chris@152 224 }
Chris@152 225
Chris@152 226 for (FileStreamMap::const_iterator i = m_streams.begin();
Chris@152 227 i != m_streams.end(); ++i) {
Chris@152 228 *(i->second) << endl << "}" << endl;
Chris@152 229 }
Chris@152 230
Chris@152 231 m_data.clear();
Chris@152 232 m_startedTargets.clear();
Chris@152 233
Chris@152 234 FileFeatureWriter::finish();
Chris@152 235 }
Chris@152 236
Chris@152 237 void
Chris@145 238 JAMSFeatureWriter::loadRDFDescription(const Transform &transform)
Chris@145 239 {
Chris@145 240 QString pluginId = transform.getPluginIdentifier();
Chris@145 241 if (m_rdfDescriptions.find(pluginId) != m_rdfDescriptions.end()) return;
Chris@145 242
Chris@145 243 if (m_network && !m_networkRetrieved) {
Chris@145 244 PluginRDFIndexer::getInstance()->indexConfiguredURLs();
Chris@145 245 m_networkRetrieved = true;
Chris@145 246 }
Chris@145 247
Chris@145 248 m_rdfDescriptions[pluginId] = PluginRDFDescription(pluginId);
Chris@145 249
Chris@145 250 if (m_rdfDescriptions[pluginId].haveDescription()) {
Chris@145 251 cerr << "NOTE: Have RDF description for plugin ID \""
Chris@145 252 << pluginId << "\"" << endl;
Chris@145 253 } else {
Chris@145 254 cerr << "NOTE: No RDF description for plugin ID \""
Chris@145 255 << pluginId << "\"" << endl;
Chris@145 256 if (!m_network) {
Chris@145 257 cerr << " Consider using the --json-network option to retrieve plugin descriptions" << endl;
Chris@145 258 cerr << " from the network where possible." << endl;
Chris@145 259 }
Chris@145 260 }
Chris@145 261 }
Chris@145 262
Chris@145 263 void
Chris@145 264 JAMSFeatureWriter::identifyTask(const Transform &transform)
Chris@145 265 {
Chris@145 266 QString transformId = transform.getIdentifier();
Chris@145 267 if (m_tasks.find(transformId) != m_tasks.end()) return;
Chris@145 268
Chris@145 269 loadRDFDescription(transform);
Chris@145 270
Chris@145 271 Task task = UnknownTask;
Chris@145 272
Chris@145 273 QString pluginId = transform.getPluginIdentifier();
Chris@145 274 QString outputId = transform.getOutput();
Chris@145 275
Chris@145 276 const PluginRDFDescription &desc = m_rdfDescriptions[pluginId];
Chris@145 277
Chris@145 278 if (desc.haveDescription()) {
Chris@145 279
Chris@145 280 PluginRDFDescription::OutputDisposition disp =
Chris@145 281 desc.getOutputDisposition(outputId);
Chris@145 282
Chris@145 283 QString af = "http://purl.org/ontology/af/";
Chris@145 284
Chris@145 285 if (disp == PluginRDFDescription::OutputSparse) {
Chris@145 286
Chris@145 287 QString eventUri = desc.getOutputEventTypeURI(outputId);
Chris@145 288
Chris@145 289 //!!! todo: allow user to prod writer for task type
Chris@145 290
Chris@145 291 if (eventUri == af + "Note") {
Chris@145 292 task = NoteTask;
Chris@145 293 } else if (eventUri == af + "Beat") {
Chris@145 294 task = BeatTask;
Chris@145 295 } else if (eventUri == af + "ChordSegment") {
Chris@145 296 task = ChordTask;
Chris@145 297 } else if (eventUri == af + "KeyChange") {
Chris@145 298 task = KeyTask;
Chris@145 299 } else if (eventUri == af + "KeySegment") {
Chris@145 300 task = KeyTask;
Chris@145 301 } else if (eventUri == af + "Onset") {
Chris@145 302 task = OnsetTask;
Chris@145 303 } else if (eventUri == af + "NonTonalOnset") {
Chris@145 304 task = OnsetTask;
Chris@145 305 } else if (eventUri == af + "Segment") {
Chris@145 306 task = SegmentTask;
Chris@145 307 } else if (eventUri == af + "SpeechSegment") {
Chris@145 308 task = SegmentTask;
Chris@145 309 } else if (eventUri == af + "StructuralSegment") {
Chris@145 310 task = SegmentTask;
Chris@145 311 } else {
Chris@145 312 cerr << "WARNING: Unsupported event type URI <"
Chris@145 313 << eventUri << ">, proceeding with UnknownTask type"
Chris@145 314 << endl;
Chris@145 315 }
Chris@145 316
Chris@145 317 } else {
Chris@145 318
Chris@145 319 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 320 }
Chris@145 321 }
Chris@145 322
Chris@145 323 m_tasks[transformId] = task;
Chris@145 324 }
Chris@145 325
Chris@145 326 QString
Chris@145 327 JAMSFeatureWriter::getTaskKey(Task task)
Chris@145 328 {
Chris@145 329 switch (task) {
Chris@145 330 case UnknownTask: return "unknown";
Chris@145 331 case BeatTask: return "beat";
Chris@145 332 case OnsetTask: return "onset";
Chris@145 333 case ChordTask: return "chord";
Chris@145 334 case SegmentTask: return "segment";
Chris@145 335 case KeyTask: return "key";
Chris@145 336 case NoteTask: return "note";
Chris@145 337 case MelodyTask: return "melody";
Chris@145 338 case PitchTask: return "pitch";
Chris@145 339 }
Chris@145 340 return "unknown";
Chris@145 341 }
Chris@165 342
Chris@165 343 QString
Chris@165 344 JAMSFeatureWriter::writeTransformToObjectContents(const Transform &t)
Chris@165 345 {
Chris@165 346 QString json;
Chris@165 347 QString stpl(" \"%1\": \"%2\",\n");
Chris@165 348 QString ntpl(" \"%1\": %2,\n");
Chris@165 349
Chris@165 350 json += stpl.arg("plugin_id").arg(t.getPluginIdentifier());
Chris@165 351 json += stpl.arg("output_id").arg(t.getOutput());
Chris@165 352
Chris@165 353 if (t.getSummaryType() != Transform::NoSummary) {
Chris@165 354 json += stpl.arg("summary_type")
Chris@165 355 .arg(Transform::summaryTypeToString(t.getSummaryType()));
Chris@165 356 }
Chris@165 357
Chris@165 358 if (t.getPluginVersion() != QString()) {
Chris@165 359 json += stpl.arg("plugin_version").arg(t.getPluginVersion());
Chris@165 360 }
Chris@165 361
Chris@165 362 if (t.getProgram() != QString()) {
Chris@165 363 json += stpl.arg("program").arg(t.getProgram());
Chris@165 364 }
Chris@165 365
Chris@165 366 if (t.getStepSize() != 0) {
Chris@165 367 json += ntpl.arg("step_size").arg(t.getStepSize());
Chris@165 368 }
Chris@165 369
Chris@165 370 if (t.getBlockSize() != 0) {
Chris@165 371 json += ntpl.arg("block_size").arg(t.getBlockSize());
Chris@165 372 }
Chris@165 373
Chris@165 374 if (t.getWindowType() != HanningWindow) {
Chris@165 375 json += stpl.arg("window_type")
Chris@165 376 .arg(Window<float>::getNameForType(t.getWindowType()).c_str());
Chris@165 377 }
Chris@165 378
Chris@165 379 if (t.getStartTime() != RealTime::zeroTime) {
Chris@165 380 json += ntpl.arg("start").arg(t.getStartTime().toDouble());
Chris@165 381 }
Chris@165 382
Chris@165 383 if (t.getDuration() != RealTime::zeroTime) {
Chris@165 384 json += ntpl.arg("duration").arg(t.getDuration().toDouble());
Chris@165 385 }
Chris@165 386
Chris@165 387 if (t.getSampleRate() != 0) {
Chris@165 388 json += ntpl.arg("sample_rate").arg(t.getSampleRate());
Chris@165 389 }
Chris@165 390
Chris@165 391 if (!t.getParameters().empty()) {
Chris@165 392 json += QString(" \"parameters\": {\n");
Chris@165 393 Transform::ParameterMap parameters = t.getParameters();
Chris@165 394 for (Transform::ParameterMap::const_iterator i = parameters.begin();
Chris@165 395 i != parameters.end(); ++i) {
Chris@165 396 QString name = i->first;
Chris@165 397 float value = i->second;
Chris@165 398 json += QString(" \"%1\": %2\n").arg(name).arg(value);
Chris@165 399 }
Chris@165 400 json += QString(" },\n");
Chris@165 401 }
Chris@165 402
Chris@165 403 // no trailing comma on final property:
Chris@165 404 json += QString(" \"transform_id\": \"%1\"\n").arg(t.getIdentifier());
Chris@165 405
Chris@165 406 return json;
Chris@165 407 }
Chris@165 408