emms-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Use MPD native seeking in emms-player-mpd.el


From: Ian Eure
Subject: [PATCH] Use MPD native seeking in emms-player-mpd.el
Date: Fri, 24 Mar 2023 13:25:07 -0700
User-agent: mu4e 1.4.15; emacs 28.2

This patch replaces the relative/absolute seek logic for MPD. The current code does unnecessary work: it polls MPD for the current song and playback position, then adds the number of seconds to seek, then issues a `seek <playlist-index> <absolute-position>` command.

MPD does both those things natively; `seekcur` seeks the currently playing track, so it’s not necessary to determine its playlist index; and both `seek` and `seekcur` accept relative seeks -- for example `-10` to seek backwards ten seconds, or `+10` to jump forward ten seconds.

This patch updates emms-player-mpd to use `seekcur` and relative offsets.

commit fe6863d07eda03f4a2263382ee7d93b36a120e1c
Author: Ian Eure <ian@retrospec.tv>
Date:   Wed Mar 15 15:18:59 2023 -0700

    Use native MPD relative seeking.
    
    The way emms-player-mpd seeks is inefficient, complex, and duplicative.
    
    There are two seek operations.  `emms-player-mpd-seek-to` is an
    absolute seek to a time within the track, while `emms-player-mpd-seek`
    is a relative seek forwards or backwards from the current playback
    position.
    
    Both these methods use MPD’s `seek` command, which requires a song
    index and absolute time, which are the root of the issue.
    
    When doing an absolute seek, the playback status is fetched, to
    determine the song index, then a MPD `seek` command is issued.
    
    Relative seeking also does this, but issues another `status` to
    determine the playback position, adds the relative amount, and issues
    a `seek` command to that absolute position.
    
    Both operations are unnecessary.  MPD has a `seekcur` command that
    operates on the currently playing song, so looking that up isn’t
    needed.  It also supports relative seek with "+N" or "-N", so EMMS
    doesn’t need to find the position and compute a new one in
    `emms-player-mpd-seek`.

diff --git a/emms-player-mpd.el b/emms-player-mpd.el
index 4a406d4..e249684 100644
--- a/emms-player-mpd.el
+++ b/emms-player-mpd.el
@@ -1008,26 +1008,16 @@ from other functions."
 (defun emms-player-mpd-seek (amount)
   "Seek backward or forward by AMOUNT seconds, depending on sign of AMOUNT."
   (interactive)
-  (emms-player-mpd-get-status
-   amount
-   (lambda (amount info)
-     (let ((song (emms-player-mpd-get-current-song nil #'ignore info))
-          (secs (emms-player-mpd-get-playing-time nil #'ignore info)))
-       (when (and song secs)
-        (emms-player-mpd-send
-         (concat "seek " song " " (number-to-string (round (+ secs amount))))
-         nil #'ignore))))))
+  (emms-player-mpd-send
+   (concat "seekcur " (if (> amount 0) "+" "-") (number-to-string amount))
+   nil #'ignore))
 
 (defun emms-player-mpd-seek-to (pos)
   "Seek to POS seconds from the start of the current track."
   (interactive)
-  (emms-player-mpd-get-current-song
-   pos
-   (lambda (pos song)
-     (when (and song pos)
-       (emms-player-mpd-send
-       (concat "seek " song " " (number-to-string (round pos)))
-       nil #'ignore)))))
+  (emms-player-mpd-send
+   (concat "seekcur " (number-to-string (round pos)))
+   nil #'ignore))
 
 (defun emms-player-mpd-next ()
   "Move forward by one track in MusicPD's internal playlist."

reply via email to

[Prev in Thread] Current Thread [Next in Thread]