Files
moonmix/README.md
T
SpriteandClaude Sonnet 5.5 3ac96fea35
CI / check (pull_request) Failing after 10s
CI / native (pull_request) Successful in 1m9s
Merge origin/main into better-icons
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 00:48:27 +00:00

198 lines
9.4 KiB
Markdown

# moonmix
A pavucontrol-style volume control for modern Linux audio (PipeWire with
WirePlumber), written in [MoonBit](https://www.moonbitlang.com) with a GTK4
GUI.
moonmix talks to the audio stack **through its libraries**: libwireplumber-0.5
on top of libpipewire-0.3. It never runs `wpctl`, `pactl`, `pw-cli`,
`pw-dump` or any other CLI tool.
| Playback | Output Devices |
| --- | --- |
| ![Playback tab](docs/screenshots/playback.png) | ![Output Devices tab](docs/screenshots/output-devices.png) |
| **Recording** | **Input Devices** |
| ![Recording tab](docs/screenshots/recording.png) | ![Input Devices tab](docs/screenshots/input-devices.png) |
*(Rendered headlessly against a private PipeWire with null sinks; see
[Testing](#testing).)*
## Features (v1)
- **Playback**: every application stream, with a volume slider, mute and the
output device it plays to (choose another to move it).
- **Recording**: capture streams, same controls, input device selector.
- **Output Devices** / **Input Devices**: sinks and sources with volume, mute
and a *Default* toggle that shows and sets the default device.
- **Live peak meters** under each slider on Playback, Output Devices and
Input Devices, on the same cubic scale as the slider. Only the visible
tab is metered; meters on outputs and app streams are passive, so they
never keep an idle device running.
- Volumes in percent exactly like pavucontrol/wpctl: the cubic mapping of
PipeWire's linear `channelVolumes`, up to 150%, with the dB value next to
it. One slider moves all channels, keeping the channel balance.
- Live updates: devices and streams appearing, disappearing or changing
elsewhere (another mixer, keyboard volume keys, a new Bluetooth headset)
show up immediately.
Not yet included: the Configuration tab (card profiles), per-channel
sliders, monitor sources in Input Devices, meters for recording streams.
## Architecture
```
cmd/moonmix GUI: builds tabs, reconciles widgets with view rows
cmd/moonmix-probe headless CLI over the same stack (smoke tests, debugging)
model/ pure MoonBit: volume curve, state reducer, view model
wp/ WirePlumber binding: wp_stub.c + wp.mbt
gtk/ minimal hand-written GTK4 binding: gtk_stub.c + gtk.mbt
tools/pkg-cc C compiler wrapper that adds pkg-config flags
```
```mermaid
flowchart LR
PW[(PipeWire)] <--> WP[libwireplumber<br/>mixer-api, default-nodes-api,<br/>object manager]
WP -- "(kind, id) events" --> WPB[wp package]
WPB -- "model.Event" --> S[model.State.apply]
S --> V[model.rows / diff_rows]
V --> G[GTK4 widgets]
G -- "set volume / mute / default / move" --> WPB
```
- **WirePlumber, not raw PipeWire.** The C layer (`wp/wp_stub.c`, about 460
lines) does what `wpctl` does internally. A `WpObjectManager` watches audio
nodes, links and the `default` metadata. The **mixer-api** plugin reads and
writes volume and mute; for hardware sinks it goes through the device's
route, which is where pavucontrol's numbers live. The **default-nodes-api**
plugin reads and sets the default sink and source. To move a stream,
moonmix sets `target.object` in the `default` metadata, as pipewire-pulse
does for pavucontrol. Peak meters are small capture `pw_stream`s on
WirePlumber's own `pw_core`, created with the properties pipewire-pulse
gives pavucontrol's peak streams (`stream.monitor`, `target.object`,
`stream.capture.sink` for sinks); their `process` callback only folds
samples into a peak value that a 40 ms GUI timer reads and resets. Nodes
with `stream.monitor` (ours and pavucontrol's) are hidden from the tabs.
The C layer sends MoonBit one callback per change,
`(kind, id)`. MoonBit then pulls node properties and volumes through small
accessor functions, so no C structs cross the FFI boundary.
- **One main loop.** WirePlumber and GTK4 both run on the default GLib main
context, so events arrive on the GUI thread with no locking or polling.
- **Pure core.** Everything else is plain MoonBit in `model/`: the cubic and
linear conversions, dB formatting, linked-slider scaling, the reducer that
applies node, link and default events, the per-tab rows, and the row diff
the GUI applies. `moon test` covers it on any target.
- **GTK binding.** It is hand-written and minimal. Each widget gets a tag
when it is created, and signals come back as `(tag, value)` through a
single callback. While MoonBit updates widgets from the model, signals are
suppressed so model updates never echo back into PipeWire. For a short
time after the user moves a slider, remote updates to that slider are
ignored so it doesn't jump while being dragged.
## Building
### Dependencies
You need MoonBit (`curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash`,
then add `~/.moon/bin` to `PATH`), a C compiler, `pkg-config` and the
development files:
| Distro | Packages |
| --- | --- |
| Arch | `pipewire wireplumber gtk4 pkgconf base-devel` |
| Debian 13+ / Ubuntu 24.04+ | `build-essential pkg-config libpipewire-0.3-dev libwireplumber-0.5-dev libgtk-4-dev` |
| Fedora (untested) | `gcc pkgconf-pkg-config pipewire-devel wireplumber-devel gtk4-devel` |
You need WirePlumber **0.5** and GTK **4.12** or newer. Debian 11 and 12
ship WirePlumber 0.4, which is not supported.
### Build and run
```sh
moon build --target native --release
./_build/native/release/build/cmd/moonmix/moonmix.exe
```
To install it for your user:
```sh
install -Dm755 _build/native/release/build/cmd/moonmix/moonmix.exe ~/.local/bin/moonmix
install -Dm644 data/org.latha.moonmix.desktop ~/.local/share/applications/org.latha.moonmix.desktop
install -Dm644 data/icons/hicolor/scalable/apps/org.latha.moonmix.svg ~/.local/share/icons/hicolor/scalable/apps/org.latha.moonmix.svg
install -Dm644 data/icons/hicolor/symbolic/apps/org.latha.moonmix-symbolic.svg ~/.local/share/icons/hicolor/symbolic/apps/org.latha.moonmix-symbolic.svg
gtk-update-icon-cache -f -t ~/.local/share/icons/hicolor # optional
```
The app icon is `org.latha.moonmix` (a speaker with a volume slider), shipped
as scalable and symbolic SVGs under `data/icons/hicolor/`. The window uses it
as its icon and the program name is `org.latha.moonmix`, which matches the
`.desktop` file so Wayland shells show it too. Run from a checkout, moonmix
also searches `./data/icons` and `$MOONMIX_ICON_DIR`.
### Icons
Everything else uses the desktop's icon theme (Adwaita, Breeze, Papirus...
any theme with the standard freedesktop names; install `adwaita-icon-theme`
if icons are missing). The names are chosen in `model/icons.mbt` and tested
with `moon test`; the GTK layer only asks the theme `has_icon`:
- **Devices**: the device's own `device.icon-name` / `node.icon-name`, then
`device.form-factor` (headphone, headset, speaker, microphone, webcam,
handset, tv, hdmi...), HDMI/webcam/Bluetooth/USB hints, then the direction
(speakers or microphone), then `audio-card-symbolic` and
`audio-x-generic-symbolic`. Symbolic variants are tried first so they follow
the theme's foreground colour. `image-missing` is the very last resort.
- **Streams**: `application.icon-name` / `media.icon-name`, then the app's
launcher icon found in the installed `.desktop` files (by
`pipewire.access.portal.app_id`, `application.process.binary` or
`application.name`), then `application-x-executable-symbolic`.
- **Mute button**: `audio-volume-high/medium/low/muted-symbolic` following the
volume and mute state. Row icons are 32 px, button and tab icons 16 px.
Pkg-config flags: moon does not expand `$(pkg-config ...)` in `moon.pkg`
link options. Instead, the packages point `link.native.stub-cc` and `cc` at
`tools/pkg-cc`, a wrapper that adds
`pkg-config --cflags --libs libpipewire-0.3 wireplumber-0.5 gtk4 gio-unix-2.0`. moon
derives the archiver name from the compiler name, so `tools/pkg-ar` is a
passthrough to `ar`. Set `MOONMIX_CC` or `MOONMIX_AR` to use another
compiler or archiver.
### Headless probe
`moonmix-probe` uses the same binding and model without a GUI:
```sh
P=./_build/native/debug/build/cmd/moonmix-probe/moonmix-probe.exe
$P # print what every tab shows
$P volume alsa_output.pci-0000_00_1f.3.analog-stereo 40
$P mute 57 1 # node id or node.name
$P default bluez_output.XX_XX.1
$P move <stream-id> <sink-id-or-name>
$P meter <id> # peak over ~1.5 s; exit 6 if silent
```
## Testing
```sh
moon test --target wasm-gc # pure model tests, no libraries needed
moon check --target native # type-check everything
tools/smoke-test.sh /tmp/shot # needs pipewire, wireplumber, pw-cat, gtk4-broadwayd
```
`tools/smoke-test.sh` starts a private PipeWire and WirePlumber in a temp
`XDG_RUNTIME_DIR`. The instance has two null sinks, a loopback virtual
microphone, one playback stream of noise and one capture stream created with `pw-cat`; the CLI is used only
by the test harness. The script runs every probe action and checks its effect, checks that peak
meters on the stream and its sink see the signal,
then starts the GUI under GTK's broadway backend. With `MOONMIX_SCREENSHOT`
set, the GUI renders each tab offscreen to a PNG and exits.
CI (`.gitea/workflows/ci.yaml`) runs the pure checks on the default runner.
It runs the native build and the smoke test in a `debian:trixie` container,
because bullseye has no WirePlumber 0.5.
## Releases
Versioning uses [sweet](.sweet.yml): add a change note with `sweet change`
and prepare releases with `sweet bump`. The version lives in `moon.mod` (the
`// sweet-version` line).