diff collector/volume_linux.py @ 4:5ad799e534a5

move to subdir
author drewp@bigasterisk.com
date Mon, 13 Mar 2023 18:50:23 -0700
parents volume_linux.py@04c8a1b3976b
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/collector/volume_linux.py	Mon Mar 13 18:50:23 2023 -0700
@@ -0,0 +1,30 @@
+from pulsectl_asyncio import PulseAsync
+
+_pulse = None
+
+
+async def _init():
+    global _pulse
+    if _pulse is not None:
+        return
+    _pulse = PulseAsync('racc')
+    await _pulse.connect()
+
+
+async def _get_sink():
+    [sink] = [
+        s for s in (await _pulse.sink_list())
+        if s.description == 'Built-in Audio Analog Stereo'
+    ]
+    return sink
+
+
+async def get_volume():
+    await _init()
+    return (await _get_sink()).volume.value_flat
+
+
+async def set_volume(v: float):
+    await _init()
+    sync = await _get_sink()
+    await _pulse.volume_set_all_chans(sink, v)