diff options
| author | Yuchen Pei <id@ypei.org> | 2026-11-08 07:58:30 +1100 |
|---|---|---|
| committer | Yuchen Pei <id@ypei.org> | 2026-11-08 07:58:30 +1100 |
| commit | 37c64109dc07a16417a42931c4eb4c682efc08ad (patch) | |
| tree | ac479bae5bf119caf337080267d02bd10e73bef9 /misc/bin | |
| parent | d1ab29768fb1d21e7733f7f3f5ab1960bf99de42 (diff) | |
[emacs][mpv] Add emms focus mode
Diffstat (limited to 'misc/bin')
| -rwxr-xr-x | misc/bin/emms-audio-focus.sh | 45 | ||||
| -rwxr-xr-x | misc/bin/emms.sh | 3 |
2 files changed, 48 insertions, 0 deletions
diff --git a/misc/bin/emms-audio-focus.sh b/misc/bin/emms-audio-focus.sh new file mode 100755 index 0000000..44a2080 --- /dev/null +++ b/misc/bin/emms-audio-focus.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# Pause EMMS while any other app plays audio; resume when quiet again. +RESUME_DELAY=2 # seconds of silence before resuming + +others_playing() { + pactl --format=json list sink-inputs 2>/dev/null | jq ' + [ .[] | select(.corked == false) + | select(.properties."application.name" != "emms") + | select(.properties."media.role" != "event") ] | length' +} + +emms_pause_if_playing() { # prints t only if *we* paused it + emacsclient -s emms --eval '(when (and emms-player-playing-p + (not emms-player-paused-p)) + (emms-pause) t)' 2>/dev/null +} + +emms_resume() { + emacsclient -s emms --eval '(when emms-player-paused-p (emms-pause))' >/dev/null 2>&1 +} + +exec 3< <(pactl subscribe) +paused_by_us=0 +quiet_since=0 + +while :; do + # Wake up on a sound-server event or once a second; drain event bursts. + read -r -t 1 -u 3 _ + while read -r -t 0.05 -u 3 _; do :; done + + n=$(others_playing) + if (( ${n:-0} > 0 )); then + quiet_since=0 + if (( ! paused_by_us )) && [[ $(emms_pause_if_playing) == t ]]; then + paused_by_us=1 + fi + elif (( paused_by_us )); then + (( quiet_since )) || quiet_since=$SECONDS + if (( SECONDS - quiet_since >= RESUME_DELAY )); then + emms_resume + paused_by_us=0 + quiet_since=0 + fi + fi +done diff --git a/misc/bin/emms.sh b/misc/bin/emms.sh new file mode 100755 index 0000000..0367671 --- /dev/null +++ b/misc/bin/emms.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +EMACS_PROFILE=emms emacs |
