Mercurial > hg > sonic-annotator
comparison runner/JAMSFeatureWriter.cpp @ 153:ad96fd5f9cd7 jams
Output some data!
author | Chris Cannam |
---|---|
date | Tue, 14 Oct 2014 14:03:34 +0100 |
parents | db83ea0e102d |
children | 4b19d824a213 |
comparison
equal
deleted
inserted
replaced
152:db83ea0e102d | 153:ad96fd5f9cd7 |
---|---|
72 | 72 |
73 void | 73 void |
74 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata) | 74 JAMSFeatureWriter::setTrackMetadata(QString trackId, TrackMetadata metadata) |
75 { | 75 { |
76 QString json | 76 QString json |
77 (" \"file_metadata\":\n" | 77 ("\n\"file_metadata\":\n" |
78 " { \"artist\": \"%1\",\n" | 78 " { \"artist\": \"%1\",\n" |
79 " \"title\": \"%2\" },\n"); | 79 " \"title\": \"%2\" },\n"); |
80 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; | 81 cerr << "setTrackMetadata: metadata is: " << m_metadata[trackId] << endl; |
82 } | |
83 | |
84 static double | |
85 realTime2Sec(const Vamp::RealTime &r) | |
86 { | |
87 return r / Vamp::RealTime(1, 0); | |
82 } | 88 } |
83 | 89 |
84 void | 90 void |
85 JAMSFeatureWriter::write(QString trackId, | 91 JAMSFeatureWriter::write(QString trackId, |
86 const Transform &transform, | 92 const Transform &transform, |
104 // Need to write track-level preamble | 110 // Need to write track-level preamble |
105 stream << "{" << m_metadata[trackId] << endl; | 111 stream << "{" << m_metadata[trackId] << endl; |
106 m_startedTargets.insert(targetKey); | 112 m_startedTargets.insert(targetKey); |
107 } | 113 } |
108 | 114 |
115 bool justBegun = false; | |
116 | |
109 if (m_data.find(tt) == m_data.end()) { | 117 if (m_data.find(tt) == m_data.end()) { |
110 | 118 |
111 identifyTask(transform); | 119 identifyTask(transform); |
112 | 120 |
113 QString json("\"%1\": [ "); | 121 QString json("\"%1\": [ "); |
114 m_data[tt] = json.arg(getTaskKey(m_tasks[transformId])); | 122 m_data[tt] = json.arg(getTaskKey(m_tasks[transformId])); |
115 } | 123 justBegun = true; |
124 } | |
125 | |
126 QString d = m_data[tt]; | |
116 | 127 |
117 for (int i = 0; i < int(features.size()); ++i) { | 128 for (int i = 0; i < int(features.size()); ++i) { |
129 | |
130 if (i > 0 || !justBegun) { | |
131 d += ",\n"; | |
132 } else { | |
133 d += "\n"; | |
134 } | |
135 | |
136 d += " { "; | |
118 | 137 |
138 Plugin::Feature f(features[i]); | |
139 | |
140 switch (m_tasks[transformId]) { | |
141 | |
142 case ChordTask: | |
143 case SegmentTask: | |
144 case NoteTask: | |
145 case UnknownTask: | |
146 if (f.hasDuration) { | |
147 d += QString | |
148 ("\"start\": { \"value\": %1 }, " | |
149 "\"end\": { \"value\": %2 }") | |
150 .arg(realTime2Sec(f.timestamp)) | |
151 .arg(realTime2Sec | |
152 (f.timestamp + | |
153 (f.hasDuration ? f.duration : Vamp::RealTime::zeroTime))); | |
154 break; | |
155 } else { | |
156 // don't break; fall through to simpler no-duration case | |
157 } | |
158 | |
159 case BeatTask: | |
160 case KeyTask: | |
161 case OnsetTask: | |
162 d += QString("\"time\": { \"value\": %1 }") | |
163 .arg(realTime2Sec(f.timestamp)); | |
164 break; | |
165 } | |
166 | |
167 if (f.label != "") { | |
168 d += QString(", \"label\": { \"value\": \"%2\" }") | |
169 .arg(f.label.c_str()); | |
170 } else if (f.values.size() > 0) { | |
171 d += QString(", \"label\": { \"value\": \"%2\" }") | |
172 .arg(f.values[0]); | |
173 } | |
174 | |
175 d += " }"; | |
119 } | 176 } |
177 | |
178 m_data[tt] = d; | |
120 } | 179 } |
121 | 180 |
122 void | 181 void |
123 JAMSFeatureWriter::finish() | 182 JAMSFeatureWriter::finish() |
124 { | 183 { |
140 if (startedStreams.find(sptr) != startedStreams.end()) { | 199 if (startedStreams.find(sptr) != startedStreams.end()) { |
141 *sptr << "," << endl; | 200 *sptr << "," << endl; |
142 } | 201 } |
143 startedStreams.insert(sptr); | 202 startedStreams.insert(sptr); |
144 | 203 |
145 *sptr << data << "]"; | 204 *sptr << data << "\n ]"; |
146 } | 205 } |
147 | 206 |
148 for (FileStreamMap::const_iterator i = m_streams.begin(); | 207 for (FileStreamMap::const_iterator i = m_streams.begin(); |
149 i != m_streams.end(); ++i) { | 208 i != m_streams.end(); ++i) { |
150 *(i->second) << endl << "}" << endl; | 209 *(i->second) << endl << "}" << endl; |