Skip to main content

aether_nodes/
record.rs

1//! RecordNode — taps audio into a lock-free SPSC ring buffer.
2//!
3//! Passes input through to output unchanged (unity gain).
4//! If the ring is full, samples are dropped silently.
5//! No allocation, no locks in the hot path.
6
7use aether_core::{node::DspNode, param::ParamBlock, BUFFER_SIZE, MAX_INPUTS};
8use ringbuf::{traits::Producer, HeapProd};
9
10pub struct RecordNode {
11    producer: HeapProd<f32>,
12}
13
14impl RecordNode {
15    pub fn new(producer: HeapProd<f32>) -> Self {
16        Self { producer }
17    }
18}
19
20impl DspNode for RecordNode {
21    fn process(
22        &mut self,
23        inputs: &[Option<&[f32; BUFFER_SIZE]>; MAX_INPUTS],
24        output: &mut [f32; BUFFER_SIZE],
25        _params: &mut ParamBlock,
26        _sample_rate: f32,
27    ) {
28        let silence = [0.0f32; BUFFER_SIZE];
29        let input = inputs[0].unwrap_or(&silence);
30
31        // Attempt to push; silently drop if ring is full.
32        let _ = self.producer.push_slice(input);
33
34        // Pass-through: copy input to output.
35        output.copy_from_slice(input);
36    }
37
38    fn type_name(&self) -> &'static str {
39        "RecordNode"
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use proptest::prelude::*;
47    use ringbuf::{traits::{Consumer, Split}, HeapRb};
48
49    /// Generate a [f32; BUFFER_SIZE] array via proptest (BUFFER_SIZE = 64).
50    fn audio_buffer() -> impl Strategy<Value = [f32; BUFFER_SIZE]> {
51        prop::collection::vec(-1.0f32..=1.0f32, BUFFER_SIZE)
52            .prop_map(|v| v.try_into().unwrap())
53    }
54
55    // Property 3
56    proptest! {
57        /// **Validates: Requirements 2.11**
58        ///
59        /// Property 3: RecordNode pass-through.
60        ///
61        /// For any input buffer passed to `RecordNode::process()`, the output buffer
62        /// SHALL be identical to the input buffer (unity gain pass-through).
63        #[test]
64        fn prop_record_node_pass_through(
65            input_samples in audio_buffer(),
66        ) {
67            let ring = HeapRb::<f32>::new(BUFFER_SIZE * 2);
68            let (producer, _consumer) = ring.split();
69            let mut node = RecordNode::new(producer);
70            let input_buffer: [f32; BUFFER_SIZE] = input_samples;
71            let mut output_buffer = [0.0f32; BUFFER_SIZE];
72            let mut params = ParamBlock::default();
73            let inputs: [Option<&[f32; BUFFER_SIZE]>; MAX_INPUTS] = {
74                let mut arr: [Option<&[f32; BUFFER_SIZE]>; MAX_INPUTS] = [None; MAX_INPUTS];
75                arr[0] = Some(&input_buffer);
76                arr
77            };
78            node.process(&inputs, &mut output_buffer, &mut params, 48000.0);
79            prop_assert_eq!(output_buffer, input_buffer);
80        }
81
82        // Property 4
83        /// **Validates: Requirements 2.2**
84        ///
85        /// Property 4: RecordNode ring buffer round-trip.
86        ///
87        /// For any input buffer passed to `RecordNode::process()` when the ring buffer
88        /// has capacity, draining the ring buffer SHALL yield the same 64 samples.
89        #[test]
90        fn prop_record_node_ring_buffer_round_trip(
91            input_samples in audio_buffer(),
92        ) {
93            let ring = HeapRb::<f32>::new(BUFFER_SIZE * 2);
94            let (producer, mut consumer) = ring.split();
95            let mut node = RecordNode::new(producer);
96            let input_buffer: [f32; BUFFER_SIZE] = input_samples;
97            let mut output_buffer = [0.0f32; BUFFER_SIZE];
98            let mut params = ParamBlock::default();
99            let inputs: [Option<&[f32; BUFFER_SIZE]>; MAX_INPUTS] = {
100                let mut arr: [Option<&[f32; BUFFER_SIZE]>; MAX_INPUTS] = [None; MAX_INPUTS];
101                arr[0] = Some(&input_buffer);
102                arr
103            };
104            node.process(&inputs, &mut output_buffer, &mut params, 48000.0);
105            let mut drained_samples = [0.0f32; BUFFER_SIZE];
106            let drained_count = consumer.pop_slice(&mut drained_samples);
107            prop_assert_eq!(drained_count, BUFFER_SIZE);
108            prop_assert_eq!(drained_samples, input_buffer);
109        }
110    }
111}