sva-engine 0.3.2

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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Concern: states what a cache key is made of, so a warm render answers a cold one | Non-concern: a store's medium or budget (stores.rs) | IO: (a composition, twice) -> the same bytes

mod fixtures;

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};

use fixtures::dir_of;
use sva_engine::{
    Ask, Cache, DiskCache, Expected, MemoryCache, Payload, PayloadKind, Render, RenderConfig,
    Representation, render, symbolic_key,
};
use sva_formula::hash::hash_closed_form_under;
use sva_formula::{ClosedForm, Origin, Var};

const SECONDS: f64 = 0.05;
const RATE: u32 = 8_000;

fn store_all(name: &str) -> DiskCache {
    DiskCache::at(dir_of(name, &[])).storing_everything()
}

fn rendered(dir: &Path, root: &str, cache: Option<&dyn Cache>) -> Render {
    let graph = sva_ast::parse_composition(dir).expect("a composition that parses");
    render(&graph, root, RenderConfig::seconds(RATE, SECONDS), cache)
        .unwrap_or_else(|e| panic!("rendering `{root}`: {e}"))
}

/// Every node held as samples, which is what a reading that consumes buffers asks for.
fn all_of(dir: &Path, root: &str, nodes: &[&str], cache: Option<&dyn Cache>) -> Render {
    let graph = sva_ast::parse_composition(dir).expect("a composition that parses");
    let asks = nodes
        .iter()
        .map(|node| Ask {
            node: (*node).to_string(),
            representation: Representation::Samples,
        })
        .collect();
    render(
        &graph,
        root,
        RenderConfig::seconds(RATE, SECONDS).asking(asks),
        cache,
    )
    .unwrap_or_else(|e| panic!("rendering `{root}`: {e}"))
}

fn samples(r: &Render) -> BTreeMap<String, Vec<f64>> {
    r.buffers
        .iter()
        .map(|(id, b)| (r.tys.name(*id).to_string(), b.plane(0).to_vec()))
        .collect()
}

fn entries(cache: &DiskCache) -> usize {
    let mut count = 0;
    let mut work = vec![cache.dir().expect("a disk store").to_path_buf()];
    while let Some(at) = work.pop() {
        for entry in fs::read_dir(at).into_iter().flatten().flatten() {
            match entry.file_type() {
                Ok(kind) if kind.is_dir() => work.push(entry.path()),
                _ => count += 1,
            }
        }
    }
    count
}

fn write(dir: &Path, rel: &str, text: &str) {
    fs::write(dir.join(rel), text).expect("a node file");
}

/// One composition of a chord, a voicing over it and a master that sums both.
fn chain(name: &str) -> PathBuf {
    dir_of(
        name,
        &[
            ("chord", "sin(2*pi*256*t) + sin(2*pi*384*t)\n"),
            ("voiced", "@chord*0.5\n"),
            ("master", "@voiced + @chord*0.25\n"),
        ],
    )
}

#[test]
fn a_warm_render_is_byte_identical_to_a_cold_one() {
    let dir = chain("warm-cold");
    let cache = store_all("warm-cold-store");
    let cold = samples(&rendered(&dir, "master", Some(&cache)));
    assert!(entries(&cache) > 0, "the cold render filled the store");
    let warm = samples(&rendered(&dir, "master", Some(&cache)));
    assert_eq!(cold, warm);
}

#[test]
fn a_render_warmed_in_memory_is_byte_identical_to_one_warmed_on_disk() {
    let dir = chain("two-stores");
    let disk = store_all("two-stores-disk");
    let memory = MemoryCache::new();
    rendered(&dir, "master", Some(&disk));
    rendered(&dir, "master", Some(&memory));
    assert_eq!(
        samples(&rendered(&dir, "master", Some(&disk))),
        samples(&rendered(&dir, "master", Some(&memory)))
    );
}

/// A key is content, so the path a ref was written down does not reach it.
#[test]
fn two_spellings_of_one_ref_path_share_a_cache_entry() {
    let dir = dir_of(
        "spellings",
        &[
            ("chord", "sin(2*pi*256*t)\n"),
            ("plain", "@chord*0.5\n"),
            ("fx/walked", "@../chord*0.5\n"),
            ("master", "@plain + @fx/walked\n"),
        ],
    );
    let cache = store_all("spellings-store");
    let held = all_of(&dir, "master", &["plain", "fx/walked"], Some(&cache));
    let one = held.id("plain").expect("plain");
    let two = held.id("fx/walked").expect("the walked spelling");
    assert_eq!(held.buffer(one), held.buffer(two), "one value, one entry");
}

/// A hit carries the label the collapse wrote, so a reused node still says how exact it is.
#[test]
fn a_reused_node_still_reports_its_label() {
    let dir = chain("labelled");
    let cache = store_all("labelled-store");
    let cold = rendered(&dir, "master", Some(&cache));
    let warm = rendered(&dir, "master", Some(&cache));
    let id = warm.id("master").expect("the root");
    assert_eq!(
        warm.labels[&id].profile,
        cold.labels[&cold.id("master").expect("the root")].profile
    );
    assert_eq!(warm.labels[&id].rate, RATE);
}

#[test]
fn editing_one_file_re_renders_it_and_its_dependents_and_nothing_else() {
    let dir = chain("edited");
    let cache = store_all("edited-store");
    let nodes = ["chord", "voiced", "master"];
    let before = samples(&all_of(&dir, "master", &nodes, Some(&cache)));
    write(&dir, "voiced", "@chord*0.75\n");
    let after = samples(&all_of(&dir, "master", &nodes, Some(&cache)));
    assert_eq!(
        before["chord"], after["chord"],
        "the untouched law is one value"
    );
    assert_ne!(before["voiced"], after["voiced"]);
    assert_ne!(before["master"], after["master"]);
}

#[test]
fn renaming_a_file_invalidates_nothing() {
    let dir = chain("renamed");
    let cache = store_all("renamed-store");
    let before = samples(&all_of(&dir, "master", &["voiced", "master"], Some(&cache)));
    fs::rename(dir.join("voiced"), dir.join("coloured")).expect("a rename");
    write(&dir, "master", "@coloured + @chord*0.25\n");
    let after = samples(&all_of(
        &dir,
        "master",
        &["coloured", "master"],
        Some(&cache),
    ));
    assert_eq!(before["master"], after["master"], "one value, one key");
    assert_eq!(before["voiced"], after["coloured"]);
}

/// Addition commutes and subtraction does not, and the spectral sum is what says so.
#[test]
fn reordering_a_sum_invalidates_nothing_but_reordering_a_difference_does() {
    let of = |name: &str, body: &str| {
        let dir = dir_of(name, &[("master", body)]);
        samples(&rendered(&dir, "master", None))["master"].clone()
    };
    let sum = of("sum", "sin(2*pi*256*t) + sin(2*pi*384*t)\n");
    let swapped = of("sum-swapped", "sin(2*pi*384*t) + sin(2*pi*256*t)\n");
    assert_eq!(sum, swapped);

    let difference = of("difference", "sin(2*pi*256*t) - sin(2*pi*384*t)\n");
    let reversed = of("difference-reversed", "sin(2*pi*384*t) - sin(2*pi*256*t)\n");
    assert_ne!(difference, reversed);
}

/// Rate keys a collapse and nothing above it: the closed form behind two rates is one entry.
#[test]
fn a_different_sample_rate_shares_the_law_and_not_the_buffer() {
    let dir = chain("rates");
    let cache = store_all("rates-store");
    let graph = sva_ast::parse_composition(&dir).expect("a composition");
    let first = render(
        &graph,
        "master",
        RenderConfig::seconds(RATE, SECONDS),
        Some(&cache),
    )
    .expect("a render");
    let held = entries(&cache);
    let second = render(
        &graph,
        "master",
        RenderConfig::seconds(RATE * 2, SECONDS),
        Some(&cache),
    )
    .expect("a render at another rate");
    assert!(entries(&cache) > held, "another rate is another buffer");
    let one = first
        .buffer(first.id("master").expect("the root"))
        .expect("a buffer");
    let two = second
        .buffer(second.id("master").expect("the root"))
        .expect("a buffer");
    assert_eq!(two.len(), one.len() * 2);
}

#[test]
fn structurally_identical_subtrees_share_one_entry() {
    let dir = dir_of(
        "identical",
        &[
            ("left", "sin(2*pi*256*t)*0.5\n"),
            ("right", "sin(2*pi*256*t)*0.5\n"),
            ("master", "@left + @right\n"),
        ],
    );
    let cache = store_all("identical-store");
    let held = all_of(&dir, "master", &["left", "right"], Some(&cache));
    assert_eq!(
        held.buffer(held.id("left").expect("left")),
        held.buffer(held.id("right").expect("right")),
        "one written value, one entry"
    );
}

#[test]
fn a_warm_cyclic_render_is_byte_identical_to_a_cold_one() {
    let dir = dir_of(
        "cyclic",
        &[("master", "sin(2*pi*220*t) + 0.5*self(t - 0.01s)\n")],
    );
    let cache = store_all("cyclic-store");
    let cold = samples(&rendered(&dir, "master", Some(&cache)));
    let warm = samples(&rendered(&dir, "master", Some(&cache)));
    assert_eq!(cold, warm);
}

/// A loop's own gain is part of the value, so editing it retires the whole loop's entry.
#[test]
fn editing_a_loop_retires_it() {
    let dir = dir_of(
        "loop-edit",
        &[("master", "sin(2*pi*220*t) + 0.5*self(t - 0.01s)\n")],
    );
    let cache = store_all("loop-edit-store");
    let before = samples(&rendered(&dir, "master", Some(&cache)));
    write(&dir, "master", "sin(2*pi*220*t) + 0.25*self(t - 0.01s)\n");
    let after = samples(&rendered(&dir, "master", Some(&cache)));
    assert_ne!(before["master"], after["master"], "the loop retires");
}

/// A loop of refs closes through no `self`, so nothing substitutes it: it says so rather
/// than unrolling forever.
#[test]
fn a_loop_of_refs_refuses_rather_than_substituting_forever() {
    let dir = dir_of(
        "ref-loop",
        &[
            ("a", "sin(2*pi*220*t) + 0.5*@b(t - 0.01s)\n"),
            ("b", "@a(t - 0.01s)*0.5\n"),
            ("master", "@a\n"),
        ],
    );
    let graph = sva_ast::parse_composition(&dir).expect("a composition");
    let refused = render(&graph, "master", RenderConfig::seconds(RATE, SECONDS), None);
    let Err(refused) = refused else {
        panic!("a loop of refs has no law");
    };
    assert_eq!(refused.code(), "engine.cyclic_substitution");
}

/// A spectral sum is rate-free, so the second rate finds the closed form already normalized.
#[test]
fn a_symbolic_result_is_reused_across_two_rates() {
    let dir = chain("symbolic-rates");
    let graph = sva_ast::parse_composition(&dir).expect("a composition");
    let cache = MemoryCache::new();
    let first = render(
        &graph,
        "master",
        RenderConfig::seconds(RATE, SECONDS),
        Some(&cache),
    )
    .expect("a render");
    let id = first.id("master").expect("the root");
    cache.sweep();
    let held = cache.held_bytes();
    let symbolic = first
        .symbolic
        .get(&id)
        .expect("the root's spectral sum")
        .clone();
    let second = render(
        &graph,
        "master",
        RenderConfig::seconds(RATE * 3, SECONDS),
        Some(&cache),
    )
    .expect("a render at another rate");
    assert_eq!(
        second.symbolic[&second.id("master").expect("the root")],
        symbolic,
        "one spectral sum answers both rates"
    );
    assert!(held > 0, "the first render kept something");
}

/// The table version is already inside a term's own hash, so a bump moves every symbolic
/// key at once and the old entries are simply never asked for again.
#[test]
fn a_table_version_bump_retires_symbolic_entries() {
    let form = ClosedForm {
        var: Var::T,
        body: sva_formula::Body::Line,
        origin: Origin::UNKNOWN,
    };
    let now = symbolic_key(hash_closed_form_under(&form, sva_formula::TABLE_VERSION));
    let later = symbolic_key(hash_closed_form_under(
        &form,
        sva_formula::TABLE_VERSION + 1,
    ));
    assert_ne!(now, later, "a bump is a new key for every law");

    let cache = MemoryCache::new();
    let payload = Payload::Symbolic(Box::new(
        sva_formula::normalize_closed_form(&form).expect("a spectral sum"),
    ));
    assert!(cache.worth_storing(
        std::time::Duration::from_millis(2),
        payload.bytes(),
        PayloadKind::Symbolic
    ));
    cache.store(now, &payload, &[], None);
    assert!(cache.load(now, "n", Expected::Symbolic).is_some());
    assert!(
        cache.load(later, "n", Expected::Symbolic).is_none(),
        "nothing answers the bumped key"
    );
}

/// One analysis of one buffer is one entry, and the buffer's own content is in its key:
/// editing what was analysed retires it.
#[test]
fn frames_are_held_under_the_window_they_were_read_through() {
    let dir = dir_of(
        "frames",
        &[
            ("chord", "sin(2*pi*256*t)\n"),
            (
                "master",
                "istft(stft(sample(@chord), window=256, hop=64))\n",
            ),
        ],
    );
    let cache = MemoryCache::new();
    let cold = samples(&rendered(&dir, "master", Some(&cache)));
    cache.sweep();
    assert!(cache.held_bytes() > 0, "the analysis was kept");
    let warm = samples(&rendered(&dir, "master", Some(&cache)));
    assert_eq!(cold, warm);

    write(&dir, "chord", "sin(2*pi*512*t)\n");
    let edited = samples(&rendered(&dir, "master", Some(&cache)));
    assert_ne!(cold, edited, "editing what was analysed retires the frames");
}

/// A spectral sum is a formula tree with no encoding on disk, so the disk store says so
/// rather than accepting one and dropping it.
#[test]
fn a_disk_store_offers_no_room_for_a_spectral_sum() {
    let cache = store_all("no-room");
    for kind in [PayloadKind::Symbolic, PayloadKind::Frames] {
        assert!(
            !cache.worth_storing(std::time::Duration::from_secs(1), 8, kind),
            "{kind:?} has no file"
        );
    }
    assert!(cache.worth_storing(std::time::Duration::from_secs(1), 8, PayloadKind::Samples));
}

/// FORMAT 9.3: the label is part of the value a collapse produced, so it is stored with the
/// buffer and answered on a hit. Warm and cold are one command answering one way.
#[test]
fn a_warm_hit_carries_the_cold_label() {
    let dir = dir_of(
        "warm-label",
        &[("chord", "sin(2*pi*256*t) + sin(2*pi*384*t)\n")],
    );
    let label_of = |cache: &dyn Cache| {
        let held = rendered(&dir, "chord", Some(cache));
        held.labels
            .get(&held.root)
            .expect("the root collapsed")
            .clone()
    };

    for cache in [
        Box::new(store_all("warm-label-disk")) as Box<dyn Cache>,
        Box::new(MemoryCache::holding(1 << 20)),
    ] {
        let cold = label_of(cache.as_ref());
        assert_eq!(
            cold.source,
            sva_engine::Source::Exact,
            "the cold run is exact"
        );
        let sva_engine::Detail::Lines { placed, summed, .. } = &cold.detail else {
            panic!("a line spectrum states what it placed: {cold:?}");
        };
        assert!(placed + summed > 0, "the lines are counted");

        let warm = label_of(cache.as_ref());
        assert_eq!(
            warm.detail, cold.detail,
            "a hit answers the row the cold run took"
        );
        assert_eq!(warm.source, cold.source);
        assert_eq!(warm.rate, cold.rate);
        assert_eq!(warm.profile, cold.profile);
    }
}

/// A sampled node is the dearest kind the engine runs and the one a caller most wants back
/// from the store, so the cache contract has to be proven over one, not only over collapses.
#[test]
fn a_sampled_node_is_stored_and_answered_from_the_store() {
    let dir = dir_of(
        "sampled-cache",
        &[
            ("tone", "sin(2*pi*220*t)\n"),
            ("acc", "self(t - 1sp)*0.5 + sample(@tone)\n"),
            ("master", "@acc*0.5\n"),
        ],
    );
    let cache = store_all("sampled-cache-store");
    let cold = all_of(&dir, "master", &["acc"], Some(&cache));
    assert_eq!(
        cold.tys.ty(cold.id("acc").expect("acc types")).held,
        sva_engine::Held::Sampled,
        "the node under test really is sampled"
    );
    let id = cold.id("acc").expect("acc types");
    let key = sva_engine::buffer_key(
        sva_engine::identity(&cold.tys, id).expect("acc has an identity"),
        RATE,
        0.0,
        cold.tys.ty(id).width as usize,
        sva_samples::AliasScore::NotAsked,
    );
    assert!(cache.holds(key), "the sampled node is in the store");

    let filled = entries(&cache);
    let warm = all_of(&dir, "master", &["acc"], Some(&cache));
    assert_eq!(samples(&cold), samples(&warm), "byte for byte");
    assert_eq!(entries(&cache), filled, "and nothing was written twice");
}