dhwani 0.1.1

A real-time audio processing engine for DAWs
Documentation
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
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
//! Owns the node graph and drives per-frame processing on the audio thread.
//! Constructed once; thereafter only mutated via [`controller::start_controller`] messages.

use std::collections::{HashMap, HashSet};

use crate::{
    Error,
    channel::ChannelPosition,
    connection::Connection,
    event::EventsFrame,
    frame::Frame,
    node::{Node, NodeBuilderTrait, NodeId, NodeInputs, NodeOutputs, NodeResetCtx},
    nodes::NodeInfo,
    port::{Port, PortId, PortType},
    signal::SingalsFrame,
    time::{SampleBaseType, SampleRateBaseType, TimeFrom, TimeRange, TimeUnit},
    track::{Track, TrackId},
};

#[derive(Debug, PartialEq)]
enum ProcessorState {
    Paused,
    Playing,
    Stopped,
}

#[derive(Debug, Clone, Copy)]
enum FadeMode {
    In,
    Out,
}

#[derive(Debug, Clone, Copy)]
struct Fader {
    mode: FadeMode,
    step: usize,
    duration: usize,
}

impl IntoIterator for Fader {
    type Item = f32;
    type IntoIter = FaderIter;

    fn into_iter(self) -> Self::IntoIter {
        FaderIter {
            mode: self.mode,
            step: self.step,
            duration: self.duration,
        }
    }
}

impl Fader {
    pub const fn new(mode: FadeMode, duration: usize) -> Self {
        Self {
            mode,
            // Already make the fader state = done
            step: duration,
            duration,
        }
    }

    const fn remaining(&self) -> usize {
        self.duration - self.step
    }

    const fn done(&self) -> bool {
        self.step >= self.duration
    }

    const fn fade_in(self) -> Self {
        Self {
            mode: FadeMode::In,
            step: 0,
            ..self
        }
    }

    /// New fader with old settings for starting fade out.
    const fn fade_out(self) -> Self {
        Self {
            mode: FadeMode::Out,
            step: 0,
            ..self
        }
    }

    pub const fn update(&mut self, step: usize) {
        self.step += step;
        if self.step > self.duration {
            self.step = self.duration;
        }
    }
}

struct FaderIter {
    mode: FadeMode,
    step: usize,
    duration: usize,
}

impl Iterator for FaderIter {
    type Item = f32;
    fn next(&mut self) -> Option<Self::Item> {
        let mul = match self.mode {
            FadeMode::Out => {
                if self.step >= self.duration {
                    0f32
                } else {
                    let mul = (self.duration - self.step) as f32 / self.duration as f32;
                    self.step += 1;
                    mul
                }
            }
            FadeMode::In => {
                if self.step > self.duration {
                    1f32
                } else {
                    let mul = self.step as f32 / self.duration as f32;
                    self.step += 1;
                    mul
                }
            }
        };
        Some(mul)
    }
}

pub struct Processor {
    state: ProcessorState,
    sr: SampleRateBaseType,
    n_channels: u16,
    buffer_size: usize,
    pub step: SampleBaseType,
    id: usize,
    pub(crate) nodes_map: HashMap<NodeId, Node>,
    pub(crate) tracks_map: HashMap<TrackId, Track>,
    /// Unlike [`Processor::frames_maps`], this is flattened for optimal insert/access
    pub(crate) connections_map: HashMap<(NodeId, PortId), Connection>,
    pub(crate) frames_maps: HashMap<NodeId, HashMap<PortId, Frame>>,
    // Compiled connection maps. This would not contain proxy ports
    compiled_connections_map: HashMap<(NodeId, PortId), Connection>,
    // The pointer to nodes sorted in DFS order for processing
    nodes: Vec<NodeId>,
    // Final port
    output_port: Option<Port>,
    end_time: SampleBaseType,
    invalidate: bool,
    fader: Fader,
}

impl Processor {
    const TRACKS_MAP_CAPACITY: usize = 32;
    const NODES_MAP_CAPACITY: usize = 512;
    const CONNECTIONS_MAP_CAPACITY: usize = 1024;
    const FRAMES_MAPS_CAPACITY: usize = 1024;
    const FADE_DURATION: f64 = 0.020f64; // 20ms

    #[must_use]
    pub fn new(sr: SampleRateBaseType, n_channels: u16, buffer_size: usize) -> Self {
        let mut processor = Self {
            state: ProcessorState::Paused,
            sr,
            n_channels,
            buffer_size,
            step: 0 as SampleBaseType,
            id: 0,
            tracks_map: HashMap::with_capacity(Self::TRACKS_MAP_CAPACITY),
            nodes_map: HashMap::with_capacity(Self::NODES_MAP_CAPACITY),
            connections_map: HashMap::with_capacity(Self::CONNECTIONS_MAP_CAPACITY),
            frames_maps: HashMap::with_capacity(Self::FRAMES_MAPS_CAPACITY),
            compiled_connections_map: HashMap::with_capacity(Self::CONNECTIONS_MAP_CAPACITY),
            nodes: vec![],
            output_port: None,
            end_time: 0 as SampleBaseType,
            invalidate: true,
            fader: Fader::new(
                FadeMode::In,
                // 10ms fader
                TimeUnit::Seconds(Self::FADE_DURATION).to_samples(sr) as usize,
            ),
        };
        processor.reset();
        processor
    }

    fn process_fader(&mut self, chunk: usize) {
        self.fader.update(chunk);
        debug_assert!(!matches!(self.state, ProcessorState::Stopped));
        if self.fader.done() {
            match self.fader.mode {
                FadeMode::In => {
                    self.set_playing(true);
                }
                FadeMode::Out => {
                    self.set_playing(false);
                }
            }
        }
    }

    #[inline]
    fn reset(&mut self) {
        self.set_playing(false);
        self.step = 0;
        self.tracks_map.clear();
        self.connections_map.clear();
        self.nodes_map.clear();
        self.id = 0;
        self.output_port = None;
    }

    #[inline]
    pub(crate) fn new_id(&mut self) -> usize {
        let id = self.id;
        assert!(self.id != usize::MAX, "ID exhausted!");
        self.id += 1;
        id
    }

    fn remove_node_connections(&mut self, id: NodeId) {
        // Remove exisiting connections
        self.connections_map.retain(|_, connection| {
            connection.source.node_id != id && connection.target.node_id != id
        });
    }

    pub(crate) fn remove_nodes(&mut self, root_id: NodeId, remove_connection: bool) {
        // Remove output node
        if remove_connection
            && self
                .output_port
                .as_ref()
                .is_some_and(|output_port| output_port.node_id == root_id)
        {
            self.output_port = None;
        }
        let mut stack = Vec::<NodeId>::with_capacity(self.nodes_map.capacity());
        stack.push(root_id);
        while let Some(id) = stack.pop() {
            self.nodes_map.remove(&id);
            self.frames_maps.remove(&id);
            if remove_connection {
                self.remove_node_connections(id);
            }
            for (child_id, _) in self
                .nodes_map
                .extract_if(|_, node| node.parent_id() == Some(id))
            {
                stack.push(child_id);
            }
        }
    }

    pub(crate) fn build_frames(&self, node: &Node) -> HashMap<PortId, Frame> {
        let mut frames: HashMap<PortId, Frame> = HashMap::<PortId, Frame>::new();
        for port in node.ports() {
            match port.kind {
                PortType::EventsIn | PortType::SignalIn | PortType::Proxy(_) => {}
                PortType::EventsOut => {
                    // Lets keep minimum of 64 events per frame
                    let frame = Frame::Events(EventsFrame::new(self.min_events_per_frame()));
                    frames.insert(port.id, frame);
                }
                PortType::SignalOut(channel_mask) => {
                    let frame = Frame::Signals(SingalsFrame::new(channel_mask, self.buffer_size));
                    frames.insert(port.id, frame);
                }
            }
        }
        frames
    }

    fn reset_frames(&mut self, id: NodeId) {
        if let Some(frames) = self.frames_maps.get_mut(&id) {
            for frame in frames.values_mut() {
                frame.reset();
            }
        }
    }

    pub(crate) fn is_child(&self, parent_id: NodeId, id: NodeId) -> bool {
        let mut node = self.nodes_map.get(&id);
        while let Some(id) = node.and_then(Node::parent_id) {
            if id == parent_id {
                return true;
            }
            node = self.nodes_map.get(&id);
        }
        false
    }

    pub(crate) fn reverse_port(&self, mut port: Port) -> Result<Port, Error> {
        while let PortType::Proxy(port_proxy) = port.kind {
            let node = self
                .nodes_map
                .get(&port_proxy.node_id)
                .ok_or_else(|| Error::msg("Failed to reverse port".into()))?;
            port = node
                .get_port(port_proxy.port_id)
                .ok_or_else(|| Error::msg("Failed to reverse port".into()))?;
        }
        Ok(port)
    }

    ///
    /// # Errors
    /// Return `Err` if buffer len is invalid.
    /// Buffer length must be a multiple of `self.n_channels` and must
    /// be <= `self.n_channels` * `self.buffer_size`
    ///
    #[allow(clippy::too_many_lines)]
    pub fn process(&mut self, buffer: &mut [f32]) -> Result<usize, Error> {
        let n_channels = usize::from(self.n_channels);
        if !buffer.len().is_multiple_of(n_channels) {
            return Err(Error::msg(
                "Buffer must be mulutple of number of channels".to_string(),
            ));
        }
        let buffer_size = buffer.len() / n_channels;
        if buffer_size > self.buffer_size {
            return Err(Error::msg(format!(
                "Buffer size must be <= number of channels * {}",
                self.buffer_size
            )));
        }
        if !self.is_playing() {
            buffer.fill(0f32);
            return Ok(buffer.len());
        }
        // So that fading completes exactly and command can be processed
        let fader_rem = self.fader.remaining();
        let chunk = if matches!(self.fader.mode, FadeMode::Out) && fader_rem > 0 {
            buffer_size.min(fader_rem)
        } else {
            buffer_size
        };
        if self.invalidate {
            self.recompile_graph();
            self.invalidate = false;
        }
        let start_sample = self.step;
        let end_sample = start_sample + chunk as SampleBaseType;
        for i in 0..self.nodes.len() {
            let node_id = self.nodes[i];
            // Temporarly take the item from hashmap
            let mut node = self.nodes_map.remove(&node_id).unwrap();
            // Compute time related
            let (track_start_sample, track_end_sample) = {
                let time_range = self.tracks_map.get(&node.track_id()).unwrap().time_range();
                (
                    time_range.start().to_samples(self.sr),
                    time_range
                        .end()
                        .map_or_else(|| end_sample, |t| t.to_samples(self.sr)),
                )
            };
            // Add duration extension from node
            let track_end_sample = node.inner.duration_extension().map_or_else(
                || end_sample, // Keep it at max if None
                |time| {
                    // Lets not take negative extension
                    track_end_sample + time.to_samples(self.sr).max(0)
                },
            );
            let new_start_sample = start_sample.max(track_start_sample);
            let new_end_sample = end_sample.min(track_end_sample);
            if new_start_sample >= new_end_sample {
                // Clear frame if not invalidated.
                // Lets say a node A is from time [a,b) and  node B is from [c,d)
                // where [a,b) < [c,d) and A is connected to B. When processing B,
                // B will fetch values from A, but it might be a previous value
                // So need to clear it
                if !node.frames_invalidated {
                    node.frames_invalidated = true;
                    self.reset_frames(node_id);
                }
                // Insert back
                self.nodes_map.insert(node_id, node);
                continue;
            }
            node.frames_invalidated = false;
            let frame_range = {
                // Frame rang is the range for which the frames are relevant
                // for the current session of processing
                debug_assert!(new_start_sample - start_sample >= 0);
                debug_assert!(new_end_sample - start_sample >= 0);
                #[allow(clippy::cast_possible_truncation)]
                #[allow(clippy::cast_sign_loss)]
                let frame_range_start = (new_start_sample - start_sample) as usize;
                let frame_range_end = (new_end_sample - start_sample) as usize;
                frame_range_start..frame_range_end
            };
            // Temporarly take the item from hashmap
            let mut frames = self.frames_maps.remove(&node_id).unwrap();
            let node_inputs = NodeInputs::new(
                self.sr,
                frame_range.clone(),
                &self.frames_maps,
                node_id,
                &self.compiled_connections_map,
            );
            let mut node_outputs = NodeOutputs::new(self.sr, frame_range, &mut frames);
            node_outputs.clear();
            let step_range = {
                // Step range is the range iterator sent to process the nodes,
                // It will a range with the track start time as base
                debug_assert!(new_start_sample - track_start_sample >= 0);
                debug_assert!(new_end_sample - track_start_sample >= 0);
                #[allow(clippy::cast_possible_truncation)]
                #[allow(clippy::cast_sign_loss)]
                let step_range_start = (new_start_sample - track_start_sample) as usize;
                let step_range_end = (new_end_sample - track_start_sample) as usize;
                step_range_start..step_range_end
            };
            node.inner
                .process(step_range, &node_inputs, &mut node_outputs);
            // Insert back
            self.frames_maps.insert(node_id, frames);
            // Insert back
            self.nodes_map.insert(node_id, node);
        }
        if let Some(output_port) = self.output_port {
            let signals = self
                .frames_maps
                .get(&output_port.node_id)
                .unwrap()
                .get(&output_port.id)
                .unwrap()
                .get_signals(0..((end_sample - start_sample) as usize))
                .unwrap();
            if let Some(signal) = signals.get(ChannelPosition::FrontLeft) {
                let mut fader = self.fader.into_iter();
                for i in 0..chunk {
                    let mul = fader.next().unwrap();
                    buffer[n_channels * i] = signal[i] * mul;
                }
            }
            if let Some(signal) = signals.get(ChannelPosition::FrontRight) {
                let mut fader = self.fader.into_iter();
                for i in 0..chunk {
                    let mul = fader.next().unwrap();
                    buffer[(n_channels * i) + 1] = signal[i] * mul;
                }
            }
        }
        self.process_fader(chunk);
        self.step = end_sample;
        Ok(n_channels * chunk)
    }

    fn recompile_connections(&mut self) {
        let mut compiled_connections_map = self.connections_map.clone();
        for (&(node_id, port_id), connection) in &self.connections_map {
            let source = self.reverse_port(connection.source).unwrap();
            if source == connection.source {
                continue;
            }
            let connection = Connection::new(source, connection.target).unwrap();
            compiled_connections_map.insert((node_id, port_id), connection);
        }
        self.compiled_connections_map = compiled_connections_map;
    }

    fn recompile_graph_iter(
        &mut self,
        target_node_id: NodeId,
        node_status: &mut HashSet<NodeId>,
        connections_map: &mut HashMap<NodeId, Vec<PortId>>,
    ) {
        // Remove temporarily
        if let Some(connections) = connections_map.remove(&target_node_id) {
            for &target_port_id in &connections {
                let &connection = self
                    .compiled_connections_map
                    .get(&(target_node_id, target_port_id))
                    .unwrap();
                debug_assert!(
                    connection.target.id == target_port_id,
                    "Target ID not valid"
                );
                if node_status.insert(connection.source.node_id) {
                    self.recompile_graph_iter(
                        connection.source.node_id,
                        node_status,
                        connections_map,
                    );
                    self.nodes.push(connection.source.node_id);
                }
            }
            // Put it back
            connections_map.insert(target_node_id, connections);
        }
    }

    fn recompile_graph(&mut self) {
        self.nodes = Vec::<NodeId>::with_capacity(self.nodes_map.len());
        let mut node_status = HashSet::<NodeId>::with_capacity(self.nodes_map.len());
        self.end_time = 0;
        for track in self.tracks_map.values() {
            if let Some(end) = track.time_range().end() {
                let end_time = end.to_samples(self.sr);
                // Set end time the maximum
                self.end_time = end_time.max(self.end_time);
            }
        }
        self.recompile_connections();
        if let Some(output_port) = self.output_port {
            // All nodes put into self.nodes in processing order
            let mut connections_map = HashMap::<NodeId, Vec<PortId>>::new();
            for connection in self.compiled_connections_map.values() {
                connections_map
                    .entry(connection.target.node_id)
                    .or_default()
                    .push(connection.target.id);
            }
            self.recompile_graph_iter(output_port.node_id, &mut node_status, &mut connections_map);
            self.nodes.push(output_port.node_id);
        }
        // #[cfg(debug_assertions)]
        // {
        //     println!("Tracks");
        //     for track in self.tracks_map.values() {
        //         println!("{:?}", track);
        //     }
        //     println!("Nodes");
        //     for node in self.nodes_map.values() {
        //         println!("{node:?}");
        //     }
        //     println!("Nodes in process");
        //     for id in &self.nodes {
        //         let node = self.nodes_map.get(id).unwrap();
        //         println!("{node:?}");
        //     }
        //     println!("Connection map");
        //     println!("{{");
        //     for ((node_id, port_id), connection) in &self.connections_map {
        //         println!("    connection: {node_id:?}, {port_id:?}, {connection:?}");
        //     }
        //     println!("}}")
        // }
    }

    /// Adds a new track
    ///
    /// Returns the [`Result`] of [`TrackId`]
    ///
    /// # Arguments
    /// * `builder` - [`NodeBuilderTrait`] object
    ///
    /// # Errors
    /// Returns error if track already exists
    ///
    pub fn add_track(&mut self, time_range: TimeRange) -> Result<TrackId, Error> {
        let id: TrackId = self.new_id().into();
        self.tracks_map.insert(id, Track::new(id, time_range));
        self.invalidate = true;
        Ok(id)
    }

    /// Adds a new node
    ///
    /// Returns the [`Result`] to reference to node
    ///
    /// # Arguments
    /// * `builder` - [`NodeBuilderTrait`] object
    ///
    /// # Errors
    /// Returns error if track_id not found
    pub fn add_node(
        &mut self,
        track_id: TrackId,
        builder: &dyn NodeBuilderTrait,
    ) -> Result<NodeId, Error> {
        self.add_node_with_parent(track_id, None, builder)
    }

    /// # Errors
    /// Returns error if track_id not found
    pub(crate) fn add_node_with_parent(
        &mut self,
        track_id: TrackId,
        parent_id: Option<NodeId>,
        builder: &dyn NodeBuilderTrait,
    ) -> Result<NodeId, Error> {
        let id = Node::add(self, track_id, parent_id, builder);
        self.invalidate = true;
        id
    }

    /// Replace node
    ///
    /// Returns the [`Result`] to reference to node
    ///
    /// If previous connection are not valid, connections are removed
    ///
    /// If node validation fails, new node is not updated and the previous node
    /// stays as it is.
    ///
    /// # Arguments
    /// * `builder` - [`NodeBuilderTrait`] object
    ///
    /// # Errors
    /// Returns error if node does not exits
    ///
    pub fn replace_node(
        &mut self,
        id: NodeId,
        builder: &dyn NodeBuilderTrait,
    ) -> Result<bool, Error> {
        let invalidate_connections = Node::replace(self, id, builder)?;
        if invalidate_connections
            && self
                .output_port
                .is_some_and(|output_port| output_port.node_id == id)
        {
            self.output_port = None;
        }
        self.invalidate = true;
        Ok(invalidate_connections)
    }

    /// Remove node for `id` including its child nodes, related connections and
    /// frames
    ///
    /// # Arguments
    /// * `id` - [`NodeId`]
    ///
    /// # Errors
    /// Returns error if node not found for `id`
    ///
    pub fn remove_node(&mut self, id: NodeId) -> Result<(), Error> {
        let node = self
            .nodes_map
            .get(&id)
            .ok_or_else(|| Error::msg("Node not found".into()))?;
        if node.parent_id().is_some() {
            return Err(Error::msg("Child nodes can't be removed".into()));
        }
        self.remove_nodes(id, true);
        self.invalidate = true;
        Ok(())
    }

    /// Remove track
    ///
    /// # Errors
    /// Returns error if track not found
    pub fn remove_track(&mut self, id: TrackId) -> Result<Track, Error> {
        let track = self
            .tracks_map
            .remove(&id)
            .ok_or_else(|| Error::msg("Track not found".into()))?;
        let ids: Vec<NodeId> = self
            .nodes_map
            .iter()
            .filter(|(_, node)| node.track_id() == id)
            .map(|(&id, _)| id)
            .collect();
        for id in ids {
            self.remove_nodes(id, true);
        }
        self.invalidate = true;
        Ok(track)
    }

    /// Set the track time range
    ///
    /// # Errors
    /// Returns error if track not found
    pub fn set_track_time_range(
        &mut self,
        id: TrackId,
        time_range: TimeRange,
    ) -> Result<(), Error> {
        let track = self
            .tracks_map
            .get_mut(&id)
            .ok_or_else(|| Error::msg("Track not found".into()))?;
        track.set_time_range(time_range);
        self.invalidate = true;
        Ok(())
    }

    /// Set the output port
    ///
    /// # Errors
    /// Returns error if node or port not found or node is a child node
    pub fn set_output_port(&mut self, port: Option<(NodeId, PortId)>) -> Result<(), Error> {
        if let Some(port) = port {
            let node = self
                .nodes_map
                .get(&port.0)
                .ok_or_else(|| Error::msg("Node not found".into()))?;
            if node.parent_id().is_some() {
                return Err(Error::msg(
                    "Port of child nodes can't be set as output".into(),
                ));
            }
            self.output_port = Some(
                node.get_port(port.1)
                    .ok_or_else(|| Error::msg("Port not found".into()))?,
            );
        } else {
            self.output_port = None;
        }
        self.invalidate = true;
        Ok(())
    }

    #[must_use]
    pub const fn get_output_port(&self) -> Option<Port> {
        self.output_port
    }

    ///
    /// # Errors
    /// Return `Err` if source/target ports invalid
    ///
    pub fn connect_ports(
        &mut self,
        (source_node_id, source_port_id): (NodeId, PortId),
        (target_node_id, target_port_id): (NodeId, PortId),
    ) -> Result<(), Error> {
        let source_node = self
            .nodes_map
            .get(&source_node_id)
            .ok_or_else(|| Error::msg("Source node not found".into()))?;
        let source = source_node
            .get_port(source_port_id)
            .ok_or_else(|| Error::msg("Source port not found".into()))?;
        let target_node = self
            .nodes_map
            .get(&target_node_id)
            .ok_or_else(|| Error::msg("Target node not found".into()))?;
        let target = target_node
            .get_port(target_port_id)
            .ok_or_else(|| Error::msg("Target port not found".into()))?;
        let connection = Connection::new(source, target)?;
        self.connections_map
            .insert((target.node_id, target.id), connection);
        self.invalidate = true;
        Ok(())
    }

    ///
    /// # Errors
    /// Return `Err` if source/target ports invalid
    ///
    pub fn connect_nodes(&mut self, source_id: NodeId, target_id: NodeId) -> Result<(), Error> {
        let source_node = self
            .nodes_map
            .get(&source_id)
            .ok_or_else(|| Error::msg("Invalid source node".into()))?;
        // Find the first auto_connect output port
        let ports = source_node.ports();
        let mut source: Option<Port> = None;
        for port in ports {
            if port.auto_connect && port.kind.is_output() {
                source = Some(*port);
                break;
            }
        }
        let source =
            source.ok_or_else(|| Error::msg("No auto connect port in source node".into()))?;
        let target_node = self
            .nodes_map
            .get(&target_id)
            .ok_or_else(|| Error::msg("Invalid target node".into()))?;
        // Find the first auto_connect input port that is not already connected
        let ports = target_node.ports();
        let mut target: Option<Port> = None;
        for port in ports {
            if port.auto_connect
                && port.kind.is_input()
                && !self.connections_map.contains_key(&(port.node_id, port.id))
            {
                target = Some(*port);
                break;
            }
        }
        let target =
            target.ok_or_else(|| Error::msg("No auto connect port in target node".into()))?;
        // Do not worry about port type, or directions. It will be checked in [`Connection::new`]
        let connection = Connection::new(source, target)?;
        self.connections_map
            .insert((target.node_id, target.id), connection);
        self.invalidate = true;
        Ok(())
    }

    ///
    /// # Errors
    /// Return `Err` target port invalid
    ///
    pub fn unlink_port(
        &mut self,
        (target_node_id, target_port_id): (NodeId, PortId),
    ) -> Result<(), Error> {
        self.connections_map
            .remove(&(target_node_id, target_port_id))
            .ok_or_else(|| Error::msg("Invalid port".into()))?;
        self.invalidate = true;
        Ok(())
    }

    #[must_use]
    pub fn get_node(&self, id: NodeId) -> Option<&Node> {
        self.nodes_map.get(&id)
    }

    #[must_use]
    pub fn min_events_per_frame(&self) -> usize {
        (self.buffer_size() / 2).max(64)
    }

    pub fn clear(&mut self) {
        self.reset();
        self.invalidate = true;
    }

    #[must_use]
    pub fn get_track_count(&self) -> usize {
        self.tracks_map.len()
    }

    pub fn get_tracks(&self, tracks: &mut Vec<Track>) {
        for track in self.tracks_map.values() {
            tracks.push(track.clone());
        }
    }

    #[must_use]
    pub fn get_node_count(&self, track_id: TrackId) -> usize {
        self.nodes_map.iter().fold(0usize, |acc, (_, node)| {
            if node.track_id() == track_id && node.parent_id().is_none() {
                acc + 1
            } else {
                acc
            }
        })
    }

    pub fn get_node_infos(&self, track_id: TrackId, nodes: &mut Vec<NodeInfo>) {
        for node in self.nodes_map.values() {
            if node.track_id() == track_id && node.parent_id().is_none() {
                // Add only user created nodes
                nodes.push(NodeInfo {
                    id: node.id(),
                    track_id: node.track_id(),
                });
            }
        }
    }

    pub const fn stop(&mut self) {
        // Do not add to command queue, instead stop immidiately
        self.state = ProcessorState::Stopped;
    }

    pub const fn set_playing(&mut self, enable: bool) {
        self.state = if enable {
            ProcessorState::Playing
        } else {
            ProcessorState::Paused
        }
    }

    pub fn set_playing_with_fader(&mut self, enable: bool) {
        if enable && self.is_playing() {
            self.set_playing(true);
        } else if !enable && !self.is_playing() {
            self.set_playing(false);
        } else {
            #[allow(clippy::collapsible_else_if)]
            if enable {
                self.set_playing(true);
                self.fader = self.fader.fade_in();
            } else {
                self.fader = self.fader.fade_out();
            }
        }
    }

    #[must_use]
    pub const fn is_fader_done(&self) -> bool {
        self.fader.done()
    }

    #[must_use]
    pub fn seek(&mut self, time: TimeFrom) -> f64 {
        let last_step = self.step;
        match time {
            TimeFrom::Start(time) => {
                self.step = time.to_samples(self.sr);
            }
            TimeFrom::Current(time) => {
                self.step += time.to_samples(self.sr);
            }
            TimeFrom::End(time) => {
                self.step = self.end_time + time.to_samples(self.sr);
            }
        }
        if self.step != last_step {
            let mut ctx = NodeResetCtx {
                sample_rate: self.sr,
                step: self.step,
            };
            for node in self.nodes_map.values_mut() {
                ctx.step = self.step
                    - self
                        .tracks_map
                        .get(&node.track_id())
                        .unwrap()
                        .time_range()
                        .start()
                        .to_samples(self.sr);
                node.inner.reset(&ctx);
            }
            // Clear all frame
            for frames in self.frames_maps.values_mut() {
                for frame in frames.values_mut() {
                    frame.reset();
                }
            }
        }
        TimeUnit::Samples(self.step).to_seconds(self.sr)
    }

    #[must_use]
    pub const fn sample_rate(&self) -> SampleRateBaseType {
        self.sr
    }

    #[must_use]
    pub const fn buffer_size(&self) -> usize {
        self.buffer_size
    }

    #[must_use]
    pub const fn n_channels(&self) -> u16 {
        self.n_channels
    }

    #[must_use]
    pub fn is_running(&self) -> bool {
        self.state != ProcessorState::Stopped
    }

    #[must_use]
    pub fn is_playing(&self) -> bool {
        self.state == ProcessorState::Playing
    }

    #[cfg(feature = "debug")]
    pub fn debug_render_graph(&self, path: String) {
        const NODE_DISTANCE: f64 = 3f64;
        const NODE_SIZE: f64 = 32f64;
        const PORT_DISTANCE: f64 = 1f64;
        const PORT_SIZE: f64 = 16f64;
        use charming::{
            Chart, HtmlRenderer,
            component::Legend,
            element::{Label, LabelPosition, LineStyle, ScaleLimit, Tooltip},
            series::{Graph, GraphCategory, GraphData, GraphLink, GraphNode},
        };
        let mut nodes = Vec::<GraphNode>::new();
        let mut links = Vec::<GraphLink>::new();
        let categories = vec![
            GraphCategory {
                name: "Nodes".into(),
            },
            GraphCategory {
                name: "Input Events".into(),
            },
            GraphCategory {
                name: "Input Signal".into(),
            },
            GraphCategory {
                name: "Output Events".into(),
            },
            GraphCategory {
                name: "Output Signal".into(),
            },
            GraphCategory {
                name: "Proxy".into(),
            },
        ];
        let mut flattened = Vec::<NodeId>::with_capacity(self.nodes_map.capacity());
        flattened.extend_from_slice(&self.nodes);
        for id in self.nodes_map.keys() {
            if !flattened.contains(id) {
                flattened.push(*id);
            }
        }
        // (orbital, index, n_per_orbital)
        let mut state = (0usize, 0usize, 1usize);
        // Distance over perimeter for 5 nodes at `NODE_DISTANCE` radius
        const DISTANCE: f64 = NODE_DISTANCE * std::f64::consts::TAU / 5f64;
        for id in &flattened {
            let node = self.nodes_map.get(id).unwrap();
            let r = NODE_DISTANCE * state.0 as f64;
            let t = state.1 as f64 / state.2 as f64;
            let x = r * (t * std::f64::consts::TAU).cos();
            let y = r * (t * std::f64::consts::TAU).sin();
            state.1 += 1;
            if state.1 >= state.2 {
                state.0 += 1;
                let p = NODE_DISTANCE * state.0 as f64 * std::f64::consts::TAU;
                state.1 = 0;
                state.2 = (p / DISTANCE).round() as usize;
            }
            let graph_node = GraphNode {
                id: format!("N{}", node.id().0),
                name: format!("N{} {}", node.id().0, node.inner.name()),
                x,
                y,
                value: node.id().0 as f64,
                category: 0,
                symbol_size: NODE_SIZE,
                label: None,
            };
            nodes.push(graph_node);
            if let Some(parent_node_id) = node.parent_id() {
                let link = GraphLink {
                    source: format!("N{}", parent_node_id.0),
                    target: format!("N{}", node.id().0),
                    value: None,
                };
                links.push(link);
            }
            let n_ports = node.ports().len();
            for (j, port) in node.ports().iter().enumerate() {
                let category = match port.kind {
                    PortType::EventsIn => 1,
                    PortType::SignalIn => 2,
                    PortType::EventsOut => 3,
                    PortType::SignalOut(_) => 4,
                    PortType::Proxy(_) => 5,
                };
                let t = j as f64 / n_ports as f64;
                let x = x + (PORT_DISTANCE * (t * std::f64::consts::TAU).cos());
                let y = y + (PORT_DISTANCE * (t * std::f64::consts::TAU).sin());
                let postfix = match port.kind {
                    PortType::SignalOut(channel_mask) => format!("[{:?}]", channel_mask),
                    _ => "".to_string(),
                };
                let graph_node = GraphNode {
                    id: format!("N{}P{}", node.id().0, port.id.0),
                    name: format!("N{}P{} {}{}", node.id().0, port.id.0, port.name, postfix),
                    x,
                    y,
                    value: node.id().0 as f64,
                    category,
                    symbol_size: PORT_SIZE,
                    label: None,
                };
                nodes.push(graph_node);
                let link = if port.kind.is_input() {
                    GraphLink {
                        source: format!("N{}P{}", port.node_id.0, port.id.0),
                        target: format!("N{}", node.id().0),
                        value: None,
                    }
                } else {
                    GraphLink {
                        source: format!("N{}", node.id().0),
                        target: format!("N{}P{}", port.node_id.0, port.id.0),
                        value: None,
                    }
                };
                links.push(link);
            }
        }
        for connection in self.connections_map.values() {
            let link = GraphLink {
                source: format!(
                    "N{}P{}",
                    connection.source.node_id.0, connection.source.id.0
                ),
                target: format!(
                    "N{}P{}",
                    connection.target.node_id.0, connection.target.id.0
                ),
                value: None,
            };
            links.push(link);
        }
        for connection in self.connections_map.values() {
            let mut source = connection.source;
            let mut last_source = source;
            while let PortType::Proxy(port_proxy) = source.kind {
                let link = GraphLink {
                    source: format!("N{}P{}", port_proxy.node_id.0, port_proxy.port_id.0),
                    target: format!("N{}P{}", source.node_id.0, source.id.0),
                    value: None,
                };
                links.push(link);
                last_source = source;
                source = self
                    .nodes_map
                    .get(&port_proxy.node_id)
                    .unwrap()
                    .get_port(port_proxy.port_id)
                    .unwrap();
            }
            if source != connection.source {
                let link = GraphLink {
                    source: format!("N{}P{}", source.node_id.0, source.id.0),
                    target: format!("N{}P{}", last_source.node_id.0, last_source.id.0),
                    value: None,
                };
                links.push(link);
            }
        }
        let data = GraphData {
            nodes,
            links,
            categories,
        };
        let chart = Chart::new()
            .tooltip(Tooltip::new())
            .legend(Legend::new().data(data.categories.iter().map(|c| c.name.clone()).collect()))
            .series(
                Graph::new()
                    .name("Dhwani")
                    .roam(true)
                    .label(
                        Label::new()
                            .show(true)
                            .position(LabelPosition::Bottom)
                            .formatter("{b}"),
                    )
                    .edge_symbol(Some(("none".to_string(), "arrow".to_string())))
                    .scale_limit(ScaleLimit::new().min(0.01).max(16))
                    .line_style(LineStyle::new().width(1.0).color("source").curveness(0.25))
                    .data(data),
            );
        let mut renderer = HtmlRenderer::new("Dhwani - Graph", 1920, 1080);
        renderer.save(&chart, path).unwrap();
    }
}