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 (setq media/mplayer/protocol-regexp
76 "^\\(AUDIO:\\|Exiting...\\|Starting\\|ANS_LENGTH\\|ANS_TIME_POSITION\\|Cache fill:\\|ICY Info:\\) *\\(.*\\)$")
78 (defun media/mplayer/filter-subfunctions (cmd param)
79 ;; (unless (string= cmd "A:")
80 ;; (message "cmd=%s param=%s" cmd param)
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 ;; If we did not parse it properly, reset the
107 ;; song name, and display the ICY string raw
108 (setq media/current-song-in-stream nil)
109 (message "ICY Info \"%s\"" param)
112 (if (and media/current-song-in-stream media/current-information)
113 (media/show-current-information))
116 ;; ----------------------------------------
120 (setq media/song-duration
121 (string-to-number (substring param 1))))
123 ;; ----------------------------------------
127 (setq media/song-current-time
128 (string-to-number (substring param 1)))
130 (when (and media/duration-to-history
131 (< media/mplayer/cumulated-duration media/duration-to-history))
133 (when media/mplayer/last-current-time
134 (setq media/mplayer/cumulated-duration
135 (+ media/mplayer/cumulated-duration
136 (- media/song-current-time media/mplayer/last-current-time))))
138 (when (>= media/mplayer/cumulated-duration media/duration-to-history)
139 (media/put-in-history)
142 (setq media/mplayer/last-current-time media/song-current-time)
146 ;; ----------------------------------------
150 ;; param = "44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)"
151 (when (string-match "^\\([0-9]+\\) Hz, \\([0-9]+\\) ch.* \\([0-9.]+\\) kbit"
153 (setq media/current-information
154 (list media/mplayer/url
155 (string-to-number (match-string 1 param))
156 (string-to-number (match-string 2 param))
157 (string-to-number (match-string 3 param))))
159 (run-hooks 'media/play-hook)
162 ;; ----------------------------------------
165 (media/mplayer/write "get_time_length\n")
166 (when media/mplayer/capture-dir
167 (media/mplayer/write "capturing\n")
168 ;; (message "Capturing stream in %s" media/mplayer/capture-dir)
172 ;; ----------------------------------------
175 (when (string-match "(\\([0-9]+\\) bytes" param)
176 (message "Caching stream (%dkb)"
177 (/ (string-to-number (match-string 1 param)) 1024))))
179 ;; ----------------------------------------
182 (setq media/mplayer/exit-type
183 (cdr (assoc param '(("(End of file)" . file-finished)
185 media/current-information nil
186 media/song-duration nil
187 media/song-current-time nil)
189 (when media/mplayer/process (kill-process media/mplayer/process))
191 (force-mode-line-update)
194 ;; ----------------------------------------
203 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205 (defun media/mplayer/filter (process str)
206 (setq media/mplayer/buffer (concat media/mplayer/buffer str))
208 (while (and (< start (length media/mplayer/buffer))
209 (string-match "\\(.*\\)[\n
\r]+" media/mplayer/buffer start))
210 (setq start (1+ (match-end 1)))
211 (let ((line (match-string 1 media/mplayer/buffer)))
212 (when (string-match media/mplayer/protocol-regexp line)
213 (media/mplayer/filter-subfunctions (match-string 1 line) (match-string 2 line))))
215 (setq media/mplayer/buffer (substring media/mplayer/buffer start)))
218 (defun media/mplayer/sentinel (process str) ()
219 ;; (message "Media process got \"%s\"" (replace-regexp-in-string "\n" "" str))
220 (unless (eq (process-status media/mplayer/process) 'run)
221 (setq media/current-information nil
222 media/mplayer/process nil
223 media/song-current-time nil
224 media/song-duration nil)
226 (media/mplayer/stop-timing-requests)
228 (if (eq media/mplayer/exit-type 'file-finished)
229 (run-hooks 'media/finished-hook)
230 (run-hooks 'media/error-hook))
232 (force-mode-line-update))
235 (defun media/mplayer/write (&rest l)
236 ;; (message "****** WROTE \"%s\"" (replace-regexp-in-string "\n" "[RETURN]" (apply 'format l)))
237 (if media/mplayer/process (process-send-string media/mplayer/process (apply 'format l))
238 (error "No mplayer process"))
241 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
242 ;; Player control abstract layer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
245 (defun media/api/init () "Called once when the media application starts"
246 (setq media/player-id "MPlayer"
247 media/mplayer/url nil
248 media/mplayer/buffer "" ;; Used as read buffer
249 media/mplayer/process nil
250 media/mplayer/exit-type nil
251 media/mplayer/paused nil
252 media/song-duration nil
253 media/song-current-time nil
254 media/mplayer/cumulated-duration 0
255 media/mplayer/last-current-time nil
259 (defun media/api/cleanup () "Called when killing the application's buffer"
260 (when media/mplayer/process
261 (delete-process media/mplayer/process)
262 (media/mplayer/stop-timing-requests)
263 (setq media/mplayer/process nil)))
265 (defun media/api/play (url) (interactive)
266 (setq media/mplayer/url url)
268 (when media/mplayer/process (kill-process media/mplayer/process))
270 ;; (if media/mplayer/process
271 ;; (media/mplayer/write (concat "loadfile "
272 ;; (replace-regexp-in-string "^file://" "" media/mplayer/url)
275 (setq media/mplayer/process
279 '("mplayer" nil "mplayer" "-slave" "-quiet")
281 (when (string-match "\\(asx\\|m3u\\|pls\\|ram\\)$" media/mplayer/url)
282 (if media/mplayer/capture-dir
284 (concat media/mplayer/capture-dir
286 (replace-regexp-in-string "[^a-zA-Z0-9\.]" "_" media/mplayer/url)
287 (format-time-string "-%Y-%m-%d-%H:%M:%S"))
293 (list (replace-regexp-in-string "^file://" "" media/mplayer/url)))
295 media/mplayer/exit-type 'unknown
296 media/mplayer/paused nil
297 media/song-duration nil
298 media/song-current-time nil
299 media/mplayer/cumulated-duration 0
300 media/mplayer/last-current-time nil
303 (set-process-filter media/mplayer/process 'media/mplayer/filter)
304 (set-process-sentinel media/mplayer/process 'media/mplayer/sentinel)
305 (process-kill-without-query media/mplayer/process)
306 (media/mplayer/start-timing-requests)
307 (media/mplayer/write "get_time_pos\n")
310 (defun media/api/stop () (interactive)
311 (media/mplayer/write "quit\n")
314 (defun media/api/pause () (interactive)
315 (media/mplayer/write "pause\n")
316 (setq media/mplayer/paused (not media/mplayer/paused))
319 (defun media/api/set-volume (mode value) (interactive)
320 (if (eq mode 'absolute)
321 (media/mplayer/write "volume %s 1\n" value)
323 (media/mplayer/write "volume +%s\n" value)
324 (media/mplayer/write "volume %s\n" value))))
326 (defun media/api/jump-at-percent (percent) (interactive)
327 (setq media/song-current-time nil)
328 (when (< media/mplayer/cumulated-duration media/duration-to-history)
329 (setq media/mplayer/cumulated-duration 0
330 media/mplayer/last-current-time nil))
331 (media/mplayer/write "seek %s 1\n" percent)
332 (media/mplayer/write "get_time_pos\n")
335 (defun media/api/jump-at-time (mode time) (interactive)
336 (setq media/song-current-time nil)
337 (when (< media/mplayer/cumulated-duration media/duration-to-history)
338 (setq media/mplayer/cumulated-duration 0
339 media/mplayer/last-current-time nil))
340 (if (eq mode 'absolute)
341 (media/mplayer/write "seek %s 2\n" time)
342 (media/mplayer/write "seek %s 0\n" time))
343 (media/mplayer/write "get_time_pos\n")
346 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;