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
extern crate rand;
mod pink;
mod white;
pub use pink::PnkGenerator;
pub use white::WhtGenerator;
pub struct Generator(f64, f64, f64);
impl Generator {
#[inline(always)] pub fn new(hz: f64, volume: f64) -> Self {
debug_assert!(volume <= 1.0 && volume >= 0.0);
Generator(hz / 48_000.0, volume, 0.0)
}
#[inline(always)] pub fn pitch(&mut self, hz: f64) {
self.0 = hz / 48_000.0;
}
#[inline(always)] pub fn volume(&mut self, volume: f64) {
debug_assert!(volume <= 1.0 && volume >= 0.0);
self.1 = volume;
}
#[inline(always)]
pub fn next(&mut self) -> f64 {
self.2 += self.0;
if self.2 >= 1.0 {
self.2 -= 1.0;
}
self.2
}
}
#[inline(always)] pub fn out(sample: f64) -> i16 {
(sample * (::std::i16::MAX as f64)) as i16
}
#[inline(always)] pub fn ovr(x: f64, generator: &mut FnMut(f64) -> f64,
overtones: &[f64]) -> f64
{
let mut o = generator(x);
let mut v = 1.0;
let mut d = 1.0;
for i in overtones {
d += 1.0;
v += i;
o += generator((x * d) % 1.0) * i;
}
o / v
}
#[inline(always)] pub fn saw(x: f64) -> f64 {
x * 2.0 - 1.0
}
#[inline(always)] pub fn sin(x: f64) -> f64 {
(x * (::std::f64::consts::PI * 2.0)).sin()
}
#[inline(always)] pub fn tri(x: f64) -> f64 {
(x * 2.0 - 1.0).abs() * 2.0 - 1.0
}
#[inline(always)] pub fn sqr(x: f64) -> f64 {
(x * 2.0 - 1.0).signum()
}
#[inline(always)] pub fn wht(white: &mut WhtGenerator) -> f64 {
white.gen()
}
#[inline(always)] pub fn pnk(pink: &mut PnkGenerator) -> f64 {
pink.gen()
}
#[inline(always)] pub fn mix(a: &[f64]) -> f64 {
add(a) / (a.len() as f64)
}
#[inline(always)] pub fn add(a: &[f64]) -> f64 {
let mut v = a[0];
for i in a.iter().skip(1) {
v += i;
}
v
}
#[inline(always)] pub fn mul(a: &[f64]) -> f64 {
let mut v = a[0];
for i in a.iter().skip(1) {
if i < &0.0 && v < 0.0 {
v *= -i;
} else {
v *= i;
}
}
v
}
#[inline(always)] pub fn inv(x: f64) -> f64 {
-x
}
#[inline(always)] pub fn hrd(a: f64, volume: f64) -> f64 {
(a * volume).min(1.0).max(-1.0)
}
#[inline(always)] pub fn sft(a: f64, volume: f64) -> f64 {
let max = (1.0 / (1.0 + (-volume).exp()) ) * 2.0 - 1.0;
((1.0 / (1.0 + (a * -volume).exp()) ) * 2.0 - 1.0) / max
}
#[inline(always)] pub fn srt(a: f64) -> f64 {
a.sqrt()
}
#[inline(always)] pub fn crt(a: f64) -> f64 {
a.cbrt()
}
#[inline(always)] pub fn asn(a: f64) -> f64 {
a.asin() * 2.0 / ::std::f64::consts::PI
}
#[inline(always)] pub fn acs(a: f64) -> f64 {
(a.acos() / ::std::f64::consts::PI) * 2.0 - 1.0
}
#[inline(always)] pub fn atn(a: f64) -> f64 {
a.atan() * 2.0 / ::std::f64::consts::PI
}
#[inline(always)] pub fn sgn(a: f64) -> f64 {
a.signum()
}
#[inline(always)] pub fn dst(a: f64, discrete_steps: u16) -> f64 {
(((a*0.5+0.5)*discrete_steps as f64).round()/discrete_steps as f64)*2.0
- 1.0
}
#[inline(always)] pub fn pos(a: f64) -> f64 {
if a > 0.0 { a } else { 0.0 }
}
#[inline(always)] pub fn neg(a: f64) -> f64 {
if a < 0.0 { a } else { 0.0 }
}