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
use crate::modulators::Modulator;
use crate::*;
use alloc::boxed::Box;
use alloc::vec::Vec;
use micromath::F32Ext;
/// A modulator connected to a parameter of a node.
struct WiredModulator {
param: u8,
modulator: Box<dyn Modulator>,
low: f32,
range: f32,
/// The modulator-specific timer.
///
/// Starts at zero (when the modulator is wired).
time: u32,
}
pub struct Node {
children: Vec<Self>,
proc: Box<dyn Processor>,
modulator: Option<WiredModulator>,
}
impl Node {
pub(crate) fn new_root(volume: f32) -> Self {
Self {
children: Vec::new(),
proc: Box::new(Gain::new(volume)),
modulator: None,
}
}
/// Add a child node.
pub(crate) fn add(&mut self, proc: Box<dyn Processor>) -> Result<u8, NodeError> {
if self.children.len() >= 4 {
return Err(NodeError::TooManyChildren);
}
#[expect(clippy::cast_possible_truncation)]
let child_id = self.children.len() as u8;
let child = Self {
children: Vec::new(),
modulator: None,
proc,
};
self.children.push(child);
Ok(child_id)
}
pub(crate) fn get_node(&mut self, path: &[u8]) -> &mut Self {
let Some(first) = path.first() else {
return self;
};
let node = &mut self.children[*first as usize];
node.get_node(&path[1..])
}
pub(crate) fn next_frame(&mut self) -> Option<Frame> {
if let Some(modulator) = self.modulator.as_mut() {
let val = modulator.modulator.get(modulator.time * 8);
let val = F32Ext::mul_add(val, modulator.range, modulator.low);
self.proc.set(modulator.param, val);
modulator.time += 1;
}
self.proc.process_children(&mut self.children)
}
pub(crate) fn clear(&mut self) {
self.children.clear();
}
/// Reset the current node (processor and modulator) to the initial state.
pub fn reset(&mut self) {
self.proc.reset();
if let Some(modulator) = self.modulator.as_mut() {
modulator.time = 0;
}
}
/// Do [`Node::reset`] on the current node and all its children (recursively).
pub fn reset_all(&mut self) {
self.reset();
for node in &mut self.children {
node.reset_all();
}
}
/// Set the value of a parameter.
///
/// The parameter IDs and values are the same as in [`Node::modulate`].
///
/// **USE WITH CAUTION:** Parameters set this way may or may not be reset
/// when calling [`Node::reset`]. If the node has a modulator set for
/// the same parameter, the modulator is removed.
pub fn set(&mut self, param: u8, val: f32) {
if let Some(modulator) = &self.modulator
&& modulator.param == param
{
self.modulator = None;
}
self.proc.set(param, val);
}
/// Set modulator for the given parameter.
///
/// The `low` is the lowest value produced by the modulator
/// and `high` is the highest.
///
/// If `low` is smaller than `high`, the modulator output is inversed.
/// For example, modulating [`Pause`] with `low=0` and `high=1`
/// will make it first paused and then playing while modulating it with
/// `low=1` and `high=0` will first have the node playing and then paused.
pub fn modulate(&mut self, param: u8, lfo: Box<dyn Modulator>, low: f32, high: f32) {
let modulator = WiredModulator {
param,
modulator: lfo,
time: 0,
low,
range: high - low,
};
self.modulator = Some(modulator);
}
}