-
Notifications
You must be signed in to change notification settings - Fork 2
/
mic_control.py
40 lines (33 loc) · 1.28 KB
/
mic_control.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
import subprocess
import sys
if sys.platform == "win32":
#in order to run this use
#pip install pywin32
#only works on windows using the Win32 api
import win32api
import win32gui
WM_APPCOMMAND = 0x319
APPCOMMAND_MICROPHONE_VOLUME_MUTE = 0x180000
def mic_mute_toggle():
hwnd_active = win32gui.GetForegroundWindow()
win32api.SendMessage(hwnd_active, WM_APPCOMMAND, None, APPCOMMAND_MICROPHONE_VOLUME_MUTE)
elif sys.platform == "darwin":
SET_VOLUME_COMMAND = "set volume input volume {value}"
GET_INPUT_VOL_COMMAND = "input volume of (get volume settings)"
DEFAULT_VOLUME = 75
def execute_apple_script(command):
result = subprocess.run(["osascript", "-e", command], stdout=subprocess.PIPE)
output = result.stdout.decode("utf-8")
result = output.strip()
try:
return int(result)
except ValueError:
return 0
def mic_mute_toggle():
volume = execute_apple_script(GET_INPUT_VOL_COMMAND)
if (volume != 0):
execute_apple_script(SET_VOLUME_COMMAND.format(value = 0))
else:
execute_apple_script(SET_VOLUME_COMMAND.format(value = DEFAULT_VOLUME))
else:
raise Exception("bad platform: not supported, this application only works on Windows and Mac")