ez_audi/cpal_abstraction/samples/
sample.rs

1use std::fmt::Debug;
2
3use cpal;
4
5use super::Samples;
6
7/// Type that the samples with be converted to in order to do stuff such as apply modifiers.
8/// **Can be switched to f64 if the performance impact of moving double the data is worth it.**
9pub type IntermediateSampleType = f32;
10/// Trait implemented on all supported samples types
11pub trait Sample: cpal::SizedSample + cpal::FromSample<IntermediateSampleType> + std::marker::Send + 'static + Into<SampleType> + Debug {}
12
13impl<T: cpal::SizedSample + cpal::FromSample<IntermediateSampleType> + std::marker::Send + 'static + Into<SampleType> + Debug> Sample for T
14where IntermediateSampleType: cpal::FromSample<IntermediateSampleType> {}
15
16#[derive(Debug, Clone, PartialEq)]
17/// The underlying data type of the samples
18pub enum SampleType {
19    /// Samples using the `u8` type
20    U8,
21    /// Samples using the `u16` type
22    U16,
23    /// Samples using the `u32` type
24    U32,
25    /// Samples using the `u64` type
26    U64,
27    /// Samples using the `i8` type
28    I8,
29    /// Samples using the `i16` type
30    I16,
31    /// Samples using the `i32` type
32    I32,
33    /// Samples using the `i64` type
34    I64,
35    /// Samples using the `f32` type
36    F32,
37    /// Samples using the `f64` type
38    F64,
39}
40
41impl From<SampleType> for cpal::SampleFormat {
42    fn from(value: SampleType) -> Self {
43        match value {
44            SampleType::U8 => cpal::SampleFormat::U8,
45            SampleType::U16 => cpal::SampleFormat::U16,
46            SampleType::U32 => cpal::SampleFormat::U32,
47            SampleType::U64 => cpal::SampleFormat::U64,
48
49            SampleType::I8 => cpal::SampleFormat::I8,
50            SampleType::I16 => cpal::SampleFormat::I16,
51            SampleType::I32 => cpal::SampleFormat::I32,
52            SampleType::I64 => cpal::SampleFormat::I64,
53
54            SampleType::F32 => cpal::SampleFormat::F32,
55            SampleType::F64 => cpal::SampleFormat::F64,
56        }
57    }
58}
59
60// TODO: This is stupid bruteforcing of the problem
61// There is probably a way to make a macro for this
62impl From<u8> for SampleType {
63    fn from(_value: u8) -> Self {
64        SampleType::U8
65    }
66}
67
68impl From<u16> for SampleType {
69    fn from(_value: u16) -> Self {
70        SampleType::U16
71    }
72}
73
74impl From<u32> for SampleType {
75    fn from(_value: u32) -> Self {
76        SampleType::U32
77    }
78}
79
80impl From<u64> for SampleType {
81    fn from(_value: u64) -> Self {
82        SampleType::U64
83    }
84}
85
86impl From<i8> for SampleType {
87    fn from(_value: i8) -> Self {
88        SampleType::I8
89    }
90}
91
92impl From<i16> for SampleType {
93    fn from(_value: i16) -> Self {
94        SampleType::I16
95    }
96}
97
98impl From<i32> for SampleType {
99    fn from(_value: i32) -> Self {
100        SampleType::I32
101    }
102}
103
104impl From<i64> for SampleType {
105    fn from(_value: i64) -> Self {
106        SampleType::I64
107    }
108}
109
110impl From<f32> for SampleType {
111    fn from(_value: f32) -> Self {
112        SampleType::F32
113    }
114}
115
116impl From<f64> for SampleType {
117    fn from(_value: f64) -> Self {
118        SampleType::F64
119    }
120}
121
122// Split
123
124impl From<&Samples<u8>> for SampleType {
125    fn from(_value: &Samples<u8>) -> Self {
126        SampleType::U8
127    }
128}
129
130impl From<&Samples<u16>> for SampleType {
131    fn from(_value: &Samples<u16>) -> Self {
132        SampleType::U16
133    }
134}
135
136impl From<&Samples<u32>> for SampleType {
137    fn from(_value: &Samples<u32>) -> Self {
138        SampleType::U32
139    }
140}
141
142impl From<&Samples<u64>> for SampleType {
143    fn from(_value: &Samples<u64>) -> Self {
144        SampleType::U64
145    }
146}
147
148impl From<&Samples<i8>> for SampleType {
149    fn from(_value: &Samples<i8>) -> Self {
150        SampleType::I8
151    }
152}
153
154impl From<&Samples<i16>> for SampleType {
155    fn from(_value: &Samples<i16>) -> Self {
156        SampleType::I16
157    }
158}
159
160impl From<&Samples<i32>> for SampleType {
161    fn from(_value: &Samples<i32>) -> Self {
162        SampleType::I32
163    }
164}
165
166impl From<&Samples<i64>> for SampleType {
167    fn from(_value: &Samples<i64>) -> Self {
168        SampleType::I64
169    }
170}
171
172impl From<&Samples<f32>> for SampleType {
173    fn from(_value: &Samples<f32>) -> Self {
174        SampleType::F32
175    }
176}
177
178impl From<&Samples<f64>> for SampleType {
179    fn from(_value: &Samples<f64>) -> Self {
180        SampleType::F64
181    }
182}