From 37c64109dc07a16417a42931c4eb4c682efc08ad Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sun, 8 Nov 2026 07:58:30 +1100 Subject: [emacs][mpv] Add emms focus mode --- misc/.config/mpv/input.conf | 1 + misc/bin/emms-audio-focus.sh | 45 ++++++++++++++++++++++++++++++++++++++++++++ misc/bin/emms.sh | 3 +++ 3 files changed, 49 insertions(+) create mode 100755 misc/bin/emms-audio-focus.sh create mode 100755 misc/bin/emms.sh (limited to 'misc') diff --git a/misc/.config/mpv/input.conf b/misc/.config/mpv/input.conf index 0238138..8d0f7a3 100644 --- a/misc/.config/mpv/input.conf +++ b/misc/.config/mpv/input.conf @@ -1,3 +1,4 @@ PREV seek -15 NEXT stop a vf toggle hflip +j script-message-to console type "seek absolute" 6 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 -- cgit v1.2.3