use std::marker::PhantomData;
use rill_core::math::Transcendental;
use rill_core::time::ClockSource;
use rill_graph::{AudioGraph, GraphBuilder};
use crate::engine::AudioProcessor;
const DEFAULT_BUF_SIZE: usize = 256;
pub struct GraphProcessor<T: Transcendental = f32, const BUF_SIZE: usize = DEFAULT_BUF_SIZE> {
graph: AudioGraph<T, BUF_SIZE>,
_marker: PhantomData<T>,
}
impl<T: Transcendental, const BUF_SIZE: usize> GraphProcessor<T, BUF_SIZE> {
pub fn from_builder(
builder: GraphBuilder<T, BUF_SIZE>,
clock: Box<dyn ClockSource>,
) -> Result<Self, rill_graph::BuildError> {
let graph = builder.build(clock)?;
Ok(Self {
graph,
_marker: PhantomData,
})
}
pub fn from_graph(graph: AudioGraph<T, BUF_SIZE>) -> Self {
Self {
graph,
_marker: PhantomData,
}
}
pub fn graph(&self) -> &AudioGraph<T, BUF_SIZE> {
&self.graph
}
pub fn graph_mut(&mut self) -> &mut AudioGraph<T, BUF_SIZE> {
&mut self.graph
}
pub fn node_count(&self) -> usize {
self.graph.node_count()
}
pub fn topo_order(&self) -> &[usize] {
self.graph.topo_order()
}
pub fn output_buffer(&self, node_idx: usize, port_idx: usize) -> Option<&[T; BUF_SIZE]> {
self.graph.output_buffer(node_idx, port_idx)
}
}
impl<T: Transcendental, const BUF_SIZE: usize> AudioProcessor for GraphProcessor<T, BUF_SIZE> {
fn process(&mut self, input: &[f32], output: &mut [f32]) {
self.graph.dispatch_set_parameters(&[]);
let n = output.len().min(input.len());
output[..n].copy_from_slice(&input[..n]);
}
fn reset(&mut self) {
}
fn set_sample_rate(&mut self, _sample_rate: f32) {
}
}