| Lift freeq's AV media plane out of sleek 90f8b89 nandi 19d ago | 1 | use crate::{Sample, SampleFormat, I24, U24}; |
| 2 | |
| 3 | #[cfg(target_os = "android")] |
| 4 | pub(crate) mod aaudio; |
| 5 | #[cfg(any( |
| 6 | target_os = "linux", |
| 7 | target_os = "dragonfly", |
| 8 | target_os = "freebsd", |
| 9 | target_os = "netbsd" |
| 10 | ))] |
| 11 | pub(crate) mod alsa; |
| 12 | #[cfg(all(windows, feature = "asio"))] |
| 13 | pub(crate) mod asio; |
| 14 | #[cfg(all( |
| 15 | feature = "wasm-bindgen", |
| 16 | feature = "audioworklet", |
| 17 | target_feature = "atomics" |
| 18 | ))] |
| 19 | pub(crate) mod audioworklet; |
| 20 | #[cfg(windows)] |
| 21 | pub(crate) mod com; |
| 22 | #[cfg(any(target_os = "macos", target_os = "ios"))] |
| 23 | pub(crate) mod coreaudio; |
| 24 | #[cfg(target_os = "emscripten")] |
| 25 | pub(crate) mod emscripten; |
| 26 | #[cfg(all( |
| 27 | feature = "jack", |
| 28 | any( |
| 29 | target_os = "linux", |
| 30 | target_os = "dragonfly", |
| 31 | target_os = "freebsd", |
| 32 | target_os = "netbsd", |
| 33 | target_os = "macos", |
| 34 | target_os = "windows", |
| 35 | ) |
| 36 | ))] |
| 37 | pub(crate) mod jack; |
| 38 | #[cfg(all( |
| 39 | any( |
| 40 | target_os = "linux", |
| 41 | target_os = "dragonfly", |
| 42 | target_os = "freebsd", |
| 43 | target_os = "netbsd", |
| 44 | ), |
| 45 | feature = "pipewire" |
| 46 | ))] |
| 47 | pub(crate) mod pipewire; |
| 48 | #[cfg(all( |
| 49 | any( |
| 50 | target_os = "linux", |
| 51 | target_os = "dragonfly", |
| 52 | target_os = "freebsd", |
| 53 | target_os = "netbsd" |
| 54 | ), |
| 55 | feature = "pulseaudio" |
| 56 | ))] |
| 57 | pub(crate) mod pulseaudio; |
| 58 | #[cfg(windows)] |
| 59 | pub(crate) mod wasapi; |
| 60 | #[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))] |
| 61 | pub(crate) mod webaudio; |
| 62 | |
| 63 | #[cfg(feature = "custom")] |
| 64 | pub(crate) mod custom; |
| 65 | #[cfg(not(any( |
| 66 | windows, |
| 67 | target_os = "linux", |
| 68 | target_os = "dragonfly", |
| 69 | target_os = "freebsd", |
| 70 | target_os = "netbsd", |
| 71 | target_os = "macos", |
| 72 | target_os = "ios", |
| 73 | target_os = "emscripten", |
| 74 | target_os = "android", |
| 75 | all(target_arch = "wasm32", feature = "wasm-bindgen"), |
| 76 | )))] |
| 77 | pub(crate) mod null; |
| 78 | |
| 79 | // Fill a buffer with equilibrium values for any sample format. |
| 80 | // Works with any buffer size, even if not perfectly aligned to sample boundaries. |
| 81 | #[allow(unused)] |
| 82 | pub(crate) fn fill_with_equilibrium(buffer: &mut [u8], sample_format: SampleFormat) { |
| 83 | macro_rules! fill_typed { |
| 84 | ($sample_type:ty) => {{ |
| 85 | let sample_size = std::mem::size_of::<$sample_type>(); |
| 86 | |
| 87 | debug_assert_eq!( |
| 88 | buffer.len() % sample_size, |
| 89 | 0, |
| 90 | "Buffer size must be aligned to sample size for format {:?}", |
| 91 | sample_format |
| 92 | ); |
| 93 | |
| 94 | let num_samples = buffer.len() / sample_size; |
| 95 | let equilibrium = <$sample_type as Sample>::EQUILIBRIUM; |
| 96 | |
| 97 | // Safety: We verified the buffer size is correctly aligned for the sample type |
| 98 | let samples = unsafe { |
| 99 | std::slice::from_raw_parts_mut( |
| 100 | buffer.as_mut_ptr() as *mut $sample_type, |
| 101 | num_samples, |
| 102 | ) |
| 103 | }; |
| 104 | |
| 105 | for sample in samples { |
| 106 | *sample = equilibrium; |
| 107 | } |
| 108 | }}; |
| 109 | } |
| 110 | const DSD_SILENCE_BYTE: u8 = 0x69; |
| 111 | |
| 112 | match sample_format { |
| 113 | SampleFormat::I8 => fill_typed!(i8), |
| 114 | SampleFormat::I16 => fill_typed!(i16), |
| 115 | SampleFormat::I24 => fill_typed!(I24), |
| 116 | SampleFormat::I32 => fill_typed!(i32), |
| 117 | // SampleFormat::I48 => fill_typed!(I48), |
| 118 | SampleFormat::I64 => fill_typed!(i64), |
| 119 | SampleFormat::U8 => fill_typed!(u8), |
| 120 | SampleFormat::U16 => fill_typed!(u16), |
| 121 | SampleFormat::U24 => fill_typed!(U24), |
| 122 | SampleFormat::U32 => fill_typed!(u32), |
| 123 | // SampleFormat::U48 => fill_typed!(U48), |
| 124 | SampleFormat::U64 => fill_typed!(u64), |
| 125 | SampleFormat::F32 => fill_typed!(f32), |
| 126 | SampleFormat::F64 => fill_typed!(f64), |
| 127 | SampleFormat::DsdU8 | SampleFormat::DsdU16 | SampleFormat::DsdU32 => { |
| 128 | buffer.fill(DSD_SILENCE_BYTE) |
| 129 | } |
| 130 | } |
| 131 | } |