sva-engine 0.7.9

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
// Concern: one value of the table and the samples it holds, merged, cut and viewed on its own clock | Non-concern: computing them (eval.rs), what is asked (demand.rs) | IO: (segment) -> samples

use sva_formula::{Hash, NodeId, SpectralSum};
use sva_samples::machine::ops::Layout;
use sva_samples::{
    Buffer, Extent, Frames, Grid, Label, Machine, NodeRenderer, Rows, Spanned, Tape, Window,
};

use std::sync::Arc;

use super::program::Source;
use super::segments::Segments;
use crate::error::{Diagnostic, EngineError, Located};

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct Key {
    pub(crate) identity: Hash,
    pub(crate) step: (i128, i128),
}

pub(crate) enum Kind {
    Rows(Arc<Rows>),
    Program(Box<Program>),
    Frames {
        window: usize,
        hop: usize,
    },
    Istft,
    Spectrum(Arc<SpectralSum>),
    /// Stored samples, loaded as asked; a value it reads computes what they miss.
    Stored(Arc<crate::cache::Stored>),
}

impl Kind {
    /// The same, run nowhere yet.
    pub(crate) fn unrun(&self) -> Kind {
        match self {
            Kind::Rows(rows) => Kind::Rows(Arc::clone(rows)),
            Kind::Program(program) => Kind::Program(Box::new(Program {
                renderer: Arc::clone(&program.renderer),
                spanned: Arc::clone(&program.spanned),
                layout: Arc::clone(&program.layout),
                machine: None,
                marks: Default::default(),
                ..**program
            })),
            Kind::Frames { window, hop } => Kind::Frames {
                window: *window,
                hop: *hop,
            },
            Kind::Istft => Kind::Istft,
            Kind::Spectrum(sum) => Kind::Spectrum(Arc::clone(sum)),
            Kind::Stored(stored) => Kind::Stored(Arc::clone(stored)),
        }
    }
}

/// What a value was built as, before any run: what each read reads, and its label and support.
pub(crate) struct Made {
    pub(crate) sources: Vec<Source>,
    pub(crate) label: Option<Label>,
    pub(crate) support: Extent,
}

impl Made {
    pub(crate) fn of(sources: Vec<Source>, label: Option<Label>, support: Extent) -> Arc<Made> {
        Arc::new(Made {
            sources,
            label,
            support,
        })
    }
}

pub(crate) struct Program {
    pub(crate) renderer: Arc<NodeRenderer>,
    pub(crate) spanned: Arc<Spanned>,
    pub(crate) layout: Arc<Layout>,
    /// Where a stateful value's run starts.
    pub(crate) start: Option<i64>,
    pub(crate) own: i64,
    /// A program that only reads one value at a whole-sample shift is that value moved: the
    /// slot and the shift, and it computes and holds nothing of its own.
    pub(crate) alias: Option<(usize, i64)>,
    pub(crate) machine: Option<Machine>,
    /// Its state where its run passes each of these, kept for the store.
    pub(crate) marks: std::collections::BTreeMap<i64, sva_samples::MachineState>,
}

impl Program {
    pub(crate) fn stateful(&self) -> bool {
        self.start.is_some()
    }
}

pub(crate) enum Held {
    Segments(Vec<Buffer>),
    Run(Tape),
    Frames(Option<Box<Frames>>),
}

pub(crate) struct Value {
    pub(crate) key: Key,
    pub(crate) node: Option<NodeId>,
    pub(crate) name: String,
    pub(crate) grid: Grid,
    pub(crate) width: usize,
    pub(crate) support: Extent,
    pub(crate) period: Option<i64>,
    pub(crate) kind: Kind,
    pub(crate) reads: Vec<usize>,
    pub(crate) held: Held,
    /// Every segment computed, in order.
    pub(crate) evaluated: Vec<Extent>,
    pub(crate) label: Option<Label>,
    /// Where a stateful value's arguments switch, and its identity before each.
    pub(crate) switches: Vec<(i64, Hash)>,
    pub(crate) moved: f64,
    /// Its samples are its identity's alone; one an edit carried on, or anything reading one,
    /// holds a history no key names, so the store neither answers nor keeps it.
    pub(crate) pure: bool,
    pub(crate) made: Arc<Made>,
}

impl Value {
    /// The value it reads and the shift it reads it at, where it only moves that value.
    pub(crate) fn alias(&self) -> Option<(usize, i64)> {
        match &self.kind {
            Kind::Program(program) => program.alias.map(|(slot, by)| (self.reads[slot], by)),
            _ => None,
        }
    }

    /// The value it only moves and by how much: an alias, or a crop with no fade of a read,
    /// whose window holds all it may be nonzero over.
    pub(crate) fn moves(&self) -> Option<(usize, i64)> {
        let Kind::Program(program) = &self.kind else {
            return None;
        };
        if let Some(moved) = self.alias() {
            return Some(moved);
        }
        let NodeRenderer::Crop {
            x,
            window,
            rise,
            fall,
            ..
        } = &*program.renderer
        else {
            return None;
        };
        let NodeRenderer::Read {
            slot: sva_samples::Slot::Read(slot),
            map,
        } = x.as_ref()
        else {
            return None;
        };
        let held = Extent::new(window.0, window.1);
        let bare = *rise <= 0.0 && *fall <= 0.0 && map.a == 1 && map.d == 1;
        let within = held.intersect(self.support) == self.support;
        (bare && within && program.start.is_none())
            .then(|| (self.reads[slot.0 as usize], map.at(0)))
    }

    /// A stateful value with nothing of its past computed: silent before `now`, stepping from
    /// there.
    pub(crate) fn silent_from(&mut self, now: i64) -> Result<(), sva_samples::SampleError> {
        let Kind::Program(program) = &mut self.kind else {
            unreachable!("a stateful value is a program");
        };
        program.machine = Some(Machine::over(&program.spanned, now)?);
        program.marks.clear();
        self.held = Held::Run(Tape::new(self.width, 0, now));
        self.support = self.support.intersect(Extent::from(now));
        self.pure = false;
        Ok(())
    }

    pub(crate) fn covers(&self) -> Segments {
        let mut out = Segments::default();
        if let Kind::Stored(stored) = &self.kind {
            for e in stored.extents() {
                out.add(e.intersect(self.support));
            }
        }
        out
    }

    pub(crate) fn holding(&self) -> Segments {
        let mut out = Segments::default();
        match &self.held {
            Held::Segments(parts) => parts.iter().for_each(|b| out.add(b.extent())),
            Held::Run(tape) => out.add(Extent::new(tape.base(), tape.end())),
            Held::Frames(Some(_)) => out.add(self.support),
            Held::Frames(None) => {}
        }
        out
    }

    pub(crate) fn end(&self) -> Option<i64> {
        match (&self.kind, &self.held) {
            (Kind::Program(program), Held::Run(tape)) if program.machine.is_some() => {
                Some(tape.end())
            }
            _ => None,
        }
    }

    pub(crate) fn bytes(&self) -> usize {
        let planes = |b: &Buffer| b.len() * b.width * size_of::<f64>();
        let held = match &self.held {
            Held::Segments(parts) => parts.iter().map(planes).sum(),
            Held::Run(tape) => tape.capacity() * tape.width() * size_of::<f64>(),
            Held::Frames(Some(frames)) => {
                frames.width * frames.frames * frames.bins * 2 * size_of::<f64>()
            }
            Held::Frames(None) => 0,
        };
        let state = match &self.kind {
            Kind::Program(program) => program.machine.as_ref().map_or(0, Machine::bytes),
            _ => 0,
        };
        held + state
    }

    /// One held segment where one meets `over`, else those it meets laid into one.
    pub(crate) fn window(&self, over: Extent) -> std::borrow::Cow<'_, Buffer> {
        let over = match self.period {
            Some(n) => Extent::new(0, n),
            None => over,
        };
        match &self.held {
            Held::Run(tape) => std::borrow::Cow::Owned(tape.clone().into_buffer(self.grid.rate)),
            Held::Segments(parts) => {
                let meets: Vec<&Buffer> = parts
                    .iter()
                    .filter(|b| !b.extent().intersect(over).is_empty())
                    .collect();
                match meets.as_slice() {
                    [one] => std::borrow::Cow::Borrowed(*one),
                    _ => std::borrow::Cow::Owned(laid(&meets, self.width, self.grid.rate)),
                }
            }
            Held::Frames(_) => {
                std::borrow::Cow::Owned(Buffer::silence(self.grid.rate, self.width, 0))
            }
        }
    }

    pub(crate) fn samples(&self, over: Extent) -> Buffer {
        let mut planes = vec![vec![0.0; over.len()]; self.width.max(1)];
        let mut lay = |window: Window| {
            for (c, plane) in planes.iter_mut().enumerate() {
                for (i, n) in (over.start..over.end).enumerate() {
                    if let Some(v) = window.get(c, n) {
                        plane[i] = v;
                    }
                }
            }
        };
        match &self.held {
            Held::Run(tape) => lay(tape.within(self.support)),
            Held::Segments(parts) => {
                for part in parts {
                    lay(Window::of(part, self.support).folded(self.period));
                }
            }
            Held::Frames(_) => {}
        }
        let mut out = Buffer::of_planes(self.grid.rate, planes);
        out.start = over.start;
        out
    }

    /// A run keeps everything from `kept`'s first sample on.
    pub(crate) fn retain(&mut self, kept: &Segments) {
        match &mut self.held {
            Held::Segments(parts) => {
                let mut out = Vec::new();
                for part in parts.drain(..) {
                    for e in kept.intersect(part.extent()).iter() {
                        out.push(match e == part.extent() {
                            true => part.clone(),
                            false => part.over(e, part.extent()),
                        });
                    }
                }
                *parts = out;
            }
            Held::Run(tape) => {
                let from = kept.iter().next().map_or(tape.end(), |first| first.start);
                tape.forget_before(from);
                if tape.base() == tape.end() {
                    *tape = Tape::new(tape.width(), 0, tape.end());
                }
            }
            Held::Frames(frames) => {
                if kept.is_empty() {
                    *frames = None;
                }
            }
        }
    }

    /// A segment continuing the one before it grows that one in place, amortised O(1) a
    /// sample, as a value computed block by block is; any other overlap is laid anew.
    pub(crate) fn hold(&mut self, buffer: Buffer) {
        let Held::Segments(parts) = &mut self.held else {
            unreachable!("only a value with no state holds segments");
        };
        let at = buffer.extent();
        let first = parts.partition_point(|b| b.extent().end < at.start);
        let last = parts.partition_point(|b| b.extent().start <= at.end);
        match &mut parts[first..last] {
            [] => parts.insert(first, buffer),
            [held] if held.extent().end == at.start && held.width == buffer.width => {
                for (plane, more) in held.planes.iter_mut().zip(&buffer.planes) {
                    plane.extend_from_slice(more);
                }
            }
            touching => {
                let mut merged: Vec<&Buffer> = touching.iter().collect();
                merged.push(&buffer);
                let joined = laid(&merged, self.width, self.grid.rate);
                parts.splice(first..last, [joined]);
            }
        }
    }
}

fn laid(parts: &[&Buffer], width: usize, rate: u32) -> Buffer {
    let hull = parts
        .iter()
        .fold(Extent::NOWHERE, |held, b| held.hull(b.extent()));
    let mut planes = vec![vec![0.0; hull.len()]; width.max(1)];
    for part in parts {
        let at = (part.start - hull.start) as usize;
        for (c, plane) in planes.iter_mut().enumerate() {
            let held = part.plane(c.min(part.width.saturating_sub(1)));
            plane[at..at + held.len()].copy_from_slice(held);
        }
    }
    let mut out = Buffer::of_planes(rate, planes);
    out.start = hull.start;
    out
}

pub(crate) fn finite<'a>(
    name: &str,
    mut samples: impl Iterator<Item = &'a f64>,
) -> Result<(), EngineError> {
    match samples.all(|v| v.is_finite()) {
        true => Ok(()),
        false => Err(EngineError::refused(Diagnostic {
            code: "collapse.not_finite".to_string(),
            message: format!("`{name}` passes the largest double where it is read"),
            location: Located::at(name, None),
            help: "crop a decay at its onset, so it is read only where it falls".to_string(),
        })),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn segments(width: usize) -> Value {
        Value {
            key: Key {
                identity: Hash(0, 0),
                step: (1, 1),
            },
            node: None,
            name: "stream".to_string(),
            grid: Grid::of(8_000),
            width,
            support: Extent::EVERYWHERE,
            period: None,
            kind: Kind::Istft,
            reads: Vec::new(),
            held: Held::Segments(Vec::new()),
            evaluated: Vec::new(),
            label: None,
            switches: Vec::new(),
            moved: 0.0,
            pure: true,
            made: Made::of(Vec::new(), None, Extent::EVERYWHERE),
        }
    }

    fn block(start: i64, len: usize, width: usize) -> Buffer {
        let mut b = Buffer::of_planes(8_000, vec![vec![start as f64; len]; width]);
        b.start = start;
        b
    }

    /// `hold`'s cost contract, which a render streamed as its own target lives on: held block
    /// by block, the samples already held move a logarithmic number of times, so each sample
    /// costs amortised O(1). Where they move is the one observable of a copy; any geometric
    /// growth passes, and relaying the segment each block fails.
    #[test]
    fn a_value_held_block_by_block_grows_in_place() {
        let (blocks, len) = (1024usize, 64usize);
        let mut value = segments(2);
        let mut moves = 0;
        let mut last = std::ptr::null();
        for k in 0..blocks {
            value.hold(block((k * len) as i64, len, 2));
            let Held::Segments(parts) = &value.held else {
                unreachable!("a value of segments");
            };
            assert_eq!(parts.len(), 1, "one run of contiguous samples");
            let at = parts[0].plane(0).as_ptr();
            moves += usize::from(at != last);
            last = at;
        }
        assert!(
            moves <= 2 * blocks.ilog2() as usize,
            "{moves} moves over {blocks} blocks"
        );
        let Held::Segments(parts) = &value.held else {
            unreachable!("a value of segments");
        };
        assert_eq!(parts[0].extent(), Extent::new(0, (blocks * len) as i64));
        assert_eq!(parts[0].plane(1)[len * 5], (len * 5) as f64);
    }
}