Add moonmix: pavucontrol-style PipeWire mixer in MoonBit (WirePlumber + GTK4) #1
No files matched your search
@@ -0,0 +1,6 @@
|
|||||||
|
_build/
|
||||||
|
target/
|
||||||
|
.mooncakes/
|
||||||
|
.moonagent/
|
||||||
|
*.o
|
||||||
|
/screenshots/tmp/
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// Pure logic: volume curves, the audio-graph state reducer and the view
|
||||||
|
// model the GUI renders. No FFI, so `moon test` runs it on any target.
|
||||||
|
import {
|
||||||
|
"moonbitlang/core/math",
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
|
package "geordi/moonmix/model"
|
||||||
|
|
||||||
|
import {
|
||||||
|
"moonbitlang/core/debug",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values
|
||||||
|
pub const MAX_PERCENT : Double = 150.0
|
||||||
|
|
||||||
|
pub const NORM_PERCENT : Double = 100.0
|
||||||
|
|
||||||
|
pub fn clamp_percent(Double) -> Double
|
||||||
|
|
||||||
|
pub fn diff_rows(Array[Row], Array[Row]) -> Diff
|
||||||
|
|
||||||
|
pub fn display_title(Node) -> String
|
||||||
|
|
||||||
|
pub fn format_db(Double) -> String
|
||||||
|
|
||||||
|
pub fn format_percent(Double) -> String
|
||||||
|
|
||||||
|
pub fn format_volume(Array[Double]) -> String
|
||||||
|
|
||||||
|
pub fn linear_to_db(Double) -> Double?
|
||||||
|
|
||||||
|
pub fn linear_to_percent(Double) -> Double
|
||||||
|
|
||||||
|
pub fn linked_percent(Array[Double]) -> Double
|
||||||
|
|
||||||
|
pub fn percent_to_linear(Double) -> Double
|
||||||
|
|
||||||
|
pub fn rows(State, Tab) -> Array[Row]
|
||||||
|
|
||||||
|
pub fn scale_linked(Array[Double], Double) -> Array[Double]
|
||||||
|
|
||||||
|
pub fn should_apply_remote(Int64, Int64, window_us? : Int64) -> Bool
|
||||||
|
|
||||||
|
// Errors
|
||||||
|
|
||||||
|
// Types and methods
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) struct DeviceChoice {
|
||||||
|
id : Int32
|
||||||
|
name : String
|
||||||
|
label : String
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) struct Diff {
|
||||||
|
removed : Array[Int32]
|
||||||
|
added : Array[Int32]
|
||||||
|
updated : Array[Int32]
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(@debug.Debug)
|
||||||
|
pub(all) enum Event {
|
||||||
|
Upsert(Node)
|
||||||
|
Remove(Int32)
|
||||||
|
Links(Array[Link])
|
||||||
|
Defaults(sink~ : Int32, source~ : Int32)
|
||||||
|
Reset
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) struct Link {
|
||||||
|
output_node : Int32
|
||||||
|
input_node : Int32
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) struct Node {
|
||||||
|
id : Int32
|
||||||
|
name : String
|
||||||
|
description : String
|
||||||
|
media_class : String
|
||||||
|
app_name : String
|
||||||
|
media_name : String
|
||||||
|
icon_name : String
|
||||||
|
volumes : Array[Double]
|
||||||
|
mute : Bool
|
||||||
|
}
|
||||||
|
pub fn Node::kind(Self) -> NodeKind
|
||||||
|
pub fn Node::new(Int32, String, name? : String, description? : String, app_name? : String, media_name? : String, icon_name? : String, volumes? : Array[Double], mute? : Bool) -> Self
|
||||||
|
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) enum NodeKind {
|
||||||
|
Sink
|
||||||
|
Source
|
||||||
|
PlaybackStream
|
||||||
|
RecordStream
|
||||||
|
Other
|
||||||
|
}
|
||||||
|
pub fn NodeKind::from_media_class(String) -> Self
|
||||||
|
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) struct Row {
|
||||||
|
id : Int32
|
||||||
|
title : String
|
||||||
|
subtitle : String
|
||||||
|
icon_name : String
|
||||||
|
volumes : Array[Double]
|
||||||
|
percent : Double
|
||||||
|
volume_text : String
|
||||||
|
mute : Bool
|
||||||
|
has_volume : Bool
|
||||||
|
is_default : Bool
|
||||||
|
choices : Array[DeviceChoice]
|
||||||
|
current_choice : Int32
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(all) struct State {
|
||||||
|
nodes : Map[Int32, Node]
|
||||||
|
mut links : Array[Link]
|
||||||
|
mut default_sink : Int32
|
||||||
|
mut default_source : Int32
|
||||||
|
}
|
||||||
|
pub fn State::apply(Self, Event) -> Bool
|
||||||
|
pub fn State::new() -> Self
|
||||||
|
pub fn State::nodes_of(Self, NodeKind) -> Array[Node]
|
||||||
|
pub fn State::stream_device(Self, Int32) -> Int32?
|
||||||
|
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) enum Tab {
|
||||||
|
Playback
|
||||||
|
Recording
|
||||||
|
OutputDevices
|
||||||
|
InputDevices
|
||||||
|
}
|
||||||
|
pub fn Tab::all() -> Array[Self]
|
||||||
|
pub fn Tab::empty_text(Self) -> String
|
||||||
|
pub fn Tab::label(Self) -> String
|
||||||
|
pub fn Tab::node_kind(Self) -> NodeKind
|
||||||
|
|
||||||
|
// Type aliases
|
||||||
|
|
||||||
|
// Traits
|
||||||
+247
@@ -0,0 +1,247 @@
|
|||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""
|
||||||
|
}
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
///|
|
||||||
|
fn sample_state() -> @model.State {
|
||||||
|
let s = @model.State::new()
|
||||||
|
let _ = s.apply(
|
||||||
|
Upsert(
|
||||||
|
@model.Node::new(
|
||||||
|
40,
|
||||||
|
"Audio/Sink",
|
||||||
|
name="alsa_output.pci.analog-stereo",
|
||||||
|
description="Built-in Audio Analog Stereo",
|
||||||
|
volumes=[1.0, 1.0],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
let _ = s.apply(
|
||||||
|
Upsert(
|
||||||
|
@model.Node::new(
|
||||||
|
41,
|
||||||
|
"Audio/Sink",
|
||||||
|
name="bluez_output.headphones",
|
||||||
|
description="Headphones",
|
||||||
|
volumes=[0.125, 0.125],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
let _ = s.apply(
|
||||||
|
Upsert(
|
||||||
|
@model.Node::new(
|
||||||
|
50,
|
||||||
|
"Audio/Source",
|
||||||
|
name="alsa_input.pci.analog-stereo",
|
||||||
|
description="Built-in Audio Analog Stereo",
|
||||||
|
volumes=[0.5],
|
||||||
|
mute=true,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
let _ = s.apply(
|
||||||
|
Upsert(
|
||||||
|
@model.Node::new(
|
||||||
|
60,
|
||||||
|
"Stream/Output/Audio",
|
||||||
|
name="firefox",
|
||||||
|
app_name="Firefox",
|
||||||
|
media_name="Big Buck Bunny",
|
||||||
|
volumes=[1.0, 1.0],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
let _ = s.apply(
|
||||||
|
Upsert(
|
||||||
|
@model.Node::new(
|
||||||
|
70,
|
||||||
|
"Stream/Input/Audio",
|
||||||
|
app_name="OBS",
|
||||||
|
media_name="Mic capture",
|
||||||
|
volumes=[1.0],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
let _ = s.apply(
|
||||||
|
Upsert(@model.Node::new(80, "Video/Source", name="v4l2_camera")),
|
||||||
|
)
|
||||||
|
let _ = s.apply(
|
||||||
|
Links([
|
||||||
|
{ output_node: 60, input_node: 41, },
|
||||||
|
{ output_node: 50, input_node: 70, },
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
let _ = s.apply(Defaults(sink=40, source=50))
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "media class mapping" {
|
||||||
|
assert_eq(@model.NodeKind::from_media_class("Audio/Sink"), Sink)
|
||||||
|
assert_eq(@model.NodeKind::from_media_class("Audio/Source/Virtual"), Source)
|
||||||
|
assert_eq(
|
||||||
|
@model.NodeKind::from_media_class("Stream/Output/Audio"),
|
||||||
|
PlaybackStream,
|
||||||
|
)
|
||||||
|
assert_eq(
|
||||||
|
@model.NodeKind::from_media_class("Stream/Input/Audio"),
|
||||||
|
RecordStream,
|
||||||
|
)
|
||||||
|
assert_eq(@model.NodeKind::from_media_class("Video/Source"), Other)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "upsert, no-op detection and remove" {
|
||||||
|
let s = sample_state()
|
||||||
|
assert_eq(s.nodes_of(Sink).length(), 2)
|
||||||
|
// identical upsert is not a change
|
||||||
|
let same = s.nodes.get(40).unwrap()
|
||||||
|
assert_false(s.apply(Upsert(same)))
|
||||||
|
// volume change is
|
||||||
|
assert_true(s.apply(Upsert({ ..same, volumes: [0.5, 0.5], })))
|
||||||
|
// removing a node drops its links too
|
||||||
|
assert_true(s.apply(Remove(41)))
|
||||||
|
assert_false(s.apply(Remove(41)))
|
||||||
|
assert_eq(s.links.length(), 1)
|
||||||
|
assert_eq(s.stream_device(60), None)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "defaults and reset" {
|
||||||
|
let s = sample_state()
|
||||||
|
assert_false(s.apply(Defaults(sink=40, source=50)))
|
||||||
|
assert_true(s.apply(Defaults(sink=41, source=50)))
|
||||||
|
assert_eq(s.default_sink, 41)
|
||||||
|
assert_true(s.apply(Reset))
|
||||||
|
assert_eq(s.nodes.length(), 0)
|
||||||
|
assert_eq(s.default_sink, -1)
|
||||||
|
assert_false(s.apply(Reset))
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "stream routing follows links" {
|
||||||
|
let s = sample_state()
|
||||||
|
assert_eq(s.stream_device(60), Some(41))
|
||||||
|
assert_eq(s.stream_device(70), Some(50))
|
||||||
|
assert_eq(s.stream_device(40), None)
|
||||||
|
assert_eq(s.stream_device(999), None)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "sorting by display title" {
|
||||||
|
let s = sample_state()
|
||||||
|
let sinks = s.nodes_of(Sink).map(fn(n) { n.id })
|
||||||
|
assert_eq(sinks, [40, 41]) // "Built-in..." < "Headphones"
|
||||||
|
}
|
||||||
+197
@@ -0,0 +1,197 @@
|
|||||||
|
// View model: what each pavucontrol-style tab shows, derived from State.
|
||||||
|
// The GTK layer only reconciles widgets against these rows.
|
||||||
|
|
||||||
|
///|
|
||||||
|
#derive(Eq, Debug)
|
||||||
|
pub(all) enum Tab {
|
||||||
|
Playback
|
||||||
|
Recording
|
||||||
|
OutputDevices
|
||||||
|
InputDevices
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Tab::all() -> Array[Tab] {
|
||||||
|
[Playback, Recording, OutputDevices, InputDevices]
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Tab::label(self : Tab) -> String {
|
||||||
|
match self {
|
||||||
|
Playback => "Playback"
|
||||||
|
Recording => "Recording"
|
||||||
|
OutputDevices => "Output Devices"
|
||||||
|
InputDevices => "Input Devices"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Tab::node_kind(self : Tab) -> NodeKind {
|
||||||
|
match self {
|
||||||
|
Playback => PlaybackStream
|
||||||
|
Recording => RecordStream
|
||||||
|
OutputDevices => Sink
|
||||||
|
InputDevices => Source
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Tab::empty_text(self : Tab) -> String {
|
||||||
|
match self {
|
||||||
|
Playback => "No application is currently playing audio."
|
||||||
|
Recording => "No application is currently recording audio."
|
||||||
|
OutputDevices => "No output devices available."
|
||||||
|
InputDevices => "No input devices available."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A device choice offered in a stream's "output/input device" dropdown.
|
||||||
|
#derive(Eq, Debug)
|
||||||
|
pub(all) struct DeviceChoice {
|
||||||
|
id : Int32
|
||||||
|
name : String // node.name, used as the routing target
|
||||||
|
label : String
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#derive(Eq, Debug)
|
||||||
|
pub(all) struct Row {
|
||||||
|
id : Int32
|
||||||
|
title : String
|
||||||
|
subtitle : String
|
||||||
|
icon_name : String
|
||||||
|
volumes : Array[Double]
|
||||||
|
percent : Double // linked slider value (cubic %)
|
||||||
|
volume_text : String // "75% (-7.50 dB)"
|
||||||
|
mute : Bool
|
||||||
|
has_volume : Bool
|
||||||
|
/// Devices only: this is the current default sink/source.
|
||||||
|
is_default : Bool
|
||||||
|
/// Streams only: devices the stream can be moved to, and the index of the
|
||||||
|
/// current one in that list (-1 when unknown / not linked yet).
|
||||||
|
choices : Array[DeviceChoice]
|
||||||
|
current_choice : Int32
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn rows(state : State, tab : Tab) -> Array[Row] {
|
||||||
|
let kind = tab.node_kind()
|
||||||
|
let device_kind = match tab {
|
||||||
|
Playback => Some(Sink)
|
||||||
|
Recording => Some(Source)
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
let choices = match device_kind {
|
||||||
|
Some(dk) =>
|
||||||
|
state
|
||||||
|
.nodes_of(dk)
|
||||||
|
.map(fn(d) { { id: d.id, name: d.name, label: display_title(d), } })
|
||||||
|
None => []
|
||||||
|
}
|
||||||
|
state
|
||||||
|
.nodes_of(kind)
|
||||||
|
.map(fn(n) {
|
||||||
|
let is_stream = device_kind is Some(_)
|
||||||
|
let subtitle = if is_stream {
|
||||||
|
n.media_name
|
||||||
|
} else if n.name != display_title(n) {
|
||||||
|
n.name
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
let current_choice = if is_stream {
|
||||||
|
match state.stream_device(n.id) {
|
||||||
|
Some(dev) => {
|
||||||
|
let mut idx = -1
|
||||||
|
for i, c in choices {
|
||||||
|
if c.id == dev {
|
||||||
|
idx = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
idx
|
||||||
|
}
|
||||||
|
None => -1
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
-1
|
||||||
|
}
|
||||||
|
let is_default = match kind {
|
||||||
|
Sink => n.id == state.default_sink
|
||||||
|
Source => n.id == state.default_source
|
||||||
|
_ => false
|
||||||
|
}
|
||||||
|
{
|
||||||
|
id: n.id,
|
||||||
|
title: display_title(n),
|
||||||
|
subtitle,
|
||||||
|
icon_name: if n.icon_name != "" {
|
||||||
|
n.icon_name
|
||||||
|
} else {
|
||||||
|
default_icon(kind)
|
||||||
|
},
|
||||||
|
volumes: n.volumes,
|
||||||
|
percent: linked_percent(n.volumes),
|
||||||
|
volume_text: if n.volumes.length() > 0 {
|
||||||
|
format_volume(n.volumes)
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
},
|
||||||
|
mute: n.mute,
|
||||||
|
has_volume: n.volumes.length() > 0,
|
||||||
|
is_default,
|
||||||
|
choices: if is_stream {
|
||||||
|
choices
|
||||||
|
} else {
|
||||||
|
[]
|
||||||
|
},
|
||||||
|
current_choice,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn default_icon(kind : NodeKind) -> String {
|
||||||
|
match kind {
|
||||||
|
Sink => "audio-speakers-symbolic"
|
||||||
|
Source => "audio-input-microphone-symbolic"
|
||||||
|
PlaybackStream => "audio-x-generic-symbolic"
|
||||||
|
RecordStream => "audio-input-microphone-symbolic"
|
||||||
|
Other => "audio-x-generic-symbolic"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Result of reconciling the rows shown on screen against new rows: which
|
||||||
|
/// row ids to remove, which to add (with their index) and which to update.
|
||||||
|
#derive(Eq, Debug)
|
||||||
|
pub(all) struct Diff {
|
||||||
|
removed : Array[Int32]
|
||||||
|
added : Array[Int32]
|
||||||
|
updated : Array[Int32]
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn diff_rows(old : Array[Row], new : Array[Row]) -> Diff {
|
||||||
|
let old_by_id : Map[Int32, Row] = Map([])
|
||||||
|
for r in old {
|
||||||
|
old_by_id.set(r.id, r)
|
||||||
|
}
|
||||||
|
let new_ids : Map[Int32, Bool] = Map([])
|
||||||
|
let added = []
|
||||||
|
let updated = []
|
||||||
|
for r in new {
|
||||||
|
new_ids.set(r.id, true)
|
||||||
|
match old_by_id.get(r.id) {
|
||||||
|
None => added.push(r.id)
|
||||||
|
Some(o) => if o != r { updated.push(r.id) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let removed = []
|
||||||
|
for r in old {
|
||||||
|
if !new_ids.contains(r.id) {
|
||||||
|
removed.push(r.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{ removed, added, updated, }
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
///|
|
||||||
|
test "playback rows" {
|
||||||
|
let s = sample_state()
|
||||||
|
let rows = @model.rows(s, Playback)
|
||||||
|
assert_eq(rows.length(), 1)
|
||||||
|
let r = rows[0]
|
||||||
|
assert_eq(r.title, "Firefox")
|
||||||
|
assert_eq(r.subtitle, "Big Buck Bunny")
|
||||||
|
assert_eq(r.volume_text, "100% (0.00 dB)")
|
||||||
|
assert_eq(r.choices.map(fn(c) { c.id }), [40, 41])
|
||||||
|
assert_eq(r.current_choice, 1)
|
||||||
|
assert_eq(r.choices[1].name, "bluez_output.headphones")
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "recording rows route to sources" {
|
||||||
|
let s = sample_state()
|
||||||
|
let r = @model.rows(s, Recording)[0]
|
||||||
|
assert_eq(r.title, "OBS")
|
||||||
|
assert_eq(r.choices.map(fn(c) { c.id }), [50])
|
||||||
|
assert_eq(r.current_choice, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "device rows" {
|
||||||
|
let s = sample_state()
|
||||||
|
let outs = @model.rows(s, OutputDevices)
|
||||||
|
assert_eq(outs.map(fn(r) { r.is_default }), [true, false])
|
||||||
|
assert_eq(outs[1].volume_text, "50% (-18.06 dB)")
|
||||||
|
assert_eq(outs[0].subtitle, "alsa_output.pci.analog-stereo")
|
||||||
|
assert_eq(outs[0].choices, [])
|
||||||
|
let ins = @model.rows(s, InputDevices)
|
||||||
|
assert_eq(ins.length(), 1)
|
||||||
|
assert_true(ins[0].mute)
|
||||||
|
assert_true(ins[0].is_default)
|
||||||
|
assert_eq(ins[0].icon_name, "audio-input-microphone-symbolic")
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "stream without app name falls back" {
|
||||||
|
let s = @model.State::new()
|
||||||
|
let _ = s.apply(Upsert(@model.Node::new(7, "Stream/Output/Audio")))
|
||||||
|
let r = @model.rows(s, Playback)[0]
|
||||||
|
assert_eq(r.title, "Stream 7")
|
||||||
|
assert_eq(r.current_choice, -1)
|
||||||
|
assert_false(r.has_volume)
|
||||||
|
assert_eq(r.volume_text, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "diff rows" {
|
||||||
|
let s = sample_state()
|
||||||
|
let before = @model.rows(s, OutputDevices)
|
||||||
|
let _ = s.apply(Remove(41))
|
||||||
|
let _ = s.apply(
|
||||||
|
Upsert(@model.Node::new(42, "Audio/Sink", description="HDMI")),
|
||||||
|
)
|
||||||
|
let _ = s.apply(Upsert({ ..s.nodes.get(40).unwrap(), mute: true, }))
|
||||||
|
let after = @model.rows(s, OutputDevices)
|
||||||
|
assert_eq(@model.diff_rows(before, after), {
|
||||||
|
removed: [41],
|
||||||
|
added: [42],
|
||||||
|
updated: [40],
|
||||||
|
})
|
||||||
|
assert_eq(@model.diff_rows(after, after), {
|
||||||
|
removed: [],
|
||||||
|
added: [],
|
||||||
|
updated: [],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "tab labels" {
|
||||||
|
assert_eq(@model.Tab::all().map(fn(t) { t.label() }), [
|
||||||
|
"Playback", "Recording", "Output Devices", "Input Devices",
|
||||||
|
])
|
||||||
|
}
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
// Volume model.
|
||||||
|
//
|
||||||
|
// PipeWire stores per-channel volumes as *linear* amplitude factors
|
||||||
|
// (`channelVolumes` in SPA_PARAM_Props, 1.0 = 0 dB). PulseAudio, and thus
|
||||||
|
// pavucontrol, shows a *cubic* scale: percent = cbrt(linear) * 100. wpctl and
|
||||||
|
// pipewire-pulse use the same mapping, so 50% here equals 50% in pavucontrol.
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Highest percentage the sliders allow (pavucontrol's "amplified" range).
|
||||||
|
pub const MAX_PERCENT : Double = 150.0
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// 100% in the cubic scale, i.e. linear 1.0 (0 dB).
|
||||||
|
pub const NORM_PERCENT : Double = 100.0
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Linear amplitude -> cubic percentage (0.0 ..).
|
||||||
|
pub fn linear_to_percent(linear : Double) -> Double {
|
||||||
|
if linear <= 0.0 {
|
||||||
|
0.0
|
||||||
|
} else {
|
||||||
|
@math.cbrt(linear) * 100.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Cubic percentage -> linear amplitude. Clamped to [0, MAX_PERCENT].
|
||||||
|
pub fn percent_to_linear(percent : Double) -> Double {
|
||||||
|
let p = clamp_percent(percent) / 100.0
|
||||||
|
p * p * p
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn clamp_percent(percent : Double) -> Double {
|
||||||
|
if percent.is_nan() || percent < 0.0 {
|
||||||
|
0.0
|
||||||
|
} else if percent > MAX_PERCENT {
|
||||||
|
MAX_PERCENT
|
||||||
|
} else {
|
||||||
|
percent
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Linear amplitude -> decibels. `None` means silence (-inf dB).
|
||||||
|
pub fn linear_to_db(linear : Double) -> Double? {
|
||||||
|
if linear <= 0.0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(20.0 * @math.log10(linear))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// The value a linked (single) slider shows for a set of channels: the
|
||||||
|
/// loudest channel, as pavucontrol does.
|
||||||
|
pub fn linked_percent(volumes : Array[Double]) -> Double {
|
||||||
|
let mut max = 0.0
|
||||||
|
for v in volumes {
|
||||||
|
if v > max {
|
||||||
|
max = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
linear_to_percent(max)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// New linear channel volumes after moving the linked slider to `percent`.
|
||||||
|
/// Channel balance is preserved: every channel is scaled by the same factor
|
||||||
|
/// so the loudest one ends at `percent`. If all channels are silent they are
|
||||||
|
/// set to the same value.
|
||||||
|
pub fn scale_linked(volumes : Array[Double], percent : Double) -> Array[Double] {
|
||||||
|
let target = percent_to_linear(percent)
|
||||||
|
let mut max = 0.0
|
||||||
|
for v in volumes {
|
||||||
|
if v > max {
|
||||||
|
max = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if volumes.length() == 0 {
|
||||||
|
[target]
|
||||||
|
} else if max <= 0.0 {
|
||||||
|
volumes.map(fn(_) { target })
|
||||||
|
} else {
|
||||||
|
let factor = target / max
|
||||||
|
volumes.map(fn(v) { v * factor })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// "75%" style label, rounded to the nearest whole percent.
|
||||||
|
pub fn format_percent(percent : Double) -> String {
|
||||||
|
let rounded = (percent + 0.5).floor().to_int()
|
||||||
|
"\{rounded}%"
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// "-7.50 dB" style label; silence is "-∞ dB".
|
||||||
|
pub fn format_db(linear : Double) -> String {
|
||||||
|
match linear_to_db(linear) {
|
||||||
|
None => "-∞ dB"
|
||||||
|
Some(db) => {
|
||||||
|
let neg = db < 0.0
|
||||||
|
let hundredths = ((if neg { -db } else { db }) * 100.0 + 0.5)
|
||||||
|
.floor()
|
||||||
|
.to_int()
|
||||||
|
let whole = hundredths / 100
|
||||||
|
let frac = hundredths % 100
|
||||||
|
let frac_s = if frac < 10 { "0\{frac}" } else { "\{frac}" }
|
||||||
|
let sign = if neg && hundredths != 0 { "-" } else { "" }
|
||||||
|
"\{sign}\{whole}.\{frac_s} dB"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Tooltip/label text pavucontrol-style: "75% (-7.50 dB)".
|
||||||
|
pub fn format_volume(volumes : Array[Double]) -> String {
|
||||||
|
let mut max = 0.0
|
||||||
|
for v in volumes {
|
||||||
|
if v > max {
|
||||||
|
max = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"\{format_percent(linear_to_percent(max))} (\{format_db(max)})"
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Echo suppression for sliders: after the user moves a slider we push the
|
||||||
|
/// value to PipeWire, which echoes it back slightly later. Remote updates that
|
||||||
|
/// arrive within `window_us` of a local change are ignored so the slider does
|
||||||
|
/// not jump back while being dragged.
|
||||||
|
pub fn should_apply_remote(
|
||||||
|
last_local_us : Int64,
|
||||||
|
now_us : Int64,
|
||||||
|
window_us~ : Int64 = 250_000L,
|
||||||
|
) -> Bool {
|
||||||
|
now_us - last_local_us >= window_us
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
///|
|
||||||
|
fn approx(a : Double, b : Double) -> Bool {
|
||||||
|
(a - b).abs() < 1.0e-9
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "linear <-> percent uses the cubic scale" {
|
||||||
|
assert_true(approx(@model.linear_to_percent(1.0), 100.0))
|
||||||
|
assert_true(approx(@model.linear_to_percent(0.125), 50.0))
|
||||||
|
assert_true(approx(@model.linear_to_percent(0.0), 0.0))
|
||||||
|
assert_true(approx(@model.linear_to_percent(-1.0), 0.0))
|
||||||
|
assert_true(approx(@model.percent_to_linear(50.0), 0.125))
|
||||||
|
assert_true(approx(@model.percent_to_linear(150.0), 3.375))
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "percent_to_linear clamps to 0..150" {
|
||||||
|
assert_true(approx(@model.percent_to_linear(200.0), 3.375))
|
||||||
|
assert_true(approx(@model.percent_to_linear(-5.0), 0.0))
|
||||||
|
assert_true(approx(@model.percent_to_linear(0.0 / 0.0), 0.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "round trip" {
|
||||||
|
for p in [0.0, 1.0, 33.3, 64.0, 100.0, 123.4, 150.0] {
|
||||||
|
assert_true(
|
||||||
|
(@model.linear_to_percent(@model.percent_to_linear(p)) - p).abs() < 1.0e-9,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "decibels" {
|
||||||
|
assert_eq(@model.linear_to_db(0.0), None)
|
||||||
|
assert_true(approx(@model.linear_to_db(1.0).unwrap(), 0.0))
|
||||||
|
assert_eq(@model.format_db(1.0), "0.00 dB")
|
||||||
|
assert_eq(@model.format_db(0.0), "-∞ dB")
|
||||||
|
// 50% cubic = 0.125 linear = -18.06 dB
|
||||||
|
assert_eq(@model.format_db(0.125), "-18.06 dB")
|
||||||
|
assert_eq(@model.format_db(3.375), "10.57 dB")
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "format percent rounds" {
|
||||||
|
assert_eq(@model.format_percent(74.5), "75%")
|
||||||
|
assert_eq(@model.format_percent(74.4), "74%")
|
||||||
|
assert_eq(@model.format_percent(0.0), "0%")
|
||||||
|
assert_eq(@model.format_volume([0.125, 0.0]), "50% (-18.06 dB)")
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "linked slider follows the loudest channel and keeps balance" {
|
||||||
|
assert_true(approx(@model.linked_percent([0.125, 1.0]), 100.0))
|
||||||
|
assert_true(approx(@model.linked_percent([]), 0.0))
|
||||||
|
let scaled = @model.scale_linked([0.5, 1.0], 50.0)
|
||||||
|
assert_true(approx(scaled[0], 0.0625))
|
||||||
|
assert_true(approx(scaled[1], 0.125))
|
||||||
|
// all silent: every channel gets the target
|
||||||
|
let from_zero = @model.scale_linked([0.0, 0.0], 100.0)
|
||||||
|
assert_true(approx(from_zero[0], 1.0) && approx(from_zero[1], 1.0))
|
||||||
|
// no channel info: a single value
|
||||||
|
assert_eq(@model.scale_linked([], 100.0).length(), 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "echo suppression window" {
|
||||||
|
assert_false(@model.should_apply_remote(1_000_000L, 1_100_000L))
|
||||||
|
assert_true(@model.should_apply_remote(1_000_000L, 1_300_000L))
|
||||||
|
assert_true(@model.should_apply_remote(0L, 1_000_000L, window_us=0L))
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
// moonmix: a pavucontrol-like volume control for PipeWire + WirePlumber.
|
||||||
|
// https://docs.moonbitlang.com/en/latest/toolchain/moon/module.html
|
||||||
|
|
||||||
|
name = "geordi/moonmix"
|
||||||
|
|
||||||
|
version = "0.1.0" // sweet-version
|
||||||
|
|
||||||
|
readme = "README.md"
|
||||||
|
|
||||||
|
repository = "https://git.site.latha.org/geordi/moonmix"
|
||||||
|
|
||||||
|
license = "MIT"
|
||||||
|
|
||||||
|
keywords = [ "pipewire", "wireplumber", "gtk4", "audio", "mixer" ]
|
||||||
|
|
||||||
|
preferred_target = "native"
|
||||||
|
|
||||||
|
description = "pavucontrol-like volume control for PipeWire/WirePlumber, written in MoonBit"
|
||||||
Reference in new issue
Block a user