apply-routes.lua: Add a mechanism for per-device default volumes

This allows us to have per-device configuration for device volumes, via
a `device.routes.default-volume` property. This allows a non-global
override for situations where we want to expose a different out-of-box
volume (say for an internal speaker where we know a better comfortable
default setting, or HDMI where we might want to avoid attenuation by
default).
This commit is contained in:
Arun Raghavan 2025-12-15 16:25:25 -08:00
parent 80478e7548
commit 080ebc5e17
2 changed files with 16 additions and 3 deletions

View file

@ -43,6 +43,10 @@ previous section: :ref:`config_configuration_option_types`.
device route (e.g. ALSA PCM sinks). This is used when the route is restored device route (e.g. ALSA PCM sinks). This is used when the route is restored
and the sink does not have a previously stored volume. and the sink does not have a previously stored volume.
It is possible to override the value on a per-device basis with a property
(*not* a setting, so this would go into a configuration file) on the device
named ``device.routes.default-sink-volume``.
:Default value: ``0.4 ^ 3`` (40% on the cubic scale) :Default value: ``0.4 ^ 3`` (40% on the cubic scale)
.. describe:: device.routes.default-source-volume .. describe:: device.routes.default-source-volume
@ -51,6 +55,10 @@ previous section: :ref:`config_configuration_option_types`.
device route (e.g. ALSA PCM sources). This is used when the route is restored device route (e.g. ALSA PCM sources). This is used when the route is restored
and the source does not have a previously stored volume. and the source does not have a previously stored volume.
It is possible to override the value on a per-device basis with a property
(*not* a setting, so this would go into a configuration file) on the device
named ``device.routes.default-source-volume``.
:Default value: ``1.0`` (100%) :Default value: ``1.0`` (100%)
.. describe:: linking.allow-moving-streams .. describe:: linking.allow-moving-streams

View file

@ -55,9 +55,14 @@ AsyncEventHook {
-- ensure default values -- ensure default values
local is_input = (route_info.direction == "Input") local is_input = (route_info.direction == "Input")
props.mute = props.mute or false props.mute = props.mute or false
props.channelVolumes = props.channelVolumes or props.channelVolumes = props.channelVolumes or {
{ is_input and Settings.get_float ("device.routes.default-source-volume") -- See if we have a per-device override
or Settings.get_float ("device.routes.default-sink-volume") } (is_input and tonumber(device.properties["device.routes.default-source-volume"]))
or tonumber(device.properties["device.routes.default-sink-volume"])
-- Otherwise we use the global default
or (is_input and Settings.get_float ("device.routes.default-source-volume"))
or Settings.get_float ("device.routes.default-sink-volume")
}
-- prefix the props with correct IDs to create a Pod.Object -- prefix the props with correct IDs to create a Pod.Object
table.insert (props, 1, "Spa:Pod:Object:Param:Props") table.insert (props, 1, "Spa:Pod:Object:Param:Props")