1 ;;; emus.el --- Simple mp3 player -*- lexical-binding:t -*-
3 ;; Copyright (C) 2019 Tim Vaughan
5 ;; Author: Tim Vaughan <timv@ughan.xyz>
6 ;; Created: 12 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.
47 (defconst emus-version "1.0.0"
48 "Current version of emus.")
55 "Simple music player for Emacs."
58 (defcustom emus-directory "~/Music/"
59 "Directory containing audio files for emus."
62 (defcustom emus-mpg123-program "mpg123"
63 "Name of (and, optionally, path to) mpg123 binary."
67 '((t :inherit font-lock-keyword-face :background "#333"))
68 "Face used for artist names in browser.")
71 '((t :inherit font-lock-function-name-face :background "#222"))
72 "Face used for album names in browser.")
75 '((t :inherit font-lock-string-face))
76 "Face used for track titles in browser.")
78 (defface emus-track-current
79 '((t :inherit font-lock-string-face :inverse-video t))
80 "Face used for track titles in browser.")
84 "Face used for current track cursor")
89 (defun emus-get-audio-files ()
90 "Get all mp3 files in main emus directory."
91 (directory-files-recursively emus-directory ".*\\.mp3"))
93 (defvar emus-records nil
94 "Emus audio library.")
96 (defun emus-make-record (artist album title filename &optional pos)
97 (vector artist album title filename pos))
99 (defun emus-record-artist (record)
102 (defun emus-record-album (record)
105 (defun emus-record-title (record)
108 (defun emus-record-file (record)
111 (defun emus-record-browser-pos (record)
114 (defun emus-set-record-browser-pos (record pos)
117 (defun emus--load-library (then)
119 (setq emus-state 'stopped)
120 (let ((proc (emus-get-process))
122 (filenames (emus-get-audio-files)))
123 (setq emus-records nil)
124 (set-process-filter proc (lambda (proc string)
125 (setq tagstr (concat tagstr string))
126 (when (string-suffix-p "@P 1\n" string)
127 (add-to-list 'emus-records
128 (emus--make-record-from-tagstr (car filenames)
131 (setq filenames (cdr filenames))
133 (emus-send-cmd "lp" (car filenames))
134 (set-process-filter proc nil)
135 (setq emus-records (reverse emus-records))
137 (unless emus-current-record
138 (setq emus-current-record (car emus-records)))
140 ;; (emus-render-records)
141 (emus--resume-cp)))))
142 (emus-send-cmd "lp" (car filenames))))
144 (defun emus--make-record-from-tagstr (filename tagstr)
148 (dolist (line (split-string tagstr "\n"))
149 (let ((found-artist (elt (split-string line "@I ID3v2.artist:") 1))
150 (found-album (elt (split-string line "@I ID3v2.album:") 1))
151 (found-title (elt (split-string line "@I ID3v2.title:") 1)))
153 (found-artist (setq artist found-artist))
154 (found-album (setq album found-album))
155 (found-title (setq title found-title)))))
156 (emus-make-record artist album title filename nil)))
158 (defun emus--sort-records ()
161 (let ((artist1 (emus-record-artist r1))
162 (artist2 (emus-record-artist r2)))
163 (if (string= artist1 artist2)
164 (let ((album1 (emus-record-album r1))
165 (album2 (emus-record-album r2)))
166 (string< album1 album2))
167 (string< artist1 artist2))))))
169 (defmacro emus--with-library (&rest args)
173 (lambda () ,@args))))
178 (defvar emus-proc-in-use nil)
180 (defun emus-get-process ()
181 "Return current or new mpg123 process."
182 (let* ((emus-process-raw (get-process "emus-process"))
183 (emus-process (if emus-process-raw
184 (if (process-live-p emus-process-raw)
186 (kill-process emus-process-raw)
191 (make-process :name "emus-process"
192 ;; :buffer (get-buffer-create "*emus-process*")
193 :command `(,emus-mpg123-program "-R"))))
194 (set-process-query-on-exit-flag proc nil)
195 (process-send-string proc "silence\n")
199 (defun emus-send-cmd (cmd &rest args)
200 (process-send-string (emus-get-process)
202 (seq-reduce (lambda (s1 s2) (concat s1 " " s2)) args cmd)
205 (defun emus-send-and-process (respfun predfun cmd &rest args)
207 (set-process-filter (emus-get-process)
208 (lambda (proc string)
209 (setq respstr (concat respstr string))
210 (when (funcall predfun respstr)
211 (set-process-filter proc nil)
212 (funcall respfun respstr))))
213 (apply #'emus-send-cmd cmd args)))
219 (defvar emus-current-record nil)
220 (defvar emus-state 'stopped)
221 (defvar emus-continuous-playback t)
223 (defun emus--suspend-cp ()
224 (setq emus-continuous-playback nil))
226 (defun emus--resume-cp ()
227 (setq emus-continuous-playback t)
228 (set-process-filter (emus-get-process)
229 (lambda (proc string)
230 (and emus-continuous-playback
231 (eq emus-state 'playing)
232 (string-suffix-p "@P 0\n" string)
235 (defun emus-play-record (record)
236 "Set RECORD as current and start playing."
238 (let ((old-record emus-current-record))
239 (emus-send-cmd "l" (emus-record-file record))
240 (setq emus-state 'playing)
241 (setq emus-current-record record)
242 (emus--update-record old-record)
243 (emus--update-record record)
246 (defun emus-select-record (record)
247 "Set RECORD as current, but do not start playing."
249 (let ((old-record emus-current-record))
250 (setq emus-state 'stopped)
251 (setq emus-current-record record)
252 (emus--update-record old-record)
253 (emus--update-record record)
258 "Stop playback of the current record."
261 (setq emus-state 'stopped)
262 (emus--update-record emus-current-record)
263 (emus-send-cmd "s")))
265 (defun emus-playpause ()
268 (when emus-current-record
269 (if (eq emus-state 'stopped)
270 (emus-play-record emus-current-record)
273 ((or 'paused 'stopped) (setq emus-state 'playing))
274 ('playing (setq emus-state 'paused)))
275 (unless (eq emus-state 'paused)))
276 (emus--update-record emus-current-record))))
278 (defvar emus-current-volume 100)
280 (defun emus-set-volume (pct)
282 (setq emus-current-volume pct)
283 (emus-send-cmd "v" (number-to-string pct))))
285 (defun emus-volume-increase-by (delta)
286 (emus-set-volume (max 0 (min 100 (+ emus-current-volume delta)))))
288 (defun emus-volume-up ()
290 (emus-volume-increase-by 10))
292 (defun emus-volume-down ()
294 (emus-volume-increase-by -10))
296 (defun emus--play-adjacent-track (&optional prev)
298 (let ((idx (seq-position emus-records emus-current-record))
299 (offset (if prev -1 +1)))
301 (let ((next-record (elt emus-records (+ idx offset))))
303 (if (eq emus-state 'playing)
304 (emus-play-record next-record)
305 (emus-select-record next-record))
306 (error "Track does not exist")))
307 (error "No track selected")))))
309 (defun emus--play-adjacent-album (&optional prev)
311 (let ((idx (seq-position emus-records emus-current-record)))
313 (let* ((search-list (if prev
314 (reverse (seq-subseq emus-records 0 idx))
315 (seq-subseq emus-records (+ idx 1))))
316 (current-album (emus-record-album emus-current-record))
317 (next-record (seq-some (lambda (r)
318 (if (string= (emus-record-album r)
324 (if (eq emus-state 'playing)
325 (emus-play-record next-record)
326 (emus-select-record next-record))
327 (error "Track does not exist")))
328 (error "No track selected")))))
330 (defun emus-play-next ()
332 (emus--play-adjacent-track))
334 (defun emus-play-prev ()
336 (emus--play-adjacent-track t))
338 (defun emus-play-next-album ()
340 (emus--play-adjacent-album))
342 (defun emus-play-prev-album ()
344 (emus--play-adjacent-album t))
346 (defun emus-display-status ()
350 (concat "Emus: Volume %d%%"
352 ('stopped " [Stopped]")
353 ('paused " [Paused]")
354 ('playing " [Playing]")
356 (if emus-current-record
357 (format " - %.30s (%.20s)"
358 (emus-record-title emus-current-record)
359 (emus-record-artist emus-current-record))
361 emus-current-volume)))
367 (defun emus--insert-record (record &optional prev-record first)
368 (let* ((artist (emus-record-artist record))
369 (album (emus-record-album record))
370 (title (emus-record-title record))
371 (help-str (format "mouse-1, RET: Play '%.30s' (%.20s)" title artist)))
372 (when (or prev-record first)
373 (unless (equal (emus-record-artist prev-record) artist)
375 (propertize artist 'face 'emus-artist)
376 'action #'emus--click-record
380 (insert (propertize "\n" 'face 'emus-artist)))
381 (unless (equal (emus-record-album prev-record) album)
383 (propertize (concat " " album) 'face 'emus-album)
384 'action #'emus--click-record
388 (insert (propertize "\n" 'face 'emus-album))))
389 (emus-set-record-browser-pos record (point))
390 (let ((is-current (equal record emus-current-record)))
400 (propertize " " 'face 'default))
401 (propertize (format " %s" title)
405 'action #'emus--click-record
409 (insert (propertize "\n"
414 (defun emus--update-record (record)
415 (let ((record-pos (emus-record-browser-pos record)))
416 (when (and (get-buffer "*emus*")
417 (emus-record-browser-pos record))
418 (with-current-buffer "*emus*"
419 (let ((inhibit-read-only t)
421 (goto-char record-pos)
422 (search-forward "\n")
423 (delete-region record-pos (point))
424 (goto-char record-pos)
425 (emus--insert-record record)
426 (goto-char old-point))))))
428 (defun emus--render-records ()
429 (with-current-buffer "*emus*"
430 (let ((inhibit-read-only t)
433 (goto-char (point-min))
434 (let ((prev-record nil))
435 (dolist (record emus-records)
436 (emus--insert-record record prev-record (not prev-record))
437 (setq prev-record record)))
438 (goto-char old-pos))))
440 (defun emus--click-record (button)
441 (emus-play-record (button-get button 'emus-record))
442 (emus-display-status))
444 (defun emus-centre-current ()
446 (when (get-buffer "*emus*")
447 (switch-to-buffer "*emus*")
448 (when emus-current-record
449 (goto-char (emus-record-browser-pos emus-current-record))
452 (defun emus-browse ()
453 "Switch to *emus* audio library browser."
456 (switch-to-buffer "*emus*")
458 (emus--render-records)
459 (emus-centre-current)))
461 (defun emus-refresh ()
464 (setq emus-records nil)
467 (defun emus-playpause-status () (interactive) (emus-playpause) (emus-display-status))
468 (defun emus-stop-status () (interactive) (emus-stop) (emus-display-status))
469 (defun emus-volume-up-status () (interactive) (emus-volume-up) (emus-display-status))
470 (defun emus-volume-down-status () (interactive) (emus-volume-down) (emus-display-status))
471 (defun emus-refresh-status () (interactive) (emus-refresh) (emus-display-status))
472 (defun emus-play-next-status () (interactive) (emus-play-next) (emus-display-status))
473 (defun emus-play-prev-status () (interactive) (emus-play-prev) (emus-display-status))
474 (defun emus-play-next-album-status () (interactive) (emus-play-next-album) (emus-display-status))
475 (defun emus-play-prev-album-status () (interactive) (emus-play-prev-album) (emus-display-status))
476 (defun emus-centre-current-status () (interactive) (emus-centre-current) (emus-display-status))
478 (defvar emus-browser-mode-map
479 (let ((map (make-sparse-keymap)))
480 (define-key map (kbd "SPC") 'emus-playpause-status)
481 (define-key map (kbd "o") 'emus-stop-status)
482 (define-key map (kbd "+") 'emus-volume-up-status)
483 (define-key map (kbd "=") 'emus-volume-up-status)
484 (define-key map (kbd "-") 'emus-volume-down-status)
485 (define-key map (kbd "R") 'emus-refresh-status)
486 (define-key map (kbd "n") 'emus-play-next-status)
487 (define-key map (kbd "p") 'emus-play-prev-status)
488 (define-key map (kbd "N") 'emus-play-next-album-status)
489 (define-key map (kbd "P") 'emus-play-prev-album-status)
490 (define-key map (kbd "c") 'emus-centre-current-status)
491 (when (fboundp 'evil-define-key*)
492 (evil-define-key* 'motion map
493 (kbd "SPC") 'emus-playpause-status
494 (kbd "o") 'emus-stop-status
495 (kbd "+") 'emus-volume-up-status
496 (kbd "=") 'emus-volume-up-status
497 (kbd "-") 'emus-volume-down-status
498 (kbd "R") 'emus-refresh-status
499 (kbd "n") 'emus-play-next-status
500 (kbd "p") 'emus-play-prev-status
501 (kbd "N") 'emus-play-next-album-status
502 (kbd "P") 'emus-play-prev-album-status
503 (kbd "c") 'emus-centre-current-status))
507 (define-derived-mode emus-browser-mode special-mode "emus-browser"
508 "Major mode for EMUS music player.")
510 (when (fboundp 'evil-set-initial-state)
511 (evil-set-initial-state 'emus-browser-mode 'motion))
513 ;;; emus.el ends here