annotate runner/JAMSFeatureWriter.cpp @ 236:a0d093495a1f

Activate summary value tests
author Chris Cannam
date Tue, 01 Mar 2016 12:20:47 +0000
parents c17b184c16db
children e257f776a589
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@204 39 m_m(1),
Chris@204 40 m_digits(6)
Chris@145 41 {
Chris@145 42 }
Chris@145 43
Chris@145 44 JAMSFeatureWriter::~JAMSFeatureWriter()
Chris@145 45 {
Chris@145 46 }
Chris@145 47
Chris@145 48 string
Chris@145 49 JAMSFeatureWriter::getDescription() const
Chris@145 50 {
Chris@170 51 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 52 }
Chris@145 53
Chris@145 54 JAMSFeatureWriter::ParameterList
Chris@145 55 JAMSFeatureWriter::getSupportedParameters() const
Chris@145 56 {
Chris@145 57 ParameterList pl = FileFeatureWriter::getSupportedParameters();
Chris@145 58 Parameter p;
Chris@145 59
Chris@204 60 p.name = "digits";
Chris@204 61 p.description = "Specify the number of significant digits to use when printing transform outputs. Outputs are represented internally using single-precision floating-point, so digits beyond the 8th or 9th place are usually meaningless. The default is 6.";
Chris@204 62 p.hasArg = true;
Chris@204 63 pl.push_back(p);
Chris@204 64
Chris@145 65 p.name = "network";
Chris@197 66 p.description = "Attempt to retrieve RDF descriptions of plugins from network, if not available locally.";
Chris@145 67 p.hasArg = false;
Chris@145 68 pl.push_back(p);
Chris@145 69
Chris@145 70 return pl;
Chris@145 71 }
Chris@145 72
Chris@145 73 void
Chris@145 74 JAMSFeatureWriter::setParameters(map<string, string> &params)
Chris@145 75 {
Chris@145 76 FileFeatureWriter::setParameters(params);
Chris@145 77
Chris@145 78 for (map<string, string>::iterator i = params.begin();
Chris@145 79 i != params.end(); ++i) {
Chris@145 80 if (i->first == "network") {
Chris@145 81 m_network = true;
Chris@204 82 } else if (i->first == "digits") {
Chris@204 83 int digits = atoi(i->second.c_str());
Chris@204 84 if (digits <= 0 || digits > 100) {
Chris@204 85 cerr << "JAMSFeatureWriter: ERROR: Invalid or out-of-range value for number of significant digits: " << i->second << endl;
Chris@204 86 cerr << "JAMSFeatureWriter: NOTE: Continuing with default settings" << endl;
Chris@204 87 } else {
Chris@204 88 m_digits = digits;
Chris@204 89 }
Chris@145 90 }
Chris@145 91 }
Chris@145 92 }
Chris@145 93
Chris@145 94 void
Chris@145 95 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata)
Chris@145 96 {
Chris@167 97 m_trackMetadata[trackId] = metadata;
Chris@145 98 }
Chris@145 99
Chris@153 100 static double
Chris@153 101 realTime2Sec(const Vamp::RealTime &r)
Chris@153 102 {
Chris@153 103 return r / Vamp::RealTime(1, 0);
Chris@153 104 }
Chris@153 105
Chris@145 106 void
Chris@145 107 JAMSFeatureWriter::write(QString trackId,
Chris@145 108 const Transform &transform,
Chris@145 109 const Plugin::OutputDescriptor& ,
Chris@145 110 const Plugin::FeatureList& features,
Chris@145 111 std::string /* summaryType */)
Chris@145 112 {
Chris@145 113 QString transformId = transform.getIdentifier();
Chris@145 114
Chris@189 115 QTextStream *sptr = getOutputStream
Chris@189 116 (trackId, transformId, QTextCodec::codecForName("UTF-8"));
Chris@145 117 if (!sptr) {
Chris@145 118 throw FailedToOpenOutputStream(trackId, transformId);
Chris@145 119 }
Chris@145 120
Chris@167 121 DataId did(trackId, transform);
Chris@145 122
Chris@167 123 if (m_data.find(did) == m_data.end()) {
Chris@167 124 identifyTask(transform);
Chris@167 125 m_streamTracks[sptr].insert(trackId);
Chris@167 126 m_streamTasks[sptr].insert(m_tasks[transformId]);
Chris@167 127 m_streamData[sptr].insert(did);
Chris@152 128 }
Chris@152 129
Chris@167 130 QString d = m_data[did];
Chris@153 131
Chris@145 132 for (int i = 0; i < int(features.size()); ++i) {
Chris@153 133
Chris@167 134 if (d != "") {
Chris@153 135 d += ",\n";
Chris@153 136 }
Chris@153 137
Chris@153 138 d += " { ";
Chris@145 139
Chris@153 140 Plugin::Feature f(features[i]);
Chris@153 141
Chris@204 142 QString timestr = f.timestamp.toString().c_str();
Chris@204 143 timestr.replace(QRegExp("^ +"), "");
Chris@204 144
Chris@208 145 QString durstr = "0.0";
Chris@168 146 if (f.hasDuration) {
Chris@208 147 durstr = f.duration.toString().c_str();
Chris@208 148 durstr.replace(QRegExp("^ +"), "");
Chris@153 149 }
Chris@153 150
Chris@208 151 d += QString("\"time\": %1, \"duration\": %2, \"confidence\": 1.0")
Chris@208 152 .arg(timestr).arg(durstr);
Chris@208 153
Chris@208 154 // here we have to differ from the JAMS 0.2.0 spec. It allows
Chris@208 155 // a single "value" element which can be either a number or a
Chris@208 156 // string, depending on the selected task. But we may have
Chris@208 157 // many values and may have a label as well, and no way to
Chris@208 158 // know whether these can be made to conform to the JAMS task
Chris@208 159 // schema. We should just write what we have. If we only have
Chris@208 160 // a label, we can write that out as "value" as JAMS requests,
Chris@208 161 // but if we have a (numerical) value and a label, we really
Chris@208 162 // have to write them separately, and if we have multiple
Chris@208 163 // values we'll have to use an array. The chances of actually
Chris@208 164 // ending up with a schema-compliant JAMS format are quite
Chris@208 165 // small, which suggests JAMS isn't a great idea for this
Chris@208 166 // after all!
Chris@208 167
Chris@153 168 if (f.label != "") {
Chris@208 169 if (f.values.empty()) {
Chris@208 170 d += QString(", \"value\": \"%2\"").arg(f.label.c_str());
Chris@208 171 } else {
Chris@208 172 d += QString(", \"label\": \"%2\"").arg(f.label.c_str());
Chris@208 173 }
Chris@167 174 }
Chris@167 175
Chris@208 176 if (!f.values.empty()) {
Chris@208 177 d += QString(", \"value\": ");
Chris@208 178 if (f.values.size() > 1) {
Chris@208 179 d += "[ ";
Chris@208 180 }
Chris@167 181 for (int j = 0; j < int(f.values.size()); ++j) {
Chris@169 182 if (isnan(f.values[j])) {
Chris@192 183 d += "\"NaN\"";
Chris@169 184 } else if (isinf(f.values[j])) {
Chris@192 185 d += "\"Inf\"";
Chris@169 186 } else {
Chris@204 187 d += QString("%1").arg(f.values[j], 0, 'g', m_digits);
Chris@192 188 }
Chris@192 189 if (j + 1 < int(f.values.size())) {
Chris@192 190 d += ", ";
Chris@169 191 }
Chris@167 192 }
Chris@208 193 if (f.values.size() > 1) {
Chris@208 194 d += " ]";
Chris@208 195 }
Chris@153 196 }
Chris@153 197
Chris@153 198 d += " }";
Chris@145 199 }
Chris@153 200
Chris@167 201 m_data[did] = d;
Chris@145 202 }
Chris@145 203
Chris@145 204 void
Chris@169 205 JAMSFeatureWriter::setNofM(int n, int m)
Chris@169 206 {
Chris@169 207 if (m_singleFileName != "" || m_stdout) {
Chris@169 208 m_n = n;
Chris@169 209 m_m = m;
Chris@169 210 } else {
Chris@169 211 m_n = 1;
Chris@169 212 m_m = 1;
Chris@169 213 }
Chris@169 214 }
Chris@169 215
Chris@169 216 void
Chris@152 217 JAMSFeatureWriter::finish()
Chris@152 218 {
Chris@167 219 for (FileStreamMap::const_iterator stri = m_streams.begin();
Chris@167 220 stri != m_streams.end(); ++stri) {
Chris@152 221
Chris@167 222 QTextStream *sptr = stri->second;
Chris@167 223 QTextStream &stream = *sptr;
Chris@152 224
Chris@167 225 bool firstInStream = true;
Chris@167 226
Chris@167 227 for (TrackIds::const_iterator tri = m_streamTracks[sptr].begin();
Chris@167 228 tri != m_streamTracks[sptr].end(); ++tri) {
Chris@167 229
Chris@167 230 TrackId trackId = *tri;
Chris@167 231
Chris@169 232 if (firstInStream) {
Chris@169 233 if (m_streamTracks[sptr].size() > 1 || (m_m > 1 && m_n == 1)) {
Chris@169 234 stream << "[\n";
Chris@169 235 }
Chris@169 236 }
Chris@169 237
Chris@169 238 if (!firstInStream || (m_m > 1 && m_n > 1)) {
Chris@167 239 stream << ",\n";
Chris@167 240 }
Chris@167 241
Chris@167 242 stream << "{\n"
Chris@167 243 << QString("\"file_metadata\": {\n"
Chris@208 244 " \"jams_version\": \"0.2.0\",\n"
Chris@208 245 " \"identifiers\": { \"filename\": \"%1\" }")
Chris@167 246 .arg(QFileInfo(trackId).fileName());
Chris@167 247
Chris@167 248 if (m_trackMetadata.find(trackId) != m_trackMetadata.end()) {
Chris@208 249
Chris@208 250 QString durstr = m_trackMetadata[trackId].duration.toString().c_str();
Chris@208 251 durstr.replace(QRegExp("^ +"), "");
Chris@208 252 stream << QString(",\n \"duration\": %1").arg(durstr);
Chris@208 253
Chris@167 254 if (m_trackMetadata[trackId].maker != "") {
Chris@167 255 stream << QString(",\n \"artist\": \"%1\"")
Chris@167 256 .arg(m_trackMetadata[trackId].maker);
Chris@167 257 }
Chris@167 258 if (m_trackMetadata[trackId].title != "") {
Chris@167 259 stream << QString(",\n \"title\": \"%1\"")
Chris@167 260 .arg(m_trackMetadata[trackId].title);
Chris@167 261 }
Chris@167 262 }
Chris@167 263
Chris@167 264 stream << "\n},\n";
Chris@208 265 stream << "\"annotations\": [\n";
Chris@167 266
Chris@167 267 bool firstInTrack = true;
Chris@167 268
Chris@167 269 for (Tasks::const_iterator ti = m_streamTasks[sptr].begin();
Chris@167 270 ti != m_streamTasks[sptr].end(); ++ti) {
Chris@167 271
Chris@167 272 Task task = *ti;
Chris@167 273
Chris@167 274 for (DataIds::const_iterator di = m_streamData[sptr].begin();
Chris@167 275 di != m_streamData[sptr].end(); ++di) {
Chris@167 276
Chris@167 277 DataId did = *di;
Chris@167 278
Chris@167 279 QString trackId = did.first;
Chris@167 280 Transform transform = did.second;
Chris@167 281
Chris@167 282 if (m_tasks[transform.getIdentifier()] != task) continue;
Chris@167 283
Chris@167 284 QString data = m_data[did];
Chris@167 285
Chris@208 286 if (!firstInTrack) {
Chris@167 287 stream << ",\n";
Chris@167 288 }
Chris@167 289
Chris@208 290 stream << "{\n \"namespace\": \"" << getTaskKey(task) << "\",\n";
Chris@208 291
Chris@167 292 stream << QString
Chris@208 293 (" \"annotation_metadata\": {\n"
Chris@167 294 " \"annotation_tools\": \"Sonic Annotator v%2\",\n"
Chris@167 295 " \"data_source\": \"Automatic feature extraction\",\n"
Chris@167 296 " \"annotator\": {\n"
Chris@167 297 "%3"
Chris@167 298 " }\n"
Chris@167 299 " },\n"
Chris@167 300 " \"data\": [\n")
Chris@167 301 .arg(RUNNER_VERSION)
Chris@167 302 .arg(writeTransformToObjectContents(transform));
Chris@167 303
Chris@167 304 stream << data;
Chris@167 305
Chris@167 306 stream << "\n ]\n}";
Chris@208 307 firstInTrack = false;
Chris@167 308 }
Chris@208 309 }
Chris@167 310
Chris@208 311 stream << "\n]";
Chris@167 312
Chris@167 313 stream << "\n}";
Chris@167 314 firstInStream = false;
Chris@152 315 }
Chris@167 316
Chris@169 317 if (!firstInStream) {
Chris@169 318 if (m_streamTracks[sptr].size() > 1 || (m_m > 1 && m_n == m_m)) {
Chris@169 319 stream << "\n]";
Chris@169 320 }
Chris@169 321 stream << "\n";
Chris@167 322 }
Chris@152 323 }
Chris@152 324
Chris@167 325 m_streamTracks.clear();
Chris@167 326 m_streamTasks.clear();
Chris@167 327 m_streamData.clear();
Chris@152 328 m_data.clear();
Chris@152 329
Chris@152 330 FileFeatureWriter::finish();
Chris@152 331 }
Chris@152 332
Chris@152 333 void
Chris@145 334 JAMSFeatureWriter::loadRDFDescription(const Transform &transform)
Chris@145 335 {
Chris@145 336 QString pluginId = transform.getPluginIdentifier();
Chris@145 337 if (m_rdfDescriptions.find(pluginId) != m_rdfDescriptions.end()) return;
Chris@145 338
Chris@145 339 if (m_network && !m_networkRetrieved) {
Chris@145 340 PluginRDFIndexer::getInstance()->indexConfiguredURLs();
Chris@145 341 m_networkRetrieved = true;
Chris@145 342 }
Chris@145 343
Chris@145 344 m_rdfDescriptions[pluginId] = PluginRDFDescription(pluginId);
Chris@145 345
Chris@145 346 if (m_rdfDescriptions[pluginId].haveDescription()) {
Chris@145 347 cerr << "NOTE: Have RDF description for plugin ID \""
Chris@145 348 << pluginId << "\"" << endl;
Chris@145 349 } else {
Chris@145 350 cerr << "NOTE: No RDF description for plugin ID \""
Chris@145 351 << pluginId << "\"" << endl;
Chris@145 352 if (!m_network) {
Chris@200 353 cerr << " Consider using the --jams-network option to retrieve plugin descriptions" << endl;
Chris@145 354 cerr << " from the network where possible." << endl;
Chris@145 355 }
Chris@145 356 }
Chris@145 357 }
Chris@145 358
Chris@145 359 void
Chris@145 360 JAMSFeatureWriter::identifyTask(const Transform &transform)
Chris@145 361 {
Chris@145 362 QString transformId = transform.getIdentifier();
Chris@145 363 if (m_tasks.find(transformId) != m_tasks.end()) return;
Chris@145 364
Chris@145 365 loadRDFDescription(transform);
Chris@145 366
Chris@145 367 Task task = UnknownTask;
Chris@145 368
Chris@145 369 QString pluginId = transform.getPluginIdentifier();
Chris@145 370 QString outputId = transform.getOutput();
Chris@145 371
Chris@145 372 const PluginRDFDescription &desc = m_rdfDescriptions[pluginId];
Chris@145 373
Chris@145 374 if (desc.haveDescription()) {
Chris@145 375
Chris@145 376 PluginRDFDescription::OutputDisposition disp =
Chris@145 377 desc.getOutputDisposition(outputId);
Chris@145 378
Chris@145 379 QString af = "http://purl.org/ontology/af/";
Chris@145 380
Chris@145 381 if (disp == PluginRDFDescription::OutputSparse) {
Chris@145 382
Chris@145 383 QString eventUri = desc.getOutputEventTypeURI(outputId);
Chris@145 384
Chris@145 385 //!!! todo: allow user to prod writer for task type
Chris@145 386
Chris@145 387 if (eventUri == af + "Note") {
Chris@145 388 task = NoteTask;
Chris@145 389 } else if (eventUri == af + "Beat") {
Chris@145 390 task = BeatTask;
Chris@145 391 } else if (eventUri == af + "ChordSegment") {
Chris@145 392 task = ChordTask;
Chris@145 393 } else if (eventUri == af + "KeyChange") {
Chris@145 394 task = KeyTask;
Chris@145 395 } else if (eventUri == af + "KeySegment") {
Chris@145 396 task = KeyTask;
Chris@145 397 } else if (eventUri == af + "Onset") {
Chris@145 398 task = OnsetTask;
Chris@145 399 } else if (eventUri == af + "NonTonalOnset") {
Chris@145 400 task = OnsetTask;
Chris@145 401 } else if (eventUri == af + "Segment") {
Chris@145 402 task = SegmentTask;
Chris@145 403 } else if (eventUri == af + "SpeechSegment") {
Chris@145 404 task = SegmentTask;
Chris@145 405 } else if (eventUri == af + "StructuralSegment") {
Chris@145 406 task = SegmentTask;
Chris@145 407 } else {
Chris@145 408 cerr << "WARNING: Unsupported event type URI <"
Chris@145 409 << eventUri << ">, proceeding with UnknownTask type"
Chris@145 410 << endl;
Chris@145 411 }
Chris@145 412
Chris@145 413 } else {
Chris@145 414
Chris@200 415 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 416 }
Chris@145 417 }
Chris@145 418
Chris@145 419 m_tasks[transformId] = task;
Chris@145 420 }
Chris@145 421
Chris@145 422 QString
Chris@145 423 JAMSFeatureWriter::getTaskKey(Task task)
Chris@145 424 {
Chris@145 425 switch (task) {
Chris@145 426 case UnknownTask: return "unknown";
Chris@145 427 case BeatTask: return "beat";
Chris@145 428 case OnsetTask: return "onset";
Chris@145 429 case ChordTask: return "chord";
Chris@145 430 case SegmentTask: return "segment";
Chris@145 431 case KeyTask: return "key";
Chris@145 432 case NoteTask: return "note";
Chris@145 433 case MelodyTask: return "melody";
Chris@145 434 case PitchTask: return "pitch";
Chris@145 435 }
Chris@145 436 return "unknown";
Chris@145 437 }
Chris@165 438
Chris@165 439 QString
Chris@165 440 JAMSFeatureWriter::writeTransformToObjectContents(const Transform &t)
Chris@165 441 {
Chris@165 442 QString json;
Chris@165 443 QString stpl(" \"%1\": \"%2\",\n");
Chris@165 444 QString ntpl(" \"%1\": %2,\n");
Chris@165 445
Chris@165 446 json += stpl.arg("plugin_id").arg(t.getPluginIdentifier());
Chris@165 447 json += stpl.arg("output_id").arg(t.getOutput());
Chris@165 448
Chris@165 449 if (t.getSummaryType() != Transform::NoSummary) {
Chris@165 450 json += stpl.arg("summary_type")
Chris@165 451 .arg(Transform::summaryTypeToString(t.getSummaryType()));
Chris@165 452 }
Chris@165 453
Chris@165 454 if (t.getPluginVersion() != QString()) {
Chris@165 455 json += stpl.arg("plugin_version").arg(t.getPluginVersion());
Chris@165 456 }
Chris@165 457
Chris@165 458 if (t.getProgram() != QString()) {
Chris@165 459 json += stpl.arg("program").arg(t.getProgram());
Chris@165 460 }
Chris@165 461
Chris@165 462 if (t.getStepSize() != 0) {
Chris@165 463 json += ntpl.arg("step_size").arg(t.getStepSize());
Chris@165 464 }
Chris@165 465
Chris@165 466 if (t.getBlockSize() != 0) {
Chris@165 467 json += ntpl.arg("block_size").arg(t.getBlockSize());
Chris@165 468 }
Chris@165 469
Chris@165 470 if (t.getWindowType() != HanningWindow) {
Chris@165 471 json += stpl.arg("window_type")
Chris@165 472 .arg(Window<float>::getNameForType(t.getWindowType()).c_str());
Chris@165 473 }
Chris@165 474
Chris@165 475 if (t.getStartTime() != RealTime::zeroTime) {
Chris@204 476 json += ntpl.arg("start")
Chris@204 477 .arg(t.getStartTime().toDouble(), 0, 'g', 9);
Chris@165 478 }
Chris@165 479
Chris@165 480 if (t.getDuration() != RealTime::zeroTime) {
Chris@204 481 json += ntpl.arg("duration")
Chris@204 482 .arg(t.getDuration().toDouble(), 0, 'g', 9);
Chris@165 483 }
Chris@165 484
Chris@165 485 if (t.getSampleRate() != 0) {
Chris@165 486 json += ntpl.arg("sample_rate").arg(t.getSampleRate());
Chris@165 487 }
Chris@165 488
Chris@165 489 if (!t.getParameters().empty()) {
Chris@165 490 json += QString(" \"parameters\": {\n");
Chris@165 491 Transform::ParameterMap parameters = t.getParameters();
Chris@165 492 for (Transform::ParameterMap::const_iterator i = parameters.begin();
Chris@165 493 i != parameters.end(); ++i) {
Chris@167 494 if (i != parameters.begin()) {
Chris@167 495 json += ",\n";
Chris@167 496 }
Chris@165 497 QString name = i->first;
Chris@165 498 float value = i->second;
Chris@204 499 json += QString(" \"%1\": %2")
Chris@204 500 .arg(name)
Chris@204 501 .arg(value, 0, 'g', 8); // parameter values always to high precision
Chris@165 502 }
Chris@167 503 json += QString("\n },\n");
Chris@165 504 }
Chris@165 505
Chris@165 506 // no trailing comma on final property:
Chris@165 507 json += QString(" \"transform_id\": \"%1\"\n").arg(t.getIdentifier());
Chris@165 508
Chris@165 509 return json;
Chris@165 510 }
Chris@165 511