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
pub const CUSTOM_WIDTH: usize = 32;
pub type CustomWaveform = [f32; CUSTOM_WIDTH];
pub(crate) fn sine(period: f32) -> f32 {
f32::sin(period * std::f32::consts::TAU)
}
pub(crate) fn triangle(period: f32) -> f32 {
-(period - 0.5).abs() * 4.0 + 1.0
}
pub(crate) fn rec_sine(period: f32) -> f32 {
if period < 0.5 {
f32::sin(period * std::f32::consts::TAU) * 2.0 - 1.0
}
else {
-1.0
}
}
pub(crate) fn saw(period: f32) -> f32 {
period * 2.0 - 1.0
}
pub(crate) fn square(period: f32) -> f32 {
if period < 0.5 {1.0}
else {-1.0}
}
pub(crate) fn pulse(period: f32) -> f32 {
if period < 0.25 {1.0}
else {-1.0}
}
pub(crate) fn noise() -> f32 {
rand::random::<f32>() * 2.0 - 1.0
}
pub(crate) fn custom(period: f32, data: &CustomWaveform) -> f32{
let index = (period * CUSTOM_WIDTH as f32).floor() as usize;
data[index]
}