Skip to main content

rill_core/macros/
sink.rs

1//! # Macro for creating active receivers (Sink)
2
3/// Creates an active signal receiver
4#[macro_export]
5macro_rules! sink_node {
6    (
7        $(#[$meta:meta])*
8        $vis:vis $struct_name:ident<$T:ident: $signal_num:path, const $BUF:ident: usize>
9        $(where $($bounds:tt)*)?
10        {
11            params { $($param_name:ident: $param_ty:ty = $param_default:expr),* $(,)? }
12            $(ports { $($ports:tt)* } )?
13            consume: $consume:expr
14        }
15    ) => {
16        #[derive(Debug)]
17        $vis struct $struct_name<$T: $signal_num, const $BUF: usize>
18        $(where $($bounds)*)?
19        {
20            state: $crate::traits::node::NodeState<T,$BUF>,
21            id: $crate::NodeId,
22            metadata: $crate::NodeMetadata,
23            inputs: Vec<$crate::Port<$T, $BUF>>,
24            $(
25                pub $param_name: $param_ty,
26            )*
27        }
28
29        impl<$T: $signal_num, const $BUF: usize>
30            $struct_name<$T, $BUF>
31        $(where $($bounds)*)?
32        {
33            pub fn new(sample_rate: f32) -> Self {
34                let metadata = $crate::NodeMetadata::new(
35                    stringify!($struct_name),
36                    $crate::NodeCategory::Sink,
37                );
38
39                let mut node = Self {
40                    state: $crate::traits::node::NodeState::new(sample_rate),
41                    id: $crate::NodeId(0),
42                    metadata,
43                    inputs: Vec::new(),
44                    $(
45                        $param_name: $param_default,
46                    )*
47                };
48
49                $(
50                    __init_ports!(ports { $($ports)* }, node, inputs)
51                )?;
52
53                node
54            }
55
56        }
57
58        impl<$T: $signal_num, const $BUF: usize>
59            $crate::Node<$T, $BUF> for $struct_name<$T, $BUF>
60        $(where $($bounds)*)?
61        {
62            fn node_type_id(&self) -> $crate::NodeTypeId
63            where
64                Self: 'static + Sized
65            {
66                $crate::NodeTypeId::of::<Self>()
67            }
68
69            fn id(&self) -> $crate::NodeId {
70                self.id
71            }
72
73            fn set_id(&mut self, id: $crate::NodeId) {
74                self.id = id;
75            }
76
77            fn metadata(&self) -> $crate::NodeMetadata {
78                self.metadata.clone()
79            }
80
81            fn init(&mut self, sample_rate: f32) {
82                self.state.sample_rate = sample_rate;
83            }
84
85            fn reset(&mut self) {
86                self.state.sample_pos = 0;
87                self.state.blocks_processed = 0;
88            }
89
90            fn get_parameter(&self, id: &$crate::ParameterId) -> Option<$crate::ParamValue> {
91                let name = id.as_str();
92                match name {
93                    $(
94                        stringify!($param_name) => Some($crate::ParamValue::Float(
95                            <_ as $crate::math::Transcendental>::to_f32(self.$param_name)
96                        )),
97                    )*
98                    _ => None,
99                }
100            }
101
102            fn set_parameter(&mut self, id: &$crate::ParameterId, value: $crate::ParamValue) -> $crate::ProcessResult<()> {
103                let name = id.as_str();
104                if let Some(v) = value.as_f32() {
105                    match name {
106                        $(
107                            stringify!($param_name) => {
108                                self.$param_name = $crate::math::Transcendental::from_f32(v);
109                                Ok(())
110                            },
111                        )*
112                        _ => Err($crate::ProcessError::parameter(format!("Unknown parameter: {}", name))),
113                    }
114                } else {
115                    Err($crate::ProcessError::parameter("Expected float value"))
116                }
117            }
118
119            fn input_port(&self, index: usize) -> Option<&$crate::Port<$T, $BUF>> {
120                self.inputs.get(index)
121            }
122
123            fn input_port_mut(&mut self, index: usize) -> Option<&mut $crate::Port<$T, $BUF>> {
124                self.inputs.get_mut(index)
125            }
126
127            fn output_port(&self, _index: usize) -> Option<&$crate::Port<$T, $BUF>> {
128                None
129            }
130
131            fn output_port_mut(&mut self, _index: usize) -> Option<&mut $crate::Port<$T, $BUF>> {
132                None
133            }
134
135            fn control_port(&self, _index: usize) -> Option<&$crate::Port<$T, $BUF>> {
136                None
137            }
138
139            fn control_port_mut(&mut self, _index: usize) -> Option<&mut $crate::Port<$T, $BUF>> {
140                None
141            }
142
143            fn num_inputs(&self) -> usize {
144                self.inputs.len()
145            }
146
147            fn num_outputs(&self) -> usize { 0 }
148
149            fn state(&self) -> &$crate::traits::node::NodeState<T,$BUF> {
150                &self.state
151            }
152
153            fn state_mut(&mut self) -> &mut $crate::traits::node::NodeState<T,$BUF> {
154                &mut self.state
155            }
156        }
157
158        impl<$T: $signal_num, const $BUF: usize>
159            $crate::Sink<$T, $BUF> for $struct_name<$T, $BUF>
160        $(where $($bounds)*)?
161        {
162            fn consume(
163                &mut self,
164                _clock: &$crate::ClockTick,
165                _signal_inputs: &[&[$T; $BUF]],
166                _control_inputs: &[$T],
167                _clock_inputs: &[$crate::ClockTick],
168                _feedback_inputs: &[&[$T; $BUF]],
169            ) -> $crate::ProcessResult<()> {
170                ($consume)(self)?;
171                Ok(())
172            }
173        }
174    };
175}