mpv: sane-sysvol: integrate with uosc mute button

This commit is contained in:
Colin 2024-04-09 08:04:29 +00:00
parent c897f4fa4b
commit 86b495cb9f
2 changed files with 92 additions and 22 deletions

View File

@ -99,14 +99,23 @@ let
substituteInPlace src/uosc/main.lua \
--replace-fail \
"mp.observe_property('volume'" \
"mp.observe_property('user-data/sane-sysvol/volume'"
"mp.observe_property('user-data/sane-sysvol/volume'" \
--replace-fail \
"mp.observe_property('mute'" \
"mp.observe_property('user-data/sane-sysvol/mute'"
substituteInPlace src/uosc/elements/Volume.lua \
--replace-fail \
"mp.commandv('set', 'volume'" \
"mp.set_property_native('user-data/sane-sysvol/volume'" \
"mp.set_property_number('user-data/sane-sysvol/volume'" \
--replace-fail \
"mp.set_property_native('volume'" \
"mp.set_property_native('user-data/sane-sysvol/volume'"
"mp.set_property_number('user-data/sane-sysvol/volume'" \
--replace-fail \
"mp.set_property_native('mute'" \
"mp.set_property_bool('user-data/sane-sysvol/mute'" \
--replace-fail \
"mp.commandv('cycle', 'mute')" \
"mp.set_property_bool('user-data/sane-sysvol/mute', not mp.get_property_bool('user-data/sane-sysvol/mute'))"
'';
});
mpv-unwrapped = pkgs.mpv-unwrapped.overrideAttrs (upstream: {

View File

@ -40,6 +40,7 @@ function sysvol_new()
-- sysvol is pipewire-native volume
-- it's the cube of the equivalent 0-100% value represented inside mpv
sysvol = nil,
sysmute = nil,
change_sysvol = function(self, mpv_vol)
-- called when mpv wants to set the system-wide volume
if mpv_vol == nil then
@ -50,12 +51,16 @@ function sysvol_new()
if self.sysvol ~= nil then
old_mpv_vol = 100 * self.sysvol^(1/3)
end
if old_mpv_vol ~= nil and math.floor(old_mpv_vol) == math.floor(mpv_vol) then
if old_mpv_vol ~= nil and math.abs(mpv_vol - old_mpv_vol) < 1.0 then
-- avoid near-infinite loop where we react to our own volume change.
-- consider that we might be a couple messages behind in parsing pipewire when we issue this command,
-- hence a check on only the pipewire -> mpv side wouldn't prevent oscillation
msg.debug("NOT setting system-wide volume:", old_mpv_vol, volstr)
return
end
local volstr = tostring(mpv_vol) .. "%"
msg.debug("setting system-wide volume:", volstr)
msg.debug("setting system-wide volume:", old_mpv_vol, volstr)
self.sysvol = (0.01*mpv_vol)^3
subprocess({
"wpctl",
@ -65,10 +70,11 @@ function sysvol_new()
})
end,
on_sysvol_change = function(self, sysvol)
-- called when the pipewire system volume is changed (either by us, or an external application)
if sysvol == nil then
return
end
-- called when the pipewire system volume is changed (either by us, or an external application)
local new_mpv_vol = 100 * sysvol^(1/3)
local old_mpv_vol = nil
if self.sysvol ~= nil then
@ -76,13 +82,47 @@ function sysvol_new()
end
if old_mpv_vol ~= nil and math.abs(new_mpv_vol - old_mpv_vol) < 1.0 then
-- avoid an infinite loop where we react to our own volume change
msg.debug("NOT announcing volume change to mpv (because it was what triggered the change):", old_mpv_vol, new_mpv_vol)
return
end
self.sysvol = sysvol
msg.debug("announcing volume change to mpv:", old_mpv_vol, new_mpv_vol)
mp.set_property_native("user-data/sane-sysvol/volume", new_mpv_vol)
self.sysvol = sysvol
mp.set_property_number("user-data/sane-sysvol/volume", new_mpv_vol)
end,
change_sysmute = function(self, mute)
if mute == nil then
return
end
if mute == self.sysmute then
msg.debug("NOT setting system-wide mute (because it didn't change)", mute)
return
end
local mutestr
if mute then
mutestr = "1"
else
mutestr = "0"
end
msg.debug("setting system-wide mute:", mutestr)
self.sysmute = mute
subprocess({
"wpctl",
"set-mute",
"@DEFAULT_AUDIO_SINK@",
mutestr
})
end,
on_sysmute_change = function(self, mute)
if mute == nil then
return
end
msg.debug("announcing mute to mpv:", mute)
self.sysmute = mute
mp.set_property_bool("user-data/sane-sysvol/mute", mute)
end
}
end
@ -146,13 +186,13 @@ function pwmon_parser_new()
msg.debug("mute:", mute)
self.mute = mute
end,
get_effective_volume = function(self)
if self.mute then
return 0
else
return self.volume
end
end
-- get_effective_volume = function(self)
-- if self.mute then
-- return 0
-- else
-- return self.volume
-- end
-- end
}
end
@ -171,14 +211,20 @@ function pwmon_new()
msg.debug("pw-mon unexpectedly closed!")
end
if buf ~= nil then
local old_vol = self.pwmon_parser:get_effective_volume()
local old_vol = self.pwmon_parser.volume
local old_mute = self.pwmon_parser.mute
self.stdout_unparsed = self.stdout_unparsed .. buf
self:consume_stdout()
local new_vol = self.pwmon_parser:get_effective_volume()
local new_vol = self.pwmon_parser.volume
local new_mute = self.pwmon_parser.mute
if new_vol ~= old_vol then
msg.debug("pipewire volume change:", old_vol, new_vol)
mp.set_property_native("user-data/sane-sysvol/pw-mon-volume", new_vol)
mp.set_property_number("user-data/sane-sysvol/pw-mon-volume", new_vol)
end
if new_mute ~= old_mute then
msg.debug("pipewire mute change:", old_mute, new_mute)
mp.set_property_bool("user-data/sane-sysvol/pw-mon-mute", new_mute)
end
end
end,
@ -196,23 +242,38 @@ function pwmon_new()
}
end
mp.set_property_native("user-data/sane-sysvol/volume", 0)
mp.set_property_number("user-data/sane-sysvol/volume", 0)
mp.set_property_bool("user-data/sane-sysvol/mute", true)
local sysvol = sysvol_new()
local first_announcement = true
local first_sysvol_announcement = true
mp.observe_property("user-data/sane-sysvol/volume", "native", function(_, val)
-- we must set the volume property early -- before we actually know the volume
-- else other modules will think it's `nil` and error.
-- but we DON'T want the value we set to actually impact the system volume
if not first_announcement then
if not first_sysvol_announcement then
sysvol:change_sysvol(val)
end
first_announcement = false
first_sysvol_announcement = false
end)
mp.observe_property("user-data/sane-sysvol/pw-mon-volume", "native", function(_, val)
sysvol:on_sysvol_change(val)
end)
local first_sysmute_announcement = true
mp.observe_property("user-data/sane-sysvol/mute", "native", function(_, val)
-- we must set the mute property early -- before we actually know the mute
-- else other modules will think it's `nil` and error.
-- but we DON'T want the value we set to actually impact the system mute
if not first_sysmute_announcement then
sysvol:change_sysmute(val)
end
first_sysmute_announcement = false
end)
mp.observe_property("user-data/sane-sysvol/pw-mon-mute", "native", function(_, val)
sysvol:on_sysmute_change(val)
end)
local pwmon = pwmon_new()
mp.register_event('tick', function() pwmon:service() end)