3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 ;; This program is free software; you can redistribute it and/or ;;
5 ;; modify it under the terms of the GNU General Public License as ;;
6 ;; published by the Free Software Foundation; either version 3, or (at ;;
7 ;; your option) any later version. ;;
9 ;; This program is distributed in the hope that it will be useful, but ;;
10 ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;
12 ;; General Public License for more details. ;;
14 ;; You should have received a copy of the GNU General Public License ;;
15 ;; along with this program. If not, see <http://www.gnu.org/licenses/>. ;;
17 ;; Written by and Copyright (C) Francois Fleuret ;;
18 ;; Contact <francois@fleuret.org> for comments & bug reports ;;
19 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
21 ;; Is it me, or the slave mode of mplayer is ugly to parse? Did I miss
24 (defcustom media/mplayer/args nil
25 "List of arguments for mplayer."
29 (defcustom media/mplayer/timing-request-period 0.25
30 "Period for the timing requests in second(s). Larger values
31 load Emacs less. Nil means no timing."
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;; It is impossible to tell mplayer to send information every time dt
38 ;; or so, hence this mess with a timer to avoid overloading emacs with
39 ;; the processing of the information
41 (defvar media/mplayer/timer nil
42 "A timer to request the timing position.")
44 (defun media/mplayer/timing-request ()
45 (if media/mplayer/process
46 (unless media/mplayer/paused
47 (media/mplayer/write "get_time_pos\n")
49 (media/mplayer/stop-timing-requests)
52 (defun media/mplayer/start-timing-requests ()
53 (when media/mplayer/timing-request-period
54 (media/mplayer/stop-timing-requests)
55 (setq media/mplayer/timer
57 media/mplayer/timing-request-period
58 'media/mplayer/timing-request))
62 (defun media/mplayer/stop-timing-requests ()
63 (when media/mplayer/timer
64 (cancel-timer media/mplayer/timer)
65 (setq media/mplayer/timer nil)
68 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
70 (defun media/mplayer/filter-subfunctions (cmd param)
71 ;; (unless (string= cmd "A:")
72 ;; (message "cmd=%s param=%s" cmd param)
80 ;; ----------------------------------------
84 (if (string-match "StreamTitle='\\([^;]*\\)';" param)
85 (setq media/current-song-in-stream (concat (match-string 1 param) " | " (current-time-string)))
86 (setq media/current-song-in-stream nil)
87 (message "ICY Info \"%s\"" param))
88 (if (and media/current-song-in-stream media/current-information)
89 (media/show-current-information)))
92 ;; ----------------------------------------
96 (setq media/song-duration
97 (string-to-number (substring param 1))))
99 ;; ----------------------------------------
101 ("ANS_TIME_POSITION" .
104 (setq media/song-current-time
105 (string-to-number (substring param 1)))
107 (when (and media/duration-to-history
108 (< media/mplayer/cumulated-duration media/duration-to-history))
110 (when media/mplayer/last-current-time
111 (setq media/mplayer/cumulated-duration
112 (+ media/mplayer/cumulated-duration
113 (- media/song-current-time media/mplayer/last-current-time))))
115 (when (>= media/mplayer/cumulated-duration media/duration-to-history)
116 (media/put-in-history)
119 (setq media/mplayer/last-current-time media/song-current-time)
126 ;; ----------------------------------------
131 ;; param = "44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)"
132 (when (string-match "^\\([0-9]+\\) Hz, \\([0-9]+\\) ch.* \\([0-9.]+\\) kbit"
134 (setq media/current-information
135 (list media/mplayer/url
136 (string-to-number (match-string 1 param))
137 (string-to-number (match-string 2 param))
138 (string-to-number (match-string 3 param))))
140 (run-hooks 'media/play-hook)
143 ;; ----------------------------------------
146 (media/mplayer/write "get_time_length\n"))
148 ;; ----------------------------------------
152 (when (string-match "(\\([0-9]+\\) bytes" param)
153 (message "Caching stream (%dkb)"
154 (/ (string-to-number (match-string 1 param)) 1024))))
156 ;; ----------------------------------------
161 (setq media/mplayer/exit-type
162 (cdr (assoc param '(("(End of file)" . file-finished)
164 media/current-information nil
165 media/song-duration nil
166 media/song-current-time nil)
168 (when media/mplayer/process (kill-process media/mplayer/process))
170 (force-mode-line-update)))
172 ;; ----------------------------------------
177 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
179 (defun media/mplayer/filter (process str)
180 (setq media/mplayer/buffer (concat media/mplayer/buffer str))
182 (while (and (< start (length media/mplayer/buffer))
183 (string-match "\\(.*\\)[\n
\r]+" media/mplayer/buffer start))
184 (setq start (1+ (match-end 1)))
185 (let ((line (match-string 1 media/mplayer/buffer)))
186 (when (string-match "^\\(AUDIO:\\|Exiting...\\|Starting\\|ANS_LENGTH\\|ANS_TIME_POSITION\\|Cache fill:\\|ICY Info:\\) *\\(.*\\)$" line)
187 (media/mplayer/filter-subfunctions (match-string 1 line) (match-string 2 line))))
189 (setq media/mplayer/buffer (substring media/mplayer/buffer start)))
192 (defun media/mplayer/sentinel (process str) ()
193 ;; (message "Media process got \"%s\"" (replace-regexp-in-string "\n" "" str))
194 (unless (eq (process-status media/mplayer/process) 'run)
195 (setq media/current-information nil
196 media/mplayer/process nil
197 media/song-current-time nil
198 media/song-duration nil)
200 (media/mplayer/stop-timing-requests)
202 (if (eq media/mplayer/exit-type 'file-finished)
203 (run-hooks 'media/finished-hook)
204 (run-hooks 'media/error-hook))
206 (force-mode-line-update))
209 (defun media/mplayer/write (&rest l)
210 ;; (message "****** WROTE \"%s\"" (replace-regexp-in-string "\n" "[RETURN]" (apply 'format l)))
211 (if media/mplayer/process (process-send-string media/mplayer/process (apply 'format l))
212 (error "No mplayer process"))
215 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
216 ;; Player control abstract layer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
217 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
219 (defun media/api/init () "Called once when the media application starts"
220 (setq media/player-id "MPlayer"
221 media/mplayer/url nil
222 media/mplayer/buffer "" ;; Used as read buffer
223 media/mplayer/process nil
224 media/mplayer/exit-type nil
225 media/mplayer/paused nil
226 media/song-duration nil
227 media/song-current-time nil
228 media/mplayer/cumulated-duration 0
229 media/mplayer/last-current-time nil
232 (defun media/api/cleanup () "Called when killing the application's buffer"
233 (when media/mplayer/process
234 (delete-process media/mplayer/process)
235 (media/mplayer/stop-timing-requests)
236 (setq media/mplayer/process nil)))
238 (defun media/api/play (url) (interactive)
239 (setq media/mplayer/url url)
241 (when media/mplayer/process (kill-process media/mplayer/process))
243 ;; (if media/mplayer/process
244 ;; (media/mplayer/write (concat "loadfile "
245 ;; (replace-regexp-in-string "^file://" "" media/mplayer/url)
248 (setq media/mplayer/process
252 '("mplayer" nil "mplayer" "-slave" "-quiet")
254 (if (string-match "\\(asx\\|m3u\\|pls\\|ram\\)$" media/mplayer/url)
256 (list (replace-regexp-in-string "^file://" "" media/mplayer/url))))
257 media/mplayer/exit-type 'unknown
258 media/mplayer/paused nil
259 media/song-duration nil
260 media/song-current-time nil
261 media/mplayer/cumulated-duration 0
262 media/mplayer/last-current-time nil
265 (set-process-filter media/mplayer/process 'media/mplayer/filter)
266 (set-process-sentinel media/mplayer/process 'media/mplayer/sentinel)
267 (process-kill-without-query media/mplayer/process)
268 (media/mplayer/start-timing-requests)
269 (media/mplayer/write "get_time_pos\n")
273 (defun media/api/stop () (interactive)
274 (media/mplayer/write "quit\n")
277 (defun media/api/pause () (interactive)
278 (media/mplayer/write "pause\n")
279 (setq media/mplayer/paused (not media/mplayer/paused))
282 (defun media/api/set-volume (mode value) (interactive)
283 (if (eq mode 'absolute)
284 (media/mplayer/write "volume %s 1\n" value)
286 (media/mplayer/write "volume +%s\n" value)
287 (media/mplayer/write "volume %s\n" value))))
289 (defun media/api/jump-at-percent (percent) (interactive)
290 (setq media/song-current-time nil)
291 (when (< media/mplayer/cumulated-duration media/duration-to-history)
292 (setq media/mplayer/cumulated-duration 0
293 media/mplayer/last-current-time nil))
294 (media/mplayer/write "seek %s 1\n" percent)
295 (media/mplayer/write "get_time_pos\n")
298 (defun media/api/jump-at-time (mode time) (interactive)
299 (setq media/song-current-time nil)
300 (when (< media/mplayer/cumulated-duration media/duration-to-history)
301 (setq media/mplayer/cumulated-duration 0
302 media/mplayer/last-current-time nil))
303 (if (eq mode 'absolute)
304 (media/mplayer/write "seek %s 2\n" time)
305 (media/mplayer/write "seek %s 0\n" time))
306 (media/mplayer/write "get_time_pos\n")
309 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;