1 ;;; emus.el --- Simple mp3 player -*- lexical-binding:t -*-
3 ;; Copyright (C) 2019 Tim Vaughan
5 ;; Author: Tim Vaughan <timv@ughan.xyz>
6 ;; Created: 8 December 2019
8 ;; Keywords: multimedia
9 ;; Homepage: http://thelambdalab.xy/emus
10 ;; Package-Requires: ((emacs "26"))
12 ;; This file is not part of GNU Emacs.
14 ;; This program is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this file. If not, see <http://www.gnu.org/licenses/>.
29 ;; This is a simple package for playing audio from a local directory
30 ;; tree of mp3 files. It uses the program mpg123 as its back-end.
31 ;; Currently the library is loaded completely every time emus starts.
48 "Simple music player for Emacs."
51 (defcustom emus-directory "~/Music/"
52 "Directory containing audio files for emus."
55 (defcustom emus-mpg123-program "mpg123"
56 "Name of (and, optionally, path to) mpg123 binary."
60 '((((background dark)) :inherit font-lock-string-face :background "#222" :extend t)
61 (t :inherit font-lock-string-face :background "#ddd" :extend t))
62 "Face used for artist names in browser.")
65 '((((background dark)) :inherit font-lock-constant-face :background "#111" :extend t)
66 (t :inherit font-lock-constant-face :background "#eee" :extend t))
67 "Face used for album names in browser.")
70 '((t :inherit font-lock-keyword-face))
71 "Face used for track titles in browser.")
73 (defface emus-track-current
74 '((t :inherit font-lock-keyword-face :inverse-video t :extend t))
75 "Face used for track titles in browser.")
79 "Face used for current track cursor")
84 (defvar emus--proc-in-use nil
85 "If non-nil, disables `emus-send-cmd'.
86 Used to prevent commands from interfering with library construction.")
88 (defvar emus-tracks nil
89 "Emus audio library.")
91 (defvar emus-current-track nil
92 "Currently-selected emus track.")
94 (defvar emus-state 'stopped
95 "Current playback state.")
97 (defvar emus-continuous-playback t
98 "If non-nil, emus will automatically play the next track when the current track is finished.")
100 (defvar emus-current-volume 100
101 "The current playback volume.")
108 (defun emus-get-process ()
109 "Return current or new mpg123 process."
110 (let* ((emus-process-raw (get-process "emus-process"))
111 (emus-process (if emus-process-raw
112 (if (process-live-p emus-process-raw)
114 (kill-process emus-process-raw)
119 (make-process :name "emus-process"
120 :command `(,emus-mpg123-program "-R"))))
121 (set-process-query-on-exit-flag proc nil)
122 (process-send-string proc "silence\n")
125 (defun emus--send-cmd-raw (cmd &rest args)
126 "Send a command CMD with args ARGS to the mpg123 process.
127 This procedure does not respect `emus--proc-in-use' and thus should only
128 be used by `emus--load-library'."
129 (process-send-string (emus-get-process)
131 (seq-reduce (lambda (s1 s2) (concat s1 " " s2)) args cmd)
134 (defun emus-send-cmd (cmd &rest args)
135 "Send a command CMD with args ARGS to the mpg123 process."
136 (unless emus--proc-in-use
137 (apply #'emus--send-cmd-raw cmd args)))
139 (defun emus-kill-process ()
140 "Kill any existing mpg123 process."
141 (let ((emus-process (get-process "emus-process")))
143 (kill-process emus-process))))
145 (defun emus-restart ()
146 "Restart emus, regardless of current state."
149 (setq emus-state 'stopped
150 emus--proc-in-use nil
156 (defun emus-get-audio-files ()
157 "Get all mp3 files in main emus directory."
160 (directory-files-recursively emus-directory ".*\\.mp3")))
162 (defun emus-get-playlist-files ()
163 "Get all m3u files in the main emus music directory."
166 (directory-files-recursively emus-directory ".*\\.m3u")))
168 (defun emus-make-track (artist album title filename &optional pos)
169 "Create an object representing an emus track.
170 ARTIST, ALBUM and TITLE are used to describe the track, FILENAME
171 refers to the mp3 file containing the track. If non-nil, POS
172 specifies the position of the record representing this track in the
173 emus browser buffer."
174 (list artist album title filename pos))
176 (defun emus-track-artist (track)
177 "The artist corresponding to TRACK."
180 (defun emus-track-album (track)
181 "The album corresponding to TRACK."
184 (defun emus-track-title (track)
185 "The title of TRACK."
188 (defun emus-track-file (track)
189 "The mp3 file corresponding to TRACK."
192 (defun emus-track-browser-pos (track)
193 "The location of the browser buffer record corresponding to TRACK."
196 (defun emus-set-track-browser-pos (track pos)
197 "Set the location of the browser buffer record corresponding to TRACK to POS."
198 (setf (seq-elt track 4) pos))
200 (defun emus--load-library (then)
201 "Initialize the emus track library.
202 Once the library is initialized, the function THEN is called."
203 (unless emus--proc-in-use
204 (setq emus--proc-in-use t)
206 (setq emus-state 'stopped)
207 (setq emus-tracks nil)
208 (let ((proc (emus-get-process))
210 (filenames (emus-get-audio-files)))
211 (set-process-filter proc (lambda (proc string)
212 (setq tagstr (concat tagstr string))
213 (when (string-suffix-p "@P 1\n" string)
214 (add-to-list 'emus-tracks
215 (emus--make-track-from-tagstr (car filenames)
218 (setq filenames (cdr filenames))
220 (emus--send-cmd-raw "lp" (car filenames))
221 (set-process-filter proc nil)
222 (setq emus-tracks (reverse emus-tracks))
224 (emus--add-tracks-from-playlist-files)
225 (unless emus-current-track
226 (setq emus-current-track (car emus-tracks)))
229 (setq emus--proc-in-use nil)))))
230 (emus--send-cmd-raw "lp" (car filenames)))))
232 (defun emus--dump-tracks ()
235 (defun emus--make-track-from-tagstr (filename tagstr)
236 "Parse TAGSTR to populate the fields of a track corresponding to FILENAME."
240 (dolist (line (split-string tagstr "\n"))
241 (let ((found-artist (elt (split-string line "@I ID3v2.artist:") 1))
242 (found-album (elt (split-string line "@I ID3v2.album:") 1))
243 (found-title (elt (split-string line "@I ID3v2.title:") 1)))
245 (found-artist (setq artist found-artist))
246 (found-album (setq album found-album))
247 (found-title (setq title found-title)))))
248 (emus-make-track artist album title filename)))
250 (defun emus--add-tracks-from-playlist-files ()
252 (dolist (filename (emus-get-playlist-files))
253 (let ((artist "Playlists")
254 (album (file-name-base filename))
256 (lines (split-string (with-temp-buffer
257 (insert-file-contents filename)
261 (pcase (string-trim line)
266 (let display-title (* any))
268 (setq title display-title))
269 ((rx (: string-start (* space) "#"))) ;skip other comments
270 ((rx (let filename (+ any)))
271 (setq tracks (cons (emus-make-track artist album (or title filename) filename)
273 (setq title nil))))))
274 (setq emus-tracks (append emus-tracks (reverse tracks)))))
276 (defun emus--sort-tracks ()
277 "Sort the library tracks according to artist and album.
278 Leaves the track titles unsorted, so they will appear in the order specified
283 (let ((artist1 (emus-track-artist r1))
284 (artist2 (emus-track-artist r2)))
285 (if (string= artist1 artist2)
286 (let ((album1 (emus-track-album r1))
287 (album2 (emus-track-album r2)))
288 (string< album1 album2))
289 (string< artist1 artist2)))))))
291 (defmacro emus--with-library (&rest body)
292 "Evaluate BODY with the library initialized."
294 (unless emus--proc-in-use ,@body)
296 (lambda () ,@body))))
302 (defun emus--suspend-cp ()
303 "Suspend continuous playback."
304 (setq emus-continuous-playback nil))
306 (defun emus--resume-cp ()
307 "Resume continuous playback."
308 (setq emus-continuous-playback t)
309 (set-process-filter (emus-get-process)
310 (lambda (_proc string)
311 (and emus-continuous-playback
312 (eq emus-state 'playing)
313 (string-suffix-p "@P 0\n" string)
316 (defun emus-play-track (track)
317 "Set TRACK as current and start playing."
319 (let ((old-track emus-current-track))
320 (emus-send-cmd "l" (emus-track-file track))
321 (setq emus-state 'playing)
322 (setq emus-current-track track)
323 (emus--update-track old-track)
324 (emus--update-track track)
326 (emus-goto-current))))
328 (defun emus-select-track (track)
329 "Set TRACK as current, but do not start playing."
331 (let ((old-track emus-current-track))
332 (setq emus-state 'stopped)
333 (setq emus-current-track track)
334 (emus--update-track old-track)
335 (emus--update-track track)
338 (emus-goto-current))))
341 "Stop playback of the current track."
344 (setq emus-state 'stopped)
345 (emus--update-track emus-current-track)
346 (emus-send-cmd "s")))
348 (defun emus-playpause ()
349 "Begin playback of the current track.
350 If the track is already playing, pause playback.
351 If the track is currently paused, resume playback."
354 (when emus-current-track
355 (if (eq emus-state 'stopped)
356 (emus-play-track emus-current-track)
359 ((or 'paused 'stopped) (setq emus-state 'playing))
360 ('playing (setq emus-state 'paused)))
361 (unless (eq emus-state 'paused)))
362 (emus--update-track emus-current-track))))
364 (defun emus-set-volume (pct)
365 "Set the playback volume to PCT %."
367 (setq emus-current-volume pct)
368 (emus-send-cmd "v" (number-to-string pct))))
370 (defun emus-volume-increase-by (delta)
371 "Increase the playback volume by DELTA %."
372 (emus-set-volume (max 0 (min 100 (+ emus-current-volume delta)))))
374 (defun emus-volume-up ()
375 "Increase the playback volume by 10%."
377 (emus-volume-increase-by 10))
379 (defun emus-volume-down ()
380 "Decrease the playback volume by 10%."
382 (emus-volume-increase-by -10))
384 (defun emus--play-adjacent-track (&optional prev)
385 "Play the next track in the library, or the previous if PREV is non-nil."
387 (let ((idx (seq-position emus-tracks emus-current-track))
388 (offset (if prev -1 +1)))
390 (let ((next-track (elt emus-tracks (+ idx offset))))
392 (if (eq emus-state 'playing)
393 (emus-play-track next-track)
394 (emus-select-track next-track))
395 (error "Track does not exist")))
396 (error "No track selected")))))
398 (defun emus--play-adjacent-album (&optional prev)
399 "Play the first track of the next album in the library.
400 If PREV is non-nil, plays the last track of the previous album."
402 (let ((idx (seq-position emus-tracks emus-current-track)))
404 (let* ((search-list (if prev
405 (reverse (seq-subseq emus-tracks 0 idx))
406 (seq-subseq emus-tracks (+ idx 1))))
407 (current-album (emus-track-album emus-current-track))
408 (next-track (seq-some (lambda (r)
409 (if (string= (emus-track-album r)
415 (if (eq emus-state 'playing)
416 (emus-play-track next-track)
417 (emus-select-track next-track))
418 (error "Track does not exist")))
419 (error "No track selected")))))
421 (defun emus-play-next ()
422 "Play the next track in the library."
424 (emus--play-adjacent-track))
426 (defun emus-play-prev ()
427 "Play the previous track in the library."
429 (emus--play-adjacent-track t))
431 (defun emus-play-next-album ()
432 "Play the first track of the next album in the library."
434 (emus--play-adjacent-album))
436 (defun emus-play-prev-album ()
437 "Play the last track of the previous album in the library."
439 (emus--play-adjacent-album t))
441 (defun emus-jump (seconds)
442 "Jump forward in current track by SECONDS seconds."
444 (emus-send-cmd "jump" (format "%+ds" seconds))))
446 (defun emus-jump-10s-forward ()
447 "Jump 10 seconds forward in current track."
451 (defun emus-jump-10s-backward ()
452 "Jump 10 seconds backward in current track."
456 (defun emus-display-status ()
457 "Display the current playback status in the minibuffer."
461 (concat "Emus: Volume %d%%"
463 ('stopped " [Stopped]")
464 ('paused " [Paused]")
465 ('playing " [Playing]")
469 (if emus-current-track
470 (format "- %.30s (%.20s)"
471 (emus-track-title emus-current-track)
472 (emus-track-artist emus-current-track))
479 (defun emus--insert-track (track &optional prev-track first)
480 "Insert a button representing TRACK into the current buffer.
482 When provided, PREV-TRACK is used to determine whether to insert additional
483 headers representing the artist or the album title.
485 If non-nil, FIRST indicates that the track is the first in the library
486 and thus requires both artist and album headers."
487 (let* ((artist (emus-track-artist track))
488 (album (emus-track-album track))
489 (title (emus-track-title track))
490 (album-symb (intern (concat artist album)))
491 (help-str (format "mouse-1, RET: Play '%.30s' (%.20s)" title artist))
492 (field (intern album))) ;Allows easy jumping between albums with cursor.
493 (when (or prev-track first)
494 (unless (equal (emus-track-artist prev-track) artist)
496 (propertize artist 'face 'emus-artist)
497 'action #'emus--click-track
502 (insert (propertize "\n"
505 (unless (equal (emus-track-album prev-track) album)
507 (propertize (concat " " album) 'face 'emus-album)
508 'action #'emus--click-track
513 (insert (propertize "\n"
516 (emus-set-track-browser-pos track (point))
517 (let ((is-current (equal track emus-current-track)))
527 (propertize " " 'face 'default))
528 (propertize (format " %s" title)
532 'action #'emus--click-track
536 'invisible album-symb
538 (insert (propertize "\n"
543 'invisible album-symb)))))
545 (defun emus--update-track (track)
546 "Rerender entry for TRACK in emus browser buffer.
547 Used to update browser display when `emus-current-track' and/or `emus-state' changes."
548 (let ((track-pos (emus-track-browser-pos track)))
549 (when (and (get-buffer "*emus*")
550 (emus-track-browser-pos track))
551 (with-current-buffer "*emus*"
552 (let ((inhibit-read-only t)
554 (goto-char track-pos)
555 (search-forward "\n")
556 (delete-region track-pos (point))
557 (goto-char track-pos)
558 (emus--insert-track track)
559 (goto-char old-point))))))
561 (defun emus--render-tracks ()
562 "Render all library tracks in emus browser buffer."
563 (with-current-buffer "*emus*"
564 (let ((inhibit-read-only t)
567 (goto-char (point-min))
568 (let ((prev-track nil))
569 (dolist (track emus-tracks)
570 (emus--insert-track track prev-track (not prev-track))
571 (setq prev-track track)))
572 (goto-char old-pos))))
574 (defun emus--click-track (button)
575 "Begin playback of track indicated by BUTTON."
576 (emus-play-track (button-get button 'emus-track))
577 (emus-display-status))
579 (defun emus-goto-current ()
580 "Move point to the current track in the browser buffer, if available."
582 (when (and (get-buffer "*emus*")
584 (with-current-buffer "*emus*"
585 (goto-char (emus-track-browser-pos emus-current-track)))))
587 (defun emus-browse ()
588 "Switch to *emus* audio library browser."
591 (pop-to-buffer-same-window "*emus*")
593 (emus--render-tracks)
594 (emus-goto-current)))
596 (defun emus-refresh ()
597 "Refresh the emus library."
600 (setq emus-tracks nil)
604 ;;; Playback + status display commands
607 (defun emus-playpause-status ()
608 "Start, pause or resume playback, then display the emus status in the minibuffer."
611 (emus-display-status))
613 (defun emus-stop-status ()
614 "Stop playback, then display the emus status in the minibuffer."
617 (emus-display-status))
619 (defun emus-volume-up-status ()
620 "Increase volume by 10%, then display the emus status in the minibuffer."
623 (emus-display-status))
625 (defun emus-volume-down-status ()
626 "Decrease volume by 10%, then display the emus status in the minibuffer."
629 (emus-display-status))
631 (defun emus-play-next-status ()
632 "Play next track, then display the emus status in the minibuffer."
635 (emus-display-status))
637 (defun emus-play-prev-status ()
638 "Play previous track, then display the emus status in the minibuffer."
641 (emus-display-status))
643 (defun emus-play-next-album-status ()
644 "Play first track of next album, then display the emus status in the minibuffer."
646 (emus-play-next-album)
647 (emus-display-status))
649 (defun emus-play-prev-album-status ()
650 "Play last track of previous album, then display the emus status in the minibuffer."
652 (emus-play-prev-album)
653 (emus-display-status))
655 (defun emus-jump-10s-forward-status ()
656 "Jump 10s forward in current track, then display the emus status in the minibuffer."
658 (emus-jump-10s-forward)
659 (emus-display-status))
661 (defun emus-jump-10s-backward-status ()
662 "Jump 10s backward in current track, then display the emus status in the minibuffer."
664 (emus-jump-10s-backward)
665 (emus-display-status))
667 (defun emus-goto-current-status ()
668 "Move point to the current track, then display the emus status in the minibuffer."
671 (emus-display-status))
673 (defun emus-refresh-status ()
674 "Refresh the emus library, then display the emus status in the minibuffer."
677 (setq emus-tracks nil)
680 (emus-display-status)))
682 (defvar emus-browser-mode-map
683 (let ((map (make-sparse-keymap)))
684 (define-key map (kbd "SPC") 'emus-playpause-status)
685 (define-key map (kbd "o") 'emus-stop-status)
686 (define-key map (kbd "+") 'emus-volume-up-status)
687 (define-key map (kbd "=") 'emus-volume-up-status)
688 (define-key map (kbd "-") 'emus-volume-down-status)
689 (define-key map (kbd "R") 'emus-refresh-status)
690 (define-key map (kbd "n") 'emus-play-next-status)
691 (define-key map (kbd "p") 'emus-play-prev-status)
692 (define-key map (kbd "N") 'emus-play-next-album-status)
693 (define-key map (kbd "P") 'emus-play-prev-album-status)
694 (define-key map (kbd ",") 'emus-jump-10s-backward-status)
695 (define-key map (kbd ".") 'emus-jump-10s-forward-status)
696 (define-key map (kbd "c") 'emus-goto-current-status)
697 (when (fboundp 'evil-define-key*)
698 (evil-define-key* 'motion map
699 (kbd "SPC") 'emus-playpause-status
700 (kbd "o") 'emus-stop-status
701 (kbd "+") 'emus-volume-up-status
702 (kbd "=") 'emus-volume-up-status
703 (kbd "-") 'emus-volume-down-status
704 (kbd "R") 'emus-refresh-status
705 (kbd "n") 'emus-play-next-status
706 (kbd "p") 'emus-play-prev-status
707 (kbd "N") 'emus-play-next-album-status
708 (kbd "P") 'emus-play-prev-album-status
709 (kbd ",") 'emus-jump-10s-backward-status
710 (kbd ".") 'emus-jump-10s-forward-status
711 (kbd "c") 'emus-goto-current-status))
713 "Keymap for emus browser.")
715 (defun emus-restart ()
720 (define-derived-mode emus-browser-mode special-mode "emus-browser"
721 "Major mode for EMUS music player file browser."
722 (setq-local buffer-invisibility-spec nil))
724 (when (fboundp 'evil-set-initial-state)
725 (evil-set-initial-state 'emus-browser-mode 'motion))
727 ;;; emus.el ends here