]> code.delx.au - gnu-emacs-elpa/blob - chess-sound.el
reward passed pawns, and make the code a bit faster
[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 (funcall chess-sound-play-function
46 (expand-file-name (concat file ".wav")
47 chess-sound-directory)))
48
49 (defsubst chess-sound-play (file)
50 (apply 'call-process chess-sound-program
51 nil nil nil (append chess-sound-args (list file))))
52
53 (defun chess-sound-handler (game event &rest args)
54 (cond
55 ((eq event 'initialize)
56 (and (file-directory-p chess-sound-directory)
57 (file-readable-p (expand-file-name "move.wav"
58 chess-sound-directory))
59 (or (eq chess-sound-play-function 'play-sound-file)
60 (and chess-sound-program
61 (file-executable-p chess-sound-program)))))
62
63 ((eq event 'move)
64 (let* ((ply (chess-game-ply game (1- (chess-game-index game))))
65 (pos (chess-ply-pos ply)))
66 (if (eq (chess-game-data game 'my-color)
67 (chess-pos-side-to-move pos))
68 (if chess-sound-my-moves
69 (chess-sound "move"))
70 (let* ((source (chess-ply-source ply))
71 (target (chess-ply-target ply))
72 (s-piece (and source (chess-pos-piece pos source)))
73 (t-piece (and target (chess-pos-piece pos target)))
74 (which (chess-ply-keyword ply :which))
75 text)
76 (cond
77 ((chess-ply-keyword ply :castle)
78 (chess-sound "O-O"))
79 ((chess-ply-keyword ply :long-castle)
80 (chess-sound "O-O-O"))
81 ((and s-piece t-piece (= t-piece ? ) target)
82 (if which
83 (chess-sound (char-to-string which)))
84 (chess-sound (format "%c_" (downcase s-piece)))
85 (chess-sound (chess-index-to-coord target)))
86 ((and s-piece t-piece target)
87 (if which
88 (chess-sound (char-to-string which)))
89 (chess-sound (format "%c_" (downcase s-piece)))
90 (chess-sound "x_")
91 (chess-sound (format "%c_" (downcase t-piece)))
92 (chess-sound (chess-index-to-coord target))))
93
94 (if (chess-ply-keyword ply :promote)
95 (chess-sound
96 (format "%c_" (downcase
97 (chess-ply-keyword ply :promote)))))
98 (if (chess-ply-keyword ply :en-passant)
99 (chess-sound "enpassant"))
100 (if (chess-ply-keyword ply :check)
101 (chess-sound "+_"))
102 (if (chess-ply-keyword ply :checkmate)
103 (chess-sound "#_"))
104 (if (chess-ply-keyword ply :stalemate)
105 (chess-sound "smate"))))))))
106
107 (provide 'chess-sound)
108
109 ;;; chess-sound.el ends here