Each row on Playback, Output Devices and Input Devices gets a level bar under its slider. A meter is a small capture pw_stream on WirePlumber's pw_core with the properties pipewire-pulse gives pavucontrol's peak streams (stream.monitor, target.object, stream.capture.sink for sinks); its process callback folds samples into a peak that a 40 ms GUI timer reads, maps with the slider's cubic curve and decays smoothly. - Only the visible tab is metered; output/stream meters are passive. - stream.monitor nodes (ours and pavucontrol's) are hidden from the tabs. - moonmix-probe gains `meter <id>`; the smoke test now plays noise and checks the stream and sink meters see it. - Screenshot mode holds the meters still before capturing, since a constantly redrawing window never yields a frame under broadway. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
172 lines
4.6 KiB
MoonBit
172 lines
4.6 KiB
MoonBit
// moonmix-probe: headless companion to the GUI. Connects through the same
|
|
// WirePlumber binding and model, prints what each tab would show, and can
|
|
// apply one action. Used for smoke tests and debugging without a display.
|
|
//
|
|
// moonmix-probe print all tabs
|
|
// <id> may be a numeric node id or a node.name.
|
|
//
|
|
// moonmix-probe volume <id> <pct> set linked volume (cubic %)
|
|
// moonmix-probe mute <id> <0|1>
|
|
// moonmix-probe default <id> make sink/source the default
|
|
// moonmix-probe move <stream> <dev> route a stream to a device
|
|
// moonmix-probe meter <id> peak level of a sink/source/playback
|
|
// stream over ~1.5 s (exit 6 if silent)
|
|
|
|
///|
|
|
extern "C" fn c_exit(code : Int32) = "exit"
|
|
|
|
///|
|
|
fn print_state(state : @model.State) -> Unit {
|
|
for tab in @model.Tab::all() {
|
|
println("== \{tab.label()}")
|
|
let rows = @model.rows(state, tab)
|
|
if rows.length() == 0 {
|
|
println(" (none)")
|
|
}
|
|
for r in rows {
|
|
let flags = []
|
|
if r.is_default {
|
|
flags.push("default")
|
|
}
|
|
if r.mute {
|
|
flags.push("muted")
|
|
}
|
|
if r.current_choice >= 0 {
|
|
flags.push("-> " + r.choices[r.current_choice].label)
|
|
}
|
|
let vol = if r.has_volume { r.volume_text } else { "(no volume)" }
|
|
let f = if flags.length() > 0 {
|
|
" [" + flags.join(", ") + "]"
|
|
} else {
|
|
""
|
|
}
|
|
println(" \{r.id}: \{r.title} | \{vol} | ch=\{r.volumes.length()}\{f}")
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn parse(s : String) -> Double {
|
|
@string.parse_double(s) catch {
|
|
_ => {
|
|
println("moonmix-probe: not a number: \{s}")
|
|
c_exit(2)
|
|
0.0
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Node id from a number or a node.name.
|
|
fn resolve(state : @model.State, s : String) -> Int32 {
|
|
for id, n in state.nodes {
|
|
if n.name == s {
|
|
return id
|
|
}
|
|
}
|
|
parse(s).to_int()
|
|
}
|
|
|
|
///|
|
|
fn act(state : @model.State, args : Array[String]) -> Bool {
|
|
match args {
|
|
["volume", id, pct] => {
|
|
let id = resolve(state, id)
|
|
let vols = match state.nodes.get(id) {
|
|
Some(n) => n.volumes
|
|
None => []
|
|
}
|
|
@wp.set_volumes(id, @model.scale_linked(vols, parse(pct)))
|
|
}
|
|
["mute", id, on] => @wp.set_mute(resolve(state, id), on == "1")
|
|
["default", id] => {
|
|
let id = resolve(state, id)
|
|
match state.nodes.get(id).map(fn(n) { n.kind() }) {
|
|
Some(Sink) => @wp.set_default_sink(id)
|
|
Some(Source) => @wp.set_default_source(id)
|
|
_ => false
|
|
}
|
|
}
|
|
["move", stream, dev] =>
|
|
@wp.move_stream(resolve(state, stream), resolve(state, dev))
|
|
_ => {
|
|
println("moonmix-probe: unknown command")
|
|
false
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn main {
|
|
let argv = @env.args()
|
|
let args = if argv.length() > 1 { argv[1:].to_owned() } else { [] }
|
|
let state = @model.State::new()
|
|
let meter_id : Ref[Int32] = { val: -1, }
|
|
@wp.on_event(fn(ev) {
|
|
for e in @wp.to_model_events(ev) {
|
|
let _ = state.apply(e)
|
|
}
|
|
match ev {
|
|
// default-nodes-api reports defaults shortly after Ready: settle first.
|
|
Ready => @wp.timeout(300, 2)
|
|
Timeout(2) => {
|
|
print_state(state)
|
|
if args is ["meter", id] {
|
|
let id = resolve(state, id)
|
|
let kind = match state.nodes.get(id) {
|
|
Some(n) => n.kind()
|
|
None => Other
|
|
}
|
|
meter_id.val = id
|
|
let ok = @wp.meter_start(id, kind)
|
|
println("== meter \{id}: \{if ok { "started" } else { "FAILED" }}")
|
|
if !ok {
|
|
c_exit(6)
|
|
}
|
|
@wp.timeout(1500, 3)
|
|
} else if args.length() > 0 {
|
|
let ok = act(state, args)
|
|
println(
|
|
"== action \{args.join(" ")}: \{if ok { "ok" } else { "FAILED" }}",
|
|
)
|
|
@wp.timeout(800, 1)
|
|
} else {
|
|
@wp.quit()
|
|
}
|
|
}
|
|
Timeout(3) => {
|
|
let peak = @wp.meter_take(meter_id.val).unwrap_or(0.0)
|
|
println("== meter \{meter_id.val}: peak \{peak}")
|
|
@wp.meter_stop(meter_id.val)
|
|
if peak <= 0.0 {
|
|
c_exit(6)
|
|
}
|
|
@wp.quit()
|
|
}
|
|
Timeout(1) => {
|
|
print_state(state)
|
|
@wp.quit()
|
|
}
|
|
PluginError => {
|
|
println("moonmix-probe: WirePlumber API modules failed to load")
|
|
c_exit(3)
|
|
}
|
|
Disconnected => {
|
|
println("moonmix-probe: disconnected")
|
|
c_exit(4)
|
|
}
|
|
Timeout(99) => {
|
|
println("moonmix-probe: timed out waiting for WirePlumber")
|
|
c_exit(5)
|
|
}
|
|
_ => ()
|
|
}
|
|
})
|
|
if !@wp.start() {
|
|
println("moonmix-probe: cannot connect to PipeWire")
|
|
c_exit(1)
|
|
}
|
|
@wp.timeout(10000, 99)
|
|
@wp.run()
|
|
}
|