+(defun emus-send-and-process (respfun predfun cmd &rest args)
+ (let ((respstr ""))
+ (set-process-filter (emus-get-process)
+ (lambda (proc string)
+ (setq respstr (concat respstr string))
+ (when (funcall predfun respstr)
+ (set-process-filter proc nil)
+ (funcall respfun respstr))))
+ (apply #'emus-send-cmd cmd args)))
+
+
+;;; Playback
+;;
+
+(defvar emus-current-record nil)
+(defvar emus-state 'stopped)
+(defvar emus-continuous-playback t)
+
+(defun emus--suspend-cp ()
+ (setq emus-continuous-playback nil))
+
+(defun emus--resume-cp ()
+ (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)))))
+
+(defun emus-play-record (record)
+ "Set RECORD as current and start playing."
+ (emus--with-library
+ (let ((old-record emus-current-record))
+ (emus-send-cmd "l" (emus-record-file record))
+ (setq emus-state 'playing)
+ (setq emus-current-record record)
+ (emus--update-record old-record)
+ (emus--update-record record)
+ (emus--resume-cp))))
+
+(defun emus-select-record (record)
+ "Set RECORD as current, but do not start playing."
+ (emus--with-library
+ (let ((old-record emus-current-record))
+ (setq emus-state 'stopped)
+ (setq emus-current-record record)
+ (emus--update-record old-record)
+ (emus--update-record record)
+ (emus-send-cmd "o")
+ (emus--resume-cp))))
+
+(defun emus-stop ()
+ "Stop playback of the current record."
+ (interactive)
+ (emus--with-library
+ (setq emus-state 'stopped)
+ (emus--update-record emus-current-record)
+ (emus-send-cmd "s")))
+
+(defun emus-playpause ()
+ (interactive)
+ (emus--with-library
+ (when emus-current-record
+ (if (eq emus-state 'stopped)
+ (emus-play-record emus-current-record)
+ (emus-send-cmd "p")
+ (pcase emus-state
+ ((or 'paused 'stopped) (setq emus-state 'playing))
+ ('playing (setq emus-state 'paused)))
+ (unless (eq emus-state 'paused)))
+ (emus--update-record emus-current-record))))
+
+(defvar emus-current-volume 100)
+
+(defun emus-set-volume (pct)
+ (emus--with-library
+ (setq emus-current-volume pct)
+ (emus-send-cmd "v" (number-to-string pct))))
+
+(defun emus-volume-increase-by (delta)
+ (emus-set-volume (max 0 (min 100 (+ emus-current-volume delta)))))
+
+(defun emus-volume-up ()
+ (interactive)
+ (emus-volume-increase-by 10))
+
+(defun emus-volume-down ()
+ (interactive)
+ (emus-volume-increase-by -10))
+
+(defun emus-play-nearby (offset)
+ (let ((idx (seq-position emus-records emus-current-record)))
+ (if idx
+ (let ((next-record (elt emus-records (+ idx offset))))
+ (if next-record
+ (if (eq emus-state 'playing)
+ (emus-play-record next-record)
+ (emus-select-record next-record))
+ (error "Track does not exist")))
+ (error "No track is currently selected"))))
+
+(defun emus-play-next ()
+ (interactive)
+ (emus-play-nearby 1))
+
+(defun emus-play-prev ()
+ (interactive)
+ (emus-play-nearby -1))
+
+(defun emus-display-status ()
+ (interactive)
+ (emus--with-library
+ (message
+ (concat "Emus: Volume %d%%"
+ (pcase emus-state
+ ('stopped " [Stopped]")
+ ('paused " [Paused]")
+ ('playing " [Playing]")
+ (_ ""))
+ (if emus-current-record
+ (format " - %.30s (%.20s)"
+ (emus-record-title emus-current-record)
+ (emus-record-artist emus-current-record))
+ ""))
+ emus-current-volume)))
+
+