Skip to main content

wavegraph_sample/
types.rs

1//! A collection of custom, non-std **Sample** types.
2
3pub use self::i11::I11;
4pub use self::i20::I20;
5pub use self::i24::I24;
6pub use self::i48::I48;
7pub use self::u11::U11;
8pub use self::u20::U20;
9pub use self::u24::U24;
10pub use self::u48::U48;
11
12macro_rules! impl_from {
13    ($T:ident: $Rep:ident from {$U:ident : $URep:ty}) => {
14        impl From<$U> for $T {
15            #[inline]
16            fn from(other: $U) -> Self {
17                $T(other.inner() as $Rep)
18            }
19        }
20    };
21    ($T:ident: $Rep:ident from $U:ident) => {
22        impl From<$U> for $T {
23            #[inline]
24            fn from(other: $U) -> Self {
25                $T(other as $Rep)
26            }
27        }
28    };
29}
30
31macro_rules! impl_froms {
32    ($T:ident: $Rep:ident, {$U:ident : $URep:ty}, $($rest:tt)*) => {
33        impl_from!($T: $Rep from {$U: $URep});
34        impl_froms!($T: $Rep, $($rest)*);
35    };
36    ($T:ident: $Rep:ident, {$U:ident : $URep:ty}) => {
37        impl_from!($T: $Rep from {$U: $URep});
38    };
39    ($T:ident: $Rep:ident, $U:ident, $($rest:tt)*) => {
40        impl_from!($T: $Rep from $U);
41        impl_froms!($T: $Rep, $($rest)*);
42    };
43    ($T:ident: $Rep:ident, $U:ident) => {
44        impl_from!($T: $Rep from $U);
45    };
46    ($T:ident: $Rep:ident,) => {};
47}
48
49macro_rules! impl_neg {
50    ($T:ident) => {
51        impl ::core::ops::Neg for $T {
52            type Output = $T;
53            #[inline]
54            fn neg(self) -> $T {
55                $T(-self.0)
56            }
57        }
58    };
59}
60
61macro_rules! new_sample_type {
62    ($(#[$attr:meta])* $T:ident: $Rep:ident, eq: $EQ:expr, min: $MIN:expr, max: $MAX:expr, total: $TOTAL:expr, from: $($rest:tt)*) => {
63        pub const MIN: $T = $T($MIN);
64        pub const MAX: $T = $T($MAX);
65        pub const EQUILIBRIUM: $T = $T($EQ);
66        const MIN_REP: $Rep = $MIN;
67        const MAX_REP: $Rep = $MAX;
68        const TOTAL: $Rep = $TOTAL;
69
70        $(#[$attr])*
71        #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
72        pub struct $T($Rep);
73
74        impl From<$Rep> for $T {
75            #[inline]
76            fn from(val: $Rep) -> Self {
77                $T(val).wrap_overflow()
78            }
79        }
80
81        impl $T {
82            /// Construct a new sample if the given value is within range.
83            ///
84            /// Returns `None` if `val` is out of range.
85            #[inline]
86            pub fn new(val: $Rep) -> Option<Self> {
87                if val > MAX_REP || val < MIN_REP {
88                    None
89                } else {
90                    Some($T(val))
91                }
92            }
93
94            /// Constructs a new sample without checking for overflowing.
95            ///
96            /// This should *only* be used if the user can guarantee the sample will be within
97            /// range and they require the extra performance.
98            ///
99            /// If this function is used, the sample crate can't guarantee that the returned sample
100            /// or any interacting samples will remain within their MIN and MAX bounds.
101            pub fn new_unchecked(s: $Rep) -> Self {
102                $T(s)
103            }
104
105            /// Return the internal value used to represent the sample type.
106            #[inline]
107            pub fn inner(self) -> $Rep {
108                self.0
109            }
110
111            /// Wraps self once in the case that self has overflowed.
112            #[inline]
113            fn wrap_overflow_once(self) -> Self {
114                if      self.0 > MAX_REP { $T(self.0 - TOTAL) }
115                else if self.0 < MIN_REP { $T(self.0 + TOTAL) }
116                else                     { self }
117            }
118
119            /// Wraps self in the case that self has overflowed.
120            #[inline]
121            fn wrap_overflow(mut self) -> Self {
122                while self.0 > MAX_REP {
123                    self.0 -= TOTAL;
124                }
125                while self.0 < MIN_REP {
126                    self.0 += TOTAL;
127                }
128                self
129            }
130        }
131
132        impl ::core::ops::Add<$T> for $T {
133            type Output = $T;
134            #[inline]
135            fn add(self, other: Self) -> Self {
136                if cfg!(debug_assertions) {
137                    $T::new(self.0 + other.0).expect("arithmetic operation overflowed")
138                } else {
139                    $T(self.0 + other.0).wrap_overflow_once()
140                }
141            }
142        }
143
144        impl ::core::ops::Sub<$T> for $T {
145            type Output = $T;
146            #[inline]
147            fn sub(self, other: Self) -> Self {
148                if cfg!(debug_assertions) {
149                    $T::new(self.0 - other.0).expect("arithmetic operation overflowed")
150                } else {
151                    $T(self.0 - other.0).wrap_overflow_once()
152                }
153            }
154        }
155
156        impl ::core::ops::Mul<$T> for $T {
157            type Output = $T;
158            #[inline]
159            fn mul(self, other: Self) -> Self {
160                if cfg!(debug_assertions) {
161                    $T::new(self.0 * other.0).expect("arithmetic operation overflowed")
162                } else {
163                    $T::from(self.0 * other.0)
164                }
165            }
166        }
167
168        impl ::core::ops::Div<$T> for $T {
169            type Output = $T;
170            #[inline]
171            fn div(self, other: Self) -> Self {
172                $T(self.0 / other.0)
173            }
174        }
175
176        impl ::core::ops::Not for $T {
177            type Output = $T;
178            #[inline]
179            fn not(self) -> $T {
180                $T(!self.0)
181            }
182        }
183
184        impl ::core::ops::Rem<$T> for $T {
185            type Output = $T;
186            #[inline]
187            fn rem(self, other: Self) -> Self {
188                $T(self.0 % other.0)
189            }
190        }
191
192        impl ::core::ops::Shl<$T> for $T {
193            type Output = $T;
194            #[inline]
195            fn shl(self, other: Self) -> Self {
196                // TODO: Needs review
197                $T(self.0 << other.0)
198            }
199        }
200
201        impl ::core::ops::Shr<$T> for $T {
202            type Output = $T;
203            #[inline]
204            fn shr(self, other: Self) -> Self {
205                // TODO: Needs review
206                $T(self.0 >> other.0)
207            }
208        }
209
210        impl ::core::ops::BitAnd<$T> for $T {
211            type Output = $T;
212            #[inline]
213            fn bitand(self, other: Self) -> Self {
214                $T(self.0 & other.0)
215            }
216        }
217
218        impl ::core::ops::BitOr<$T> for $T {
219            type Output = $T;
220            #[inline]
221            fn bitor(self, other: Self) -> Self {
222                $T(self.0 | other.0)
223            }
224        }
225
226        impl ::core::ops::BitXor<$T> for $T {
227            type Output = $T;
228            #[inline]
229            fn bitxor(self, other: Self) -> Self {
230                $T(self.0 ^ other.0)
231            }
232        }
233
234        impl_froms!($T: $Rep, $($rest)*);
235    };
236}
237
238pub mod i11 {
239    new_sample_type!(I11: i16, eq: 0, min: -1024, max: 1023, total: 2048,
240                     from: i8, u8);
241    impl_neg!(I11);
242}
243
244pub mod i20 {
245    use super::{I11, U11};
246    new_sample_type!(I20: i32, eq: 0, min: -524_288, max: 524_287, total: 1_048_576,
247                     from: i8, {I11:i16}, i16, u8, {U11:i16}, u16);
248}
249
250pub mod i24 {
251    use super::{I20, U20};
252    new_sample_type!(
253        /// Signed 24-bit PCM with the layout and alignment of a native-endian `i32`.
254        #[repr(transparent)]
255        I24: i32, eq: 0, min: -8_388_608, max: 8_388_607, total: 16_777_216,
256        from: i8, i16, {I20:i32}, u8, u16, {U20:i32}
257    );
258    impl_neg!(I24);
259
260    impl I24 {
261        /// Quantize a float in signed 24-bit PCM units, truncating toward zero.
262        /// Clip overloads to the corresponding rail and map NaN to silence.
263        #[inline]
264        #[must_use]
265        pub fn from_pcm_f32(sample: f32) -> Self {
266            const RAIL: f32 = 8_388_608.0;
267            if sample.abs() < RAIL {
268                // SAFETY: the ordered comparison excludes NaN and infinities.
269                // Every remaining value truncates to an i32 within the Q23 rails.
270                Self(unsafe { sample.to_int_unchecked::<i32>() })
271            } else if sample >= RAIL {
272                MAX
273            } else if sample <= -RAIL {
274                MIN
275            } else {
276                EQUILIBRIUM
277            }
278        }
279
280        /// Borrow the underlying PCM words without copying or changing their scale.
281        #[must_use]
282        pub fn as_i32_slice(samples: &[Self]) -> &[i32] {
283            // SAFETY: each transparent I24 contains one initialized i32 with
284            // the same alignment and stride. The cast preserves the slice's
285            // non-null pointer and byte extent, including for an empty slice.
286            // The result shares the input's borrow and lifetime.
287            unsafe { core::slice::from_raw_parts(samples.as_ptr().cast(), samples.len()) }
288        }
289    }
290}
291
292pub mod i48 {
293    use super::{I20, I24, U20, U24};
294    new_sample_type!(I48: i64, eq: 0, min: -140_737_488_355_328, max: 140_737_488_355_327, total: 281_474_976_710_656,
295                     from: i8, i16, {I20:i32}, {I24:i32}, i32, u8, u16, {U20:i32}, {U24:i32}, u32);
296    impl_neg!(I48);
297}
298
299pub mod u11 {
300    new_sample_type!(U11: i16, eq: 1024, min: 0, max: 2047, total: 2048,
301                     from: u8);
302    impl_neg!(U11);
303}
304
305pub mod u20 {
306    new_sample_type!(U20: i32, eq: 524_288, min: 0, max: 1_048_575, total: 1_048_576,
307                     from: u8, u16);
308}
309
310pub mod u24 {
311    use super::U20;
312    new_sample_type!(U24: i32, eq: 8_388_608, min: 0, max: 16_777_215, total: 16_777_216,
313                     from: u8, u16, {U20:i32});
314}
315
316pub mod u48 {
317    use super::{U20, U24};
318    new_sample_type!(U48: i64, eq: 140_737_488_355_328, min: 0, max: 281_474_976_710_655, total: 281_474_976_710_656,
319                     from: u8, u16, {U20:i32}, {U24:i32}, u32);
320}