-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport_configurer.py
82 lines (68 loc) · 2.73 KB
/
port_configurer.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
import jack, time, sys
def get_ports_containing(client, string):
ret_ports = []
try:
for port in client.get_ports():
if string.lower() in port.name.lower():
ret_ports.append(port)
except jack.JackError:
return []
return ret_ports
def attempt_disconnect(client, port1, port2, debug=False):
try:
client.disconnect(port1, port2)
return True
except:
if debug:
print("Port " + port1.name + " and port " + port2.name + " have no connection")
return False
def attempt_connect(client, port1, port2, debug=False):
try:
client.connect(port1, port2)
return True
except:
if debug:
print("Port " + port1.name + " and port " + port2.name + " already connected")
return False
def alsa_can_connect(client):
alsa_ports = get_ports_containing(client, 'alsa-jack')
if len(alsa_ports) < 2:
return False
sink_ports = get_ports_containing(client, 'sink')
try:
port_connectable = attempt_connect(client, sink_ports[0], alsa_ports[0])
except IndexError:
return False
return port_connectable
def reconfigure_port_connections(client):
alsa_ports = get_ports_containing(client, 'alsa-jack')
sink_ports = get_ports_containing(client, 'sink')
source_ports = get_ports_containing(client, 'source')
capture_ports = get_ports_containing(client, 'capture')
playback_ports = get_ports_containing(client, 'playback')
attempt_disconnect(client, capture_ports[0], source_ports[0])
attempt_disconnect(client, capture_ports[1], source_ports[1])
attempt_disconnect(client, sink_ports[0], playback_ports[0])
attempt_disconnect(client, sink_ports[1], playback_ports[1])
attempt_disconnect(client, capture_ports[0], alsa_ports[0])
attempt_disconnect(client, capture_ports[1], alsa_ports[1])
attempt_connect(client, sink_ports[0], source_ports[0])
attempt_connect(client, sink_ports[1], source_ports[1])
attempt_connect(client, sink_ports[0], alsa_ports[0])
attempt_connect(client, sink_ports[1], alsa_ports[1])
#Check for record.py also recording
if len(alsa_ports) > 2:
attempt_disconnect(client, capture_ports[0], alsa_ports[2])
attempt_disconnect(client, capture_ports[1], alsa_ports[3])
attempt_connect(client, sink_ports[0], alsa_ports[2])
attempt_connect(client, sink_ports[1], alsa_ports[3])
def main(argv):
client = jack.Client('JackClient')
client.activate()
reconfigurations_needed = False
#Watch for port to open
while True:
if alsa_can_connect(client):
reconfigure_port_connections(client)
if __name__ == "__main__":
main(sys.argv)