-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
165 lines (134 loc) · 4.27 KB
/
mod.rs
File metadata and controls
165 lines (134 loc) · 4.27 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use core::slice;
use std::{
array,
ops::{Div, DivAssign, IndexMut},
};
use dasp::sample::ToSample;
pub(crate) mod instrument;
pub mod playback;
pub(crate) mod sample;
pub use sample::Interpolation;
#[repr(transparent)]
#[derive(Clone, Copy, Default, Debug, PartialEq)]
pub struct Frame([f32; 2]);
impl std::ops::AddAssign for Frame {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl std::ops::Add for Frame {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self([self.0[0] + rhs.0[0], self.0[1] + rhs.0[1]])
}
}
impl std::ops::Sub for Frame {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self([self.0[0] - rhs.0[0], self.0[1] - rhs.0[1]])
}
}
impl std::ops::SubAssign for Frame {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl std::ops::MulAssign<f32> for Frame {
fn mul_assign(&mut self, rhs: f32) {
*self.0.index_mut(0) *= rhs;
*self.0.index_mut(1) *= rhs;
}
}
impl std::ops::Mul<f32> for Frame {
type Output = Self;
fn mul(self, rhs: f32) -> Self::Output {
Self([self.0[0] * rhs, self.0[1] * rhs])
}
}
impl Div<f32> for Frame {
type Output = Frame;
fn div(self, rhs: f32) -> Self::Output {
Self([self.0[0] / rhs, self.0[1] / rhs])
}
}
impl DivAssign<f32> for Frame {
fn div_assign(&mut self, rhs: f32) {
*self.0.index_mut(0) *= rhs;
*self.0.index_mut(1) *= rhs;
}
}
impl crate::sample::ProcessingFrame for Frame {
fn mul_add(self, mul: f32, add: Self) -> Self {
Self([
f32::mul_add(self.0[0], mul, add.0[0]),
f32::mul_add(self.0[1], mul, add.0[1]),
])
}
}
impl std::iter::Sum for Frame {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.reduce(|acc, x| acc + x).unwrap_or_default()
}
}
impl From<[f32; 2]> for Frame {
fn from(value: [f32; 2]) -> Self {
Self(value)
}
}
impl From<f32> for Frame {
fn from(value: f32) -> Self {
Self([value, value])
}
}
impl Frame {
// split into left and right.
pub fn split_array<const N: usize>(value: [Frame; N]) -> ([f32; N], [f32; N]) {
(
array::from_fn(|i| value[i].0[0]),
array::from_fn(|i| value[i].0[1]),
)
}
pub fn sum_to_mono(self) -> f32 {
self.0[0] + self.0[1]
}
pub fn from_mut<'a>(value: &'a mut [f32; 2]) -> &'a mut Self {
// SAFETY: lifetime is specified, both mut, Self is repr(transparent).
unsafe { std::mem::transmute::<&'a mut [f32; 2], &'a mut Self>(value) }
}
pub fn from_interleaved(value: &[f32]) -> &[Frame] {
debug_assert!(value.len().rem_euclid(2) == 0);
let len = value.len() / 2;
let ptr = value.as_ptr().cast();
// SAFETY: keeps the same lifetime and mutability.
// [f32; 2] is a valid frame
unsafe { slice::from_raw_parts(ptr, len) }
}
pub fn from_ref<'a>(value: &'a [f32; 2]) -> &'a Self {
// SAFETY: lifetime is specified, both not mut, Self is repr(transparent).
unsafe { std::mem::transmute::<&'a [f32; 2], &'a Self>(value) }
}
pub fn to_sample<S: dasp::sample::FromSample<f32>>(self) -> [S; 2] {
[self.0[0].to_sample_(), self.0[1].to_sample_()]
}
pub fn to_raw<'a>(into: &mut [Self]) -> &'a mut [[f32; 2]] {
unsafe { std::mem::transmute(into) }
}
// pan laws taken from: https://www.cs.cmu.edu/~music/icm-online/readings/panlaws/index.html
// /// angle in radians between 0 and 90°
// pub fn pan_linear(&mut self, angle: f32) {
// self.0[0] *= (std::f32::consts::FRAC_PI_2 - angle) * std::f32::consts::FRAC_2_PI;
// self.0[1] *= angle * std::f32::consts::FRAC_2_PI;
// }
/// angle in radians between 0 and 90°
pub fn pan_constant_power(&mut self, angle: f32) {
self.0[0] *= angle.cos();
self.0[1] *= angle.sin();
}
// /// angle in radians between 0 and 90°
// pub fn pan_compromise(&mut self, angle: f32) {
// self.0[0] *= f32::sqrt(
// (std::f32::consts::FRAC_PI_2 - angle) * std::f32::consts::FRAC_2_PI * angle.cos(),
// );
// self.0[1] *= f32::sqrt(angle * std::f32::consts::FRAC_2_PI * angle.sin());
// }
}