-
Notifications
You must be signed in to change notification settings - Fork 50
/
emacspeak-m-player.el
1870 lines (1696 loc) · 67.4 KB
/
emacspeak-m-player.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; emacspeak-m-player.el --- Control mplayer from Emacs -*- lexical-binding: t; -*-
;;; $Id$
;;; $Author: tv.raman.tv $
;;; Description: Controlling mplayer from emacs
;;; Keywords: Emacspeak, m-player streaming media
;;{{{ LCD Archive entry:
;;; LCD Archive Entry:
;;; emacspeak| T. V. Raman |raman@cs.cornell.edu
;;; A speech interface to Emacs |
;;; $Date: 2008-06-29 17:58:19 -0700 (Sun, 29 Jun 2008) $ |
;;; $Revision: 4532 $ |
;;; Location undetermined
;;;
;;}}}
;;{{{ Copyright:
;;; Copyright (c) 1995 -- 2018, T. V. Raman
;;; All Rights Reserved.
;;;
;;; This file is not part of GNU Emacs, but the same permissions apply.
;;;
;;; GNU Emacs is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2, or (at your option)
;;; any later version.
;;;
;;; GNU Emacs is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Emacs; see the file COPYING. If not, write to
;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
;;}}}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;{{{ Introduction:
;;; Commentary:
;;; Defines an Emacspeak front-end for interacting with @code{mplayer}.
;;; Program @code{mplayer} is a versatile media player capable of playing many
;;; streaming media formats.
;;; This module provides complete access to all @code{mplayer} functionality
;;; from a convenient Emacs interface.
;;;
;;;@subsection Usage
;;;
;;; The main entry-point is command @code{emacspeak-multimedia}
;;;bound to @kbd{C-e ;}.
;;; This prompts for and launches the desired media stream.
;;; Once a stream is playing, you can control it with single-letter keystrokes
;;; in the @code{*M-Player*} buffer.
;;; Alternatively, you can switch away from that buffer to do real work,
;;; And invoke @code{m-player} commands by first pressing @kbd{C-e ;}.
;;; As an example, pressing @kbd{v} in the @code{*M-Player*} buffer
;;; prompts for and sets the volume;
;;; When not in the @code{*M-Player*} buffer, you can achieve the same
;;; by pressing @kbd{C-e ; v}.
;;; Press @kbd{C-h b} in the @code{*M-Player*}
;;; buffer to list @code{m-player} keybindings.
;;;
;;; Code:
;;}}}
;;{{{ Required modules
(require 'cl-lib)
(cl-declaim (optimize (safety 0) (speed 3)))
(require 'emacspeak-preamble)
(require 'ansi-color)
(require 'ladspa)
(require 'ido)
(require 'emacspeak-amark)
(require 'emacspeak-webutils)
(require 'dired)
(require 'locate)
(require 'comint)
;;}}}
;;{{{ Stream Metadata:
(cl-defstruct emacspeak-m-player-metadata
title artist album info
year comment track genre)
(defvar-local emacspeak-m-player-metadata nil
"Instance of stream metadata for this buffer.")
(defun emacspeak-m-player-display-metadata ()
"Display metadata after refreshing it if needed."
(interactive)
(let ((data (emacspeak-m-player-refresh-metadata)))
(with-output-to-temp-buffer "M Player Metadata"
(cl-loop
for f in
(cl-rest (mapcar #'car (cl-struct-slot-info 'emacspeak-m-player-metadata)))
do
(when (cl-struct-slot-value 'emacspeak-m-player-metadata f data)
(princ
(format
"%s:\t%s\n"
f
(cl-struct-slot-value 'emacspeak-m-player-metadata f data))))))
(message "Displayed metadata in other window.")
(emacspeak-auditory-icon 'task-done)))
;;}}}
;;{{{ define a derived mode for m-player interaction
(defconst emacspeak-media-shortcuts-directory
(expand-file-name "media/radio/" emacspeak-directory)
"*Directory where we organize and media shortcuts. ")
(defvar emacspeak-m-player-process nil
"Process handle to m-player.")
(defun emacspeak-m-player-dispatch (command)
"Dispatch command to m-player."
(cl-declare (special emacspeak-m-player-process))
(with-current-buffer (process-buffer emacspeak-m-player-process)
(erase-buffer)
(process-send-string emacspeak-m-player-process
(format "pausing_keep %s\n" command))
(accept-process-output emacspeak-m-player-process 0.1)
(unless (zerop (buffer-size))
(buffer-substring-no-properties (point-min) (1- (point-max))))))
(defvar-local emacspeak-m-player-current-directory nil
"Records current directory of media being played.
This is set to nil when playing Internet streams.")
(defsubst ems--seconds-string-to-duration (sec)
"Return seconds formatted as time if valid, otherwise return as is."
(let ((v (car (read-from-string sec))))
(cond
((and (numberp v) (not (cl-minusp v)))
(format-seconds "%.2h:%.2m:%.2s%z" v))
(t sec))))
(defsubst ems--duration-to-seconds (d)
"Convert hh:mm:ss to seconds."
(let ((v (mapcar #'car (mapcar #'read-from-string (split-string d ":")))))
(+
(* 3600 (cl-first v))
(* 60 (cl-second v))
(cl-third v))))
(defun emacspeak-m-player-mode-line ()
"Meaningful mode-line for *M-Player* buffers."
(cl-declare (special emacspeak-m-player-process))
(cond
((eq 'run (process-status emacspeak-m-player-process))
(let ((info (emacspeak-m-player-get-position))
(dtk-split-caps t))
(when info
(put-text-property 0 (length (cl-first info))
'personality 'voice-smoothen (cl-first info) )
(dtk-notify-speak
(concat
(cl-second info) " : "
(ems--seconds-string-to-duration (cl-first info))
" of "
(ems--seconds-string-to-duration (cl-third info)))))))
(t (message "Process MPlayer not running."))))
(defun emacspeak-m-player-speak-mode-line ()
"Speak mode line"
(interactive)
(tts-with-punctuations
'all
(dtk-speak (emacspeak-m-player-mode-line))))
(define-derived-mode emacspeak-m-player-mode comint-mode
"M-Player Interaction"
"Major mode for m-player interaction. \n\n
\\{emacspeak-m-player-mode-map}"
(progn
(setq emacspeak-m-player-metadata (make-emacspeak-m-player-metadata))
(setq buffer-undo-list t)))
;;}}}
;;{{{ emacspeak-m-player
;;;###autoload
(defgroup emacspeak-m-player nil
"Emacspeak media player settings."
:group 'emacspeak)
;;;###autoload
(defcustom emacspeak-m-player-program
(executable-find "mplayer")
"Media player program."
:type 'string
:group 'emacspeak-m-player)
(defvar emacspeak-m-player-openal-options
'("-ao" "openal")
"Additional options to use openal --- this gives us hrtf for instance.")
(defvar emacspeak-m-player-default-options
(list
"-msglevel" ; reduce chattiness while preserving metadata
(mapconcat
#'identity
'("all=4"
"header=0" "vo=0" "ao=0"
"decaudio=0" "decvideo=0" "open=0"
"network=0" "statusline=0" "cplayer=0"
"seek=0"
) ":")
"-slave" "-softvol" "-softvol-max" "300" "-quiet")
"Default options for MPlayer.")
(defcustom emacspeak-m-player-options
(copy-sequence emacspeak-m-player-default-options)
"Options passed to mplayer."
:type '(repeat
(string :tag "option"))
:group 'emacspeak-m-player)
(defcustom emacspeak-media-location-bindings nil
"*Map specific key sequences to launching MPlayer accelerators
on a specific directory."
:group 'emacspeak-m-player
:group 'emacspeak-media
:type '(repeat
:tag "Emacspeak Media Locations"
(list
:tag "KeyBinding"
(string :tag "Key")
(directory :tag "Directory")))
:set #'(lambda (sym val)
(mapc
#'(lambda (binding)
(let ((key (cl-first binding))
(directory (cl-second binding)))
(emacspeak-m-player-bind-accelerator directory (kbd key))))
val)
(set-default sym val)))
(defvar emacspeak-media-directory-regexp
(regexp-opt '("mp3" "audio" "music"))
"Pattern matching locations where we store media.")
;;;###autoload
(defun emacspeak-multimedia ()
"Start or control Emacspeak multimedia player.
Uses current context to prompt for media to play.
Controls media playback when already playing a stream.
\\{emacspeak-m-player-mode-map}."
(interactive)
(cl-declare (special emacspeak-m-player-process))
(cond
((and emacspeak-m-player-process
(eq 'run (process-status emacspeak-m-player-process))
(buffer-live-p (process-buffer emacspeak-m-player-process)))
(with-current-buffer (process-buffer emacspeak-m-player-process)
(call-interactively #'emacspeak-m-player-command)))
(t
(call-interactively #'emacspeak-m-player))))
;;;###autoload
(defun emacspeak-m-player-pop-to-player ()
"Pop to m-player buffer."
(interactive)
(cl-declare (special emacspeak-m-player-process))
(unless (process-live-p emacspeak-m-player-process)
(emacspeak-multimedia))
(pop-to-buffer (process-buffer emacspeak-m-player-process))
(emacspeak-speak-mode-line))
(defun emacspeak-m-player-command (key)
"Invoke MPlayer commands."
(interactive (list (read-key-sequence "MPlayer Key: ")))
(unless (eq 'run (process-status emacspeak-m-player-process))
(emacspeak-multimedia))
(call-interactively
(or (lookup-key emacspeak-m-player-mode-map key) 'undefined)))
;;;###autoload
(defvar emacspeak-m-player-playlist-pattern
(concat
(regexp-opt
(list ".m3u" ".asx" ".pls" ".rpm" ".ram"))
"$")
"Pattern for matching playlists.")
(defun emacspeak-m-player-playlist-p (resource)
"Check if specified resource matches a playlist type."
(cl-declare (special emacspeak-m-player-playlist-pattern))
(string-match emacspeak-m-player-playlist-pattern resource))
;;;###autoload
(defun emacspeak-m-player-bind-accelerator (directory key)
"Binds key to invoke m-player on specified directory."
(interactive
(list
(read-directory-name"Media Directory: ")
(read-key-sequence "Key: ")))
(let ((command
(eval
`(defun
,(intern (format "emacspeak-media-%s"
(file-name-base
(directory-file-name directory))))
()
,(format "Launch media from directory %s" directory)
(interactive)
(cl-declare (special emacspeak-m-player-current-directory))
(setq emacspeak-m-player-current-directory ,directory)
(emacspeak-m-player-accelerator ,directory)))))
(global-set-key key command)))
(defvar emacspeak-m-player-accelerator-p nil
"Flag set by accelerators. Let-binding this causes default-directory
etc to be ignored when guessing directory.")
;;;###autoload
(defun emacspeak-m-player-accelerator (directory)
"Launch MPlayer on specified directory."
(let ((ido-case-fold t)
(emacspeak-m-player-accelerator-p t)
(emacspeak-media-shortcuts-directory (expand-file-name directory)))
(call-interactively #'emacspeak-multimedia)
(emacspeak-auditory-icon 'select-object)
(emacspeak-speak-mode-line)))
;;;###autoload
(defun emacspeak-media-guess-directory ()
"Guess default directory."
(cl-declare (special emacspeak-media-directory-regexp
emacspeak-m-player-accelerator-p))
(cond
((or (eq major-mode 'dired-mode) (eq major-mode 'locate-mode)) nil)
(emacspeak-m-player-accelerator-p emacspeak-media-shortcuts-directory)
((or ; dir contains media:
(string-match emacspeak-media-directory-regexp default-directory)
(directory-files default-directory nil emacspeak-media-extensions))
default-directory)
(t emacspeak-media-shortcuts-directory)))
;;;###autoload
(defun emacspeak-m-player-url (url &optional playlist-p)
"Call emacspeak-m-player with specified URL."
(interactive (list (car (browse-url-interactive-arg "Media URL: "))))
(ems-with-messages-silenced
(emacspeak-m-player url playlist-p)))
(defvar-local emacspeak-m-player-file-list nil
"List that records list of files being played.")
(defun emacspeak-m-player-directory-files (directory)
"Return media files in directory by searching recursively. "
(cl-declare (special emacspeak-media-extensions))
(directory-files-recursively directory emacspeak-media-extensions))
(defvar-local emacspeak-m-player-url-p nil
"Flag that records if we are playing a stream URL")
;;;###autoload
(defun emacspeak-media-read-resource ()
"Read resource from minibuffer with contextual smarts."
(let ((completion-ignore-case t)
(read-file-name-function
(if (eq major-mode 'locate-mode)
#'read-file-name-default
#'ido-read-file-name))
(read-file-name-completion-ignore-case t)
(default-filename
(when (or (eq major-mode 'dired-mode) (eq major-mode 'locate-mode))
(dired-get-filename nil 'no-error)))
(dir (emacspeak-media-guess-directory))
(result nil))
(setq result
(expand-file-name
(funcall read-file-name-function
"Media Resource: "
dir
default-filename 'must-match)))
result))
(defun emacspeak-m-player-refresh-metadata ()
"Populate metadata fields from currently playing stream."
(cl-declare (special emacspeak-m-player-metadata))
(with-current-buffer (process-buffer emacspeak-m-player-process)
(cl-loop
for f in
'(title artist album year comment track genre)
do
(aset emacspeak-m-player-metadata
(cl-struct-slot-offset 'emacspeak-m-player-metadata f)
(cl-second
(split-string
(emacspeak-m-player-slave-command (format "get_meta_%s" f))
"="))))
emacspeak-m-player-metadata))
(defvar emacspeak-m-player-cue-info nil
"Set to T if ICY info cued automatically.")
(defun emacspeak-m-player-process-filter (process output)
"Filter function that captures metadata.
Also cleanup ANSI escape sequences."
(cl-declare (special emacspeak-m-player-cue-info
ansi-color-control-seq-regexp))
(when (process-live-p process)
(with-current-buffer (process-buffer process)
(when (and emacspeak-m-player-metadata
(emacspeak-m-player-metadata-p emacspeak-m-player-metadata)
(string-match "ICY Info:" output))
(setf
(emacspeak-m-player-metadata-info emacspeak-m-player-metadata)
(format "%s" output))
(when emacspeak-m-player-cue-info
(emacspeak-auditory-icon 'progress)
(emacspeak-m-player-stream-info)))
(goto-char (process-mark process))
(let ((start (point)))
(insert output)
(save-excursion
(goto-char start)
(while (re-search-forward ansi-color-control-seq-regexp (point-max) 'no-error)
(delete-region (match-beginning 0) (match-end 0))))))))
(defun emacspeak-m-player-amark-save ()
"Ensure that amarks are saved in the directory of the resource ."
(interactive)
(cl-declare (special emacspeak-m-player-process))
(when (process-live-p emacspeak-m-player-process)
(with-current-buffer
(process-buffer emacspeak-m-player-process)
(emacspeak-amark-save))))
;;;###autoload
(defun emacspeak-m-player (resource &optional play-list)
"Play specified resource using m-player. Optional prefix argument
play-list interprets resource as a play-list. Second interactive
prefix arg adds option -allow-dangerous-playlist-parsing to mplayer.
Resource is a media resource or playlist containing media resources.
The player is placed in a buffer in emacspeak-m-player-mode."
(interactive
(list
(emacspeak-media-read-resource)
current-prefix-arg))
(cl-declare (special
emacspeak-m-player-file-list emacspeak-m-player-current-directory
emacspeak-media-directory-regexp
emacspeak-media-shortcuts-directory emacspeak-m-player-process
emacspeak-m-player-program emacspeak-m-player-options))
(when (and emacspeak-m-player-process
(eq 'run (process-status emacspeak-m-player-process))
(y-or-n-p "Stop currently playing music? "))
(emacspeak-m-player-quit)
(setq emacspeak-m-player-process nil))
(let ((buffer (get-buffer-create "*M-Player*"))
(alsa-device (getenv "ALSA_DEFAULT"))
(process-connection-type nil)
(playlist-p
(or play-list
(emacspeak-m-player-playlist-p resource)))
(options (copy-sequence emacspeak-m-player-options))
(file-list nil))
(with-current-buffer buffer
(emacspeak-m-player-mode)
(setq emacspeak-m-player-url-p
(and
(not emacspeak-m-player-accelerator-p)
(or
(string-match emacspeak-media-shortcuts-directory resource )
(string-match "^http" resource))))
(unless emacspeak-m-player-url-p ; not a URL
(setq
resource (expand-file-name resource))
(setq emacspeak-m-player-current-directory (file-name-directory resource)))
(if (file-directory-p resource)
(setq file-list (emacspeak-m-player-directory-files resource))
(setq file-list (list resource)))
(when (and alsa-device (not (string= alsa-device "default")))
(setq options
(nconc options
(list "-ao" (format "alsa:device=%s" alsa-device)))))
(setq options
(cond
((and play-list (listp play-list)(< 4 (car play-list)))
(nconc options
(list "-allow-dangerous-playlist-parsing" "-playlist"
resource)))
(playlist-p
(nconc options (list "-playlist" resource)))
(file-list (nconc options file-list))
(t
(nconc options (list resource)))))
(setq buffer-undo-list t)
(setq emacspeak-m-player-process
(apply
#'start-process "MPLayer" buffer
emacspeak-m-player-program options))
(set-process-filter emacspeak-m-player-process
#'emacspeak-m-player-process-filter)
(when (and
emacspeak-m-player-current-directory
(file-exists-p emacspeak-m-player-current-directory))
(cd emacspeak-m-player-current-directory)
(emacspeak-amark-load))
(setq emacspeak-m-player-file-list file-list)
(emacspeak-auditory-icon 'progress)
(when (called-interactively-p 'interactive)
(message "MPlayer opened %s"
(if (file-directory-p resource)
(car (last (split-string resource "/" t)))
(file-name-nondirectory resource)))))))
;;;###autoload
(defun emacspeak-m-player-using-openal (resource &optional play-list)
"Use openal as the audio output driver. Adding hrtf=true to
~/.alsoftrc gives HRTF. You need to have openal installed and have an
mplayer that has been compiled with openal support to use this
feature. Calling spec is like `emacspeak-m-player'."
(interactive
(list
(emacspeak-media-read-resource)
current-prefix-arg))
(cl-declare (special emacspeak-m-player-options
emacspeak-m-player-openal-options
emacspeak-m-player-process))
(when (and emacspeak-m-player-process
(eq 'run (process-status emacspeak-m-player-process))
(y-or-n-p "Stop currently playing music? "))
(emacspeak-m-player-quit))
(unless (process-live-p emacspeak-m-player-process)
(let ((emacspeak-m-player-options
(append emacspeak-m-player-openal-options
emacspeak-m-player-options)))
(funcall-interactively #'emacspeak-m-player resource play-list))))
(defvar emacspeak-m-player-hrtf-options
'("-af" "hrtf" "-af" "resample=48000")
"Additional options to use built-in HRTF.")
;;;###autoload
(defun emacspeak-m-player-using-hrtf ()
"Add af resample=48000,hrtf to startup options.
This will work if the soundcard is set to 48000."
(interactive)
(cl-declare (special emacspeak-m-player-options emacspeak-m-player-hrtf-options
emacspeak-m-player-process))
(when (and emacspeak-m-player-process
(eq 'run (process-status emacspeak-m-player-process))
(y-or-n-p "Stop currently playing music? "))
(emacspeak-m-player-quit))
(unless (process-live-p emacspeak-m-player-process)
(let ((emacspeak-m-player-options
(append emacspeak-m-player-hrtf-options
emacspeak-m-player-options)))
(call-interactively #'emacspeak-m-player))))
;;;###autoload
(defun emacspeak-m-player-shuffle ()
"Launch M-Player with shuffle turned on."
(interactive)
(cl-declare (special emacspeak-m-player-options))
(let ((emacspeak-m-player-options
(append emacspeak-m-player-options (list "-shuffle"))))
(call-interactively #'emacspeak-m-player)))
;;;###autoload
(defun emacspeak-m-player-load (resource &optional append)
"Load specified resource into a running m-player.
Interactive prefix arg appends the new resource to what is playing."
(interactive
(list
(ems-with-messages-silenced
(let ((completion-ignore-case t)
(read-file-name-completion-ignore-case t))
(read-file-name
"MP3 Resource: "
(if
(string-match "\\(mp3\\)\\|\\(audio\\)"
(expand-file-name default-directory))
default-directory
emacspeak-media-shortcuts-directory)
(when (eq major-mode 'dired-mode)
(dired-get-filename)))))
current-prefix-arg))
(cl-declare (special emacspeak-media-extensions
emacspeak-media-shortcuts-directory))
(unless (string-match "^[a-z]+:" resource)
(setq resource (expand-file-name resource)))
(emacspeak-m-player-dispatch
(format "loadfile %s %s" resource
(if append 1 ""))))
;;}}}
;;{{{ Table of slave commands:
(defvar emacspeak-m-player-command-list nil
"Cache of MPlayer slave commands.")
(defun emacspeak-m-player-command-list ()
"Return MPlayer slave command table, populating it if
necessary."
(cl-declare (special emacspeak-m-player-command-list))
(cond
(emacspeak-m-player-command-list emacspeak-m-player-command-list)
(t
(let ((commands
(split-string
(shell-command-to-string
(format "%s -input cmdlist"
emacspeak-m-player-program))
"\n" 'omit-nulls)))
(setq emacspeak-m-player-command-list
(cl-loop for c in commands
collect
(split-string c " " 'omit-nulls)))))))
;;}}}
;;{{{ commands
(defun emacspeak-m-player-get-position ()
"Return list suitable to use as an amark. --- see emacspeak-amark.el.
Return value is of the form (position filename length)."
(cl-declare (special emacspeak-m-player-process))
(with-current-buffer (process-buffer emacspeak-m-player-process)
;;; dispatch command twice to avoid flakiness in mplayer
(emacspeak-m-player-dispatch "get_time_pos\nget_file_name\nget_time_length\n")
(let* ((output (emacspeak-m-player-dispatch "get_time_pos\nget_file_name\nget_time_length\n") )
(lines (split-string output "\n" 'omit-nulls))
(fields
(cl-loop
for l in lines
collect (cl-second (split-string l "=")))))
(list
(format "%s" (cl-first fields)) ; position
(if (cl-second fields)
(substring (cl-second fields) 1 -1)
"")
(format "%s" (cl-third fields))))))
(defun emacspeak-m-player-current-filename ()
"Return filename of currently playing track."
(cl-second
(split-string
(emacspeak-m-player-dispatch "get_file_name\n")
"=")))
(defun emacspeak-m-player-scale-speed (factor)
"Scale speed by specified factor."
(interactive "nFactor:")
(emacspeak-m-player-dispatch
(format "af_add scaletempo=scale=%f:speed=pitch" factor)))
(defun emacspeak-m-player-slower ()
"Slow down playback.
This affects pitch."
(interactive)
(emacspeak-m-player-scale-speed 0.9091))
(defun emacspeak-m-player-faster ()
"Speed up playback.
This affects pitch."
(interactive)
(emacspeak-m-player-scale-speed 1.1))
(defun emacspeak-m-player-half-speed ()
"Scale speed by 0.5."
(interactive)
(emacspeak-m-player-scale-speed 0.5))
(defun emacspeak-m-player-double-speed()
"Scale speed by 2.0"
(interactive)
(emacspeak-m-player-scale-speed 2.0))
(defun emacspeak-m-player-reset-speed ()
"Reset playing speed to normal."
(interactive)
(emacspeak-m-player-dispatch
"speed_set 1.0"))
(defun emacspeak-m-player-play-tracks-jump (step)
"Move within the play tree."
(interactive"nSkip Tracks:")
(unless (zerop step)
(emacspeak-m-player-dispatch
(format "pt_step %d" step))))
(defun emacspeak-m-player-previous-track ()
"Move to previous track."
(interactive)
(emacspeak-m-player-play-tracks-jump -1))
(defun emacspeak-m-player-next-track ()
"Move to next track."
(interactive)
(emacspeak-m-player-play-tracks-jump 1))
(defun emacspeak-m-player-play-tree-up (step)
"Move within the play tree."
(interactive
(list
(read-from-minibuffer "Move by: ")))
(emacspeak-m-player-dispatch
(format "pt_up %s" step)))
(defun emacspeak-m-player-alt-src-step (step)
"Move within an ASF playlist."
(interactive
(list
(read-from-minibuffer "Move by: ")))
(emacspeak-m-player-dispatch
(format "alt_src_step %s" step)))
(defun emacspeak-m-player-seek-relative (offset)
"Seek by offset into stream from current position.
Time offset can be specified as a number of seconds, or as HH:MM:SS."
(interactive
(list
(read-from-minibuffer "Offset: ")))
(when (string-match ":" offset)
(setq offset (ems--duration-to-seconds offset)))
(emacspeak-m-player-dispatch (format "seek %s" offset)))
(defun emacspeak-m-player-seek-percentage (pos)
"Seek to absolute specified pos in percent."
(interactive
(list
(read-from-minibuffer "Seek to percentage: ")))
(emacspeak-m-player-dispatch
(format "seek %s 1" pos)))
(defun emacspeak-m-player-seek-absolute (pos)
"Seek to absolute specified pos in seconds.
The time position can also be specified as HH:MM:SS."
(interactive
(list
(read-from-minibuffer "Seek to pos in seconds: ")))
(when (string-match ":" pos)
(setq pos (ems--duration-to-seconds pos)))
(emacspeak-m-player-dispatch (format "seek %s 2" pos)))
(defun emacspeak-m-player-beginning-of-track()
"Move to beginning of track."
(interactive)
(emacspeak-m-player-seek-absolute "0"))
(defun emacspeak-m-player-end-of-track()
"Move to beginning of track."
(interactive)
(emacspeak-m-player-seek-absolute "99"))
(defun emacspeak-m-player-backward-10s ()
"Move back by 10 seconds."
(interactive)
(emacspeak-m-player-seek-relative "-10"))
(defun emacspeak-m-player-forward-10s ()
"Move forward by 10 seconds."
(interactive)
(emacspeak-m-player-seek-relative "10"))
(defun emacspeak-m-player-backward-1min ()
"Move back by 1 minute."
(interactive)
(emacspeak-m-player-seek-relative "-60"))
(defun emacspeak-m-player-forward-1min ()
"Move forward by 1 minute."
(interactive)
(emacspeak-m-player-seek-relative "60"))
(defun emacspeak-m-player-backward-10min ()
"Move backward by ten minutes."
(interactive)
(emacspeak-m-player-seek-relative "-600"))
(defun emacspeak-m-player-forward-10min ()
"Move forward by ten minutes."
(interactive)
(emacspeak-m-player-seek-relative "600"))
(defun emacspeak-m-player-pause ()
"Pause or unpause media player."
(interactive)
(emacspeak-m-player-dispatch "pause"))
(defvar ems--m-player-mark "LastStopped"
"Name used to mark position where we quit a stream.")
(defun emacspeak-m-player-quit ()
"Quit media player."
(interactive)
(cl-declare (special emacspeak-amark-list ems--m-player-mark
emacspeak-m-player-url-p emacspeak-m-player-process))
(let ((kill-buffer-query-functions nil))
(when (eq (process-status emacspeak-m-player-process) 'run)
(let ((buffer (process-buffer emacspeak-m-player-process)))
(with-current-buffer buffer
(unless
(or
emacspeak-m-player-url-p ;;;dont amark streams
(string-equal emacspeak-media-shortcuts-directory
(substring default-directory 0 -1)))
(emacspeak-m-player-amark-add ems--m-player-mark)
(emacspeak-m-player-amark-save))
(emacspeak-m-player-dispatch "quit")
(emacspeak-auditory-icon 'close-object)
(and (buffer-live-p buffer) (kill-buffer buffer))))
(unless (eq (process-status emacspeak-m-player-process) 'exit)
(delete-process emacspeak-m-player-process))
(setq emacspeak-m-player-process nil)
(emacspeak-speak-mode-line))))
;;;###autoload
(defun emacspeak-m-player-volume-up ()
"Increase volume."
(interactive)
(emacspeak-m-player-dispatch "volume 1"))
;;;###autoload
(defun emacspeak-m-player-volume-down ()
"Decrease volume."
(interactive)
(emacspeak-m-player-dispatch "volume -1"))
(defvar emacspeak-m-player-active-filters nil
"Caches filters that are active.")
(make-variable-buffer-local 'emacspeak-m-player-active-filters)
;;;###autoload
(defun emacspeak-m-player-volume-change (value)
"Change volume to specified absolute value."
(interactive"sChange Volume to:")
(cl-declare (special emacspeak-m-player-active-filters))
(cl-pushnew "volume" emacspeak-m-player-active-filters :test #'string=)
(emacspeak-m-player-dispatch
(format "volume %s, 1" value)))
;;;###autoload
(defun emacspeak-m-player-balance ()
"Set left/right balance."
(interactive)
(emacspeak-m-player-dispatch
(format "balance %s"
(read-from-minibuffer "Balance -- Between -1 and 1:"))))
;;;###autoload
(defun emacspeak-m-player-slave-command (command)
"Dispatch slave command read from minibuffer."
(interactive
(list
(completing-read "Slave Command: " (emacspeak-m-player-command-list))))
(with-current-buffer (process-buffer emacspeak-m-player-process)
(let* ((args
(when (cdr (assoc command emacspeak-m-player-command-list))
(read-from-minibuffer
(mapconcat #'identity
(cdr (assoc command emacspeak-m-player-command-list))
" "))))
(result
(emacspeak-m-player-dispatch (format "%s %s" command args))))
(when result
(setq result (replace-regexp-in-string "^ans_" "" result))
(setq result (replace-regexp-in-string "_" " " result)))
(when (called-interactively-p 'interactive)
(message "%s"
(or result "Waiting")))
result)))
(defun emacspeak-m-player-delete-filter (filter)
"Delete filter."
(interactive
(list
(with-current-buffer (process-buffer emacspeak-m-player-process)
(completing-read "Filter:"
(or emacspeak-m-player-active-filters
emacspeak-m-player-filters nil nil)))))
(cl-declare (special emacspeak-m-player-filters
emacspeak-m-player-active-filters))
(with-current-buffer (process-buffer emacspeak-m-player-process)
(let* ((result (emacspeak-m-player-dispatch (format "af_del %s" filter))))
(setq emacspeak-m-player-active-filters (remove filter emacspeak-m-player-active-filters))
(when result
(setq result (replace-regexp-in-string "^ans_" "" result))
(setq result (replace-regexp-in-string "_" " " result)))
(message "%s" (or result "Waiting")))))
;;;###autoload
(defun emacspeak-m-player-display-percent ()
"Display current percentage."
(interactive)
(dtk-speak-and-echo (emacspeak-m-player-slave-command "get_percent_pos")))
;;;###autoload
(defun emacspeak-m-player-stream-info (&optional toggle-cue)
"Speak and display metadata if available.
Interactive prefix arg toggles automatic cueing of ICY info updates."
(interactive "P")
(cl-declare (special emacspeak-m-player-metadata emacspeak-m-player-cue-info))
(with-current-buffer (process-buffer emacspeak-m-player-process)
(unless emacspeak-m-player-metadata (error "No metadata"))
(let* ((m (emacspeak-m-player-metadata-info emacspeak-m-player-metadata))
(info (and m (cl-second (split-string m "=")))))
(when toggle-cue
(setq emacspeak-m-player-cue-info (not emacspeak-m-player-cue-info)))
(if toggle-cue
(progn
(emacspeak-auditory-icon
(if emacspeak-m-player-cue-info 'on 'off))
(message "ICY messages turned %s."
(if emacspeak-m-player-cue-info "on" "off")))
(message"%s" (format "%s" (or info "No Stream Info")))))))
;;;###autoload
(defun emacspeak-m-player-get-length ()
"Display length of track in seconds."
(interactive)
(dtk-speak-and-echo (emacspeak-m-player-dispatch "get_time_length")))
(defconst emacspeak-m-player-display-cmd
"get_time_pos\nget_percent_pos\nget_time_length\nget_file_name\n"
"Command we send MPlayer to display position.")
(defun emacspeak-m-player-display-position ()
"Display current position in track and its length."
(interactive)
(cl-declare (special emacspeak-m-player-display-cmd))
(let ((fields nil)
(result (emacspeak-m-player-dispatch emacspeak-m-player-display-cmd)))
(when result
(setq result (replace-regexp-in-string "^ans_" "" result))
(setq fields
(mapcar
#'(lambda (s) (split-string s "="))
(split-string result "\n"))))
(cond
(fields ; speak them after audio formatting
(cl-loop
for f in fields do
(put-text-property 0 (length (cl-first f))
'personality 'voice-smoothen (cl-first f))
(put-text-property 0 (length (cl-second f))
'personality 'voice-bolden (cl-second f)))
(setq result
(cl-loop
for f in fields
collect
(concat (cl-first f) " " (cl-second f) "\n ")))
(tts-with-punctuations 'some
(dtk-speak-and-echo (apply #'concat result))))
(t (dtk-speak-and-echo "Waiting")))))
(defun emacspeak-m-player-load-file(f)
"Load specified file."
(interactive "fMedia File:")
(emacspeak-m-player-dispatch
(format "loadfile %s"
(expand-file-name f))))
(defun emacspeak-m-player-load-playlist(f)
"Load specified playlist file."
(interactive "fPlaylist File:")
(emacspeak-m-player-dispatch
(format "loadlist %s"
(expand-file-name f))))
(defconst emacspeak-m-player-filters
'("extrastereo" "volnorm" "surround"
"channels=1:0:0:0:1"
"channels=1:1:0:1:1"
"channels=1:2"
"ladspa=bs2b:bs2b:700:4.5"
"ladspa=tap_pinknoise.so:tap_pinknoise:0.5:-2:-12"
"ladspa=ZamHeadX2-ladspa.so:ZamHeadX2:0:60:2.5"
"ladspa=ZamHeadX2-ladspa.so:ZamHeadX2:0:30:2.5"
"ladspa=ZamHeadX2-ladspa.so:ZamHeadX2:0:45:2.5"
"ladspa=ZamHeadX2-ladspa.so:ZamHeadX2:0:15:2.5"
"ladspa=amp:amp_stereo:2"
"ladspa=amp:amp_stereo:0.5"
"ladspa=tap_autopan:tap_autopan:.0016:100:1.5, ladspa=tap_autopan:tap_autopan:.06:33:2"
"bs2b=cmoy" "bs2b=jmeier" "bs2b")
"Table of useful MPlayer filters.")
(defun emacspeak-m-player-add-autopan ()
"Add predefined autopan effect."
(interactive)
(emacspeak-m-player-add-filter
(concat
"ladspa=tap_autopan:tap_autopan:.0016:100:1,"
"ladspa=tap_autopan:tap_autopan:.016:33:1")))
(defun emacspeak-m-player-add-filter (filter-name &optional edit)
"Adds specified filter. Prompts with one of several pre-defined
filters. Optional interactive prefix arg `edit' enables editing the