annotate runner/JAMSFeatureWriter.cpp @ 190:2d2253469ca4

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