Mercurial > hg > sonic-annotator
comparison runner/JAMSFeatureWriter.cpp @ 167:e5873fb4ffb3 jams
Restructure
author | Chris Cannam |
---|---|
date | Wed, 15 Oct 2014 14:56:39 +0100 |
parents | e98b1abeb792 |
children | 3e30dbb68ca2 |
comparison
equal
deleted
inserted
replaced
166:e98b1abeb792 | 167:e5873fb4ffb3 |
---|---|
75 } | 75 } |
76 | 76 |
77 void | 77 void |
78 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata) | 78 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata) |
79 { | 79 { |
80 m_metadata[trackId] = metadata; | 80 m_trackMetadata[trackId] = metadata; |
81 } | 81 } |
82 | 82 |
83 static double | 83 static double |
84 realTime2Sec(const Vamp::RealTime &r) | 84 realTime2Sec(const Vamp::RealTime &r) |
85 { | 85 { |
98 QTextStream *sptr = getOutputStream(trackId, transformId); | 98 QTextStream *sptr = getOutputStream(trackId, transformId); |
99 if (!sptr) { | 99 if (!sptr) { |
100 throw FailedToOpenOutputStream(trackId, transformId); | 100 throw FailedToOpenOutputStream(trackId, transformId); |
101 } | 101 } |
102 | 102 |
103 QTextStream &stream = *sptr; | 103 DataId did(trackId, transform); |
104 | 104 |
105 TrackTransformPair tt(trackId, transformId); | 105 if (m_data.find(did) == m_data.end()) { |
106 TrackTransformPair targetKey = getFilenameKey(trackId, transformId); | |
107 | |
108 if (m_startedTargets.find(targetKey) == m_startedTargets.end()) { | |
109 // Need to write track-level preamble | |
110 stream << "{\n"; | |
111 stream << QString("\"file_metadata\": {\n" | |
112 " \"filename\": \"%1\"") | |
113 .arg(QFileInfo(trackId).fileName()); | |
114 | |
115 if (m_metadata.find(trackId) != m_metadata.end()) { | |
116 if (m_metadata[trackId].maker != "") { | |
117 stream << QString(",\n \"artist\": \"%1\"") | |
118 .arg(m_metadata[trackId].maker); | |
119 } | |
120 if (m_metadata[trackId].title != "") { | |
121 stream << QString(",\n \"title\": \"%1\"") | |
122 .arg(m_metadata[trackId].title); | |
123 } | |
124 } | |
125 | |
126 stream << "\n},\n"; | |
127 | |
128 m_startedTargets.insert(targetKey); | |
129 } | |
130 | |
131 bool justBegun = false; | |
132 | |
133 if (m_data.find(tt) == m_data.end()) { | |
134 | |
135 identifyTask(transform); | 106 identifyTask(transform); |
136 | 107 m_streamTracks[sptr].insert(trackId); |
137 QString json | 108 m_streamTasks[sptr].insert(m_tasks[transformId]); |
138 ("\"%1\": [ { \n" | 109 m_streamData[sptr].insert(did); |
139 " \"annotation_metadata\": {\n" | 110 } |
140 " \"annotation_tools\": \"Sonic Annotator v%2\",\n" | 111 |
141 " \"data_source\": \"Automatic feature extraction\",\n" | 112 QString d = m_data[did]; |
142 " \"annotator\": {\n" | |
143 "%3" | |
144 " },\n" | |
145 " },\n" | |
146 " \"data\": ["); | |
147 m_data[tt] = json | |
148 .arg(getTaskKey(m_tasks[transformId])) | |
149 .arg(RUNNER_VERSION) | |
150 .arg(writeTransformToObjectContents(transform)); | |
151 justBegun = true; | |
152 } | |
153 | |
154 QString d = m_data[tt]; | |
155 | 113 |
156 for (int i = 0; i < int(features.size()); ++i) { | 114 for (int i = 0; i < int(features.size()); ++i) { |
157 | 115 |
158 if (i > 0 || !justBegun) { | 116 if (d != "") { |
159 d += ",\n"; | 117 d += ",\n"; |
160 } else { | |
161 d += "\n"; | |
162 } | 118 } |
163 | 119 |
164 d += " { "; | 120 d += " { "; |
165 | 121 |
166 Plugin::Feature f(features[i]); | 122 Plugin::Feature f(features[i]); |
198 } | 154 } |
199 | 155 |
200 if (f.label != "") { | 156 if (f.label != "") { |
201 d += QString(", \"label\": { \"value\": \"%2\" }") | 157 d += QString(", \"label\": { \"value\": \"%2\" }") |
202 .arg(f.label.c_str()); | 158 .arg(f.label.c_str()); |
203 } else if (f.values.size() > 0) { | 159 } |
204 d += QString(", \"label\": { \"value\": \"%2\" }") | 160 |
205 .arg(f.values[0]); | 161 if (f.values.size() > 0) { |
162 d += QString(", \"value\": [ "); | |
163 for (int j = 0; j < int(f.values.size()); ++j) { | |
164 d += QString("%1 ").arg(f.values[i]); | |
165 } | |
166 d += "]"; | |
206 } | 167 } |
207 | 168 |
208 d += " }"; | 169 d += " }"; |
209 } | 170 } |
210 | 171 |
211 m_data[tt] = d; | 172 m_data[did] = d; |
212 } | 173 } |
213 | 174 |
214 void | 175 void |
215 JAMSFeatureWriter::finish() | 176 JAMSFeatureWriter::finish() |
216 { | 177 { |
217 cerr << "Finish called on " << this << endl; | 178 for (FileStreamMap::const_iterator stri = m_streams.begin(); |
218 | 179 stri != m_streams.end(); ++stri) { |
219 set<QTextStream *> startedStreams; | 180 |
220 | 181 QTextStream *sptr = stri->second; |
221 for (DataMap::const_iterator i = m_data.begin(); | 182 QTextStream &stream = *sptr; |
222 i != m_data.end(); ++i) { | 183 |
223 | 184 if (m_streamTracks[sptr].size() > 1) { |
224 TrackTransformPair tt = i->first; | 185 stream << "[\n"; |
225 QString data = i->second; | 186 } |
226 | 187 |
227 QTextStream *sptr = getOutputStream(tt.first, tt.second); | 188 bool firstInStream = true; |
228 if (!sptr) { | 189 |
229 throw FailedToOpenOutputStream(tt.first, tt.second); | 190 for (TrackIds::const_iterator tri = m_streamTracks[sptr].begin(); |
230 } | 191 tri != m_streamTracks[sptr].end(); ++tri) { |
231 | 192 |
232 if (startedStreams.find(sptr) != startedStreams.end()) { | 193 TrackId trackId = *tri; |
233 *sptr << "," << endl; | 194 |
234 } | 195 if (!firstInStream) { |
235 startedStreams.insert(sptr); | 196 stream << ",\n"; |
197 } | |
198 | |
199 stream << "{\n" | |
200 << QString("\"file_metadata\": {\n" | |
201 " \"filename\": \"%1\"") | |
202 .arg(QFileInfo(trackId).fileName()); | |
203 | |
204 if (m_trackMetadata.find(trackId) != m_trackMetadata.end()) { | |
205 if (m_trackMetadata[trackId].maker != "") { | |
206 stream << QString(",\n \"artist\": \"%1\"") | |
207 .arg(m_trackMetadata[trackId].maker); | |
208 } | |
209 if (m_trackMetadata[trackId].title != "") { | |
210 stream << QString(",\n \"title\": \"%1\"") | |
211 .arg(m_trackMetadata[trackId].title); | |
212 } | |
213 } | |
214 | |
215 stream << "\n},\n"; | |
216 | |
217 bool firstInTrack = true; | |
218 | |
219 for (Tasks::const_iterator ti = m_streamTasks[sptr].begin(); | |
220 ti != m_streamTasks[sptr].end(); ++ti) { | |
221 | |
222 Task task = *ti; | |
223 | |
224 if (!firstInTrack) { | |
225 stream << ",\n"; | |
226 } | |
227 | |
228 stream << "\"" << getTaskKey(task) << "\": [\n"; | |
229 | |
230 bool firstInTask = true; | |
231 | |
232 for (DataIds::const_iterator di = m_streamData[sptr].begin(); | |
233 di != m_streamData[sptr].end(); ++di) { | |
234 | |
235 DataId did = *di; | |
236 | |
237 QString trackId = did.first; | |
238 Transform transform = did.second; | |
239 | |
240 if (m_tasks[transform.getIdentifier()] != task) continue; | |
241 | |
242 QString data = m_data[did]; | |
243 | |
244 if (!firstInTask) { | |
245 stream << ",\n"; | |
246 } | |
247 | |
248 stream << QString | |
249 ("{ \n" | |
250 " \"annotation_metadata\": {\n" | |
251 " \"annotation_tools\": \"Sonic Annotator v%2\",\n" | |
252 " \"data_source\": \"Automatic feature extraction\",\n" | |
253 " \"annotator\": {\n" | |
254 "%3" | |
255 " }\n" | |
256 " },\n" | |
257 " \"data\": [\n") | |
258 .arg(RUNNER_VERSION) | |
259 .arg(writeTransformToObjectContents(transform)); | |
260 | |
261 stream << data; | |
262 | |
263 stream << "\n ]\n}"; | |
264 firstInTask = false; | |
265 } | |
266 | |
267 stream << "\n]"; | |
268 firstInTrack = false; | |
269 } | |
270 | |
271 stream << "\n}"; | |
272 firstInStream = false; | |
273 } | |
274 | |
275 if (m_streamTracks[sptr].size() > 1) { | |
276 stream << "\n]"; | |
277 } | |
278 stream << "\n"; | |
279 } | |
236 | 280 |
237 *sptr << data << "\n ]\n} ]"; | 281 m_streamTracks.clear(); |
238 } | 282 m_streamTasks.clear(); |
239 | 283 m_streamData.clear(); |
240 for (FileStreamMap::const_iterator i = m_streams.begin(); | |
241 i != m_streams.end(); ++i) { | |
242 *(i->second) << endl << "}" << endl; | |
243 } | |
244 | |
245 m_data.clear(); | 284 m_data.clear(); |
246 m_startedTargets.clear(); | |
247 | 285 |
248 FileFeatureWriter::finish(); | 286 FileFeatureWriter::finish(); |
249 } | 287 } |
250 | 288 |
251 void | 289 void |
405 if (!t.getParameters().empty()) { | 443 if (!t.getParameters().empty()) { |
406 json += QString(" \"parameters\": {\n"); | 444 json += QString(" \"parameters\": {\n"); |
407 Transform::ParameterMap parameters = t.getParameters(); | 445 Transform::ParameterMap parameters = t.getParameters(); |
408 for (Transform::ParameterMap::const_iterator i = parameters.begin(); | 446 for (Transform::ParameterMap::const_iterator i = parameters.begin(); |
409 i != parameters.end(); ++i) { | 447 i != parameters.end(); ++i) { |
448 if (i != parameters.begin()) { | |
449 json += ",\n"; | |
450 } | |
410 QString name = i->first; | 451 QString name = i->first; |
411 float value = i->second; | 452 float value = i->second; |
412 json += QString(" \"%1\": %2\n").arg(name).arg(value); | 453 json += QString(" \"%1\": %2").arg(name).arg(value); |
413 } | 454 } |
414 json += QString(" },\n"); | 455 json += QString("\n },\n"); |
415 } | 456 } |
416 | 457 |
417 // no trailing comma on final property: | 458 // no trailing comma on final property: |
418 json += QString(" \"transform_id\": \"%1\"\n").arg(t.getIdentifier()); | 459 json += QString(" \"transform_id\": \"%1\"\n").arg(t.getIdentifier()); |
419 | 460 |