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;
15pub mod operator;
17pub mod signal;
19pub mod filter;
21pub mod sequencer;
24pub mod delay;
26pub mod envelope;
28pub mod effect;
30pub mod compound;
32pub mod synth;
35#[cfg(feature = "use-samples")]
38pub mod sampling;
39
40#[cfg(feature = "use-meta")]
44pub mod dynamic;
45
46pub 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
54pub 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 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 pub fn buffers(&self) -> &[Buffer<N>] {
75 unsafe { std::slice::from_raw_parts(self.buffers_ptr, self.buffers_len) }
79 }
80}
81
82unsafe 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}