aboutsummaryrefslogtreecommitdiff
path: root/emms-tag-tracktag.el
blob: 4ffabb0fddd8102e79de9f628129fc26735df375 (plain) (blame)
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
;;; emms-tag-tracktag.el --- EMMS interface for audiotools tracktag  -*- lexical-binding: t; -*-

;; Copyright (C) 2021  Free Software Foundation, Inc.

;; Author: Grant Shoshin Shangreaux <grant@churls.world>
;; Keywords:

;; 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:

;; Provides a wrapper for audiotools tracktag executable
;; http://audiotools.sourceforge.net/tracktag.html
;; Given an EMMS TRACK structure, it will map the emms-info fields onto
;; arguments for tracktag. Then it calls the tracktag process to write the
;; info as metadata tags on the track's associated file.

;;; Code:

(require 'emms)

(defvar emms-tag-tracktag--info-fields
  '((info-artist . artist)
    (info-composer . composer)
    (info-performer . performer)
    (info-title . name)
    (info-album . album)
    (info-tracknumber . number)
    (info-discnumber . album-number)
    (info-year . year)
    (info-date . date)
    (info-note . comment))
  "An alist mapping info-* fields to tracktag fields.")

(defvar emms-tag-tracktag-log-buffer "*EMMS-LOG*"
  "Name of emms-tag-tracktag's log buffer.
Defaults to the same value as emms-tag-editor-log-buffer")

(defun emms-tag-tracktag--map-track-info (track)
  (seq-filter (lambda (cell) (cdr cell))
              (mapcar (lambda (pair)
                        (cons (cdr pair) (emms-track-get track (car pair))))
                      emms-tag-tracktag--info-fields)))

(defun emms-tag-tracktag--build-args (track)
  (flatten-list
   (append
    (mapcar (lambda (pair)
              (let ((tag (car pair)) (value (cdr pair)))
                (when value
                  ;; if we've deleted a tag value in the editor, remove the tag from file metadata.
                  (if (string-equal "" value) (concat "--remove-" (format "%s" tag))
                    (concat "--" (format "%s" tag) "=" value)))))
            (emms-tag-tracktag--map-track-info track))
    (list (emms-track-name track)))))

(defun emms-tag-tracktag-file (track)
  (apply #'call-process
   "tracktag" nil
   (get-buffer-create emms-tag-tracktag-log-buffer)
   nil
   "-Vdebug"
   (emms-tag-tracktag--build-args track)))

(provide 'emms-tag-tracktag)
;;; emms-tag-tracktag.el ends here