Wireplumber autoconnect speakers

This commit is contained in:
Christian Colglazier 2023-11-24 15:26:45 -05:00
parent f59c3a52c6
commit b889d0d667
2 changed files with 156 additions and 0 deletions

View File

@ -0,0 +1 @@
load_script("/home/aqua/.config/wireplumber/scripts/auto-connect-ports.lua")

View File

@ -0,0 +1,155 @@
-- As explained on: https://bennett.dev/auto-link-pipewire-ports-wireplumber/
--
-- This script keeps my stereo-null-sink connected to whatever output I'm currently using.
-- I do this so Pulseaudio (and Wine) always sees a stereo output plus I can swap the output
-- without needing to reconnect everything.
-- Link two ports together
function link_port(output_port, input_port)
if not input_port or not output_port then
return false
end
local link_args = {
["link.input.node"] = input_port.properties["node.id"],
["link.input.port"] = input_port.properties["object.id"],
["link.output.node"] = output_port.properties["node.id"],
["link.output.port"] = output_port.properties["object.id"],
-- The node never got created if it didn't have this field set to something
["object.id"] = nil,
-- I was running into issues when I didn't have this set
["object.linger"] = true,
["node.description"] = "Link created by auto_connect_ports"
}
local link = Link("link-factory", link_args)
link:activate(1)
return true
end
-- Automatically link ports together by their specific audio channels.
--
-- ┌──────────────────┐ ┌───────────────────┐
-- │ │ │ │
-- │ FL ├────────►│ AUX0 │
-- │ OUTPUT │ │ │
-- │ FR ├────────►│ AUX1 INPUT │
-- │ │ │ │
-- └──────────────────┘ │ AUX2 │
-- │ │
-- └───────────────────┘
--
-- -- Call this method inside a script in global scope
--
-- auto_connect_ports {
--
-- -- A constraint for all the required ports of the output device
-- output = Constraint { "node.name"}
--
-- -- A constraint for all the required ports of the input device
-- input = Constraint { .. }
--
-- -- A mapping of output audio channels to input audio channels
--
-- connections = {
-- ["FL"] = "AUX0"
-- ["FR"] = "AUX1"
-- }
--
-- }
--
function auto_connect_ports(args)
local output_om = ObjectManager {
Interest {
type = "port",
args["output"],
Constraint { "port.direction", "equals", "out" }
}
}
local input_om = ObjectManager {
Interest {
type = "port",
args["input"],
Constraint { "port.direction", "equals", "in" }
}
}
local all_links = ObjectManager {
Interest {
type = "link",
}
}
local unless = nil
if args["unless"] then
unless = ObjectManager {
Interest {
type = "port",
args["unless"],
Constraint { "port.direction", "equals", "in" }
}
}
end
function _connect()
local delete_links = unless and unless:get_n_objects() > 0
print("Delete links", delete_links)
for output_name, input_name in pairs(args.connect) do
local output = output_om:lookup { Constraint { "audio.channel", "equals", output_name } }
local input = input_om:lookup { Constraint { "audio.channel", "equals", input_name } }
if delete_links then
delete_link(all_links, output, input)
else
link_port(output, input)
end
end
end
output_om:connect("object-added", _connect)
input_om:connect("object-added", _connect)
all_links:connect("object-added", _connect)
output_om:activate()
input_om:activate()
all_links:activate()
if unless then
unless:connect("object-added", _connect)
unless:connect("object-removed", _connect)
unless:activate()
end
end
-- pw-cli list-objects | grep object.path
-- Connect to speakers
auto_connect_ports {
output = Constraint { "object.path", "matches", "speakers:*" },
input = Constraint { "object.path", "matches", "alsa:pcm:2:hw:2,0:playback:*" },
connect = {
["FL"] = "AUX0",
["FR"] = "AUX1"
}
}
-- Connect to mic
auto_connect_ports {
output = Constraint { "object.path", "matches", "alsa:pcm:2:hw:2,0:capture:*" },
input = Constraint { "object.path", "matches", "sm7b-processed:" },
connect = {
["AUX0"] = "FL",
["AUX1"] = "FR"
}
}