comparison runner/JAMSFeatureWriter.cpp @ 152:db83ea0e102d jams

Make the JSON well-formed (though still empty), except in the case where we are asked to write more than one file's features to the same output file or stdout
author Chris Cannam
date Tue, 14 Oct 2014 12:35:19 +0100
parents 3921e0c1f4dd
children ad96fd5f9cd7
comparison
equal deleted inserted replaced
151:a1300db6c63e 152:db83ea0e102d
23 #include "rdf/PluginRDFIndexer.h" 23 #include "rdf/PluginRDFIndexer.h"
24 24
25 JAMSFeatureWriter::JAMSFeatureWriter() : 25 JAMSFeatureWriter::JAMSFeatureWriter() :
26 FileFeatureWriter(SupportOneFilePerTrackTransform | 26 FileFeatureWriter(SupportOneFilePerTrackTransform |
27 SupportOneFilePerTrack | 27 SupportOneFilePerTrack |
28 SupportOneFileTotal |
28 SupportStdOut, 29 SupportStdOut,
29 "json"), 30 "json"),
30 m_network(false), 31 m_network(false),
31 m_networkRetrieved(false) 32 m_networkRetrieved(false)
32 { 33 {
71 72
72 void 73 void
73 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata) 74 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata)
74 { 75 {
75 QString json 76 QString json
76 ("'file_metadata':" 77 (" \"file_metadata\":\n"
77 " { 'artist': \"%1\"," 78 " { \"artist\": \"%1\",\n"
78 " 'title': \"%2\" }"); 79 " \"title\": \"%2\" },\n");
79 m_metadata[trackId] = json.arg(metadata.maker).arg(metadata.title); 80 m_metadata[trackId] = json.arg(metadata.maker).arg(metadata.title);
81 cerr << "setTrackMetadata: metadata is: " << m_metadata[trackId] << endl;
80 } 82 }
81 83
82 void 84 void
83 JAMSFeatureWriter::write(QString trackId, 85 JAMSFeatureWriter::write(QString trackId,
84 const Transform &transform, 86 const Transform &transform,
93 throw FailedToOpenOutputStream(trackId, transformId); 95 throw FailedToOpenOutputStream(trackId, transformId);
94 } 96 }
95 97
96 QTextStream &stream = *sptr; 98 QTextStream &stream = *sptr;
97 99
98 if (m_startedTransforms.find(transformId) == m_startedTransforms.end()) { 100 TrackTransformPair tt(trackId, transformId);
101 TrackTransformPair targetKey = getFilenameKey(trackId, transformId);
102
103 if (m_startedTargets.find(targetKey) == m_startedTargets.end()) {
104 // Need to write track-level preamble
105 stream << "{" << m_metadata[trackId] << endl;
106 m_startedTargets.insert(targetKey);
107 }
108
109 if (m_data.find(tt) == m_data.end()) {
99 110
100 identifyTask(transform); 111 identifyTask(transform);
101 112
102 if (m_manyFiles || 113 QString json("\"%1\": [ ");
103 (m_startedTracks.find(trackId) == m_startedTracks.end())) { 114 m_data[tt] = json.arg(getTaskKey(m_tasks[transformId]));
104 115 }
105 // track-level preamble
106 stream << "{" << m_metadata[trackId] << endl;
107 }
108
109 stream << "'" << getTaskKey(m_tasks[transformId]) << "':" << endl;
110 stream << " [ ";
111 }
112
113 m_startedTracks.insert(trackId);
114 m_startedTransforms.insert(transformId);
115 116
116 for (int i = 0; i < int(features.size()); ++i) { 117 for (int i = 0; i < int(features.size()); ++i) {
117 118
118 } 119 }
120 }
121
122 void
123 JAMSFeatureWriter::finish()
124 {
125 cerr << "Finish called on " << this << endl;
126
127 set<QTextStream *> startedStreams;
128
129 for (DataMap::const_iterator i = m_data.begin();
130 i != m_data.end(); ++i) {
131
132 TrackTransformPair tt = i->first;
133 QString data = i->second;
134
135 QTextStream *sptr = getOutputStream(tt.first, tt.second);
136 if (!sptr) {
137 throw FailedToOpenOutputStream(tt.first, tt.second);
138 }
139
140 if (startedStreams.find(sptr) != startedStreams.end()) {
141 *sptr << "," << endl;
142 }
143 startedStreams.insert(sptr);
144
145 *sptr << data << "]";
146 }
147
148 for (FileStreamMap::const_iterator i = m_streams.begin();
149 i != m_streams.end(); ++i) {
150 *(i->second) << endl << "}" << endl;
151 }
152
153 m_data.clear();
154 m_startedTargets.clear();
155
156 FileFeatureWriter::finish();
119 } 157 }
120 158
121 void 159 void
122 JAMSFeatureWriter::loadRDFDescription(const Transform &transform) 160 JAMSFeatureWriter::loadRDFDescription(const Transform &transform)
123 { 161 {
221 case MelodyTask: return "melody"; 259 case MelodyTask: return "melody";
222 case PitchTask: return "pitch"; 260 case PitchTask: return "pitch";
223 } 261 }
224 return "unknown"; 262 return "unknown";
225 } 263 }
226
227 void
228 JAMSFeatureWriter::finish()
229 {
230 for (FileStreamMap::const_iterator i = m_streams.begin();
231 i != m_streams.end(); ++i) {
232 *(i->second) << "}" << endl;
233 }
234
235 FileFeatureWriter::finish();
236 }
237