-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_distortion.py
63 lines (45 loc) · 1.33 KB
/
sound_distortion.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
import numpy as np
from wave_file import wave_write_16bit_mono
from biquad_filter import LPF
from biquad_filter import filter
def distortion(sampling,gain,level,si) :
length_of_si = len(si)
so = np.zeros(length_of_si)
# LPF 2000Hz
fcut = 2000
quality = 1 / np.sqrt(2)
a,b = LPF(sampling,fcut,quality)
s0 = filter(a,b,si)
# 4x Up-Sampling
ratio = 4
s1 = np.zeros(length_of_si * ratio )
for n in range(length_of_si) :
a = s0[n]
b = 0
if ( n < length_of_si - 1 ) : b = s0[n+1]
for r in range(ratio) :
s1[(n-1)*ratio + r] = a + (b - a) * r / ratio;
# LPF 20000Hz
fcut = 20000 / ratio
quality = 1 / np.sqrt(2)
a,b = LPF(sampling,fcut,quality)
s2 = filter(a,b,s1)
# DISTORTION !!!
s3 = np.zeros(length_of_si * ratio )
for n in range(length_of_si*ratio) :
s3[n] = np.tanh( 5 * s2[n] * gain / 2 );
# LPF 20000Hz
fcut = 20000 / ratio
quality = 1 / np.sqrt(2)
a,b = LPF(sampling,fcut,quality)
s4 = filter(a,b,s3)
# 1/4 Down-Sampling
for n in range(length_of_si) :
sum = 0
for r in range(ratio) :
sum += s4[n*ratio + r];
so[n] = sum / ratio;
# level control
so /= np.max(np.abs(so))
so *= level
return so