Repository navigation
Add a low-latency audio toggle #13838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
57c8646
94d8cb6
2be56d3
4a25bc1
b01c69f
2ff70c6
8760397
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| ;; | ||
| *) | ||
| 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 | ||
| 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")" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Greptile was right: |
||
| 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" | ||
Uh oh!
There was an error while loading. Please reload this page.