|
| 1 | +-- As explained on: https://bennett.dev/auto-link-pipewire-ports-wireplumber/ |
| 2 | +-- |
| 3 | +-- This script keeps my stereo-null-sink connected to whatever output I'm currently using. |
| 4 | +-- I do this so Pulseaudio (and Wine) always sees a stereo output plus I can swap the output |
| 5 | +-- without needing to reconnect everything. |
| 6 | + |
| 7 | +-- Link two ports together |
| 8 | +function link_port(output_port, input_port) |
| 9 | + if not input_port or not output_port then |
| 10 | + return nil |
| 11 | + end |
| 12 | + |
| 13 | + local link_args = { |
| 14 | + ["link.input.node"] = input_port.properties["node.id"], |
| 15 | + ["link.input.port"] = input_port.properties["object.id"], |
| 16 | + |
| 17 | + ["link.output.node"] = output_port.properties["node.id"], |
| 18 | + ["link.output.port"] = output_port.properties["object.id"], |
| 19 | + |
| 20 | + -- The node never got created if it didn't have this field set to something |
| 21 | + ["object.id"] = nil, |
| 22 | + |
| 23 | + -- I was running into issues when I didn't have this set |
| 24 | + ["object.linger"] = true, |
| 25 | + |
| 26 | + ["node.description"] = "Link created by auto_connect_ports" |
| 27 | + } |
| 28 | + |
| 29 | + local link = Link("link-factory", link_args) |
| 30 | + link:activate(1) |
| 31 | + |
| 32 | + return link |
| 33 | +end |
| 34 | + |
| 35 | +-- Automatically link ports together by their specific audio channels. |
| 36 | +-- |
| 37 | +-- ┌──────────────────┐ ┌───────────────────┐ |
| 38 | +-- │ │ │ │ |
| 39 | +-- │ FL ├────────►│ AUX0 │ |
| 40 | +-- │ SOURCE │ │ │ |
| 41 | +-- │ FR ├────────►│ AUX1 SINK │ |
| 42 | +-- │ │ │ │ |
| 43 | +-- └──────────────────┘ │ AUX2 │ |
| 44 | +-- │ │ |
| 45 | +-- └───────────────────┘ |
| 46 | +-- |
| 47 | +-- -- Call this method inside a script in global scope |
| 48 | +-- |
| 49 | +-- auto_connect_ports { |
| 50 | +-- |
| 51 | +-- -- A constraint for all the required ports of the output device |
| 52 | +-- source = Constraint { .. } |
| 53 | +-- |
| 54 | +-- -- A constraint for all the required ports of the input device |
| 55 | +-- sink = Constraint { .. } |
| 56 | +-- |
| 57 | +-- -- A mapping of output audio channels to input audio channels |
| 58 | +-- |
| 59 | +-- connections = { |
| 60 | +-- FL = "AUX0" |
| 61 | +-- FR = "AUX1" |
| 62 | +-- } |
| 63 | +-- |
| 64 | +-- } |
| 65 | +-- |
| 66 | +function auto_connect_ports(args) |
| 67 | + local source_om = ObjectManager { |
| 68 | + Interest { |
| 69 | + type = "port", |
| 70 | + args["source"], |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + local sink_om = ObjectManager { |
| 75 | + Interest { |
| 76 | + type = "port", |
| 77 | + args["sink"], |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + local all_links = ObjectManager { |
| 82 | + Interest { |
| 83 | + type = "link", |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + function _connect() |
| 88 | + print("_connect") |
| 89 | + for source_name, sink_name in pairs(args.connect) do |
| 90 | + print(source_name, sink_name) |
| 91 | + for source in source_om:iterate { Constraint { "port.name", "equals", source_name } } do |
| 92 | + print(source) |
| 93 | + for sink in sink_om:iterate { Constraint { "audio.channel", "equals", sink_name } } do |
| 94 | + print(sink) |
| 95 | + local link = link_port(source, sink) |
| 96 | + if link then |
| 97 | + print("link") |
| 98 | + else |
| 99 | + print("no link") |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + source_om:connect("object-added", _connect) |
| 107 | + sink_om:connect("object-added", _connect) |
| 108 | + all_links:connect("object-added", _connect) |
| 109 | + |
| 110 | + source_om:activate() |
| 111 | + sink_om:activate() |
| 112 | + all_links:activate() |
| 113 | +end |
| 114 | + |
| 115 | +auto_connect_ports { |
| 116 | + source = Constraint { "object.path", "matches", "virtual_in:*" }, |
| 117 | + sink = Constraint { "port.alias", "matches", "ALC671 Analog:*" }, |
| 118 | + connect = { |
| 119 | + monitor_FL = "FL", |
| 120 | + monitor_FR = "FR" |
| 121 | + } |
| 122 | +} |
| 123 | + |
0 commit comments