-
Notifications
You must be signed in to change notification settings - Fork 2
/
micmute_listener_pa.py
executable file
·73 lines (56 loc) · 2.08 KB
/
micmute_listener_pa.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
#!/bin/python3
import pulsectl
import os
import sys
import signal
pulse = pulsectl.Pulse('micmute-listener')
# TODO: Read this from an external config file
mic_name = 'alsa_input.pci-0000_04_00.6.analog-stereo'
active_index = 1 # This is the index of the source
pulse_event = None
def shutdown(signum, frame):
print('Shutting down client...')
pulse.disconnect()
quit(0)
def find_mic_index():
global active_index
for source in pulse.source_list():
if(source.name == mic_name):
active_index = int(source.index)
print('Mic index set: %d' % (active_index))
return
raise RuntimeError('Could not find a device corresponding to %s!' % (mic_name))
def catch_events(ev):
print('Pulse event:', ev)
global pulse_event
pulse_event = ev
### Raise PulseLoopStop for event_listen() to return before timeout (if any)
raise pulsectl.PulseLoopStop
def set_led_value(value):
os.system("/usr/local/bin/set_micmute_led " + str(value))
def init():
pulse.connect(wait= True)
#print('Event types:', pulsectl.PulseEventTypeEnum)
#print('Event facilities:', pulsectl.PulseEventFacilityEnum)
#print('Event masks:', pulsectl.PulseEventMaskEnum)
print('Source list:', pulse.source_list())
find_mic_index()
init_led_value = 0 if pulse.source_info(active_index).mute == 1 else 1
set_led_value(init_led_value)
pulse.event_mask_set('all')
pulse.event_callback_set(catch_events)
if __name__ == '__main__':
print('Starting the micmute listener...')
signal.signal(signal.SIGINT, shutdown)
init()
while True:
try:
pulse.event_listen()
except pulsectl.pulsectl.PulseDisconnected:
# TODO: check the docu, maybe this isn't the best way to restart the connection
print('PulseAudio disconnected! Attempting to reconnect...', file=sys.stderr)
init()
continue
if pulse_event.index == active_index:
led_value = 0 if pulse.source_info(active_index).mute == 1 else 1
set_led_value(led_value)