use std::fmt::Debug;
use crate::{context::AudioContext, msg::NodeMessage, ports::Ports};
pub type Inputs<'a> = [Option<&'a [f32]>];
pub type Channels<'a> = &'a [&'a [f32]];
pub type Outputs<'a> = Channels<'a>;
pub trait Node {
fn process(&mut self, ctx: &mut AudioContext, inputs: &Inputs, outputs: &mut [&mut [f32]]);
fn handle_msg(&mut self, _msg: NodeMessage) {}
fn ports(&self) -> &Ports;
}
pub trait NodeClone {
fn clone_box(&self) -> Box<dyn DynNode>;
}
pub trait DynNode: Node + NodeClone + Send {}
impl<T> DynNode for T where T: Node + NodeClone + Send {}
impl<T> NodeClone for T
where
T: Node + Clone + Send + 'static,
{
fn clone_box(&self) -> Box<dyn DynNode> {
Box::new(self.clone())
}
}
pub struct LegatoNode {
pub name: String,
pub node_kind: String,
node: Box<dyn DynNode>,
}
impl LegatoNode {
pub fn new(name: String, node_kind: String, node: Box<dyn DynNode>) -> Self {
Self {
name,
node_kind,
node,
}
}
#[inline(always)]
pub fn get_node(&self) -> &dyn DynNode {
&*self.node
}
#[inline(always)]
pub fn get_node_mut(&mut self) -> &mut Box<dyn DynNode> {
&mut self.node
}
#[inline(always)]
pub fn handle_msg(&mut self, msg: NodeMessage) {
self.get_node_mut().as_mut().handle_msg(msg);
}
}
impl Clone for LegatoNode {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
node_kind: self.node_kind.clone(),
node: self.node.clone_box(),
}
}
}
impl Debug for LegatoNode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct(&self.name)
.field("node_kind", &self.node_kind)
.field("ports", self.node.ports())
.finish()
}
}