aboutsummaryrefslogtreecommitdiff
path: root/emms-info-libtag.el
blob: 97d25c20050f453bc60e0c0f861a3ae7786d57a6 (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
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
;;; emms-info-libtag.el --- Info-method for EMMS using libtag  -*- lexical-binding: t; -*-

;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008,
;;   2009 Free Software Foundation, Inc.

;; Authors: Ulrik Jensen <terryp@daimi.au.dk>
;;          Jorgen Schäfer <forcer@forcix.cx>
;; Keywords:

;; This file is part of EMMS.

;; EMMS 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, or (at your option)
;; any later version.

;; EMMS 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 EMMS; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; This code has been adapted from code found in mp3player.el, written
;; by Jean-Philippe Theberge (jphiltheberge@videotron.ca), Mario
;; Domgoergen (kanaldrache@gmx.de) and Jorgen Schäfer
;; <forcer@forcix.cx>

;; To activate this method for getting info, use something like:

;; (require 'emms-info-libtag)
;; (add-to-list 'emms-info-functions 'emms-info-libtag)

;; Note that you should remove emms-info-mp3info and emms-info-ogginfo
;; from the emms-info-functions list if you want to avoid
;; conflicts. For example, to set libtag as your exclusive info
;; provider:

;; (setq emms-info-functions '(emms-info-libtag))

;; You may have to compile the program from source.
;; Make sure that you have libtag installed.
;; In the EMMS source directory do
;;
;;    make emms-print-metadata
;;
;; and copy src/emms-print-metadata to your PATH.

;; If compilation fails and libtag is installed, you may have to
;; change the line
;;
;;    #include <tag_c.h>
;;
;; to the correction location, e.g.
;;
;;    #include <taglib/tag_c.h>

;;; Code:

(require 'emms-info)

(defgroup emms-info-libtag nil
  "Options for EMMS."
  :group 'emms-info)

(defvar emms-info-libtag-coding-system 'utf-8)

(defcustom emms-info-libtag-program-name "emms-print-metadata"
  "Name of emms-info-libtag program."
  :type '(string)
  :group 'emms-info-libtag)

(defcustom emms-info-libtag-known-extensions
  (regexp-opt '("mp3" "mp4" "m4a" "ogg" "flac" "spx" "wma"))
  "Regexp of known extensions compatible with `emms-info-libtag-program-name'.

Case is irrelevant."
  :type '(string)
  :group 'emms-info-libtag)

(defun emms-info-libtag (track)
  (when (and (eq 'file (emms-track-type track))
             (let ((case-fold-search t))
               (string-match
                emms-info-libtag-known-extensions
                (emms-track-name track))))
    (with-temp-buffer
      (when (zerop
             (let ((coding-system-for-read 'utf-8))
               (call-process emms-info-libtag-program-name
                             nil '(t nil) nil
                             (emms-track-name track))))
        (goto-char (point-min))
        ;; Crush the trailing whitespace
        (while (re-search-forward "[[:space:]]+$" nil t)
          (replace-match "" nil nil))
        (goto-char (point-min))
        (while (looking-at "^\\([^=\n]+\\)=\\(.*\\)$")
          (let ((name (intern-soft (match-string 1)))
                (value (match-string 2)))
            (when (> (length value)
                     0)
              (emms-track-set track
                              name
                              (if (eq name 'info-playing-time)
                                  (string-to-number value)
                                value))))
          (forward-line 1))))))

(provide 'emms-info-libtag)
;;; emms-info-libtag.el ends here