1use std::path::Path;
4
5use sva_engine::{
6 Alias, AliasBand, Answer, Arguments, BandCrest, BandTrack, Bands, Binding, Buffer, CacheStats,
7 Cost, Crest, Detail, EnvelopeFrame, FormantFrame, Label, LedgerEntry, Loudness, LoudnessFrame,
8 Outcome, Output, PayloadKind, Pruned, Source, SpectralSum, Spectrum, StereoFrame, StereoImage,
9 Work,
10};
11
12use crate::json::{NONE, capped, escape, latest, list, num};
13
14pub const SAMPLE_LIMIT: usize = 4096;
16
17fn maybe(v: Option<f64>) -> String {
18 v.map(num).unwrap_or_else(|| NONE.to_string())
19}
20
21fn counted(v: Option<usize>) -> String {
22 v.map_or_else(|| NONE.to_string(), |c| c.to_string())
23}
24
25fn ledger_json(entries: &[LedgerEntry], skim: bool) -> String {
26 list(entries, |e| {
27 let clipped = e
28 .clipped
29 .map_or_else(|| NONE.to_string(), |c| c.to_string());
30 let channel = counted(e.channel);
31 if skim {
32 return format!(
33 "\n {{ \"node\": \"{}\", \"channel\": {channel}, \"rms\": {}, \"peak\": {}, \"clipped\": {clipped} }}",
34 escape(&e.node),
35 num(e.rms),
36 num(e.peak)
37 );
38 }
39 format!(
40 "\n {{ \"node\": \"{}\", \"channel\": {channel}, \"depth\": {}, \"unit\": \"{}\", \
41 \"control\": {}, \"rms\": {}, \"peak\": {}, \"min\": {}, \"max\": {}, \
42 \"share\": {}, \"clipped\": {clipped} }}",
43 escape(&e.node),
44 e.depth,
45 e.kind.unit().name(),
46 !e.kind.is_audio(),
47 num(e.rms),
48 num(e.peak),
49 num(e.min),
50 num(e.max),
51 maybe(e.share)
52 )
53 })
54}
55
56fn spectrum_json(s: &Spectrum) -> String {
57 format!(
58 "{{ \"frame_size\": {}, \"frames\": {}, \"resolution_hz\": {}, \"rms\": {}, \
59 \"centroid_hz\": {}, \"rolloff85_hz\": {}, \"peaks\": {}, \"bands\": {} }}",
60 s.frame_size,
61 s.frames,
62 num(s.resolution_hz),
63 num(s.rms),
64 num(s.centroid_hz),
65 num(s.rolloff85_hz),
66 list(&s.peaks, |p| format!(
67 "{{ \"hz\": {}, \"db\": {} }}",
68 num(p.hz),
69 num(p.db)
70 )),
71 list(&s.bands, |b| format!(
72 "\n {{ \"lo_hz\": {}, \"hi_hz\": {}, \"db\": {} }}",
73 num(b.lo_hz),
74 num(b.hi_hz),
75 num(b.db)
76 ))
77 )
78}
79
80fn stereo_json(image: &StereoImage) -> String {
81 let frame = |f: &StereoFrame| {
82 format!(
83 "\n {{ \"t\": {}, \"correlation\": {}, \"mid_rms\": {}, \"side_rms\": {}, \
84 \"width\": {}, \"balance_db\": {}, \"mono_db\": {} }}",
85 num(f.t_secs),
86 num(f.correlation),
87 num(f.mid_rms),
88 num(f.side_rms),
89 num(f.width),
90 num(f.balance_db),
91 num(f.mono_db)
92 )
93 };
94 format!(
95 "{{ \"channels\": {}, \"overall\": {}, \"frames\": {} }}",
96 image.channels,
97 frame(&image.overall),
98 list(&image.frames, frame)
99 )
100}
101
102fn formants_json(f: &FormantFrame) -> String {
103 format!(
104 "\n {{ \"t\": {}, \"order\": {}, \"energy\": {}, \"residual\": {}, \"formants\": {} }}",
105 num(f.t_secs),
106 f.order,
107 num(f.energy),
108 num(f.residual),
109 list(&f.formants, |v| format!(
110 "{{ \"hz\": {}, \"bandwidth_hz\": {}, \"db\": {} }}",
111 num(v.hz),
112 num(v.bandwidth_hz),
113 num(v.db)
114 ))
115 )
116}
117
118fn band_json(b: &BandTrack, start_secs: f64, rate_hz: f64, limit: Option<usize>) -> String {
119 let shown = limit.map_or(b.rms.len(), |cap| b.rms.len().min(cap));
120 format!(
121 "\n {{ \"centre_hz\": {}, \"cam\": {}, \"erb_hz\": {}, \"q\": {}, \"peak\": {}, \
122 \"time_to_peak_secs\": {}, \"rise_10_90_secs\": {}, \
123 \"floor\": {{ \"peak\": {}, \"time_to_peak_secs\": {}, \"rise_10_90_secs\": {} }}, \
124 \"rms\": {} }}",
125 num(b.centre_hz),
126 num(b.cam),
127 num(b.erb_hz),
128 num(b.q),
129 num(b.peak),
130 maybe(b.time_to_peak_secs),
131 maybe(b.rise_10_90_secs),
132 num(b.floor.peak),
133 num(b.floor.time_to_peak_secs),
134 maybe(b.floor.rise_10_90_secs),
135 capped(
136 &b.rms,
137 shown,
138 |n| start_secs + n as f64 / rate_hz,
139 |v| num(*v),
140 )
141 )
142}
143
144fn bands_json(b: &Bands, limit: Option<usize>) -> String {
145 format!(
146 "{{ \"rate_hz\": {}, \"start_secs\": {}, \"bands\": {} }}",
147 num(b.rate_hz),
148 num(b.start_secs),
149 list(&b.bands, |band| band_json(
150 band,
151 b.start_secs,
152 b.rate_hz,
153 limit
154 ))
155 )
156}
157
158fn loudness_json(l: &Loudness, limit: Option<usize>) -> String {
159 let frames = |fs: &[LoudnessFrame]| {
160 let shown = limit.map_or(fs.len(), |cap| fs.len().min(cap));
161 capped(
162 fs,
163 shown,
164 |n| fs[n].t,
165 |f| format!("\n {{ \"t\": {}, \"lufs\": {} }}", num(f.t), num(f.lufs)),
166 )
167 };
168 format!(
169 "{{ \"integrated_lufs\": {}, \"range_lu\": {}, \"momentary_max_lufs\": {}, \
170 \"short_term_max_lufs\": {}, \"sample_peak\": {}, \"sample_peak_dbfs\": {}, \
171 \"peak_note\": \"{}\", \"momentary\": {}, \"short_term\": {} }}",
172 maybe(l.integrated_lufs),
173 maybe(l.range_lu),
174 maybe(l.momentary_max_lufs),
175 maybe(l.short_term_max_lufs),
176 num(l.sample_peak),
177 maybe(l.sample_peak_dbfs),
178 escape(l.peak_note),
179 frames(&l.momentary),
180 frames(&l.short_term)
181 )
182}
183
184fn crest_json(c: &Crest) -> String {
185 let band = |b: &BandCrest| {
186 format!(
187 "\n {{ \"lo_hz\": {}, \"hi_hz\": {}, \"centre_hz\": {}, \"peak\": {}, \
188 \"rms\": {}, \"crest_db\": {}, \"counted\": {} }}",
189 num(b.lo_hz),
190 num(b.hi_hz),
191 num(b.centre_hz),
192 num(b.peak),
193 num(b.rms),
194 num(b.crest_db),
195 b.counted
196 )
197 };
198 format!(
199 "{{ \"broadband_crest_db\": {}, \"spread_db\": {}, \"widest_band_hz\": {}, \
200 \"tightest_band_hz\": {}, \"counted_under_db\": {}, \"bands\": {} }}",
201 num(c.broadband_crest_db),
202 maybe(c.spread_db),
203 maybe(c.widest_band_hz),
204 maybe(c.tightest_band_hz),
205 num(c.counted_under_db),
206 list(&c.bands, band)
207 )
208}
209
210fn alias_json(a: &Alias) -> String {
211 let band = |b: &AliasBand| {
212 format!(
213 "\n {{ \"lo_hz\": {}, \"hi_hz\": {}, \"signal_db\": {}, \"alias_db\": {}, \
214 \"nmr_db\": {} }}",
215 num(b.lo_hz),
216 num(b.hi_hz),
217 num(b.signal_db),
218 num(b.alias_db),
219 num(b.nmr_db)
220 )
221 };
222 format!(
223 "{{ \"oversample\": {}, \"sample_rate\": {}, \"frame_size\": {}, \"frames\": {}, \
224 \"scored_frames\": {}, \"playback_db_spl\": {}, \"asr_db\": {}, \"nmr_db\": {}, \
225 \"nmr_peak_db\": {}, \"peak_at_secs\": {}, \"audible\": {}, \
226 \"instances\": {}, \"bands\": {} }}",
227 a.oversample,
228 num(a.sample_rate),
229 a.frame_size,
230 a.frames,
231 a.scored_frames,
232 num(a.playback_db_spl),
233 num(a.asr_db),
234 num(a.nmr_db),
235 num(a.nmr_peak_db),
236 num(a.peak_at_secs),
237 a.audible,
238 a.instances,
239 list(&a.bands, band)
240 )
241}
242
243fn binding_json(b: &Binding) -> String {
244 format!(
245 "\n {{ \"name\": \"{}\", \"source\": \"{}\" }}",
246 escape(&b.name),
247 escape(&b.source)
248 )
249}
250
251fn arguments_json(a: &Arguments) -> String {
252 let at = |s: sva_ast::ByteSpan| format!("{{ \"start\": {}, \"end\": {} }}", s.start, s.end);
253 let calls = list(&a.calls, |c| {
254 let arguments = list(&c.arguments, |x| {
255 format!(
256 "{{ \"name\": \"{}\", \"value\": {}, \"written\": {} }}",
257 escape(&x.name),
258 num(x.value),
259 x.written
260 )
261 });
262 format!(
263 "\n {{ \"name\": \"{}\", \"at\": {}, \"arguments\": {arguments} }}",
264 escape(&c.name),
265 at(c.at)
266 )
267 });
268 let chosen = list(&a.chosen, |c| {
269 format!(
270 "\n {{ \"name\": \"{}\", \"at\": {}, \"operands\": {}, \"chosen\": {} }}",
271 escape(&c.name),
272 at(c.at),
273 list(&c.operands, |v| num(*v)),
274 c.chosen
275 )
276 });
277 format!(
278 "\n {{ \"node\": \"{}\", \"calls\": {calls}, \"chosen\": {chosen} }}",
279 escape(&a.node)
280 )
281}
282
283fn samples_json(b: &Buffer, limit: Option<usize>) -> String {
285 let component = |c: usize| {
286 let plane = b.plane(c);
287 let shown = limit.map_or(plane.len(), |cap| plane.len().min(cap));
288 format!(
289 "\n {{ \"channel\": {c}, \"values\": {} }}",
290 capped(
291 plane,
292 shown,
293 |n| b.origin_secs() + n as f64 / f64::from(b.rate),
294 |v| num(*v)
295 )
296 )
297 };
298 let components: Vec<usize> = (0..b.width).collect();
299 format!(
300 "{{ \"rate\": {}, \"origin_secs\": {}, \"width\": {}, \"components\": {} }}",
301 b.rate,
302 num(b.origin_secs()),
303 b.width,
304 list(&components, |c| component(*c))
305 )
306}
307
308fn symbolic_json(n: &SpectralSum) -> String {
309 let terms: Vec<String> = n.atoms().map(sva_engine::sketch_atom).collect();
310 format!(
311 "{{ \"var\": \"{}\", \"lanes\": {}, \"terms\": {} }}",
312 match n.var {
313 sva_engine::Var::T => "t",
314 sva_engine::Var::F => "f",
315 },
316 n.lanes.len(),
317 crate::json::strings(&terms)
318 )
319}
320
321fn flops_json(tree: &sva_engine::FlopTree) -> String {
323 format!(
324 "{{ \"total\": {}, \"budget\": {}, \"rows\": {} }}",
325 tree.total,
326 tree.budget,
327 list(&tree.rows, |r: &sva_engine::FlopRow| format!(
328 "\n {{ \"depth\": {}, \"node\": \"{}\", \"own\": {}, \"subtree\": {}, \
329 \"percent\": {}, \"route\": \"{}\", \"shared\": {} }}",
330 r.depth,
331 escape(&r.node),
332 r.own,
333 r.subtree,
334 num(r.percent),
335 escape(r.route),
336 r.shared
337 ))
338 )
339}
340
341pub fn value_json(output: &Output, limit: Option<usize>, skim: bool) -> String {
344 match output {
345 Output::Lines(lines) => list(lines, line_json),
346 Output::Atoms(sketches) => crate::json::strings(sketches),
347 Output::Symbolic(sum) => symbolic_json(sum),
348 Output::Samples(buffer) => samples_json(buffer, limit),
349 Output::Ledger(entries) => ledger_json(entries, skim),
350 Output::Spectrum(s) => spectrum_json(s),
351 Output::Stereo(image) => stereo_json(image),
352 Output::Bands(b) => bands_json(b, limit),
353 Output::Loudness(l) => loudness_json(l, limit),
354 Output::Crest(c) => crest_json(c),
355 Output::Alias(a) => alias_json(a),
356 Output::Bindings(b) => list(b, binding_json),
357 Output::Arguments(a) => list(a, arguments_json),
358 Output::Flops(tree) => flops_json(tree),
359 Output::Envelope(frames) => list(frames, |f: &EnvelopeFrame| {
360 format!(
361 "\n {{ \"t\": {}, \"rms\": {}, \"peak\": {} }}",
362 num(f.t_secs),
363 num(f.rms),
364 num(f.peak)
365 )
366 }),
367 Output::Pitch(frames) => list(frames, |f| {
368 format!(
369 "\n {{ \"t\": {}, \"notes\": {} }}",
370 num(f.t_secs),
371 list(&f.notes, |n| format!(
372 "{{ \"note\": \"{}\", \"hz\": {}, \"cents\": {}, \"db\": {}, \
373 \"harmonic_of\": {} }}",
374 escape(&n.name),
375 num(n.hz),
376 num(n.cents),
377 num(n.db),
378 maybe(n.harmonic_of)
379 ))
380 )
381 }),
382 Output::Formants(frames) => list(frames, formants_json),
383 }
384}
385
386fn line_json(l: &sva_engine::Line) -> String {
387 format!(
388 "\n {{ \"hz\": {}, \"re\": {}, \"im\": {}, \"db\": {} }}",
389 num(l.hz),
390 num(l.amp.re),
391 num(l.amp.im),
392 num(20.0 * l.amp.abs().log10())
393 )
394}
395
396pub fn answer_json(answer: &Answer, limit: Option<usize>, skim: bool) -> String {
400 format!(
401 "{{ \"source\": \"{}\", \"profile\": \"{}\", \"rate\": {}, \"value\": {}, \
402 \"dropped\": {}, \"tail_db\": {} }}",
403 match answer.source {
404 Source::Exact => "exact",
405 Source::Measured => "measured",
406 },
407 escape(answer.profile),
408 answer
409 .rate
410 .map_or_else(|| NONE.to_string(), |r| r.to_string()),
411 value_json(&answer.value, limit, skim),
412 list(&answer.dropped, line_json),
413 maybe(answer.tail_db)
414 )
415}
416
417fn detail_json(detail: &Detail) -> String {
419 match detail {
420 Detail::Lines {
421 placed,
422 summed,
423 dropped,
424 dropped_more,
425 terms,
426 tail_db,
427 ..
428 } => format!(
429 ", \"placed\": {placed}, \"summed\": {summed}, \"dropped\": {}, \
430 \"dropped_more\": {dropped_more}, \"terms\": {}, \"tail_db\": {}",
431 list(dropped, |d| format!(
432 "{{ \"hz\": {}, \"db\": {} }}",
433 num(d.hz),
434 num(d.db)
435 )),
436 counted(*terms),
437 maybe(*tail_db)
438 ),
439 Detail::Cropped { tail_db, .. } => format!(", \"tail_db\": {}", maybe(*tail_db)),
440 Detail::Point { alias_db, .. } => format!(", \"alias_db\": {}", maybe(*alias_db)),
441 Detail::Spectrum { wrap_db, .. } => format!(", \"wrap_db\": {}", num(*wrap_db)),
442 Detail::Roundtrip { edited, .. } => format!(", \"edited\": {edited}"),
443 Detail::Continuous { .. } | Detail::Reading { .. } => String::new(),
444 Detail::Added { parts } => format!(
445 ", \"addends\": {}",
446 list(parts, |part| format!(
447 "{{ \"rule\": \"{}\"{} }}",
448 part.rule().as_str(),
449 detail_json(part)
450 ))
451 ),
452 }
453}
454
455pub fn label_json(label: &Label) -> String {
458 let detail = detail_json(&label.detail);
459 let cost = match label.cost {
460 Some(Cost { flops, budget }) => format!(", \"flops\": {flops}, \"flop_budget\": {budget}"),
461 None => format!(", \"flops\": {NONE}, \"flop_budget\": {NONE}"),
462 };
463 let pruned = label.pruned.as_ref().map_or(NONE.to_string(), pruned_json);
464 let cost = format!(
465 "{cost}, \"moved_s\": {}, \"pruned\": {pruned}",
466 maybe(label.moved)
467 );
468 format!(
469 "{{ \"source\": \"{}\", \"profile\": \"{}\", \"rate\": {}, \"rule\": \"{}\"{detail}{cost} }}",
470 match label.source {
471 Source::Exact => "exact",
472 Source::Measured => "measured",
473 },
474 escape(label.profile),
475 label.rate,
476 escape(label.rule().as_str())
477 )
478}
479
480pub fn pruned_json(pruned: &Pruned) -> String {
483 let cuts = list(&pruned.cuts, |(node, from)| {
484 format!("{{ \"node\": \"{}\", \"from\": {from} }}", escape(node))
485 });
486 format!("{{ \"db\": {}, \"cuts\": {cuts} }}", num(pruned.db))
487}
488
489pub fn work_json(work: &Work) -> String {
491 let waves = work.waves.map_or(NONE.to_string(), |w| w.to_string());
492 format!(
493 "{{ \"samples\": {}, \"priced_flops\": {}, \"waves\": {waves} }}",
494 work.samples, work.priced_flops
495 )
496}
497
498pub fn stats_json(stats: &CacheStats) -> String {
501 stats_with(stats, "")
502}
503
504pub fn stream_stats_json(
508 stats: &CacheStats,
509 (dropped, made): (&[&str], usize),
510 pruned: &Pruned,
511) -> String {
512 let dropped = latest(dropped, made, |name| format!("\"{}\"", escape(name)));
513 let pruned = pruned_json(pruned);
514 stats_with(
515 stats,
516 &format!(", \"dropped\": {dropped}, \"pruned\": {pruned}"),
517 )
518}
519
520fn stats_with(stats: &CacheStats, extra: &str) -> String {
521 let made = stats.shed + stats.lookups.len();
522 let lookups = latest(&stats.lookups, made, |l| {
523 let outcome = match l.outcome {
524 Outcome::Hit => "hit",
525 Outcome::ComputedStored => "computed_stored",
526 Outcome::ComputedNotStored => "computed_not_stored",
527 Outcome::ComputedReplaced => "computed_replaced",
528 Outcome::Extended => "extended",
529 Outcome::Prefix => "prefix",
530 };
531 format!(
532 "{{ \"node\": \"{}\", \"key\": \"{}\", \"kind\": \"{}\", \"outcome\": \"{outcome}\" }}",
533 escape(&l.node),
534 l.key,
535 match l.kind {
536 PayloadKind::Segments => "segments",
537 PayloadKind::Frames => "frames",
538 PayloadKind::Run => "run",
539 }
540 )
541 });
542 format!(
543 "{{ \"nodes\": {}, \"hits\": {}, \"computed\": {}, \"stored\": {}, \"replaced\": {}, \"extended\": {}, \
544 \"bytes\": {}, \"max_bytes\": {}, \"entries\": {}, \"evictions\": {}, \
545 \"lookups\": {lookups}{extra} }}",
546 stats.nodes(),
547 stats.hits(),
548 stats.computed(),
549 stats.stored(),
550 stats.replaced(),
551 stats.extended(),
552 stats.bytes,
553 stats.max_bytes,
554 stats.entries,
555 stats.evictions
556 )
557}
558
559pub struct Printed {
561 pub name: String,
562 pub answer: Answer,
563 pub skim: bool,
565}
566
567pub struct Report<'a> {
569 pub target: &'a str,
570 pub rate: u32,
571 pub bits: Option<i32>,
573 pub interval: Option<(f64, f64)>,
575 pub profile: &'a str,
576 pub label: Option<&'a Label>,
577 pub written: &'a [(String, &'a Path)],
578 pub answers: &'a [Printed],
579 pub analyses: &'a [(String, String)],
582 pub limit: Option<usize>,
583}
584
585pub fn query_data(report: &Report) -> String {
587 let written = list(report.written, |(name, path)| {
588 format!(
589 "{{ \"representation\": \"{}\", \"path\": \"{}\" }}",
590 escape(name),
591 escape(&path.display().to_string())
592 )
593 });
594 let label = report
595 .label
596 .map(label_json)
597 .unwrap_or_else(|| NONE.to_string());
598 let reads = report
599 .answers
600 .iter()
601 .map(|printed| {
602 format!(
603 "\"{}\": {}",
604 escape(&printed.name),
605 answer_json(&printed.answer, report.limit, printed.skim)
606 )
607 })
608 .chain(report.analyses.iter().map(|(name, value)| {
609 format!(
610 "\"{}\": {{ \"source\": \"measured\", \"profile\": \"{}\", \"rate\": {}, \
611 \"value\": {value} }}",
612 escape(name),
613 escape(report.profile),
614 report.rate
615 )
616 }))
617 .collect::<Vec<_>>()
618 .join(",\n ");
619 let interval = report.interval.map_or(NONE.to_string(), |(start, end)| {
620 format!(
621 "{{ \"start_secs\": {}, \"end_secs\": {} }}",
622 num(start),
623 num(end)
624 )
625 });
626 format!(
627 "{{\n \"target\": \"{}\",\n \"sample_rate\": {},\n \"bits\": {},\n \
628 \"profile\": \"{}\",\n \"interval\": {interval},\n \"label\": {label},\n \
629 \"written\": {written},\n \"representations\": {{\n {reads}\n }}\n}}",
630 escape(report.target),
631 report.rate,
632 report.bits.map_or(NONE.to_string(), |b| b.to_string()),
633 escape(report.profile),
634 )
635}