sva-engine 0.7.11

Renders a resolved graph into per-node buffers a query can be asked of
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Concern: folds one node's Expr into the closed form, cast or sampled op its type names | Non-concern: typing a term (sva-formula), one builtin's image (calls.rs) | IO: (Expr, Cx) -> a typed node

mod calls;
mod casts;
mod constant;
pub(crate) mod physics;
mod solvers;
mod walk;
mod waves;

use std::borrow::Cow;

use sva_ast::{Address, Arg, ByteSpan, Expr, Literal};
use sva_formula::{Body, ClosedForm, Held, IndexId, NodeId, Part, Ty, Var};

use crate::cast::{Cast, Mismatch};
use crate::error::{Diagnostic, EngineError, Located};
use crate::instantiate::{Cx, Instances, Node};
use crate::loops::{self, SelfKind};
use crate::overload;
use crate::time::Grid;
use crate::typing::{Node as Typed, Typing, Value, When};

pub(crate) use calls::{noise_at, rand_arguments};
pub(crate) use constant::{
    constant_call, constant_modulo, constant_value, holds_infinite, number_of,
};
pub(crate) use solvers::{field, value_of};

/// One written subterm, either still inside a closed form or already a node of its own.
pub enum Piece {
    ClosedForm(Body),
    Value(NodeId),
}

/// What `self` stands for while this node's body is lowered: nothing, the zero a Neumann
/// series expands around, or a read of the node's own past, stepped, where the construct it
/// names makes the loop discrete.
enum SelfMode {
    Absent,
    Zero,
    Discrete(String),
}

pub struct Lowering<'a> {
    inst: &'a Instances,
    typing: &'a mut Typing,
    node: &'a str,
    indices: Vec<(String, IndexId)>,
    mode: SelfMode,
    own: Vec<(crate::time::Q, NodeId)>,
    grid: Grid,
}

/// Dependencies are already typed, so every ref this reads answers with a decided `Ty`.
pub fn node(path: &str, inst: &Instances, typing: &mut Typing) -> Result<NodeId, EngineError> {
    match typing.id(path) {
        Some(id) if !typing.pending(id) => Ok(id),
        _ => lowered(path, inst.grid(), inst, typing),
    }
}

/// `path` on `grid`, lowered once per step with its inputs there: its step is what `sp` counts
/// in and what a stateful node steps by.
pub(crate) fn on(
    path: &str,
    grid: Grid,
    inst: &Instances,
    typing: &mut Typing,
) -> Result<NodeId, EngineError> {
    let base = typing
        .id(path)
        .ok_or_else(|| EngineError::UnknownNode(path.to_string()))?;
    if grid.is_rate() {
        return Ok(base);
    }
    if let Some(id) = typing.copy(path, grid) {
        return Ok(id);
    }
    if !typing.opened(path) {
        return Err(crate::refs::cyclic(typing, base));
    }
    let found = lowered(path, grid, inst, typing);
    typing.closed(path);
    let id = found?;
    typing.copied(path, grid, id);
    Ok(id)
}

/// A value only at the steps it takes: no closed form, and nothing a formula reads anywhere.
pub(crate) fn holds_state(typing: &Typing, id: NodeId) -> bool {
    !typing.ty(id).is_closed_form() && !crate::schedule::anywhere(typing, id)
}

fn lowered(
    path: &str,
    grid: Grid,
    inst: &Instances,
    typing: &mut Typing,
) -> Result<NodeId, EngineError> {
    let (expr, cx) = inst
        .at(path)
        .ok_or_else(|| EngineError::UnknownNode(path.to_string()))?;
    typing.lowering(path);
    typing.begin(path, grid);
    let lowered = lowered_on(path, grid, (expr, cx), inst, typing);
    typing.end();
    lowered
}

fn lowered_on(
    path: &str,
    grid: Grid,
    (expr, cx): (&Expr, Cx),
    inst: &Instances,
    typing: &mut Typing,
) -> Result<NodeId, EngineError> {
    let cx = cx.on(grid);
    let var = axis_of(inst, typing, expr, cx, path)?;
    let kind = match inst.reads_self(path) {
        false => None,
        true => {
            let discrete = crosses(inst, typing, expr, cx);
            Some(loops::classify(inst, expr, cx, path, discrete))
        }
    };
    let (mode, closed) = match kind {
        Some(SelfKind::Refuse(e)) => return Err(*e),
        Some(SelfKind::Series { gain, delay }) => (SelfMode::Zero, Some((gain, delay))),
        Some(SelfKind::Discrete { why }) => (SelfMode::Discrete(why), None),
        None => (SelfMode::Absent, None),
    };
    let mut low = Lowering {
        inst,
        typing,
        node: path,
        indices: Vec::new(),
        mode,
        own: Vec::new(),
        grid,
    };
    let piece = low.walk(expr, cx, var)?;
    let piece = match closed {
        Some((gain, delay)) => low.expand(piece, var, gain, delay)?,
        None => piece,
    };
    low.seal(piece, var, Some(path))
}

/// What in the body leaves the closed form, if anything: a series expands a closed form around
/// itself, and nothing that already reached samples can be one of its terms.
fn crosses(inst: &Instances, typing: &Typing, e: &Expr, cx: Cx) -> Option<String> {
    if let Some(r) = inst.follow(e, cx, |e2, cx2| crosses(inst, typing, e2, cx2)) {
        return r;
    }
    match inst.node(e, cx) {
        Node::Lit(Literal::Samples(_)) => Some("a step in `sp`".to_string()),
        Node::Lit(_) | Node::Name(_) => None,
        Node::Bin(_, l, r) => crosses(inst, typing, l, cx).or_else(|| crosses(inst, typing, r, cx)),
        Node::Own {
            address: Address::Index,
            ..
        } => Some("the index read `self[...]`".to_string()),
        Node::Own { arg, .. } => crosses(inst, typing, arg, cx),
        Node::Read {
            path,
            address: Address::Index,
            ..
        } => Some(format!("the index read `@{path}[...]`")),
        Node::Signal { name, .. } => Some(format!("the index read `{name}[...]`")),
        Node::Read { path, .. } => typing
            .id(path)
            .is_some_and(|id| !typing.ty(id).is_closed_form())
            .then(|| format!("the sampled input `@{path}`")),
        Node::Call { name, args, .. } => {
            let sampled = matches!(name, "sample" | "stft" | "istft")
                || sva_ast::FINITE_DIFFERENCE.contains(&name);
            match sampled {
                true => Some(format!("the sampled input `{name}(...)`")),
                false => args.iter().find_map(|a| {
                    let (Arg::Pos(x) | Arg::Named(_, x)) = a;
                    crosses(inst, typing, x, cx)
                }),
            }
        }
    }
}

/// The axis a node's own closed form is written on: a bare `f` or a spectrum it reads puts it in `f`,
/// and a subtree under a cast declares its own.
fn axis_of(
    inst: &Instances,
    typing: &Typing,
    e: &Expr,
    cx: Cx,
    at: &str,
) -> Result<Var, EngineError> {
    let mut seen = (false, false);
    scan_axis(inst, typing, e, cx, &mut seen);
    match seen {
        (true, true) => Err(EngineError::refused(Diagnostic {
            code: "type.domain_mismatch".to_string(),
            message: format!("`{at}` has no overload for (t, f)."),
            location: Located::at(at, None),
            help: "write ifourier on the f side to work in t, or fourier on the t side to \
                   work in f"
                .to_string(),
        })),
        (_, true) => Ok(Var::F),
        _ => Ok(Var::T),
    }
}

fn scan_axis(inst: &Instances, typing: &Typing, e: &Expr, cx: Cx, seen: &mut (bool, bool)) {
    if inst
        .follow(e, cx, |e2, cx2| scan_axis(inst, typing, e2, cx2, seen))
        .is_some()
    {
        return;
    }
    match inst.node(e, cx) {
        Node::Lit(_) => {}
        Node::Name("t") => seen.0 = true,
        Node::Name("f") => seen.1 = true,
        Node::Name(_) => {}
        Node::Bin(_, l, r) => {
            scan_axis(inst, typing, l, cx, seen);
            scan_axis(inst, typing, r, cx, seen);
        }
        Node::Own { arg, .. } | Node::Signal { arg, .. } => scan_axis(inst, typing, arg, cx, seen),
        Node::Read { path, .. } => match typing.id(path).map(|id| typing.ty(id)) {
            Some(ty) if ty.has_dual() => {}
            Some(ty) if ty.held == Held::Form(Var::T) => seen.0 = true,
            Some(ty) if ty.held == Held::Form(Var::F) => seen.1 = true,
            _ => {}
        },
        Node::Call { name, args, .. } if Cast::from_name(name).is_none() => {
            for a in args {
                let (Arg::Pos(x) | Arg::Named(_, x)) = a;
                scan_axis(inst, typing, x, cx, seen);
            }
        }
        Node::Call { .. } => {}
    }
}

impl Lowering<'_> {
    fn here(&self, span: Option<ByteSpan>) -> Located {
        Located::at(self.node, span)
    }

    fn part(&mut self, f: Body, span: Option<ByteSpan>) -> Part {
        let origin = self.typing.mark(self.node, span);
        Part::new(origin, f)
    }

    fn refused(&self, code: &str, message: String, help: &str) -> EngineError {
        self.refused_at(code, message, help, None)
    }

    fn refused_at(
        &self,
        code: &str,
        message: String,
        help: &str,
        span: Option<ByteSpan>,
    ) -> EngineError {
        EngineError::refused(Diagnostic {
            code: code.to_string(),
            message,
            location: self.here(span),
            help: help.to_string(),
        })
    }

    fn refuse(&self, call: &str, m: &Mismatch, span: Option<ByteSpan>) -> EngineError {
        EngineError::refused(overload::format_refusal(call, m, self.here(span), &[]))
    }

    /// A subterm becomes a node of its own wherever a cast or a sampled operand ends the closed form.
    fn seal(&mut self, piece: Piece, var: Var, path: Option<&str>) -> Result<NodeId, EngineError> {
        match match piece {
            // A file forwarding one ref is a node of its own; an intermediate is not.
            Piece::ClosedForm(Body::Node(id)) if path.is_none() => Piece::Value(id),
            held => held,
        } {
            Piece::Value(id) => {
                let path = path.filter(|_| self.grid.is_rate());
                let settled = path
                    .and_then(|p| self.typing.id(p))
                    .filter(|held| self.typing.pending(*held));
                match settled {
                    Some(held) => {
                        let site = self.typing.mark(self.node, None);
                        let node = Typed {
                            name: self.node.to_string(),
                            ty: self.typing.ty(id),
                            var: self.typing.var(id),
                            value: Value::Read {
                                source: id,
                                at: When::At(crate::time::Affine::NOW),
                                site,
                            },
                            grid: self.grid,
                        };
                        self.typing.settle(held, node);
                        Ok(held)
                    }
                    None => {
                        if let Some(path) = path {
                            self.typing.alias(path, id);
                        }
                        Ok(id)
                    }
                }
            }
            Piece::ClosedForm(body) => {
                let origin = self.typing.mark(self.node, None);
                // FORMAT 15.3: a ref naming one number is that number to the typing too.
                let body = match crate::refs::fold_constants(self.typing, &body) {
                    Cow::Owned(folded) => folded,
                    Cow::Borrowed(_) => body,
                };
                if !matches!(body, Body::Const(_)) && holds_infinite(&body) {
                    return Err(
                        self.infinite("inf stands in a term that moves, which names no value")
                    );
                }
                let form = ClosedForm { var, body, origin };
                let ty = self.typing.infer_closed_form(&form)?;
                let node = Typed {
                    name: path.unwrap_or(self.node).to_string(),
                    ty,
                    var,
                    value: Value::ClosedForm(form),
                    grid: self.grid,
                };
                let path = path.filter(|_| self.grid.is_rate());
                match path.and_then(|p| self.typing.id(p)) {
                    Some(held) if self.typing.pending(held) => {
                        self.typing.settle(held, node);
                        Ok(held)
                    }
                    _ => Ok(self.typing.push(node, path)),
                }
            }
        }
    }

    /// `path` on the grid this node steps on.
    fn source(&mut self, path: &str) -> Result<NodeId, EngineError> {
        on(path, self.grid, self.inst, self.typing)
    }

    fn register(&mut self, value: Value, ty: Ty, var: Var) -> NodeId {
        let name = self.node.to_string();
        self.typing.push(
            Typed {
                name,
                ty,
                var,
                value,
                grid: self.grid,
            },
            None,
        )
    }
}

#[cfg(test)]
mod tests {
    use sva_formula::NodeId;

    /// A closed form is exact at any instant, so a filter over it read at many shifts, each
    /// landing between two samples, leaves the form lowered once.
    #[test]
    fn a_closed_form_read_at_many_fractional_shifts_is_lowered_once() {
        let reads: Vec<String> = (1..=24)
            .map(|k| format!("@hit(t - {}s)", 0.0123 + f64::from(k) * 0.0000137))
            .collect();
        let mut files = sva_ast::Composition::new();
        files
            .insert("env", "exp(-t/0.01)*sin(2*pi*440*t)\n")
            .insert("hit", "lowpass(sample(@env), cutoff=900)\n")
            .insert("song", format!("{}\n", reads.join(" + ")));
        let g = sva_ast::load(&files).expect("a composition");
        let typing = crate::types(&g, "song").unwrap_or_else(|e| panic!("song: {e}"));
        let forms = (0..typing.len())
            .map(|n| NodeId(n as u32))
            .filter(|id| typing.name(*id) == "env")
            .count();
        assert_eq!(forms, 1, "env is lowered once, not once per shift");
    }

    /// A signal passed in as `x` reads by index as a ref does: an allpass reads its stateful
    /// input's nearest step at a delay between two samples, and `x(t - d)` reads the same one
    /// value at the whole sample `d` rounds to.
    #[test]
    fn a_parameter_read_by_index_lowers_its_signal_once() {
        const RATE: u32 = 8_000;
        let mut files = sva_ast::Composition::new();
        files
            .insert(
                "src",
                "lowpass(crop(sample(sin(2*pi*220*t)), 0s, 0.02s), cutoff=900)\n",
            )
            .insert(
                "allpass",
                "-g*x + x[idx(t - 0.00510204s)] + g*self[idx(t - 0.00510204s)]\n",
            )
            .insert(
                "timed",
                "-g*x + x(t - 0.00510204s) + g*self[idx(t - 0.00510204s)]\n",
            )
            .insert("passed", "@allpass(t, x=@src, g=0.5)\n")
            .insert("shifted", "@timed(t, x=@src, g=0.5)\n")
            .insert(
                "written",
                "-0.5*@src + @src[idx(t - 0.00510204s)] + 0.5*self[idx(t - 0.00510204s)]\n",
            );
        let g = sva_ast::load(&files).expect("a composition");
        let filters = |root: &str| {
            let typing = crate::types_at(&g, root, RATE).unwrap_or_else(|e| panic!("{root}: {e}"));
            (0..typing.len())
                .map(|n| NodeId(n as u32))
                .filter(|id| typing.name(*id) == "src")
                .filter(|id| matches!(typing.value(*id), crate::Value::Filter { .. }))
                .count()
        };
        assert_eq!(
            filters("passed"),
            1,
            "src is lowered once, on the render's grid"
        );
        assert_eq!(
            filters("shifted"),
            1,
            "a read at t - d reads src's one value at a whole-sample shift"
        );

        let config = crate::RenderConfig::seconds(RATE, 0.05);
        let out = |root: &str| -> Vec<u64> {
            let held = crate::render(&g, root, config.clone(), &crate::Tier::default())
                .unwrap_or_else(|e| panic!("{root}: {e}"));
            let id = held.id(root).expect("the root");
            let plane = held.output(id).expect("a buffer").plane(0).to_vec();
            assert!(plane.iter().any(|v| *v != 0.0), "silence tests nothing");
            plane.iter().map(|v| v.to_bits()).collect()
        };
        assert_eq!(
            out("passed"),
            out("written"),
            "x[i] reads what @src[i] reads, bit for bit"
        );
    }
}