atm0s_media_server_codecs/
resample.rs1use libsoxr::{Datatype, IOSpec, QualityFlags, QualityRecipe, QualitySpec, Soxr};
2
3fn create_soxr(from: u32, to: u32) -> Option<Soxr> {
4 let io_spec = IOSpec::new(Datatype::Int16I, Datatype::Int16I);
5 let quality_spec = QualitySpec::new(&QualityRecipe::VeryHigh, QualityFlags::HI_PREC_CLOCK);
6 Soxr::create(from as f64, to as f64, 1, Some(&io_spec), Some(&quality_spec), None).ok()
7}
8
9pub struct Resampler<const FROM: u32, const TO: u32> {
10 soxr: Soxr,
11}
12
13impl<const FROM: u32, const TO: u32> Default for Resampler<FROM, TO> {
14 fn default() -> Self {
15 Self {
16 soxr: create_soxr(FROM, TO).expect("Should create soxr"),
17 }
18 }
19}
20
21impl<const FROM: u32, const TO: u32> Resampler<FROM, TO> {
22 pub fn resample(&mut self, input: &[i16], output: &mut [i16]) -> Option<usize> {
23 let (_used, generated) = self.soxr.process(Some(input), &mut output[0..(input.len() * TO as usize / FROM as usize)]).expect("Should process");
24 Some(generated)
25 }
26}