firefly_audio/
basic_types.rs

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
use core::ops::{Add, Div};

pub type Position = u32;
pub const SAMPLE_RATE: Position = 44_100;
pub const SAMPLE_DURATION: f32 = 1.0 / SAMPLE_RATE as f32;

pub type Sample = wide::f32x8;

#[derive(Clone)]
pub struct Frame {
    pub left: Sample,
    pub right: Option<Sample>,
}

impl Frame {
    pub(crate) fn zero() -> Self {
        Self {
            left: Sample::ZERO,
            right: None,
        }
    }

    #[must_use]
    pub fn mono(s: Sample) -> Self {
        Self {
            left: s,
            right: None,
        }
    }

    #[must_use]
    pub fn stereo(l: Sample, r: Sample) -> Self {
        Self {
            left: l,
            right: Some(r),
        }
    }
}

impl Add<&Frame> for Frame {
    type Output = Self;

    fn add(self, rhs: &Frame) -> Self {
        let left = self.left + rhs.left;
        let right = match (self.right, rhs.right) {
            (None, None) => None,
            (None, Some(r)) | (Some(r), None) => Some(r),
            (Some(a), Some(b)) => Some(a + b),
        };
        Self { left, right }
    }
}

impl Div<f32> for Frame {
    type Output = Self;

    fn div(self, rhs: f32) -> Self::Output {
        let left = self.left / rhs;
        let right = self.right.map(|r| r / rhs);
        Self { left, right }
    }
}