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 ;; ----------------------------------------
83 (if (string-match "StreamTitle='\\([^']*\\)';" param)
84 (message "Now in stream (%s) \"%s\"" (current-time-string) (match-string 1 param))
85 (message "ICY Info \"%s\"" param)))
87 ;; ----------------------------------------
91 (setq media/song-duration
92 (string-to-number (substring param 1))))
94 ;; ----------------------------------------
96 ("ANS_TIME_POSITION" .
99 (setq media/song-current-time
100 (string-to-number (substring param 1)))
102 (when (and media/duration-to-history
103 (< media/mplayer/cumulated-duration media/duration-to-history))
105 (when media/mplayer/last-current-time
106 (setq media/mplayer/cumulated-duration
107 (+ media/mplayer/cumulated-duration
108 (- media/song-current-time media/mplayer/last-current-time))))
110 (when (>= media/mplayer/cumulated-duration media/duration-to-history)
111 (media/put-in-history)
114 (setq media/mplayer/last-current-time media/song-current-time)
121 ;; ----------------------------------------
126 ;; param = "44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)"
127 (when (string-match "^\\([0-9]+\\) Hz, \\([0-9]+\\) ch.* \\([0-9.]+\\) kbit"
129 (setq media/current-information
130 (list media/mplayer/url
131 (string-to-number (match-string 1 param))
132 (string-to-number (match-string 2 param))
133 (string-to-number (match-string 3 param))))
135 (run-hooks 'media/play-hook)
138 ;; ----------------------------------------
141 (media/mplayer/write "get_time_length\n"))
143 ;; ----------------------------------------
147 (when (string-match "(\\([0-9]+\\) bytes" param)
148 (message "Caching stream (%dkb)"
149 (/ (string-to-number (match-string 1 param)) 1024))))
151 ;; ----------------------------------------
156 (setq media/mplayer/exit-type
157 (cdr (assoc param '(("(End of file)" . file-finished)
159 media/current-information nil
160 media/song-duration nil
161 media/song-current-time nil)
163 (when media/mplayer/process (kill-process media/mplayer/process))
165 (force-mode-line-update)))
167 ;; ----------------------------------------
172 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
174 (defun media/mplayer/filter (process str)
175 (setq media/mplayer/buffer (concat media/mplayer/buffer str))
177 (while (and (< start (length media/mplayer/buffer))
178 (string-match "\\(.*\\)[\n
\r]+" media/mplayer/buffer start))
179 (setq start (1+ (match-end 1)))
180 (let ((line (match-string 1 media/mplayer/buffer)))
181 (when (string-match "^\\(AUDIO:\\|Exiting...\\|Starting\\|ANS_LENGTH\\|ANS_TIME_POSITION\\|Cache fill:\\|ICY Info:\\) *\\(.*\\)$" line)
182 (media/mplayer/filter-subfunctions (match-string 1 line) (match-string 2 line)))))
183 (setq media/mplayer/buffer (substring media/mplayer/buffer start)))
186 (defun media/mplayer/sentinel (process str) ()
187 ;; (message "Media process got \"%s\"" (replace-regexp-in-string "\n" "" str))
188 (unless (eq (process-status media/mplayer/process) 'run)
189 (setq media/current-information nil
190 media/mplayer/process nil
191 media/song-current-time nil
192 media/song-duration nil)
194 (media/mplayer/stop-timing-requests)
196 (if (eq media/mplayer/exit-type 'file-finished)
197 (run-hooks 'media/finished-hook)
198 (run-hooks 'media/error-hook))
200 (force-mode-line-update))
203 (defun media/mplayer/write (&rest l)
204 ;; (message "****** WROTE \"%s\"" (replace-regexp-in-string "\n" "[RETURN]" (apply 'format l)))
205 (if media/mplayer/process (process-send-string media/mplayer/process (apply 'format l))
206 (error "No mplayer process"))
209 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
210 ;; Player control abstract layer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
211 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
213 (defun media/api/init () "Called once when the media application starts"
214 (setq media/player-id "MPlayer"
215 media/mplayer/url nil
216 media/mplayer/buffer "" ;; Used as read buffer
217 media/mplayer/process nil
218 media/mplayer/exit-type nil
219 media/mplayer/paused nil
220 media/song-duration nil
221 media/song-current-time nil
222 media/mplayer/cumulated-duration 0
223 media/mplayer/last-current-time nil
226 (defun media/api/cleanup () "Called when killing the application's buffer"
227 (when media/mplayer/process
228 (delete-process media/mplayer/process)
229 (media/mplayer/stop-timing-requests)
230 (setq media/mplayer/process nil)))
232 (defun media/api/play (url) (interactive)
233 (setq media/mplayer/url url)
235 (when media/mplayer/process (kill-process media/mplayer/process))
237 ;; (if media/mplayer/process
238 ;; (media/mplayer/write (concat "loadfile "
239 ;; (replace-regexp-in-string "^file://" "" media/mplayer/url)
242 (setq media/mplayer/process
246 '("mplayer" nil "mplayer" "-slave" "-quiet")
248 (if (string-match "\\(asx\\|m3u\\|pls\\|ram\\)$" media/mplayer/url)
250 (list (replace-regexp-in-string "^file://" "" media/mplayer/url))))
251 media/mplayer/exit-type 'unknown
252 media/mplayer/paused nil
253 media/song-duration nil
254 media/song-current-time nil
255 media/mplayer/cumulated-duration 0
256 media/mplayer/last-current-time nil
259 (set-process-filter media/mplayer/process 'media/mplayer/filter)
260 (set-process-sentinel media/mplayer/process 'media/mplayer/sentinel)
261 (process-kill-without-query media/mplayer/process)
262 (media/mplayer/start-timing-requests)
263 (media/mplayer/write "get_time_pos\n")
267 (defun media/api/stop () (interactive)
268 (media/mplayer/write "quit\n")
271 (defun media/api/pause () (interactive)
272 (media/mplayer/write "pause\n")
273 (setq media/mplayer/paused (not media/mplayer/paused))
276 (defun media/api/set-volume (mode value) (interactive)
277 (if (eq mode 'absolute)
278 (media/mplayer/write "volume %s 1\n" value)
280 (media/mplayer/write "volume +%s\n" value)
281 (media/mplayer/write "volume %s\n" value))))
283 (defun media/api/jump-at-percent (percent) (interactive)
284 (setq media/song-current-time nil)
285 (when (< media/mplayer/cumulated-duration media/duration-to-history)
286 (setq media/mplayer/cumulated-duration 0
287 media/mplayer/last-current-time nil))
288 (media/mplayer/write "seek %s 1\n" percent)
289 (media/mplayer/write "get_time_pos\n")
292 (defun media/api/jump-at-time (mode time) (interactive)
293 (setq media/song-current-time nil)
294 (when (< media/mplayer/cumulated-duration media/duration-to-history)
295 (setq media/mplayer/cumulated-duration 0
296 media/mplayer/last-current-time nil))
297 (if (eq mode 'absolute)
298 (media/mplayer/write "seek %s 2\n" time)
299 (media/mplayer/write "seek %s 0\n" time))
300 (media/mplayer/write "get_time_pos\n")
303 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;