Skip to main content

aether_nodes/
scope.rs

1//! ScopeNode — feeds audio into a lock-free SPSC ring buffer for visualisation.
2//!
3//! The ring is sized to 512 samples (8 × 64-sample frames).
4//! Passes input through to output unchanged (unity gain).
5//! If the ring is full, samples are dropped silently.
6//! No allocation, no locks in the hot path.
7
8use aether_core::{node::DspNode, param::ParamBlock, BUFFER_SIZE, MAX_INPUTS};
9use ringbuf::{traits::Producer, HeapProd};
10
11pub struct ScopeNode {
12    producer: HeapProd<f32>,
13}
14
15impl ScopeNode {
16    pub fn new(producer: HeapProd<f32>) -> Self {
17        Self { producer }
18    }
19}
20
21impl DspNode for ScopeNode {
22    fn process(
23        &mut self,
24        inputs: &[Option<&[f32; BUFFER_SIZE]>; MAX_INPUTS],
25        output: &mut [f32; BUFFER_SIZE],
26        _params: &mut ParamBlock,
27        _sample_rate: f32,
28    ) {
29        let silence = [0.0f32; BUFFER_SIZE];
30        let input = inputs[0].unwrap_or(&silence);
31
32        // Attempt to push; silently drop if ring is full.
33        let _ = self.producer.push_slice(input);
34
35        // Pass-through: copy input to output.
36        output.copy_from_slice(input);
37    }
38
39    fn type_name(&self) -> &'static str {
40        "ScopeNode"
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use proptest::prelude::*;
48    use ringbuf::{traits::Split, HeapRb};
49
50    /// Generate a [f32; BUFFER_SIZE] array via proptest (BUFFER_SIZE = 64).
51    fn audio_buffer() -> impl Strategy<Value = [f32; BUFFER_SIZE]> {
52        prop::collection::vec(-1.0f32..=1.0f32, BUFFER_SIZE).prop_map(|v| v.try_into().unwrap())
53    }
54
55    // Property 9
56    proptest! {
57        /// **Validates: Requirements 4.4**
58        ///
59        /// Property 9: ScopeNode pass-through.
60        ///
61        /// For any input buffer passed to `ScopeNode::process()`, the output buffer
62        /// SHALL be identical to the input buffer (unity gain pass-through).
63        #[test]
64        fn prop_scope_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 = ScopeNode::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
83    // Property 10
84    proptest! {
85        /// **Validates: Requirements 4.7**
86        ///
87        /// Property 10: Scope frame serialization.
88        ///
89        /// For any array of 64 f32 samples serialized as a binary scope frame,
90        /// the resulting byte slice SHALL be exactly 256 bytes, and decoding it
91        /// as [f32; 64] little-endian SHALL recover the original values exactly.
92        #[test]
93        fn prop_scope_frame_serialization(
94            samples in audio_buffer(),
95        ) {
96            let mut serialized = Vec::with_capacity(256);
97            for &sample in &samples {
98                serialized.extend_from_slice(&sample.to_le_bytes());
99            }
100            prop_assert_eq!(serialized.len(), 256);
101            let mut deserialized = [0.0f32; 64];
102            for (i, chunk) in serialized.chunks_exact(4).enumerate() {
103                let bytes: [u8; 4] = chunk.try_into().unwrap();
104                deserialized[i] = f32::from_le_bytes(bytes);
105            }
106            prop_assert_eq!(deserialized, samples);
107        }
108    }
109}