mkaudiolibrary 2.1.0

Modular audio processing library including MKAU plugin format based on Rust.
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
use super::buffer_pool::BufferPool;
use super::node::NodeId;
use crate::sim::components::CircuitComponent;

/// Edge in the circuit graph representing signal flow.
#[derive(Clone, Debug)]
pub struct Edge {
    /// Upstream node this edge reads from.
    pub source: NodeId,
    /// Downstream node this edge feeds into.
    pub dest: NodeId,
}

/// A node wrapping a circuit component.
pub struct Node {
    /// This node's identifier within the owning [`CircuitGraph`].
    pub id: NodeId,
    /// The wrapped component that does the actual signal processing.
    pub component: Box<dyn CircuitComponent>,
}

/// Zero Delay Feedback cluster — a group of nodes in a feedback loop
/// that must be solved together using Newton iteration.
#[derive(Clone, Debug)]
pub struct ZdfCluster {
    /// Node IDs participating in this feedback cluster.
    pub nodes: Vec<NodeId>,
    /// Maximum Newton iterations per sample when solving this cluster.
    pub solver_max_iter: u32,
}

/// Compiled graph ready for block processing.
pub struct CompiledGraph {
    /// Topologically sorted execution order (excludes ZDF cluster internals).
    pub execution_order: Vec<NodeId>,
    /// ZDF feedback clusters requiring iterative solving.
    pub zdf_clusters: Vec<ZdfCluster>,
    /// Buffer assignment: node `i` uses buffer `buffer_assignments[i]`.
    pub buffer_assignments: Vec<usize>,
}

/// Directed graph connecting circuit components.
///
/// Builder pattern: `add_node()` → `connect()` → `compile()` → `process_block()`.
pub struct CircuitGraph {
    nodes: Vec<Node>,
    edges: Vec<Edge>,
    compiled: Option<CompiledGraph>,
    buffer_pool: BufferPool,
    block_size: usize,
    sample_rate: f32,
}

impl CircuitGraph {
    /// Create an empty graph for the given block size and sample rate.
    pub fn new(block_size: usize, sample_rate: f32) -> Self {
        Self {
            nodes: Vec::new(),
            edges: Vec::new(),
            compiled: None,
            buffer_pool: BufferPool::new(0, block_size),
            block_size,
            sample_rate,
        }
    }

    /// Add a component node. Returns its NodeId.
    pub fn add_node(&mut self, mut component: Box<dyn CircuitComponent>) -> NodeId {
        let id = self.nodes.len();
        component.prepare(self.sample_rate);
        self.nodes.push(Node { id, component });
        self.compiled = None; // invalidate
        id
    }

    /// Connect source node's output to dest node's input.
    pub fn connect(&mut self, source: NodeId, dest: NodeId) {
        assert!(source < self.nodes.len(), "invalid source node");
        assert!(dest < self.nodes.len(), "invalid dest node");
        self.edges.push(Edge { source, dest });
        self.compiled = None;
    }

    /// Compile the graph: topological sort, ZDF detection, buffer assignment.
    pub fn compile(&mut self) -> Result<(), GraphError> {
        let n = self.nodes.len();
        if n == 0 {
            self.compiled = Some(CompiledGraph {
                execution_order: Vec::new(),
                zdf_clusters: Vec::new(),
                buffer_assignments: Vec::new(),
            });
            return Ok(());
        }

        // Build adjacency list and in-degree
        let mut adj: Vec<Vec<NodeId>> = vec![Vec::new(); n];
        let mut in_degree = vec![0u32; n];

        for edge in &self.edges {
            adj[edge.source].push(edge.dest);
            in_degree[edge.dest] += 1;
        }

        // Detect cycles using DFS
        let cycles = self.detect_cycles(&adj);

        // Topological sort (Kahn's algorithm), treating cycle nodes as ZDF clusters
        let cycle_nodes: std::collections::HashSet<NodeId> =
            cycles.iter().flat_map(|c| c.iter().copied()).collect();

        let mut queue: Vec<NodeId> = Vec::new();
        for (i, &deg) in in_degree.iter().enumerate() {
            if deg == 0 && !cycle_nodes.contains(&i) {
                queue.push(i);
            }
        }

        let mut execution_order = Vec::with_capacity(n);
        let mut visited = vec![false; n];

        // Process queue
        let mut head = 0;
        while head < queue.len() {
            let node = queue[head];
            head += 1;
            execution_order.push(node);
            visited[node] = true;

            for &next in &adj[node] {
                if !cycle_nodes.contains(&next) {
                    in_degree[next] -= 1;
                    if in_degree[next] == 0 {
                        queue.push(next);
                    }
                }
            }
        }

        // Add cycle nodes as ZDF clusters
        let zdf_clusters: Vec<ZdfCluster> = cycles
            .into_iter()
            .map(|nodes| ZdfCluster {
                nodes,
                solver_max_iter: 4,
            })
            .collect();

        // Insert ZDF cluster nodes into execution order at appropriate positions
        for cluster in &zdf_clusters {
            for &node_id in &cluster.nodes {
                if !visited[node_id] {
                    execution_order.push(node_id);
                    visited[node_id] = true;
                }
            }
        }

        // Any remaining unvisited nodes (disconnected)
        for (i, &v) in visited.iter().enumerate() {
            if !v {
                execution_order.push(i);
            }
        }

        // Buffer assignments: each node gets its own buffer
        let buffer_assignments: Vec<usize> = (0..n).collect();

        // Allocate buffer pool
        self.buffer_pool = BufferPool::new(n + 1, self.block_size); // +1 for temp

        self.compiled = Some(CompiledGraph {
            execution_order,
            zdf_clusters,
            buffer_assignments,
        });

        Ok(())
    }

    /// Detect cycles in the graph using DFS.
    fn detect_cycles(&self, adj: &[Vec<NodeId>]) -> Vec<Vec<NodeId>> {
        let n = adj.len();
        let mut visited = vec![0u8; n]; // 0=unvisited, 1=in-stack, 2=done
        let mut stack = Vec::new();
        let mut cycles = Vec::new();

        for start in 0..n {
            if visited[start] == 0 {
                self.dfs_cycle(start, adj, &mut visited, &mut stack, &mut cycles);
            }
        }

        cycles
    }

    fn dfs_cycle(
        &self,
        node: NodeId,
        adj: &[Vec<NodeId>],
        visited: &mut [u8],
        stack: &mut Vec<NodeId>,
        cycles: &mut Vec<Vec<NodeId>>,
    ) {
        visited[node] = 1;
        stack.push(node);

        for &next in &adj[node] {
            if visited[next] == 1 {
                // Found a cycle — collect nodes from `next` to end of stack
                if let Some(pos) = stack.iter().position(|&n| n == next) {
                    cycles.push(stack[pos..].to_vec());
                }
            } else if visited[next] == 0 {
                self.dfs_cycle(next, adj, visited, stack, cycles);
            }
        }

        stack.pop();
        visited[node] = 2;
    }

    /// Process a block of audio through the compiled graph.
    ///
    /// `input` is fed to the first node, `output` is read from the last node.
    pub fn process_block(&mut self, input: &[f32], output: &mut [f32]) {
        let compiled = match &self.compiled {
            Some(c) => c,
            None => {
                // Auto-compile if not yet compiled
                if self.compile().is_err() {
                    output.iter_mut().for_each(|s| *s = 0.0);
                    return;
                }
                self.compiled.as_ref().unwrap()
            }
        };

        let len = input.len().min(output.len()).min(self.block_size);

        if compiled.execution_order.is_empty() {
            output[..len].copy_from_slice(&input[..len]);
            return;
        }

        // Copy input into first node's input buffer
        let first_node = compiled.execution_order[0];
        let first_buf = compiled.buffer_assignments[first_node];
        self.buffer_pool.get_mut(first_buf).as_mut_slice()[..len].copy_from_slice(&input[..len]);

        // Process nodes in execution order
        let execution_order = compiled.execution_order.clone();
        let buffer_assignments = compiled.buffer_assignments.clone();

        for (order_idx, &node_id) in execution_order.iter().enumerate() {
            let buf_idx = buffer_assignments[node_id];

            // Determine input: from predecessor's buffer or the initial input buffer
            let temp_buf_idx = self.buffer_pool.len() - 1; // temp buffer

            // Find predecessor
            let pred = self
                .edges
                .iter()
                .find(|e| e.dest == node_id)
                .map(|e| e.source);

            if let Some(pred_id) = pred {
                let pred_buf = buffer_assignments[pred_id];
                // Copy predecessor output to temp
                let src_data: Vec<f32> = self.buffer_pool.get(pred_buf).as_slice()[..len].to_vec();
                self.buffer_pool.get_mut(temp_buf_idx).as_mut_slice()[..len]
                    .copy_from_slice(&src_data);
            } else if order_idx == 0 {
                // First node: input already copied above
                let src_data: Vec<f32> = self.buffer_pool.get(buf_idx).as_slice()[..len].to_vec();
                self.buffer_pool.get_mut(temp_buf_idx).as_mut_slice()[..len]
                    .copy_from_slice(&src_data);
            } else {
                // No predecessor, zero input
                self.buffer_pool.get_mut(temp_buf_idx).as_mut_slice()[..len]
                    .iter_mut()
                    .for_each(|s| *s = 0.0);
            }

            // Process: read from temp, write to node's buffer
            let input_data: Vec<f32> =
                self.buffer_pool.get(temp_buf_idx).as_slice()[..len].to_vec();
            let node = &mut self.nodes[node_id];
            node.component.process_block(
                &input_data,
                &mut self.buffer_pool.get_mut(buf_idx).as_mut_slice()[..len],
            );
        }

        // Process ZDF clusters (iterative solving)
        let zdf_clusters = compiled.zdf_clusters.clone();
        for cluster in &zdf_clusters {
            for _ in 0..cluster.solver_max_iter {
                for &node_id in &cluster.nodes {
                    let buf_idx = buffer_assignments[node_id];
                    let pred = self
                        .edges
                        .iter()
                        .find(|e| e.dest == node_id)
                        .map(|e| e.source);

                    let input_data = if let Some(pred_id) = pred {
                        let pred_buf = buffer_assignments[pred_id];
                        self.buffer_pool.get(pred_buf).as_slice()[..len].to_vec()
                    } else {
                        vec![0.0; len]
                    };

                    let node = &mut self.nodes[node_id];
                    node.component.process_block(
                        &input_data,
                        &mut self.buffer_pool.get_mut(buf_idx).as_mut_slice()[..len],
                    );
                }
            }
        }

        // Copy last node's output
        let last_node = *execution_order.last().unwrap();
        let last_buf = buffer_assignments[last_node];
        output[..len].copy_from_slice(&self.buffer_pool.get(last_buf).as_slice()[..len]);
    }

    /// Get the number of nodes in the graph.
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Check if the graph has been compiled.
    pub fn is_compiled(&self) -> bool {
        self.compiled.is_some()
    }
}

/// Errors that can occur while building or compiling a [`CircuitGraph`].
#[derive(Debug)]
pub enum GraphError {
    /// The graph contains a feedback cycle that wasn't resolved into a
    /// [`ZdfCluster`] (should not occur via the normal `compile()` path,
    /// which detects and clusters cycles automatically).
    CycleDetected,
    /// An edge or lookup referenced a node ID that doesn't exist in the graph.
    InvalidNode(NodeId),
}

impl std::fmt::Display for GraphError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GraphError::CycleDetected => write!(f, "cycle detected in circuit graph"),
            GraphError::InvalidNode(id) => write!(f, "invalid node id: {id}"),
        }
    }
}

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

    /// Simple pass-through component for testing.
    struct PassThrough {
        gain: f32,
    }

    impl CircuitComponent for PassThrough {
        fn prepare(&mut self, _sample_rate: f32) {}
        fn process_block(&mut self, input: &[f32], output: &mut [f32]) {
            for i in 0..input.len().min(output.len()) {
                output[i] = input[i] * self.gain;
            }
        }
        fn update_parameters(&mut self) {}
    }

    #[test]
    fn test_single_node() {
        let mut graph = CircuitGraph::new(64, 44100.0);
        let _n = graph.add_node(Box::new(PassThrough { gain: 2.0 }));
        graph.compile().unwrap();

        let input = [1.0f32; 64];
        let mut output = [0.0f32; 64];
        graph.process_block(&input, &mut output);

        for &s in &output {
            assert!((s - 2.0).abs() < 1e-6, "expected 2.0, got {s}");
        }
    }

    #[test]
    fn test_chain() {
        let mut graph = CircuitGraph::new(32, 44100.0);
        let n0 = graph.add_node(Box::new(PassThrough { gain: 2.0 }));
        let n1 = graph.add_node(Box::new(PassThrough { gain: 3.0 }));
        graph.connect(n0, n1);
        graph.compile().unwrap();

        let input = [1.0f32; 32];
        let mut output = [0.0f32; 32];
        graph.process_block(&input, &mut output);

        for &s in &output {
            assert!((s - 6.0).abs() < 1e-5, "expected 6.0, got {s}");
        }
    }

    #[test]
    fn test_topological_sort() {
        let mut graph = CircuitGraph::new(16, 44100.0);
        let n0 = graph.add_node(Box::new(PassThrough { gain: 1.0 }));
        let n1 = graph.add_node(Box::new(PassThrough { gain: 1.0 }));
        let n2 = graph.add_node(Box::new(PassThrough { gain: 1.0 }));
        graph.connect(n0, n1);
        graph.connect(n1, n2);
        graph.compile().unwrap();

        let compiled = graph.compiled.as_ref().unwrap();
        // n0 must come before n1, n1 before n2
        let pos = |id: NodeId| {
            compiled
                .execution_order
                .iter()
                .position(|&n| n == id)
                .unwrap()
        };
        assert!(pos(n0) < pos(n1));
        assert!(pos(n1) < pos(n2));
    }

    #[test]
    fn test_cycle_detection() {
        let mut graph = CircuitGraph::new(16, 44100.0);
        let n0 = graph.add_node(Box::new(PassThrough { gain: 1.0 }));
        let n1 = graph.add_node(Box::new(PassThrough { gain: 1.0 }));
        graph.connect(n0, n1);
        graph.connect(n1, n0); // creates cycle

        graph.compile().unwrap();
        let compiled = graph.compiled.as_ref().unwrap();
        assert!(
            !compiled.zdf_clusters.is_empty(),
            "should detect ZDF cluster"
        );
    }

    #[test]
    fn test_empty_graph() {
        let mut graph = CircuitGraph::new(16, 44100.0);
        graph.compile().unwrap();

        let input = [1.0f32; 16];
        let mut output = [0.0f32; 16];
        graph.process_block(&input, &mut output);

        // Empty graph: input passes through
        for i in 0..16 {
            assert_eq!(output[i], input[i]);
        }
    }
}