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 (defcustom media/mplayer/capture-dir nil
36 "States where to save the dumped streams."
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 ;; It is impossible to tell mplayer to send information every time dt
43 ;; or so, hence this mess with a timer to avoid overloading emacs with
44 ;; the processing of the information
46 (defvar media/mplayer/timer nil
47 "A timer to request the timing position.")
49 (defun media/mplayer/timing-request ()
50 (if media/mplayer/process
51 (unless media/mplayer/paused
52 (media/mplayer/write "get_time_pos\n")
54 (media/mplayer/stop-timing-requests)
57 (defun media/mplayer/start-timing-requests ()
58 (when media/mplayer/timing-request-period
59 (media/mplayer/stop-timing-requests)
60 (setq media/mplayer/timer
62 media/mplayer/timing-request-period
63 'media/mplayer/timing-request))
67 (defun media/mplayer/stop-timing-requests ()
68 (when media/mplayer/timer
69 (cancel-timer media/mplayer/timer)
70 (setq media/mplayer/timer nil)
73 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
75 (defun media/mplayer/filter-subfunctions (cmd param)
76 ;; (unless (string= cmd "A:")
77 ;; (message "cmd=%s param=%s" cmd param)
86 ;; ----------------------------------------
89 ;; (message "ICY Info \"%s\"" param)
91 (if (string-match "StreamTitle='\\([^;]*\\)';" param)
93 (setq media/current-song-in-stream
94 (let ((s (match-string 1 param)))
95 (concat (if (string= s "")
97 ;; (encode-coding-string s 'latin-1)
101 (format-time-string "%a %b %d %H:%M:%S")
106 (setq media/current-song-in-stream nil)
108 ;; If we did not parse it properly, show it
109 (message "ICY Info \"%s\"" param))
111 (if (and media/current-song-in-stream media/current-information)
112 (media/show-current-information))
115 ;; ----------------------------------------
119 (setq media/song-duration
120 (string-to-number (substring param 1))))
122 ;; ----------------------------------------
126 (setq media/song-current-time
127 (string-to-number (substring param 1)))
129 (when (and media/duration-to-history
130 (< media/mplayer/cumulated-duration media/duration-to-history))
132 (when media/mplayer/last-current-time
133 (setq media/mplayer/cumulated-duration
134 (+ media/mplayer/cumulated-duration
135 (- media/song-current-time media/mplayer/last-current-time))))
137 (when (>= media/mplayer/cumulated-duration media/duration-to-history)
138 (media/put-in-history)
141 (setq media/mplayer/last-current-time media/song-current-time)
145 ;; ----------------------------------------
149 ;; param = "44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)"
150 (when (string-match "^\\([0-9]+\\) Hz, \\([0-9]+\\) ch.* \\([0-9.]+\\) kbit"
152 (setq media/current-information
153 (list media/mplayer/url
154 (string-to-number (match-string 1 param))
155 (string-to-number (match-string 2 param))
156 (string-to-number (match-string 3 param))))
158 (run-hooks 'media/play-hook)
161 ;; ----------------------------------------
164 (media/mplayer/write "get_time_length\n")
165 (if media/mplayer/capture-dir (media/mplayer/write "capturing\n"))
168 ;; ----------------------------------------
171 (when (string-match "(\\([0-9]+\\) bytes" param)
172 (message "Caching stream (%dkb)"
173 (/ (string-to-number (match-string 1 param)) 1024))))
175 ;; ----------------------------------------
178 (setq media/mplayer/exit-type
179 (cdr (assoc param '(("(End of file)" . file-finished)
181 media/current-information nil
182 media/song-duration nil
183 media/song-current-time nil)
185 (when media/mplayer/process (kill-process media/mplayer/process))
187 (force-mode-line-update)
190 ;; ----------------------------------------
199 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
201 (defun media/mplayer/filter (process str)
202 (setq media/mplayer/buffer (concat media/mplayer/buffer str))
204 (while (and (< start (length media/mplayer/buffer))
205 (string-match "\\(.*\\)[\n
\r]+" media/mplayer/buffer start))
206 (setq start (1+ (match-end 1)))
207 (let ((line (match-string 1 media/mplayer/buffer)))
208 (when (string-match "^\\(AUDIO:\\|Exiting...\\|Starting\\|ANS_LENGTH\\|ANS_TIME_POSITION\\|Cache fill:\\|ICY Info:\\) *\\(.*\\)$" line)
209 (media/mplayer/filter-subfunctions (match-string 1 line) (match-string 2 line))))
211 (setq media/mplayer/buffer (substring media/mplayer/buffer start)))
214 (defun media/mplayer/sentinel (process str) ()
215 ;; (message "Media process got \"%s\"" (replace-regexp-in-string "\n" "" str))
216 (unless (eq (process-status media/mplayer/process) 'run)
217 (setq media/current-information nil
218 media/mplayer/process nil
219 media/song-current-time nil
220 media/song-duration nil)
222 (media/mplayer/stop-timing-requests)
224 (if (eq media/mplayer/exit-type 'file-finished)
225 (run-hooks 'media/finished-hook)
226 (run-hooks 'media/error-hook))
228 (force-mode-line-update))
231 (defun media/mplayer/write (&rest l)
232 ;; (message "****** WROTE \"%s\"" (replace-regexp-in-string "\n" "[RETURN]" (apply 'format l)))
233 (if media/mplayer/process (process-send-string media/mplayer/process (apply 'format l))
234 (error "No mplayer process"))
237 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
238 ;; Player control abstract layer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
239 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
241 (defun media/api/init () "Called once when the media application starts"
242 (setq media/player-id "MPlayer"
243 media/mplayer/url nil
244 media/mplayer/buffer "" ;; Used as read buffer
245 media/mplayer/process nil
246 media/mplayer/exit-type nil
247 media/mplayer/paused nil
248 media/song-duration nil
249 media/song-current-time nil
250 media/mplayer/cumulated-duration 0
251 media/mplayer/last-current-time nil
254 (defun media/api/cleanup () "Called when killing the application's buffer"
255 (when media/mplayer/process
256 (delete-process media/mplayer/process)
257 (media/mplayer/stop-timing-requests)
258 (setq media/mplayer/process nil)))
260 (defun media/api/play (url) (interactive)
261 (setq media/mplayer/url url)
263 (when media/mplayer/process (kill-process media/mplayer/process))
265 ;; (if media/mplayer/process
266 ;; (media/mplayer/write (concat "loadfile "
267 ;; (replace-regexp-in-string "^file://" "" media/mplayer/url)
270 (setq media/mplayer/process
274 '("mplayer" nil "mplayer" "-slave" "-quiet")
276 (when (string-match "\\(asx\\|m3u\\|pls\\|ram\\)$" media/mplayer/url)
277 (if media/mplayer/capture-dir (list "-dumpfile"
278 (concat media/mplayer/capture-dir
280 (replace-regexp-in-string "[^a-zA-Z0-9\.]" "_" media/mplayer/url)
281 (format-time-string "-%Y-%m-%d-%H:%M:%S"))
287 (list (replace-regexp-in-string "^file://" "" media/mplayer/url)))
289 media/mplayer/exit-type 'unknown
290 media/mplayer/paused nil
291 media/song-duration nil
292 media/song-current-time nil
293 media/mplayer/cumulated-duration 0
294 media/mplayer/last-current-time nil
297 (set-process-filter media/mplayer/process 'media/mplayer/filter)
298 (set-process-sentinel media/mplayer/process 'media/mplayer/sentinel)
299 (process-kill-without-query media/mplayer/process)
300 (media/mplayer/start-timing-requests)
301 (media/mplayer/write "get_time_pos\n")
304 (defun media/api/stop () (interactive)
305 (media/mplayer/write "quit\n")
308 (defun media/api/pause () (interactive)
309 (media/mplayer/write "pause\n")
310 (setq media/mplayer/paused (not media/mplayer/paused))
313 (defun media/api/set-volume (mode value) (interactive)
314 (if (eq mode 'absolute)
315 (media/mplayer/write "volume %s 1\n" value)
317 (media/mplayer/write "volume +%s\n" value)
318 (media/mplayer/write "volume %s\n" value))))
320 (defun media/api/jump-at-percent (percent) (interactive)
321 (setq media/song-current-time nil)
322 (when (< media/mplayer/cumulated-duration media/duration-to-history)
323 (setq media/mplayer/cumulated-duration 0
324 media/mplayer/last-current-time nil))
325 (media/mplayer/write "seek %s 1\n" percent)
326 (media/mplayer/write "get_time_pos\n")
329 (defun media/api/jump-at-time (mode time) (interactive)
330 (setq media/song-current-time nil)
331 (when (< media/mplayer/cumulated-duration media/duration-to-history)
332 (setq media/mplayer/cumulated-duration 0
333 media/mplayer/last-current-time nil))
334 (if (eq mode 'absolute)
335 (media/mplayer/write "seek %s 2\n" time)
336 (media/mplayer/write "seek %s 0\n" time))
337 (media/mplayer/write "get_time_pos\n")
340 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;