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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#![cfg(feature = "dot")]
use fundsp::net::{Net, Source};
use fundsp::prelude::*;
use petgraph::dot::{Config, Dot};
use petgraph::graph::{DiGraph, NodeIndex};
use std::collections::HashMap;
/// Kind of node in the rendered graph.
#[derive(Clone, Debug)]
enum DspNodeKind {
Unit,
GlobalIn,
GlobalOut,
}
/// Node weight with kind and human-readable label.
#[allow(dead_code)]
#[derive(Clone, Debug)]
struct DspNode {
kind: DspNodeKind,
label: String,
// Optional unit id for unit nodes
unit_id: Option<NodeId>,
// Optional port index for global input/output nodes
port: Option<usize>,
}
/// Edge weight capturing port indices for labeling.
#[derive(Clone, Debug)]
struct DspEdge {
/// Source port index (unit output channel) if applicable.
src_port: Option<usize>,
/// Destination port index (unit input channel) if applicable.
dst_port: Option<usize>,
}
/// Build a petgraph DiGraph from a fundsp Net, including global inputs/outputs and edges.
/// Returns:
/// - The constructed graph.
/// - Mapping from fundsp NodeId to petgraph NodeIndex.
/// - The list of global input node indices.
/// - The list of global output node indices.
#[allow(clippy::type_complexity)]
fn build_petgraph(
net: &mut Net,
) -> (
DiGraph<DspNode, DspEdge>,
HashMap<NodeId, NodeIndex>,
Vec<NodeIndex>,
Vec<NodeIndex>,
) {
let mut g: DiGraph<DspNode, DspEdge> = DiGraph::new();
// Create global input nodes
let mut global_in_nodes = Vec::new();
for in_port in 0..net.inputs() {
let idx = g.add_node(DspNode {
kind: DspNodeKind::GlobalIn,
label: format!("IN[{}]", in_port),
unit_id: None,
port: Some(in_port),
});
global_in_nodes.push(idx);
}
// Create global output nodes
let mut global_out_nodes = Vec::new();
for out_port in 0..net.outputs() {
let idx = g.add_node(DspNode {
kind: DspNodeKind::GlobalOut,
label: format!("OUT[{}]", out_port),
unit_id: None,
port: Some(out_port),
});
global_out_nodes.push(idx);
}
// Collect ids to avoid mixed borrows, then sort deterministically by numeric NodeId value
let mut ids: Vec<NodeId> = net.ids().copied().collect();
// NodeId Debug format is `NodeId(n)`. Parse n to a number for stable sorting across runs.
ids.sort_by_key(|id| {
// Fallback to Debug string if parsing fails (shouldn't happen), to keep deterministic order.
let s = format!("{:?}", id);
if let Some(num) = s
.strip_prefix("NodeId(")
.and_then(|r| r.strip_suffix(')'))
.and_then(|r| r.parse::<u64>().ok())
{
num
} else {
// Hash the string into a u64 for a stable fallback order
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
s.hash(&mut hasher);
hasher.finish()
}
});
// Precompute unit display labels to avoid borrow conflicts later
let mut labels: HashMap<NodeId, String> = HashMap::new();
for &id in &ids {
let label = net.node_mut(id).display();
labels.insert(id, label);
}
// Create unit nodes
let mut id_to_idx: HashMap<NodeId, NodeIndex> = HashMap::new();
for &id in &ids {
let label = labels
.get(&id)
.cloned()
.unwrap_or_else(|| format!("Unit {:?}", id));
let idx = g.add_node(DspNode {
kind: DspNodeKind::Unit,
label,
unit_id: Some(id),
port: None,
});
id_to_idx.insert(id, idx);
}
// Prepare to collect and sort edges before insertion
let mut edges: Vec<(NodeIndex, NodeIndex, DspEdge)> = Vec::new();
// Edges for unit inputs: connect sources to each unit input channel
for &id in &ids {
let inputs = net.inputs_in(id);
for i in 0..inputs {
match net.source(id, i) {
Source::Local(src_id, src_port) => {
if let (Some(&src_idx), Some(&dst_idx)) =
(id_to_idx.get(&src_id), id_to_idx.get(&id))
{
edges.push((
src_idx,
dst_idx,
DspEdge {
src_port: Some(src_port),
dst_port: Some(i),
},
));
}
}
Source::Global(global_port) => {
if let (Some(&src_idx), Some(&dst_idx)) =
(global_in_nodes.get(global_port), id_to_idx.get(&id))
{
edges.push((
src_idx,
dst_idx,
DspEdge {
src_port: None,
dst_port: Some(i),
},
));
}
}
Source::Zero => {
// No edge for zero source
}
}
}
}
// Edges for global outputs: connect sources to each global output channel
for out_port in 0..net.outputs() {
match net.output_source(out_port) {
Source::Local(src_id, src_port) => {
if let (Some(&src_idx), Some(&dst_idx)) =
(id_to_idx.get(&src_id), global_out_nodes.get(out_port))
{
edges.push((
src_idx,
dst_idx,
DspEdge {
src_port: Some(src_port),
dst_port: None,
},
));
}
}
Source::Global(global_port) => {
if let (Some(&src_idx), Some(&dst_idx)) = (
global_in_nodes.get(global_port),
global_out_nodes.get(out_port),
) {
edges.push((
src_idx,
dst_idx,
DspEdge {
src_port: None,
dst_port: None,
},
));
}
}
Source::Zero => {
// No edge for zero source
}
}
}
// Sort edges by (source index, dest index, src_port, dst_port) for deterministic DOT.
edges.sort_by_key(|(s, d, w)| {
(
s.index(),
d.index(),
w.src_port.unwrap_or(usize::MAX),
w.dst_port.unwrap_or(usize::MAX),
)
});
// Insert edges in sorted order.
for (s, d, w) in edges {
g.add_edge(s, d, w);
}
(g, id_to_idx, global_in_nodes, global_out_nodes)
}
/// Render the given graph to Graphviz DOT format using custom node and edge labels.
/// Returns the DOT as a String.
fn dot_string(graph: &DiGraph<DspNode, DspEdge>) -> String {
let dot = Dot::with_attr_getters(
graph,
&[Config::NodeNoLabel, Config::EdgeNoLabel],
// Edge attributes: include port indices where available.
&|_g, e| {
let w = e.weight();
match (w.src_port, w.dst_port) {
(Some(sp), Some(dp)) => format!(r#"label = "out {} -> in {}""#, sp, dp),
(Some(sp), None) => format!(r#"label = "out {}""#, sp),
(None, Some(dp)) => format!(r#"label = "in {}""#, dp),
(None, None) => String::new(),
}
},
// Node attributes: label and shape based on kind.
&|_g, (_idx, weight)| {
let shape = match weight.kind {
DspNodeKind::GlobalIn => "shape = invhouse",
DspNodeKind::GlobalOut => "shape = house",
DspNodeKind::Unit => "shape = plaintext, margin = 0",
};
// Build Graphviz HTML-like TABLE label to preserve whitespace.
// - Escape &, <, > for HTML safety.
// - Replace spaces with to keep alignment.
// - Replace newlines with <BR/> to preserve line breaks.
let html_safe = weight
.label
.replace('&', "&")
.replace('<', "<")
.replace('>', ">");
let html_nbsp = html_safe.replace(' ', " ");
let html_lines = html_nbsp.replace('\n', "<BR/>");
// Read unit_id/port to avoid dead code warnings and enrich labels
let extra = match weight.kind {
DspNodeKind::Unit => weight
.unit_id
.map(|id| format!(" [id: {:?}]", id))
.unwrap_or_default(),
DspNodeKind::GlobalIn | DspNodeKind::GlobalOut => weight
.port
.map(|p| format!(" [ch: {}]", p))
.unwrap_or_default(),
};
let extra_row = if extra.is_empty() {
String::new()
} else {
let extra_html = extra
.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace(' ', " ");
format!(
r#"<TR><TD><FONT FACE="monospace">{}</FONT></TD></TR>"#,
extra_html
)
};
// Use Graphviz HTML-like labels with a TABLE to ensure whitespace is preserved.
format!(
r#"label = <<TABLE BORDER="1" CELLBORDER="1" CELLSPACING="0" CELLPADDING="2"><TR><TD><FONT FACE="monospace" POINT-SIZE="10">{}</FONT></TD></TR>{}</TABLE>>, {}"#,
html_lines, extra_row, shape
)
},
);
format!("{:?}", dot)
}
/// Build the graph from the given Net and return the DOT output as bytes (Vec<u8>).
pub fn snapshot_dsp_net_wiring(mut net: Net) -> Vec<u8> {
let (graph, _map, _ins, _outs) = build_petgraph(&mut net);
let dot = dot_string(&graph);
dot.into_bytes()
}