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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use crate::Format;
use byte_slice_cast::AsByteSlice;
use std::fs::File;
use std::io::Write;

pub trait WavBitDepth {
    type T: Copy + Clone + Default + Send;

    fn new(max_block_frames: usize, num_channels: u16) -> Self;

    fn format() -> Format;

    fn write_to_disk(&mut self, data: &[Self::T], file: &mut File) -> Result<(), std::io::Error>;
}

pub struct Uint8 {}

impl WavBitDepth for Uint8 {
    type T = u8;

    fn new(_max_block_frames: usize, _num_channels: u16) -> Self {
        Self {}
    }

    fn format() -> Format {
        Format::Uint8
    }

    fn write_to_disk(&mut self, data: &[u8], file: &mut File) -> Result<(), std::io::Error> {
        file.write_all(data)
    }
}

pub struct Int16 {}

impl WavBitDepth for Int16 {
    type T = i16;

    fn new(_max_block_frames: usize, _num_channels: u16) -> Self {
        Self {}
    }

    fn format() -> Format {
        Format::Int16
    }

    fn write_to_disk(&mut self, data: &[i16], file: &mut File) -> Result<(), std::io::Error> {
        file.write_all(data.as_byte_slice())
    }
}

pub struct Int24 {
    cram_buffer: Vec<u8>,
}

impl WavBitDepth for Int24 {
    type T = i32;

    fn new(max_block_frames: usize, num_channels: u16) -> Self {
        let cram_buffer_size = max_block_frames * usize::from(num_channels) * 3;

        Self {
            cram_buffer: Vec::with_capacity(cram_buffer_size),
        }
    }

    fn format() -> Format {
        Format::Int24
    }

    fn write_to_disk(&mut self, data: &[i32], file: &mut File) -> Result<(), std::io::Error> {
        self.cram_buffer.clear();
        let num_frames = data.len();

        let data_u8 = data.as_byte_slice();

        // Hint to compiler to optimize loop.
        assert!(num_frames * 3 <= self.cram_buffer.capacity());
        assert!(num_frames * 4 == data_u8.len());

        for f in 0..num_frames {
            self.cram_buffer.push(data_u8[f * 4]);
            self.cram_buffer.push(data_u8[f * 4 + 1]);
            self.cram_buffer.push(data_u8[f * 4 + 2]);
        }

        file.write_all(&self.cram_buffer)
    }
}

pub struct Float32 {}

impl WavBitDepth for Float32 {
    type T = f32;

    fn new(_max_block_frames: usize, _num_channels: u16) -> Self {
        Self {}
    }

    fn format() -> Format {
        Format::Float32
    }

    fn write_to_disk(&mut self, data: &[f32], file: &mut File) -> Result<(), std::io::Error> {
        file.write_all(data.as_byte_slice())
    }
}

pub struct Float64 {}

impl WavBitDepth for Float64 {
    type T = f64;

    fn new(_max_block_frames: usize, _num_channels: u16) -> Self {
        Self {}
    }

    fn format() -> Format {
        Format::Float64
    }

    fn write_to_disk(&mut self, data: &[f64], file: &mut File) -> Result<(), std::io::Error> {
        file.write_all(data.as_byte_slice())
    }
}