-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
179 lines (108 loc) · 4.14 KB
/
sample.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
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
import math
import os
from audioop import add, mul
import pygame
import numpy as np
from scipy.io.wavfile import read, write
class Sample:
def __init__(self, name):
self.name = name
self.filepath = 'static/samples/' + name + '.wav'
self.outpath = 'static/samples/out_' + name + '.wav'
self.rate, _ = read( self.filepath )
pygame.mixer.pre_init(44100, 16, 2, 4096)
pygame.mixer.init()
self.effect_mapping = {
'DE': self.delay,
'FL': self.flanger,
'PH': self.phaser,
'TR': self.tremolo,
'DI': self.distortion,
}
def play( self, effects_list ):
# pygame.mixer.Sound( self.outpath ).stop()
# producing outfile
self.apply_all_effects( effects_list )
# and then play
# print("Playing... ", self.filepath)
# self.blink()
# pygame.mixer.Sound( self.outpath ).play()
# myBuffer = memoryview( self.transformed_bytes )
'''======= EFFECTS ======='''
def delay( self, x, params ):
feedback = int(params['factor']) / 100
delaytime = int(params['time'])
# someday...
# num = params['num']
offset = int( self.rate / 1000 ) * delaytime * 2
y = np.array( x )
for i in range( offset, len(x) ):
if (i-offset)>0:
y[i] = x[i] + (feedback) * (x[ i-offset ])
return y
def phaser( self, x, params ):
rnge = int(params['rnge'])
sweep = float(params['sweep'])
y = np.zeros( len(x) )
for i in range( len(x) - rnge ):
y[i] = x[i] + x[i + round(rnge * math.sin( (2*math.pi*i*sweep)/ self.rate ))]
return y
def flanger( self, x, params ):
sweep_range = int(params['sweep_range'])
sweep_freq = float(params['sweep_freq'])
given_delay = 15
y = np.zeros( len(x) )
for i in range( given_delay+sweep_range, len(x) ):
y[i] = x[i] + x[ i - given_delay - round(
sweep_range * math.sin(2*math.pi*i*sweep_freq/self.rate)) ]
return y
def distortion( self, x, params ):
clipVal = float(params['clipVal']) / 100
y = np.zeros( len(x) )
for i in range(len(x)):
if x[i] > clipVal:
y[i] = clipVal
elif x[i] < -clipVal:
y[i] = -clipVal
else:
y[i] = x[i]
return y
def tremolo( self, x, params ):
freq = int(params['freq'])
y = np.zeros( len(x) )
for i in range( len(x) ):
y[i] = math.sin( 2*np.pi*freq/ self.rate *i ) * x[i]
# print(freq, self.rate)
# print(y)
return y
'''======= APPLYING ======='''
def apply_effect( self, audio, effect_code, params ):
# print('Len before:', len(audio[:-5]))
# print(audio[:-5])
effect = self.effect_mapping[ effect_code ]
bag = []
for i in range(2):
channel = [p[i] for p in audio]
print(i+1, f'channel (len={len(channel)}):', channel[:5])
applied = effect( channel, params )
bag.append(applied)
merged = np.array([
[ lt, rt ] for lt, rt in zip(bag[0], bag[1])
])
# print('\nmerged\n', merged[:-5])
# print('Len after:', len(merged))
return merged
def apply_all_effects( self, effects_list ):
# Duplicate
os.system( f'cp {self.filepath} {self.outpath}' )
divided = False
for effect in effects_list:
_, audio = read( self.outpath )
if not divided:
audio = audio / 32767
divided = True
audio = self.apply_effect( audio, effect['code'], effect['params'] )
print('Applied', effect['code'], effect['id'], 'with params:', effect['params'])
# if len(audio) != 0:
write( self.outpath, self.rate, audio )
print( 'Saved to:', self.outpath )