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
126
127
128
129
130
131
132
133
use std::convert::TryFrom;
pub const WAV_FORMAT_PCM: u16 = 0x01;
pub const WAV_FORMAT_IEEE_FLOAT: u16 = 0x03;
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
#[allow(missing_docs)]
pub struct Header {
pub audio_format: u16,
pub channel_count: u16,
pub sampling_rate: u32,
pub bytes_per_second: u32,
pub bytes_per_sample: u16,
pub bits_per_sample: u16,
}
impl Header {
pub fn new(
audio_format: u16,
channel_count: u16,
sampling_rate: u32,
bits_per_sample: u16,
) -> Header {
Header {
audio_format,
channel_count,
sampling_rate,
bits_per_sample,
bytes_per_second: (((bits_per_sample >> 3) * channel_count) as u32) * sampling_rate,
bytes_per_sample: ((bits_per_sample >> 3) * channel_count) as u16,
}
}
}
impl From<Header> for [u8; 16] {
fn from(h: Header) -> Self {
let mut v: [u8; 16] = [0; 16];
let b = h.audio_format.to_le_bytes();
v[0] = b[0];
v[1] = b[1];
let b = h.channel_count.to_le_bytes();
v[2] = b[0];
v[3] = b[1];
let b = h.sampling_rate.to_le_bytes();
v[4] = b[0];
v[5] = b[1];
v[6] = b[2];
v[7] = b[3];
let b = h.bytes_per_second.to_le_bytes();
v[8] = b[0];
v[9] = b[1];
v[10] = b[2];
v[11] = b[3];
let b = h.bytes_per_sample.to_le_bytes();
v[12] = b[0];
v[13] = b[1];
let b = h.bits_per_sample.to_le_bytes();
v[14] = b[0];
v[15] = b[1];
v
}
}
impl From<[u8; 16]> for Header {
fn from(v: [u8; 16]) -> Self {
let audio_format = u16::from_le_bytes([v[0], v[1]]);
let channel_count = u16::from_le_bytes([v[2], v[3]]);
let sampling_rate = u32::from_le_bytes([v[4], v[5], v[6], v[7]]);
let bytes_per_second = u32::from_le_bytes([v[8], v[9], v[10], v[11]]);
let bytes_per_sample = u16::from_le_bytes([v[12], v[13]]);
let bits_per_sample = u16::from_le_bytes([v[14], v[15]]);
Header {
audio_format,
channel_count,
sampling_rate,
bytes_per_second,
bytes_per_sample,
bits_per_sample,
}
}
}
impl TryFrom<&[u8]> for Header {
type Error = &'static str;
fn try_from(v: &[u8]) -> Result<Self, Self::Error> {
if v.len() < 16 {
Err("Slice is smaller than the minimum-required 16 bytes")
} else {
let mut a: [u8; 16] = [0; 16];
a.copy_from_slice(&v[0..16]);
Ok(Header::from(a))
}
}
}