Mercurial > hg > sonic-annotator
comparison runner/JAMSFeatureWriter.cpp @ 204:1f8fef5c6ea2
Add digits option to JAMS output, for number of sig figs
author | Chris Cannam |
---|---|
date | Tue, 03 Nov 2015 11:36:24 +0000 |
parents | f35bbb3e4d41 |
children | c17b184c16db |
comparison
equal
deleted
inserted
replaced
203:08d492f861e9 | 204:1f8fef5c6ea2 |
---|---|
34 SupportStdOut, | 34 SupportStdOut, |
35 "json"), // file extension is json even with jams writer | 35 "json"), // file extension is json even with jams writer |
36 m_network(false), | 36 m_network(false), |
37 m_networkRetrieved(false), | 37 m_networkRetrieved(false), |
38 m_n(1), | 38 m_n(1), |
39 m_m(1) | 39 m_m(1), |
40 m_digits(6) | |
40 { | 41 { |
41 } | 42 } |
42 | 43 |
43 JAMSFeatureWriter::~JAMSFeatureWriter() | 44 JAMSFeatureWriter::~JAMSFeatureWriter() |
44 { | 45 { |
53 JAMSFeatureWriter::ParameterList | 54 JAMSFeatureWriter::ParameterList |
54 JAMSFeatureWriter::getSupportedParameters() const | 55 JAMSFeatureWriter::getSupportedParameters() const |
55 { | 56 { |
56 ParameterList pl = FileFeatureWriter::getSupportedParameters(); | 57 ParameterList pl = FileFeatureWriter::getSupportedParameters(); |
57 Parameter p; | 58 Parameter p; |
59 | |
60 p.name = "digits"; | |
61 p.description = "Specify the number of significant digits to use when printing transform outputs. Outputs are represented internally using single-precision floating-point, so digits beyond the 8th or 9th place are usually meaningless. The default is 6."; | |
62 p.hasArg = true; | |
63 pl.push_back(p); | |
58 | 64 |
59 p.name = "network"; | 65 p.name = "network"; |
60 p.description = "Attempt to retrieve RDF descriptions of plugins from network, if not available locally."; | 66 p.description = "Attempt to retrieve RDF descriptions of plugins from network, if not available locally."; |
61 p.hasArg = false; | 67 p.hasArg = false; |
62 pl.push_back(p); | 68 pl.push_back(p); |
71 | 77 |
72 for (map<string, string>::iterator i = params.begin(); | 78 for (map<string, string>::iterator i = params.begin(); |
73 i != params.end(); ++i) { | 79 i != params.end(); ++i) { |
74 if (i->first == "network") { | 80 if (i->first == "network") { |
75 m_network = true; | 81 m_network = true; |
82 } else if (i->first == "digits") { | |
83 int digits = atoi(i->second.c_str()); | |
84 if (digits <= 0 || digits > 100) { | |
85 cerr << "JAMSFeatureWriter: ERROR: Invalid or out-of-range value for number of significant digits: " << i->second << endl; | |
86 cerr << "JAMSFeatureWriter: NOTE: Continuing with default settings" << endl; | |
87 } else { | |
88 m_digits = digits; | |
89 } | |
76 } | 90 } |
77 } | 91 } |
78 } | 92 } |
79 | 93 |
80 void | 94 void |
123 | 137 |
124 d += " { "; | 138 d += " { "; |
125 | 139 |
126 Plugin::Feature f(features[i]); | 140 Plugin::Feature f(features[i]); |
127 | 141 |
142 QString timestr = f.timestamp.toString().c_str(); | |
143 timestr.replace(QRegExp("^ +"), ""); | |
144 | |
128 if (f.hasDuration) { | 145 if (f.hasDuration) { |
146 | |
147 QString endstr = (f.timestamp + f.duration).toString().c_str(); | |
148 endstr.replace(QRegExp("^ +"), ""); | |
149 | |
129 d += QString | 150 d += QString |
130 ("\"start\": { \"value\": %1 }, " | 151 ("\"start\": { \"value\": %1 }, " |
131 "\"end\": { \"value\": %2 }") | 152 "\"end\": { \"value\": %2 }").arg(timestr).arg(endstr); |
132 .arg(realTime2Sec(f.timestamp)) | |
133 .arg(realTime2Sec | |
134 (f.timestamp + | |
135 (f.hasDuration ? f.duration : Vamp::RealTime::zeroTime))); | |
136 } else { | 153 } else { |
137 d += QString("\"time\": { \"value\": %1 }") | 154 d += QString("\"time\": { \"value\": %1 }").arg(timestr); |
138 .arg(realTime2Sec(f.timestamp)); | |
139 } | 155 } |
140 | 156 |
141 if (f.label != "") { | 157 if (f.label != "") { |
142 d += QString(", \"label\": { \"value\": \"%2\" }") | 158 d += QString(", \"label\": { \"value\": \"%2\" }") |
143 .arg(f.label.c_str()); | 159 .arg(f.label.c_str()); |
149 if (isnan(f.values[j])) { | 165 if (isnan(f.values[j])) { |
150 d += "\"NaN\""; | 166 d += "\"NaN\""; |
151 } else if (isinf(f.values[j])) { | 167 } else if (isinf(f.values[j])) { |
152 d += "\"Inf\""; | 168 d += "\"Inf\""; |
153 } else { | 169 } else { |
154 d += QString("%1").arg(f.values[j]); | 170 d += QString("%1").arg(f.values[j], 0, 'g', m_digits); |
155 } | 171 } |
156 if (j + 1 < int(f.values.size())) { | 172 if (j + 1 < int(f.values.size())) { |
157 d += ", "; | 173 d += ", "; |
158 } | 174 } |
159 } | 175 } |
437 json += stpl.arg("window_type") | 453 json += stpl.arg("window_type") |
438 .arg(Window<float>::getNameForType(t.getWindowType()).c_str()); | 454 .arg(Window<float>::getNameForType(t.getWindowType()).c_str()); |
439 } | 455 } |
440 | 456 |
441 if (t.getStartTime() != RealTime::zeroTime) { | 457 if (t.getStartTime() != RealTime::zeroTime) { |
442 json += ntpl.arg("start").arg(t.getStartTime().toDouble()); | 458 json += ntpl.arg("start") |
459 .arg(t.getStartTime().toDouble(), 0, 'g', 9); | |
443 } | 460 } |
444 | 461 |
445 if (t.getDuration() != RealTime::zeroTime) { | 462 if (t.getDuration() != RealTime::zeroTime) { |
446 json += ntpl.arg("duration").arg(t.getDuration().toDouble()); | 463 json += ntpl.arg("duration") |
464 .arg(t.getDuration().toDouble(), 0, 'g', 9); | |
447 } | 465 } |
448 | 466 |
449 if (t.getSampleRate() != 0) { | 467 if (t.getSampleRate() != 0) { |
450 json += ntpl.arg("sample_rate").arg(t.getSampleRate()); | 468 json += ntpl.arg("sample_rate").arg(t.getSampleRate()); |
451 } | 469 } |
458 if (i != parameters.begin()) { | 476 if (i != parameters.begin()) { |
459 json += ",\n"; | 477 json += ",\n"; |
460 } | 478 } |
461 QString name = i->first; | 479 QString name = i->first; |
462 float value = i->second; | 480 float value = i->second; |
463 json += QString(" \"%1\": %2").arg(name).arg(value); | 481 json += QString(" \"%1\": %2") |
482 .arg(name) | |
483 .arg(value, 0, 'g', 8); // parameter values always to high precision | |
464 } | 484 } |
465 json += QString("\n },\n"); | 485 json += QString("\n },\n"); |
466 } | 486 } |
467 | 487 |
468 // no trailing comma on final property: | 488 // no trailing comma on final property: |