view collector/volume_linux.py @ 10:6b6b1ea0e00d default tip

ok for no window to be focused (untested)
author drewp@bigasterisk.com
date Mon, 13 Mar 2023 19:16:58 -0700
parents 5ad799e534a5
children
line wrap: on
line source

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)