firewheel-graph 0.11.0

Core audio graph algorithm and executor for Firewheel
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
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
use bevy_platform::sync::{
    Arc,
    atomic::{AtomicBool, Ordering},
};
use core::error::Error;
use core::num::NonZeroU32;
use core::time::Duration;
use core::{any::Any, f64};
use firewheel_core::node::{NodeError, ProcStore};
use firewheel_core::{
    StreamInfo,
    channel_config::{ChannelConfig, ChannelCount},
    diff::EventQueue,
    dsp::declick::DeclickValues,
    event::{NodeEvent, NodeEventType},
    node::{AudioNode, DynAudioNode, NodeID},
};
use firewheel_core::{
    dsp::volume::Volume,
    log::{RealtimeLogger, RealtimeLoggerConfig, RealtimeLoggerMainThread},
};
use ringbuf::traits::{Consumer, Producer, Split};
use smallvec::SmallVec;

#[cfg(not(feature = "std"))]
use num_traits::Float;

#[cfg(feature = "scheduled_events")]
use bevy_platform::time::Instant;
#[cfg(feature = "scheduled_events")]
use core::cell::RefCell;
#[cfg(feature = "scheduled_events")]
use firewheel_core::clock::{AudioClock, DurationSeconds};

#[cfg(all(not(feature = "std"), feature = "musical_transport"))]
use bevy_platform::prelude::Box;
#[cfg(not(feature = "std"))]
use bevy_platform::prelude::Vec;

use crate::{
    error::{ActivateError, RemoveNodeError},
    processor::SharedFlags,
};
use crate::{
    error::{AddEdgeError, UpdateError},
    graph::{AudioGraph, Edge, EdgeID, NodeEntry, PortIdx},
    processor::{
        ContextToProcessorMsg, FirewheelProcessor, FirewheelProcessorInner, ProcessorToContextMsg,
    },
};
use crate::{
    error::{CompileGraphError, DeactivateError},
    processor::{
        BufferOutOfSpaceMode, FirewheelProcessorConfig, ProfilingData,
        profiling::{ProfilerRx, ProfilerTx},
    },
};

#[cfg(feature = "scheduled_events")]
use crate::processor::{ClearScheduledEventsEvent, SharedClock};
#[cfg(feature = "scheduled_events")]
use firewheel_core::clock::EventInstant;

#[cfg(feature = "musical_transport")]
use firewheel_core::clock::TransportState;

/// Information about the running audio stream.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ActivateInfo {
    /// The sample rate of the audio stream.
    pub sample_rate: NonZeroU32,
    /// The maximum number of frames that can appear in a single process cyle.
    pub max_block_frames: NonZeroU32,
    /// The number of input audio channels in the stream.
    pub num_stream_in_channels: u32,
    /// The number of output audio channels in the stream.
    pub num_stream_out_channels: u32,
    /// The latency of the input to output stream in seconds.
    pub input_to_output_latency_seconds: f64,
}

/// The configuration of a Firewheel context.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FirewheelConfig {
    /// The number of input channels in the audio graph.
    pub num_graph_inputs: ChannelCount,
    /// The number of output channels in the audio graph.
    pub num_graph_outputs: ChannelCount,

    /// Extra configuration flags.
    ///
    /// By default, no flags are set.
    pub flags: FirewheelFlags,

    /// An initial capacity to allocate for the nodes in the audio graph.
    ///
    /// By default this is set to `128`.
    pub initial_node_capacity: u32,
    /// An initial capacity to allocate for the edges in the audio graph.
    ///
    /// By default this is set to `256`.
    pub initial_edge_capacity: u32,
    /// The amount of time in seconds to fade in/out when pausing/resuming
    /// to avoid clicks and pops.
    ///
    /// By default this is set to `10.0 / 1_000.0`.
    pub declick_seconds: f32,
    /// The initial capacity for a group of events.
    ///
    /// By default this is set to `128`.
    pub initial_event_group_capacity: u32,
    /// The capacity of the engine's internal message channel.
    ///
    /// By default this is set to `64`.
    pub channel_capacity: u32,
    /// The maximum number of events that can be sent in a single call
    /// to [`AudioNodeProcessor::process`].
    ///
    /// By default this is set to `128`.
    ///
    /// [`AudioNodeProcessor::process`]: firewheel_core::node::AudioNodeProcessor::process
    pub event_queue_capacity: usize,
    /// The maximum number of immediate events (events that do *NOT* have a
    /// scheduled time component) that can be stored at once in the audio
    /// thread.
    ///
    /// By default this is set to `512`.
    pub immediate_event_capacity: usize,
    /// The maximum number of scheduled events (events that have a scheduled
    /// time component) that can be stored at once in the audio thread.
    ///
    /// This can be set to `0` to save some memory if you do not plan on using
    /// scheduled events.
    ///
    /// This has no effect if the `scheduled_events` feature is disabled.
    ///
    /// By default this is set to `512`.
    pub scheduled_event_capacity: usize,
    /// How to handle event buffers on the audio thread running out of space.
    ///
    /// By default this is set to [`BufferOutOfSpaceMode::AllocateOnAudioThread`].
    pub buffer_out_of_space_mode: BufferOutOfSpaceMode,

    /// The configuration of the realtime safe logger.
    pub logger_config: RealtimeLoggerConfig,

    /// The initial number of slots to allocate for the [`ProcStore`].
    ///
    /// By default this is set to `8`.
    pub proc_store_capacity: usize,

    /// If `Some`, then inputs to the audio graph will be clamped to silence if the
    /// max peak amplitude is less than the given volume. This can help improve the
    /// performance of processing chains which use the graph inputs.
    ///
    /// If this is `None`, then no clamping will occur.
    ///
    /// Note, while this is functionally a noise gate, it is not a good noise gate,
    /// and values above -70dB may cause audible clicking. If you need to increase
    /// the threshold, it is recommended to instead use a dedicated noise gate node.
    ///
    /// By default this is set to `Some(Volume::Decibels(-70.0)`.
    pub clamp_graph_inputs_below: Option<Volume>,
}

impl Default for FirewheelConfig {
    fn default() -> Self {
        Self {
            num_graph_inputs: ChannelCount::ZERO,
            num_graph_outputs: ChannelCount::STEREO,
            flags: FirewheelFlags::default(),
            initial_node_capacity: 128,
            initial_edge_capacity: 256,
            declick_seconds: DeclickValues::DEFAULT_FADE_SECONDS,
            initial_event_group_capacity: 128,
            channel_capacity: 64,
            event_queue_capacity: 128,
            immediate_event_capacity: 512,
            scheduled_event_capacity: 512,
            buffer_out_of_space_mode: BufferOutOfSpaceMode::AllocateOnAudioThread,
            logger_config: RealtimeLoggerConfig::default(),
            proc_store_capacity: 8,
            clamp_graph_inputs_below: Some(Volume::Decibels(-70.0)),
        }
    }
}

/// Configuration flags for a [`FirewheelContext`]
///
/// Unlike [`FirewheelConfig`], these flags can be changed after the context has
/// been created.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(bevy_reflect::Reflect))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FirewheelFlags {
    /// Hard clip all samples in the final output buffer to the range `[-1.0, 1.0]`.
    ///
    /// This usually isn't necessary since the OS itself generally hard clips the
    /// output, but it is here if you need it.
    ///
    /// By default this is set to `false`.
    pub hard_clip_outputs: bool,

    /// Detect when a sample in the final output buffer falls outside the range
    /// `[-1.0, 1.0]`. If a sample falls outside this range, then
    /// [`FirewheelContext::clipping_occurred`] will return `true`.
    ///
    /// This check takes place before hard clipping if the
    /// [`FirewheelFlags::hard_clip_outputs`] is set to `true`.
    ///
    /// By default this is set to `false`.
    pub detect_clipping_on_output: bool,

    /// Validate that all samples in the final output buffer are a valid finite
    /// number. If a non finite number is detected, then the sample will will be
    /// set to `0.0` and an error is logged.
    ///
    /// By default this is set to `false`.
    pub validate_output_is_finite: bool,

    /// Force all of a node's output buffers to be cleared before processing.
    /// This shouldn't be necessary, but it can be used to debug nodes that
    /// are misusing the silence flag feature.
    ///
    /// By default this is set to `false`.
    pub force_clear_buffers: bool,

    /// Enables performance profiling for engine bookkeeping operations on the
    /// audio thread such as message handling, event sorting, event searching,
    /// and final output processing.
    ///
    /// By default this is set to `false`.
    pub profile_engine_bookkeeping: bool,

    /// Enable per-node performance profiling.
    ///
    /// This has no effect when the `node_profiling` feature is disabled.
    ///
    /// By default this is set to `false`.
    pub profile_nodes: bool,
}

bitflags::bitflags! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
    pub struct FirewheelBitFlags: u32 {
        const HARD_CLIP_OUTPUTS = 1 << 0;
        const DETECT_CLIPPING_ON_OUTPUT = 1 << 1;
        const VALIDATE_OUTPUT_IS_FINITE = 1 << 2;
        const FORCE_CLEAR_BUFFERS = 1 << 3;
        const PROFILE_ENGINE_BOOKKEEPING = 1 << 4;
        const PROFILE_NODES = 1 << 5;
    }
}

impl From<FirewheelFlags> for FirewheelBitFlags {
    fn from(value: FirewheelFlags) -> Self {
        let mut b = Self::empty();
        b.set(Self::HARD_CLIP_OUTPUTS, value.hard_clip_outputs);
        b.set(
            Self::DETECT_CLIPPING_ON_OUTPUT,
            value.detect_clipping_on_output,
        );
        b.set(
            Self::VALIDATE_OUTPUT_IS_FINITE,
            value.validate_output_is_finite,
        );
        b.set(Self::FORCE_CLEAR_BUFFERS, value.force_clear_buffers);
        b.set(
            Self::PROFILE_ENGINE_BOOKKEEPING,
            value.profile_engine_bookkeeping,
        );
        b.set(Self::PROFILE_NODES, value.profile_nodes);
        b
    }
}

pub(crate) struct ProcessorChannel {
    pub(crate) shared_flags: Arc<SharedFlags>,
    pub(crate) from_context_rx: ringbuf::HeapCons<ContextToProcessorMsg>,
    pub(crate) to_context_tx: ringbuf::HeapProd<ProcessorToContextMsg>,
    pub(crate) logger: RealtimeLogger,
    pub(crate) store: ProcStore,
    pub(crate) profiler_tx: ProfilerTx,
    #[cfg(feature = "scheduled_events")]
    pub(crate) shared_clock_input: triple_buffer::Input<SharedClock>,
}

/// A Firewheel context
pub struct FirewheelContext {
    graph: AudioGraph,

    to_processor_tx: ringbuf::HeapProd<ContextToProcessorMsg>,
    from_processor_rx: ringbuf::HeapCons<ProcessorToContextMsg>,
    processor_drop_flag: Option<Arc<AtomicBool>>,
    profiler_rx: ProfilerRx,
    logger_rx: RealtimeLoggerMainThread,

    pending_processor_channel: Option<ProcessorChannel>,
    processor_drop_rx: Option<ringbuf::HeapCons<FirewheelProcessorInner>>,

    #[cfg(feature = "scheduled_events")]
    shared_clock_output: RefCell<triple_buffer::Output<SharedClock>>,

    sample_rate: NonZeroU32,
    sample_rate_recip: f64,
    stream_info: Option<StreamInfo>,
    shared_flags: Arc<SharedFlags>,

    #[cfg(feature = "musical_transport")]
    transport_state: Box<TransportState>,
    #[cfg(feature = "musical_transport")]
    transport_state_alloc_reuse: Option<Box<TransportState>>,

    // Re-use the allocations for groups of events.
    event_group_pool: Vec<Vec<NodeEvent>>,
    event_group: Vec<NodeEvent>,
    initial_event_group_capacity: usize,

    #[cfg(feature = "scheduled_events")]
    queued_clear_scheduled_events: Vec<ClearScheduledEventsEvent>,

    config: FirewheelConfig,
}

impl FirewheelContext {
    /// Create a new Firewheel context.
    pub fn new(config: FirewheelConfig) -> Self {
        let (to_processor_tx, from_context_rx) =
            ringbuf::HeapRb::<ContextToProcessorMsg>::new(config.channel_capacity as usize).split();
        let (to_context_tx, from_processor_rx) =
            ringbuf::HeapRb::<ProcessorToContextMsg>::new(config.channel_capacity as usize * 2)
                .split();

        let initial_event_group_capacity = config.initial_event_group_capacity as usize;
        let mut event_group_pool = Vec::with_capacity(16);
        for _ in 0..3 {
            event_group_pool.push(Vec::with_capacity(initial_event_group_capacity));
        }

        let graph = AudioGraph::new(&config);

        #[cfg(feature = "scheduled_events")]
        let (shared_clock_input, shared_clock_output) =
            triple_buffer::triple_buffer(&SharedClock::default());

        let (logger, logger_rx) = firewheel_core::log::realtime_logger(config.logger_config);
        let (profiler_tx, profiler_rx) = crate::processor::profiling::profiler_channel(
            config.initial_node_capacity as usize,
            #[cfg(feature = "node_profiling")]
            graph.graph_out_node(),
        );
        let shared_flags = Arc::new(SharedFlags::default());

        let store = ProcStore::with_capacity(config.proc_store_capacity);

        Self {
            graph,
            to_processor_tx,
            from_processor_rx,
            processor_drop_flag: None,
            profiler_rx,
            logger_rx,
            pending_processor_channel: Some(ProcessorChannel {
                shared_flags: Arc::clone(&shared_flags),
                from_context_rx,
                to_context_tx,
                logger,
                store,
                profiler_tx,
                #[cfg(feature = "scheduled_events")]
                shared_clock_input,
            }),
            processor_drop_rx: None,
            #[cfg(feature = "scheduled_events")]
            shared_clock_output: RefCell::new(shared_clock_output),
            sample_rate: NonZeroU32::new(44100).unwrap(),
            sample_rate_recip: 44100.0f64.recip(),
            stream_info: None,
            shared_flags,
            #[cfg(feature = "musical_transport")]
            transport_state: Box::new(TransportState::default()),
            #[cfg(feature = "musical_transport")]
            transport_state_alloc_reuse: None,
            event_group_pool,
            event_group: Vec::with_capacity(initial_event_group_capacity),
            initial_event_group_capacity,
            #[cfg(feature = "scheduled_events")]
            queued_clear_scheduled_events: Vec::new(),
            config,
        }
    }

    /// Try to modify the graph. If the given closure returns an error (or
    /// if a cycle is detected), then any changes made to the graph inside
    /// the closure will be reverted.
    ///
    /// Any custom error type can be used, though
    /// [`ModifyGraphError`](crate::error::ModifyGraphError) is provided
    /// for convenience.
    pub fn try_modify_graph<E: Error>(
        &mut self,
        f: impl FnOnce(&mut Self) -> Result<(), E>,
    ) -> Result<(), E> {
        self.graph.begin_modify_guard();

        let res = (f)(self);

        self.graph.end_modify_guard(res.is_err());

        res
    }

    /// Get an immutable reference to the processor store.
    ///
    /// If an audio stream is currently running, this will return `None`.
    pub fn proc_store(&self) -> Option<&ProcStore> {
        if let Some(proc_channel) = &self.pending_processor_channel {
            Some(&proc_channel.store)
        } else if let Some(processor) = self.processor_drop_rx.as_ref() {
            if let Some(processor) = processor.last() {
                if processor.poisoned {
                    panic!("The audio thread has panicked!");
                }

                Some(&processor.extra.store)
            } else {
                None
            }
        } else {
            None
        }
    }

    /// Get a mutable reference to the processor store.
    ///
    /// If an audio stream is currently running, this will return `None`.
    pub fn proc_store_mut(&mut self) -> Option<&mut ProcStore> {
        if let Some(proc_channel) = &mut self.pending_processor_channel {
            Some(&mut proc_channel.store)
        } else if let Some(processor) = self.processor_drop_rx.as_mut() {
            if let Some(processor) = processor.last_mut() {
                if processor.poisoned {
                    panic!("The audio thread has panicked!");
                }

                Some(&mut processor.extra.store)
            } else {
                None
            }
        } else {
            None
        }
    }

    /// Returns `true` if the context is currently active (the [`FirewheelProcessor`]
    /// counterpart is still alive).
    pub fn is_active(&self) -> bool {
        if let Some(rx) = &self.processor_drop_rx {
            rx.try_peek().is_none()
        } else {
            false
        }
    }

    /// Activate the context with the given audio stream.
    ///
    /// Use [`FirewheelContext::is_active`] to check if the context is ready to
    /// be activated.
    ///
    /// Note, in rare cases where the audio thread crashes without cleanly dropping
    /// its contents, this may never succeed. Consider adding a timeout to avoid
    /// deadlocking.
    pub fn activate(&mut self, info: ActivateInfo) -> Result<FirewheelProcessor, ActivateError> {
        let ActivateInfo {
            sample_rate,
            max_block_frames,
            num_stream_in_channels,
            num_stream_out_channels,
            input_to_output_latency_seconds,
        } = info;

        if self.is_active() {
            return Err(ActivateError::AlreadyActive);
        }

        let maybe_proc_channel = self.pending_processor_channel.take();

        let prev_sample_rate = if maybe_proc_channel.is_some() {
            sample_rate
        } else {
            self.sample_rate
        };

        let stream_info = StreamInfo {
            sample_rate,
            sample_rate_recip: (sample_rate.get() as f64).recip(),
            prev_sample_rate,
            max_block_frames,
            num_stream_in_channels,
            num_stream_out_channels,
            input_to_output_latency_seconds,
            declick_frames: NonZeroU32::new(
                (self.config.declick_seconds * sample_rate.get() as f32).round() as u32,
            )
            .unwrap_or(NonZeroU32::MIN),
        };

        self.sample_rate = stream_info.sample_rate;
        self.sample_rate_recip = stream_info.sample_rate_recip;

        let schedule = self.graph.compile(&stream_info)?;

        let (drop_tx, drop_rx) = ringbuf::HeapRb::<FirewheelProcessorInner>::new(1).split();

        let processor = if let Some(proc_channel) = maybe_proc_channel {
            FirewheelProcessorInner::new(
                FirewheelProcessorConfig {
                    flags: self.config.flags.into(),
                    immediate_event_buffer_capacity: self.config.immediate_event_capacity,
                    buffer_out_of_space_mode: self.config.buffer_out_of_space_mode,
                    clamp_graph_inputs_below_amp: self
                        .config
                        .clamp_graph_inputs_below
                        .map(|v| v.amp()),
                    node_event_buffer_capacity: self.config.event_queue_capacity,
                    #[cfg(feature = "scheduled_events")]
                    scheduled_event_buffer_capacity: self.config.scheduled_event_capacity,
                },
                proc_channel,
                &stream_info,
            )
        } else {
            let mut processor = self.processor_drop_rx.as_mut().unwrap().try_pop().unwrap();

            if processor.poisoned {
                panic!("The audio thread has panicked!");
            }

            processor.new_stream(&stream_info);

            processor
        };

        if self
            .send_message_to_processor(ContextToProcessorMsg::NewSchedule(schedule))
            .is_err()
        {
            panic!("Firewheel message channel is full!");
        }

        self.processor_drop_rx = Some(drop_rx);
        self.stream_info = Some(stream_info);

        let drop_flag = Arc::new(AtomicBool::new(false));
        self.processor_drop_flag = Some(drop_flag.clone());

        Ok(FirewheelProcessor::new(processor, drop_tx, drop_flag))
    }

    /// Request the context to be deactivated if it is active.
    ///
    /// This does not block the current thread. It might take a while for the
    /// context to deactivate. Use [`FirewheelContext::is_active`] to check when
    /// the context has been deactivated.
    ///
    /// Note, another way to deactivate the context is to drop the [`FirewheelProcessor`]
    /// counterpart in the audio thread.
    pub fn request_deactivate(&mut self) {
        if let Some(flag) = &self.processor_drop_flag {
            flag.store(true, Ordering::Relaxed);
        }
    }

    /// If the context is active, request the context to be deactivated and wait
    /// for the context to be deactivated before returning.
    ///
    /// If the `timeout` duration has been reached and the context is still not
    /// deactivated, then an error is returned.
    #[cfg(not(target_family = "wasm"))]
    pub fn deactivate_blocking(&mut self, timeout: Duration) -> Result<(), DeactivateError> {
        self.request_deactivate();

        let now = bevy_platform::time::Instant::now();

        while self.is_active() {
            if now.elapsed() > timeout {
                return Err(DeactivateError::TimedOut);
            }

            bevy_platform::thread::sleep(core::time::Duration::from_millis(1));
        }

        Ok(())
    }

    /// Information about the running audio stream.
    ///
    /// Returns `None` if the context is not currently active.
    pub fn stream_info(&self) -> Option<&StreamInfo> {
        if self.is_active() {
            self.stream_info.as_ref()
        } else {
            None
        }
    }

    /// Get the current time of the audio clock, without accounting for the delay
    /// between when the clock was last updated and now.
    ///
    /// For most use cases you probably want to use [`FirewheelContext::audio_clock_corrected`]
    /// instead, but this method is provided if needed.
    ///
    /// Note, due to the nature of audio processing, this clock is is *NOT* synced with
    /// the system's time (`Instant::now`). (Instead it is based on the amount of data
    /// that has been processed.) For applications where the timing of audio events is
    /// critical (i.e. a rhythm game), sync the game to this audio clock instead of the
    /// OS's clock (`Instant::now()`).
    ///
    /// Note, calling this method is not super cheap, so avoid calling it many
    /// times within the same game loop iteration if possible.
    #[cfg(feature = "scheduled_events")]
    pub fn audio_clock(&self) -> AudioClock {
        // Reading the latest value of the clock doesn't meaningfully mutate
        // state, so treat it as an immutable operation with interior mutability.
        //
        // PANIC SAFETY: This struct is the only place this is ever borrowed, so this
        // will never panic.
        let mut clock_borrowed = self.shared_clock_output.borrow_mut();
        let clock = clock_borrowed.read();

        AudioClock {
            samples: clock.clock_samples,
            seconds: clock
                .clock_samples
                .to_seconds(self.sample_rate, self.sample_rate_recip),
            #[cfg(feature = "musical_transport")]
            musical: clock.current_playhead,
            #[cfg(feature = "musical_transport")]
            transport_is_playing: clock.transport_is_playing,
            update_instant: self.is_active().then_some(clock.update_instant),
        }
    }

    /// Get the current time of the audio clock.
    ///
    /// Unlike, [`FirewheelContext::audio_clock`], this method accounts for the delay
    /// between when the audio clock was last updated and now, leading to a more
    /// accurate result for games and other applications.
    ///
    /// If the delay could not be determined (i.e. an audio stream is not currently
    /// running), then this will assume there was no delay between when the audio
    /// clock was last updated and now.
    ///
    /// Note, due to the nature of audio processing, this clock is is *NOT* synced with
    /// the system's time (`Instant::now`). (Instead it is based on the amount of data
    /// that has been processed.) For applications where the timing of audio events is
    /// critical (i.e. a rhythm game), sync the game to this audio clock instead of the
    /// OS's clock (`Instant::now()`).
    ///
    /// Note, calling this method is not super cheap, so avoid calling it many
    /// times within the same game loop iteration if possible.
    #[cfg(feature = "scheduled_events")]
    pub fn audio_clock_corrected(&self) -> AudioClock {
        // Reading the latest value of the clock doesn't meaningfully mutate
        // state, so treat it as an immutable operation with interior mutability.
        //
        // PANIC SAFETY: This struct is the only place this is ever borrowed, so this
        // will never panic.
        let mut clock_borrowed = self.shared_clock_output.borrow_mut();
        let clock = clock_borrowed.read();

        if !self.is_active() {
            // The audio thread is not currently running, so just return the
            // latest value of the clock.
            return AudioClock {
                samples: clock.clock_samples,
                seconds: clock
                    .clock_samples
                    .to_seconds(self.sample_rate, self.sample_rate_recip),
                #[cfg(feature = "musical_transport")]
                musical: clock.current_playhead,
                #[cfg(feature = "musical_transport")]
                transport_is_playing: clock.transport_is_playing,
                update_instant: None,
            };
        }

        let update_instant = clock.update_instant;
        let delay = update_instant.elapsed();

        // Account for the delay between when the clock was last updated and now.
        let delta_seconds = DurationSeconds(delay.as_secs_f64());

        let samples = clock.clock_samples + delta_seconds.to_samples(self.sample_rate);

        #[cfg(feature = "musical_transport")]
        let musical = clock.current_playhead.map(|musical_time| {
            if clock.transport_is_playing
                && let Some(transport) = &self.transport_state.transport
            {
                transport.delta_seconds_from(musical_time, delta_seconds, clock.speed_multiplier)
            } else {
                musical_time
            }
        });

        AudioClock {
            samples,
            seconds: samples.to_seconds(self.sample_rate, self.sample_rate_recip),
            #[cfg(feature = "musical_transport")]
            musical,
            #[cfg(feature = "musical_transport")]
            transport_is_playing: clock.transport_is_playing,
            update_instant: Some(update_instant),
        }
    }

    /// Get the instant the audio clock was last updated.
    ///
    /// If the audio thread is not currently running, or if the instant could not
    /// be determined for any other reason, then this will return `None`.
    ///
    /// Note, calling this method is not super cheap, so avoid calling it many
    /// times within the same game loop iteration if possible.
    #[cfg(feature = "scheduled_events")]
    pub fn audio_clock_instant(&self) -> Option<Instant> {
        // Reading the latest value of the clock doesn't meaningfully mutate
        // state, so treat it as an immutable operation with interior mutability.
        //
        // PANIC SAFETY: This struct is the only place this is ever borrowed, so this
        // will never panic.
        let mut clock_borrowed = self.shared_clock_output.borrow_mut();
        let clock = clock_borrowed.read();

        self.is_active().then_some(clock.update_instant)
    }

    /// Sync the state of the musical transport.
    ///
    /// If the message channel is full, then this will return an error.
    #[cfg(feature = "musical_transport")]
    pub fn sync_transport(&mut self, transport: &TransportState) -> Result<(), UpdateError> {
        if &*self.transport_state != transport {
            let transport_msg = if let Some(mut t) = self.transport_state_alloc_reuse.take() {
                *t = transport.clone();
                t
            } else {
                Box::new(transport.clone())
            };

            self.send_message_to_processor(ContextToProcessorMsg::SetTransportState(transport_msg))
                .map_err(|(_, e)| e)?;

            *self.transport_state = transport.clone();
        }

        Ok(())
    }

    /// Get the current transport state.
    #[cfg(feature = "musical_transport")]
    pub fn transport_state(&self) -> &TransportState {
        &self.transport_state
    }

    /// Get the current transport state.
    #[cfg(feature = "musical_transport")]
    pub fn transport(&self) -> &TransportState {
        &self.transport_state
    }

    /// The current configuration flags being used by this context.
    pub fn flags(&self) -> &FirewheelFlags {
        &self.config.flags
    }

    /// Set the configuration flags.
    ///
    /// This can be set while the context is active or inactive.
    ///
    /// If the message channel is full, then this will return an error.
    pub fn set_flags(&mut self, flags: FirewheelFlags) -> Result<(), UpdateError> {
        if self.config.flags == flags {
            return Ok(());
        }
        self.config.flags = flags;

        self.send_message_to_processor(ContextToProcessorMsg::SetFlags(flags.into()))
            .map_err(|(_, e)| e)
    }

    /// Returns `true` if both the `FirewheelFlags::VALIDATE_OUTPUT_DOES_NOT_CLIP`
    /// flag is set and a sample in the final output buffer fell outside the range
    /// `[-1.0, 1.0]`.
    ///
    /// Calling this method resets the internal flag.
    pub fn clipping_occurred(&self) -> bool {
        self.shared_flags
            .clipping_occurred
            .swap(false, Ordering::Relaxed)
    }

    /// Retrieve the latest performance profiling data.
    pub fn profiling_data(&mut self) -> &ProfilingData {
        self.profiler_rx.fetch_info()
    }

    /// Update the firewheel context.
    ///
    /// This must be called regularly (i.e. once every frame).
    pub fn update(&mut self) -> Result<(), UpdateError> {
        self.logger_rx.flush(
            |msg| {
                #[cfg(feature = "tracing")]
                tracing::error!("{}", msg);

                #[cfg(all(feature = "log", not(feature = "tracing")))]
                log::error!("{}", msg);

                let _ = msg;
            },
            |msg| {
                #[cfg(feature = "tracing")]
                tracing::debug!("{}", msg);

                #[cfg(all(feature = "log", not(feature = "tracing")))]
                log::debug!("{}", msg);

                let _ = msg;
            },
        );

        firewheel_core::collector::GlobalRtGc::collect();

        for msg in self.from_processor_rx.pop_iter() {
            match msg {
                ProcessorToContextMsg::DropEventGroup(mut event_group) => {
                    event_group.clear();
                    self.event_group_pool.push(event_group);
                }
                ProcessorToContextMsg::DropSchedule(schedule_data) => {
                    self.graph.drop_old_schedule_data(schedule_data);
                }
                #[cfg(feature = "musical_transport")]
                ProcessorToContextMsg::DropTransportState(transport_state) => {
                    if self.transport_state_alloc_reuse.is_none() {
                        self.transport_state_alloc_reuse = Some(transport_state);
                    }
                }
                #[cfg(feature = "scheduled_events")]
                ProcessorToContextMsg::DropClearScheduledEvents(msgs) => {
                    let _ = msgs;
                }
            }
        }

        self.graph
            .update(self.stream_info.as_ref(), &mut self.event_group);

        if self.is_active() {
            if self.graph.needs_compile() {
                let schedule_data = self.graph.compile(self.stream_info.as_ref().unwrap())?;

                if let Err((msg, e)) = self
                    .send_message_to_processor(ContextToProcessorMsg::NewSchedule(schedule_data))
                {
                    let ContextToProcessorMsg::NewSchedule(schedule) = msg else {
                        unreachable!();
                    };

                    self.graph.on_schedule_send_failed(schedule);

                    return Err(e);
                }
            }

            #[cfg(feature = "scheduled_events")]
            if !self.queued_clear_scheduled_events.is_empty() {
                let msgs: SmallVec<[ClearScheduledEventsEvent; 1]> =
                    self.queued_clear_scheduled_events.drain(..).collect();

                if let Err((msg, e)) = self
                    .send_message_to_processor(ContextToProcessorMsg::ClearScheduledEvents(msgs))
                {
                    let ContextToProcessorMsg::ClearScheduledEvents(mut msgs) = msg else {
                        unreachable!();
                    };

                    self.queued_clear_scheduled_events = msgs.drain(..).collect();

                    return Err(e);
                }
            }

            if !self.event_group.is_empty() {
                let mut next_event_group = self
                    .event_group_pool
                    .pop()
                    .unwrap_or_else(|| Vec::with_capacity(self.initial_event_group_capacity));
                core::mem::swap(&mut next_event_group, &mut self.event_group);

                if let Err((msg, e)) = self
                    .send_message_to_processor(ContextToProcessorMsg::EventGroup(next_event_group))
                {
                    let ContextToProcessorMsg::EventGroup(mut event_group) = msg else {
                        unreachable!();
                    };

                    core::mem::swap(&mut event_group, &mut self.event_group);
                    self.event_group_pool.push(event_group);

                    return Err(e);
                }
            }
        } else {
            self.stream_info = None;
            self.graph.deactivate();
        }

        Ok(())
    }

    /// The ID of the graph input node
    pub fn graph_in_node_id(&self) -> NodeID {
        self.graph.graph_in_node()
    }

    /// The ID of the graph output node
    pub fn graph_out_node_id(&self) -> NodeID {
        self.graph.graph_out_node()
    }

    /// Add a node to the audio graph.
    pub fn add_node<T: AudioNode + 'static>(
        &mut self,
        node: T,
        config: Option<T::Configuration>,
    ) -> Result<NodeID, NodeError> {
        self.graph.add_node(node, config)
    }

    /// Add a node to the audio graph which implements the type-erased [`DynAudioNode`] trait.
    pub fn add_dyn_node<T: DynAudioNode + 'static>(
        &mut self,
        node: T,
    ) -> Result<NodeID, NodeError> {
        self.graph.add_dyn_node(node)
    }

    /// Add a node to the audio graph with the given bypass state.
    pub fn add_node_bypassed<T: AudioNode + 'static>(
        &mut self,
        node: T,
        config: Option<T::Configuration>,
        bypassed: bool,
    ) -> Result<NodeID, NodeError> {
        let node_id = self.add_node(node, config)?;
        if bypassed {
            self.queue_event_for(node_id, NodeEventType::SetBypassed(true));
        }
        Ok(node_id)
    }

    /// Add a node with the given bypass state to the audio graph which implements
    /// the type-erased [`DynAudioNode`] trait.
    pub fn add_dyn_node_bypassed<T: DynAudioNode + 'static>(
        &mut self,
        node: T,
        bypassed: bool,
    ) -> Result<NodeID, NodeError> {
        let node_id = self.graph.add_dyn_node(node)?;
        if bypassed {
            self.queue_event_for(node_id, NodeEventType::SetBypassed(true));
        }
        Ok(node_id)
    }

    /// Remove the given node from the audio graph.
    ///
    /// This will automatically remove all edges from the graph that
    /// were connected to this node.
    ///
    /// On success, this returns a list of all edges that were removed
    /// from the graph as a result of removing this node.
    ///
    /// This will return an error if the ID is of the graph input or graph
    /// output node.
    pub fn remove_node(&mut self, node_id: NodeID) -> Result<SmallVec<[Edge; 4]>, RemoveNodeError> {
        self.graph.remove_node(node_id, false)
    }

    /// Returns `true` if the node exists in the graph.
    pub fn contains_node(&self, id: NodeID) -> bool {
        self.graph.contains_node(id)
    }

    /// Get information about a node in the graph.
    ///
    /// If the node does not exist in the graph, then `None` will be returned.
    pub fn node_info(&self, id: NodeID) -> Option<&NodeEntry> {
        self.graph.node_info(id)
    }

    /// Get the [`ChannelConfig`] of a node in the graph.
    ///
    /// If the node does not exist in the graph, then `None` will be returned.
    pub fn node_channel_config(&self, id: NodeID) -> Option<ChannelConfig> {
        self.graph.node_info(id).map(|n| n.info.channel_config)
    }

    /// Get an immutable reference to the custom state of a node.
    ///
    /// If the node does not exist in the graph, then `None` will be returned.
    pub fn node_state<T: 'static>(&self, id: NodeID) -> Option<&T> {
        self.graph.node_state(id)
    }

    /// Get a type-erased, immutable reference to the custom state of a node.
    ///
    /// If the node does not exist in the graph, then `None` will be returned.
    pub fn node_state_dyn(&self, id: NodeID) -> Option<&dyn Any> {
        self.graph.node_state_dyn(id)
    }

    /// Get a mutable reference to the custom state of a node.
    ///
    /// If the node does not exist in the graph, then `None` will be returned.
    pub fn node_state_mut<T: 'static>(&mut self, id: NodeID) -> Option<&mut T> {
        self.graph.node_state_mut(id)
    }

    /// Get a type-erased, mutable reference to the custom state of a node.
    ///
    /// If the node does not exist in the graph, then `None` will be returned.
    pub fn node_state_dyn_mut(&mut self, id: NodeID) -> Option<&mut dyn Any> {
        self.graph.node_state_dyn_mut(id)
    }

    /// Get a list of all the existing nodes in the graph.
    pub fn nodes(&self) -> impl Iterator<Item = &NodeEntry> {
        self.graph.nodes()
    }

    /// Get a list of all the existing edges in the graph.
    pub fn edges(&self) -> impl Iterator<Item = &Edge> {
        self.graph.edges()
    }

    /// Set the number of input and output channels to and from the audio graph.
    ///
    /// Returns the list of edges that were removed.
    pub fn set_graph_channel_config(
        &mut self,
        channel_config: ChannelConfig,
    ) -> SmallVec<[Edge; 4]> {
        self.graph.set_graph_channel_config(channel_config, false)
    }

    /// Add connections (edges) between two nodes to the graph.
    ///
    /// * `src_node` - The ID of the source node.
    /// * `dst_node` - The ID of the destination node.
    /// * `ports_src_dst` - The port indices for each connection to make,
    ///   where the first value in a tuple is the output port on `src_node`,
    ///   and the second value in that tuple is the input port on `dst_node`.
    /// * `check_for_cycles` - If `true`, then this will run a check to
    ///   see if adding these edges will create a cycle in the graph, and
    ///   return an error if it does. Note, checking for cycles can be quite
    ///   expensive, so avoid enabling this when calling this method many times
    ///   in a row.
    ///
    /// If successful, then this returns a list of edge IDs in order.
    ///
    /// If this returns an error, then the audio graph has not been
    /// modified.
    pub fn connect(
        &mut self,
        src_node: NodeID,
        dst_node: NodeID,
        ports_src_dst: &[(PortIdx, PortIdx)],
        check_for_cycles: bool,
    ) -> Result<SmallVec<[EdgeID; 4]>, AddEdgeError> {
        self.graph
            .connect(src_node, dst_node, ports_src_dst, check_for_cycles, false)
    }

    /// Connect two nodes in the graph, connecting output port 0 to input port
    /// 0, output port 1 to input port 1, etc.
    ///
    /// If the number of output ports on `src_node` does not equal the number
    /// of input ports on `dst_node`, then only the first valid ports will be
    /// connected.
    ///
    /// If successful, then this returns a list of edge IDs in order.
    ///
    /// If this returns an error, then the audio graph has not been
    /// modified.
    pub fn auto_connect(
        &mut self,
        src_node: NodeID,
        dst_node: NodeID,
        check_for_cycles: bool,
    ) -> Result<SmallVec<[EdgeID; 4]>, AddEdgeError> {
        let num_src_out_ports = self
            .node_info(src_node)
            .ok_or(AddEdgeError::SrcNodeNotFound(src_node))?
            .info
            .channel_config
            .num_outputs
            .get();
        let num_dst_in_ports = self
            .node_info(dst_node)
            .ok_or(AddEdgeError::DstNodeNotFound(dst_node))?
            .info
            .channel_config
            .num_inputs
            .get();
        let num_connect_ports = num_src_out_ports.min(num_dst_in_ports);

        let ports_src_dst: SmallVec<[(u32, u32); 4]> =
            (0..num_connect_ports).map(|i| (i, i)).collect();

        self.graph
            .connect(src_node, dst_node, &ports_src_dst, check_for_cycles, false)
    }

    /// Connect the first two output ports of a node to the first two input
    /// ports of a second node.
    ///
    /// * `src_node` - The ID of the source node.
    /// * `dst_node` - The ID of the destination node.
    /// * `check_for_cycles` - If `true`, then this will run a check to
    ///   see if adding these edges will create a cycle in the graph, and
    ///   return an error if it does. Note, checking for cycles can be quite
    ///   expensive, so avoid enabling this when calling this method many times
    ///   in a row.
    ///
    /// ## Behavior
    ///
    /// * If `num_out_ports_on_src_node >= 2 && num_in_ports_on_dst_node >= 2`,
    ///   then src port 0 will be connected to dst port 0, and src port 1 will be
    ///   connected to dst port 1.
    /// * If `num_out_ports_on_src_node == 1 && num_in_ports_on_dst_node >= 2`,
    ///   then src port 0 will be connected to both dst port 0 and dst port 1.
    /// * In all other cases, an error will be returned. (Note that converting
    ///   a stereo signal into a mono signal should be done with the
    ///   `StereoToMonoNode`.)
    ///
    /// If successful, then this returns a list of edge IDs in order.
    ///
    /// If this returns an error, then the audio graph has not been
    /// modified.
    pub fn connect_stereo(
        &mut self,
        src_node: NodeID,
        dst_node: NodeID,
        check_for_cycles: bool,
    ) -> Result<SmallVec<[EdgeID; 4]>, AddEdgeError> {
        let num_src_out_ports = self
            .node_info(src_node)
            .ok_or(AddEdgeError::SrcNodeNotFound(src_node))?
            .info
            .channel_config
            .num_outputs;
        let num_dst_in_ports = self
            .node_info(dst_node)
            .ok_or(AddEdgeError::DstNodeNotFound(dst_node))?
            .info
            .channel_config
            .num_inputs;

        let ports_src_dst = if num_src_out_ports.get() >= 2 && num_dst_in_ports.get() >= 2 {
            &[(0, 0), (1, 1)]
        } else if num_src_out_ports.get() == 1 && num_dst_in_ports.get() >= 2 {
            &[(0, 0), (0, 1)]
        } else {
            return Err(if num_dst_in_ports.get() < 2 {
                AddEdgeError::InPortOutOfRange {
                    node: dst_node,
                    port_idx: 1,
                    num_in_ports: num_dst_in_ports,
                }
            } else {
                AddEdgeError::InPortOutOfRange {
                    node: src_node,
                    port_idx: 0,
                    num_in_ports: num_src_out_ports,
                }
            });
        };

        self.graph
            .connect(src_node, dst_node, ports_src_dst, check_for_cycles, false)
    }

    /// Remove connections (edges) between two nodes from the graph.
    ///
    /// * `src_node` - The ID of the source node.
    /// * `dst_node` - The ID of the destination node.
    /// * `ports_src_dst` - The port indices for each connection to make,
    ///   where the first value in a tuple is the output port on `src_node`,
    ///   and the second value in that tuple is the input port on `dst_node`.
    ///
    /// Returns the list of edges that were successfully removed.
    pub fn disconnect(
        &mut self,
        src_node: NodeID,
        dst_node: NodeID,
        ports_src_dst: &[(PortIdx, PortIdx)],
    ) -> SmallVec<[Edge; 4]> {
        self.graph.disconnect(src_node, dst_node, ports_src_dst)
    }

    /// Remove all connections (edges) between two nodes in the graph.
    ///
    /// * `src_node` - The ID of the source node.
    /// * `dst_node` - The ID of the destination node.
    ///
    /// Returns the list of edges that were successfully removed.
    pub fn disconnect_all_between(
        &mut self,
        src_node: NodeID,
        dst_node: NodeID,
    ) -> SmallVec<[Edge; 4]> {
        self.graph.disconnect_all_between(src_node, dst_node)
    }

    /// Remove a connection (edge) via the edge's unique ID.
    ///
    /// If the edge did not exist in this graph, then `None` will be returned.
    pub fn disconnect_by_edge_id(&mut self, edge_id: EdgeID) -> Option<Edge> {
        self.graph.disconnect_by_edge_id(edge_id, false)
    }

    /// Get information about the given [Edge]
    pub fn edge(&self, edge_id: EdgeID) -> Option<&Edge> {
        self.graph.edge(edge_id)
    }

    /// Runs a check to see if a cycle exists in the audio graph. If a cycle
    /// exists, an error is returned.
    ///
    /// Note, this method is expensive.
    pub fn cycle_detected(&mut self) -> Result<(), CompileGraphError> {
        if self.graph.cycle_detected() {
            Err(CompileGraphError::CycleDetected)
        } else {
            Ok(())
        }
    }

    /// Queue an event to be sent to an audio node's processor.
    ///
    /// Note, this event will not be sent until the event queue is flushed
    /// in [`FirewheelContext::update`].
    pub fn queue_event(&mut self, event: NodeEvent) {
        if self.contains_node(event.node_id) {
            self.event_group.push(event);
        }
    }

    /// Queue an event to be sent to an audio node's processor.
    ///
    /// Note, this event will not be sent until the event queue is flushed
    /// in [`FirewheelContext::update`].
    pub fn queue_event_for(&mut self, node_id: NodeID, event: NodeEventType) {
        self.queue_event(NodeEvent {
            node_id,
            #[cfg(feature = "scheduled_events")]
            time: None,
            event,
        });
    }

    /// Queue a [`NodeEventType::SetBypassed`] event for the given node.
    pub fn queue_bypassed_for(&mut self, node_id: NodeID, bypassed: bool) {
        self.queue_event(NodeEvent {
            node_id,
            #[cfg(feature = "scheduled_events")]
            time: None,
            event: NodeEventType::SetBypassed(bypassed),
        });
    }

    /// Queue an event at a certain time, to be sent to an audio node's processor.
    ///
    /// If `time` is `None`, then the event will occur as soon as the node's
    /// processor receives the event.
    ///
    /// Note, this event will not be sent until the event queue is flushed
    /// in [`FirewheelContext::update`].
    #[cfg(feature = "scheduled_events")]
    pub fn schedule_event_for(
        &mut self,
        node_id: NodeID,
        event: NodeEventType,
        time: Option<EventInstant>,
    ) {
        self.queue_event(NodeEvent {
            node_id,
            time,
            event,
        });
    }

    /// Construct a [`ContextQueue`] for diffing.
    ///
    /// Returns `None` if the node does not exist in the graph.
    pub fn event_queue(&mut self, id: NodeID) -> ContextQueue<'_> {
        ContextQueue {
            context: self,
            id,
            #[cfg(feature = "scheduled_events")]
            time: None,
        }
    }

    /// Construct a [`ContextQueue`] for diffing, along with the event instant that
    /// any pushed events will be scheduled for.
    ///
    /// Returns `None` if the node does not exist in the graph.
    #[cfg(feature = "scheduled_events")]
    pub fn event_queue_scheduled(
        &mut self,
        id: NodeID,
        time: Option<EventInstant>,
    ) -> ContextQueue<'_> {
        ContextQueue {
            context: self,
            id,
            time,
        }
    }

    /// Cancel scheduled events for all nodes.
    ///
    /// This will clear all events that have been scheduled since the last call to
    /// [`FirewheelContext::update`]. Any events scheduled between then and the next call
    /// to [`FirewheelContext::update`] will not be canceled.
    ///
    /// This only takes effect once [`FirewheelContext::update`] is called.
    #[cfg(feature = "scheduled_events")]
    pub fn cancel_all_scheduled_events(&mut self, event_type: ClearScheduledEventsType) {
        self.queued_clear_scheduled_events
            .push(ClearScheduledEventsEvent {
                node_id: None,
                event_type,
            });
    }

    /// Cancel scheduled events for a specific node.
    ///
    /// This will clear all events that have been scheduled since the last call to
    /// [`FirewheelContext::update`]. Any events scheduled between then and the next call
    /// to [`FirewheelContext::update`] will not be canceled.
    ///
    /// This only takes effect once [`FirewheelContext::update`] is called.
    #[cfg(feature = "scheduled_events")]
    pub fn cancel_scheduled_events_for(
        &mut self,
        node_id: NodeID,
        event_type: ClearScheduledEventsType,
    ) {
        self.queued_clear_scheduled_events
            .push(ClearScheduledEventsEvent {
                node_id: Some(node_id),
                event_type,
            });
    }

    fn send_message_to_processor(
        &mut self,
        msg: ContextToProcessorMsg,
    ) -> Result<(), (ContextToProcessorMsg, UpdateError)> {
        self.to_processor_tx
            .try_push(msg)
            .map_err(|msg| (msg, UpdateError::MsgChannelFull))
    }
}

impl Drop for FirewheelContext {
    fn drop(&mut self) {
        // Wait for the processor to be drop to avoid deallocating it on
        // the audio thread.
        #[cfg(not(target_family = "wasm"))]
        let _ = self.deactivate_blocking(core::time::Duration::from_secs(3));

        #[cfg(target_family = "wasm")]
        self.request_deactivate();

        // Make sure all node processors are dropped before node states in
        // order to be compatible with CLAP plugin hosting.
        if let Some(p) = &mut self.processor_drop_rx {
            p.clear();
        }
        self.from_processor_rx.clear();
        firewheel_core::collector::GlobalRtGc::collect();
    }
}

/// An event queue acquired from [`FirewheelContext::event_queue`].
///
/// This can help reduce event queue allocations
/// when you have direct access to the context.
///
/// ```
/// # use firewheel_core::{diff::{Diff, PathBuilder}, node::NodeID};
/// # use firewheel_graph::{backend::AudioBackend, FirewheelContext, ContextQueue};
/// # fn context_queue<B: AudioBackend, D: Diff>(
/// #     context: &mut FirewheelContext,
/// #     node_id: NodeID,
/// #     params: &D,
/// #     baseline: &D,
/// # ) {
/// // Get a queue that will send events directly to the provided node.
/// let mut queue = context.event_queue(node_id);
/// // Perform diffing using this queue.
/// params.diff(baseline, PathBuilder::default(), &mut queue);
/// # }
/// ```
pub struct ContextQueue<'a> {
    pub context: &'a mut FirewheelContext,
    id: NodeID,
    #[cfg(feature = "scheduled_events")]
    time: Option<EventInstant>,
}

impl ContextQueue<'_> {
    /// Send an event to set the bypass state of the node.
    pub fn push_bypassed(&mut self, bypassed: bool) {
        self.push(NodeEventType::SetBypassed(bypassed));
    }
}

#[cfg(feature = "scheduled_events")]
impl ContextQueue<'_> {
    pub fn time(&self) -> Option<EventInstant> {
        self.time
    }
}

impl EventQueue for ContextQueue<'_> {
    fn push(&mut self, data: NodeEventType) {
        self.context.queue_event(NodeEvent {
            event: data,
            #[cfg(feature = "scheduled_events")]
            time: self.time,
            node_id: self.id,
        });
    }
}

/// The type of scheduled events to clear
#[cfg(feature = "scheduled_events")]
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub enum ClearScheduledEventsType {
    /// Clear both musical and non-musical scheduled events.
    #[default]
    All,
    /// Clear only non-musical scheduled events.
    NonMusicalOnly,
    /// Clear only musical scheduled events.
    MusicalOnly,
}