-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-waves.py
72 lines (56 loc) · 1.8 KB
/
make-waves.py
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
import random
random.seed(1982)
def lerp(x, v1, v2):
return (1 - x) * v1 + x * v2
class pelgrin:
def __init__(self, num_samples):
self.n = num_samples
self.rnd_vals = [2 * (random.random() - 0.5) for x in range(self.n)]
self.rnd_vals[0] = 0 # Don't distort the boundary values
self.rnd_vals.append(self.rnd_vals[0]) # Avoid index modulus calculation
def _add_noise(self, samples, step, mul, start_index):
for i in range(self.n):
i0 = i - (i % step)
if (self.n - i) <= step:
if step > 1:
v = (i - i0) / (step - 1)
else:
v = 1
else:
v = (i - i0) / step
delta = lerp(v, self.rnd_vals[i0], self.rnd_vals[i0 + step])
samples[i + start_index] += mul * delta
def add_noise(self, samples, mul = 1, start_index = 0):
step = self.n
while 1:
step >>= 1
mul *= 0.5
if step == 0:
break
self._add_noise(samples, step, mul, start_index)
def output_samples(samples):
n = 0
for v in samples:
print("%4d, " % (v), end='')
n += 1
if n % 16 == 0:
print()
def scale(samples):
mn = min(samples)
mx = max(samples)
return [int(lerp((x - mn) / (mx - mn), -128, 127)) for x in samples]
def make_noise_wave():
n = 256
w = []
w.extend([x for x in range(-128, 127)])
w.extend([x for x in range(127, -128, -1)])
w.append(-128) # Extra sample so that pelgrin noise can be added to second half
mul = 128
pelgrin(n).add_noise(w, mul)
pelgrin(n).add_noise(w, mul, n - 1)
w = scale(w)
output_samples(w[:n-1])
print()
output_samples(w[n-1:])
return w
make_noise_wave()