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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
pub type Wave = f32;
pub type Amplitude = f32;
pub enum Format {
F32,
I32,
I16,
I8,
U32,
U16,
U8,
}
impl Format {
pub fn size_in_bytes(&self) -> usize {
use std::mem::size_of;
match *self {
Format::F32 => size_of::<f32>(),
Format::I32 => size_of::<i32>(),
Format::I16 => size_of::<i16>(),
Format::I8 => size_of::<i8>(),
Format::U32 => size_of::<u32>(),
Format::U16 => size_of::<u16>(),
Format::U8 => size_of::<u8>(),
}
}
}
pub trait Sample:
Copy +
Clone +
::std::default::Default +
::std::fmt::Debug +
PartialOrd +
PartialEq +
::std::ops::Add<Output=Self> +
::std::ops::Sub<Output=Self> +
::std::ops::Mul<Output=Self> +
::std::ops::Div<Output=Self> +
::std::ops::Rem<Output=Self> +
{
fn from_wave(Wave) -> Self;
fn to_wave(self) -> Wave;
fn sample_format(&self) -> Format;
fn format<S: Sample>() -> Format {
let sample: S = ::std::default::Default::default();
sample.sample_format()
}
#[inline]
fn mul_amp(self, amp: f32) -> Self {
Sample::from_wave(self.to_wave() * amp)
}
#[inline]
fn from_sample<S: Sample>(sample: S) -> Self {
Sample::from_wave(sample.to_wave())
}
#[inline]
fn to_sample<S: Sample>(self) -> S {
Sample::from_wave(self.to_wave())
}
#[inline]
fn zero() -> Self { ::std::default::Default::default() }
#[inline]
fn add_buffers(output: &mut [Self], working: &[Self], vol_per_channel: &[Amplitude]) {
let channels = vol_per_channel.len();
let output_frames = output.chunks_mut(channels);
let working_frames = working.chunks(channels);
for (output_frame, working_frame) in output_frames.zip(working_frames) {
let output_channels = output_frame.iter_mut();
let working_channels = working_frame.iter();
let channel_vols = vol_per_channel.iter();
for ((o, w), vol) in output_channels.zip(working_channels).zip(channel_vols) {
*o = *o + w.mul_amp(*vol);
}
}
}
#[inline]
fn zero_buffer(buffer: &mut [Self]) {
for sample in buffer.iter_mut() { *sample = Sample::zero() }
}
}
impl Sample for f32 {
#[inline]
fn from_wave(wave: Wave) -> f32 { wave }
#[inline]
fn to_wave(self) -> Wave { self }
fn sample_format(&self) -> Format { Format::F32 }
}
const RESOLUTION_HEADROOM_I32: i32 = 128;
impl Sample for i32 {
#[inline]
fn from_wave(wave: Wave) -> i32 {
const MAX: Wave = (::std::i32::MAX - RESOLUTION_HEADROOM_I32) as Wave;
(MAX * wave) as i32
}
#[inline]
fn to_wave(self) -> Wave {
const MAX: Wave = (::std::i32::MAX - RESOLUTION_HEADROOM_I32) as Wave;
self as Wave / MAX
}
fn sample_format(&self) -> Format { Format::I32 }
}
impl Sample for i16 {
#[inline]
fn from_wave(wave: Wave) -> i16 {
const MAX: Wave = ::std::i16::MAX as Wave;
(MAX * wave) as i16
}
#[inline]
fn to_wave(self) -> Wave {
const MAX: Wave = ::std::i16::MAX as Wave;
self as Wave / MAX
}
fn sample_format(&self) -> Format { Format::I16 }
}
impl Sample for i8 {
#[inline]
fn from_wave(wave: Wave) -> i8 {
const MAX: Wave = ::std::i8::MAX as Wave;
(MAX * wave) as i8
}
#[inline]
fn to_wave(self) -> Wave {
const MAX: Wave = ::std::i8::MAX as Wave;
self as Wave / MAX
}
fn sample_format(&self) -> Format { Format::I8 }
}
const RESOLUTION_HEADROOM_U32: u32 = 128;
impl Sample for u32 {
#[inline]
fn from_wave(wave: Wave) -> u32 {
const HALF_MAX: Wave = ((::std::u32::MAX - RESOLUTION_HEADROOM_U32) / 2) as Wave;
(HALF_MAX + HALF_MAX * wave) as u32
}
#[inline]
fn to_wave(self) -> Wave {
const MAX: Wave = (::std::u32::MAX - RESOLUTION_HEADROOM_U32) as Wave;
(self as Wave / MAX) * 2.0 - 1.0
}
fn sample_format(&self) -> Format { Format::U32 }
}
impl Sample for u16 {
#[inline]
fn from_wave(wave: Wave) -> u16 {
const HALF_MAX: Wave = (::std::u16::MAX / 2) as Wave;
(HALF_MAX + HALF_MAX * wave) as u16
}
#[inline]
fn to_wave(self) -> Wave {
const MAX: Wave = ::std::u16::MAX as Wave;
(self as Wave / MAX) * 2.0 - 1.0
}
fn sample_format(&self) -> Format { Format::U16 }
}
impl Sample for u8 {
#[inline]
fn from_wave(wave: Wave) -> u8 {
const HALF_MAX: Wave = (::std::u8::MAX / 2) as Wave;
(HALF_MAX + HALF_MAX * wave) as u8
}
#[inline]
fn to_wave(self) -> Wave {
const MAX: Wave = ::std::u8::MAX as Wave;
(self as Wave / MAX) * 2.0 - 1.0
}
fn sample_format(&self) -> Format { Format::U8 }
}