rill_core/traits/router.rs
1//! # Router Trait — signal routing
2//!
3//! `Router` — a semantically separate type of graph node intended
4//! exclusively for signal routing (mixers, matrix switches,
5//! selectors). Unlike `Processor`, which performs DSP signal
6//! processing, `Router` only redistributes input signals to outputs
7//! with the ability to dynamically change connection topology.
8//!
9//! ## Differences from Processor
10//!
11//! | Characteristic | Processor | Router |
12//! |---|---|---|
13//! | I/O count | Fixed | Dynamic N→M |
14//! | DSP | Yes (filter, effect) | No (only sum/commutation) |
15//! | Topology | Known at build time | Can change at runtime |
16//! | Visualization | Rectangle (P-scheme) | Diamond (P-scheme, condition) |
17
18use crate::math::Transcendental;
19use crate::time::RenderContext;
20use crate::traits::node::Node;
21use crate::traits::ProcessResult;
22
23/// Signal router — N inputs, M outputs, configurable matrix.
24///
25/// Unlike `Processor::process()`, which performs DSP, `Router`
26/// only redistributes input signals to outputs. The router
27/// manages its own output ports via `Node::output_port_mut()`.
28///
29/// `TapeLoop` is obtained not through this trait, but through the graph resource registry
30/// — see `GraphBuilder::add_resource()` and `Node::init()`.
31pub trait Router<T: Transcendental, const BUF_SIZE: usize>: Node<T, BUF_SIZE> {
32 /// Route one block.
33 ///
34 /// The implementation must read signals from `inputs` and write
35 /// the results to its output ports (via `self.output_port_mut(i)`).
36 fn route(&mut self, ctx: &RenderContext, inputs: &[&[T; BUF_SIZE]]) -> ProcessResult<()>;
37
38 /// Number of input ports for routing.
39 fn num_route_inputs(&self) -> usize;
40
41 /// Number of output ports for routing.
42 fn num_route_outputs(&self) -> usize;
43
44 /// Set up a connection: route input `from` to output `to` with gain coefficient `gain`.
45 fn set_connection(&mut self, from: usize, to: usize, gain: T) -> ProcessResult<()>;
46
47 /// Remove a connection (zero the coefficient).
48 fn remove_connection(&mut self, from: usize, to: usize) -> ProcessResult<()>;
49
50 /// Get the current routing matrix: for each output — a list of inputs with gains.
51 fn routing_matrix(&self) -> Vec<Vec<(usize, T)>>;
52}