Skip to main content

rill_core/macros/
sink.rs

1//! # Макрос для создания активных приёмников (Sink)
2
3/// Создаёт активный приёмник сигнала
4#[macro_export]
5macro_rules! sink_node {
6    (
7        $(#[$meta:meta])*
8        $vis:vis $struct_name:ident<$T:ident: $audio_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: $audio_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: $audio_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            pub fn sample_rate(&self) -> f32 {
57                self.state.sample_rate
58            }
59        }
60
61        impl<$T: $audio_num, const $BUF: usize>
62            $crate::SignalNode<$T, $BUF> for $struct_name<$T, $BUF>
63        $(where $($bounds)*)?
64        {
65            fn node_type_id(&self) -> $crate::NodeTypeId
66            where
67                Self: 'static + Sized
68            {
69                $crate::NodeTypeId::of::<Self>()
70            }
71
72            fn id(&self) -> $crate::NodeId {
73                self.id
74            }
75
76            fn set_id(&mut self, id: $crate::NodeId) {
77                self.id = id;
78            }
79
80            fn metadata(&self) -> $crate::NodeMetadata {
81                self.metadata.clone()
82            }
83
84            fn init(&mut self, sample_rate: f32) {
85                self.state.sample_rate = sample_rate;
86            }
87
88            fn reset(&mut self) {
89                self.state.sample_pos = 0;
90                self.state.blocks_processed = 0;
91            }
92
93            fn get_parameter(&self, id: &$crate::ParameterId) -> Option<$crate::ParamValue> {
94                let name = id.as_str();
95                match name {
96                    $(
97                        stringify!($param_name) => Some($crate::ParamValue::Float(
98                            <_ as $crate::math::Transcendental>::to_f32(self.$param_name)
99                        )),
100                    )*
101                    _ => None,
102                }
103            }
104
105            fn set_parameter(&mut self, id: &$crate::ParameterId, value: $crate::ParamValue) -> $crate::ProcessResult<()> {
106                let name = id.as_str();
107                if let Some(v) = value.as_f32() {
108                    match name {
109                        $(
110                            stringify!($param_name) => {
111                                self.$param_name = $crate::math::Transcendental::from_f32(v);
112                                Ok(())
113                            },
114                        )*
115                        _ => Err($crate::ProcessError::parameter(format!("Unknown parameter: {}", name))),
116                    }
117                } else {
118                    Err($crate::ProcessError::parameter("Expected float value"))
119                }
120            }
121
122            fn input_port(&self, index: usize) -> Option<&$crate::Port<$T, $BUF>> {
123                self.inputs.get(index)
124            }
125
126            fn input_port_mut(&mut self, index: usize) -> Option<&mut $crate::Port<$T, $BUF>> {
127                self.inputs.get_mut(index)
128            }
129
130            fn output_port(&self, _index: usize) -> Option<&$crate::Port<$T, $BUF>> {
131                None
132            }
133
134            fn output_port_mut(&mut self, _index: usize) -> Option<&mut $crate::Port<$T, $BUF>> {
135                None
136            }
137
138            fn control_port(&self, _index: usize) -> Option<&$crate::Port<$T, $BUF>> {
139                None
140            }
141
142            fn control_port_mut(&mut self, _index: usize) -> Option<&mut $crate::Port<$T, $BUF>> {
143                None
144            }
145
146            fn num_inputs(&self) -> usize {
147                self.inputs.len()
148            }
149
150            fn num_outputs(&self) -> usize { 0 }
151
152            fn state(&self) -> &$crate::traits::node::NodeState<T,$BUF> {
153                &self.state
154            }
155
156            fn state_mut(&mut self) -> &mut $crate::traits::node::NodeState<T,$BUF> {
157                &mut self.state
158            }
159        }
160
161        impl<$T: $audio_num, const $BUF: usize>
162            $crate::Sink<$T, $BUF> for $struct_name<$T, $BUF>
163        $(where $($bounds)*)?
164        {
165            fn consume(
166                &mut self,
167                clock: &$crate::ClockTick,
168                signal_inputs: &[&[$T; $BUF]],
169                control_inputs: &[$T],
170                clock_inputs: &[$crate::ClockTick],
171                feedback_inputs: &[&[$T; $BUF]],
172            ) -> $crate::ProcessResult<()> {
173                ($consume)(self)?;
174                Ok(())
175            }
176        }
177    };
178}