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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
// Copyright (c) 2021 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 super::{
    FeedbackFilter, GraphEvent, GraphMessage, HxMidiEvent, NodeOp, NodeProg, MAX_DSP_NODE_INPUTS,
    UNUSED_MONITOR_IDX,
};
use crate::dsp::{node_factory, Node, NodeId, NodeInfo, ParamId, SAtom};
use crate::monitor::{new_monitor_processor, MinMaxMonitorSamples, Monitor, MON_SIG_CNT};
use crate::nodes::drop_thread::DropThread;
use crate::SampleLibrary;
use crate::{NodeGlobalData, NodeGlobalRef};

use ringbuf::{Consumer, Producer, RingBuffer};
use std::collections::HashMap;
use std::sync::Arc;

use synfx_dsp::AtomicFloat;
use triple_buffer::Output;

const NODE_COMMUNICATION_BUFFER_SIZE: usize = 512;

/// A NodeInstance describes the input/output/atom ports of a Node
/// and holds other important house keeping information for the [NodeConfigurator].
#[derive(Debug, Clone)]
pub struct NodeInstance {
    id: NodeId,
    node: Node,
    in_use: bool,
    prog_idx: usize,
    out_start: usize,
    out_end: usize,
    in_start: usize,
    in_end: usize,
    at_start: usize,
    at_end: usize,
    mod_start: usize,
    mod_end: usize,
    /// A mapping array, to map from input index of the node
    /// to the modulator index. Because not every input has an
    /// associated modulator.
    /// This is used later to send [GraphMessage::ModamtUpdate].
    /// The input index into this array is the index returned from
    /// routines like [NodeId::inp_param].
    in2mod_map: [Option<usize>; MAX_DSP_NODE_INPUTS],
}

impl NodeInstance {
    pub fn new(id: NodeId, node: Node) -> Self {
        Self {
            id,
            node,
            in_use: false,
            prog_idx: 0,
            out_start: 0,
            out_end: 0,
            in_start: 0,
            in_end: 0,
            at_start: 0,
            at_end: 0,
            mod_start: 0,
            mod_end: 0,
            in2mod_map: [None; MAX_DSP_NODE_INPUTS],
        }
    }

    pub fn mark_used(&mut self) {
        self.in_use = true;
    }
    pub fn is_used(&self) -> bool {
        self.in_use
    }

    pub fn as_op(&self) -> NodeOp {
        NodeOp {
            idx: self.prog_idx as u8,
            node: self.node.clone(),
            out_idxlen: (self.out_start, self.out_end),
            in_idxlen: (self.in_start, self.in_end),
            at_idxlen: (self.at_start, self.at_end),
            mod_idxlen: (self.mod_start, self.mod_end),
            out_connected: 0x0,
            in_connected: 0x0,
            inputs: vec![],
        }
    }

    pub fn mod_in_local2global(&self, idx: u8) -> Option<usize> {
        if (idx as usize) > self.in2mod_map.len() {
            return None;
        }
        self.in2mod_map[idx as usize]
    }

    pub fn in_local2global(&self, idx: u8) -> Option<usize> {
        let idx = self.in_start + idx as usize;
        if idx < self.in_end {
            Some(idx)
        } else {
            None
        }
    }

    pub fn out_local2global(&self, idx: u8) -> Option<usize> {
        let idx = self.out_start + idx as usize;
        if idx < self.out_end {
            Some(idx)
        } else {
            None
        }
    }

    pub fn set_index(&mut self, idx: usize) -> &mut Self {
        self.prog_idx = idx;
        self
    }

    pub fn set_output(&mut self, s: usize, e: usize) -> &mut Self {
        self.out_start = s;
        self.out_end = e;
        self
    }

    pub fn set_input(&mut self, s: usize, e: usize) -> &mut Self {
        self.in_start = s;
        self.in_end = e;
        self
    }

    pub fn set_mod(&mut self, s: usize, e: usize) -> &mut Self {
        self.mod_start = s;
        self.mod_end = e;
        self
    }

    /// Sets the modulator index mapping: `idx` is the
    /// index of the parameter like in [NodeId::inp_param_by_idx],
    /// and `i` is the absolute index of the modulator that belongs
    /// to this parameter.
    pub fn set_mod_idx(&mut self, idx: usize, i: usize) -> &mut Self {
        self.in2mod_map[idx] = Some(i);
        self
    }

    pub fn set_atom(&mut self, s: usize, e: usize) -> &mut Self {
        self.at_start = s;
        self.at_end = e;
        self
    }
}

#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
struct NodeInputParam {
    param_id: ParamId,
    input_idx: usize,
    value: f32,
    modamt: Option<(usize, f32)>,
}

#[derive(Debug, Clone)]
struct NodeInputAtom {
    param_id: ParamId,
    at_idx: usize,
    value: SAtom,
}

/// This struct holds the frontend node configuration.
///
/// It stores which nodes are allocated and where.
/// Allocation of new nodes is done here, and parameter management
/// and synchronization is also done by this. It generally acts
/// as facade for the executed node graph in the backend.
///
/// This API is the most low level API provided by HexoDSP.
/// It shows how to create nodes and connect them.
/// The execution of the nodes in the audio thread is
/// controlled by a `NodeProg`, which defines the order
/// the nodes are executed in.
///
/// This only showcases the non-realtime generation of audio
/// samples. For a real time application of this library please
/// refer to the examples that come with this library.
///
/// ```rust
/// use hexodsp::*;
///
/// let (mut node_conf, mut node_exec) = new_node_engine();
///
/// node_conf.create_node(NodeId::Sin(0));
/// node_conf.create_node(NodeId::Amp(0));
///
/// let mut prog = node_conf.rebuild_node_ports();
///
/// node_conf.add_prog_node(&mut prog, &NodeId::Sin(0));
/// node_conf.add_prog_node(&mut prog, &NodeId::Amp(0));
///
/// node_conf.set_prog_node_exec_connection(
///     &mut prog,
///     (NodeId::Amp(0), NodeId::Amp(0).inp("inp").unwrap()),
///     (NodeId::Sin(0), NodeId::Sin(0).out("sig").unwrap()));
///
/// node_conf.upload_prog(prog, true);
///
/// let (out_l, out_r) = node_exec.test_run(0.1, false, &[]);
/// ```
pub struct NodeConfigurator {
    /// Holds all the nodes, their parameters and type.
    pub(crate) nodes: Vec<(NodeInfo, Option<NodeInstance>, Node)>,
    /// An index of all nodes ever instanciated.
    /// Be aware, that currently there is no cleanup implemented.
    /// That means, any instanciated NodeId will persist throughout
    /// the whole runtime. A garbage collector might be implemented
    /// when saving presets.
    pub(crate) node2idx: HashMap<NodeId, usize>,
    /// The shared parts of the [NodeConfigurator]
    /// and the [crate::nodes::NodeExecutor].
    pub(crate) shared: SharedNodeConf,

    /// Reference to the [crate::NodeGlobalData] that is used to initialize the
    /// [crate::dsp::DspNode] instances creates by this [NodeConfigurator].
    pub(crate) node_global: NodeGlobalRef,

    feedback_filter: FeedbackFilter,

    /// Loads and Caches audio samples that are set as parameters
    /// for nodes.
    sample_lib: SampleLibrary,

    /// Error messages:
    errors: Vec<String>,

    /// Contains (automateable) parameters
    params: std::collections::HashMap<ParamId, NodeInputParam>,
    /// Stores the most recently set parameter values
    param_values: std::collections::HashMap<ParamId, f32>,
    /// Stores the modulation amount of a parameter
    param_modamt: std::collections::HashMap<ParamId, Option<f32>>,
    /// Contains non automateable atom data for the nodes
    atoms: std::collections::HashMap<ParamId, NodeInputAtom>,
    /// Stores the most recently set atoms
    atom_values: std::collections::HashMap<ParamId, SAtom>,

    /// Holds a copy of the most recently updated output port feedback
    /// values. Update this by calling [NodeConfigurator::update_output_feedback].
    output_fb_values: Vec<f32>,

    /// Holds the channel to the backend that sends output port feedback.
    /// This is queried by [NodeConfigurator::update_output_feedback].
    output_fb_cons: Option<Output<Vec<f32>>>,
}

pub(crate) struct SharedNodeConf {
    /// Holds the LED values of the nodes
    pub(crate) node_ctx_values: Vec<Arc<AtomicFloat>>,
    /// For updating the NodeExecutor with graph updates.
    pub(crate) graph_update_prod: Producer<GraphMessage>,
    /// For receiving events from the DSP graph.
    pub(crate) graph_event_cons: Consumer<GraphEvent>,
    /// For receiving monitor data from the backend thread.
    pub(crate) monitor: Monitor,
    /// Handles deallocation of dead nodes from the backend.
    #[allow(dead_code)]
    pub(crate) drop_thread: DropThread,
    /// Sample rate of the backend
    pub(crate) sample_rate: Arc<AtomicFloat>,
}

use super::node_exec::SharedNodeExec;

impl SharedNodeConf {
    pub(crate) fn new() -> (Self, SharedNodeExec) {
        let rb_graph = RingBuffer::new(NODE_COMMUNICATION_BUFFER_SIZE);
        let rb_drop = RingBuffer::new(NODE_COMMUNICATION_BUFFER_SIZE);
        let rb_ev = RingBuffer::new(NODE_COMMUNICATION_BUFFER_SIZE);

        let (rb_graph_prod, rb_graph_con) = rb_graph.split();
        let (rb_drop_prod, rb_drop_con) = rb_drop.split();
        let (rb_ev_prod, rb_ev_con) = rb_ev.split();

        let drop_thread = DropThread::new(rb_drop_con);

        let (monitor_backend, monitor) = new_monitor_processor();

        let mut node_ctx_values = Vec::new();
        node_ctx_values
            .resize_with(NODE_COMMUNICATION_BUFFER_SIZE, || Arc::new(AtomicFloat::new(0.0)));

        let mut exec_node_ctx_vals = Vec::new();
        for ctx_val in node_ctx_values.iter() {
            exec_node_ctx_vals.push(ctx_val.clone());
        }

        let sample_rate = Arc::new(AtomicFloat::new(44100.0));

        (
            Self {
                node_ctx_values,
                graph_update_prod: rb_graph_prod,
                graph_event_cons: rb_ev_con,
                monitor,
                drop_thread,
                sample_rate: sample_rate.clone(),
            },
            SharedNodeExec {
                node_ctx_values: exec_node_ctx_vals,
                graph_update_con: rb_graph_con,
                graph_drop_prod: rb_drop_prod,
                graph_event_prod: rb_ev_prod,
                monitor_backend,
                sample_rate,
            },
        )
    }
}

impl NodeConfigurator {
    pub(crate) fn new() -> (Self, SharedNodeExec) {
        let nodes = Vec::new();

        let (shared, shared_exec) = SharedNodeConf::new();

        let node_global = NodeGlobalData::new_ref();

        (
            NodeConfigurator {
                nodes,
                shared,
                node_global,
                errors: vec![],
                sample_lib: SampleLibrary::new(),
                feedback_filter: FeedbackFilter::new(),
                output_fb_values: vec![],
                output_fb_cons: None,
                params: std::collections::HashMap::new(),
                param_values: std::collections::HashMap::new(),
                param_modamt: std::collections::HashMap::new(),
                atoms: std::collections::HashMap::new(),
                atom_values: std::collections::HashMap::new(),
                node2idx: HashMap::new(),
            },
            shared_exec,
        )
    }
    // FIXME: We can't drop nodes at runtime!
    //        We need to reinitialize the whole engine for this.
    //        There are too many things relying on the node index (UI).
    //
    //    pub fn drop_node(&mut self, idx: usize) {
    //        if idx >= self.nodes.len() {
    //            return;
    //        }
    //
    //        match self.nodes[idx] {
    //            NodeInfo::Nop => { return; },
    //            _ => {},
    //        }
    //
    //        self.nodes[idx] = NodeInfo::Nop;
    //        let _ =
    //            self.graph_update_prod.push(
    //                GraphMessage::NewNode {
    //                    index: idx as u8,
    //                    node: Node::Nop,
    //                });
    //    }

    pub fn for_each<F: FnMut(&NodeInfo, NodeId, usize)>(&self, mut f: F) {
        for (i, n) in self.nodes.iter().enumerate() {
            let nid = n.0.to_id();
            if NodeId::Nop == nid {
                break;
            }

            f(&n.0, nid, i);
        }
    }

    pub fn pop_error(&mut self) -> Option<String> {
        self.errors.pop()
    }

    pub fn unique_index_for(&self, ni: &NodeId) -> Option<usize> {
        self.node2idx.get(&ni).copied()
    }

    pub(crate) fn node_by_id(
        &self,
        ni: &NodeId,
    ) -> Option<&(NodeInfo, Option<NodeInstance>, Node)> {
        let idx = self.unique_index_for(ni)?;
        self.nodes.get(idx)
    }

    pub(crate) fn node_by_id_mut(
        &mut self,
        ni: &NodeId,
    ) -> Option<&mut (NodeInfo, Option<NodeInstance>, Node)> {
        let idx = self.unique_index_for(ni)?;
        self.nodes.get_mut(idx)
    }

    /// Returns the current modulation amount of the given parameter.
    /// Returns `None` if no modulation amount if set and thus no
    /// implicit attenuverter is set.
    pub fn get_param_modamt(&self, param: &ParamId) -> Option<f32> {
        self.param_modamt.get(&param).copied().flatten()
    }

    /// Set the modulation amount of a parameter.
    /// Returns true if a new [NodeProg] needs to be created, which can be
    /// necessary if there was no modulation amount assigned to this parameter
    /// yet.
    pub fn set_param_modamt(&mut self, param: ParamId, v: Option<f32>) -> bool {
        if param.is_atom() {
            return false;
        }

        let mut mod_idx = None;

        if let Some(nparam) = self.params.get_mut(&param) {
            if let Some(modamt) = &mut nparam.modamt {
                mod_idx = Some(modamt.0);
                modamt.1 = v.unwrap_or(0.0);
            }
        }

        // Check if the modulation amount was already set, if not, the caller
        // needs to reconstruct the graph and upload an updated NodeProg.
        if let Some(_old_modamt) = self.param_modamt.get(&param).copied().flatten() {
            if v.is_none() {
                self.param_modamt.insert(param, v);
                true
            } else {
                let modamt = v.unwrap();
                self.param_modamt.insert(param, v);

                if let Some(mod_idx) = mod_idx {
                    let _ = self
                        .shared
                        .graph_update_prod
                        .push(GraphMessage::ModamtUpdate { mod_idx, modamt });
                }

                false
            }
        } else {
            self.param_modamt.insert(param, v);
            true
        }
    }

    /// Retrieve [SAtom] values for input parameters and atoms.
    pub fn get_param(&self, param: &ParamId) -> Option<SAtom> {
        if param.is_atom() {
            self.atom_values.get(param).cloned()
        } else {
            self.param_values.get(param).map(|v| (*v).into())
        }
    }

    /// Assign [SAtom] values to input parameters and atoms.
    ///
    /// Only updates the DSP backend if [NodeConfigurator::rebuild_node_ports] was called
    /// before calling this. If no graph or the corresponding parameter is not active yet,
    /// then the value will be remembered until [NodeConfigurator::rebuild_node_ports] is called.
    pub fn set_param(&mut self, param: ParamId, at: SAtom) {
        if param.is_atom() {
            let at = if let SAtom::AudioSample((path, None)) = at.clone() {
                if !path.is_empty() {
                    match self.sample_lib.load(&path) {
                        Ok(sample) => sample.clone(),
                        Err(e) => {
                            self.errors.push(format!(
                                "Sample Loading Error\n\
                                        Couldn't load sample '{}':\n{:?}",
                                path, e
                            ));
                            at
                        }
                    }
                } else {
                    at
                }
            } else {
                at
            };

            self.atom_values.insert(param, at.clone());

            if let Some(nparam) = self.atoms.get_mut(&param) {
                nparam.value = at.clone();

                let at_idx = nparam.at_idx;
                let _ = self
                    .shared
                    .graph_update_prod
                    .push(GraphMessage::AtomUpdate { at_idx, value: at });
            }
        } else {
            self.param_values.insert(param, at.f());

            if let Some(nparam) = self.params.get_mut(&param) {
                let value = at.f();
                nparam.value = value;

                let input_idx = nparam.input_idx;
                let _ = self
                    .shared
                    .graph_update_prod
                    .push(GraphMessage::ParamUpdate { input_idx, value });
            }
        }
    }

    /// Dumps all set parameters (inputs and atoms).
    /// Most useful for serialization and saving patches.
    #[allow(clippy::type_complexity)]
    pub fn dump_param_values(&self) -> (Vec<(ParamId, f32, Option<f32>)>, Vec<(ParamId, SAtom)>) {
        let params: Vec<(ParamId, f32, Option<f32>)> = self
            .param_values
            .iter()
            .map(|(param_id, value)| {
                (
                    *param_id,
                    param_id.denorm(*value),
                    self.param_modamt.get(param_id).copied().flatten(),
                )
            })
            .collect();

        let atoms: Vec<(ParamId, SAtom)> =
            self.atom_values.iter().map(|(param_id, value)| (*param_id, value.clone())).collect();

        (params, atoms)
    }

    /// Loads parameter values from a dump. You will still need to upload
    /// a new [NodeProg] which contains these values.
    pub fn load_dumped_param_values(
        &mut self,
        params: &[(ParamId, f32, Option<f32>)],
        atoms: &[(ParamId, SAtom)],
        normalize_params: bool,
    ) {
        for (param_id, val, modamt) in params.iter() {
            let val = if normalize_params { param_id.norm(*val) } else { *val };
            self.set_param(*param_id, val.into());
            self.set_param_modamt(*param_id, *modamt);
        }

        for (param_id, val) in atoms.iter() {
            self.set_param(*param_id, val.clone());
        }
    }

    /// Iterates over every parameter and calls the given function with
    /// it's current value.
    pub fn for_each_param<F: FnMut(usize, ParamId, &SAtom, Option<f32>)>(&self, mut f: F) {
        for (_, node_input) in self.atoms.iter() {
            if let Some(unique_idx) = self.unique_index_for(&node_input.param_id.node_id()) {
                f(unique_idx, node_input.param_id, &node_input.value, None);
            }
        }

        for (_, node_input) in self.params.iter() {
            if let Some(unique_idx) = self.unique_index_for(&node_input.param_id.node_id()) {
                let modamt = self.param_modamt.get(&node_input.param_id).copied().flatten();

                f(unique_idx, node_input.param_id, &SAtom::param(node_input.value), modamt);
            }
        }
    }

    /// Returns the current phase value of the given node.
    ///
    /// It usually returns something like the position of a sequencer
    /// or the phase of an oscillator.
    pub fn phase_value_for(&self, ni: &NodeId) -> f32 {
        if let Some(idx) = self.unique_index_for(ni) {
            self.shared.node_ctx_values[(idx * 2) + 1].get()
        } else {
            0.0
        }
    }

    /// Returns the current status LED value of the given node.
    ///
    /// A status LED might be anything a specific node deems the most
    /// important value. Often it might be just the current value
    /// of the primary signal output.
    pub fn led_value_for(&self, ni: &NodeId) -> f32 {
        if let Some(idx) = self.unique_index_for(ni) {
            self.shared.node_ctx_values[idx * 2].get()
        } else {
            0.0
        }
    }

    /// Triggers recalculation of the filtered values from the
    /// current LED values and output feedback.
    ///
    /// This function internally calls [NodeConfigurator::update_output_feedback]
    /// for you, so you don't need to call it yourself.
    ///
    /// See also [NodeConfigurator::filtered_led_for]
    /// and [NodeConfigurator::filtered_out_fb_for].
    pub fn update_filters(&mut self) {
        self.update_output_feedback();
        self.feedback_filter.trigger_recalc();
    }

    /// Returns a filtered LED value that is smoothed a bit
    /// and provides a min and max value.
    ///
    /// Make sure to call [NodeConfigurator::update_filters]
    /// before calling this function, or the values won't be up to date.
    ///
    ///```
    /// use hexodsp::*;
    ///
    /// let (mut node_conf, mut node_exec) = new_node_engine();
    ///
    /// node_conf.create_node(NodeId::Sin(0));
    /// node_conf.create_node(NodeId::Amp(0));
    ///
    /// let mut prog = node_conf.rebuild_node_ports();
    ///
    /// node_conf.add_prog_node(&mut prog, &NodeId::Sin(0));
    /// node_conf.add_prog_node(&mut prog, &NodeId::Amp(0));
    ///
    /// node_conf.set_prog_node_exec_connection(
    ///     &mut prog,
    ///     (NodeId::Amp(0), NodeId::Amp(0).inp("inp").unwrap()),
    ///     (NodeId::Sin(0), NodeId::Sin(0).out("sig").unwrap()));
    ///
    /// node_conf.upload_prog(prog, true);
    ///
    /// node_exec.test_run(0.1, false, &[]);
    /// assert!((node_conf.led_value_for(&NodeId::Sin(0)) - (-0.062522)).abs() < 0.001);
    /// assert!((node_conf.led_value_for(&NodeId::Amp(0)) - (-0.062522)).abs() < 0.001);
    ///
    /// for _ in 0..10 {
    ///     node_exec.test_run(0.1, false, &[]);
    ///     node_conf.update_filters();
    ///     node_conf.filtered_led_for(&NodeId::Sin(0));
    ///     node_conf.filtered_led_for(&NodeId::Amp(0));
    /// }
    ///
    /// assert_eq!((node_conf.filtered_led_for(&NodeId::Sin(0)).0 * 1000.0).floor() as i64, 62);
    /// assert_eq!((node_conf.filtered_led_for(&NodeId::Amp(0)).0 * 1000.0).floor() as i64, 62);
    ///```
    pub fn filtered_led_for(&mut self, ni: &NodeId) -> (f32, f32) {
        let led_value = self.led_value_for(ni);
        self.feedback_filter.get_led(ni, led_value)
    }

    /// Returns a filtered output port value that is smoothed
    /// a bit and provides a min and max value.
    ///
    /// Make sure to call [NodeConfigurator::update_filters]
    /// before calling this function, or the values won't be up to date.
    /// That function also calls [NodeConfigurator::update_output_feedback]
    /// for you conveniently.
    ///
    /// For an example on how to use see [NodeConfigurator::filtered_led_for]
    /// which has the same semantics as this function.
    pub fn filtered_out_fb_for(&mut self, node_id: &NodeId, out: u8) -> (f32, f32) {
        let out_value = self.out_fb_for(node_id, out).unwrap_or(0.0);
        self.feedback_filter.get_out(node_id, out, out_value)
    }

    /// Monitor the given inputs and outputs of a specific node.
    ///
    /// The monitor data can be retrieved using
    /// [NodeConfigurator::get_minmax_monitor_samples].
    pub fn monitor(&mut self, node_id: &NodeId, inputs: &[Option<u8>], outputs: &[Option<u8>]) {
        let mut bufs = [UNUSED_MONITOR_IDX; MON_SIG_CNT];

        if let Some((_node_info, Some(node_instance), _)) = self.node_by_id(node_id) {
            let mut i = 0;
            for inp_idx in inputs.iter().take(MON_SIG_CNT / 2) {
                if let Some(inp_idx) = inp_idx {
                    if let Some(global_idx) = node_instance.in_local2global(*inp_idx) {
                        bufs[i] = global_idx;
                    }
                }

                i += 1;
            }

            for out_idx in outputs.iter().take(MON_SIG_CNT / 2) {
                if let Some(out_idx) = out_idx {
                    if let Some(global_idx) = node_instance.out_local2global(*out_idx) {
                        bufs[i] = global_idx;
                    }
                }

                i += 1;
            }

            let _ = self.shared.graph_update_prod.push(GraphMessage::SetMonitor { bufs });
        }
    }

    pub fn get_node_global(&self) -> NodeGlobalRef {
        self.node_global.clone()
    }

    pub fn delete_nodes(&mut self) {
        self.node2idx.clear();
        self.nodes.clear();
        self.params.clear();
        self.param_values.clear();
        self.param_modamt.clear();
        self.atoms.clear();
        self.atom_values.clear();

        let _ = self.shared.graph_update_prod.push(GraphMessage::Clear { prog: NodeProg::empty() });
    }

    pub fn create_node(&mut self, ni: NodeId) -> Option<(&NodeInfo, u8)> {
        if let Some((node, info)) = node_factory(ni, &self.node_global) {
            node.set_sample_rate(self.shared.sample_rate.get());

            let index = self.nodes.len();
            self.node2idx.insert(ni, index);
            self.nodes.push((info, None, node.clone()));
            Some((&self.nodes[index].0, index as u8))
        } else {
            None
        }
    }

    /// Returns the first instance of the given [NodeId] (starting with the
    /// instance of the [NodeId]) that has not been used.
    ///
    /// Primarily used by the (G)UI when creating new nodes to be added to the
    /// graph.
    ///
    /// Should be called after the [NodeProg] has been created
    /// (and after [NodeConfigurator::rebuild_node_ports] was called).
    ///
    /// If new nodes were created/deleted/reordered in between this function
    /// might not work properly and assign already used instances.
    pub fn unused_instance_node_id(&self, mut id: NodeId) -> NodeId {
        while let Some((_, Some(ni), _)) = self.node_by_id(&id) {
            if !ni.is_used() {
                return ni.id;
            }

            id = id.to_instance(id.instance() + 1);
        }

        id
    }

    /// Rebuilds Input/Output/Atom indices for the nodes, which is necessary
    /// if nodes were created/deleted or reordered. It also assigns
    /// input parameter and atom values for new nodes.
    ///
    /// Returns a new NodeProg with space for all allocated nodes
    /// inputs, outputs and atoms.
    ///
    /// Execute this after a [NodeConfigurator::create_node].
    pub fn rebuild_node_ports(&mut self) -> NodeProg {
        // Regenerating the params and atoms in the next step:
        self.params.clear();
        self.atoms.clear();

        let mut out_len = 0;
        let mut in_len = 0;
        let mut at_len = 0;
        let mut mod_len = 0;

        for (i, (node_info, node_instance, node)) in self.nodes.iter_mut().enumerate() {
            let id = node_info.to_id();

            // - calculate size of output vector.
            let out_idx = out_len;
            out_len += node_info.out_count();

            // - calculate size of input vector.
            let in_idx = in_len;
            in_len += node_info.in_count();

            // - calculate size of atom vector.
            let at_idx = at_len;
            at_len += node_info.at_count();

            // - hold the mod start index of this node.
            let mod_idx = mod_len;

            if id == NodeId::Nop {
                break;
            }

            let mut ni = NodeInstance::new(id, node.clone());
            ni.set_index(i)
                .set_output(out_idx, out_len)
                .set_input(in_idx, in_len)
                .set_atom(at_idx, at_len);

            // - save offset and length of each node's
            //   allocation in the output vector.
            *node_instance = Some(ni);

            //d// println!("INSERT[{}]: {:?} outidx: {},{} inidx: {},{} atidx: {},{}",
            //d//          i, id, out_idx, out_len, in_idx, in_len, at_idx, at_len);

            // Create new parameters and initialize them if they did not
            // already exist previously
            for param_idx in in_idx..in_len {
                let input_idx = param_idx - in_idx;

                if let Some(param_id) = id.inp_param_by_idx(input_idx) {
                    let value = if let Some(value) = self.param_values.get(&param_id) {
                        *value
                    } else {
                        param_id.norm_def()
                    };

                    // If we have a modulation, store the absolute
                    // index of it in the [NodeProg::modops] vector later:
                    let ma = self.param_modamt.get(&param_id).copied().flatten();
                    let modamt = if ma.is_some() {
                        let mod_idx = mod_len;
                        node_instance.as_mut().unwrap().set_mod_idx(input_idx, mod_idx);
                        mod_len += 1;
                        Some((mod_idx, ma.unwrap()))
                    } else {
                        None
                    };

                    self.param_values.insert(param_id, value);
                    self.params.insert(
                        param_id,
                        NodeInputParam { param_id, value, input_idx: param_idx, modamt },
                    );
                }
            }

            // After iterating through the parameters we can
            // store the range of the indices of this node.
            node_instance.as_mut().unwrap().set_mod(mod_idx, mod_len);

            // Create new atom data and initialize it if it did not
            // already exist from a previous matrix instance.
            for atom_idx in at_idx..at_len {
                // XXX: See also the documentation of atom_param_by_idx about the
                // little param_id for an Atom weirdness here.
                if let Some(param_id) = id.atom_param_by_idx(atom_idx - at_idx) {
                    let value = if let Some(atom) = self.atom_values.get(&param_id) {
                        atom.clone()
                    } else {
                        param_id.as_atom_def()
                    };

                    self.atom_values.insert(param_id, value.clone());
                    self.atoms
                        .insert(param_id, NodeInputAtom { param_id, value, at_idx: atom_idx });
                }
            }
        }

        NodeProg::new(out_len, in_len, at_len, mod_len)
    }

    /// Creates a new [NodeOp] and add it to the [NodeProg].
    ///
    /// It will fail silently if the nodes have not been created yet or
    /// [NodeConfigurator::rebuild_node_ports] was not called before. So make sure this is the
    /// case or don't expect the node and input to be executed.
    pub fn add_prog_node(&mut self, prog: &mut NodeProg, node_id: &NodeId) {
        if let Some((_node_info, Some(node_instance), _)) = self.node_by_id_mut(node_id) {
            node_instance.mark_used();
            let op = node_instance.as_op();
            prog.append_op(op);
        }
    }

    /// Adds an adjacent output connection to the given node input.
    /// Will either create a new [NodeOp] in the [NodeProg] or append to an
    /// existing one. This means the order you set the to be executed node
    /// connections, is the order the [NodeProg] is going to be executed by the
    /// DSP thread later.
    ///
    /// It will fail silently if the nodes have not been created yet or
    /// [NodeConfigurator::rebuild_node_ports] was not called before. So make sure this is the
    /// case or don't expect the node and input to be executed.
    pub fn set_prog_node_exec_connection(
        &mut self,
        prog: &mut NodeProg,
        node_input: (NodeId, u8),
        adjacent_output: (NodeId, u8),
    ) {
        let output_index =
            if let Some((_, Some(node_instance), _)) = self.node_by_id(&adjacent_output.0) {
                node_instance.out_local2global(adjacent_output.1)
            } else {
                return;
            };

        if let Some((_node_info, Some(node_instance), _)) = self.node_by_id_mut(&node_input.0) {
            node_instance.mark_used();
            let op = node_instance.as_op();

            let input_index = node_instance.in_local2global(node_input.1);
            let mod_index = node_instance.mod_in_local2global(node_input.1);
            if let (Some(input_index), Some(output_index)) = (input_index, output_index) {
                prog.append_edge(op, input_index, output_index, mod_index);
            }
        }
    }

    /// Uploads a new NodeProg instance.
    ///
    /// Create a new NodeProg instance with [NodeConfigurator::rebuild_node_ports]
    /// for each call to this function. Otherwise things like the
    /// [NodeConfigurator::out_fb_for] might not work properly!
    ///
    /// The `copy_old_out` parameter should be set if there are only
    /// new nodes appended to the end of the node instances.
    /// It helps to prevent clicks when there is a feedback path somewhere.
    ///
    /// It must not be set when a completely new set of node instances
    /// was created, for instance when a completely new patch was loaded.
    ///
    /// Here is an example on how to use the [NodeConfigurator]
    /// directly to setup and upload a [NodeProg]:
    ///
    ///```
    /// use hexodsp::*;
    ///
    /// let (mut node_conf, mut node_exec) = new_node_engine();
    ///
    /// node_conf.create_node(NodeId::Sin(0));
    /// node_conf.create_node(NodeId::Amp(0));
    ///
    /// let mut prog = node_conf.rebuild_node_ports();
    ///
    /// node_conf.add_prog_node(&mut prog, &NodeId::Sin(0));
    /// node_conf.add_prog_node(&mut prog, &NodeId::Amp(0));
    ///
    /// node_conf.set_prog_node_exec_connection(
    ///     &mut prog,
    ///     (NodeId::Amp(0), NodeId::Amp(0).inp("inp").unwrap()),
    ///     (NodeId::Sin(0), NodeId::Sin(0).out("sig").unwrap()));
    ///
    /// node_conf.upload_prog(prog, true);
    ///```
    pub fn upload_prog(&mut self, mut prog: NodeProg, copy_old_out: bool) {
        // Copy the parameter values and atom data into the program:
        // They are extracted by process_graph_updates() later to
        // reset the inp[] input value vector.
        for (_param_id, param) in self.params.iter() {
            prog.params_mut()[param.input_idx] = param.value;

            if let Some((mod_idx, amt)) = param.modamt {
                prog.modops_mut()[mod_idx].set_amt(amt);
            }
        }

        // The atoms are referred to directly on process() call.
        for (_param_id, param) in self.atoms.iter() {
            prog.atoms_mut()[param.at_idx] = param.value.clone();
        }

        self.output_fb_cons = prog.take_feedback_consumer();

        let _ = self.shared.graph_update_prod.push(GraphMessage::NewProg { prog, copy_old_out });
    }

    /// Retrieves the feedback value for a specific output port of the
    /// given [NodeId]. You need to call [NodeConfigurator::update_output_feedback]
    /// before this, or otherwise your output values might be outdated
    /// or not available at all.
    ///
    /// See also [NodeConfigurator::filtered_out_fb_for] for a
    /// filtered variant suitable for UI usage.
    pub fn out_fb_for(&self, node_id: &NodeId, out: u8) -> Option<f32> {
        if let Some((_, Some(node_instance), _)) = self.node_by_id(node_id) {
            self.output_fb_values.get(node_instance.out_local2global(out)?).copied()
        } else {
            None
        }
    }

    /// Checks if the backend has new output feedback values.
    /// Call this function for each frame of the UI to get the most
    /// up to date output feedback values that are available.
    ///
    /// Retrieve the output value by calling [NodeConfigurator::out_fb_for].
    pub fn update_output_feedback(&mut self) {
        if let Some(out_fb_output) = &mut self.output_fb_cons {
            out_fb_output.update();
            let out_vec = out_fb_output.output_buffer();

            self.output_fb_values.clear();
            self.output_fb_values.resize(out_vec.len(), 0.0);
            self.output_fb_values.copy_from_slice(&out_vec[..]);
        }
    }

    pub fn get_minmax_monitor_samples(&mut self, idx: usize) -> &MinMaxMonitorSamples {
        self.shared.monitor.get_minmax_monitor_samples(idx)
    }

    /// Injects a [HxMidiEvent] directly into audio thread, so that it can trickle
    /// back to the GUI thread the standard way. This is mostly used for automated testing.
    /// And maybe some day for some kind of remote control script from WLambda?
    pub fn inject_midi_event(&mut self, midi_ev: HxMidiEvent) {
        let _ = self.shared.graph_update_prod.push(GraphMessage::InjectMidi { midi_ev });
    }

    /// Returns the next [GraphEvent] from the DSP/audio/backend thread.
    pub fn next_event(&mut self) -> Option<GraphEvent> {
        self.shared.graph_event_cons.pop()
    }
}