glicol_synth/node/
mod.rs

1use crate::buffer::Buffer;
2use hashbrown::HashMap;
3
4#[cfg(feature = "node-boxed")]
5mod boxed;
6pub use boxed::*;
7#[cfg(feature = "node-pass")]
8mod pass;
9pub use pass::*;
10#[cfg(feature = "node-sum")]
11mod sum;
12pub use sum::*;
13
14pub mod oscillator;
15// pub use oscillator::*;
16pub mod operator;
17// pub use operator::*;
18pub mod signal;
19// pub use signal::*;
20pub mod filter;
21// pub use filter::*;
22
23pub mod sequencer;
24// pub use sequencer::*;
25pub mod delay;
26// pub use delay::*;
27pub mod envelope;
28// pub use envelope::*;
29pub mod effect;
30// pub use effect::*;
31pub mod compound;
32// pub use compound::*;
33
34pub mod synth;
35// pub use synth::*;
36
37#[cfg(feature = "use-samples")]
38pub mod sampling;
39
40// #[cfg(feature = "use-samples")]
41// pub use sampling::*;
42
43#[cfg(feature = "use-meta")]
44pub mod dynamic;
45
46// #[cfg(feature = "use-meta")]
47// pub use dynamic::*;
48
49pub trait Node<const N: usize> {
50    fn process(&mut self, inputs: &mut HashMap<usize, Input<N>>, output: &mut [Buffer<N>]);
51    fn send_msg(&mut self, info: crate::Message);
52}
53
54/// An important part of the `Node` trait; each `Input` contains the relevant node id as `usize`
55pub struct Input<const N: usize> {
56    buffers_ptr: *const Buffer<N>,
57    buffers_len: usize,
58    pub node_id: usize,
59}
60
61impl<const N: usize> Input<N> {
62    // Constructor solely for use within the graph `process` function.
63    pub(crate) fn new(slice: &[Buffer<N>], node_id: usize) -> Self {
64        let buffers_ptr = slice.as_ptr();
65        let buffers_len = slice.len();
66        Input {
67            buffers_ptr,
68            buffers_len,
69            node_id,
70        }
71    }
72
73    /// A reference to the buffers of the input node.
74    pub fn buffers(&self) -> &[Buffer<N>] {
75        // As we know that an `Input` can only be constructed during a call to the graph `process`
76        // function, we can be sure that our slice is still valid as long as the input itself is
77        // alive.
78        unsafe { std::slice::from_raw_parts(self.buffers_ptr, self.buffers_len) }
79    }
80}
81
82// Inputs can only be created by the `dasp_graph::process` implementation and only ever live as
83// long as the lifetime of the call to the function. Thus, it's safe to implement this so that
84// `Send` closures can be stored within the graph and sent between threads.
85unsafe impl<const N: usize> Send for Input<N> {}
86
87impl<const N: usize> core::fmt::Debug for Input<N> {
88    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
89        core::fmt::Debug::fmt(self.buffers(), f)
90    }
91}
92
93impl<'a, T, const N: usize> Node<N> for &'a mut T
94where
95    T: Node<N>,
96{
97    fn process(&mut self, inputs: &mut HashMap<usize, Input<N>>, output: &mut [Buffer<N>]) {
98        (**self).process(inputs, output)
99    }
100    fn send_msg(&mut self, info: crate::Message) {
101        (**self).send_msg(info)
102    }
103}
104
105impl<T, const N: usize> Node<N> for Box<T>
106where
107    T: Node<N>,
108{
109    fn process(&mut self, inputs: &mut HashMap<usize, Input<N>>, output: &mut [Buffer<N>]) {
110        (**self).process(inputs, output)
111    }
112    fn send_msg(&mut self, _info: crate::Message) {}
113}
114
115impl<const N: usize> Node<N> for dyn Fn(&HashMap<usize, Input<N>>, &mut [Buffer<N>]) {
116    fn process(&mut self, inputs: &mut HashMap<usize, Input<N>>, output: &mut [Buffer<N>]) {
117        (*self)(inputs, output)
118    }
119    fn send_msg(&mut self, _info: crate::Message) {}
120}
121
122impl<const N: usize> Node<N> for dyn FnMut(&HashMap<usize, Input<N>>, &mut [Buffer<N>]) {
123    fn process(&mut self, inputs: &mut HashMap<usize, Input<N>>, output: &mut [Buffer<N>]) {
124        (*self)(inputs, output)
125    }
126
127    fn send_msg(&mut self, _info: crate::Message) {}
128}
129
130impl<const N: usize> Node<N> for fn(&HashMap<usize, Input<N>>, &mut [Buffer<N>]) {
131    fn process(&mut self, inputs: &mut HashMap<usize, Input<N>>, output: &mut [Buffer<N>]) {
132        (*self)(inputs, output)
133    }
134    fn send_msg(&mut self, _info: crate::Message) {}
135}