-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacmd.py
44 lines (31 loc) · 1.35 KB
/
pacmd.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
import subprocess
def get_sinks(card: str = None) -> [str]:
# find all audio sinks
sinks = []
stdout = subprocess.check_output("pacmd list-sinks", shell=True).decode()
currentSink = None
for _, line in enumerate(stdout.splitlines()):
# looking for "name: <SINK>"
if "name: <" in line and line.endswith(">"):
currentSink = line[line.index("<") + 1:-1]
# looking for "card: \d* <CARD>"
if "card: " in line and line.endswith(">"):
currentCard = line[line.index("<") + 1:-1]
sinks.append((currentSink, currentCard))
if card is not None:
filtered_sinks = [(s, c) for s, c in sinks if c == card]
assert (len(filtered_sinks) > 0), f"audio card \"{card}\" not found: {sinks}"
sinks = filtered_sinks
return sinks
def get_current() -> str:
sink = None
stdout = subprocess.check_output("pacmd stat", shell=True).decode()
for _, line in enumerate(stdout.splitlines()):
# looking for "Default sink name: SINK"
if "Default sink name:" in line:
sink = line[line.index(":") + 2:]
card = next(c for s, c in get_sinks() if s == sink)
return (sink, card)
def set_default_sink(sink: str):
cmd = " ".join(["pacmd", f"set-default-sink {sink}"])
subprocess.run(cmd, shell=True)