Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions bin/omarchy-toggle-low-latency-audio
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash

# omarchy:summary=Toggle low-latency audio for recording and playing instruments
# omarchy:args=[toggle|on|off|--status]
# omarchy:examples=omarchy toggle low-latency-audio | omarchy toggle low-latency-audio on | omarchy toggle low-latency-audio --status

# Forces PipeWire's graph to a small buffer, like the buffer size in macOS's
# Audio MIDI Setup. It is a runtime setting: nothing restarts, playback carries
# on, and a new session starts back on PipeWire's power-friendly default.
QUANTUM=256

forced_quantum() {
local output
output=$(pw-metadata -n settings 0 clock.force-quantum 2>/dev/null) || return 1
sed -n "s/.*key:'clock.force-quantum' value:'\([0-9]*\)'.*/\1/p" <<<"$output"
}

enabled() {
[[ $quantum == "$QUANTUM" ]]
}

set_quantum() {
if pw-metadata -n settings 0 clock.force-quantum "$1" >/dev/null 2>&1; then
quantum=$1
else
echo "Could not change PipeWire's buffer size. Is PipeWire running?" >&2
exit 1
fi
}

# A buffer someone forced to another size is theirs; only ours is switched off.
other_quantum() {
if [[ -n $quantum && $quantum != 0 && $quantum != "$QUANTUM" ]]; then
echo "PipeWire's buffer is already forced to $quantum samples; leaving it alone." >&2
exit 1
fi
}

# A small buffer only stays glitch-free when this user's PipeWire runs its
# data loop, the thread that processes audio, with realtime priority (FIFO or
# round-robin scheduling), which RealtimeKit grants.
realtime() {
local pid
pid=$(pgrep -xo -u "$UID" pipewire) || return 1
ps -L -o cls=,comm= -p "$pid" | grep -qE '^ *(FF|RR) +data-loop'
}

# A setting that can't be read is never reported as off: nothing was checked.
if ! quantum=$(forced_quantum); then
if [[ ${1:-} == "--status" ]]; then
exit 1
else
echo "Could not read PipeWire's buffer size. Is PipeWire running?" >&2
exit 1
fi
fi

case ${1:-toggle} in
--status)
enabled
exit
;;
toggle)
if enabled; then
set_quantum 0
else
other_quantum
set_quantum "$QUANTUM"
fi
;;
on) set_quantum "$QUANTUM" ;;
off)
if enabled; then
set_quantum 0
else
other_quantum
fi
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
;;
*)
echo "Usage: omarchy-toggle-low-latency-audio [toggle|on|off|--status]" >&2
exit 1
;;
esac

if enabled; then
if realtime; then
omarchy-notification-send -g 󰋋 "Low-latency audio on"
else
omarchy-notification-send -g 󰋋 "Low-latency audio on" "PipeWire isn't running with realtime priority (install rtkit), so expect crackles under load"
fi
else
omarchy-notification-send -g 󰋋 "Low-latency audio off"
fi
1 change: 1 addition & 0 deletions default/omarchy/omarchy-menu.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"trigger.toggle.nightlight": {"icon":"󰔎","label":"Nightlight","action":"omarchy-toggle-nightlight"},
"trigger.toggle.screensaver": {"icon":"󱄄","label":"Screensaver","action":"omarchy-toggle-screensaver"},
"trigger.toggle.crash-capture": {"icon":"󱚡","label":"Crash Capture","action":"omarchy-toggle-crash-capture"},
"trigger.toggle.low-latency-audio": {"icon":"󰋋","label":"Low Latency Audio","checked":"omarchy-toggle-low-latency-audio --status","action":"omarchy-toggle-low-latency-audio"},
"trigger.emoji": {"icon":"","label":"Emoji","aliases":["emoji","emojis"],"action":"omarchy-menu-emoji"},
"trigger.clipboard-history": {"icon":"󰅇","label":"Clipboard History","action":"omarchy-shell shell summon omarchy.clipboard"},
"trigger.reminder": {"icon":"󰢌","label":"Reminder","aliases":["reminder"]},
Expand Down
3 changes: 3 additions & 0 deletions manual/13-toggles-idle-screensaver.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ From the terminal, the same switches are `omarchy toggle <thing>`. Run `omarchy
| Stay awake (no idle lock) | `Super + Ctrl + I` | `omarchy toggle idle` |
| Crash capture | — | `omarchy toggle crash-capture` |
| Screensaver | — | `omarchy toggle screensaver` |
| Low-latency audio | — | `omarchy toggle low-latency-audio` |
| [Herdr](https://herdr.dev) theme sync | — | `omarchy toggle theme sync` |
| Menu bar | `Super + Shift + Space` | `omarchy toggle bar` |
| Touchpad | `XF86TouchpadToggle` | `omarchy toggle touchpad` |
Expand All @@ -26,6 +27,8 @@ The touchpad, touchscreen, and hybrid GPU switches live under _Trigger > Hardwar

_Trigger > Appearance_ holds the menu bar and animations, plus a few things that aren't `omarchy toggle` commands but behave the same: battery percentage in the bar, the default workspace layout, window gaps (`Super + Shift + Backspace`), and the 1-window square aspect (`Super + Ctrl + Backspace`).

Low-latency audio drops PipeWire's buffer to 256 samples, about 5 ms, for recording or playing software instruments, the way you'd lower the buffer size in Audio MIDI Setup on a Mac. It's a runtime switch: playback carries on, and the next session starts back on the power-friendly default. PipeWire needs `rtkit` for realtime priority, and without it a buffer this small can crackle under load, which the toggle's notification points out. If you've already forced a different buffer size yourself, the toggle leaves it alone.

Most of these are just a flag file under `~/.local/state/omarchy/toggles/`. If you want to branch on one in a script, `omarchy-toggle-enabled` gives you an exit code instead of making you go looking:

```bash
Expand Down
100 changes: 100 additions & 0 deletions test/shell.d/low-latency-audio-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
mkdir -p "$tmp/bin"

# pw-metadata keeps clock.force-quantum in a file; reads print it the way
# PipeWire does.
cat >"$tmp/bin/pw-metadata" <<SH
#!/bin/bash
state="$tmp/quantum"
[[ \${PIPEWIRE_DOWN:-0} == 1 ]] && exit 1
if (( \$# == 5 )); then
echo "\$5" >"\$state"
elif [[ -f \$state ]]; then
echo "Found \"settings\" metadata 30"
echo "update: id:0 key:'clock.force-quantum' value:'\$(cat "\$state")' type:''"
fi
SH
printf '#!/bin/bash\necho "$*" >>"%s/notifications"\n' "$tmp" >"$tmp/bin/omarchy-notification-send"
# PipeWire's data loop shows FF (FIFO) when it has realtime priority, TS
# otherwise. pgrep must ask for this user's PipeWire.
printf '#!/bin/bash\n[[ " $* " == *" -u %s "* ]] && echo 4242\n' "$UID" >"$tmp/bin/pgrep"
printf '#!/bin/bash\necho " TS pipewire"\necho " ${OTHER_CLASS:-TS} module-rt"\necho " ${PIPEWIRE_CLASS:-FF} data-loop.0"\n' >"$tmp/bin/ps"
chmod +x "$tmp/bin/"*

toggle() {
PATH="$tmp/bin:$PATH" "$ROOT/bin/omarchy-toggle-low-latency-audio" "$@"
}

rm -f "$tmp/quantum"
! toggle --status || fail "low-latency audio starts off when PipeWire has no forced quantum"
pass "low-latency audio starts off when PipeWire has no forced quantum"

toggle
[[ $(cat "$tmp/quantum") == 256 ]] || fail "toggling on forces a 256-sample quantum" "$(cat "$tmp/quantum")"
toggle --status || fail "status reports low-latency audio on"
pass "toggling on forces a 256-sample quantum and status reports it"

toggle
[[ $(cat "$tmp/quantum") == 0 ]] || fail "toggling off clears the forced quantum"
! toggle --status || fail "status reports low-latency audio off"
pass "toggling off clears the forced quantum"

toggle on && toggle on
[[ $(cat "$tmp/quantum") == 256 ]] || fail "on is idempotent"
toggle off
[[ $(cat "$tmp/quantum") == 0 ]] || fail "off clears the forced quantum"
pass "on and off set the state explicitly"

: >"$tmp/notifications"
PIPEWIRE_CLASS=TS toggle on
grep -q "realtime priority" "$tmp/notifications" || fail "turning on without realtime priority says so" "$(cat "$tmp/notifications")"
: >"$tmp/notifications"
PIPEWIRE_CLASS=FF toggle on
! grep -q "realtime" "$tmp/notifications" || fail "turning on with realtime priority shows no hint" "$(cat "$tmp/notifications")"
: >"$tmp/notifications"
PIPEWIRE_CLASS=TS OTHER_CLASS=FF toggle on
grep -q "realtime priority" "$tmp/notifications" || fail "only the data loop's scheduling counts" "$(cat "$tmp/notifications")"
pass "the realtime hint follows the data loop's actual scheduling"

toggle off
: >"$tmp/notifications"
if PIPEWIRE_DOWN=1 toggle on 2>/dev/null; then
fail "a failed PipeWire write is reported as a failure"
fi
[[ ! -s $tmp/notifications ]] || fail "a failed PipeWire write sends no notification" "$(cat "$tmp/notifications")"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Failed writes go untested

PIPEWIRE_DOWN=1 makes the stub fail the initial read, so this case never reaches a write. It would still pass if set_quantum stopped checking write failures, leaving that behavior unprotected.

Add a separate write-only failure flag. Let the initial read succeed, then check that the command fails without changing the setting or sending a notification.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile was right: PIPEWIRE_DOWN=1 failed the initial read, so the alleged write-failure case never reached set_quantum. Commit 2ff70c64 adds a separate write-only failure flag, lets the read succeed, and verifies that on exits non-zero without changing the quantum or sending a notification. The focused test passes, along with Bash syntax, shellcheck, and git diff --check; a fresh Codex review found no issue.

echo 256 >"$tmp/quantum"
: >"$tmp/notifications"
if PIPEWIRE_DOWN=1 toggle 2>/dev/null || PIPEWIRE_DOWN=1 toggle off 2>/dev/null; then
fail "a failed PipeWire read is reported as a failure"
fi
[[ ! -s $tmp/notifications && $(cat "$tmp/quantum") == 256 ]] ||
fail "a failed PipeWire read neither notifies nor changes the setting" "$(cat "$tmp/notifications")"
! PIPEWIRE_DOWN=1 toggle --status || fail "status is not on when PipeWire can't be read"
echo 0 >"$tmp/quantum"
pass "a failed PipeWire read or write fails without a notification"

echo 128 >"$tmp/quantum"
! toggle --status || fail "another forced quantum is not reported as low-latency audio"
if toggle 2>/dev/null || toggle off 2>/dev/null; then
fail "toggle and off refuse to replace another forced quantum"
fi
[[ $(cat "$tmp/quantum") == 128 ]] || fail "another forced quantum is left alone" "$(cat "$tmp/quantum")"
toggle on
[[ $(cat "$tmp/quantum") == 256 ]] || fail "on still sets low-latency audio explicitly"
pass "a buffer forced to another size is left alone unless on is asked for"

if toggle sideways 2>/dev/null; then
fail "an unknown argument is rejected"
fi
pass "an unknown argument is rejected"

grep -qF '"checked":"omarchy-toggle-low-latency-audio --status","action":"omarchy-toggle-low-latency-audio"' "$ROOT/default/omarchy/omarchy-menu.jsonc" ||
fail "the toggle menu offers low-latency audio with its state checked"
pass "the toggle menu offers low-latency audio with its state checked"