Added commands to skip +/- 1 minute.
[emus.git] / emus.el
diff --git a/emus.el b/emus.el
index 9d17b2a..e8b960d 100644 (file)
--- a/emus.el
+++ b/emus.el
@@ -100,6 +100,9 @@ Used to prevent commands from interfering with library construction.")
 (defvar emus-current-volume 100
   "The current playback volume.")
 
+(defvar emus-current-progress ""
+  "String describing the progress through the current track.")
+
 
 ;;; mpg123 process
 ;;
@@ -119,7 +122,7 @@ Used to prevent commands from interfering with library construction.")
              (make-process :name "emus-process"
                            :command `(,emus-mpg123-program "-R"))))
         (set-process-query-on-exit-flag proc nil)
-        (process-send-string proc "silence\n")
+        ;; (process-send-string proc "silence\n")
         proc))))
 
 (defun emus--send-cmd-raw (cmd &rest args)
@@ -305,13 +308,13 @@ by the filesystem."
 
 (defun emus--resume-cp ()
   "Resume continuous playback."
-  (setq emus-continuous-playback t)
-  (set-process-filter (emus-get-process)
-                      (lambda (_proc string)
-                        (and emus-continuous-playback
-                             (eq emus-state 'playing)
-                             (string-suffix-p "@P 0\n" string)
-                             (emus-play-next)))))
+  (setq emus-continuous-playback t))
+
+(defun emus--timestamp (seconds-total)
+  "Produce a timestamp string representation of SECONDS-TOTAL."
+  (let* ((seconds (truncate (mod seconds-total 60)))
+         (minutes (truncate (/ seconds-total 60))))
+    (format "%02d:%02d" minutes seconds)))
 
 (defun emus-play-track (track)
   "Set TRACK as current and start playing."
@@ -320,6 +323,31 @@ by the filesystem."
      (emus-send-cmd "l" (emus-track-file track))
      (setq emus-state 'playing)
      (setq emus-current-track track)
+     (setq emus-current-progress "")
+     (set-process-filter
+      (emus-get-process)
+      (lambda (_proc string)
+        (pcase string
+          ((and "@P 0\n"
+                (guard emus-continuous-playback)
+                (guard (eq emus-state 'playing)))
+           (emus-play-next))
+          ((rx (: string-start
+                  "@F "
+                  (+ digit)
+                  " "
+                  (+ digit)
+                  " "
+                  (let left-str (+ (not " ")))
+                  " "
+                  (let right-str (+ any))))
+           (let* ((left (string-to-number left-str))
+                  (right (string-to-number right-str))
+                  (total (+ left right)))
+             (setq emus-current-progress
+                   (format " %s/%s"
+                           (emus--timestamp left)
+                           (emus--timestamp total))))))))
      (emus--update-track old-track)
      (emus--update-track track)
      (emus--resume-cp)
@@ -453,6 +481,16 @@ If PREV is non-nil, plays the last track of the previous album."
   (interactive)
   (emus-jump -10))
 
+(defun emus-jump-1m-forward ()
+  "Jump 1 minute forward in current track."
+  (interactive)
+  (emus-jump 60))
+
+(defun emus-jump-1m-backward ()
+  "Jump 1 minute backward in current track."
+  (interactive)
+  (emus-jump -60))
+
 (defun emus-display-status ()
   "Display the current playback status in the minibuffer."
   (interactive)
@@ -461,8 +499,8 @@ If PREV is non-nil, plays the last track of the previous album."
     (concat "Emus: Volume %d%%"
             (pcase emus-state
               ('stopped " [Stopped]")
-              ('paused " [Paused]")
-              ('playing " [Playing]")
+              ('paused (format " [Paused%s]" emus-current-progress))
+              ('playing (format " [Playing%s]" emus-current-progress))
               (_ ""))
             " %s")
     emus-current-volume
@@ -665,6 +703,18 @@ Used to update browser display when `emus-current-track' and/or `emus-state' cha
   (emus-jump-10s-backward)
   (emus-display-status))
 
+(defun emus-jump-1m-forward-status ()
+  "Jump 10s forward in current track, then display the emus status in the minibuffer."
+  (interactive)
+  (emus-jump-1m-forward)
+  (emus-display-status))
+
+(defun emus-jump-1m-backward-status ()
+  "Jump 10s backward in current track, then display the emus status in the minibuffer."
+  (interactive)
+  (emus-jump-1m-backward)
+  (emus-display-status))
+
 (defun emus-goto-current-status ()
   "Move point to the current track, then display the emus status in the minibuffer."
   (interactive)
@@ -694,6 +744,8 @@ Used to update browser display when `emus-current-track' and/or `emus-state' cha
     (define-key map (kbd "P") 'emus-play-prev-album-status)
     (define-key map (kbd ",") 'emus-jump-10s-backward-status)
     (define-key map (kbd ".") 'emus-jump-10s-forward-status)
+    (define-key map (kbd "<") 'emus-jump-1m-backward-status)
+    (define-key map (kbd ">") 'emus-jump-1m-forward-status)
     (define-key map (kbd "c") 'emus-goto-current-status)
     (when (fboundp 'evil-define-key*)
       (evil-define-key* 'motion map
@@ -709,6 +761,8 @@ Used to update browser display when `emus-current-track' and/or `emus-state' cha
                         (kbd "P") 'emus-play-prev-album-status
                         (kbd ",") 'emus-jump-10s-backward-status
                         (kbd ".") 'emus-jump-10s-forward-status
+                        (kbd "<") 'emus-jump-1m-backward-status
+                        (kbd ">") 'emus-jump-1m-forward-status
                         (kbd "c") 'emus-goto-current-status))
     map)
   "Keymap for emus browser.")