A browser extension reports audible tabs to moonmix-tabs-host, which matches them to PipeWire streams by time and publishes the names for the mixer; streams of one tab share a row. Ambiguous streams keep the browser's name. Without the extension, a browser's only running stream shows its MPRIS title. The smoke test no longer claims real audio hardware. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
322 lines
8.1 KiB
MoonBit
322 lines
8.1 KiB
MoonBit
// The audio graph as moonmix sees it, and a reducer that applies change
|
|
// events coming from WirePlumber (node added/changed/removed, links changed,
|
|
// default nodes changed).
|
|
|
|
///|
|
|
#derive(Eq, Debug)
|
|
pub(all) enum NodeKind {
|
|
Sink // Output device ("Audio/Sink")
|
|
Source // Input device ("Audio/Source", "Audio/Source/Virtual")
|
|
PlaybackStream // Application playing audio ("Stream/Output/Audio")
|
|
RecordStream // Application recording audio ("Stream/Input/Audio")
|
|
Other
|
|
}
|
|
|
|
///|
|
|
pub fn NodeKind::from_media_class(media_class : String) -> NodeKind {
|
|
match media_class {
|
|
"Audio/Sink" | "Audio/Duplex" => Sink
|
|
"Audio/Source" | "Audio/Source/Virtual" => Source
|
|
"Stream/Output/Audio" => PlaybackStream
|
|
"Stream/Input/Audio" => RecordStream
|
|
_ => Other
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// One PipeWire node as reported by WirePlumber.
|
|
#derive(Eq, Debug)
|
|
pub(all) struct Node {
|
|
id : Int32 // PipeWire global (bound) id
|
|
name : String // node.name
|
|
description : String // node.description (devices) / node.nick
|
|
media_class : String
|
|
app_name : String // application.name (streams)
|
|
media_name : String // media.name (streams)
|
|
/// Icon names PipeWire/the app suggested, best first (application.icon-name,
|
|
/// media.icon-name for streams; device.icon-name, node.icon-name for devices).
|
|
icon_hints : Array[String]
|
|
form_factor : String // device.form-factor: "headset", "webcam", ...
|
|
bus : String // device.bus: "usb", "bluetooth", "pci", ...
|
|
app_binary : String // application.process.binary
|
|
app_id : String // pipewire.access.portal.app_id / application.id
|
|
serial : String // object.serial, never reused (ids are)
|
|
parent_pid : Int32 // parent of application.process.id; 0 if unknown
|
|
running : Bool // streams: node state is "running"
|
|
volumes : Array[Double] // linear, one per channel; empty = no volume
|
|
mute : Bool
|
|
}
|
|
|
|
///|
|
|
pub fn Node::kind(self : Node) -> NodeKind {
|
|
NodeKind::from_media_class(self.media_class)
|
|
}
|
|
|
|
///|
|
|
pub fn Node::new(
|
|
id : Int32,
|
|
media_class : String,
|
|
name~ : String = "",
|
|
description~ : String = "",
|
|
app_name~ : String = "",
|
|
media_name~ : String = "",
|
|
icon_hints~ : Array[String] = [],
|
|
form_factor~ : String = "",
|
|
bus~ : String = "",
|
|
app_binary~ : String = "",
|
|
app_id~ : String = "",
|
|
serial~ : String = "",
|
|
parent_pid~ : Int32 = 0,
|
|
running~ : Bool = true,
|
|
volumes~ : Array[Double] = [],
|
|
mute~ : Bool = false,
|
|
) -> Node {
|
|
{
|
|
id,
|
|
name,
|
|
description,
|
|
media_class,
|
|
app_name,
|
|
media_name,
|
|
icon_hints,
|
|
form_factor,
|
|
bus,
|
|
app_binary,
|
|
app_id,
|
|
serial,
|
|
parent_pid,
|
|
running,
|
|
volumes,
|
|
mute,
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// A PipeWire link between two nodes (ports are folded into nodes).
|
|
#derive(Eq, Debug)
|
|
pub(all) struct Link {
|
|
output_node : Int32
|
|
input_node : Int32
|
|
}
|
|
|
|
///|
|
|
#derive(Debug)
|
|
pub(all) enum Event {
|
|
/// Node appeared or one of its properties / volume / mute changed.
|
|
Upsert(Node)
|
|
/// Node disappeared.
|
|
Remove(Int32)
|
|
/// Complete current set of links.
|
|
Links(Array[Link])
|
|
/// Current default sink / source node ids (-1 = none).
|
|
Defaults(sink~ : Int32, source~ : Int32)
|
|
/// Streams matched to browser tabs, by object.serial.
|
|
TabNames(Map[String, TabName])
|
|
/// Title a browser is playing (MPRIS), by browser pid.
|
|
MediaTitles(Map[Int32, String])
|
|
/// Connection to PipeWire lost: forget everything.
|
|
Reset
|
|
}
|
|
|
|
///|
|
|
pub(all) struct State {
|
|
nodes : Map[Int32, Node]
|
|
mut links : Array[Link]
|
|
mut default_sink : Int32
|
|
mut default_source : Int32
|
|
mut tab_names : Map[String, TabName]
|
|
mut media_titles : Map[Int32, String]
|
|
}
|
|
|
|
///|
|
|
pub fn State::new() -> State {
|
|
{
|
|
nodes: {},
|
|
links: [],
|
|
default_sink: -1,
|
|
default_source: -1,
|
|
tab_names: Map([]),
|
|
media_titles: Map([]),
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Apply one event. Returns true if anything visible changed, so callers can
|
|
/// skip re-rendering for no-op updates (e.g. echoes of our own changes).
|
|
pub fn State::apply(self : State, ev : Event) -> Bool {
|
|
match ev {
|
|
Upsert(node) =>
|
|
match self.nodes.get(node.id) {
|
|
Some(old) if old == node => false
|
|
_ => {
|
|
self.nodes.set(node.id, node)
|
|
true
|
|
}
|
|
}
|
|
Remove(id) =>
|
|
if self.nodes.contains(id) {
|
|
self.nodes.remove(id)
|
|
self.links = self.links.filter(fn(l) {
|
|
l.output_node != id && l.input_node != id
|
|
})
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
Links(links) => {
|
|
let changed = links != self.links
|
|
self.links = links
|
|
changed
|
|
}
|
|
Defaults(sink~, source~) => {
|
|
let changed = sink != self.default_sink || source != self.default_source
|
|
self.default_sink = sink
|
|
self.default_source = source
|
|
changed
|
|
}
|
|
TabNames(names) => {
|
|
let changed = names != self.tab_names
|
|
self.tab_names = names
|
|
changed
|
|
}
|
|
MediaTitles(titles) => {
|
|
let changed = titles != self.media_titles
|
|
self.media_titles = titles
|
|
changed
|
|
}
|
|
Reset => {
|
|
let changed = self.nodes.length() > 0 || self.links.length() > 0
|
|
self.nodes.clear()
|
|
self.links = []
|
|
self.default_sink = -1
|
|
self.default_source = -1
|
|
changed
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Nodes of one kind, sorted for stable display (devices by description,
|
|
/// streams by application then id).
|
|
pub fn State::nodes_of(self : State, kind : NodeKind) -> Array[Node] {
|
|
let out = []
|
|
for _, n in self.nodes {
|
|
if n.kind() == kind {
|
|
out.push(n)
|
|
}
|
|
}
|
|
out.sort_by(fn(a, b) {
|
|
let c = compare_text(display_title(a), display_title(b))
|
|
if c != 0 {
|
|
c
|
|
} else {
|
|
a.id.compare(b.id)
|
|
}
|
|
})
|
|
out
|
|
}
|
|
|
|
///|
|
|
/// The device a stream is currently routed to: for a playback stream the
|
|
/// sink it links into, for a record stream the source it reads from.
|
|
pub fn State::stream_device(self : State, stream_id : Int32) -> Int32? {
|
|
let kind = match self.nodes.get(stream_id) {
|
|
Some(n) => n.kind()
|
|
None => return None
|
|
}
|
|
for l in self.links {
|
|
match kind {
|
|
PlaybackStream =>
|
|
if l.output_node == stream_id && self.is_kind(l.input_node, Sink) {
|
|
return Some(l.input_node)
|
|
}
|
|
RecordStream =>
|
|
if l.input_node == stream_id && self.is_kind(l.output_node, Source) {
|
|
return Some(l.output_node)
|
|
}
|
|
_ => ()
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
///|
|
|
fn State::is_kind(self : State, id : Int32, kind : NodeKind) -> Bool {
|
|
match self.nodes.get(id) {
|
|
Some(n) => n.kind() == kind
|
|
None => false
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Human title of a node, falling back through the available properties.
|
|
pub fn display_title(n : Node) -> String {
|
|
match n.kind() {
|
|
PlaybackStream | RecordStream =>
|
|
first_non_empty([n.app_name, n.description, n.name, "Stream \{n.id}"])
|
|
_ => first_non_empty([n.description, n.name, "Node \{n.id}"])
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// The browser tab a playback stream was matched to.
|
|
pub fn State::tab_of(self : State, n : Node) -> TabName? {
|
|
if n.kind() is PlaybackStream && n.serial != "" {
|
|
self.tab_names.get(n.serial)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// What the stream's browser says it is playing (MPRIS): only when that can
|
|
/// only mean this stream, i.e. it is the browser's one running stream.
|
|
pub fn State::media_title(self : State, n : Node) -> String? {
|
|
if !(n.kind() is PlaybackStream) || n.parent_pid == 0 || !n.running {
|
|
return None
|
|
}
|
|
for _, other in self.nodes {
|
|
if other.id != n.id &&
|
|
other.kind() is PlaybackStream &&
|
|
other.running &&
|
|
other.parent_pid == n.parent_pid {
|
|
return None
|
|
}
|
|
}
|
|
self.media_titles.get(n.parent_pid)
|
|
}
|
|
|
|
///|
|
|
/// Case-insensitive lexicographic comparison for display ordering.
|
|
/// (MoonBit's built-in String compare orders by length first.)
|
|
pub fn compare_text(a : String, b : String) -> Int32 {
|
|
let n = if a.length() < b.length() { a.length() } else { b.length() }
|
|
for i in 0..<n {
|
|
let x = fold_case(a.code_unit_at(i).to_int())
|
|
let y = fold_case(b.code_unit_at(i).to_int())
|
|
if x != y {
|
|
return x.compare(y)
|
|
}
|
|
}
|
|
a.length().compare(b.length())
|
|
}
|
|
|
|
///|
|
|
fn fold_case(c : Int32) -> Int32 {
|
|
if c >= 'A'.to_int() && c <= 'Z'.to_int() {
|
|
c + 32
|
|
} else {
|
|
c
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn first_non_empty(xs : Array[String]) -> String {
|
|
for x in xs {
|
|
if x != "" {
|
|
return x
|
|
}
|
|
}
|
|
""
|
|
}
|