annotate runner/JAMSFeatureWriter.cpp @ 202:cdd825dbba35

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