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
// 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.

use crate::dsp::{
    DspNode, GraphFun, LedPhaseVals, NodeContext, NodeGlobalRef, NodeId, ProcBuf, SAtom,
};
use crate::nodes::{NodeAudioContext, NodeExecContext};
use std::sync::Arc;
use synfx_dsp::AtomicFloat;
use triple_buffer::{Input, Output, TripleBuffer};

/// A context structure for supporting the [DynamicNode1x1::process] function.
///
/// It provides access to the input slices of the `alpha`, `beta`, `gamma` and `delta`
/// values. And to the LED and phase values, which are basically two [AtomicFloat]
/// values that you can read out in the frontend thread using the [crate::SynthConstructor]
/// or [crate::NodeConfigurator] API.
pub struct DynNode1x1Context {
    nframes: usize,
    alpha: ProcBuf,
    beta: ProcBuf,
    gamma: ProcBuf,
    delta: ProcBuf,
    led_value: Arc<AtomicFloat>,
    phase_value: Arc<AtomicFloat>,
}

impl DynNode1x1Context {
    pub fn alpha_slice(&self) -> &[f32] {
        self.alpha.slice(self.nframes)
    }

    pub fn beta_slice(&self) -> &[f32] {
        self.beta.slice(self.nframes)
    }

    pub fn gamma_slice(&self) -> &[f32] {
        self.gamma.slice(self.nframes)
    }

    pub fn delta_slice(&self) -> &[f32] {
        self.delta.slice(self.nframes)
    }

    pub fn led_value(&self) -> &Arc<AtomicFloat> {
        &self.led_value
    }
    pub fn phase_value(&self) -> &Arc<AtomicFloat> {
        &self.phase_value
    }
}

/// This trait allows you to write custom HexoDSP nodes in Rust and provide them
/// at runtime using [crate::NodeConfigurator::set_dynamic_node1x1] or [crate::SynthConstructor::set_dynamic_node1x1].
///
/// The 1x1 means there is one dedicated input signal and one dedicated output signal.
/// The input signal is accompanied with 4 auxiliary signals using the [DynNode1x1Context]
/// structure.
///
/// There is a trait implementation for `FnMut(&[f32], &mut [f32], &DynNode1x1Context)` functions,
/// which means you don't have to implement a full structure yourself and can just pass in
/// closures to [crate::SynthConstructor::set_dynamic_node1x1]:
///
///```
/// use hexodsp::{SynthConstructor, DynamicNode1x1, DynNode1x1Context};
/// use hexodsp::build::*;
///
/// let mut sc = SynthConstructor::new();
///
/// sc.set_dynamic_node1x1(0, Box::new(|inp: &[f32], out: &mut [f32], ctx: &DynNode1x1Context| {
///     // Your code here!
/// }));
///
/// sc.upload(&out(0).input().ch1(&rust1x1(0).output().sig()));
///```
/// See also: [crate::SynthConstructor::set_dynamic_node1x1] for a more detailed example.
pub trait DynamicNode1x1: Send {
    /// The sample rate function sets the sample rate the DSP graph is currently running at.
    fn set_sample_rate(&mut self, _sample_rate: f32) {}
    /// This is called whenever the DSP in the audio thread is resetted.
    fn reset(&mut self) {}
    /// You implement this method with your own custom DSP code.
    fn process(&mut self, _input: &[f32], _output: &mut [f32], _ctx: &DynNode1x1Context);
}

impl<T> crate::dsp::DynamicNode1x1 for T
where
    T: FnMut(&[f32], &mut [f32], &DynNode1x1Context) + Send,
{
    fn process(&mut self, input: &[f32], output: &mut [f32], ctx: &DynNode1x1Context) {
        (self)(input, output, ctx)
    }
}

struct RustDummyNode {}

pub fn new_dummy_dynamic_node1x1() -> Box<dyn DynamicNode1x1> {
    Box::new(RustDummyNode {})
}

impl DynamicNode1x1 for RustDummyNode {
    fn process(&mut self, _input: &[f32], output: &mut [f32], _ctx: &DynNode1x1Context) {
        for o in output.iter_mut() {
            *o = 0.0;
        }
    }
}

pub struct DynNodeBuffer<T: Send> {
    output: Output<T>,
}

impl<T> DynNodeBuffer<T>
where
    T: Send,
{
    #[inline]
    pub fn access(&mut self) -> &mut T {
        self.output.update();
        self.output.output_buffer()
    }
}

pub struct DynNodeHandle<T: Send + Default> {
    input: Input<T>,
    output: Option<Output<T>>,
}

impl Default for Box<dyn DynamicNode1x1> {
    fn default() -> Self {
        new_dummy_dynamic_node1x1()
    }
}

impl<T> DynNodeHandle<T>
where
    T: Send + Default,
{
    pub fn new() -> Self {
        let (input, output) = TripleBuffer::default().split();
        Self { input, output: Some(output) }
    }

    pub fn write(&mut self, node: T) {
        self.input.write(node);
    }

    pub fn get_output_buffer(&mut self) -> DynNodeBuffer<T> {
        let output = if let Some(output) = self.output.take() {
            output
        } else {
            let (input, output) = TripleBuffer::default().split();
            self.input = input;
            output
        };
        DynNodeBuffer { output }
    }
}

impl std::fmt::Debug for Rust1x1 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Rust1x1()")
    }
}

/// A native Rust code node that uses trait objects for dispatch
pub struct Rust1x1 {
    buffer: DynNodeBuffer<Box<dyn DynamicNode1x1>>,
}

impl Rust1x1 {
    pub fn new(nid: &NodeId, node_global: &NodeGlobalRef) -> Self {
        let buffer = if let Ok(mut handle) = node_global.lock() {
            handle.get_dynamic_node1x1_buffer(nid.instance() as usize)
        } else {
            let mut handle = DynNodeHandle::<Box<dyn DynamicNode1x1>>::new();
            handle.get_output_buffer()
        };
        Self { buffer }
    }

    pub const inp: &'static str =
        "Signal input. Signal input to the dynamically dispatched Rust node.";
    pub const alpha: &'static str = "Alpha parameter for the dynamically dispatched Rust node.";
    pub const beta: &'static str = "Beta parameter for the dynamically dispatched Rust node.";
    pub const gamma: &'static str = "Gamma parameter for the dynamically dispatched Rust node.";
    pub const delta: &'static str = "Delta parameter for the dynamically dispatched Rust node.";
    pub const sig: &'static str =
        "Signal output. Signal output of the dynamically dispatched Rust node.";
    pub const DESC: &'static str = r#"Rust Code Node

This node does provide the user of HexoDSP or the SynthConstructor with an API
to code custom DSP node implementations in pure Rust at compile time.
It does not have any relevance for HexoSynth.
See also [crate::SynthConstructor] and [crate::DynamicNode1x1].
"#;
    pub const HELP: &'static str = r#"Rust Code Node

This node does provide the user of HexoDSP or the SynthConstructor with an API
to code custom DSP node implementations in pure Rust at compile time.

Treat this node as plugin API into the HexoDSP DSP graph.

This node does nothing in HexoSynth.

See also [crate::SynthConstructor] and [crate::DynamicNode1x1].
"#;

    pub fn graph_fun() -> Option<GraphFun> {
        None
    }
}

impl DspNode for Rust1x1 {
    fn set_sample_rate(&mut self, _srate: f32) {}

    fn reset(&mut self) {}

    #[inline]
    fn process(
        &mut self,
        ctx: &mut dyn NodeAudioContext,
        _ectx: &mut NodeExecContext,
        _nctx: &NodeContext,
        _atoms: &[SAtom],
        inputs: &[ProcBuf],
        outputs: &mut [ProcBuf],
        ctx_vals: LedPhaseVals,
    ) {
        use crate::dsp::{inp, inp_buf, out};

        let inp = inp::Rust1x1::inp(inputs);
        let out = out::Rust1x1::sig(outputs);

        let n1x1ctx = DynNode1x1Context {
            nframes: ctx.nframes(),
            alpha: inp_buf::Rust1x1::alpha(inputs),
            beta: inp_buf::Rust1x1::beta(inputs),
            gamma: inp_buf::Rust1x1::gamma(inputs),
            delta: inp_buf::Rust1x1::delta(inputs),
            led_value: ctx_vals[0].clone(),
            phase_value: ctx_vals[1].clone(),
        };

        self.buffer.access().process(
            inp.slice(ctx.nframes()),
            out.slice_mut(ctx.nframes()),
            &n1x1ctx,
        );
    }
}