[Music] Introduce ‘db/play-auto-playlist’

This is the main entry point to play automatically generated playlists (“auto
playlists”).  It is using the value of ‘db/auto-playlist-file-function’ to
generate a list of files to play.  This list if currently played using EMMS, via
‘db/-emms-playlist-from-files’.
This commit is contained in:
Daniel - 2019-06-10 14:11:52 +02:00
parent bc41e3c5be
commit 9e075d6e0c
No known key found for this signature in database
GPG Key ID: 1C7071A75BB72D64
3 changed files with 57 additions and 43 deletions

12
init.el
View File

@ -2145,10 +2145,14 @@ search commands like `db/helm-shortcuts."
(require 'helm-adaptive))) (require 'helm-adaptive)))
(use-package db-music (use-package db-music
:init (setq db/playlist-play-function #'db/play-playlist-from-git-annex-find) :init (setq db/auto-playlist-file-function
:commands (db/play-playlist-from-cache #'(lambda ()
db/play-playlist-from-git-annex-find (db/playlist-files-from-git-annex-find
db/update-playlist-from-directory)) "--metadata db-playlist=include")))
:commands (db/play-auto-playlist
db/playlist-files-from-cache
db/playlist-files-from-git-annex-find
db/update-playlist-cache-from-directory))
;; * Shells and such ;; * Shells and such

View File

@ -131,7 +131,7 @@ _RET_: ?RET? _M_: ?M?
("-" emms-volume-lower "lower volume") ("-" emms-volume-lower "lower volume")
("+" emms-volume-raise "raise volume") ("+" emms-volume-raise "raise volume")
("M" emms "show playlist") ("M" emms "show playlist")
("P" (call-interactively db/playlist-play-function) ("P" (db/play-auto-playlist)
"Play automatically generated playlist")) "Play automatically generated playlist"))

View File

@ -13,18 +13,22 @@
:group 'convenience :group 'convenience
:tag "db-music") :tag "db-music")
(defcustom db/playlist-play-function #'db/play-playlist-from-cache (defcustom db/auto-playlist-file-function #'db/play-playlist-from-cache
"Function to use to automatically generate playlists" "Function that has to return a list of all music files that
should be included in the auto playlist."
:group 'db-music :group 'db-music
:type 'function) :type 'function)
(defcustom db/playlist nil (defun db/play-auto-playlist ()
"List of songs to include in a random playlist." "Generate playlist using `db/auto-playlist-file-function and
:group 'db-music start playing it.
:type '(alist :value-type (choice (const :tag "Undecided" :undecided)
(const :tag "Include" :include) Current backend is EMMS."
(const :tag "Exclude" :exclude)) (interactive)
:key-type file)) (db/-emms-playlist-from-files (funcall db/auto-playlist-file-function)))
;; Idea: make this customizable, so that we can later switch to another backend
;; if necessary
(defun db/-emms-playlist-from-files (files) (defun db/-emms-playlist-from-files (files)
"Generate EMMS playlist from FILES. "Generate EMMS playlist from FILES.
@ -47,16 +51,23 @@ Shuffle it and start playing it afterwards."
(emms-playlist-select-first) (emms-playlist-select-first)
(emms-start))))) (emms-start)))))
(defun db/play-playlist-from-cache () (defcustom db/playlist nil
"Start playing songs from `db/playlist" "List of songs to include in a random playlist."
(interactive) :group 'db-music
(db/-emms-playlist-from-files :type '(alist :value-type (choice (const :tag "Undecided" :undecided)
(->> db/playlist (const :tag "Include" :include)
(cl-remove-if-not #'(lambda (track) (const :tag "Exclude" :exclude))
(eq (cdr track) :include))) :key-type file))
(mapcar #'car))))
(defun db/play-playlist-from-git-annex-find (match-expression) (defun db/playlist-files-from-cache ()
"Generate files for auto playlist from `db/playlist cache."
(interactive)
(->> db/playlist
(cl-remove-if-not #'(lambda (track)
(eq (cdr track) :include)))
(mapcar #'car)))
(defun db/playlist-files-from-git-annex-find (match-expression)
"Generate playlist from git annex find on MATCH-EXPRESSION. "Generate playlist from git annex find on MATCH-EXPRESSION.
Prompts for MATCH-EXPRESSION when called interactively. Prompts for MATCH-EXPRESSION when called interactively.
@ -65,27 +76,26 @@ are match it. Assumes `emms-source-file-default-directory to be
part of a git-annex repository, and will complain otherwise." part of a git-annex repository, and will complain otherwise."
(interactive "smatch expression: ") (interactive "smatch expression: ")
(let* ((default-directory emms-source-file-default-directory)) (let* ((default-directory emms-source-file-default-directory))
(db/-emms-playlist-from-files (->> (split-string (with-output-to-string
(->> (split-string (with-output-to-string (with-current-buffer standard-output
(with-current-buffer standard-output (let ((return-value (apply #'call-process
(let ((return-value (apply #'call-process "git" nil t nil
"git" nil t nil "annex" "find"
"annex" "find" (split-string match-expression))))
(split-string match-expression)))) (unless (zerop return-value)
(unless (zerop return-value) (error "Call to `git-annex-find failed: %s"
(error "Call to `git-annex-find failed: %s" (buffer-string))))))
(buffer-string)))))) "\n")
"\n") (cl-remove-if-not #'(lambda (path)
(cl-remove-if-not #'(lambda (path) (and (not (string-empty-p path))
(and (not (string-empty-p path)) (file-exists-p path)
(file-exists-p path) (file-readable-p path))))
(file-readable-p path)))) (mapcar #'(lambda (path)
(mapcar #'(lambda (path) (expand-file-name
(expand-file-name path
path emms-source-file-default-directory))))))
emms-source-file-default-directory)))))))
(defun db/update-playlist-from-directory (directory) (defun db/update-playlist-cache-from-directory (directory)
"Recursively traverse DIRECTORY and update `db/playlist. "Recursively traverse DIRECTORY and update `db/playlist.
Files not present `db/playlist but that are found in DIRECTORY Files not present `db/playlist but that are found in DIRECTORY