Add moonmix: pavucontrol-style PipeWire mixer in MoonBit (WirePlumber + GTK4) (#1)
This commit was merged in pull request #1.
This commit is contained in:
1 parent
13a1de9b2c
commit
92ae5cdbfe
36 files changed
+3789
-1
No files matched your search
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user