annotate runner/JAMSFeatureWriter.cpp @ 167:e5873fb4ffb3 jams

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