legato 0.0.12

Legato is a WIP audiograph and DSL for quickly developing audio applications
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
use indexmap::IndexMap;

use crate::dsl::ir::*;
use std::collections::HashMap;

/// Convert the ASTMacro to the IRMacro
fn convert_macro(
    name: &str,
    ast_map: &mut HashMap<String, AstMacro>,
    converted: &mut HashMap<String, IRMacro>,
) {
    // Check to avoid redundant macro conversion
    if converted.contains_key(name) {
        return;
    }

    let ast_macro = ast_map
        .remove(name)
        .unwrap_or_else(|| panic!("Macro '{}' not found", name));

    // Resolve dependencies first
    for scope in &ast_macro.declarations {
        for decl in &scope.declarations {
            if ast_map.contains_key(&decl.node_type) {
                convert_macro(&decl.node_type, ast_map, converted);
            }
        }
    }

    // Now build the body IRGraph for this macro
    let mut body = IRGraph::new();
    let mut local_alias_to_id: HashMap<String, NodeId> = HashMap::new();

    for scope in &ast_macro.declarations {
        for decl in &scope.declarations {
            let alias = decl.alias.clone().unwrap_or_else(|| decl.node_type.clone());

            // Classify as MacroRef if it names a known macro
            let kind = if converted.contains_key(&decl.node_type) {
                IRNodeKind::MacroRef
            } else {
                IRNodeKind::Leaf
            };

            let id = body.add_node(
                kind,
                scope.namespace.clone(),
                decl.node_type.clone(),
                alias.clone(),
                decl.params.clone().unwrap_or_default(),
                decl.pipes.clone(),
                decl.count,
            );
            local_alias_to_id.insert(alias, id);
        }
    }

    for conn in &ast_macro.connections {
        // This is handled below
        if ast_macro.virtual_ports_in.contains(&conn.source.node) {
            continue;
        }
        let src = local_alias_to_id[&conn.source.node];
        let snk = local_alias_to_id[&conn.sink.node];
        body.connect_multi(
            src,
            conn.source.node_selector.clone(),
            conn.source.port.clone(),
            snk,
            conn.sink.node_selector.clone(),
            conn.sink.port.clone(),
        );
    }

    let mut virtual_input_map: IndexMap<String, Vec<(NodeId, NodeSelector, Port)>> =
        IndexMap::new();
    for c in ast_macro
        .connections
        .iter()
        .filter(|c| ast_macro.virtual_ports_in.contains(&c.source.node))
    {
        let target_id = local_alias_to_id[&c.sink.node];
        virtual_input_map
            .entry(c.source.node.clone())
            .or_default()
            .push((target_id, c.sink.node_selector.clone(), c.sink.port.clone()));
    }

    let sink_id = local_alias_to_id[&ast_macro.sink];
    body.sink = Some(sink_id);

    converted.insert(
        name.to_string(),
        IRMacro {
            name: name.to_string(),
            default_params: ast_macro.default_params,
            virtual_input_map,
            body,
            sink: sink_id,
        },
    );
}

/// We have a bit of an easier Ast shape that the actual IR, since patches
/// are not-quite recursive. Here, we convert macros to a type that can recurse,
/// this makes various graph transformations easier.
pub fn ast_to_graph(ast: Ast) -> IRGraph {
    let mut graph = IRGraph::new();

    let mut macro_ast_map: HashMap<String, AstMacro> = ast
        .macros
        .into_iter()
        .map(|m| (m.name.clone(), m))
        .collect();

    let mut converted: HashMap<String, IRMacro> = HashMap::new();

    // Process each macro, recursing into dependencies first
    let names: Vec<String> = macro_ast_map.keys().cloned().collect();
    for name in names {
        convert_macro(&name, &mut macro_ast_map, &mut converted);
    }

    graph.macro_registry = converted;

    // Add one node per declaration; classify each as Leaf or MacroRef.
    let mut alias_to_id: HashMap<String, NodeId> = HashMap::new();

    for scope in &ast.declarations {
        for decl in &scope.declarations {
            let alias = decl.alias.clone().unwrap_or_else(|| decl.node_type.clone());

            let kind = if graph.macro_registry.contains_key(&decl.node_type) {
                IRNodeKind::MacroRef
            } else {
                IRNodeKind::Leaf
            };

            let id = graph.add_node(
                kind,
                scope.namespace.clone(),
                decl.node_type.clone(),
                alias.clone(),
                decl.params.clone().unwrap_or_default(),
                decl.pipes.clone(),
                decl.count,
            );
            alias_to_id.insert(alias, id);
        }
    }

    // Preserve connections verbatim.  Virtual ports are not resolved here;
    // they pass through as `Port::Named` and are handled by MacroExpansionPass.
    for conn in &ast.connections {
        let src = *alias_to_id
            .get(&conn.source.node)
            .unwrap_or_else(|| panic!("ast_to_graph: source '{}' not found", conn.source.node));
        let snk = *alias_to_id
            .get(&conn.sink.node)
            .unwrap_or_else(|| panic!("ast_to_graph: sink '{}' not found", conn.sink.node));
        graph.connect_multi(
            src,
            conn.source.node_selector.clone(),
            conn.source.port.clone(),
            snk,
            conn.sink.node_selector.clone(),
            conn.sink.port.clone(),
        );
    }

    graph.sink = alias_to_id.get(&ast.sink).copied();
    graph.source = ast
        .source
        .as_ref()
        .and_then(|s| alias_to_id.get(s).copied());

    graph
}

#[macro_export]
macro_rules! object {
    () => { ::std::collections::BTreeMap::new() };
    ( $($key:expr => template $val:expr),* $(,)? ) => {{
        let mut _map = ::std::collections::BTreeMap::new();
        $( _map.insert($key.to_string(), $crate::ir::Value::Template($val.to_string())); )*
        _map
    }};
    ( $($key:expr => $value:expr),* $(,)? ) => {{
        let mut _map = ::std::collections::BTreeMap::new();
        $( _map.insert($key.to_string(), $crate::dsl::ir::Value::from($value)); )*
        _map
    }};
}

#[cfg(test)]
mod spawn_tests {
    use crate::dsl::pipeline::Pipeline;

    use super::*;

    fn expand(ast: Ast) -> IRGraph {
        Pipeline::default().run_from_ast(ast)
    }

    fn multi_decl(node_type: &str, alias: &str, count: u32) -> NodeDeclaration {
        NodeDeclaration {
            node_type: node_type.into(),
            alias: Some(alias.into()),
            count,
            ..Default::default()
        }
    }

    fn scope(ns: &str, decls: Vec<NodeDeclaration>) -> DeclarationScope {
        DeclarationScope {
            namespace: ns.into(),
            declarations: decls,
        }
    }

    fn conn(
        src: &str,
        src_sel: NodeSelector,
        src_port: Port,
        snk: &str,
        snk_sel: NodeSelector,
        snk_port: Port,
    ) -> Connection {
        Connection {
            source: Endpoint {
                node: src.into(),
                node_selector: src_sel,
                port: src_port,
            },
            sink: Endpoint {
                node: snk.into(),
                node_selector: snk_sel,
                port: snk_port,
            },
        }
    }

    #[test]
    fn test_spawn_creates_n_instances() {
        let ast = Ast {
            declarations: vec![scope("audio", vec![multi_decl("sine", "osc", 4)])],
            sink: "osc".into(),
            ..Default::default()
        };
        let graph = expand(ast);

        assert_eq!(graph.node_count(), 4);
        for i in 0..4 {
            assert!(
                graph.find_node_by_alias(&format!("osc.{i}")).is_some(),
                "missing osc.{i}"
            );
        }
        // No edges declared -> no edges produced.
        assert_eq!(graph.edge_count(), 0);
    }

    #[test]
    fn test_spawn_instances_inherit_params() {
        let ast = Ast {
            declarations: vec![scope(
                "audio",
                vec![NodeDeclaration {
                    node_type: "sine".into(),
                    alias: Some("osc".into()),
                    count: 3,
                    params: Some(object! { "freq" => 220.0f32 }),
                    ..Default::default()
                }],
            )],
            sink: "osc".into(),
            ..Default::default()
        };
        let graph = expand(ast);

        for i in 0..3 {
            assert_eq!(
                graph
                    .find_node_by_alias(&format!("osc.{i}"))
                    .unwrap()
                    .params
                    .get("freq"),
                Some(&Value::F32(220.0)),
                "osc.{i} should have freq=220.0"
            );
        }
    }

    // ── automap (*) >> (*) ─────────────────────────────────────────────────

    #[test]
    fn test_automap_all_to_all() {
        // my_mod(*) >> my_carrier(*)  — both count=4
        let ast = Ast {
            declarations: vec![scope(
                "audio",
                vec![
                    multi_decl("sine", "modulator", 4),
                    multi_decl("sine", "carrier", 4),
                ],
            )],
            connections: vec![conn(
                "modulator",
                NodeSelector::All,
                Port::None,
                "carrier",
                NodeSelector::All,
                Port::Index(0),
            )],
            sink: "carrier".into(),
            ..Default::default()
        };
        let graph = expand(ast);

        assert_eq!(graph.node_count(), 8);
        // One edge per pair: 4 modulator->carrier edges.
        assert_eq!(graph.edge_count(), 4);
        for i in 0..4 {
            let src_alias = format!("modulator.{i}");
            let snk_alias = format!("carrier.{i}");
            let edges = graph.find_edges_between(&src_alias, &snk_alias);
            assert_eq!(edges.len(), 1, "expected edge {src_alias} -> {snk_alias}");
            assert_eq!(edges[0].sink_port, Port::Index(0));
        }
    }

    // ── range selector ─────────────────────────────────────────────────────

    #[test]
    fn test_range_selector_partial_zip() {
        // my_source(1..3).out >> my_sink(1..3).audio_in — pick instances 1 and 2
        let ast = Ast {
            declarations: vec![scope(
                "audio",
                vec![multi_decl("osc", "src", 4), multi_decl("filter", "snk", 4)],
            )],
            connections: vec![conn(
                "src",
                NodeSelector::Range(1, 3),
                Port::Named("out".into()),
                "snk",
                NodeSelector::Range(1, 3),
                Port::Named("audio_in".into()),
            )],
            sink: "snk".into(),
            ..Default::default()
        };
        let graph = expand(ast);

        assert_eq!(graph.edge_count(), 2);
        for i in 1..3 {
            let edges = graph.find_edges_between(&format!("src.{i}"), &format!("snk.{i}"));
            assert_eq!(edges.len(), 1, "expected src.{i} -> snk.{i}");
            assert_eq!(edges[0].source_port, Port::Named("out".into()));
            assert_eq!(edges[0].sink_port, Port::Named("audio_in".into()));
        }
        // src.0 and src.3 should have no edges.
        assert!(graph.find_edges_from("src.0").is_empty());
        assert!(graph.find_edges_from("src.3").is_empty());
    }

    #[test]
    fn test_source_range_to_sink_port_slice() {
        // my_example(0..2).out >> mixer[0..2]
        let ast = Ast {
            declarations: vec![scope(
                "audio",
                vec![multi_decl("osc", "src", 4), multi_decl("mixer", "mixer", 1)],
            )],
            connections: vec![conn(
                "src",
                NodeSelector::Range(0, 2),
                Port::Named("out".into()),
                "mixer",
                NodeSelector::Single,
                Port::Slice(0, 2),
            )],
            sink: "mixer".into(),
            ..Default::default()
        };

        let graph = expand(ast);

        assert_eq!(graph.edge_count(), 2);

        let edges_to_mixer = graph.find_edges_to("mixer");
        assert_eq!(edges_to_mixer.len(), 2);

        let slot0 = edges_to_mixer
            .iter()
            .find(|e| e.sink_port == Port::Index(0))
            .expect("[0] missing");

        let slot1 = edges_to_mixer
            .iter()
            .find(|e| e.sink_port == Port::Index(1))
            .expect("[1] missing");

        let src0 = graph.find_node_by_alias("src.0").unwrap();
        let src1 = graph.find_node_by_alias("src.1").unwrap();
        assert_eq!(slot0.source, src0.id);
        assert_eq!(slot1.source, src1.id);
    }

    #[test]
    fn test_broadcast_single_source_to_multi_sink() {
        let ast = Ast {
            declarations: vec![scope(
                "audio",
                vec![
                    NodeDeclaration {
                        node_type: "lfo".into(),
                        alias: Some("lfo".into()),
                        count: 1,
                        ..Default::default()
                    },
                    multi_decl("filter", "filt", 4),
                ],
            )],
            connections: vec![conn(
                "lfo",
                NodeSelector::Single,
                Port::None,
                "filt",
                NodeSelector::All,
                Port::Named("cutoff".into()),
            )],
            sink: "filt".into(),
            ..Default::default()
        };
        let graph = expand(ast);

        assert_eq!(graph.edge_count(), 4);
        let lfo = graph.find_node_by_alias("lfo").unwrap();
        for i in 0..4 {
            let filt = graph.find_node_by_alias(&format!("filt.{i}")).unwrap();
            let edges = graph.find_edges_between("lfo", &format!("filt.{i}"));
            assert_eq!(edges.len(), 1, "expected lfo -> filt.{i}");
            assert_eq!(edges[0].source, lfo.id);
            assert_eq!(edges[0].sink, filt.id);
            assert_eq!(edges[0].sink_port, Port::Named("cutoff".into()));
        }
    }

    #[test]
    fn test_multi_node_inside_macro_expands_with_fqn() {
        // patch poly_voice { sine: osc * 4 { freq: 440.0 } }
        let poly_voice = AstMacro {
            name: "poly_voice".into(),
            default_params: Some(object! { "freq" => 440.0f32 }),
            declarations: vec![DeclarationScope {
                namespace: "audio".into(),
                declarations: vec![NodeDeclaration {
                    node_type: "sine".into(),
                    alias: Some("osc".into()),
                    count: 4,
                    params: Some(object! { "freq" => Value::Template("$freq".into()) }),
                    ..Default::default()
                }],
            }],
            sink: "osc".into(),
            ..Default::default()
        };

        let ast = Ast {
            macros: vec![poly_voice],
            declarations: vec![DeclarationScope {
                namespace: "audio".into(),
                declarations: vec![NodeDeclaration {
                    node_type: "poly_voice".into(),
                    alias: Some("lead".into()),
                    params: Some(object! { "freq" => 880.0f32 }),
                    count: 1,
                    ..Default::default()
                }],
            }],
            sink: "lead".into(),
            ..Default::default()
        };

        let graph = expand(ast);
        assert_eq!(graph.node_count(), 4);

        for i in 0..4 {
            let alias = format!("lead.osc.{i}");
            let node = graph
                .find_node_by_alias(&alias)
                .unwrap_or_else(|| panic!("missing {alias}"));
            // Param substituted through the macro.
            assert_eq!(node.params.get("freq"), Some(&Value::F32(880.0)));
        }
    }

    #[test]
    fn test_index_selector_connects_single_instance() {
        // connect only osc(2) -> filter
        let ast = Ast {
            declarations: vec![scope(
                "audio",
                vec![multi_decl("osc", "osc", 4), multi_decl("filter", "filt", 1)],
            )],
            connections: vec![conn(
                "osc",
                NodeSelector::Index(2),
                Port::None,
                "filt",
                NodeSelector::Single,
                Port::None,
            )],
            sink: "filt".into(),
            ..Default::default()
        };
        let graph = expand(ast);

        assert_eq!(graph.edge_count(), 1);
        let edges = graph.find_edges_between("osc.2", "filt");
        assert_eq!(edges.len(), 1);
    }
}