rill_core/executor/mod.rs
1//! Graph executor for driving signal processing using the topology graph and active ports.
2//!
3//! The `GraphExecutor` coordinates clock ticks, pulls data from input ports,
4//! calls node processing, and pushes data to output ports.
5//!
6//! This module provides a prototype implementation that demonstrates the active ports flow.
7//! A real implementation would integrate with a concrete graph (e.g., `rill_graph::SignalGraph`).
8//!
9//! # Example
10//! ```
11//! use rill_core::executor::GraphExecutor;
12//! use rill_core::math::Transcendental;
13//!
14//! // Create a graph executor (graph omitted for prototype).
15//! // let mut executor = GraphExecutor::new(graph);
16//! // executor.process_block().unwrap();
17//! ```
18
19use crate::math::Transcendental;
20use crate::traits::ActivePort;
21
22/// Executor for a signal graph that processes nodes in topological order.
23///
24/// This is a prototype that outlines the structure. In a real implementation,
25/// the executor would hold a concrete graph and iterate over its nodes.
26pub struct GraphExecutor<T: Transcendental, const BUF_SIZE: usize> {
27 // Placeholder for the signal graph.
28 // In reality, this would be something like `graph: rill_graph::SignalGraph<T, BUF_SIZE>`.
29 _phantom: std::marker::PhantomData<T>,
30}
31
32impl<T: Transcendental, const BUF_SIZE: usize> Default for GraphExecutor<T, BUF_SIZE> {
33 fn default() -> Self { Self::new() }
34}
35
36impl<T: Transcendental, const BUF_SIZE: usize> GraphExecutor<T, BUF_SIZE> {
37 /// Create a new executor from an existing graph.
38 pub fn new() -> Self {
39 Self {
40 _phantom: std::marker::PhantomData,
41 }
42 }
43
44 /// Process one block of audio.
45 ///
46 /// This advances the clock, obtains topological order, processes each node
47 /// by pulling inputs, calling appropriate processing method, and pushing outputs.
48 ///
49 /// # Returns
50 /// `Ok(())` if processing succeeded, or an error if something went wrong.
51 ///
52 /// # Algorithm (conceptual)
53 /// 1. Advance the clock (if the graph provides a clock source).
54 /// 2. Obtain topological order (if the graph provides it).
55 /// 3. For each node in order:
56 /// a. Gather input data by calling `pull` on each input port (ActivePort trait).
57 /// b. Determine node type (Source/Processor/Sink) via metadata.
58 /// c. Call the appropriate processing method (generate/process/consume).
59 /// d. Push output data by calling `push` on each output port.
60 /// 4. Handle errors (disconnected ports, missing data).
61 pub fn process_block(&mut self) -> Result<(), Box<dyn std::error::Error>> {
62 // This is a stub implementation. A real executor would need access to
63 // the graph's nodes and connections, which is beyond the scope of this prototype.
64 Ok(())
65 }
66}
67
68/// Example of using ActivePort trait to pull data from an input port.
69/// This function illustrates the intended pattern.
70pub fn demonstrate_pull<T: Transcendental, const BUF_SIZE: usize>(
71 port: &mut dyn ActivePort<T, BUF_SIZE>,
72) -> Option<[T; BUF_SIZE]> {
73 // Pull data from the port (if connected).
74 port.pull()
75}
76
77/// Example of pushing data to an output port.
78pub fn demonstrate_push<T: Transcendental, const BUF_SIZE: usize>(
79 port: &mut dyn ActivePort<T, BUF_SIZE>,
80 data: [T; BUF_SIZE],
81) -> Result<(), crate::traits::PortError> {
82 port.push(data)
83}