summaryrefslogtreecommitdiffhomepage
path: root/emms/jao-emms-info-track.el
blob: 92eac7b50cf39e9894bead9fbefe110d9251cd45 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;; jao-emms-info-track.el -- utilities to show tracks

;; Copyright (C) 2009, 2010, 2013, 2017 Jose Antonio Ortega Ruiz

;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
;; Start date: Sat Jul 04, 2009 13:47

;; This file 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 file 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 <http://www.gnu.org/licenses/>.

;;; Code:

(require 'emms)
(require 'emms-tag-editor)
(require 'jao-osd)
(require 'jao-emms)

(defgroup jao-emms-faces nil "Faces"
  :group 'faces
  :group 'jao-emms)

(defface jao-emms-font-lock-album '((t (:foreground "lightgoldenrod2")))
  "Album name in EMMS track message."
  :group 'jao-emms-faces)

(defface jao-emms-font-lock-track '((t (:bold t)))
  "Track number in EMMS track message."
  :group 'jao-emms-faces)

(defface jao-emms-font-lock-title '((t (:foreground "dodgerblue2")))
  "Track title in EMMS track message."
  :group 'jao-emms-faces)

(defface jao-emms-font-lock-artist '((t (:foreground "dodgerblue3")))
  "Artist name in EMMS track message."
  :group 'jao-emms-faces)

(defcustom jao-emms-show-osd-p nil
  "Whether to show osd notices on track change"
  :group 'jao-emms)



(defun jao-emms-info-track-stream (track)
  "Return track info for streams"
  (let ((name (emms-track-name track))
        (title (or (emms-track-get track 'title nil)
                   (car (emms-track-get track 'metadata nil)))))
    (format "♪ %s (%s)" title (if title (emms-track-type track) name))))

(defsubst jao--put-face (str face)
  (put-text-property 0 (length str) 'face face str)
  str)

(defun jao-emms-info-track-file (track &optional lapsed titlesep)
  "Return a description of the current track."
  (let ((no (string-to-number (emms-track-get track 'info-tracknumber "0")))
        (time (emms-track-get track 'info-playing-time))
        (artist (emms-track-get track 'info-artist ""))
        (composer (emms-track-get track 'info-composer nil))
        (title (emms-track-get track 'info-title ""))
        (album (emms-track-get track 'info-album))
        (last-played (or (emms-track-get track 'last-played) '(0 0 0)))
        (play-count (or (emms-track-get track 'play-count) 0)))
    (if (or (not title) (not album))
        (emms-track-simple-description track)
      (format "♪ [%s%s] %s%s%s%s%s%s"
              (if lapsed (format "%02d:%02d/" (/ lapsed 60) (mod lapsed 60)) "")
              (if time (format "%02d:%02d" (/ time 60) (mod time 60)) "")
              (jao--put-face artist 'jao-emms-font-lock-artist)
              (jao--put-face (if composer (format " [%s]" composer) "")
                             'jao-emms-font-lock-artist)
              (jao--put-face (if album (format " (%s)" album) " *")
                             'jao-emms-font-lock-album)
              (or titlesep " ")
              (jao--put-face (if (zerop no) "" (format " %02d · " no))
                             'jao-emms-font-lock-track)
              (jao--put-face title
                             'jao-emms-font-lock-title)))))

(defun jao-emms-info-track-description (track &optional lapsed tsep)
  (if (memq (emms-track-type track) '(streamlist url))
      (jao-emms-info-track-stream track)
    (jao-emms-info-track-file track lapsed tsep)))

(defun jao-emms-toggle-osd ()
  (interactive)
  (setq jao-emms-show-osd-p (not jao-emms-show-osd-p))
  (message "Emms OSD %s" (if jao-emms-show-osd-p "enabled" "disabled")))

(defsubst jao-emms-current-track-str (&optional lapsed tsep)
  (substring-no-properties
   (jao-emms-info-track-description (emms-playlist-current-selected-track)
                                    lapsed tsep)))

(defvar jao-emms-show-icon nil)

(defun jao-emms-show-osd ()
  (interactive)
  (emms-player-mpd-get-playing-time
   nil
   (lambda (v l)
     (let* ((sep "~~~~~")
            (s (substring (jao-emms-current-track-str l sep) 2))
            (cs (split-string s sep)))
       (jao-notify (cadr cs) (car cs) jao-emms-show-icon))
     t)))

(defun jao-emms-show-osd-hook ()
  (interactive)
  (when jao-emms-show-osd-p (jao-emms-show-osd))
  t)

(defun jao-emms-install-i3dv2 ()
  (add-to-list 'emms-tag-editor-tagfile-functions
               '("mp3" "id3v2" ((info-artist      . "a")
                                (info-title       . "t")
                                (info-album       . "A")
                                (info-tracknumber . "T")
                                (info-year        . "y")
                                (info-genre       . "g")
                                (info-composer    . "-TCOM")
                                (info-note        . "c")))))

(defun jao-emms-info-setup (&optional show-osd show-echo-line no-id3)
  (setq emms-track-description-function 'jao-emms-info-track-description)
  (setq jao-emms-show-osd-p show-osd)
  (add-hook 'emms-player-started-hook 'jao-emms-show-osd-hook)
  (unless show-echo-line
    (eval-after-load 'emms-player-mpd
      '(remove-hook 'emms-player-started-hook 'emms-player-mpd-show)))
  (unless no-id3 (jao-emms-install-i3dv2)))

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