-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume_control.py
More file actions
executable file
·58 lines (47 loc) · 1.49 KB
/
Copy pathvolume_control.py
File metadata and controls
executable file
·58 lines (47 loc) · 1.49 KB
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
#!/usr/bin/env python3
import subprocess
import sys
import executor
def get_active_sink():
return executor.run('pacmd list-sinks | grep "* index" | awk \'{print $3}\'')[0]
def get_volume():
return executor.run('amixer -D pulse get Master | grep -o "\[.*%\]" | grep -o "[0-9]*" | head -n1')[0]
def set_volume(percentage):
executor.run('pactl set-sink-volume ' + get_active_sink() + ' ' + str(percentage) + '%')
def toggle_volume():
executor.run('amixer -D pulse set Master Playback Switch toggle')
def is_muted():
return not executor.run('amixer -D pulse get Master | grep -o "\[on\]" | head -n1')[0]
def write(message):
sys.stdout.write(message)
sys.stdout.flush()
def trim_to_range(volume):
volume = int(volume)
if volume < 0:
volume = 0
elif volume > 200:
volume = 200
return volume
def status():
if int(get_volume()) == 0 or is_muted():
return 'muted'
else:
return 'on'
if __name__ == '__main__':
command = sys.argv[1]
if command == 'set':
set_volume(trim_to_range(sys.argv[2]))
elif command == 'up':
new_volume = trim_to_range(int(get_volume()) + int(sys.argv[2]))
set_volume(new_volume)
elif command == 'down':
new_volume = trim_to_range(int(get_volume()) - int(sys.argv[2]))
set_volume(new_volume)
elif command == 'toggle':
toggle_volume()
elif command == 'read':
write(get_volume())
elif command == 'status':
write(status())
else:
write('Usage: ' + sys.argv[0] + ' [set|up|down|toggle|read|status] [value]\n')