-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__main__.py
321 lines (280 loc) · 10.5 KB
/
__main__.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/python
GPIO_CAPABLE = False
import time
from functools import partial
import signal
import sys
import pyo
try:
import RPi.GPIO as GPIO
GPIO_CAPABLE = True
except ImportError:
pass
if GPIO_CAPABLE:
import gpiocontrol
import bridge
import configparser
import jackserver
import flanger
import socket
SOCKET_TIMEOUT = 30 #seconds
button_pin = 17
PYO_INIT_SETTINGS = {
'audio':'jack',
'nchnls':1
}
def start_pyo_server():
"""Start the Pyo server
Return the pyo instance of the server
"""
print("Attempting to start the pyo server")
pyo_server = pyo.Server(**PYO_INIT_SETTINGS).boot()
print("Pyo server booted")
pyo_server.start()
print("Pyo server started")
return pyo_server
def stop_pyo_server(pyo_server):
"""Stop the Pyo server
"""
print("Attempting to stop the pyo server")
pyo_server.stop()
print("Pyo server stopped")
def chain_effects( initial_source, config_effects_dict ):
'''
Loop through the effects and assembles their configuration in order according to their keys.
initial_source - the medium by which the audio stream is read (i.e through the input port)
config_effects_dict - the list of effects to enable on top of the audio stream.
'''
vol = 1 #default volume
enabled_effects = [initial_source]
# Make the source of the next effect the previously applied effect.
source = enabled_effects[len(enabled_effects) - 1]
# If the volume was set, change the default value to the requested volume.
if "volume" in config_effects_dict:
vol = config_effects_dict.pop("volume")
enabled_effects.append(pyo.Tone(
source,
freq = 20000,
mul = vol
)
)
# Run through all the effects in our configuration file and apply
# them to the previously used stream (i.e source)
for effect in sorted(config_effects_dict.keys()):
source = enabled_effects[len(enabled_effects) - 1]
# print("Effect: " + effect + ", Params: " + str(effects_dict[effect]))
params = config_effects_dict[effect]
if params['name'] == 'distortion':
# distortion stuff
print("Enable distortion effect")
enabled_effects.append(pyo.Disto(
source,
drive=float(params['drive']),
slope=float(params['slope']),
mul = vol
)
)
elif params['name'] == 'delay':
# delay stuff
print("Enable delay effect")
enabled_effects.append(pyo.Delay(
source,
delay=[0, float(params['delay'])],
feedback=float(params['feedback']),
maxdelay=5,
mul = vol
)
)
elif params['name'] == 'reverb':
# reverb stuff
print("Enable reverb effect")
enabled_effects.append(pyo.STRev(
source,
inpos=0.25,
revtime=float(params['revtime']),
cutoff=float(params['cutoff']),
bal=float(params['balance']),
roomSize=float(params['roomsize']),
mul = vol
)
)
elif params['name'] == 'chorus':
# chorus stuff
print("Enable chorus effect")
enabled_effects.append(pyo.Chorus(
source,
depth=[(params['depth_min']), (params['depth_max'])],
feedback=float(params['feedback']),
bal=float(params['balance']),
mul = vol
)
)
elif params['name'] == 'flanger':
# flanger stuff
print("Enable flanger effect")
enabled_effects.append(flanger.Flanger(
source,
depth=float(params['depth']),
freq=float(params['freq']),
feedback=float(params['feedback']),
mul = vol
)
)
elif params['name'] == 'freqshift':
# frequency shift stuff
print("Enable frequency shift effect")
enabled_effects.append(pyo.FreqShift(
source,
shift=params['shift'],
mul = vol
)
)
elif params['name'] == 'harmonizer':
# harmonizer stuff
print("Enable harmonizer effect")
enabled_effects.append(pyo.Harmonizer(
source,
transpo=params['transpose'],
feedback=float(params['feedback']),
winsize=0.1,
mul = vol
)
)
elif params['name'] == 'phaser':
# phaser stuff
print("Enable phaser effect")
enabled_effects.append(pyo.Phaser(
source,
freq=float(params['frequency']),
spread=float(params['spread']),
q=float(params['q']),
feedback=float(params['feedback']),
num=int(params['num']),
mul = vol
)
)
return enabled_effects
def apply_effects( effects_list ):
'''Once an effects list has been assembled by chain_effects, use this function to enable it'''
effects_list[len(effects_list) - 1].out()
print("APPLIED EFFECTS: ", effects_list)
def signal_handler(jack_id, pyo_server, signal, frame):
'''Close the program and kill JACK appropriately'''
stop_pyo_server(pyo_server)
time.sleep(1)
jackserver.kill_jack_server(jack_id)
sys.exit(0)
def main():
'''Main method. Inits gpio, bridge, jack, and pyo. Then reads effects and starts handling gpio'''
# If GPIO is enabled, initialize the pins and GPIO module.
if GPIO_CAPABLE:
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(button_pin, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(23, GPIO.OUT)
gpio_controller = gpiocontrol.GpioController()
# Initialize the bridge to allow the app to accept connections.
bridge_conn = bridge.Bridge()
# Set up custom options for the sockets
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #UDP SOCKET
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP SOCKET
s.setblocking(0)
sock.setblocking(0)
s.bind(('', 10000))
sock.bind(('', 10001))
jack_id = jackserver.start_jack_server()
# give the application time for JACK to boot.
time.sleep(5)
# JACK and Pyo set up procedures
pyo_server = start_pyo_server()
pyo_server.setJackAuto()
# Read input from the audio device on channel 0
# and apply the necessary effects from the config file
enabled_effects = chain_effects(pyo.Input(chnl=0), configparser.get_effects())
apply_effects( enabled_effects )
# Create necessary variables used by the GPIO controller module
record_table = []
audio_recorder = []
loop = []
record_table.append(pyo.NewTable(length=60, chnls=1, feedback=0.5))
audio_recorder.append(pyo.TableRec((enabled_effects[len(enabled_effects) - 1]), table=record_table[-1], fadetime=0.05))
already_recording = False
recording_time = 0
inactive_end_time = 0
signal.signal(signal.SIGINT, partial(signal_handler, jack_id, pyo_server))
while True:
# Executes GPIO and loop machine logic flow.
if GPIO_CAPABLE:
# Read the state of the button press.
BUTTON_STATE = gpio_controller.update_gpio()
# Perform actions dependent on the state of the button press.
if BUTTON_STATE == 'INACTIVE' or BUTTON_STATE == 'LOOPING':
inactive_end_time = time.time()
if BUTTON_STATE == 'RECORDING':
recording_time = time.time()
if not already_recording:
print("Recording audios for 5 segundos")
(audio_recorder[-1]).play()
already_recording = True
elif BUTTON_STATE == 'ACTIVATE_LOOP':
loop_len = recording_time - inactive_end_time
loop.append(
pyo.Looper(
table=record_table[-1],
dur=loop_len, xfade=0,
mul=1).out()
)
record_table.append(
pyo.NewTable(
length=60,
chnls=1,
feedback=0.5)
)
audio_recorder.append(
pyo.TableRec(
(enabled_effects[len(enabled_effects) - 1]),
table=record_table[-1],
fadetime=0.05)
)
print("ACTIVATING LOOP")
gpio_controller.set_state("LOOPING")
already_recording = False
elif BUTTON_STATE == 'CLEAR_LOOP':
loop = []
record_table = []
audio_recorder = []
record_table.append(
pyo.NewTable(
length=60,
chnls=1,
feedback=0.5)
)
audio_recorder.append(
pyo.TableRec(
(enabled_effects[len(enabled_effects) - 1]),
table=record_table[-1],
fadetime=0.05)
)
gpio_controller.set_state("INACTIVE")
# See if we got a message from the frontend application
res = bridge_conn.backend(s,sock)
if res:
print(res)
if 'UPDATEPORT' == res[0]:
# There was a request to update the ports. Kill the
# JACK server and restart it with the new ports.
print("Request to update ports")
pyo_server.shutdown()
jackserver.kill_jack_server(jack_id)
time.sleep(2)
jack_id = jackserver.start_jack_server(jackserver.filter_port_selection(res[1]), jackserver.filter_port_selection(res[2]))
time.sleep(2)
pyo_server.reinit(**PYO_INIT_SETTINGS)
pyo_server.boot()
pyo_server.start()
enabled_effects[-1].stop()
enabled_effects = chain_effects(pyo.Input(chnl=0), configparser.get_effects())
apply_effects( enabled_effects )
time.sleep(0.0001)
if __name__ == "__main__":
main()