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@162
|
25 #include "version.h"
|
Chris@162
|
26
|
Chris@145
|
27 JAMSFeatureWriter::JAMSFeatureWriter() :
|
Chris@145
|
28 FileFeatureWriter(SupportOneFilePerTrackTransform |
|
Chris@145
|
29 SupportOneFilePerTrack |
|
Chris@152
|
30 SupportOneFileTotal |
|
Chris@145
|
31 SupportStdOut,
|
Chris@145
|
32 "json"),
|
Chris@145
|
33 m_network(false),
|
Chris@145
|
34 m_networkRetrieved(false)
|
Chris@145
|
35 {
|
Chris@145
|
36 }
|
Chris@145
|
37
|
Chris@145
|
38 JAMSFeatureWriter::~JAMSFeatureWriter()
|
Chris@145
|
39 {
|
Chris@145
|
40 }
|
Chris@145
|
41
|
Chris@145
|
42 string
|
Chris@145
|
43 JAMSFeatureWriter::getDescription() const
|
Chris@145
|
44 {
|
Chris@145
|
45 return "Write features to JSON files in JAMS (JSON Annotated Music Specification) format.";
|
Chris@145
|
46 }
|
Chris@145
|
47
|
Chris@145
|
48 JAMSFeatureWriter::ParameterList
|
Chris@145
|
49 JAMSFeatureWriter::getSupportedParameters() const
|
Chris@145
|
50 {
|
Chris@145
|
51 ParameterList pl = FileFeatureWriter::getSupportedParameters();
|
Chris@145
|
52 Parameter p;
|
Chris@145
|
53
|
Chris@145
|
54 p.name = "network";
|
Chris@145
|
55 p.description = "Attempt to retrieve RDF descriptions of plugins from network, if not available locally";
|
Chris@145
|
56 p.hasArg = false;
|
Chris@145
|
57 pl.push_back(p);
|
Chris@145
|
58
|
Chris@145
|
59 return pl;
|
Chris@145
|
60 }
|
Chris@145
|
61
|
Chris@145
|
62 void
|
Chris@145
|
63 JAMSFeatureWriter::setParameters(map<string, string> ¶ms)
|
Chris@145
|
64 {
|
Chris@145
|
65 FileFeatureWriter::setParameters(params);
|
Chris@145
|
66
|
Chris@145
|
67 for (map<string, string>::iterator i = params.begin();
|
Chris@145
|
68 i != params.end(); ++i) {
|
Chris@145
|
69 if (i->first == "network") {
|
Chris@145
|
70 m_network = true;
|
Chris@145
|
71 }
|
Chris@145
|
72 }
|
Chris@145
|
73 }
|
Chris@145
|
74
|
Chris@145
|
75 void
|
Chris@145
|
76 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata)
|
Chris@145
|
77 {
|
Chris@145
|
78 QString json
|
Chris@162
|
79 ("\n\"file_metadata\": {\n"
|
Chris@162
|
80 " \"artist\": \"%1\",\n"
|
Chris@162
|
81 " \"title\": \"%2\"\n},\n");
|
Chris@145
|
82 m_metadata[trackId] = json.arg(metadata.maker).arg(metadata.title);
|
Chris@152
|
83 cerr << "setTrackMetadata: metadata is: " << m_metadata[trackId] << endl;
|
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@145
|
101 QTextStream *sptr = getOutputStream(trackId, transformId);
|
Chris@145
|
102 if (!sptr) {
|
Chris@145
|
103 throw FailedToOpenOutputStream(trackId, transformId);
|
Chris@145
|
104 }
|
Chris@145
|
105
|
Chris@145
|
106 QTextStream &stream = *sptr;
|
Chris@145
|
107
|
Chris@152
|
108 TrackTransformPair tt(trackId, transformId);
|
Chris@152
|
109 TrackTransformPair targetKey = getFilenameKey(trackId, transformId);
|
Chris@152
|
110
|
Chris@152
|
111 if (m_startedTargets.find(targetKey) == m_startedTargets.end()) {
|
Chris@152
|
112 // Need to write track-level preamble
|
Chris@152
|
113 stream << "{" << m_metadata[trackId] << endl;
|
Chris@152
|
114 m_startedTargets.insert(targetKey);
|
Chris@152
|
115 }
|
Chris@152
|
116
|
Chris@153
|
117 bool justBegun = false;
|
Chris@153
|
118
|
Chris@152
|
119 if (m_data.find(tt) == m_data.end()) {
|
Chris@145
|
120
|
Chris@145
|
121 identifyTask(transform);
|
Chris@145
|
122
|
Chris@162
|
123 QString json
|
Chris@162
|
124 ("\"%1\": [ { \n"
|
Chris@162
|
125 " \"annotation_metadata\": {\n"
|
Chris@162
|
126 " \"annotation_tools\": \"Sonic Annotator v%2\",\n"
|
Chris@162
|
127 " \"data_source\": \"Automatic feature extraction\",\n"
|
Chris@162
|
128 " \"annotator\": { \"transform_id\": \"%3\" }\n"
|
Chris@162
|
129 " },\n"
|
Chris@162
|
130 " \"data\": [");
|
Chris@162
|
131 m_data[tt] = json
|
Chris@162
|
132 .arg(getTaskKey(m_tasks[transformId]))
|
Chris@162
|
133 .arg(RUNNER_VERSION)
|
Chris@162
|
134 .arg(transformId);
|
Chris@153
|
135 justBegun = true;
|
Chris@145
|
136 }
|
Chris@145
|
137
|
Chris@153
|
138 QString d = m_data[tt];
|
Chris@153
|
139
|
Chris@145
|
140 for (int i = 0; i < int(features.size()); ++i) {
|
Chris@153
|
141
|
Chris@153
|
142 if (i > 0 || !justBegun) {
|
Chris@153
|
143 d += ",\n";
|
Chris@153
|
144 } else {
|
Chris@153
|
145 d += "\n";
|
Chris@153
|
146 }
|
Chris@153
|
147
|
Chris@153
|
148 d += " { ";
|
Chris@145
|
149
|
Chris@153
|
150 Plugin::Feature f(features[i]);
|
Chris@153
|
151
|
Chris@153
|
152 switch (m_tasks[transformId]) {
|
Chris@153
|
153
|
Chris@153
|
154 case ChordTask:
|
Chris@153
|
155 case SegmentTask:
|
Chris@153
|
156 case NoteTask:
|
Chris@153
|
157 case UnknownTask:
|
Chris@153
|
158 if (f.hasDuration) {
|
Chris@153
|
159 d += QString
|
Chris@153
|
160 ("\"start\": { \"value\": %1 }, "
|
Chris@153
|
161 "\"end\": { \"value\": %2 }")
|
Chris@153
|
162 .arg(realTime2Sec(f.timestamp))
|
Chris@153
|
163 .arg(realTime2Sec
|
Chris@153
|
164 (f.timestamp +
|
Chris@153
|
165 (f.hasDuration ? f.duration : Vamp::RealTime::zeroTime)));
|
Chris@153
|
166 break;
|
Chris@153
|
167 } else {
|
Chris@153
|
168 // don't break; fall through to simpler no-duration case
|
Chris@153
|
169 }
|
Chris@153
|
170
|
Chris@153
|
171 case BeatTask:
|
Chris@153
|
172 case KeyTask:
|
Chris@153
|
173 case OnsetTask:
|
Chris@153
|
174 d += QString("\"time\": { \"value\": %1 }")
|
Chris@153
|
175 .arg(realTime2Sec(f.timestamp));
|
Chris@153
|
176 break;
|
Chris@161
|
177
|
Chris@161
|
178 case MelodyTask:
|
Chris@161
|
179 case PitchTask:
|
Chris@161
|
180 //!!!
|
Chris@161
|
181 break;
|
Chris@153
|
182 }
|
Chris@153
|
183
|
Chris@153
|
184 if (f.label != "") {
|
Chris@153
|
185 d += QString(", \"label\": { \"value\": \"%2\" }")
|
Chris@153
|
186 .arg(f.label.c_str());
|
Chris@153
|
187 } else if (f.values.size() > 0) {
|
Chris@153
|
188 d += QString(", \"label\": { \"value\": \"%2\" }")
|
Chris@153
|
189 .arg(f.values[0]);
|
Chris@153
|
190 }
|
Chris@153
|
191
|
Chris@153
|
192 d += " }";
|
Chris@145
|
193 }
|
Chris@153
|
194
|
Chris@153
|
195 m_data[tt] = d;
|
Chris@145
|
196 }
|
Chris@145
|
197
|
Chris@145
|
198 void
|
Chris@152
|
199 JAMSFeatureWriter::finish()
|
Chris@152
|
200 {
|
Chris@152
|
201 cerr << "Finish called on " << this << endl;
|
Chris@152
|
202
|
Chris@152
|
203 set<QTextStream *> startedStreams;
|
Chris@152
|
204
|
Chris@152
|
205 for (DataMap::const_iterator i = m_data.begin();
|
Chris@152
|
206 i != m_data.end(); ++i) {
|
Chris@152
|
207
|
Chris@152
|
208 TrackTransformPair tt = i->first;
|
Chris@152
|
209 QString data = i->second;
|
Chris@152
|
210
|
Chris@152
|
211 QTextStream *sptr = getOutputStream(tt.first, tt.second);
|
Chris@152
|
212 if (!sptr) {
|
Chris@152
|
213 throw FailedToOpenOutputStream(tt.first, tt.second);
|
Chris@152
|
214 }
|
Chris@152
|
215
|
Chris@152
|
216 if (startedStreams.find(sptr) != startedStreams.end()) {
|
Chris@152
|
217 *sptr << "," << endl;
|
Chris@152
|
218 }
|
Chris@152
|
219 startedStreams.insert(sptr);
|
Chris@152
|
220
|
Chris@162
|
221 *sptr << data << "\n ]\n} ]";
|
Chris@152
|
222 }
|
Chris@152
|
223
|
Chris@152
|
224 for (FileStreamMap::const_iterator i = m_streams.begin();
|
Chris@152
|
225 i != m_streams.end(); ++i) {
|
Chris@152
|
226 *(i->second) << endl << "}" << endl;
|
Chris@152
|
227 }
|
Chris@152
|
228
|
Chris@152
|
229 m_data.clear();
|
Chris@152
|
230 m_startedTargets.clear();
|
Chris@152
|
231
|
Chris@152
|
232 FileFeatureWriter::finish();
|
Chris@152
|
233 }
|
Chris@152
|
234
|
Chris@152
|
235 void
|
Chris@145
|
236 JAMSFeatureWriter::loadRDFDescription(const Transform &transform)
|
Chris@145
|
237 {
|
Chris@145
|
238 QString pluginId = transform.getPluginIdentifier();
|
Chris@145
|
239 if (m_rdfDescriptions.find(pluginId) != m_rdfDescriptions.end()) return;
|
Chris@145
|
240
|
Chris@145
|
241 if (m_network && !m_networkRetrieved) {
|
Chris@145
|
242 PluginRDFIndexer::getInstance()->indexConfiguredURLs();
|
Chris@145
|
243 m_networkRetrieved = true;
|
Chris@145
|
244 }
|
Chris@145
|
245
|
Chris@145
|
246 m_rdfDescriptions[pluginId] = PluginRDFDescription(pluginId);
|
Chris@145
|
247
|
Chris@145
|
248 if (m_rdfDescriptions[pluginId].haveDescription()) {
|
Chris@145
|
249 cerr << "NOTE: Have RDF description for plugin ID \""
|
Chris@145
|
250 << pluginId << "\"" << endl;
|
Chris@145
|
251 } else {
|
Chris@145
|
252 cerr << "NOTE: No RDF description for plugin ID \""
|
Chris@145
|
253 << pluginId << "\"" << endl;
|
Chris@145
|
254 if (!m_network) {
|
Chris@145
|
255 cerr << " Consider using the --json-network option to retrieve plugin descriptions" << endl;
|
Chris@145
|
256 cerr << " from the network where possible." << endl;
|
Chris@145
|
257 }
|
Chris@145
|
258 }
|
Chris@145
|
259 }
|
Chris@145
|
260
|
Chris@145
|
261 void
|
Chris@145
|
262 JAMSFeatureWriter::identifyTask(const Transform &transform)
|
Chris@145
|
263 {
|
Chris@145
|
264 QString transformId = transform.getIdentifier();
|
Chris@145
|
265 if (m_tasks.find(transformId) != m_tasks.end()) return;
|
Chris@145
|
266
|
Chris@145
|
267 loadRDFDescription(transform);
|
Chris@145
|
268
|
Chris@145
|
269 Task task = UnknownTask;
|
Chris@145
|
270
|
Chris@145
|
271 QString pluginId = transform.getPluginIdentifier();
|
Chris@145
|
272 QString outputId = transform.getOutput();
|
Chris@145
|
273
|
Chris@145
|
274 const PluginRDFDescription &desc = m_rdfDescriptions[pluginId];
|
Chris@145
|
275
|
Chris@145
|
276 if (desc.haveDescription()) {
|
Chris@145
|
277
|
Chris@145
|
278 PluginRDFDescription::OutputDisposition disp =
|
Chris@145
|
279 desc.getOutputDisposition(outputId);
|
Chris@145
|
280
|
Chris@145
|
281 QString af = "http://purl.org/ontology/af/";
|
Chris@145
|
282
|
Chris@145
|
283 if (disp == PluginRDFDescription::OutputSparse) {
|
Chris@145
|
284
|
Chris@145
|
285 QString eventUri = desc.getOutputEventTypeURI(outputId);
|
Chris@145
|
286
|
Chris@145
|
287 //!!! todo: allow user to prod writer for task type
|
Chris@145
|
288
|
Chris@145
|
289 if (eventUri == af + "Note") {
|
Chris@145
|
290 task = NoteTask;
|
Chris@145
|
291 } else if (eventUri == af + "Beat") {
|
Chris@145
|
292 task = BeatTask;
|
Chris@145
|
293 } else if (eventUri == af + "ChordSegment") {
|
Chris@145
|
294 task = ChordTask;
|
Chris@145
|
295 } else if (eventUri == af + "KeyChange") {
|
Chris@145
|
296 task = KeyTask;
|
Chris@145
|
297 } else if (eventUri == af + "KeySegment") {
|
Chris@145
|
298 task = KeyTask;
|
Chris@145
|
299 } else if (eventUri == af + "Onset") {
|
Chris@145
|
300 task = OnsetTask;
|
Chris@145
|
301 } else if (eventUri == af + "NonTonalOnset") {
|
Chris@145
|
302 task = OnsetTask;
|
Chris@145
|
303 } else if (eventUri == af + "Segment") {
|
Chris@145
|
304 task = SegmentTask;
|
Chris@145
|
305 } else if (eventUri == af + "SpeechSegment") {
|
Chris@145
|
306 task = SegmentTask;
|
Chris@145
|
307 } else if (eventUri == af + "StructuralSegment") {
|
Chris@145
|
308 task = SegmentTask;
|
Chris@145
|
309 } else {
|
Chris@145
|
310 cerr << "WARNING: Unsupported event type URI <"
|
Chris@145
|
311 << eventUri << ">, proceeding with UnknownTask type"
|
Chris@145
|
312 << endl;
|
Chris@145
|
313 }
|
Chris@145
|
314
|
Chris@145
|
315 } else {
|
Chris@145
|
316
|
Chris@145
|
317 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
|
318 }
|
Chris@145
|
319 }
|
Chris@145
|
320
|
Chris@145
|
321 m_tasks[transformId] = task;
|
Chris@145
|
322 }
|
Chris@145
|
323
|
Chris@145
|
324 QString
|
Chris@145
|
325 JAMSFeatureWriter::getTaskKey(Task task)
|
Chris@145
|
326 {
|
Chris@145
|
327 switch (task) {
|
Chris@145
|
328 case UnknownTask: return "unknown";
|
Chris@145
|
329 case BeatTask: return "beat";
|
Chris@145
|
330 case OnsetTask: return "onset";
|
Chris@145
|
331 case ChordTask: return "chord";
|
Chris@145
|
332 case SegmentTask: return "segment";
|
Chris@145
|
333 case KeyTask: return "key";
|
Chris@145
|
334 case NoteTask: return "note";
|
Chris@145
|
335 case MelodyTask: return "melody";
|
Chris@145
|
336 case PitchTask: return "pitch";
|
Chris@145
|
337 }
|
Chris@145
|
338 return "unknown";
|
Chris@145
|
339 }
|