-
Notifications
You must be signed in to change notification settings - Fork 9
/
musicy.el
123 lines (99 loc) · 4.57 KB
/
musicy.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
;;; musicy.el --- View a music library in a useful taxonomy -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Free Software Foundation, Inc.
;; Author: Adam Porter <[email protected]>
;; Maintainer: Adam Porter <[email protected]>
;; Keywords: convenience
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This is a sample application using `taxy'. It uses the "mediainfo"
;; program to get metadata of about audio files, but any function
;; could be swapped into its place (e.g. one that retrieved data from
;; MPD).
;;; Code:
;;;; Requirements
(require 'cl-lib)
(require 'taxy)
(require 'taxy-magit-section)
;; Used to avoid repeated calls to "mediainfo" for the same file.
(require 'memoize)
;;;; Variables
(defvar musicy-taxy
(cl-labels ((call-proc (process &rest args)
"Return results of running PROCESS with ARGS."
(declare (indent defun))
(with-temp-buffer
(if (zerop (apply #'call-process process nil t nil
args))
(buffer-substring-no-properties (point-min) (point-max))
(warn "mediainfo failed for: %S" args))))
(mediainfo (file)
(call-proc "mediainfo" file))
(mediainfo-attr (attr file)
(if-let ((info (musicy-mediainfo file)))
(when (string-match
(rx-to-string `(seq (eval ,attr) (1+ blank) ":" (1+ blank) (group (1+ nonl))))
info)
(match-string 1 info))
(format "No info for file: %S" file)))
(genre (file)
(or (mediainfo-attr "Genre" file)
"[unknown genre]"))
(year (file)
(or (when-let (date (or (mediainfo-attr "Recorded date" file)
(mediainfo-attr "Original/Released date" file)
(mediainfo-attr "Year" file)))
(when (string-match (rx (group (1+ digit))) date)
(match-string 1 date)))
"[unknown year]"))
(artist (file)
(mediainfo-attr "Performer" file))
(album (file)
(mediainfo-attr "Album" file))
(track-name (file)
(mediainfo-attr "Track name" file))
(track-number (file)
(mediainfo-attr "Track name/Position" file))
(track-string (file)
(concat
(pcase (track-number file)
((or "-1" 'nil) nil)
(number (format "%s: " number)))
(track-name file))))
(make-taxy
:name "Musicy"
:taxys (list (make-taxy
:name "Genres"
:take (apply-partially #'taxy-take-keyed
(list #'genre #'artist #'year #'album #'track-string)))))))
;;;; Customization
;;;; Commands
(defun musicy (directory)
(interactive (list (read-directory-name "Directory of music files: ")))
(let ((files (directory-files-recursively
directory (rx "." (or "mp3" "ogg") eos))))
(musicy-files files)))
(defun musicy-files (files)
(thread-last musicy-taxy
taxy-emptied
(taxy-fill files)
(taxy-sort #'string< #'taxy-name)
(taxy-sort* #'string< #'taxy-name)
taxy-magit-section-pp))
;;;; Functions
(defmemoize musicy-mediainfo (file)
(with-temp-buffer
(if (zerop (call-process "mediainfo" nil t nil file))
(buffer-substring-no-properties (point-min) (point-max))
(warn "mediainfo failed for: %S" file))))
;;;; Footer
(provide 'musicy)
;;; musicy.el ends here