creek_encode_wav/
wav_bit_depth.rs

1use crate::Format;
2use byte_slice_cast::AsByteSlice;
3use std::fs::File;
4use std::io::Write;
5
6pub trait WavBitDepth {
7    type T: Copy + Clone + Default + Send;
8
9    fn new(max_block_frames: usize, num_channels: u16) -> Self;
10
11    fn format() -> Format;
12
13    fn write_to_disk(&mut self, data: &[Self::T], file: &mut File) -> Result<(), std::io::Error>;
14}
15
16pub struct Uint8 {}
17
18impl WavBitDepth for Uint8 {
19    type T = u8;
20
21    fn new(_max_block_frames: usize, _num_channels: u16) -> Self {
22        Self {}
23    }
24
25    fn format() -> Format {
26        Format::Uint8
27    }
28
29    fn write_to_disk(&mut self, data: &[u8], file: &mut File) -> Result<(), std::io::Error> {
30        file.write_all(data)
31    }
32}
33
34pub struct Int16 {}
35
36impl WavBitDepth for Int16 {
37    type T = i16;
38
39    fn new(_max_block_frames: usize, _num_channels: u16) -> Self {
40        Self {}
41    }
42
43    fn format() -> Format {
44        Format::Int16
45    }
46
47    fn write_to_disk(&mut self, data: &[i16], file: &mut File) -> Result<(), std::io::Error> {
48        file.write_all(data.as_byte_slice())
49    }
50}
51
52pub struct Int24 {
53    cram_buffer: Vec<u8>,
54}
55
56impl WavBitDepth for Int24 {
57    type T = i32;
58
59    fn new(max_block_frames: usize, num_channels: u16) -> Self {
60        let cram_buffer_size = max_block_frames * usize::from(num_channels) * 3;
61
62        Self {
63            cram_buffer: Vec::with_capacity(cram_buffer_size),
64        }
65    }
66
67    fn format() -> Format {
68        Format::Int24
69    }
70
71    fn write_to_disk(&mut self, data: &[i32], file: &mut File) -> Result<(), std::io::Error> {
72        self.cram_buffer.clear();
73        let num_frames = data.len();
74
75        let data_u8 = data.as_byte_slice();
76
77        // Hint to compiler to optimize loop.
78        assert!(num_frames * 3 <= self.cram_buffer.capacity());
79        assert!(num_frames * 4 == data_u8.len());
80
81        for f in 0..num_frames {
82            self.cram_buffer.push(data_u8[f * 4]);
83            self.cram_buffer.push(data_u8[f * 4 + 1]);
84            self.cram_buffer.push(data_u8[f * 4 + 2]);
85        }
86
87        file.write_all(&self.cram_buffer)
88    }
89}
90
91pub struct Float32 {}
92
93impl WavBitDepth for Float32 {
94    type T = f32;
95
96    fn new(_max_block_frames: usize, _num_channels: u16) -> Self {
97        Self {}
98    }
99
100    fn format() -> Format {
101        Format::Float32
102    }
103
104    fn write_to_disk(&mut self, data: &[f32], file: &mut File) -> Result<(), std::io::Error> {
105        file.write_all(data.as_byte_slice())
106    }
107}
108
109pub struct Float64 {}
110
111impl WavBitDepth for Float64 {
112    type T = f64;
113
114    fn new(_max_block_frames: usize, _num_channels: u16) -> Self {
115        Self {}
116    }
117
118    fn format() -> Format {
119        Format::Float64
120    }
121
122    fn write_to_disk(&mut self, data: &[f64], file: &mut File) -> Result<(), std::io::Error> {
123        file.write_all(data.as_byte_slice())
124    }
125}