alpha2_engine/extract/
wire.rs1use alpha2_model::{DataInterval, DataWord};
4use alpha2_time::MediaTime;
5use serde_json::{Value, json};
6
7use super::{Emit, ExtractSession, RunContext};
8
9pub fn emit_from_json(value: &Value) -> Result<Emit, String> {
16 let output = value
17 .get("output")
18 .and_then(Value::as_str)
19 .ok_or("an emit needs `output`")?
20 .to_owned();
21 let tick = |name: &str| value.get(name).and_then(Value::as_i64).map(MediaTime);
22 if let Some(values) = value.get("values") {
23 let time = tick("time").ok_or("a sample needs an integer `time`")?;
24 let values = values
25 .as_array()
26 .ok_or("`values` is an array")?
27 .iter()
28 .map(|value| match value {
29 Value::Null => Ok(None),
30 #[allow(clippy::cast_possible_truncation)]
31 Value::Number(number) => number
32 .as_f64()
33 .map(|number| Some(number as f32))
34 .ok_or("a value is a number or null"),
35 _ => Err("a value is a number or null"),
36 })
37 .collect::<Result<Vec<_>, _>>()?;
38 return Ok(Emit::Sample {
39 output,
40 time,
41 values,
42 });
43 }
44 let start = tick("start").ok_or("an item needs an integer `start`")?;
45 let end = tick("end").ok_or("an item needs an integer `end`")?;
46 let text = |name: &str| value.get(name).and_then(Value::as_str).map(str::to_owned);
47 if let Some(word) = text("text") {
48 return Ok(Emit::Word {
49 output,
50 word: DataWord {
51 start,
52 end,
53 text: word,
54 speaker: text("speaker"),
55 },
56 });
57 }
58 #[allow(clippy::cast_possible_truncation)]
59 let score = value
60 .get("score")
61 .and_then(Value::as_f64)
62 .map(|score| score as f32);
63 Ok(Emit::Interval {
64 output,
65 interval: DataInterval {
66 start,
67 end,
68 label: text("label"),
69 score,
70 },
71 })
72}
73
74#[must_use]
76pub fn client_context_json(context: &RunContext) -> Value {
77 json!({
78 "range": context.range,
79 "passes": context.passes,
80 "from": context.from,
81 "video": context.video.map(|(size, format)| json!({ "size": size, "format": format })),
82 "audio": context.audio.map(|(rate, channels, block)| json!({ "rate": rate, "channels": channels, "blockFrames": block })),
83 "frameRate": context.frame_rate,
84 })
85}
86
87#[must_use]
90pub fn progress_json(session: &ExtractSession) -> Value {
91 let (stage, pass) = session.stage();
92 let progress = session.progress()[pass];
93 json!({
94 "stage": stage.name(),
95 "pass": pass,
96 "completed": progress.completed,
97 "total": progress.total,
98 "covered": progress.covered,
99 "span": progress.span,
100 })
101}