annotate runner/JAMSFeatureWriter.cpp @ 169:859d8ec60e06 jams

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