+(defun emus-make-track (artist album title filename &optional pos)
+ "Create an object representing an emus track.
+ARTIST, ALBUM and TITLE are used to describe the track, FILENAME
+refers to the mp3 file containing the track. If non-nil, POS
+specifies the position of the record representing this track in the
+emus browser buffer."
+ (vector artist album title filename pos))
+
+(defun emus-track-artist (track)
+ "The artist corresponding to TRACK."
+ (elt track 0))
+
+(defun emus-track-album (track)
+ "The album corresponding to TRACK."
+ (elt track 1))
+
+(defun emus-track-title (track)
+ "The title of TRACK."
+ (elt track 2))
+
+(defun emus-track-file (track)
+ "The mp3 file corresponding to TRACK."
+ (elt track 3))
+
+(defun emus-track-browser-pos (track)
+ "The location of the browser buffer record corresponding to TRACK."
+ (elt track 4))
+
+(defun emus-set-track-browser-pos (track pos)
+ "Set the location of the browser buffer record corresponding to TRACK to POS."
+ (aset track 4 pos))
+
+(defun emus--load-library (then)
+ "Initialize the emus track library.
+Once the library is initialized, the function THEN is called."
+ (unless emus--proc-in-use
+ (setq emus--proc-in-use t)
+ (emus--suspend-cp)
+ (setq emus-state 'stopped)
+ (let ((proc (emus-get-process))
+ (tagstr "")
+ (filenames (emus-get-audio-files)))
+ (setq emus-tracks nil)
+ (set-process-filter proc (lambda (proc string)
+ (setq tagstr (concat tagstr string))
+ (when (string-suffix-p "@P 1\n" string)
+ (add-to-list 'emus-tracks
+ (emus--make-track-from-tagstr (car filenames)
+ tagstr))
+ (setq tagstr "")
+ (setq filenames (cdr filenames))
+ (if filenames
+ (emus--send-cmd-raw "lp" (car filenames))
+ (set-process-filter proc nil)
+ (setq emus-tracks (reverse emus-tracks))
+ (emus--sort-tracks)
+ (unless emus-current-track
+ (setq emus-current-track (car emus-tracks)))
+ (funcall then)
+ (emus--resume-cp)
+ (setq emus--proc-in-use nil)))))
+ (emus--send-cmd-raw "lp" (car filenames)))))
+
+(defun emus--make-track-from-tagstr (filename tagstr)
+ "Parse TAGSTR to populate the fields of a track corresponding to FILENAME."
+ (let ((artist "")
+ (album "")
+ (title ""))
+ (dolist (line (split-string tagstr "\n"))
+ (let ((found-artist (elt (split-string line "@I ID3v2.artist:") 1))
+ (found-album (elt (split-string line "@I ID3v2.album:") 1))
+ (found-title (elt (split-string line "@I ID3v2.title:") 1)))
+ (cond
+ (found-artist (setq artist found-artist))
+ (found-album (setq album found-album))
+ (found-title (setq title found-title)))))
+ (emus-make-track artist album title filename nil)))
+
+(defun emus--sort-tracks ()
+ "Sort the library tracks according to artist and album.
+Leaves the track titles unsorted, so they will appear in the order specified
+by the filesystem."
+ (sort emus-tracks
+ (lambda (r1 r2)
+ (let ((artist1 (emus-track-artist r1))
+ (artist2 (emus-track-artist r2)))
+ (if (string= artist1 artist2)
+ (let ((album1 (emus-track-album r1))
+ (album2 (emus-track-album r2)))
+ (string< album1 album2))
+ (string< artist1 artist2))))))
+
+(defmacro emus--with-library (&rest body)
+ "Evaluate BODY with the library initialized."
+ `(if emus-tracks
+ (progn ,@body)
+ (emus--load-library
+ (lambda () ,@body))))
+
+
+;;; Playback
+;;
+
+(defun emus--suspend-cp ()
+ "Suspend continuous playback."
+ (setq emus-continuous-playback nil))
+
+(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)))))
+
+(defun emus-play-track (track)
+ "Set TRACK as current and start playing."
+ (emus--with-library
+ (let ((old-track emus-current-track))
+ (emus-send-cmd "l" (emus-track-file track))
+ (setq emus-state 'playing)
+ (setq emus-current-track track)
+ (emus--update-track old-track)
+ (emus--update-track track)
+ (emus--resume-cp)
+ (emus-goto-current))))
+
+(defun emus-select-track (track)
+ "Set TRACK as current, but do not start playing."
+ (emus--with-library
+ (let ((old-track emus-current-track))
+ (setq emus-state 'stopped)
+ (setq emus-current-track track)
+ (emus--update-track old-track)
+ (emus--update-track track)
+ (emus-send-cmd "o")
+ (emus--resume-cp)
+ (emus-goto-current))))
+
+(defun emus-stop ()
+ "Stop playback of the current track."
+ (interactive)
+ (emus--with-library
+ (setq emus-state 'stopped)
+ (emus--update-track emus-current-track)
+ (emus-send-cmd "s")))
+
+(defun emus-playpause ()
+ "Begin playback of the current track.
+If the track is already playing, pause playback.
+If the track is currently paused, resume playback."
+ (interactive)
+ (emus--with-library
+ (when emus-current-track
+ (if (eq emus-state 'stopped)
+ (emus-play-track emus-current-track)
+ (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-track emus-current-track))))
+
+(defun emus-set-volume (pct)
+ "Set the playback volume to PCT %."
+ (emus--with-library
+ (setq emus-current-volume pct)
+ (emus-send-cmd "v" (number-to-string pct))))
+
+(defun emus-volume-increase-by (delta)
+ "Increase the playback volume by DELTA %."
+ (emus-set-volume (max 0 (min 100 (+ emus-current-volume delta)))))
+
+(defun emus-volume-up ()
+ "Increase the playback volume by 10%."
+ (interactive)
+ (emus-volume-increase-by 10))
+
+(defun emus-volume-down ()
+ "Decrease the playback volume by 10%."
+ (interactive)
+ (emus-volume-increase-by -10))
+
+(defun emus--play-adjacent-track (&optional prev)
+ "Play the next track in the library, or the previous if PREV is non-nil."
+ (emus--with-library
+ (let ((idx (seq-position emus-tracks emus-current-track))
+ (offset (if prev -1 +1)))
+ (if idx
+ (let ((next-track (elt emus-tracks (+ idx offset))))
+ (if next-track
+ (if (eq emus-state 'playing)
+ (emus-play-track next-track)
+ (emus-select-track next-track))
+ (error "Track does not exist")))
+ (error "No track selected")))))
+
+(defun emus--play-adjacent-album (&optional prev)
+ "Play the first track of the next album in the library.
+If PREV is non-nil, plays the last track of the previous album."
+ (emus--with-library
+ (let ((idx (seq-position emus-tracks emus-current-track)))
+ (if idx
+ (let* ((search-list (if prev
+ (reverse (seq-subseq emus-tracks 0 idx))
+ (seq-subseq emus-tracks (+ idx 1))))
+ (current-album (emus-track-album emus-current-track))
+ (next-track (seq-some (lambda (r)
+ (if (string= (emus-track-album r)
+ current-album)
+ nil
+ r))
+ search-list)))
+ (if next-track
+ (if (eq emus-state 'playing)
+ (emus-play-track next-track)
+ (emus-select-track next-track))
+ (error "Track does not exist")))
+ (error "No track selected")))))
+
+(defun emus-play-next ()
+ "Play the next track in the library."
+ (interactive)
+ (emus--play-adjacent-track))
+
+(defun emus-play-prev ()
+ "Play the previous track in the library."
+ (interactive)
+ (emus--play-adjacent-track t))
+
+(defun emus-play-next-album ()
+ "Play the first track of the next album in the library."
+ (interactive)
+ (emus--play-adjacent-album))
+
+(defun emus-play-prev-album ()
+ "Play the last track of the previous album in the library."
+ (interactive)
+ (emus--play-adjacent-album t))
+
+(defun emus-jump (seconds)
+ "Jump forward in current track by SECONDS seconds."
+ (emus--with-library
+ (emus-send-cmd "jump" (format "%+ds" seconds))))
+
+(defun emus-jump-10s-forward ()
+ "Jump 10 seconds forward in current track."
+ (interactive)
+ (emus-jump 10))
+
+(defun emus-jump-10s-backward ()
+ "Jump 10 seconds backward in current track."
+ (interactive)
+ (emus-jump -10))
+
+(defun emus-display-status ()
+ "Display the current playback status in the minibuffer."
+ (interactive)
+ (emus--with-library
+ (message
+ (concat "Emus: Volume %d%%"
+ (pcase emus-state
+ ('stopped " [Stopped]")
+ ('paused " [Paused]")
+ ('playing " [Playing]")
+ (_ ""))
+ (if emus-current-track
+ (format " - %.30s (%.20s)"
+ (emus-track-title emus-current-track)
+ (emus-track-artist emus-current-track))
+ ""))
+ emus-current-volume)))
+
+
+;;; Browser
+;;
+
+(defun emus--insert-track (track &optional prev-track first)
+ "Insert a button representing TRACK into the current buffer.
+
+When provided, PREV-TRACK is used to determine whether to insert additional
+headers representing the artist or the album title.
+
+If non-nil, FIRST indicates that the track is the first in the library
+and thus requires both artist and album headers."
+ (let* ((artist (emus-track-artist track))
+ (album (emus-track-album track))
+ (title (emus-track-title track))
+ (help-str (format "mouse-1, RET: Play '%.30s' (%.20s)" title artist)))
+ (when (or prev-track first)
+ (unless (equal (emus-track-artist prev-track) artist)
+ (insert-text-button
+ (propertize artist 'face 'emus-artist)
+ 'action #'emus--click-track
+ 'follow-link t
+ 'help-echo help-str
+ 'emus-track track)
+ (insert (propertize "\n" 'face 'emus-artist)))
+ (unless (equal (emus-track-album prev-track) album)
+ (insert-text-button
+ (propertize (concat " " album) 'face 'emus-album)
+ 'action #'emus--click-track
+ 'follow-link t
+ 'help-echo help-str
+ 'emus-track track)
+ (insert (propertize "\n" 'face 'emus-album))))
+ (emus-set-track-browser-pos track (point))
+ (let ((is-current (equal track emus-current-track)))
+ (insert-text-button
+ (concat
+ (if is-current
+ (propertize
+ (pcase emus-state
+ ('playing "->")
+ ('paused "-)")
+ ('stopped "-]"))
+ 'face 'emus-cursor)
+ (propertize " " 'face 'default))
+ (propertize (format " %s" title)
+ 'face (if is-current
+ 'emus-track-current
+ 'emus-track)))
+ 'action #'emus--click-track
+ 'follow-link t
+ 'help-echo help-str
+ 'emus-track track)
+ (insert (propertize "\n"
+ 'face (if is-current
+ 'emus-track-current
+ 'emus-track))))))
+
+(defun emus--update-track (track)
+ "Rerender entry for TRACK in emus browser buffer.
+Used to update browser display when `emus-current-track' and/or `emus-state' changes."
+ (let ((track-pos (emus-track-browser-pos track)))
+ (when (and (get-buffer "*emus*")
+ (emus-track-browser-pos track))
+ (with-current-buffer "*emus*"
+ (let ((inhibit-read-only t)
+ (old-point (point)))
+ (goto-char track-pos)
+ (search-forward "\n")
+ (delete-region track-pos (point))
+ (goto-char track-pos)
+ (emus--insert-track track)
+ (goto-char old-point))))))
+
+(defun emus--render-tracks ()
+ "Render all library tracks in emus browser buffer."
+ (with-current-buffer "*emus*"
+ (let ((inhibit-read-only t)
+ (old-pos (point)))
+ (erase-buffer)
+ (goto-char (point-min))
+ (let ((prev-track nil))
+ (dolist (track emus-tracks)
+ (emus--insert-track track prev-track (not prev-track))
+ (setq prev-track track)))
+ (goto-char old-pos))))
+
+(defun emus--click-track (button)
+ "Begin playback of track indicated by BUTTON."
+ (emus-play-track (button-get button 'emus-track))
+ (emus-display-status))
+
+(defun emus-goto-current ()
+ "Move point to the current track in the browser buffer, if available."
+ (interactive)
+ (when (and (get-buffer "*emus*")
+ emus-current-track)
+ (with-current-buffer "*emus*"
+ (goto-char (emus-track-browser-pos emus-current-track)))))
+
+(defun emus-browse ()
+ "Switch to *emus* audio library browser."
+ (interactive)
+ (emus--with-library
+ (pop-to-buffer "*emus*")
+ (emus-browser-mode)
+ (emus--render-tracks)
+ (emus-goto-current)))
+
+(defun emus-refresh ()
+ "Refresh the emus library."
+ (interactive)
+ (emus-stop)
+ (setq emus-tracks nil)
+ (emus-browse))
+
+
+;;; Playback + status display commands
+;;
+
+(defun emus-playpause-status ()
+ "Start, pause or resume playback, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-playpause)
+ (emus-display-status))
+
+(defun emus-stop-status ()
+ "Stop playback, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-stop)
+ (emus-display-status))
+
+(defun emus-volume-up-status ()
+ "Increase volume by 10%, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-volume-up)
+ (emus-display-status))
+
+(defun emus-volume-down-status ()
+ "Decrease volume by 10%, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-volume-down)
+ (emus-display-status))
+
+(defun emus-play-next-status ()
+ "Play next track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-play-next)
+ (emus-display-status))
+
+(defun emus-play-prev-status ()
+ "Play previous track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-play-prev)
+ (emus-display-status))
+
+(defun emus-play-next-album-status ()
+ "Play first track of next album, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-play-next-album)
+ (emus-display-status))
+
+(defun emus-play-prev-album-status ()
+ "Play last track of previous album, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-play-prev-album)
+ (emus-display-status))
+
+(defun emus-jump-10s-forward-status ()
+ "Jump 10s forward in current track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-jump-10s-forward)
+ (emus-display-status))
+
+(defun emus-jump-10s-backward-status ()
+ "Jump 10s backward in current track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-jump-10s-backward)
+ (emus-display-status))
+
+(defun emus-goto-current-status ()
+ "Move point to the current track, then display the emus status in the minibuffer."
+ (interactive)
+ (emus-goto-current)
+ (emus-display-status))
+
+(defun emus-refresh-status ()
+ "Refresh the emus library, then display the emus status in the minibuffer."