2022-12-24 06:21:58 +01:00
|
|
|
// use libpulse_binding::sample::Format;
|
|
|
|
|
|
|
|
// pub fn parser(fmt: Format) -> impl SampleParser {
|
|
|
|
// match fmt {
|
|
|
|
// Format::S16NE => Signed16PCM {},
|
|
|
|
// _ => panic!("parser not implemented for this format")
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
pub trait SampleParser {
|
2023-01-05 00:51:05 +01:00
|
|
|
fn oscilloscope(&self, data: &mut [u8], channels: u8) -> Vec<Vec<f64>>;
|
|
|
|
fn sample_size(&self) -> usize;
|
2022-12-24 06:21:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Signed16PCM {}
|
|
|
|
|
2022-12-24 06:57:56 +01:00
|
|
|
/// TODO these are kinda inefficient, can they be faster?
|
2022-12-24 06:21:58 +01:00
|
|
|
impl SampleParser for Signed16PCM {
|
2023-01-05 00:51:05 +01:00
|
|
|
fn oscilloscope(&self, data: &mut [u8], channels: u8) -> Vec<Vec<f64>> {
|
2022-12-27 19:01:48 +01:00
|
|
|
let mut out = vec![vec![]; channels as usize];
|
|
|
|
let mut channel = 0;
|
|
|
|
for chunk in data.chunks(2) {
|
|
|
|
let buf = chunk[0] as i16 | (chunk[1] as i16) << 8;
|
|
|
|
out[channel].push(buf as f64);
|
|
|
|
channel = (channel + 1 ) % channels as usize;
|
2022-12-24 06:21:58 +01:00
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
2023-01-05 00:51:05 +01:00
|
|
|
|
|
|
|
fn sample_size(&self) -> usize {
|
|
|
|
return 2; // 16 bit, thus 2 bytes
|
|
|
|
}
|
2022-12-24 06:21:58 +01:00
|
|
|
}
|