annotate runner/JAMSFeatureWriter.cpp @ 204:1f8fef5c6ea2

Add digits option to JAMS output, for number of sig figs
author Chris Cannam
date Tue, 03 Nov 2015 11:36:24 +0000
parents f35bbb3e4d41
children c17b184c16db
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@168 145 if (f.hasDuration) {
Chris@204 146
Chris@204 147 QString endstr = (f.timestamp + f.duration).toString().c_str();
Chris@204 148 endstr.replace(QRegExp("^ +"), "");
Chris@204 149
Chris@168 150 d += QString
Chris@168 151 ("\"start\": { \"value\": %1 }, "
Chris@204 152 "\"end\": { \"value\": %2 }").arg(timestr).arg(endstr);
Chris@168 153 } else {
Chris@204 154 d += QString("\"time\": { \"value\": %1 }").arg(timestr);
Chris@153 155 }
Chris@153 156
Chris@153 157 if (f.label != "") {
Chris@153 158 d += QString(", \"label\": { \"value\": \"%2\" }")
Chris@153 159 .arg(f.label.c_str());
Chris@167 160 }
Chris@167 161
Chris@167 162 if (f.values.size() > 0) {
Chris@167 163 d += QString(", \"value\": [ ");
Chris@167 164 for (int j = 0; j < int(f.values.size()); ++j) {
Chris@169 165 if (isnan(f.values[j])) {
Chris@192 166 d += "\"NaN\"";
Chris@169 167 } else if (isinf(f.values[j])) {
Chris@192 168 d += "\"Inf\"";
Chris@169 169 } else {
Chris@204 170 d += QString("%1").arg(f.values[j], 0, 'g', m_digits);
Chris@192 171 }
Chris@192 172 if (j + 1 < int(f.values.size())) {
Chris@192 173 d += ", ";
Chris@169 174 }
Chris@167 175 }
Chris@192 176 d += " ]";
Chris@153 177 }
Chris@153 178
Chris@153 179 d += " }";
Chris@145 180 }
Chris@153 181
Chris@167 182 m_data[did] = d;
Chris@145 183 }
Chris@145 184
Chris@145 185 void
Chris@169 186 JAMSFeatureWriter::setNofM(int n, int m)
Chris@169 187 {
Chris@169 188 if (m_singleFileName != "" || m_stdout) {
Chris@169 189 m_n = n;
Chris@169 190 m_m = m;
Chris@169 191 } else {
Chris@169 192 m_n = 1;
Chris@169 193 m_m = 1;
Chris@169 194 }
Chris@169 195 }
Chris@169 196
Chris@169 197 void
Chris@152 198 JAMSFeatureWriter::finish()
Chris@152 199 {
Chris@167 200 for (FileStreamMap::const_iterator stri = m_streams.begin();
Chris@167 201 stri != m_streams.end(); ++stri) {
Chris@152 202
Chris@167 203 QTextStream *sptr = stri->second;
Chris@167 204 QTextStream &stream = *sptr;
Chris@152 205
Chris@167 206 bool firstInStream = true;
Chris@167 207
Chris@167 208 for (TrackIds::const_iterator tri = m_streamTracks[sptr].begin();
Chris@167 209 tri != m_streamTracks[sptr].end(); ++tri) {
Chris@167 210
Chris@167 211 TrackId trackId = *tri;
Chris@167 212
Chris@169 213 if (firstInStream) {
Chris@169 214 if (m_streamTracks[sptr].size() > 1 || (m_m > 1 && m_n == 1)) {
Chris@169 215 stream << "[\n";
Chris@169 216 }
Chris@169 217 }
Chris@169 218
Chris@169 219 if (!firstInStream || (m_m > 1 && m_n > 1)) {
Chris@167 220 stream << ",\n";
Chris@167 221 }
Chris@167 222
Chris@167 223 stream << "{\n"
Chris@167 224 << QString("\"file_metadata\": {\n"
Chris@167 225 " \"filename\": \"%1\"")
Chris@167 226 .arg(QFileInfo(trackId).fileName());
Chris@167 227
Chris@167 228 if (m_trackMetadata.find(trackId) != m_trackMetadata.end()) {
Chris@167 229 if (m_trackMetadata[trackId].maker != "") {
Chris@167 230 stream << QString(",\n \"artist\": \"%1\"")
Chris@167 231 .arg(m_trackMetadata[trackId].maker);
Chris@167 232 }
Chris@167 233 if (m_trackMetadata[trackId].title != "") {
Chris@167 234 stream << QString(",\n \"title\": \"%1\"")
Chris@167 235 .arg(m_trackMetadata[trackId].title);
Chris@167 236 }
Chris@167 237 }
Chris@167 238
Chris@167 239 stream << "\n},\n";
Chris@167 240
Chris@167 241 bool firstInTrack = true;
Chris@167 242
Chris@167 243 for (Tasks::const_iterator ti = m_streamTasks[sptr].begin();
Chris@167 244 ti != m_streamTasks[sptr].end(); ++ti) {
Chris@167 245
Chris@167 246 Task task = *ti;
Chris@167 247
Chris@167 248 if (!firstInTrack) {
Chris@167 249 stream << ",\n";
Chris@167 250 }
Chris@167 251
Chris@167 252 stream << "\"" << getTaskKey(task) << "\": [\n";
Chris@167 253
Chris@167 254 bool firstInTask = true;
Chris@167 255
Chris@167 256 for (DataIds::const_iterator di = m_streamData[sptr].begin();
Chris@167 257 di != m_streamData[sptr].end(); ++di) {
Chris@167 258
Chris@167 259 DataId did = *di;
Chris@167 260
Chris@167 261 QString trackId = did.first;
Chris@167 262 Transform transform = did.second;
Chris@167 263
Chris@167 264 if (m_tasks[transform.getIdentifier()] != task) continue;
Chris@167 265
Chris@167 266 QString data = m_data[did];
Chris@167 267
Chris@167 268 if (!firstInTask) {
Chris@167 269 stream << ",\n";
Chris@167 270 }
Chris@167 271
Chris@167 272 stream << QString
Chris@167 273 ("{ \n"
Chris@167 274 " \"annotation_metadata\": {\n"
Chris@167 275 " \"annotation_tools\": \"Sonic Annotator v%2\",\n"
Chris@167 276 " \"data_source\": \"Automatic feature extraction\",\n"
Chris@167 277 " \"annotator\": {\n"
Chris@167 278 "%3"
Chris@167 279 " }\n"
Chris@167 280 " },\n"
Chris@167 281 " \"data\": [\n")
Chris@167 282 .arg(RUNNER_VERSION)
Chris@167 283 .arg(writeTransformToObjectContents(transform));
Chris@167 284
Chris@167 285 stream << data;
Chris@167 286
Chris@167 287 stream << "\n ]\n}";
Chris@167 288 firstInTask = false;
Chris@167 289 }
Chris@167 290
Chris@167 291 stream << "\n]";
Chris@167 292 firstInTrack = false;
Chris@167 293 }
Chris@167 294
Chris@167 295 stream << "\n}";
Chris@167 296 firstInStream = false;
Chris@152 297 }
Chris@167 298
Chris@169 299 if (!firstInStream) {
Chris@169 300 if (m_streamTracks[sptr].size() > 1 || (m_m > 1 && m_n == m_m)) {
Chris@169 301 stream << "\n]";
Chris@169 302 }
Chris@169 303 stream << "\n";
Chris@167 304 }
Chris@152 305 }
Chris@152 306
Chris@167 307 m_streamTracks.clear();
Chris@167 308 m_streamTasks.clear();
Chris@167 309 m_streamData.clear();
Chris@152 310 m_data.clear();
Chris@152 311
Chris@152 312 FileFeatureWriter::finish();
Chris@152 313 }
Chris@152 314
Chris@152 315 void
Chris@145 316 JAMSFeatureWriter::loadRDFDescription(const Transform &transform)
Chris@145 317 {
Chris@145 318 QString pluginId = transform.getPluginIdentifier();
Chris@145 319 if (m_rdfDescriptions.find(pluginId) != m_rdfDescriptions.end()) return;
Chris@145 320
Chris@145 321 if (m_network && !m_networkRetrieved) {
Chris@145 322 PluginRDFIndexer::getInstance()->indexConfiguredURLs();
Chris@145 323 m_networkRetrieved = true;
Chris@145 324 }
Chris@145 325
Chris@145 326 m_rdfDescriptions[pluginId] = PluginRDFDescription(pluginId);
Chris@145 327
Chris@145 328 if (m_rdfDescriptions[pluginId].haveDescription()) {
Chris@145 329 cerr << "NOTE: Have RDF description for plugin ID \""
Chris@145 330 << pluginId << "\"" << endl;
Chris@145 331 } else {
Chris@145 332 cerr << "NOTE: No RDF description for plugin ID \""
Chris@145 333 << pluginId << "\"" << endl;
Chris@145 334 if (!m_network) {
Chris@200 335 cerr << " Consider using the --jams-network option to retrieve plugin descriptions" << endl;
Chris@145 336 cerr << " from the network where possible." << endl;
Chris@145 337 }
Chris@145 338 }
Chris@145 339 }
Chris@145 340
Chris@145 341 void
Chris@145 342 JAMSFeatureWriter::identifyTask(const Transform &transform)
Chris@145 343 {
Chris@145 344 QString transformId = transform.getIdentifier();
Chris@145 345 if (m_tasks.find(transformId) != m_tasks.end()) return;
Chris@145 346
Chris@145 347 loadRDFDescription(transform);
Chris@145 348
Chris@145 349 Task task = UnknownTask;
Chris@145 350
Chris@145 351 QString pluginId = transform.getPluginIdentifier();
Chris@145 352 QString outputId = transform.getOutput();
Chris@145 353
Chris@145 354 const PluginRDFDescription &desc = m_rdfDescriptions[pluginId];
Chris@145 355
Chris@145 356 if (desc.haveDescription()) {
Chris@145 357
Chris@145 358 PluginRDFDescription::OutputDisposition disp =
Chris@145 359 desc.getOutputDisposition(outputId);
Chris@145 360
Chris@145 361 QString af = "http://purl.org/ontology/af/";
Chris@145 362
Chris@145 363 if (disp == PluginRDFDescription::OutputSparse) {
Chris@145 364
Chris@145 365 QString eventUri = desc.getOutputEventTypeURI(outputId);
Chris@145 366
Chris@145 367 //!!! todo: allow user to prod writer for task type
Chris@145 368
Chris@145 369 if (eventUri == af + "Note") {
Chris@145 370 task = NoteTask;
Chris@145 371 } else if (eventUri == af + "Beat") {
Chris@145 372 task = BeatTask;
Chris@145 373 } else if (eventUri == af + "ChordSegment") {
Chris@145 374 task = ChordTask;
Chris@145 375 } else if (eventUri == af + "KeyChange") {
Chris@145 376 task = KeyTask;
Chris@145 377 } else if (eventUri == af + "KeySegment") {
Chris@145 378 task = KeyTask;
Chris@145 379 } else if (eventUri == af + "Onset") {
Chris@145 380 task = OnsetTask;
Chris@145 381 } else if (eventUri == af + "NonTonalOnset") {
Chris@145 382 task = OnsetTask;
Chris@145 383 } else if (eventUri == af + "Segment") {
Chris@145 384 task = SegmentTask;
Chris@145 385 } else if (eventUri == af + "SpeechSegment") {
Chris@145 386 task = SegmentTask;
Chris@145 387 } else if (eventUri == af + "StructuralSegment") {
Chris@145 388 task = SegmentTask;
Chris@145 389 } else {
Chris@145 390 cerr << "WARNING: Unsupported event type URI <"
Chris@145 391 << eventUri << ">, proceeding with UnknownTask type"
Chris@145 392 << endl;
Chris@145 393 }
Chris@145 394
Chris@145 395 } else {
Chris@145 396
Chris@200 397 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 398 }
Chris@145 399 }
Chris@145 400
Chris@145 401 m_tasks[transformId] = task;
Chris@145 402 }
Chris@145 403
Chris@145 404 QString
Chris@145 405 JAMSFeatureWriter::getTaskKey(Task task)
Chris@145 406 {
Chris@145 407 switch (task) {
Chris@145 408 case UnknownTask: return "unknown";
Chris@145 409 case BeatTask: return "beat";
Chris@145 410 case OnsetTask: return "onset";
Chris@145 411 case ChordTask: return "chord";
Chris@145 412 case SegmentTask: return "segment";
Chris@145 413 case KeyTask: return "key";
Chris@145 414 case NoteTask: return "note";
Chris@145 415 case MelodyTask: return "melody";
Chris@145 416 case PitchTask: return "pitch";
Chris@145 417 }
Chris@145 418 return "unknown";
Chris@145 419 }
Chris@165 420
Chris@165 421 QString
Chris@165 422 JAMSFeatureWriter::writeTransformToObjectContents(const Transform &t)
Chris@165 423 {
Chris@165 424 QString json;
Chris@165 425 QString stpl(" \"%1\": \"%2\",\n");
Chris@165 426 QString ntpl(" \"%1\": %2,\n");
Chris@165 427
Chris@165 428 json += stpl.arg("plugin_id").arg(t.getPluginIdentifier());
Chris@165 429 json += stpl.arg("output_id").arg(t.getOutput());
Chris@165 430
Chris@165 431 if (t.getSummaryType() != Transform::NoSummary) {
Chris@165 432 json += stpl.arg("summary_type")
Chris@165 433 .arg(Transform::summaryTypeToString(t.getSummaryType()));
Chris@165 434 }
Chris@165 435
Chris@165 436 if (t.getPluginVersion() != QString()) {
Chris@165 437 json += stpl.arg("plugin_version").arg(t.getPluginVersion());
Chris@165 438 }
Chris@165 439
Chris@165 440 if (t.getProgram() != QString()) {
Chris@165 441 json += stpl.arg("program").arg(t.getProgram());
Chris@165 442 }
Chris@165 443
Chris@165 444 if (t.getStepSize() != 0) {
Chris@165 445 json += ntpl.arg("step_size").arg(t.getStepSize());
Chris@165 446 }
Chris@165 447
Chris@165 448 if (t.getBlockSize() != 0) {
Chris@165 449 json += ntpl.arg("block_size").arg(t.getBlockSize());
Chris@165 450 }
Chris@165 451
Chris@165 452 if (t.getWindowType() != HanningWindow) {
Chris@165 453 json += stpl.arg("window_type")
Chris@165 454 .arg(Window<float>::getNameForType(t.getWindowType()).c_str());
Chris@165 455 }
Chris@165 456
Chris@165 457 if (t.getStartTime() != RealTime::zeroTime) {
Chris@204 458 json += ntpl.arg("start")
Chris@204 459 .arg(t.getStartTime().toDouble(), 0, 'g', 9);
Chris@165 460 }
Chris@165 461
Chris@165 462 if (t.getDuration() != RealTime::zeroTime) {
Chris@204 463 json += ntpl.arg("duration")
Chris@204 464 .arg(t.getDuration().toDouble(), 0, 'g', 9);
Chris@165 465 }
Chris@165 466
Chris@165 467 if (t.getSampleRate() != 0) {
Chris@165 468 json += ntpl.arg("sample_rate").arg(t.getSampleRate());
Chris@165 469 }
Chris@165 470
Chris@165 471 if (!t.getParameters().empty()) {
Chris@165 472 json += QString(" \"parameters\": {\n");
Chris@165 473 Transform::ParameterMap parameters = t.getParameters();
Chris@165 474 for (Transform::ParameterMap::const_iterator i = parameters.begin();
Chris@165 475 i != parameters.end(); ++i) {
Chris@167 476 if (i != parameters.begin()) {
Chris@167 477 json += ",\n";
Chris@167 478 }
Chris@165 479 QString name = i->first;
Chris@165 480 float value = i->second;
Chris@204 481 json += QString(" \"%1\": %2")
Chris@204 482 .arg(name)
Chris@204 483 .arg(value, 0, 'g', 8); // parameter values always to high precision
Chris@165 484 }
Chris@167 485 json += QString("\n },\n");
Chris@165 486 }
Chris@165 487
Chris@165 488 // no trailing comma on final property:
Chris@165 489 json += QString(" \"transform_id\": \"%1\"\n").arg(t.getIdentifier());
Chris@165 490
Chris@165 491 return json;
Chris@165 492 }
Chris@165 493