1 ;;; emus.el --- Simple music player for Emacs. -*- lexical-binding:t -*-
3 ;; Author: Tim Vaughan <timv@ughan.xyz>
5 ;; Keywords: multimedia
6 ;; URL: https://thelambdalab.xyz/emus
10 ;; This is a simple package for playing audio from a local library
19 "Simple music player for Emacs."
22 (defcustom emus-directory "~/Music/"
23 "Directory containing audio files for emus."
26 (defcustom emus-mpg123-program "mpg123"
27 "Name of (and, optionally, path to) mpg123 binary."
31 '((t :inherit font-lock-keyword-face))
32 "Face used for artist names in browser.")
35 '((t :inherit font-lock-function-name-face))
36 "Face used for album names in browser.")
39 '((t :inherit font-lock-string-face))
40 "Face used for track titles in browser.")
45 (defun emus-get-audio-files ()
46 "Get all mp3 files in main emus directory."
47 (directory-files-recursively emus-directory ".*\\.mp3"))
49 (defvar emus-records nil
50 "Emus audio library.")
52 (defun emus-make-record (filename tagstr)
56 (dolist (line (split-string tagstr "\n"))
57 (let ((found-artist (elt (split-string line "@I ID3v2.artist:") 1))
58 (found-album (elt (split-string line "@I ID3v2.album:") 1))
59 (found-title (elt (split-string line "@I ID3v2.title:") 1)))
61 (found-artist (setq artist found-artist))
62 (found-album (setq album found-album))
63 (found-title (setq title found-title)))))
64 (vector artist album title filename nil)))
66 (defun emus-record-artist (record)
69 (defun emus-record-album (record)
72 (defun emus-record-title (record)
75 (defun emus-record-file (record)
78 (defun emus-record-browser-pos (record)
81 (defun emus-set-record-browser-pos (record pos)
84 (defun emus-update-records ()
87 (setq emus-state 'stopped)
88 (let ((proc (emus-get-process))
90 (filenames (emus-get-audio-files)))
91 (setq emus-records nil)
92 (set-process-filter proc (lambda (proc string)
93 (setq tagstr (concat tagstr string))
94 (when (string-suffix-p "@P 1\n" string)
95 (add-to-list 'emus-records
96 (emus-make-record (car filenames)
99 (setq filenames (cdr filenames))
101 (emus-send-cmd "lp" (car filenames))
102 (set-process-filter proc nil)
103 (setq emus-records (reverse emus-records))
105 (emus-render-records)
106 (emus--resume-cp)))))
107 (emus-send-cmd "lp" (car filenames))))
109 (defun emus-sort-records ()
112 (let ((artist1 (emus-record-artist r1))
113 (artist2 (emus-record-artist r2)))
114 (if (string= artist1 artist2)
115 (let ((album1 (emus-record-album r1))
116 (album2 (emus-record-album r2)))
117 (string< album1 album2))
118 (string< artist1 artist2))))))
123 (defvar emus-proc-in-use nil)
125 (defun emus-get-process ()
126 "Return current or new mpg123 process."
127 (let* ((emus-process-raw (get-process "emus-process"))
128 (emus-process (if emus-process-raw
129 (if (process-live-p emus-process-raw)
131 (kill-process emus-process-raw)
136 (make-process :name "emus-process"
137 ;; :buffer (get-buffer-create "*emus-process*")
138 :command `(,emus-mpg123-program "-R"))))
139 (process-send-string proc "silence\n")
143 (defun emus-send-cmd (cmd &rest args)
144 (process-send-string (emus-get-process)
146 (seq-reduce (lambda (s1 s2) (concat s1 " " s2)) args cmd)
149 (defun emus-send-and-process (respfun predfun cmd &rest args)
151 (set-process-filter (emus-get-process)
152 (lambda (proc string)
153 (setq respstr (concat respstr string))
154 (when (funcall predfun respstr)
155 (set-process-filter proc nil)
156 (funcall respfun respstr))))
157 (apply #'emus-send-cmd cmd args)))
163 (defvar emus-current-record nil)
164 (defvar emus-state 'stopped)
165 (defvar emus-continuous-playback t)
167 (defun emus--suspend-cp ()
168 (setq emus-continuous-playback nil))
170 (defun emus--resume-cp ()
171 (setq emus-continuous-playback t)
172 (set-process-filter (emus-get-process)
173 (lambda (proc string)
174 (and emus-continuous-playback
175 (eq emus-state 'playing)
176 (string-suffix-p "@P 0\n" string)
179 (defun emus-play-record (record)
180 (let ((old-record emus-current-record))
181 (emus-send-cmd "l" (emus-record-file record))
182 (setq emus-state 'playing)
183 (setq emus-current-record record)
184 (emus-update-record old-record)
185 (emus-update-record record)
190 (setq emus-state 'stopped)
191 (emus-update-record emus-current-record)
194 (defun emus-playpause ()
196 (when emus-current-record
197 (if (eq emus-state 'stopped)
198 (emus-play-record emus-current-record)
201 ((or 'paused 'stopped) (setq emus-state 'playing))
202 ('playing (setq emus-state 'paused)))
203 (unless (eq emus-state 'paused)))
204 (emus-update-record emus-current-record)))
206 (defun emus-set-volume (pct)
207 (emus-send-cmd "v" (number-to-string pct)))
209 (defvar emus-current-volume 100)
211 (defun emus-volume-delta (delta)
212 (setq emus-current-volume (max 0 (min 100 (+ emus-current-volume delta))))
213 (emus-set-volume emus-current-volume))
215 (defun emus-volume-up ()
217 (emus-volume-delta 10))
219 (defun emus-volume-down ()
221 (emus-volume-delta -10))
223 (defun emus--play-nearby (offset)
224 (let ((idx (seq-position emus-records emus-current-record)))
226 (let ((next-record (elt emus-records (+ idx offset))))
228 (emus-play-record next-record)
229 (error "Track does not exist")))
230 (error "No track is currently selected."))))
232 (defun emus-play-next ()
234 (emus--play-nearby 1))
236 (defun emus-play-prev ()
238 (emus--play-nearby -1))
240 (defun emus-display-status ()
243 (concat "Emus: Volume %d%%"
245 ('stopped " [Stopped]")
246 ('paused " [Paused]")
247 ('playing " [Playing]")
249 (if emus-current-record
250 (format " - %.30s (%.20s)"
251 (emus-record-title emus-current-record)
252 (emus-record-artist emus-current-record))
254 emus-current-volume))
260 (defun emus-insert-record (record &optional prev-record first)
261 (let ((artist (emus-record-artist record))
262 (album (emus-record-album record))
263 (title (emus-record-title record)))
264 (when (or prev-record first)
265 (unless (equal (emus-record-artist prev-record) artist)
267 (propertize artist 'face 'emus-artist)
268 'action #'emus-click-record
272 (unless (equal (emus-record-album prev-record) album)
274 (propertize (concat " " album) 'face 'emus-album)
275 'action #'emus-click-record
279 (emus-set-record-browser-pos record (point))
280 (let ((is-current (equal record emus-current-record)))
285 ('playing (propertize ">" 'face 'bold))
286 ('paused (propertize ")" 'face 'bold))
287 ('stopped (propertize "]" 'face 'bold)))
288 (propertize " " 'face 'default))
289 (propertize (format " %s" title)
291 'action #'emus-click-record
293 'emus-record record)))
296 (defun emus-update-record (record)
297 (let ((record-pos (emus-record-browser-pos record)))
298 (when (and (get-buffer "*emus*")
299 (emus-record-browser-pos record))
300 (with-current-buffer "*emus*"
301 (let ((inhibit-read-only t)
303 (goto-char record-pos)
304 (search-forward "\n")
305 (delete-region record-pos (point))
306 (goto-char record-pos)
307 (emus-insert-record record)
308 (goto-char old-point))))))
310 (defun emus-render-records ()
311 (save-mark-and-excursion
312 (with-current-buffer "*emus*"
313 (let ((inhibit-read-only t))
315 (goto-char (point-min))
316 (let ((prev-record nil))
317 (dolist (record emus-records)
318 (emus-insert-record record prev-record (not prev-record))
319 (setq prev-record record)))))))
321 (defun emus-click-record (button)
322 (emus-play-record (button-get button 'emus-record)))
324 (defun emus-browse ()
325 "Switch to *emus* audio library browser."
327 (switch-to-buffer "*emus*")
329 (emus-volume emus-current-volume)
331 (emus-render-records)
332 (emus-update-records)))
334 (defvar emus-browser-mode-map
335 (let ((map (make-sparse-keymap)))
336 (define-key map (kbd "SPC") 'emus-playpause)
337 (define-key map (kbd "o") 'emus-stop)
338 (define-key map (kbd "+") 'emus-volume-up)
339 (define-key map (kbd "=") 'emus-volume-up)
340 (define-key map (kbd "-") 'emus-volume-down)
341 (define-key map (kbd "R") 'emus-update-records)
342 (define-key map (kbd "n") 'emus-play-next)
343 (define-key map (kbd "p") 'emus-play-prev)
344 (when (fboundp 'evil-define-key*)
345 (evil-define-key* 'motion map
346 (kbd "SPC") 'emus-playpause
348 (kbd "+") 'emus-volume-up
349 (kbd "=") 'emus-volume-up
350 (kbd "-") 'emus-volume-down
351 (kbd "R") 'emus-update-records
352 (kbd "n") 'emus-play-next
353 (kbd "p") 'emus-play-prev))
357 (define-derived-mode emus-browser-mode special-mode "emus-browser"
358 "Major mode for EMUS music player.")
360 (when (fboundp 'evil-set-initial-state)
361 (evil-set-initial-state 'emus-browser-mode 'motion))
365 ;;; emus.el ends here