aboutsummaryrefslogtreecommitdiff
path: root/emms-score.el
blob: 134d6c4c04bef50804ddea76b2ed7eff19ffc723 (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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
;;; emms-score.el --- Scoring system for mp3player

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

;; Authors: Jean-Philippe Theberge <jphiltheberge@videotron.ca>, Yoni
;;          Rabkin <yonirabkin@member.fsf.org>
;; Keywords: emms, mp3, mpeg, multimedia

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

;; NOTE: This is experimental stuff - comments welcome!  There
;; shouldn't worky anything in that file... scores aren't saved, they
;; even don't have any consequence on playing order and there's just
;; one mood in the moment. But it's a beginning and you can score down
;; or up tracks... :)
;;
;; * How to use scoring in emms
;;
;; When you load emms, you are set to a default mood
;; 'emms-default-mood' A mood is a one word string describing how
;; you feel (like "funny", "tired", "aggresive"...)  Each mood have is
;; own set of scoring rules.
;;
;; You can change your mood with M-x emms-score-change-mood.
;;
;; Every music file start with a default score of 0 the command
;; emms-score-up-playing and emms-score-down-playing modify the
;; score of the file you are curently listening by 1 In addition,
;; skipping a file (with emms-skip) automaticaly score the file
;; down.
;;
;; With scoring on (this mean the variable emms-use-scoring is t),
;; emms will compare the score of the file with your tolerance to
;; decide if it is played or not.
;;
;; The default tolerance level is 0 (or the variable
;; emms-score-min-score).  This mean files with a score of 0 or more will
;; be played and files with a score of -1 or less will be skipped.
;;
;; You can change the tolerance (by 1) with M-x
;; emms-score-lower-tolerance and M-x
;; emms-score-be-more-tolerant

;;; Code:

(require 'emms)

(defvar emms-scores-list nil)
(defvar emms-score-current-mood 'default)
(defvar emms-score-min-score 0)
(defvar emms-score-default-score 0)
(defvar emms-score-hash (make-hash-table :test 'equal))
(defvar emms-score-enabled-p nil
  "If non-nil, emms score is active.")

(defcustom emms-score-file "~/.emms/scores"
  "*Directory to store the score file."
  :type 'directory
  :group 'emms)


;;; User Interfaces

(defun emms-score (arg)
  "Turn on emms-score if prefix argument ARG is a positive integer,
off otherwise."
  (interactive "p")
  (if (and arg (> arg 0))
      (progn
	(setq emms-score-enabled-p t)
        (setq emms-player-next-function 'emms-score-next-noerror)
	(emms-score-load-hash)
	(add-hook 'kill-emacs-hook 'emms-score-save-hash))
    (setq emms-score-enabled-p nil)
    (setq emms-player-next-function 'emms-next-noerror)
    (emms-score-save-hash)
    (remove-hook 'kill-emacs-hook 'emms-score-save-hash)))

;;;###autoload
(defun emms-score-enable ()
  "Turn on emms-score."
  (interactive)
  (emms-score 1)
  (message "emms score enabled"))

;;;###autoload
(defun emms-score-disable ()
  "Turn off emms-score."
  (interactive)
  (emms-score -1)
  (message "emms score disabled"))

;;;###autoload
(defun emms-score-toggle ()
  "Toggle emms-score."
  (interactive)
  (if emms-score-enabled-p
      (emms-score-disable)
    (emms-score-enable)))

(defun emms-score-change-mood (mood)
  "Change the current MOOD.
The score hash is automatically saved."
  (interactive "sMood: ")
  (emms-score-save-hash)
  (setq emms-score-current-mood (intern (downcase mood))))

(defun emms-score-up-playing ()
  (interactive)
  (if emms-player-playing-p
      (emms-score-change-score 1 (emms-score-current-selected-track-filename))
    (error "No track currently playing")))

(defun emms-score-down-playing ()
  (interactive)
  (if emms-player-playing-p
      (emms-score-change-score -1 (emms-score-current-selected-track-filename))
    (error "No track currently playing")))

(defun emms-score-up-file-on-line ()
  (interactive)
  (emms-score-change-score 1 (emms-score-track-at-filename)))

(defun emms-score-down-file-on-line ()
  (interactive)
  (emms-score-change-score -1 (emms-score-track-at-filename)))

(defun emms-score-less-tolerant ()
  "Only play mp3 with a higher score"
  (interactive)
  (setq emms-score-min-score (+ emms-score-min-score 1))
  (message "Will play songs with a score >= %d" emms-score-min-score))

(defun emms-score-more-tolerant ()
  "Allow playing of mp3 with a lower score."
  (interactive)
  (setq emms-score-min-score (- emms-score-min-score 1))
  (message "Will play songs with a score >= %d" emms-score-min-score))

(defun emms-score-set-playing (score)
  "Set score for current playing track."
  (interactive "nSet score for playing track: ")
  (let ((filename (emms-score-current-selected-track-filename)))
    (if emms-player-playing-p
	(emms-score-change-score
	 (- score (emms-score-get-score filename))
	 filename)
      (error "No track currently playing"))))

(defun emms-score-set-file-on-line (score)
  "Set score for track at point in emms-playlist buffer."
  (interactive "nSet score for track at point: ")
  (let ((filename (emms-score-track-at-filename)))
    (if emms-player-playing-p
	(emms-score-change-score
	 (- score (emms-score-get-score filename))
	 filename))))

(defun emms-score-set-tolerance (tolerance)
  "Allow playing tracks with a score >= tolerance."
  (interactive "nSet tolerance: ")
  (setq emms-score-min-score tolerance)
  (message "Will play songs with a score >= %d" emms-score-min-score))

(defun emms-score-show-playing ()
  "Show score for current playing track in minibuf."
  (interactive)
  (message "track/tolerance score: %d/%d"
	   (emms-score-get-score
	    (emms-score-current-selected-track-filename))
	   emms-score-min-score))

(defun emms-score-show-file-on-line ()
  "Show score for track at point in emms-playlist buffer."
  (interactive)
  (message "track/tolerance score: %d/%d"
	   (emms-score-get-score
            (emms-score-track-at-filename))
	   emms-score-min-score))


;;; Internal Functions

(defun emms-score-current-selected-track-filename ()
  "Return filename of current selected track."
  (emms-track-get (emms-playlist-current-selected-track) 'name))

(defun emms-score-track-at-filename ()
  "Return file of track at point in emms-playlist buffer."
  (emms-track-get (emms-playlist-track-at) 'name))

(defun emms-score-next-noerror ()
  "Run `emms-next-noerror' with score check.
See also `emms-next-noerror'."
  (interactive)
  (when emms-player-playing-p
    (error "A track is already being played"))
  (cond (emms-repeat-track
	 (emms-start))
	((condition-case nil
	     (progn
	       (emms-playlist-current-select-next)
	       t)
	   (error nil))
	 (if (emms-score-check-score
	      (emms-score-current-selected-track-filename))
	     (emms-start)
	   (emms-score-next-noerror)))
	(t
	 (message "No next track in playlist"))))

(defun emms-score-save-hash ()
  "Save score hash in `emms-score-file'."
  (interactive)
  (unless (file-directory-p (file-name-directory emms-score-file))
    (make-directory (file-name-directory emms-score-file)))
  (with-temp-file emms-score-file
    (let ((standard-output (current-buffer)))
      (insert "(")
      (maphash (lambda (key value)
		 (prin1 (cons key value)))
	       emms-score-hash)
      (insert ")"))))

(defun emms-score-load-hash ()
  "Load score hash from `emms-score-file'."
  (interactive)
  (if (file-exists-p emms-score-file)
      (mapc (lambda (elt)
              (puthash (car elt) (cdr elt) emms-score-hash))
            (read
             (with-temp-buffer
               (insert-file-contents emms-score-file)
               (buffer-string))))
    ;; when file not exists, make empty but valid score file
    (emms-score-save-hash)))

(defun emms-score-get-plist (filename)
  (gethash filename emms-score-hash))

(defun emms-score-change-score (score filename)
  (let ((sp (emms-score-get-plist filename) )
	(sc (emms-score-get-score filename)))
    (puthash filename
	     (plist-put sp emms-score-current-mood (+ sc score))
	     emms-score-hash)
    (message "New score is %s" (+ score sc))))

(defun emms-score-create-entry (filename)
  (puthash filename
	   `(,emms-score-current-mood ,emms-score-default-score)
	   emms-score-hash))

(defun emms-score-get-score (filename)
  "Return score of TRACK."
  (let ((plist (emms-score-get-plist filename)))
    (if (member emms-score-current-mood plist)
	(plist-get plist emms-score-current-mood)
      (emms-score-create-entry filename)
      (emms-score-get-score filename))))

(defun emms-score-check-score (filename)
  (>= (emms-score-get-score filename) emms-score-min-score))

(provide 'emms-score)

;;; emms-scores.el ends here