| 1 | use std::{fmt::Display, mem}; |
| 2 | #[cfg (target_os = "emscripten" )] |
| 3 | use wasm_bindgen::prelude::*; |
| 4 | |
| 5 | pub use dasp_sample::{FromSample, Sample, I24, I48, U24, U48}; |
| 6 | |
| 7 | /// Format that each sample has. |
| 8 | #[cfg_attr (target_os = "emscripten" , wasm_bindgen)] |
| 9 | #[derive (Clone, Copy, Debug, PartialEq, Eq)] |
| 10 | #[non_exhaustive ] |
| 11 | pub enum SampleFormat { |
| 12 | /// `i8` with a valid range of 'u8::MIN..=u8::MAX' with `0` being the origin |
| 13 | I8, |
| 14 | |
| 15 | /// `i16` with a valid range of 'u16::MIN..=u16::MAX' with `0` being the origin |
| 16 | I16, |
| 17 | |
| 18 | // /// `I24` with a valid range of '-(1 << 23)..(1 << 23)' with `0` being the origin |
| 19 | // I24, |
| 20 | /// `i32` with a valid range of 'u32::MIN..=u32::MAX' with `0` being the origin |
| 21 | I32, |
| 22 | |
| 23 | // /// `I24` with a valid range of '-(1 << 47)..(1 << 47)' with `0` being the origin |
| 24 | // I48, |
| 25 | /// `i64` with a valid range of 'u64::MIN..=u64::MAX' with `0` being the origin |
| 26 | I64, |
| 27 | |
| 28 | /// `u8` with a valid range of 'u8::MIN..=u8::MAX' with `1 << 7 == 128` being the origin |
| 29 | U8, |
| 30 | |
| 31 | /// `u16` with a valid range of 'u16::MIN..=u16::MAX' with `1 << 15 == 32768` being the origin |
| 32 | U16, |
| 33 | |
| 34 | // /// `U24` with a valid range of '0..16777216' with `1 << 23 == 8388608` being the origin |
| 35 | // U24, |
| 36 | /// `u32` with a valid range of 'u32::MIN..=u32::MAX' with `1 << 31` being the origin |
| 37 | U32, |
| 38 | |
| 39 | // /// `U48` with a valid range of '0..(1 << 48)' with `1 << 47` being the origin |
| 40 | // U48, |
| 41 | /// `u64` with a valid range of 'u64::MIN..=u64::MAX' with `1 << 63` being the origin |
| 42 | U64, |
| 43 | |
| 44 | /// `f32` with a valid range of `-1.0..1.0` with `0.0` being the origin |
| 45 | F32, |
| 46 | |
| 47 | /// `f64` with a valid range of -1.0..1.0 with 0.0 being the origin |
| 48 | F64, |
| 49 | } |
| 50 | |
| 51 | impl SampleFormat { |
| 52 | /// Returns the size in bytes of a sample of this format. |
| 53 | #[inline ] |
| 54 | #[must_use ] |
| 55 | pub fn sample_size(&self) -> usize { |
| 56 | match *self { |
| 57 | SampleFormat::I8 | SampleFormat::U8 => mem::size_of::<i8>(), |
| 58 | SampleFormat::I16 | SampleFormat::U16 => mem::size_of::<i16>(), |
| 59 | // SampleFormat::I24 | SampleFormat::U24 => 3, |
| 60 | SampleFormat::I32 | SampleFormat::U32 => mem::size_of::<i32>(), |
| 61 | // SampleFormat::I48 | SampleFormat::U48 => 6, |
| 62 | SampleFormat::I64 | SampleFormat::U64 => mem::size_of::<i64>(), |
| 63 | SampleFormat::F32 => mem::size_of::<f32>(), |
| 64 | SampleFormat::F64 => mem::size_of::<f64>(), |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #[inline ] |
| 69 | #[must_use ] |
| 70 | pub fn is_int(&self) -> bool { |
| 71 | //matches!(*self, SampleFormat::I8 | SampleFormat::I16 | SampleFormat::I24 | SampleFormat::I32 | SampleFormat::I48 | SampleFormat::I64) |
| 72 | matches!( |
| 73 | *self, |
| 74 | SampleFormat::I8 | SampleFormat::I16 | SampleFormat::I32 | SampleFormat::I64 |
| 75 | ) |
| 76 | } |
| 77 | |
| 78 | #[inline ] |
| 79 | #[must_use ] |
| 80 | pub fn is_uint(&self) -> bool { |
| 81 | //matches!(*self, SampleFormat::U8 | SampleFormat::U16 | SampleFormat::U24 | SampleFormat::U32 | SampleFormat::U48 | SampleFormat::U64) |
| 82 | matches!( |
| 83 | *self, |
| 84 | SampleFormat::U8 | SampleFormat::U16 | SampleFormat::U32 | SampleFormat::U64 |
| 85 | ) |
| 86 | } |
| 87 | |
| 88 | #[inline ] |
| 89 | #[must_use ] |
| 90 | pub fn is_float(&self) -> bool { |
| 91 | matches!(*self, SampleFormat::F32 | SampleFormat::F64) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl Display for SampleFormat { |
| 96 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 97 | match&'static str *self { |
| 98 | SampleFormat::I8 => "i8" , |
| 99 | SampleFormat::I16 => "i16" , |
| 100 | // SampleFormat::I24 => "i24", |
| 101 | SampleFormat::I32 => "i32" , |
| 102 | // SampleFormat::I48 => "i48", |
| 103 | SampleFormat::I64 => "i64" , |
| 104 | SampleFormat::U8 => "u8" , |
| 105 | SampleFormat::U16 => "u16" , |
| 106 | // SampleFormat::U24 => "u24", |
| 107 | SampleFormat::U32 => "u32" , |
| 108 | // SampleFormat::U48 => "u48", |
| 109 | SampleFormat::U64 => "u64" , |
| 110 | SampleFormat::F32 => "f32" , |
| 111 | SampleFormat::F64 => "f64" , |
| 112 | } |
| 113 | .fmt(f) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | pub trait SizedSample: Sample { |
| 118 | const FORMAT: SampleFormat; |
| 119 | } |
| 120 | |
| 121 | impl SizedSample for i8 { |
| 122 | const FORMAT: SampleFormat = SampleFormat::I8; |
| 123 | } |
| 124 | |
| 125 | impl SizedSample for i16 { |
| 126 | const FORMAT: SampleFormat = SampleFormat::I16; |
| 127 | } |
| 128 | |
| 129 | // impl SizedSample for I24 { const FORMAT: SampleFormat = SampleFormat::I24; } |
| 130 | |
| 131 | impl SizedSample for i32 { |
| 132 | const FORMAT: SampleFormat = SampleFormat::I32; |
| 133 | } |
| 134 | |
| 135 | // impl SizedSample for I48 { const FORMAT: SampleFormat = SampleFormat::I48; } |
| 136 | |
| 137 | impl SizedSample for i64 { |
| 138 | const FORMAT: SampleFormat = SampleFormat::I64; |
| 139 | } |
| 140 | |
| 141 | impl SizedSample for u8 { |
| 142 | const FORMAT: SampleFormat = SampleFormat::U8; |
| 143 | } |
| 144 | |
| 145 | impl SizedSample for u16 { |
| 146 | const FORMAT: SampleFormat = SampleFormat::U16; |
| 147 | } |
| 148 | |
| 149 | // impl SizedSample for U24 { const FORMAT: SampleFormat = SampleFormat::U24; } |
| 150 | |
| 151 | impl SizedSample for u32 { |
| 152 | const FORMAT: SampleFormat = SampleFormat::U32; |
| 153 | } |
| 154 | |
| 155 | // impl SizedSample for U48 { const FORMAT: SampleFormat = SampleFormat::U48; } |
| 156 | |
| 157 | impl SizedSample for u64 { |
| 158 | const FORMAT: SampleFormat = SampleFormat::U64; |
| 159 | } |
| 160 | |
| 161 | impl SizedSample for f32 { |
| 162 | const FORMAT: SampleFormat = SampleFormat::F32; |
| 163 | } |
| 164 | |
| 165 | impl SizedSample for f64 { |
| 166 | const FORMAT: SampleFormat = SampleFormat::F64; |
| 167 | } |
| 168 | |