rill_core/macros/
processor.rs1#[macro_export]
5macro_rules! processor_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 process: $process: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 outputs: Vec<$crate::Port<$T, $BUF>>,
25 controls: Vec<$crate::Port<$T, $BUF>>,
26 $(
27 pub $param_name: $param_ty,
28 )*
29 }
30
31 impl<$T: $signal_num, const $BUF: usize>
32 $struct_name<$T, $BUF>
33 $(where $($bounds)*)?
34 {
35 pub fn new(sample_rate: f32) -> Self {
36 let metadata = $crate::NodeMetadata::new(
37 stringify!($struct_name),
38 $crate::NodeCategory::Processor,
39 );
40
41 let mut node = Self {
42 state: $crate::traits::node::NodeState::new(sample_rate),
43 id: $crate::NodeId(0),
44 metadata,
45 inputs: Vec::new(),
46 outputs: Vec::new(),
47 controls: Vec::new(),
48 $(
49 $param_name: $param_default,
50 )*
51 };
52
53 $(
54 __init_ports!(ports { $($ports)* }, node, inputs, outputs, controls)
55 )?;
56
57 node
58 }
59
60 }
61
62 impl<$T: $signal_num, const $BUF: usize>
63 $crate::SignalNode<$T, $BUF> for $struct_name<$T, $BUF>
64 $(where $($bounds)*)?
65 {
66 fn node_type_id(&self) -> $crate::NodeTypeId
67 where
68 Self: 'static + Sized
69 {
70 $crate::NodeTypeId::of::<Self>()
71 }
72
73 fn id(&self) -> $crate::NodeId {
74 self.id
75 }
76
77 fn set_id(&mut self, id: $crate::NodeId) {
78 self.id = id;
79 }
80
81 fn metadata(&self) -> $crate::NodeMetadata {
82 self.metadata.clone()
83 }
84
85 fn init(&mut self, sample_rate: f32) {
86 self.state.sample_rate = sample_rate;
87 }
88
89 fn reset(&mut self) {
90 self.state.sample_pos = 0;
91 self.state.blocks_processed = 0;
92 }
93
94 fn get_parameter(&self, id: &$crate::ParameterId) -> Option<$crate::ParamValue> {
95 let name = id.as_str();
96 match name {
97 $(
98 stringify!($param_name) => Some($crate::ParamValue::Float(
99 <_ as $crate::math::Transcendental>::to_f32(self.$param_name)
100 )),
101 )*
102 _ => None,
103 }
104 }
105
106 fn set_parameter(&mut self, id: &$crate::ParameterId, value: $crate::ParamValue) -> $crate::ProcessResult<()> {
107 let name = id.as_str();
108 if let Some(v) = value.as_f32() {
109 match name {
110 $(
111 stringify!($param_name) => {
112 self.$param_name = $crate::math::Transcendental::from_f32(v);
113 Ok(())
114 },
115 )*
116 _ => Err($crate::ProcessError::parameter(format!("Unknown parameter: {}", name))),
117 }
118 } else {
119 Err($crate::ProcessError::parameter("Expected float value"))
120 }
121 }
122
123 fn input_port(&self, index: usize) -> Option<&$crate::Port<$T, $BUF>> {
124 self.inputs.get(index)
125 }
126
127 fn input_port_mut(&mut self, index: usize) -> Option<&mut $crate::Port<$T, $BUF>> {
128 self.inputs.get_mut(index)
129 }
130
131 fn output_port(&self, index: usize) -> Option<&$crate::Port<$T, $BUF>> {
132 self.outputs.get(index)
133 }
134
135 fn output_port_mut(&mut self, index: usize) -> Option<&mut $crate::Port<$T, $BUF>> {
136 self.outputs.get_mut(index)
137 }
138
139 fn control_port(&self, index: usize) -> Option<&$crate::Port<$T, $BUF>> {
140 self.controls.get(index)
141 }
142
143 fn control_port_mut(&mut self, index: usize) -> Option<&mut $crate::Port<$T, $BUF>> {
144 self.controls.get_mut(index)
145 }
146
147 fn num_inputs(&self) -> usize {
148 self.inputs.len()
149 }
150
151 fn num_outputs(&self) -> usize {
152 self.outputs.len()
153 }
154
155 fn state(&self) -> &$crate::traits::node::NodeState<T,$BUF> {
156 &self.state
157 }
158
159 fn state_mut(&mut self) -> &mut $crate::traits::node::NodeState<T,$BUF> {
160 &mut self.state
161 }
162 }
163
164 impl<$T: $crate::math::Transcendental, const $BUF: usize>
165 $crate::Processor<$T, $BUF> for $struct_name<$T, $BUF>
166 $(where $($bounds)*)?
167 {
168 fn process(
169 &mut self,
170 _clock: &$crate::ClockTick,
171 _signal_inputs: &[&[$T; $BUF]],
172 _control_inputs: &[$T],
173 _clock_inputs: &[$crate::ClockTick],
174 _feedback_inputs: &[&[$T; $BUF]],
175 ) -> $crate::ProcessResult<()> {
176 ($process)(self)?;
177 Ok(())
178 }
179
180 fn latency(&self) -> usize {
181 0
182 }
183 }
184 };
185}