embedded_audio/stream/
sigma_delta_bits.rs1#[derive(Debug, Clone, Copy)]
3pub struct SigmaDeltaBitStream<'a> {
4 data: &'a [u8],
5 byte_index: usize,
6 bit_mask: u8,
7 looped: bool,
8}
9
10impl<'a> SigmaDeltaBitStream<'a> {
11 pub fn new(data: &'a [u8], effect_flags: u8) -> Self {
12 Self {
13 data,
14 byte_index: 0,
15 bit_mask: 0x80,
16 looped: effect_flags & crate::tier::flags::LOOP != 0,
17 }
18 }
19
20 pub fn reset(&mut self) {
21 self.byte_index = 0;
22 self.bit_mask = 0x80;
23 }
24
25 pub fn is_done(&self) -> bool {
26 !self.looped && self.byte_index >= self.data.len()
27 }
28}
29
30impl<'a> SigmaDeltaBitStream<'a> {
31 pub fn next_sample(&mut self) -> Option<i8> {
32 if self.byte_index >= self.data.len() {
33 if self.looped && !self.data.is_empty() {
34 self.reset();
35 } else {
36 return None;
37 }
38 }
39 let byte = self.data[self.byte_index];
40 let high = (byte & self.bit_mask) != 0;
41 self.bit_mask >>= 1;
42 if self.bit_mask == 0 {
43 self.bit_mask = 0x80;
44 self.byte_index += 1;
45 }
46 Some(if high { 127 } else { -127 })
47 }
48}