]> code.delx.au - gnu-emacs-elpa/blob - chess-sound.el
Low level polyglot binary opening book support.
[gnu-emacs-elpa] / chess-sound.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;
3 ;; This is very similar to chess-announce, except it uses specific
4 ;; .WAV files instead of text-to-speech.
5 ;;
6
7 (require 'chess-game)
8
9 (defgroup chess-sound nil
10 "Code to play specific sounds when announcing chess moves."
11 :group 'chess)
12
13 (defcustom chess-sound-directory
14 (expand-file-name "sounds"
15 (file-name-directory
16 (or load-file-name buffer-file-name)))
17 "The directory where chess sounds can be found."
18 :type 'directory
19 :group 'chess-sound)
20
21 (defcustom chess-sound-play-function (if (fboundp 'play-sound-file)
22 'play-sound-file
23 'chess-sound-play)
24 "Non-nil if chess-sound should play sounds ."
25 :type 'function
26 :group 'chess-sound)
27
28 (defcustom chess-sound-program (or (executable-find "esdplay")
29 (executable-find "play"))
30 "Program used to play sounds, if `play-sound-file' does not exist."
31 :type 'file
32 :group 'chess-sound)
33
34 (defcustom chess-sound-args nil
35 "Additional args to pass to `chess-sound-program', before the .WAV file."
36 :type '(repeat string)
37 :group 'chess-sound)
38
39 (defcustom chess-sound-my-moves nil
40 "If non-nil, plays the move.wav sound whenever you make a move."
41 :type 'boolean
42 :group 'chess-sound)
43
44 (defsubst chess-sound (file)
45 (ignore-errors
46 (funcall chess-sound-play-function
47 (expand-file-name (concat file ".wav")
48 chess-sound-directory))))
49
50 (defsubst chess-sound-play (file)
51 (apply 'call-process chess-sound-program
52 nil nil nil (append chess-sound-args (list file))))
53
54 (defun chess-sound-handler (game event &rest args)
55 (cond
56 ((eq event 'initialize)
57 (and (file-directory-p chess-sound-directory)
58 (file-readable-p (expand-file-name "move.wav"
59 chess-sound-directory))
60 (or (eq chess-sound-play-function 'play-sound-file)
61 (and chess-sound-program
62 (file-executable-p chess-sound-program)))))
63
64 ((eq event 'move)
65 (let* ((ply (chess-game-ply game (1- (chess-game-index game))))
66 (pos (chess-ply-pos ply)))
67 (if (eq (chess-game-data game 'my-color)
68 (chess-pos-side-to-move pos))
69 (if chess-sound-my-moves
70 (chess-sound "move"))
71 (let* ((source (chess-ply-source ply))
72 (target (chess-ply-target ply))
73 (s-piece (and source (chess-pos-piece pos source)))
74 (t-piece (and target (chess-pos-piece pos target)))
75 (which (chess-ply-keyword ply :which))
76 text)
77 (cond
78 ((chess-ply-keyword ply :castle)
79 (chess-sound "O-O"))
80 ((chess-ply-keyword ply :long-castle)
81 (chess-sound "O-O-O"))
82 ((and s-piece t-piece (= t-piece ? ) target)
83 (if which
84 (chess-sound (char-to-string which)))
85 (chess-sound (format "%c_" (downcase s-piece)))
86 (chess-sound (chess-index-to-coord target)))
87 ((and s-piece t-piece target)
88 (if which
89 (chess-sound (char-to-string which)))
90 (chess-sound (format "%c_" (downcase s-piece)))
91 (chess-sound "x_")
92 (chess-sound (format "%c_" (downcase t-piece)))
93 (chess-sound (chess-index-to-coord target))))
94
95 (if (chess-ply-keyword ply :promote)
96 (chess-sound
97 (format "%c_" (downcase
98 (chess-ply-keyword ply :promote)))))
99 (if (chess-ply-keyword ply :en-passant)
100 (chess-sound "enpassant"))
101 (if (chess-ply-keyword ply :check)
102 (chess-sound "+_"))
103 (if (chess-ply-keyword ply :checkmate)
104 (chess-sound "#_"))
105 (if (chess-ply-keyword ply :stalemate)
106 (chess-sound "smate"))))))))
107
108 (provide 'chess-sound)
109
110 ;;; chess-sound.el ends here