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 (setq media/song-duration
85 (string-to-number (substring param 1))))
87 ;; ----------------------------------------
89 ("ANS_TIME_POSITION" .
92 (setq media/song-current-time
93 (string-to-number (substring param 1)))
95 (when (and media/duration-to-history
96 (< media/mplayer/cumulated-duration media/duration-to-history))
98 (when media/mplayer/last-current-time
99 (setq media/mplayer/cumulated-duration
100 (+ media/mplayer/cumulated-duration
101 (- media/song-current-time media/mplayer/last-current-time))))
103 (when (>= media/mplayer/cumulated-duration media/duration-to-history)
104 (media/put-in-history)
107 (setq media/mplayer/last-current-time media/song-current-time)
114 ;; ----------------------------------------
119 ;; param = "44100 Hz, 2 ch, s16le, 128.0 kbit/9.07% (ratio: 16000->176400)"
120 (when (string-match "^\\([0-9]+\\) Hz, \\([0-9]+\\) ch.* \\([0-9.]+\\) kbit"
122 (setq media/current-information
123 (list media/mplayer/url
124 (string-to-number (match-string 1 param))
125 (string-to-number (match-string 2 param))
126 (string-to-number (match-string 3 param))))
128 (run-hooks 'media/play-hook)
131 ;; ----------------------------------------
134 (media/mplayer/write "get_time_length\n"))
136 ;; ----------------------------------------
140 (when (string-match "(\\([0-9]+\\) bytes" param)
141 (message "Caching stream (%dkb)"
142 (/ (string-to-number (match-string 1 param)) 1024))))
144 ;; ----------------------------------------
149 (setq media/mplayer/exit-type
150 (cdr (assoc param '(("(End of file)" . file-finished)
152 media/current-information nil
153 media/song-duration nil
154 media/song-current-time nil)
156 (when media/mplayer/process (kill-process media/mplayer/process))
158 (force-mode-line-update)))
160 ;; ----------------------------------------
165 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167 (defun media/mplayer/filter (process str)
168 (setq media/mplayer/buffer (concat media/mplayer/buffer str))
170 (while (and (< start (length media/mplayer/buffer))
171 (string-match "\\(.*\\)[\n
\r]+" media/mplayer/buffer start))
172 (setq start (1+ (match-end 1)))
173 (let ((line (match-string 1 media/mplayer/buffer)))
174 (when (string-match "^\\(AUDIO:\\|Exiting...\\|Starting\\|ANS_LENGTH\\|ANS_TIME_POSITION\\|Cache fill:\\) *\\(.*\\)$" line)
175 (media/mplayer/filter-subfunctions (match-string 1 line) (match-string 2 line)))))
176 (setq media/mplayer/buffer (substring media/mplayer/buffer start)))
179 (defun media/mplayer/sentinel (process str) ()
180 ;; (message "Media process got \"%s\"" (replace-regexp-in-string "\n" "" str))
181 (unless (eq (process-status media/mplayer/process) 'run)
182 (setq media/current-information nil
183 media/mplayer/process nil
184 media/song-current-time nil
185 media/song-duration nil)
187 (media/mplayer/stop-timing-requests)
189 (if (eq media/mplayer/exit-type 'file-finished)
190 (run-hooks 'media/finished-hook)
191 (run-hooks 'media/error-hook))
193 (force-mode-line-update))
196 (defun media/mplayer/write (&rest l)
197 ;; (message "****** WROTE \"%s\"" (replace-regexp-in-string "\n" "[RETURN]" (apply 'format l)))
198 (if media/mplayer/process (process-send-string media/mplayer/process (apply 'format l))
199 (error "No mplayer process"))
202 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
203 ;; Player control abstract layer ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
204 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
206 (defun media/api/init () "Called once when the media application starts"
207 (setq media/player-id "MPlayer"
208 media/mplayer/url nil
209 media/mplayer/buffer "" ;; Used as read buffer
210 media/mplayer/process nil
211 media/mplayer/exit-type nil
212 media/mplayer/paused nil
213 media/song-duration nil
214 media/song-current-time nil
215 media/mplayer/cumulated-duration 0
216 media/mplayer/last-current-time nil
219 (defun media/api/cleanup () "Called when killing the application's buffer"
220 (when media/mplayer/process
221 (delete-process media/mplayer/process)
222 (media/mplayer/stop-timing-requests)
223 (setq media/mplayer/process nil)))
225 (defun media/api/play (url) (interactive)
226 (setq media/mplayer/url url)
228 (when media/mplayer/process (kill-process media/mplayer/process))
230 ;; (if media/mplayer/process
231 ;; (media/mplayer/write (concat "loadfile "
232 ;; (replace-regexp-in-string "^file://" "" media/mplayer/url)
235 (setq media/mplayer/process
239 '("mplayer" nil "mplayer" "-slave" "-quiet")
241 (if (string-match "\\(asx\\|m3u\\|pls\\|ram\\)$" media/mplayer/url)
243 (list (replace-regexp-in-string "^file://" "" media/mplayer/url))))
244 media/mplayer/exit-type 'unknown
245 media/mplayer/paused nil
246 media/song-duration nil
247 media/song-current-time nil
248 media/mplayer/cumulated-duration 0
249 media/mplayer/last-current-time nil
252 (set-process-filter media/mplayer/process 'media/mplayer/filter)
253 (set-process-sentinel media/mplayer/process 'media/mplayer/sentinel)
254 (process-kill-without-query media/mplayer/process)
255 (media/mplayer/start-timing-requests)
256 (media/mplayer/write "get_time_pos\n")
260 (defun media/api/stop () (interactive)
261 (media/mplayer/write "quit\n")
264 (defun media/api/pause () (interactive)
265 (media/mplayer/write "pause\n")
266 (setq media/mplayer/paused (not media/mplayer/paused))
269 (defun media/api/set-volume (mode value) (interactive)
270 (if (eq mode 'absolute)
271 (media/mplayer/write "volume %s 1\n" value)
273 (media/mplayer/write "volume +%s\n" value)
274 (media/mplayer/write "volume %s\n" value))))
276 (defun media/api/jump-at-percent (percent) (interactive)
277 (setq media/song-current-time nil)
278 (when (< media/mplayer/cumulated-duration media/duration-to-history)
279 (setq media/mplayer/cumulated-duration 0
280 media/mplayer/last-current-time nil))
281 (media/mplayer/write "seek %s 1\n" percent)
282 (media/mplayer/write "get_time_pos\n")
285 (defun media/api/jump-at-time (mode time) (interactive)
286 (setq media/song-current-time nil)
287 (when (< media/mplayer/cumulated-duration media/duration-to-history)
288 (setq media/mplayer/cumulated-duration 0
289 media/mplayer/last-current-time nil))
290 (if (eq mode 'absolute)
291 (media/mplayer/write "seek %s 2\n" time)
292 (media/mplayer/write "seek %s 0\n" time))
293 (media/mplayer/write "get_time_pos\n")
296 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;