knyst 0.5.1

Real time dynamic audio graph and synthesis library
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
use std::{
    cell::UnsafeCell,
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
    },
};

use rtrb::Producer;
use slotmap::SlotMap;

use crate::{internal_filter::hiir::StandardDownsampler2X, Resources};

use super::{
    node::Node, Gen, GenContext, GenState, NodeBufferRef, NodeId, NodeKey, Oversampling,
    OwnedRawBuffer, Sample, ScheduleReceiver, TaskData,
};

pub(super) fn make_graph_gen(
    sample_rate: Sample,
    parent_sample_rate: Sample,
    current_task_data: TaskData,
    block_size: usize,
    parent_block_size: usize,
    oversampling: Oversampling,
    parent_oversampling: Oversampling,
    num_outputs: usize,
    num_inputs: usize,
    timestamp: Arc<AtomicU64>,
    free_node_queue_producer: Producer<(NodeKey, GenState)>,
    schedule_receiver: ScheduleReceiver,
    arc_nodes: Arc<UnsafeCell<SlotMap<NodeKey, Node>>>,
    task_data_to_be_dropped_producer: rtrb::Producer<TaskData>,
    new_task_data_consumer: rtrb::Consumer<TaskData>,
    arc_inputs_buffers_ptr: Arc<OwnedRawBuffer>,
) -> Box<dyn Gen + Send> {
    let graph_gen = Box::new(GraphGen {
        sample_rate: sample_rate * oversampling.as_usize() as Sample,
        current_task_data,
        block_size: block_size * oversampling.as_usize(),
        num_outputs,
        num_inputs,
        graph_state: GenState::Continue,
        sample_counter: 0,
        timestamp,
        free_node_queue_producer,
        schedule_receiver,
        _arc_nodes: arc_nodes,
        task_data_to_be_dropped_producer,
        new_task_data_consumer,
        _arc_inputs_buffers_ptr: arc_inputs_buffers_ptr,
    });
    // TODO:
    // If the graph is the same as the parent Graph, do no conversion.
    // Otherwise, first convert oversampling to that of the parent if necessary.
    // Then convert sample rate further if necessary.
    // Then convert block size if necessary.
    let graph_gen: Box<dyn Gen + Send + 'static> = if oversampling != parent_oversampling {
        if num_inputs > 0 {
            panic!("Oversampling is currently not implemented for Graphs with inputs. This will be supported in the future.");
        }
        let graph_oversampling_converter = GraphConvertOversampling2XGen::new(
            Node::new("GraphConvertOversamplingGen", graph_gen),
            parent_sample_rate as usize * parent_oversampling.as_usize(),
            sample_rate as usize * oversampling.as_usize(),
            // Use the inner block size here because that conversion is done afterwards if necessary
            block_size * parent_oversampling.as_usize(),
            block_size * oversampling.as_usize(),
        );
        Box::new(graph_oversampling_converter)
    } else {
        graph_gen
    };
    if parent_block_size != block_size {
        if parent_block_size < block_size && num_inputs > 0 {
            panic!("An inner Graph cannot have inputs if it has a larger block size since the inputs will not be sufficiently filled.");
        }
        let graph_block_converter_gen = GraphBlockConverterGen::new(
            Node::new("GraphBlockConverterGen", graph_gen),
            parent_block_size,
            block_size,
        );
        Box::new(graph_block_converter_gen)
    } else {
        graph_gen
    }
}

/// Contains a GraphGen which it will run inside itself, converting block size
/// and sample rate to that of the parent Graph of this Graph.
///
/// Note that if the inner Graph has a larger block size it cannot have any
/// inputs since these inputs would only be half filled.
pub(super) struct GraphBlockConverterGen {
    graph_gen_node: Node,
    // The block size of the
    inner_block_size: usize,
    parent_block_size: usize,
    species: BlockConverterSpecies,
}

enum BlockConverterSpecies {
    InnerSmallOuterLarge {
        /// How many times the inner block size goes into the outer block size
        num_batches: usize,
        /// This buffer is used to hold the inputs to the inner graph so that they
        /// can be passed to the node. It only needs to have as many channels as the
        /// inner node has inputs. It could have been a Vec if not for the interface
        /// for calling Node::process which needs to work with raw pointers in every
        /// other situation.
        inputs_buffers_ptr: Arc<OwnedRawBuffer>,
    },
    InnerLargeOuterSmall {
        /// How many times the outer block size goes into the inner block size
        num_batches: usize,
        /// Keeping track of how many batches we have already taken from the
        /// inner graph and if we need to process another inner block
        batch_counter: usize,
        gen_state: GenState,
    },
}

impl GraphBlockConverterGen {
    pub fn new(graph_gen_node: Node, parent_block_size: usize, inner_block_size: usize) -> Self {
        let species = if parent_block_size > inner_block_size {
            let inputs_buffers_ptr = Box::<[Sample]>::into_raw(
                vec![0.0 as Sample; inner_block_size * graph_gen_node.num_inputs()]
                    .into_boxed_slice(),
            );
            let inputs_buffers_ptr = Arc::new(OwnedRawBuffer {
                ptr: inputs_buffers_ptr,
            });
            BlockConverterSpecies::InnerSmallOuterLarge {
                num_batches: parent_block_size / inner_block_size,
                inputs_buffers_ptr,
            }
        } else {
            BlockConverterSpecies::InnerLargeOuterSmall {
                num_batches: inner_block_size / parent_block_size,
                // Set to max from the start so that one block is immediately processed
                batch_counter: inner_block_size / parent_block_size,
                gen_state: GenState::Continue,
            }
        };
        Self {
            graph_gen_node,
            inner_block_size,
            parent_block_size,
            species,
        }
    }
    pub fn convert_inner_large_outer_small(
        graph_gen_node: &mut Node,
        ctx: GenContext,
        resources: &mut Resources,
        parent_block_size: usize,
        num_batches: usize,
        batch_counter: &mut usize,
        gen_state: &mut GenState,
    ) {
        if *batch_counter == num_batches {
            *gen_state =
                graph_gen_node.process(&NodeBufferRef::null_buffer(), ctx.sample_rate, resources);
            *batch_counter = 0;
        }

        let offset = *batch_counter * parent_block_size;
        let mut node_outputs = graph_gen_node.output_buffers();
        for (output_channel, node_output) in ctx.outputs.iter_mut().zip(node_outputs.iter_mut()) {
            for sample in 0..parent_block_size {
                output_channel[sample] = node_output[sample + offset];
            }
        }
        *batch_counter += 1;
    }
    pub fn convert_inner_small_outer_large(
        &mut self,
        ctx: GenContext,
        resources: &mut Resources,
        num_batches: usize,
        input_buffers: &mut NodeBufferRef,
    ) -> GenState {
        // We need to run our inner graph_gen many times to fill the outer block.

        for batch in 0..num_batches {
            let batch_sample_offset = batch * self.inner_block_size;
            // Copy inputs from input to this graph
            for input_num in 0..input_buffers.channels() {
                for sample in 0..self.inner_block_size {
                    input_buffers.write(
                        ctx.inputs.read(input_num, sample + batch_sample_offset),
                        input_num,
                        sample,
                    );
                }
            }
            // GraphBlockConverterGen does not convert sample rate so just
            // use the sample rate from upstream
            let returned_gen_state =
                self.graph_gen_node
                    .process(&input_buffers, ctx.sample_rate, resources);
            // Copy the outputs of the node to the outputs of this Gen
            for output_num in 0..ctx.outputs.channels() {
                for sample in 0..self.inner_block_size {
                    ctx.outputs.write(
                        self.graph_gen_node
                            .output_buffers()
                            .read(output_num, sample),
                        output_num,
                        sample + batch_sample_offset,
                    );
                }
            }
            match returned_gen_state {
                GenState::Continue => (),
                GenState::FreeSelf
                | GenState::FreeSelfMendConnections
                | GenState::FreeGraph(_)
                | GenState::FreeGraphMendConnections(_) => {
                    // zero the following batches and then break
                    for batch_left in (batch + 1)..num_batches {
                        let batch_sample_offset = batch_left * self.inner_block_size;
                        for output_num in 0..ctx.outputs.channels() {
                            for sample in 0..self.inner_block_size {
                                ctx.outputs
                                    .write(0.0, output_num, sample + batch_sample_offset);
                            }
                        }
                    }
                    return returned_gen_state;
                }
            }
        }
        GenState::Continue
    }
}

impl Gen for GraphBlockConverterGen {
    fn process(&mut self, ctx: GenContext, resources: &mut Resources) -> GenState {
        match &mut self.species {
            BlockConverterSpecies::InnerSmallOuterLarge {
                num_batches,
                inputs_buffers_ptr,
            } => {
                let input_buffers_first_sample = inputs_buffers_ptr.ptr.cast::<Sample>();
                let mut input_buffers = NodeBufferRef::new(
                    input_buffers_first_sample,
                    self.graph_gen_node.num_inputs(),
                    self.inner_block_size,
                );
                let num_batches = *num_batches;
                self.convert_inner_small_outer_large(
                    ctx,
                    resources,
                    num_batches,
                    &mut input_buffers,
                )
            }
            BlockConverterSpecies::InnerLargeOuterSmall {
                num_batches,
                batch_counter,
                gen_state,
            } => {
                GraphBlockConverterGen::convert_inner_large_outer_small(
                    &mut self.graph_gen_node,
                    ctx,
                    resources,
                    self.parent_block_size,
                    *num_batches,
                    batch_counter,
                    gen_state,
                );
                *gen_state
            }
        }
    }

    fn num_inputs(&self) -> usize {
        self.graph_gen_node.num_inputs()
    }

    fn num_outputs(&self) -> usize {
        self.graph_gen_node.num_outputs()
    }

    fn init(&mut self, _block_size: usize, sample_rate: Sample, node_id: NodeId) {
        self.graph_gen_node
            .init(self.inner_block_size, sample_rate, node_id);
    }

    fn input_desc(&self, input: usize) -> &'static str {
        self.graph_gen_node.input_desc(input)
    }

    fn output_desc(&self, output: usize) -> &'static str {
        self.graph_gen_node.output_desc(output)
    }

    fn name(&self) -> &'static str {
        self.graph_gen_node.name
    }
}

unsafe impl Send for GraphBlockConverterGen {}

pub(super) struct GraphConvertOversampling2XGen {
    graph_gen_node: Node,
    /// Inner GraphGen sample rate with oversampling applied
    inner_sample_rate: Sample,
    /// Inner block size with oversampling applied
    inner_block_size: usize,
    downsamplers: Vec<StandardDownsampler2X>,
}

impl GraphConvertOversampling2XGen {
    pub(super) fn new(
        graph_gen_node: Node,
        parent_sample_rate: usize,
        inner_sample_rate: usize,
        parent_block_size: usize,
        inner_block_size: usize,
    ) -> Self {
        // let num_input_channels = graph_gen_node.num_inputs();
        let num_output_channels = graph_gen_node.num_outputs();
        let oversampling_rate = if inner_sample_rate > parent_sample_rate {
            assert_eq!(inner_sample_rate % parent_sample_rate, 0);
            assert_eq!(
                parent_block_size * inner_sample_rate / parent_sample_rate,
                inner_block_size
            );
            inner_sample_rate / parent_sample_rate
        } else {
            assert_eq!(parent_sample_rate % inner_sample_rate, 0);
            assert_eq!(
                inner_block_size * parent_sample_rate / inner_sample_rate,
                parent_block_size
            );
            parent_sample_rate / inner_sample_rate
        };
        assert!(oversampling_rate == 2);
        // let inputs_oversampled_buffers = vec![vec![0.0; inner_block_size]; num_input_channels];
        // // let inputs_buffer =
        // //     vec![
        // //         vec![0.0; parent_block_size * inner_sample_rate / parent_sample_rate];
        // //         graph_gen_node.num_inputs()
        // //     ];
        // // let outputs_buffer =
        // //     vec![vec![0.0 as Sample; parent_block_size]; graph_gen_node.num_outputs()];
        // let inner_outputs_conversion =
        //     vec![vec![0.0 as Sample; inner_block_size]; graph_gen_node.num_outputs()];
        // // let inner_outputs_conversion = output_resampler.input_buffer_allocate();
        // let parent_inputs_conversion =
        //     vec![vec![0.0 as Sample; parent_block_size]; graph_gen_node.num_inputs()];
        // // let parent_inputs_conversion = input_resampler.input_buffer_allocate();

        // let parent_inputs_oversampled_buffers_ptr = Box::<[Sample]>::into_raw(
        //     vec![0.0 as Sample; inner_block_size * graph_gen_node.num_inputs()].into_boxed_slice(),
        // );
        // let parent_inputs_oversampled_buffers_ptr = Arc::new(OwnedRawBuffer {
        //     ptr: parent_inputs_oversampled_buffers_ptr,
        // });

        let downsamplers = vec![StandardDownsampler2X::new(); num_output_channels];
        Self {
            graph_gen_node,
            inner_sample_rate: inner_sample_rate as Sample,
            inner_block_size,
            downsamplers,
        }
    }
}
impl Gen for GraphConvertOversampling2XGen {
    fn process(&mut self, ctx: GenContext, resources: &mut Resources) -> GenState {
        // let input_buffers = if self.graph_gen_node.num_inputs() > 0 {
        //     // Convert inputs to the inner sample rate
        //     for i in 0..ctx.inputs.channels() {
        //         let inp = ctx.inputs.get_channel(i);
        //         let conv = &mut self.parent_inputs_conversion[i];
        //         for (&inp, conv) in inp.iter().zip(conv.iter_mut()) {
        //             *conv = inp;
        //         }
        //     }
        //     // TODO: Upsample here without rubato
        //     self.input_resampler.process_into_buffer(
        //         self.parent_inputs_conversion.as_slice(),
        //         &mut self.inputs_oversampled_buffers,
        //         None,
        //     );
        //     // TODO: Can a Vec based output be converted to a NodeBufferRef for free?
        //     // Copy into out NodeBufferRef structure
        //     let input_buffers_first = self.inputs_oversampled_buffers_ptr.ptr.cast::<Sample>();
        //     let mut converted_inputs = NodeBufferRef::new(
        //         input_buffers_first,
        //         self.graph_gen_node.num_inputs(),
        //         self.inner_block_size,
        //     );
        //     assert_eq!(ctx.inputs.channels(), self.graph_gen_node.num_inputs());
        //     for (vec_channel, noderef_channel) in self
        //         .inputs_oversampled_buffers
        //         .iter()
        //         .zip(converted_inputs.iter_mut())
        //     {
        //         for (&vec_val, noderef_val) in vec_channel.iter().zip(noderef_channel.iter_mut()) {
        //             *noderef_val = vec_val;
        //         }
        //     }

        //     converted_inputs
        // } else {
        //     NodeBufferRef::null_buffer()
        // };
        let input_buffers = NodeBufferRef::null_buffer();

        // The GraphGen does nothing with the sample rate parameter sent here,
        // replacing it by its own sample rate.
        let gen_state = self.graph_gen_node.process(&input_buffers, 0., resources);
        // dbg!(self.graph_gen_node.output_buffers().get_channel(0));

        // TODO: Remove this step if we can get NodeBufferRef compatible with &[AsRef<[Sample]>]
        /*
                for (vec_channel, noderef_channel) in self
                    .inner_outputs_conversion
                    .iter_mut()
                    .zip(self.graph_gen_node.output_buffers().iter())
                {
                    for (vec_val, &noderef_val) in vec_channel.iter_mut().zip(noderef_channel.iter()) {
                        *vec_val = noderef_val;
                    }
                }

                // dbg!(self.graph_gen_node.output_buffers().get_channel(0));
                // dbg!(&self.inner_outputs_conversion);
                // Convert outputs to the parent sample rate
                let _res = self.output_resampler.process_into_buffer(
                    self.inner_outputs_conversion.as_slice(),
                    &mut self.outputs_buffer,
                    None,
                );

                for (vec_channel, noderef_channel) in self.outputs_buffer.iter().zip(ctx.outputs.iter_mut())
                {
                    for (&vec_val, noderef_val) in vec_channel.iter().zip(noderef_channel.iter_mut()) {
                        *noderef_val = vec_val;
                    }
                }
        */
        // TODO: Use an antialiasing filter on the output here before copying
        // for ((from_graph, to_out), filter) in self
        //     .graph_gen_node
        //     .output_buffers()
        //     .iter()
        //     .zip(ctx.outputs.iter_mut())
        //     .zip(self.downsampling_filter.iter_mut())
        // {
        //     assert!(from_graph.len() / self.oversampling_rate == to_out.len());
        //     for i in 0..(self.inner_block_size / self.oversampling_rate) {
        //         to_out[i] = from_graph[i * self.oversampling_rate];
        //     }
        // }
        for ((from_graph, to_out), downsampler) in self
            .graph_gen_node
            .output_buffers()
            .iter()
            .zip(ctx.outputs.iter_mut())
            .zip(self.downsamplers.iter_mut())
        {
            // If we want more than 2x we need to stack downsamplers
            assert!(from_graph.len() / 2 == to_out.len());
            downsampler.process_block(from_graph, to_out);
        }
        // dbg!(ctx.outputs.get_channel(0));
        gen_state
    }

    fn num_inputs(&self) -> usize {
        self.graph_gen_node.num_inputs()
    }

    fn num_outputs(&self) -> usize {
        self.graph_gen_node.num_outputs()
    }

    fn init(&mut self, _block_size: usize, _sample_rate: Sample, node_id: NodeId) {
        self.graph_gen_node
            .init(self.inner_block_size, self.inner_sample_rate, node_id);
    }

    fn input_desc(&self, input: usize) -> &'static str {
        self.graph_gen_node.input_desc(input)
    }

    fn output_desc(&self, output: usize) -> &'static str {
        self.graph_gen_node.output_desc(output)
    }

    fn name(&self) -> &'static str {
        self.graph_gen_node.name
    }
}

unsafe impl Send for GraphConvertOversampling2XGen {}

/// This gets placed as a dyn Gen in a Node in a Graph. It's how the Graph gets
/// run. The Graph communicates with the GraphGen in a thread safe way.
///
/// # Safety
/// Using this struct is safe only if used in conjunction with the
/// Graph. The Graph owns nodes and gives its corresponding GraphGen raw
/// pointers to them through Tasks, but it never accesses or deallocates a node
/// while it can be accessed by the [`GraphGen`] through a Task. The [`GraphGen`]
/// mustn't use the _arc_nodes field; it is only there to make sure the nodes
/// don't get dropped.
pub(super) struct GraphGen {
    // block_size with oversampling applied
    block_size: usize,
    // sample_rate with oversampling applied
    sample_rate: Sample,
    num_outputs: usize,
    num_inputs: usize,
    current_task_data: TaskData,
    // This Arc is cloned from the Graph and exists so that if the Graph gets dropped, the GraphGen can continue on without segfaulting.
    _arc_nodes: Arc<UnsafeCell<SlotMap<NodeKey, Node>>>,
    // This Arc makes sure the input buffers allocation is valid for as long as it needs to be
    _arc_inputs_buffers_ptr: Arc<OwnedRawBuffer>,
    graph_state: GenState,
    /// Stores the number of completed samples, updated at the end of a block
    sample_counter: u64,
    timestamp: Arc<AtomicU64>,
    schedule_receiver: ScheduleReceiver,
    free_node_queue_producer: rtrb::Producer<(NodeKey, GenState)>,
    task_data_to_be_dropped_producer: rtrb::Producer<TaskData>,
    new_task_data_consumer: rtrb::Consumer<TaskData>,
}

impl Gen for GraphGen {
    fn name(&self) -> &'static str {
        "GraphGen"
    }
    fn process(&mut self, ctx: GenContext, resources: &mut Resources) -> GenState {
        match self.graph_state {
            GenState::Continue => {
                // TODO: Support output with a different block size, i.e. local buffering and running this graph more or less often than the parent graph
                //
                //
                // Check if there is a new clock to update to
                if let Some(new_sample_counter) =
                    self.schedule_receiver.clock_update(self.sample_rate)
                {
                    self.sample_counter = new_sample_counter;
                }
                let mut do_empty_buffer = None;
                let mut do_mend_connections = None;
                let num_new_task_data = self.new_task_data_consumer.slots();
                if num_new_task_data > 0 {
                    if let Ok(td_chunk) = self.new_task_data_consumer.read_chunk(num_new_task_data)
                    {
                        for td in td_chunk {
                            // Setting `applied` to true signals that the new TaskData have been received and old data can be dropped
                            td.applied.store(true, Ordering::SeqCst);
                            let old_td = std::mem::replace(&mut self.current_task_data, td);
                            match self.task_data_to_be_dropped_producer.push(old_td) {
                          Ok(_) => (),
                          Err(e) => eprintln!("RingBuffer for TaskData to be dropped was full. Please increase the size of the RingBuffer. The GraphGen will drop the TaskData here instead. e: {e}"),
                      }
                        }
                    }
                }

                let task_data = &mut self.current_task_data;
                let TaskData {
                    applied: _,
                    tasks,
                    output_tasks,
                    new_inputs_buffers_ptr,
                    input_to_output_tasks,
                } = task_data;

                if let Some(inputs_buffers_ptr) = new_inputs_buffers_ptr.take() {
                    // The old buffers will be kept alive until the Arc has been dropped in the GraphGen
                    self._arc_inputs_buffers_ptr = inputs_buffers_ptr;
                }

                let changes = self.schedule_receiver.changes();

                // Run the tasks
                for task in tasks.iter_mut() {
                    task.init_constants();
                    // If there are any changes to the constants of the node, apply them here
                    let mut i = 0;
                    while i < changes.len() {
                        let change = &changes[i];
                        if change.key == task.node_key {
                            let sample_to_apply = if change.timestamp < self.sample_counter {
                                if change.timestamp != 0 {
                                    // timestamps of 0 simply means as fast as possible. It is not an error or issue.
                                    // TODO: Send this off of the audio thread
                                    eprintln!("Warning: Scheduled change was applied late. Consider increasing latency.");
                                }
                                0
                            } else {
                                change.timestamp - self.sample_counter
                            } as usize;
                            if sample_to_apply < self.block_size {
                                task.apply_constant_change(change, sample_to_apply);
                                // TODO: This is inefficient since the the first
                                // changes are the most likely to be removed,
                                // and are the most expensive to remove. Either
                                // the changes can be in reverse order, but then
                                // pushing into the list always puts new changes
                                // in the wrong place, or many changes can be
                                // removed all at once after applying them.
                                changes.remove(i);
                            } else {
                                i += 1;
                            }
                        } else {
                            i += 1;
                        }
                    }
                    match task.run(ctx.inputs, resources, self.sample_rate, self.sample_counter) {
                        GenState::Continue => (),
                        GenState::FreeSelf => {
                            // We don't care if it fails since if it does the
                            // node will still exist, return FreeSelf and get
                            // added to the queue next block.
                            self.free_node_queue_producer
                                .push((task.node_key, GenState::FreeSelf))
                                .ok();
                        }
                        GenState::FreeSelfMendConnections => {
                            self.free_node_queue_producer
                                .push((task.node_key, GenState::FreeSelfMendConnections))
                                .ok();
                        }
                        GenState::FreeGraph(from_sample_nr) => {
                            self.graph_state = GenState::FreeSelf;
                            do_empty_buffer = Some(from_sample_nr);
                        }
                        GenState::FreeGraphMendConnections(from_sample_nr) => {
                            self.graph_state = GenState::FreeSelfMendConnections;
                            do_mend_connections = Some(from_sample_nr);
                        }
                    }
                }

                // Remove changes that have expired for tasks that don't exist
                // (anymore, removed nodes). Otherwise they will accumulate
                // until there is a crash.
                let mut i = 0;
                while i < changes.len() {
                    let change = &mut changes[i];
                    if change.timestamp < self.sample_counter {
                        change.removal_countdown += 1;
                        if change.removal_countdown >= 10 {
                            let change = changes.remove(i);
                            eprintln!("Removed scheduled change: {change:?}");
                            continue;
                        }
                    }
                    i += 1;
                }

                // Set the output of the graph
                // Zero the output buffer.
                ctx.outputs.fill(0.0);
                for output_task in output_tasks.iter() {
                    let input_values = output_task
                        .input_buffers
                        .get_channel(output_task.input_index);
                    // Safety: We always drop the&mut refernce before requesting
                    // another one so we cannot hold mutliple references to the
                    // same channnel.
                    let output =
                        unsafe { ctx.outputs.get_channel_mut(output_task.graph_output_index) };
                    for i in 0..self.block_size {
                        let value = input_values[i];
                        // Since many nodes may write to the same output we
                        // need to add the outputs together. The Node makes no promise as to the content of
                        // the output buffer provided.
                        output[i] += value;
                    }
                }
                if let Some(from_relative_sample_nr) = do_empty_buffer {
                    for channel in ctx.outputs.iter_mut() {
                        for sample in &mut channel[from_relative_sample_nr..] {
                            *sample = 0.0;
                        }
                    }
                }
                if let Some(from_relative_sample_nr) = do_mend_connections {
                    let channels = ctx.inputs.channels().min(ctx.outputs.channels());
                    for channel in 0..channels {
                        let input = ctx.inputs.get_channel(channel);
                        // Safety: We are only holding one &mut to a channel at a time.
                        let output = unsafe { ctx.outputs.get_channel_mut(channel) };
                        // TODO: Check if the input is constant or not first. Only non-constant inputs should be passed through (because it's "mending" the connection)
                        for (i, o) in input[from_relative_sample_nr..]
                            .iter()
                            .zip(output[from_relative_sample_nr..].iter_mut())
                        {
                            *o = *i;
                        }
                    }
                }
                // Copy from inputs to outputs of the graph

                for output_task in input_to_output_tasks.iter() {
                    let input_values = ctx.inputs.get_channel(output_task.graph_input_index);
                    // Safety: We always drop the&mut refernce before requesting
                    // another one so we cannot hold mutliple references to the
                    // same channnel.
                    let output =
                        unsafe { ctx.outputs.get_channel_mut(output_task.graph_output_index) };
                    for i in 0..self.block_size {
                        let value = input_values[i];
                        // Since many nodes may write to the same output we
                        // need to add the outputs together. The Node makes no promise as to the content of
                        // the output buffer provided.
                        output[i] += value;
                    }
                }
                if let Some(from_relative_sample_nr) = do_empty_buffer {
                    for channel in ctx.outputs.iter_mut() {
                        for sample in &mut channel[from_relative_sample_nr..] {
                            *sample = 0.0;
                        }
                    }
                }
                if let Some(from_relative_sample_nr) = do_mend_connections {
                    let channels = ctx.inputs.channels().min(ctx.outputs.channels());
                    for channel in 0..channels {
                        let input = ctx.inputs.get_channel(channel);
                        // Safety: We are only holding one &mut to a channel at a time.
                        let output = unsafe { ctx.outputs.get_channel_mut(channel) };
                        // TODO: Check if the input is constant or not first. Only non-constant inputs should be passed through (because it's "mending" the connection)
                        for (i, o) in input[from_relative_sample_nr..]
                            .iter()
                            .zip(output[from_relative_sample_nr..].iter_mut())
                        {
                            *o = *i;
                        }
                    }
                }
                self.sample_counter += self.block_size as u64;
                self.timestamp.store(self.sample_counter, Ordering::SeqCst);
            }
            GenState::FreeSelf => {
                ctx.outputs.fill(0.0);
            }
            GenState::FreeSelfMendConnections => {
                let channels = ctx.inputs.channels().min(ctx.outputs.channels());
                for channel in 0..channels {
                    let input = ctx.inputs.get_channel(channel);
                    // Safety: We are only holding a &mut to one channel at a time.
                    let output = unsafe { ctx.outputs.get_channel_mut(channel) };
                    // TODO: Check if the input is constant or not first. Only non-constant inputs should be passed through (because it's "mending" the connection)
                    for (i, o) in input.iter().zip(output.iter_mut()) {
                        *o = *i;
                    }
                }
            }
            // These are unreachable because they are converted to the FreeSelf
            // variants at the Graph level which is this level.
            GenState::FreeGraph(_) => unreachable!(),
            GenState::FreeGraphMendConnections(_) => unreachable!(),
        }
        self.graph_state
    }
    fn num_inputs(&self) -> usize {
        self.num_inputs
    }
    fn num_outputs(&self) -> usize {
        self.num_outputs
    }
}

/// Safety: This impl of Send is required because of the Arc<UnsafeCell<...>> in
/// GraphGen. The _arc_nodes field of GraphGen exists only so that the nodes
/// won't get dropped if the Graph is dropped. The UnsafeCell will never be used
/// to access the data from within GraphGen.
unsafe impl Send for GraphGen {}