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