sva-samples 0.7.17

Buffers, frames, the f64 machine, the collapse of a law onto a grid, and the measured observations over one
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Concern: runs one node renderer a block at a time, span after span, over a tape | Non-concern: the op array's own shape (ops.rs), cutting the spans (live.rs) | IO: (Spanned, reads, tape) -> samples

mod block;
mod live;
pub mod ops;
mod read;
pub mod renderer;
pub mod tape;

use crate::error::SampleError;
use crate::filters::FilterSite;
use crate::physics::{Solver, site};
use block::{BLOCK, Block, Here};
use ops::{Layout, Op, lowered};
use renderer::{Formula, Grid, Index, NodeRenderer, Site, Slot};
use tape::{Tape, Window};

pub use live::{Span, Spanned};

pub use ops::Layout as MachineLayout;

/// The op array, each op's width and operand slots, the formulas its ops name and the call
/// sites the run opens state for.
#[derive(Clone)]
pub(super) struct Program {
    ops: Vec<Op>,
    widths: Vec<usize>,
    args: Vec<Vec<usize>>,
    formulas: Vec<Formula>,
    indices: Vec<Index<usize>>,
    sites: Vec<Site>,
    pub width: usize,
}

impl NodeRenderer {
    pub(super) fn compile(&self, layout: &Layout) -> Result<Program, SampleError> {
        let (lowered, width) = lowered(self, layout)?;
        Ok(Program {
            ops: lowered.ops,
            widths: lowered.widths,
            args: lowered.args,
            formulas: lowered.formulas,
            indices: lowered.indices,
            sites: layout.sites.clone(),
            width,
        })
    }
}

#[derive(Clone)]
enum State {
    Filter(FilterSite),
    Physics(Box<dyn Solver>),
}

impl Clone for Box<dyn Solver> {
    fn clone(&self) -> Box<dyn Solver> {
        self.boxed()
    }
}

/// A filter site carries one lane per component of its widest argument, which the compiled
/// slot width already states.
fn open(p: &Program, grid: Grid) -> Result<Vec<State>, SampleError> {
    let mut lanes = vec![1usize; p.sites.len()];
    for (slot, op) in p.ops.iter().enumerate() {
        if let Op::Filter { site, .. } = op {
            lanes[site.0 as usize] = p.widths[slot];
        }
    }
    p.sites
        .iter()
        .zip(lanes)
        .map(|(s, width)| {
            Ok(match s {
                Site::Filter(shape) => State::Filter(FilterSite::new(
                    *shape,
                    width,
                    &[0.0],
                    &[0.0],
                    &[0.0],
                    grid.sr(),
                )),
                Site::Physics(params) => State::Physics(site(params, grid.sr())?),
            })
        })
        .collect()
}

/// Component `c` of an operand that may be mono where its neighbour is wide.
fn part(v: &[f64], c: usize) -> f64 {
    v[c.min(v.len() - 1)]
}

/// One compiled node and every call site's state, run over any span of the grid in order.
/// A span continues exactly where the last ended, so blocks write the samples one run would.
pub struct Machine {
    program: Program,
    states: Vec<State>,
    block: Block,
    grid: Grid,
    /// Each later span's first sample and the program it runs, the next one last.
    ahead: Vec<(i64, Program)>,
}

#[derive(Clone)]
pub struct MachineState {
    sites: Vec<Site>,
    states: Vec<State>,
}

impl State {
    fn bytes(&self) -> usize {
        match self {
            State::Filter(filter) => filter.bytes(),
            State::Physics(solver) => solver.bytes(),
        }
    }
}

impl MachineState {
    pub fn bytes(&self) -> usize {
        let states: usize = self.states.iter().map(State::bytes).sum();
        size_of::<Self>() + std::mem::size_of_val(self.sites.as_slice()) + states
    }
}

impl Machine {
    /// Stepping from `at`, each span's own program from where it starts.
    pub fn over(spanned: &Spanned, at: i64) -> Result<Machine, SampleError> {
        let grid = spanned.grid();
        let mut ahead: Vec<(i64, Program)> = spanned
            .compiled()
            .iter()
            .filter(|(span, _)| span.to > at)
            .map(|(span, program)| (span.from, program.clone()))
            .collect();
        ahead.reverse();
        let first = match ahead.pop() {
            Some((_, program)) => program,
            None => spanned.silent()?,
        };
        let states = open(&first, grid)?;
        let block = Block::of(&first.widths);
        Ok(Machine {
            program: first,
            states,
            block,
            grid,
            ahead,
        })
    }

    pub fn width(&self) -> usize {
        self.program.width
    }

    pub fn stateful(&self) -> bool {
        !self.program.sites.is_empty()
    }

    pub fn bytes(&self) -> usize {
        self.states.iter().map(State::bytes).sum()
    }

    pub fn run_to(&mut self, to: i64, reads: &[Window], own: &mut Tape) -> Result<(), SampleError> {
        for state in &mut self.states {
            if let State::Filter(filter) = state {
                filter.forget_frames();
            }
        }
        self.steps(to, reads, own)
    }

    /// `run_to`, the filters' frames kept.
    pub fn run_on(&mut self, to: i64, reads: &[Window], own: &mut Tape) -> Result<(), SampleError> {
        self.steps(to, reads, own)
    }

    fn steps(&mut self, to: i64, reads: &[Window], own: &mut Tape) -> Result<(), SampleError> {
        loop {
            while let Some((from, _)) = self.ahead.last()
                && *from <= own.end()
            {
                let (_, program) = self.ahead.pop().expect("a span ahead");
                self.block = Block::of(&program.widths);
                self.program = program;
            }
            let until = self.ahead.last().map_or(to, |(from, _)| (*from).min(to));
            self.stepped(until, reads, own)?;
            if own.end() >= to {
                return Ok(());
            }
        }
    }

    fn stepped(&mut self, to: i64, reads: &[Window], own: &mut Tape) -> Result<(), SampleError> {
        let p = &self.program;
        while own.end() < to {
            let from = own.end();
            let most = (to - from).min(BLOCK as i64) as usize;
            let len = p.block(from, most, &mut self.block.recurrent);
            let here = Here {
                reads,
                own: own.window(),
                grid: self.grid,
            };
            let (held, refused) =
                block::run(p, &mut self.block, &here, &mut self.states, (from, len));
            for i in 0..held {
                let top = self.block.top(p, i);
                for c in 0..p.width {
                    own.push(c, part(top, c));
                }
            }
            if let Some(e) = refused {
                return Err(e);
            }
        }
        Ok(())
    }

    /// Call site `site`'s solver as it stands now, where that site is one.
    pub fn solver(&self, site: usize) -> Option<&dyn Solver> {
        match self.states.get(site)? {
            State::Physics(solver) => Some(solver.as_ref()),
            State::Filter(_) => None,
        }
    }

    pub fn filter(&self, site: usize) -> Option<&FilterSite> {
        match self.states.get(site)? {
            State::Filter(filter) => Some(filter),
            State::Physics(_) => None,
        }
    }

    pub fn state(&self) -> MachineState {
        MachineState {
            sites: self.program.sites.clone(),
            states: self.states.clone(),
        }
    }

    pub fn restart(&mut self) {
        self.states = open(&self.program, self.grid).expect("the sites opened once already");
    }

    /// Whether `carry` takes `held`: the same sites, a varying parameter's values aside.
    pub fn accepts(&self, held: &MachineState) -> bool {
        held.sites == self.program.sites
    }

    /// Takes `held`'s state whole where its sites are these; `false`, and nothing taken, else.
    pub fn carry(&mut self, held: &MachineState) -> bool {
        let taken = self.accepts(held);
        if taken {
            self.states.clone_from(&held.states);
        }
        taken
    }
}

impl Program {
    /// Up to `most` samples from `from`, and the slots run sample by sample: each reading its
    /// own past inside the block, and what reads one.
    fn block(&self, from: i64, most: usize, recurrent: &mut Vec<bool>) -> usize {
        let reach: Vec<Option<usize>> = self
            .ops
            .iter()
            .map(|op| own_reach(op, from, most))
            .collect();
        let len = reach
            .iter()
            .flatten()
            .filter(|n| **n >= RUN)
            .fold(most, |len, n| len.min(*n))
            .max(1);
        recurrent.clear();
        for (slot, reach) in reach.iter().enumerate() {
            let reads = self.args[slot].iter().any(|a| recurrent[*a]);
            recurrent.push(reads || reach.is_some_and(|n| n < len));
        }
        len
    }
}

/// How long a block from `from` may run with an own-past read reading only before it.
fn own_reach(op: &Op, from: i64, most: usize) -> Option<usize> {
    match op {
        Op::Read {
            slot: Slot::Own,
            at,
        }
        | Op::ReadScaled {
            slot: Slot::Own,
            at,
            ..
        } => {
            let last = |len: usize| from + len as i64 - 1;
            let mut len = most;
            while len > 1 && at.at(from).max(at.at(last(len))) >= from {
                len /= 2;
            }
            Some(match at.at(from).max(at.at(last(len))) < from {
                true => len,
                false => 0,
            })
        }
        Op::Indexed {
            slot: Slot::Own,
            reach,
            ..
        } => Some(match reach {
            Some((_, most)) if *most < 0 => {
                usize::try_from(most.unsigned_abs()).unwrap_or(usize::MAX)
            }
            _ => 0,
        }),
        _ => None,
    }
}

/// The fewest samples a block an own-past read shortens runs; a nearer read runs per sample.
const RUN: usize = 16;

/// Passes over a block, ops run over part of one, and samples read one by one, per thread.
#[cfg(test)]
mod counts {
    use std::cell::Cell;

    thread_local! {
        pub(super) static PASSES: Cell<u64> = const { Cell::new(0) };
        pub(super) static FILLS: Cell<u64> = const { Cell::new(0) };
        pub(super) static READS: Cell<u64> = const { Cell::new(0) };
    }
}

#[cfg(test)]
mod tests {
    use super::counts::{FILLS, PASSES, READS};
    use super::renderer::{BufId, Grid, Index, Map, NodeRenderer, Round, Slot, WIDE};
    use super::*;
    use crate::collapse::Extent;

    const LONG: [i64; 2] = [1489, 1721];

    /// A reverb's loop: two lines fed back through a mix, each damped by its last sample.
    fn feedback() -> NodeRenderer {
        let own = |lag: i64, k: usize| NodeRenderer::Channel {
            x: Box::new(NodeRenderer::Read {
                slot: Slot::Own,
                map: Map::shift(-lag),
            }),
            k,
        };
        let input = NodeRenderer::Read {
            slot: Slot::Read(BufId(0)),
            map: Map::shift(0),
        };
        let line = |k: usize| {
            let mix = LONG
                .iter()
                .enumerate()
                .map(|(j, lag)| NodeRenderer::Mul(vec![NodeRenderer::Const(0.6), own(*lag, j)]));
            let fed = NodeRenderer::Add(std::iter::once(input.clone()).chain(mix).collect());
            NodeRenderer::Add(vec![
                NodeRenderer::Mul(vec![NodeRenderer::Const(0.7), fed]),
                NodeRenderer::Mul(vec![NodeRenderer::Const(0.2), own(1, k)]),
            ])
        };
        NodeRenderer::Join(vec![line(0), line(1)])
    }

    fn ran(to: i64, step: i64) -> (Vec<Vec<f64>>, [u64; 3]) {
        let layout = Layout {
            grid: Grid::of(48_000),
            width: 2,
            read_widths: vec![1],
            sites: Vec::new(),
        };
        let spanned =
            Spanned::new(&feedback(), &layout, (0, to), &[Extent::EVERYWHERE]).expect("a program");
        let mut input = Tape::new(1, to as usize, 0);
        (0..to).for_each(|n| input.push(0, f64::from(u8::from(n % 4800 == 0))));
        let mut out = Tape::new(2, to as usize, 0);
        let mut machine = Machine::over(&spanned, 0).expect("a machine");
        let counted = || [&PASSES, &FILLS, &READS].map(|c| c.with(std::cell::Cell::get));
        let before = counted();
        let mut at = 0;
        while at < to {
            at = (at + step).min(to);
            machine
                .run_to(at, &[input.window()], &mut out)
                .expect("samples");
        }
        let after = counted();
        (out.into_planes(), [0, 1, 2].map(|k| after[k] - before[k]))
    }

    /// A pass per block, the far reads copied whole and only the damping run sample by sample,
    /// writing what a sample-at-a-time run does.
    #[test]
    fn a_loop_reading_itself_one_sample_back_runs_a_pass_per_block() {
        let to = 24_000;
        let (whole, [passes, fills, reads]) = ran(to, to);
        let (single, [one_by_one, ..]) = ran(to, 1);
        assert_eq!(whole, single);
        assert_eq!(one_by_one, to as u64);
        let blocks = (to as u64).div_ceil(BLOCK as u64);
        assert!(passes <= blocks + 1, "{passes} passes over {blocks} blocks");
        let per_sample = 2 * 4 + 1;
        let ops = feedback().ops(&Layout {
            grid: Grid::of(48_000),
            width: 2,
            read_widths: vec![1],
            sites: Vec::new(),
        });
        let ops = ops.expect("ops") as u64;
        assert!(
            fills <= passes * ops + per_sample * to as u64,
            "{fills} op runs"
        );
        let far = 2 * 2 * LONG.iter().sum::<i64>() as u64;
        assert!(reads <= 2 * 2 * to as u64 + far, "{reads} reads");
    }

    /// Each line reads both long lines and its own last sample, and both mix the same input:
    /// every identical read and mix runs once, writing the bits the loop's own arithmetic does.
    #[test]
    fn identical_reads_of_a_loops_own_past_run_once() {
        let to = 24_000;
        let layout = Layout {
            grid: Grid::of(48_000),
            width: 2,
            read_widths: vec![1],
            sites: Vec::new(),
        };
        assert_eq!(feedback().ops(&layout).expect("ops"), 20);
        let (planes, [_, fills, reads]) = ran(to, to);
        let mut y = vec![vec![0.0f64; to as usize]; 2];
        let past = |y: &[Vec<f64>], k: usize, n: i64| match n {
            n if n < 0 => 0.0,
            n => y[k][n as usize],
        };
        for n in 0..to {
            let input = f64::from(u8::from(n % 4800 == 0));
            let mut fed = 0.0 + input;
            for (j, lag) in LONG.iter().enumerate() {
                fed += 1.0 * 0.6 * past(&y, j, n - lag);
            }
            for k in 0..2 {
                y[k][n as usize] = 0.0 + 1.0 * 0.7 * fed + 1.0 * 0.2 * past(&y, k, n - 1);
            }
        }
        let bits = |p: &[Vec<f64>]| -> Vec<Vec<u64>> {
            p.iter()
                .map(|c| c.iter().map(|v| v.to_bits()).collect())
                .collect()
        };
        assert_eq!(bits(&planes), bits(&y));
        let blocks = (to as u64).div_ceil(BLOCK as u64) + 1;
        let (last, channels, scaled, sums, join) = (1, 2, 2, 2, 1);
        let per_sample = last + channels + scaled + sums + join;
        assert!(
            fills <= blocks * 20 + per_sample * to as u64,
            "{fills} op runs"
        );
        let far = 2 * 2 * LONG.iter().sum::<i64>() as u64;
        assert!(reads <= 2 * to as u64 + far, "{reads} reads");
    }

    /// A delay read at a moving length and a loop reading itself far back, by index.
    fn delayed() -> NodeRenderer {
        let now = Index::At(Map::whole(1, 0));
        let length = NodeRenderer::Add(vec![
            NodeRenderer::Const(0.002),
            NodeRenderer::Mul(vec![
                NodeRenderer::Const(0.000_2),
                NodeRenderer::Map(
                    super::renderer::Unary::Sin,
                    Box::new(NodeRenderer::Mul(vec![
                        NodeRenderer::Const(18.85),
                        NodeRenderer::Time,
                    ])),
                ),
            ]),
        ]);
        let back = Index::Neg(Box::new(Index::Step(Box::new(length), Round::Floor)));
        let echo = Index::Add(vec![now.clone(), Index::At(Map::whole(0, -1489))]);
        NodeRenderer::Add(vec![
            NodeRenderer::Indexed {
                slot: Slot::Read(BufId(0)),
                index: Index::Add(vec![now, back]),
                reach: None,
            },
            NodeRenderer::Mul(vec![
                NodeRenderer::Const(0.5),
                NodeRenderer::Indexed {
                    slot: Slot::Own,
                    index: echo,
                    reach: Some((-1489, -1489)),
                },
            ]),
        ])
    }

    /// Every index a sample reads, its delay's step included, is rounded in doubles or `i64`:
    /// none in 128-bit integers.
    #[test]
    fn a_delay_and_an_echo_round_no_index_in_wide_integers() {
        let to = 9_600;
        let layout = Layout {
            grid: Grid::of(48_000),
            width: 1,
            read_widths: vec![1],
            sites: Vec::new(),
        };
        let spanned =
            Spanned::new(&delayed(), &layout, (0, to), &[Extent::EVERYWHERE]).expect("a program");
        let mut input = Tape::new(1, to as usize, 0);
        (0..to).for_each(|n| input.push(0, (n as f64 * 0.01).sin()));
        let mut out = Tape::new(1, to as usize, 0);
        let mut machine = Machine::over(&spanned, 0).expect("a machine");
        let before = WIDE.with(std::cell::Cell::get);
        machine
            .run_to(to, &[input.window()], &mut out)
            .expect("samples");
        assert_eq!(WIDE.with(std::cell::Cell::get), before);
    }
}