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)
81 ;; ----------------------------------------
84 ;; (message "ICY Info \"%s\"" param)
86 (if (string-match "StreamTitle='\\([^;]*\\)';" param)
88 (setq media/current-song-in-stream
89 (let ((s (match-string 1 param)))
90 (concat (if (string= s "") "<no title>" s)
92 (format-time-string "%a %b %d %H:%M:%S")
97 (setq media/current-song-in-stream nil)
99 ;; If we did not parse it properly, show it
100 (message "ICY Info \"%s\"" param))
102 (if (and media/current-song-in-stream media/current-information)
103 (media/show-current-information))
106 ;; ----------------------------------------
110 (setq media/song-duration
111 (string-to-number (substring param 1))))
113 ;; ----------------------------------------
117 (setq media/song-current-time
118 (string-to-number (substring param 1)))
120 (when (and media/duration-to-history
121 (< media/mplayer/cumulated-duration media/duration-to-history))
123 (when media/mplayer/last-current-time
124 (setq media/mplayer/cumulated-duration
125 (+ media/mplayer/cumulated-duration
126 (- media/song-current-time media/mplayer/last-current-time))))
128 (when (>= media/mplayer/cumulated-duration media/duration-to-history)
129 (media/put-in-history)
132 (setq media/mplayer/last-current-time media/song-current-time)
136 ;; ----------------------------------------
140 ;; param = "44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)"
141 (when (string-match "^\\([0-9]+\\) Hz, \\([0-9]+\\) ch.* \\([0-9.]+\\) kbit"
143 (setq media/current-information
144 (list media/mplayer/url
145 (string-to-number (match-string 1 param))
146 (string-to-number (match-string 2 param))
147 (string-to-number (match-string 3 param))))
149 (run-hooks 'media/play-hook)
152 ;; ----------------------------------------
155 (media/mplayer/write "get_time_length\n"))
157 ;; ----------------------------------------
160 (when (string-match "(\\([0-9]+\\) bytes" param)
161 (message "Caching stream (%dkb)"
162 (/ (string-to-number (match-string 1 param)) 1024))))
164 ;; ----------------------------------------
167 (setq media/mplayer/exit-type
168 (cdr (assoc param '(("(End of file)" . file-finished)
170 media/current-information nil
171 media/song-duration nil
172 media/song-current-time nil)
174 (when media/mplayer/process (kill-process media/mplayer/process))
176 (force-mode-line-update)
179 ;; ----------------------------------------
188 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
190 (defun media/mplayer/filter (process str)
191 (setq media/mplayer/buffer (concat media/mplayer/buffer str))
193 (while (and (< start (length media/mplayer/buffer))
194 (string-match "\\(.*\\)[\n
\r]+" media/mplayer/buffer start))
195 (setq start (1+ (match-end 1)))
196 (let ((line (match-string 1 media/mplayer/buffer)))
197 (when (string-match "^\\(AUDIO:\\|Exiting...\\|Starting\\|ANS_LENGTH\\|ANS_TIME_POSITION\\|Cache fill:\\|ICY Info:\\) *\\(.*\\)$" line)
198 (media/mplayer/filter-subfunctions (match-string 1 line) (match-string 2 line))))
200 (setq media/mplayer/buffer (substring media/mplayer/buffer start)))
203 (defun media/mplayer/sentinel (process str) ()
204 ;; (message "Media process got \"%s\"" (replace-regexp-in-string "\n" "" str))
205 (unless (eq (process-status media/mplayer/process) 'run)
206 (setq media/current-information nil
207 media/mplayer/process nil
208 media/song-current-time nil
209 media/song-duration nil)
211 (media/mplayer/stop-timing-requests)
213 (if (eq media/mplayer/exit-type 'file-finished)
214 (run-hooks 'media/finished-hook)
215 (run-hooks 'media/error-hook))
217 (force-mode-line-update))
220 (defun media/mplayer/write (&rest l)
221 ;; (message "****** WROTE \"%s\"" (replace-regexp-in-string "\n" "[RETURN]" (apply 'format l)))
222 (if media/mplayer/process (process-send-string media/mplayer/process (apply 'format l))
223 (error "No mplayer process"))
226 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 ;; Player control abstract layer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
228 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
230 (defun media/api/init () "Called once when the media application starts"
231 (setq media/player-id "MPlayer"
232 media/mplayer/url nil
233 media/mplayer/buffer "" ;; Used as read buffer
234 media/mplayer/process nil
235 media/mplayer/exit-type nil
236 media/mplayer/paused nil
237 media/song-duration nil
238 media/song-current-time nil
239 media/mplayer/cumulated-duration 0
240 media/mplayer/last-current-time nil
243 (defun media/api/cleanup () "Called when killing the application's buffer"
244 (when media/mplayer/process
245 (delete-process media/mplayer/process)
246 (media/mplayer/stop-timing-requests)
247 (setq media/mplayer/process nil)))
249 (defun media/api/play (url) (interactive)
250 (setq media/mplayer/url url)
252 (when media/mplayer/process (kill-process media/mplayer/process))
254 ;; (if media/mplayer/process
255 ;; (media/mplayer/write (concat "loadfile "
256 ;; (replace-regexp-in-string "^file://" "" media/mplayer/url)
259 (setq media/mplayer/process
263 '("mplayer" nil "mplayer" "-slave" "-quiet")
265 (if (string-match "\\(asx\\|m3u\\|pls\\|ram\\)$" media/mplayer/url)
267 (list (replace-regexp-in-string "^file://" "" media/mplayer/url))))
268 media/mplayer/exit-type 'unknown
269 media/mplayer/paused nil
270 media/song-duration nil
271 media/song-current-time nil
272 media/mplayer/cumulated-duration 0
273 media/mplayer/last-current-time nil
276 (set-process-filter media/mplayer/process 'media/mplayer/filter)
277 (set-process-sentinel media/mplayer/process 'media/mplayer/sentinel)
278 (process-kill-without-query media/mplayer/process)
279 (media/mplayer/start-timing-requests)
280 (media/mplayer/write "get_time_pos\n")
284 (defun media/api/stop () (interactive)
285 (media/mplayer/write "quit\n")
288 (defun media/api/pause () (interactive)
289 (media/mplayer/write "pause\n")
290 (setq media/mplayer/paused (not media/mplayer/paused))
293 (defun media/api/set-volume (mode value) (interactive)
294 (if (eq mode 'absolute)
295 (media/mplayer/write "volume %s 1\n" value)
297 (media/mplayer/write "volume +%s\n" value)
298 (media/mplayer/write "volume %s\n" value))))
300 (defun media/api/jump-at-percent (percent) (interactive)
301 (setq media/song-current-time nil)
302 (when (< media/mplayer/cumulated-duration media/duration-to-history)
303 (setq media/mplayer/cumulated-duration 0
304 media/mplayer/last-current-time nil))
305 (media/mplayer/write "seek %s 1\n" percent)
306 (media/mplayer/write "get_time_pos\n")
309 (defun media/api/jump-at-time (mode time) (interactive)
310 (setq media/song-current-time nil)
311 (when (< media/mplayer/cumulated-duration media/duration-to-history)
312 (setq media/mplayer/cumulated-duration 0
313 media/mplayer/last-current-time nil))
314 (if (eq mode 'absolute)
315 (media/mplayer/write "seek %s 2\n" time)
316 (media/mplayer/write "seek %s 0\n" time))
317 (media/mplayer/write "get_time_pos\n")
320 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;