-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound_FM_unit.py
130 lines (86 loc) · 2.51 KB
/
sound_FM_unit.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
import numpy as np
from sound_FM_envelope import set_FME
# UNIT-TONE
def FM_unitT(sound_a,duration,note,ratio,feedback,sampling):
length_of_s = int(duration)
s = np.zeros(length_of_s)
f0 = sound_a * np.power(2, note / 12)
fm = f0*ratio
sb = 0
for n in range(length_of_s):
s[n] = fm / sampling * n + sb * feedback
sb = np.sin(2 * np.pi * s[n])
return s
# UNIT-FREQ
def FM_unitF(sound_a,duration,f,ratio,feedback,sampling):
length_of_s = int(duration)
s = np.zeros(length_of_s)
f0 = f
fm = f0*ratio
sb = 0
for n in range(length_of_s):
s[n] = fm / sampling * n + sb * feedback
sb = np.sin(2 * np.pi * s[n])
return s
# UNIT-SOUND
def FM_sound(sp):
length_of_s = len(sp)
s = np.zeros(length_of_s)
for n in range(length_of_s):
s[n] = np.sin(2 * np.pi * sp[n])
so = set_FME(s)
return so
# MIX
def FM_MIX(s1,s2,ratio):
length_of_s = len(s1)
s = np.zeros(length_of_s)
for n in range(length_of_s):
s[n] = np.sin(2 * np.pi * s1[n])*ratio + np.sin(2 * np.pi * s2[n])*(1.0 - ratio)
return s
# MIX
def FM_ADD(s1,s2,ratio):
length_of_s = len(s1)
s = np.zeros(length_of_s)
for n in range(length_of_s):
s[n] = s1[n]*ratio + s2[n]*(1.0 - ratio)
return s
# MODULATE
def FM_MODULATE(s1,md,power,feedback):
width = len(s1)
length_of_s = len(md)
s = np.zeros(length_of_s)
fb = 0
idx = 0
for n in range(length_of_s):
s[n] = fb*feedback
i1 = int(idx)
i2 = i1 + 1
f2 = idx - i1
if 0<= i1 and i1<width :
s[n] = s[n] + s1[i1]*(1.0-f2)
if 0<= i2 and i2<width :
s[n] = s[n] + s1[i2]*f2
sb = np.sin(2 * np.pi * s[n] )
st = md[n]
idx = idx + (1 + st)*power
return s
# MODULATE
def FM_MODULATE_S(s1,s2,power,feedback):
width = len(s1)
length_of_s = len(s2)
s = np.zeros(length_of_s)
fb = 0
idx = 0
for n in range(length_of_s):
s[n] = fb*feedback
i1 = int(idx)
i2 = i1 + 1
f2 = idx - i1
if 0<= i1 and i1<width :
s[n] = s[n] + s1[i1]*(1.0-f2)
if 0<= i2 and i2<width :
s[n] = s[n] + s1[i2]*f2
sb = np.sin(2 * np.pi * s[n] )
st = np.sin(2 * np.pi * s2[n])
idx = idx + (1 + st)*power
return s