From c891e97f4a78bec80e19495a875b2c48ecc514c5 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Fri, 13 Aug 2021 15:30:41 +0800 Subject: bongo: only "tick" when seeking * bongo.el (durand-bongo-mpv-player-tick, bongo-mpv-player-tick): Cancel this timer as soon as possible. (durand-bongo-seek-stop-timer, durand-bongo-seek-tick) (durand-bongo-seek-start-timer): Start a dedicated timer for seeking purposes. Also update the seek buffer after updating the elapsed time. (durand-bongo-seek-start-timer-maybe): For compatibility, don't start the timer if the backend is not mpv. --- bongo.el | 107 +++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 84 insertions(+), 23 deletions(-) diff --git a/bongo.el b/bongo.el index 8a74599..c78e723 100644 --- a/bongo.el +++ b/bongo.el @@ -257,27 +257,6 @@ convention is changed." (advice-add #'bongo-compose-remote-option :override 'durand-bongo-compose-remote-option) -;;;###autoload - (defun durand-bongo-mpv-player-tick (player) - "Only fetch metadata and length of track if not fetched already. - -Afterwards just stop the annoying timer." - (let ((timer (bongo-player-get player 'timer))) - (cond - ;; ((or (not (bongo-player-running-p player)) - ;; (and (bongo-player-get player 'socket) - ;; (not (equal (process-status (bongo-player-get player 'socket)) - ;; 'open)))) - ;; (bongo-mpv-player-stop-timer player)) - ((null (bongo-player-total-time player)) - (bongo--run-mpv-command player "duration" "get_property" "duration")) - ;; ((null (bongo-player-get player 'metadata-fetched)) - ;; (bongo--run-mpv-command player "metadata" "get_property" "metadata")) - (t (bongo-mpv-player-stop-timer player))))) - -;;;###autoload - ;; (advice-add #'bongo-mpv-player-tick :override #'durand-bongo-mpv-player-tick) - ;;;###autoload (defvar durand-bongo-save-playlist-hist nil "A variable that holds the history values for saving playlist names.") @@ -490,9 +469,9 @@ insert an action track at point." ((not (eq (current-buffer) bongo-seek-buffer)) (select-window (get-buffer-window bongo-seek-buffer)))) (bongo-seek-mode) - ;; NOTE: added by Durand to start always in evil-emacs-state (setq buffer-read-only t) - (bongo-seek-redisplay))) + (bongo-seek-redisplay) + (durand-bongo-seek-start-timer-maybe))) (advice-add 'bongo-seek :override #'durand-bongo-seek) @@ -655,5 +634,87 @@ Modified by Durand." (advice-add #'bongo-default-playlist-buffer :override #'durand-bongo-default-playlist-buffer-a) ) +;;; Improved bongo-mpv backend + +;;;; Disable the tick timer as soon as possible + +(defun durand-bongo-mpv-player-tick (player) + "Fetch metadata and length of track of PLAYER \ + if not fetched already. +Also fetch the time-pos initially. + +Afterwards just stop the annoying timer." + (bongo--run-mpv-command player "time-pos" "get_property" "time-pos") + (let ((timer (bongo-player-get player 'timer))) + (cond + ;; ((or (not (bongo-player-running-p player)) + ;; (and (bongo-player-get player 'socket) + ;; (not (equal (process-status (bongo-player-get player 'socket)) + ;; 'open)))) + ;; (bongo-mpv-player-stop-timer player)) + ((null (bongo-player-total-time player)) + (bongo--run-mpv-command player "duration" "get_property" "duration")) + ;; ((null (bongo-player-get player 'metadata-fetched)) + ;; (bongo--run-mpv-command player "metadata" "get_property" "metadata")) + (t (bongo-mpv-player-stop-timer player))))) + +;;;###autoload +(advice-add #'bongo-mpv-player-tick :override #'durand-bongo-mpv-player-tick) + +;;;; get-elapsed-time + +;; Since the tick is stopped, we are not constantly and furiously +;; updating the elapsed time of mpv now. This is exactly what we +;; want. But there is a problem: when seeking we don't know the +;; time-pos anymore. So we fix this by manually requesting the +;; time-pos in seeking. + +(defun durand-bongo-seek-stop-timer (player) + "Stop seek timer for the PLAYER." + (let ((timer (bongo-player-get player 'seek-timer))) + (cond + (timer + (cancel-timer timer) + (bongo-player-put player 'seek-timer nil))))) + +(defun durand-bongo-seek-tick (player) + "Tick when seeking." + (if (or (null bongo-seek-buffer) + (not (bongo-player-running-p player)) + (and (bongo-player-get player 'socket) + (not (equal (process-status (bongo-player-get player 'socket)) + 'open)))) + (durand-bongo-seek-stop-timer player) + (bongo--run-mpv-command player "time-pos" "get_property" "time-pos") + (when (null (bongo-player-total-time player)) + (bongo--run-mpv-command player "duration" "get_property" "duration")) + (unless (bongo-player-get player 'metadata-fetched) + (bongo--run-mpv-command player "metadata" "get_property" "metadata")) + (bongo-seek-redisplay))) + +(defun durand-bongo-seek-start-timer (player) + "Start ticking when seeking." + (durand-bongo-seek-stop-timer player) + (let ((timer (run-with-timer bongo-mpv-initialization-period + (bongo-player-get + player 'time-update-delay-after-seek) + 'durand-bongo-seek-tick + player))) + (bongo-player-put player 'seek-timer timer))) + +(defun durand-bongo-seek-start-timer-maybe (&rest _args) + "Start the seek timer for mpv backends." + (with-bongo-playlist-buffer + (cond + ((and bongo-player + (eq (car bongo-player) + 'mpv)) + (durand-bongo-seek-start-timer bongo-player))))) + +;; Normally we should do this, but I override the function by my +;; custom function, so I can just call this function there. + +;; (advice-add #'bongo-seek :after #'durand-bongo-seek-start-timer-maybe) + (use-package "volume" 'volume (define-key volume-mode-map [?s] #'volume-set)) -- cgit v1.2.3-18-g5258