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

Ian Eure <[email protected]>
Newsgroups gmane.emacs.emms.user
Message-ID <877cv5ew2k.fsf@meson>
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.
emms-mpd-native-seek.patch (text/x-diff, 2.5 KB)
commit fe6863d07eda03f4a2263382ee7d93b36a120e1c
Author: Ian Eure <[email protected]>
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."
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.