sound_flow 0.3.0

Execute graphs of functions in real time
Documentation
use crate::graph::Instruction;
use crate::{Node, Sockets};
use std::collections::HashMap;
use std::hash::Hash;

#[derive(Debug, Clone)]
pub struct CompiledGraph<Id: Hash + Eq + Copy, N: Node> {
    pub(crate) instructions: Vec<Instruction<N>>,
    pub(crate) buffers: Vec<N::Data>,
    pub(crate) lookup: HashMap<Id, usize>,
}

#[derive(Copy, Clone, Debug)]
pub struct NodeIndex(usize);

impl<Id: Hash + Eq + Copy, N: Node> CompiledGraph<Id, N> {
    pub fn rt_transfer_state(&mut self, _source: Self) {
        todo!()
    }

    pub fn rt_compute(&mut self, context: &N::Context) {
        for instruction in self.instructions.iter_mut() {
            let sockets = Sockets {
                socket_inputs: &*instruction.socket_inputs,
                socket_outputs: &*instruction.socket_outputs,
                buffers: &mut *self.buffers,
            };
            instruction.node.rt_process(context, sockets)
        }
    }

    pub fn node_index(&self, id: Id) -> Option<NodeIndex> {
        self.lookup.get(&id).map(|&i| NodeIndex(i))
    }

    pub fn node(&self, index: NodeIndex) -> &N {
        &self.instructions[index.0].node
    }

    pub fn node_mut(&mut self, index: NodeIndex) -> &mut N {
        &mut self.instructions[index.0].node
    }
}