pub fn parse_212_format(buf: &[u8]) -> Vec<i16> {
let mut output_buf = vec![];
for idx in (0..buf.len()).step_by(3) {
if idx + 1 >= buf.len() { break }
let sample_1_lower = buf[idx] as u16;
let mut sample_1_upper = ((buf[idx+1] & 0x0F) as u16) << 8;
if (sample_1_upper & 0x0800) != 0 {
sample_1_upper |= 0xF000;
}
output_buf.push((sample_1_lower | sample_1_upper) as i16);
if idx + 2 >= buf.len() { break }
let sample_2_lower = buf[idx+2] as u16;
let mut sample_2_upper = ((buf[idx+1] & 0xF0) as u16) << 4;
if (sample_2_upper & 0x0800) != 0 {
sample_2_upper |= 0xF000;
}
output_buf.push((sample_2_lower | sample_2_upper) as i16);
}
output_buf
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn basic_byte_parser() {
let byte_buf = [
0xF0, 0x68, 0x80,
0xF0, 0x68, 0x80,
];
assert_eq!(
parse_212_format(&byte_buf),
vec![
-1808, 1664, -1808, 1664, ]
);
}
#[test]
fn incomplete_buffer() {
let byte_buf = [
0xF0, 0x86,
];
assert_eq!(
parse_212_format(&byte_buf),
vec![
1776, ]
);
}
#[test]
fn negative_values_buffer() {
let byte_buf = [
0xFF, 0x8F, 0x80
];
assert_eq!(
parse_212_format(&byte_buf),
vec![
-1, -1920 ]
);
}
}