cpal icon indicating copy to clipboard operation
cpal copied to clipboard

24 Bit Sample Support

Open julientregoat opened this issue 4 years ago • 2 comments

original issue here https://github.com/RustAudio/cpal/issues/414. there was also discussion in the 32 bit PR before it was split off https://github.com/RustAudio/cpal/pull/420

julientregoat avatar Jan 11 '21 00:01 julientregoat

If it helps any bit, I've got a working implementation of S24 and S24_3 samples using a i24 type in librespot-org/librespot#660.

Basically:

#[derive(AsBytes, Copy, Clone, Debug)]
 #[allow(non_camel_case_types)]
 #[repr(transparent)]
 pub struct i24([u8; 3]);
 impl i24 {
     fn pcm_from_i32(sample: i32) -> Self {
         // drop the least significant byte
         let [a, b, c, _d] = (sample >> 8).to_le_bytes();
         i24([a, b, c])
     }
 }

and:

macro_rules! convert_samples_to {
     ($type: ident, $samples: expr) => {
         convert_samples_to!($type, $samples, 0)
     };
     ($type: ident, $samples: expr, $drop_bits: expr) => {
         $samples
             .iter()
             .map(|sample| {
                 (*sample as f64 * (std::$type::MAX as f64 + 0.5) - 0.5) as $type >> $drop_bits
             })
             .collect()
     };
 }

 pub struct SamplesConverter {}
 impl SamplesConverter {

// ...

     pub fn to_s24(samples: &[f32]) -> Vec<i32> {
         convert_samples_to!(i32, samples, 8)
     }

     pub fn to_s24_3(samples: &[f32]) -> Vec<i24> {
         Self::to_s32(samples)
             .iter()
             .map(|sample| i24::pcm_from_i32(*sample))
             .collect()
     }

// ...

 }

roderickvd avatar Mar 22 '21 20:03 roderickvd

Is there any progress on this?

RoccoDevs avatar Jul 18 '21 15:07 RoccoDevs