firefly_audio/
basic_types.rs1use core::ops::{Add, Div};
2
3pub type Position = u32;
4pub const SAMPLE_RATE: Position = 44_100;
5pub const SAMPLE_DURATION: f32 = 1.0 / SAMPLE_RATE as f32;
6
7pub type Sample = wide::f32x8;
8
9#[derive(Clone)]
10pub struct Frame {
11 pub left: Sample,
12 pub right: Option<Sample>,
13}
14
15impl Frame {
16 pub(crate) const fn zero() -> Self {
17 Self {
18 left: Sample::ZERO,
19 right: None,
20 }
21 }
22
23 #[must_use]
24 pub const fn mono(s: Sample) -> Self {
25 Self {
26 left: s,
27 right: None,
28 }
29 }
30
31 #[must_use]
32 pub const fn stereo(l: Sample, r: Sample) -> Self {
33 Self {
34 left: l,
35 right: Some(r),
36 }
37 }
38}
39
40impl Add<&Self> for Frame {
41 type Output = Self;
42
43 fn add(self, rhs: &Self) -> Self {
44 let left = self.left + rhs.left;
45 let right = match (self.right, rhs.right) {
46 (None, None) => None,
47 (None, Some(r)) | (Some(r), None) => Some(r),
48 (Some(a), Some(b)) => Some(a + b),
49 };
50 Self { left, right }
51 }
52}
53
54impl Div<f32> for Frame {
55 type Output = Self;
56
57 fn div(self, rhs: f32) -> Self::Output {
58 let left = self.left / rhs;
59 let right = self.right.map(|r| r / rhs);
60 Self { left, right }
61 }
62}