1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (c) 2022 Weird Constructor <weirdconstructor@gmail.com>
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
// See README.md and COPYING for details.

/*! Node Reference and Graph Builder for [crate::SynthConstructor]

This module contains function bindings for all DSP nodes that are included
in HexoDSP. All nodes, their inputs, settings and outputs are accessible via this API.
You can write type safe and compile time checked graphs for HexoDSP using this API.

Below you will find a comprehensive reference of all HexoDSP nodes, their
parameters and some of the possible values.

## HexoDSP DSP Node Reference
*/
#![allow(rustdoc::invalid_rust_codeblocks)]
#![doc = include_str!("dsp_nodes_ref.md")]

use crate::node_list;

macro_rules! make_node_constructor {
    ($s1: ident => $v1: ident,
        $($str: ident => $variant: ident
            UIType:: $gui_type: ident
            UICategory:: $ui_cat: ident
            $(($in_idx: literal $para: ident
               $n_fun: ident $d_fun: ident $r_fun: ident $f_fun: ident
               $steps: ident $min: expr, $max: expr, $def: expr))*
            $({$in_at_idx: literal $at_idx: literal $atom: ident
               $at_fun: ident ($at_init: expr) $at_ui: ident $fa_fun: ident
               $amin: literal $amax: literal})*
            $([$out_idx: literal $out: ident])*
            ,)+
    ) => {
        use std::rc::Rc;
        use std::cell::RefCell;

        #[derive(Debug, Clone, PartialEq)]
        pub enum ConstructorOp {
            SetDenorm(String, f32),
            SetDenormModAmt(String, f32, f32),
            SetSetting(String, i64),
            Input(String, ConstructorNode, String),
        }

        #[derive(Clone)]
        pub struct ConstructorNode {
            pub node_id: crate::dsp::NodeId,
            pub ops: Rc<RefCell<Vec<crate::build::ConstructorOp>>>,
        }

        impl PartialEq for ConstructorNode {
            fn eq(&self, other: &Self) -> bool {
                self.node_id == other.node_id
            }
        }

        impl std::fmt::Display for ConstructorNode {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", stringify!(self.node_id))
            }
        }

        impl std::fmt::Debug for ConstructorNode {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                let node_id_str = format!("[{}]", self.node_id);
                for op in self.ops.borrow().iter() {
                    match op {
                        ConstructorOp::SetDenorm(port, v) => {
                            f.debug_tuple(&node_id_str).field(port).field(v).finish()?;
                        },
                        ConstructorOp::SetDenormModAmt(port, v, ma) => {
                            f.debug_tuple(&node_id_str).field(port).field(v).field(ma).finish()?;
                        },
                        ConstructorOp::SetSetting(port, v) => {
                            f.debug_tuple(&node_id_str).field(port).field(v).finish()?;
                        },
                        ConstructorOp::Input(port, constr, output) => {
                            f.debug_struct(&node_id_str).field("port", port).field("output", output).field("input", constr).finish()?;
                        },
                    }
                }
                writeln!(f, "")
            }
        }

        pub trait ConstructorNodeBuilder {
            fn id(&self) -> crate::dsp::NodeId;
            fn build(&self) -> crate::build::ConstructorNode;
        }

        pub trait ConstructorNodeOutputPort: ConstructorNodeBuilder {
            fn port(&self) -> (ConstructorNode, String);
        }

        pub mod output_port {
            $(
                #[doc = concat!("Output Port API Struct for [Node ", stringify!($variant), "](../index.html#nodeid", stringify!($str), ")")]
                #[derive(Debug, Clone)]
                pub struct $variant {
                    pub node_id: crate::dsp::NodeId,
                    pub node: crate::build::ConstructorNode,
                    pub port: Option<String>
                }

                impl super::ConstructorNodeOutputPort for $variant {
                    fn port(&self) -> (super::ConstructorNode, String) {
                        (self.node.clone(), self.port.clone().unwrap_or_else(|| "".to_string()))
                    }
                }

                impl super::ConstructorNodeBuilder for $variant {
                    fn id(&self) -> crate::dsp::NodeId {
                        self.node_id
                    }

                    fn build(&self) -> crate::build::ConstructorNode {
                        self.node.clone()
                    }
                }

                impl $variant {
                    $(
                        #[doc = concat!("See also [Node ", stringify!($variant), "](../index.html#nodeid", stringify!($str), ") about details to this output")]
                        pub fn $out(mut self) -> Self {
                            self.port = Some(stringify!($out).to_string());
                            self
                        }
                    )*
                }
            )*
        }

        pub mod input_port {
            $(
                #[doc = concat!("Input Port API Struct for [Node ", stringify!($variant), "](../index.html#nodeid", stringify!($str), ")")]
                pub struct $variant { pub node: super::$variant }
                impl $variant {
                    $(
                        #[doc = concat!("Assign to input port for [Node ", stringify!($variant), " Input ", stringify!($para),"](../index.html#nodeid", stringify!($str), "-input-", stringify!($para),")")]
                        pub fn $para(self, node: &dyn super::ConstructorNodeOutputPort) -> super::$variant {
                            let (node, portname) = node.port();

                            if !portname.is_empty() {
                                self.node.ops.borrow_mut().push(
                                    super::ConstructorOp::Input(
                                        stringify!($para).to_string(), node, portname));
                            }

                            self.node
                        }
                    )*
                }
            )*
        }

        pub mod set_param {
            $(
                #[doc = concat!("Parameter Setter API Struct for [Node ", stringify!($variant), "](../index.html#nodeid", stringify!($str), ")")]
                pub struct $variant { pub node: super::$variant }
                impl $variant {
                    $(
                        #[doc = concat!("Set input parameter for [Node ", stringify!($variant), " Input ", stringify!($para),"](../index.html#nodeid", stringify!($str), "-input-", stringify!($para),")")]
                        pub fn $para(self, v: f32) -> super::$variant {
                            self.node.ops.borrow_mut().push(
                                super::ConstructorOp::SetDenorm(
                                    stringify!($para).to_string(), v));
                            self.node
                        }
                    )*
                    $(
                        #[doc = concat!("Set setting for [Node ", stringify!($variant), " Setting ", stringify!($atom),"](../index.html#nodeid", stringify!($str), "-setting-", stringify!($atom),")")]
                        pub fn $atom(self, v: i64) -> super::$variant {
                            self.node.ops.borrow_mut().push(
                                super::ConstructorOp::SetSetting(
                                    stringify!($atom).to_string(), v));
                            self.node
                        }
                    )*
                }
            )*
        }

        pub mod set_param_mod {
            $(
                #[doc = concat!("Parameter and Modulation Amount Setter API Struct for [Node ", stringify!($variant), "](../index.html#nodeid", stringify!($str), ")")]
                pub struct $variant { pub node: super::$variant }
                impl $variant {
                    $(
                        #[doc = concat!("Set input parameter and modulation amount for [Node ", stringify!($variant), " Input ", stringify!($para),"](../index.html#nodeid", stringify!($str), "-input-", stringify!($para),")")]
                        pub fn $para(self, v: f32, ma: f32) -> super::$variant {
                            self.node.ops.borrow_mut().push(
                                super::ConstructorOp::SetDenormModAmt(
                                    stringify!($para).to_string(), v, ma));
                            self.node
                        }
                    )*
                }
            )*
        }

        $(
            #[derive(Debug, Clone)]
            #[doc = concat!("Build API Struct for [Node ", stringify!($variant), "](../index.html#nodeid", stringify!($str), ")")]
            pub struct $variant {
                node_id: crate::dsp::NodeId,
                ops: Rc<RefCell<Vec<ConstructorOp>>>,
            }

            impl $variant {
                /// Returns a structure with all settable parameters and settings.
                #[doc = concat!("See also [Node ", stringify!($variant), "](../index.html#nodeid", stringify!($str), ")")]
                pub fn set(self) -> set_param::$variant {
                    set_param::$variant { node: self }
                }

                /// Returns a structure with all settable parameters with modulation amount.
                #[doc = concat!("See also [Node ", stringify!($variant), "](../index.html#nodeid", stringify!($str), ")")]
                pub fn set_mod(self) -> set_param_mod::$variant {
                    set_param_mod::$variant { node: self }
                }

                /// Returns a structure with all input ports/parameters.
                #[doc = concat!("See also [Node ", stringify!($variant), "](../index.html#nodeid", stringify!($str), ")")]
                pub fn input(self) -> input_port::$variant {
                    input_port::$variant { node: self }
                }

                /// Returns a structure with all output ports/paramters.
                #[doc = concat!("See also [Node ", stringify!($variant), "](../index.html#nodeid", stringify!($str), ")")]
                pub fn output(&self) -> output_port::$variant {
                    output_port::$variant {
                        node_id: self.id(),
                        node: self.build(),
                        port: None
                    }
                }
            }

            #[doc = concat!("Build API Function for [Node ", stringify!($variant), "](../index.html#nodeid", stringify!($str), ")")]
            pub fn $str(instance: u8) -> crate::build::$variant {
                $variant {
                    node_id: crate::dsp::NodeId::$variant(instance),
                    ops: Rc::new(RefCell::new(vec![])),
                }
            }

            impl ConstructorNodeBuilder for $variant {
                fn id(&self) -> crate::dsp::NodeId {
                    self.node_id
                }

                fn build(&self) -> crate::build::ConstructorNode {
                    ConstructorNode {
                        node_id: self.node_id,
                        ops: self.ops.clone()
                    }
                }
            }
        )+
    }
}

node_list! {make_node_constructor}