-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_color.py
140 lines (82 loc) · 2.73 KB
/
sound_color.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
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
import numpy as np
def sin_out(sound_a,duration,note,pow,sampling):
length_of_s = int(duration)
s = np.zeros(length_of_s)
f0 = sound_a * np.power(2, note / 12)
T = 1 / f0
for n in range(length_of_s):
s[n] = pow * np.sin(2 * np.pi * f0/sampling*n)
return s
def square_out(sound_a,duration,note,pow,sampling):
length_of_s = int(duration)
s = np.zeros(length_of_s)
f0 = sound_a * np.power(2, note / 12)
T = 1 / f0
for n in range(length_of_s):
sw = np.sin(2 * np.pi * f0/sampling*n)
if sw > 0 : s[n] = pow
if sw < 0 : s[n] = - pow
return s
def pulse_out(sound_a,duration,note,pow,sampling):
length_of_s = int(duration)
s = np.zeros(length_of_s)
f0 = sound_a * np.power(2, note / 12)
T = 1 / f0
for n in range(length_of_s):
sw = np.sin(2 * np.pi * f0/sampling*n)
if sw > 0.99 : s[n] = pow
if sw < -0.99 : s[n] = - pow
return s
def sawtooth_out(sound_a,duration,note,pow,sampling):
length_of_s = int(duration)
s = np.zeros(length_of_s)
f0 = sound_a * np.power(2, note / 12)
T = 1 / f0
for n in range(length_of_s):
saw = (f0/sampling*n)
saw -= int(saw)
s[n] = saw - 0.5
return s
def sin_decay(sound_a,duration,note,pow,sampling,decay):
length_of_s = int(duration)
s = np.zeros(length_of_s)
f0 = sound_a * np.power(2, note / 12)
T = 1 / f0
for n in range(length_of_s):
dec = np.power(np.e , - n * decay / duration )
s[n] = pow * dec * np.sin(2 * np.pi * f0/sampling*n)
return s
def white_noise(duration) :
s = np.zeros(duration)
mean = 0
for n in range(duration) :
s[n] = ( np.random.rand()*2 ) - 1
mean += s[n]
mean /= duration
for n in range(duration) :
s[n] -= mean
return s
def sound_string(sound_a,duration,note,sampling) :
f_c = 0.985
f_d = 0.7
f0 = sound_a * np.power(2,note/12)
T = 1/f0
D = int(T*sampling)
s0 = white_noise(D+1)
length_of_s = int(duration)
s = np.zeros(length_of_s)
for n in range(D+1):
s[n] = s0[n]
for n in range(D+1,length_of_s):
s[n] = f_c*(f_d*s[n-D] + (1-f_d)*s[n-D-1])
return s
def FM_out(sound_a,duration,note,pw,ratio,pw_m,sampling):
length_of_s = int(duration)
s = np.zeros(length_of_s)
f0 = sound_a * np.power(2, note / 12)
fm = f0*ratio
for n in range(length_of_s):
md = np.sin(2 * np.pi * fm / sampling * n)
cr = np.sin(2 * np.pi * f0 / sampling * n)
s[n] = pw * ( cr + pw_m * md )
return s