annotate runner/JAMSFeatureWriter.cpp @ 166:e98b1abeb792 jams

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