248 lines
6.0 KiB
MoonBit
248 lines
6.0 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_name : String // application.icon-name / device.icon-name
|
|
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_name~ : String = "",
|
|
volumes~ : Array[Double] = [],
|
|
mute~ : Bool = false,
|
|
) -> Node {
|
|
{
|
|
id,
|
|
name,
|
|
description,
|
|
media_class,
|
|
app_name,
|
|
media_name,
|
|
icon_name,
|
|
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)
|
|
/// 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
|
|
}
|
|
|
|
///|
|
|
pub fn State::new() -> State {
|
|
{ nodes: {}, links: [], default_sink: -1, default_source: -1, }
|
|
}
|
|
|
|
///|
|
|
/// 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
|
|
}
|
|
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}"])
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// 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
|
|
}
|
|
}
|
|
""
|
|
}
|