Manage volume using PulseVolumeInfo
masterIn PulseAudio, volume is not a single number but a list of numbers (one per channel). In pulsectl, these are represented by PulseVolumeInfo objects.
Key Concepts
- Per-channel values: A list of floats representing volume for each channel (e.g., left, right).
value_flat: A property onPulseVolumeInfothat provides the average level across all channels.- Volume Range:
0.0: Muted (PA_VOLUME_MUTED).1.0: Normal volume (PA_VOLUME_NORM).> 1.0: Software-boosted volume (up to65536.0). It is recommended to keep values in the0.0to1.0range and adjust hardware volume instead to avoid quality loss.
Usage Patterns
- Direct channel control: Create a
PulseVolumeInfofrom a list of channel levels:PulseVolumeInfo([0.15, 0.25]). - Uniform channel control: Create a
PulseVolumeInfowith a single value applied to all channels:PulseVolumeInfo(0.5, n_channels). - Convenience methods: Use
pulse.volume_set_all_chans(object, value)to quickly set the volume of an object to a single numeric value across all channels without manually constructing aPulseVolumeInfoobject.
from pulsectl import Pulse, PulseVolumeInfo
import time
with Pulse('volume-example') as pulse:
sink_input = pulse.sink_input_list()[0]
# Accessing volume
volume = sink_input.volume
print(volume.values) # list of per-channel values (floats)
print(volume.value_flat) # average level across channels (float)
# Setting volume via flat value
volume.value_flat = 0.3
pulse.volume_set(sink_input, volume)
# Setting volume via PulseVolumeInfo
n_channels = len(volume.values)
new_volume = PulseVolumeInfo(0.5, n_channels)
pulse.volume_set(sink_input, new_volume)
# Most common way for simple tasks:
pulse.volume_set_all_chans(sink_input, 0.2)