grafos_dsp/
types.rs

1//! DSP-specific types: samples, blocks, and configuration.
2
3extern crate alloc;
4use alloc::vec::Vec;
5
6/// A single audio sample.
7pub type Sample = f32;
8
9/// A complex number for frequency-domain representation.
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub struct Complex {
12    pub re: f32,
13    pub im: f32,
14}
15
16impl Complex {
17    pub fn new(re: f32, im: f32) -> Self {
18        Self { re, im }
19    }
20
21    pub fn zero() -> Self {
22        Self { re: 0.0, im: 0.0 }
23    }
24
25    pub fn magnitude(&self) -> f32 {
26        (self.re * self.re + self.im * self.im).sqrt()
27    }
28}
29
30impl core::ops::Add for Complex {
31    type Output = Self;
32    fn add(self, rhs: Self) -> Self {
33        Self {
34            re: self.re + rhs.re,
35            im: self.im + rhs.im,
36        }
37    }
38}
39
40impl core::ops::Sub for Complex {
41    type Output = Self;
42    fn sub(self, rhs: Self) -> Self {
43        Self {
44            re: self.re - rhs.re,
45            im: self.im - rhs.im,
46        }
47    }
48}
49
50impl core::ops::Mul for Complex {
51    type Output = Self;
52    fn mul(self, rhs: Self) -> Self {
53        Self {
54            re: self.re * rhs.re - self.im * rhs.im,
55            im: self.re * rhs.im + self.im * rhs.re,
56        }
57    }
58}
59
60impl core::ops::Mul<f32> for Complex {
61    type Output = Self;
62    fn mul(self, rhs: f32) -> Self {
63        Self {
64            re: self.re * rhs,
65            im: self.im * rhs,
66        }
67    }
68}
69
70/// A fixed-size block of audio samples.
71#[derive(Debug, Clone)]
72pub struct Block {
73    /// Sample data (interleaved if multi-channel).
74    pub data: Vec<Sample>,
75    /// Sample rate in Hz.
76    pub sample_rate: u32,
77    /// Number of audio channels.
78    pub channels: u16,
79}
80
81impl Block {
82    /// Number of sample frames (samples per channel).
83    pub fn frames(&self) -> usize {
84        if self.channels == 0 {
85            return 0;
86        }
87        self.data.len() / self.channels as usize
88    }
89}
90
91/// Configuration for a DSP pipeline.
92#[derive(Debug, Clone)]
93pub struct DspConfig {
94    /// Number of sample frames per block.
95    pub block_size: usize,
96    /// Sample rate in Hz.
97    pub sample_rate: u32,
98    /// Number of audio channels.
99    pub channels: u16,
100}
101
102/// Statistics from a DSP pipeline run.
103#[derive(Debug, Clone, Default)]
104pub struct DspStats {
105    /// Total number of blocks processed.
106    pub blocks_processed: u64,
107    /// Average per-block latency in microseconds.
108    pub avg_latency_us: u64,
109    /// Maximum per-block latency in microseconds.
110    pub max_latency_us: u64,
111    /// Jitter (max - min latency) in microseconds.
112    pub jitter_us: u64,
113    /// Number of blocks where processing exceeded the block duration.
114    pub overruns: u64,
115}