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
use super::*;
use std::sync::Arc;
use std::collections::VecDeque;
use petgraph::graph;
pub type Graph = graph::DiGraph<BlockSP, ()>;
pub type GraphNode = graph::NodeIndex;
pub type ProcessOrder = VecDeque<GraphNode>;
#[derive(Clone)]
pub struct ComplexSound {
graph: Graph,
process_order: ProcessOrder,
input_gain: GraphNode,
output_gain: GraphNode,
id: Option<usize>,
is_muted: bool,
is_paused: bool
}
impl ComplexSound {
pub fn new(input_gain: MathT, output_gain: MathT) -> Self {
let mut graph = Graph::new();
let input_gain = graph.add_node(
Arc::new(
StandardBlock::from_modifier(
modifiers::Gain::new(input_gain as SampleT)
)
)
);
let output_gain = graph.add_node(
Arc::new(
StandardBlock::from_modifier(
modifiers::Gain::new(output_gain as SampleT)
)
)
);
ComplexSound {
graph,
process_order: ProcessOrder::new(),
input_gain,
output_gain,
id: None,
is_muted: false,
is_paused: false
}
}
pub fn get_input_gain(&self) -> GraphNode {
self.input_gain
}
pub fn get_output_gain(&self) -> GraphNode {
self.output_gain
}
pub fn add_block(&mut self, block: BlockSP) -> GraphNode {
self.graph.add_node(block)
}
pub fn add_connection(&mut self, from: GraphNode, to: GraphNode) {
self.graph.update_edge(from, to, ());
self.process_order();
}
pub fn remove_connection(&mut self, from: GraphNode, to: GraphNode) {
if let Some(e) = self.graph.find_edge(from, to) {
self.graph.remove_edge(e);
}
self.process_order();
}
pub fn get_nodes(&self) -> ProcessOrder {
self.process_order.clone()
}
fn process_order(&mut self) {
self.process_order.clear();
self.process_order.extend(self.graph.externals(petgraph::Direction::Incoming));
let mut i = 0;
while i < self.process_order.len() {
let neighbors = self.graph.neighbors(self.process_order[i]);
self.process_order.extend(neighbors);
let mut j = 0;
while j < i {
Self::remove_dups(&mut self.process_order, j);
j += 1;
}
i += 1;
}
let ig = self.input_gain;
let og = self.output_gain;
self.process_order.retain(|e| *e != ig && *e != og);
self.process_order.push_front(self.input_gain);
self.process_order.push_back(self.output_gain);
}
fn remove_dups(v: &mut ProcessOrder, whitelist: usize) {
let mut i = whitelist + 1;
while i < v.len() {
if v[i] == v[whitelist] {
v.remove(i);
} else {
i += 1;
}
}
}
}
impl Sound for ComplexSound {
fn toggle_pause(&mut self) {
self.is_paused = !self.is_paused;
}
fn is_paused(&self) -> bool {
self.is_paused
}
fn toggle_mute(&mut self) {
self.is_muted = !self.is_muted;
}
fn is_muted(&self) -> bool {
self.is_muted
}
fn register(&mut self, id: usize) {
self.id = Some(id);
}
fn unregister(&mut self) {
self.id = None;
}
fn process(&mut self, input: SampleT) -> SampleT {
if self.is_paused {
return Default::default();
}
let mut out = Default::default();
BlockSP::get_mut(
self.graph.node_weight_mut(self.input_gain).unwrap()
).unwrap().prime_input(input);
for b in &self.process_order {
let block = BlockSP::get_mut(self.graph.node_weight_mut(*b).unwrap()).unwrap();
out = block.process();
let mut neighbors = self.graph.neighbors(*b).detach();
while let Some(t) = neighbors.next(&self.graph) {
BlockSP::get_mut(self.graph.node_weight_mut(t.1).unwrap()).unwrap().
prime_input(out);
}
}
if self.is_muted {
Default::default()
} else {
out
}
}
fn get_id(&self) -> Option<usize> {
self.id
}
}