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 ;; (message "ICY Info \"%s\"" param)
85 (if (string-match "StreamTitle='\\([^;]*\\)';" param)
86 (setq media/current-song-in-stream
87 (let ((s (match-string 1 param)))
88 (concat (if (string= s "") "<no title>" s)
90 ;; (current-time-string)
91 (format-time-string "%a %b %d %H:%M:%S")
93 (setq media/current-song-in-stream nil)
94 (message "ICY Info \"%s\"" param))
95 (if (and media/current-song-in-stream media/current-information)
96 (media/show-current-information)))
99 ;; ----------------------------------------
103 (setq media/song-duration
104 (string-to-number (substring param 1))))
106 ;; ----------------------------------------
108 ("ANS_TIME_POSITION" .
111 (setq media/song-current-time
112 (string-to-number (substring param 1)))
114 (when (and media/duration-to-history
115 (< media/mplayer/cumulated-duration media/duration-to-history))
117 (when media/mplayer/last-current-time
118 (setq media/mplayer/cumulated-duration
119 (+ media/mplayer/cumulated-duration
120 (- media/song-current-time media/mplayer/last-current-time))))
122 (when (>= media/mplayer/cumulated-duration media/duration-to-history)
123 (media/put-in-history)
126 (setq media/mplayer/last-current-time media/song-current-time)
133 ;; ----------------------------------------
138 ;; param = "44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)"
139 (when (string-match "^\\([0-9]+\\) Hz, \\([0-9]+\\) ch.* \\([0-9.]+\\) kbit"
141 (setq media/current-information
142 (list media/mplayer/url
143 (string-to-number (match-string 1 param))
144 (string-to-number (match-string 2 param))
145 (string-to-number (match-string 3 param))))
147 (run-hooks 'media/play-hook)
150 ;; ----------------------------------------
153 (media/mplayer/write "get_time_length\n"))
155 ;; ----------------------------------------
159 (when (string-match "(\\([0-9]+\\) bytes" param)
160 (message "Caching stream (%dkb)"
161 (/ (string-to-number (match-string 1 param)) 1024))))
163 ;; ----------------------------------------
168 (setq media/mplayer/exit-type
169 (cdr (assoc param '(("(End of file)" . file-finished)
171 media/current-information nil
172 media/song-duration nil
173 media/song-current-time nil)
175 (when media/mplayer/process (kill-process media/mplayer/process))
177 (force-mode-line-update)))
179 ;; ----------------------------------------
184 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186 (defun media/mplayer/filter (process str)
187 (setq media/mplayer/buffer (concat media/mplayer/buffer str))
189 (while (and (< start (length media/mplayer/buffer))
190 (string-match "\\(.*\\)[\n
\r]+" media/mplayer/buffer start))
191 (setq start (1+ (match-end 1)))
192 (let ((line (match-string 1 media/mplayer/buffer)))
193 (when (string-match "^\\(AUDIO:\\|Exiting...\\|Starting\\|ANS_LENGTH\\|ANS_TIME_POSITION\\|Cache fill:\\|ICY Info:\\) *\\(.*\\)$" line)
194 (media/mplayer/filter-subfunctions (match-string 1 line) (match-string 2 line))))
196 (setq media/mplayer/buffer (substring media/mplayer/buffer start)))
199 (defun media/mplayer/sentinel (process str) ()
200 ;; (message "Media process got \"%s\"" (replace-regexp-in-string "\n" "" str))
201 (unless (eq (process-status media/mplayer/process) 'run)
202 (setq media/current-information nil
203 media/mplayer/process nil
204 media/song-current-time nil
205 media/song-duration nil)
207 (media/mplayer/stop-timing-requests)
209 (if (eq media/mplayer/exit-type 'file-finished)
210 (run-hooks 'media/finished-hook)
211 (run-hooks 'media/error-hook))
213 (force-mode-line-update))
216 (defun media/mplayer/write (&rest l)
217 ;; (message "****** WROTE \"%s\"" (replace-regexp-in-string "\n" "[RETURN]" (apply 'format l)))
218 (if media/mplayer/process (process-send-string media/mplayer/process (apply 'format l))
219 (error "No mplayer process"))
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;; Player control abstract layer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
224 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
226 (defun media/api/init () "Called once when the media application starts"
227 (setq media/player-id "MPlayer"
228 media/mplayer/url nil
229 media/mplayer/buffer "" ;; Used as read buffer
230 media/mplayer/process nil
231 media/mplayer/exit-type nil
232 media/mplayer/paused nil
233 media/song-duration nil
234 media/song-current-time nil
235 media/mplayer/cumulated-duration 0
236 media/mplayer/last-current-time nil
239 (defun media/api/cleanup () "Called when killing the application's buffer"
240 (when media/mplayer/process
241 (delete-process media/mplayer/process)
242 (media/mplayer/stop-timing-requests)
243 (setq media/mplayer/process nil)))
245 (defun media/api/play (url) (interactive)
246 (setq media/mplayer/url url)
248 (when media/mplayer/process (kill-process media/mplayer/process))
250 ;; (if media/mplayer/process
251 ;; (media/mplayer/write (concat "loadfile "
252 ;; (replace-regexp-in-string "^file://" "" media/mplayer/url)
255 (setq media/mplayer/process
259 '("mplayer" nil "mplayer" "-slave" "-quiet")
261 (if (string-match "\\(asx\\|m3u\\|pls\\|ram\\)$" media/mplayer/url)
263 (list (replace-regexp-in-string "^file://" "" media/mplayer/url))))
264 media/mplayer/exit-type 'unknown
265 media/mplayer/paused nil
266 media/song-duration nil
267 media/song-current-time nil
268 media/mplayer/cumulated-duration 0
269 media/mplayer/last-current-time nil
272 (set-process-filter media/mplayer/process 'media/mplayer/filter)
273 (set-process-sentinel media/mplayer/process 'media/mplayer/sentinel)
274 (process-kill-without-query media/mplayer/process)
275 (media/mplayer/start-timing-requests)
276 (media/mplayer/write "get_time_pos\n")
280 (defun media/api/stop () (interactive)
281 (media/mplayer/write "quit\n")
284 (defun media/api/pause () (interactive)
285 (media/mplayer/write "pause\n")
286 (setq media/mplayer/paused (not media/mplayer/paused))
289 (defun media/api/set-volume (mode value) (interactive)
290 (if (eq mode 'absolute)
291 (media/mplayer/write "volume %s 1\n" value)
293 (media/mplayer/write "volume +%s\n" value)
294 (media/mplayer/write "volume %s\n" value))))
296 (defun media/api/jump-at-percent (percent) (interactive)
297 (setq media/song-current-time nil)
298 (when (< media/mplayer/cumulated-duration media/duration-to-history)
299 (setq media/mplayer/cumulated-duration 0
300 media/mplayer/last-current-time nil))
301 (media/mplayer/write "seek %s 1\n" percent)
302 (media/mplayer/write "get_time_pos\n")
305 (defun media/api/jump-at-time (mode time) (interactive)
306 (setq media/song-current-time nil)
307 (when (< media/mplayer/cumulated-duration media/duration-to-history)
308 (setq media/mplayer/cumulated-duration 0
309 media/mplayer/last-current-time nil))
310 (if (eq mode 'absolute)
311 (media/mplayer/write "seek %s 2\n" time)
312 (media/mplayer/write "seek %s 0\n" time))
313 (media/mplayer/write "get_time_pos\n")
316 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;