1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! Parsing logic for each signal format type

/// Parse a byte buffer of data in format 212, 12-bit two's complement amplitude.
///
/// Two 12-bit samples in 3 bytes:
///
/// ```text
/// | 8 7 6 5 4 3 2 1 | 12 11 10 9 12 11 10 9 | 8 7 6 5 4 3 2 1 |
/// ```
///
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] & 0xF0) as u16) << 4;
        if (sample_1_upper & 0x0800) != 0 {
            // Extend two's complement sign bits if last bit is 1
            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] & 0x0F) as u16) << 8;
        if (sample_2_upper & 0x0800) != 0 {
            // Extend two's complement sign bits if last bit is 1
            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
        ];
        assert_eq!(
            parse_212_format(&byte_buf),
            vec![
                1776, -1920, // 0x6F0, 0x880
            ]
        );
    }

    #[test]
    fn incomplete_buffer() {
        let byte_buf = [
            0xF0, 0x68,
        ];
        assert_eq!(
            parse_212_format(&byte_buf),
            vec![
                1776, //0x6F0
            ]
        );
    }

    #[test]
    fn negative_values_buffer() {
        let byte_buf = [
            0xFF, 0xF8, 0x80
        ];
        assert_eq!(
            parse_212_format(&byte_buf),
            vec![
                -1, -1920 // 0xFFF, 0x880
            ]
        );
    }
}