]> code.delx.au - gnu-emacs-elpa/blob - chess-sound.el
*** no comment ***
[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 ;; $Revision$
7
8 (require 'chess-game)
9
10 (defgroup chess-sound nil
11 "Code to play specific sounds when announcing chess moves."
12 :group 'chess)
13
14 (defcustom chess-sound-directory
15 (expand-file-name "sounds"
16 (file-name-directory
17 (or load-file-name buffer-file-name)))
18 "The directory where chess sounds can be found."
19 :type 'directory
20 :group 'chess-sound)
21
22 (defcustom chess-sound-play-function (if (fboundp 'play-sound-file)
23 'play-sound-file
24 'chess-sound-play)
25 "Non-nil if chess-sound should play sounds ."
26 :type 'file
27 :group 'chess-sound)
28
29 (defcustom chess-sound-program (or (executable-find "esdplay")
30 (executable-find "play"))
31 "Program used to play sounds, if `play-sound-file' does not exist."
32 :type 'file
33 :group 'chess-sound)
34
35 (defcustom chess-sound-args nil
36 "Additional args to pass to `chess-sound-program', before the .WAV file."
37 :type '(repeat string)
38 :group 'chess-sound)
39
40 (defun chess-sound-for-game (game)
41 "Announce the opponent's moves in GAME."
42 (if (and (file-directory-p chess-sound-directory)
43 (file-exists-p (expand-file-name "tap.wav"
44 chess-sound-directory)))
45 (chess-game-add-hook game 'chess-sound-event-handler)))
46
47 (defun chess-sound (ch)
48 (let ((file
49 (cond
50 ((stringp ch)
51 (format "%s.wav" ch))
52 ((memq ch '(?\# ?\+ ?k ?q ?b ?n ?r ?p ?x))
53 (format "%c_.wav" ch))
54 (t
55 (format "%s.wav" (chess-index-to-coord ch))))))
56 (funcall chess-sound-play-function
57 (expand-file-name file chess-sound-directory))))
58
59 (defun chess-sound-play (file)
60 (apply 'call-process chess-sound-program
61 nil nil nil chess-sound-args))
62
63 (defun chess-sound-event-handler (game ignore event &rest args)
64 "This display module presents a standard chessboard.
65 See `chess-display-type' for the different kinds of displays."
66 (cond
67 ((memq event '(move game-over))
68 (let* ((ply (chess-game-ply game (1- (chess-game-index game))))
69 (pos (chess-ply-pos ply)))
70 (if (eq (chess-game-data game 'my-color)
71 (chess-pos-side-to-move pos))
72 (chess-sound "tap")
73 (let* ((source (chess-ply-source ply))
74 (target (chess-ply-target ply))
75 (s-piece (chess-pos-piece pos source))
76 (t-piece (chess-pos-piece pos target))
77 text)
78 (cond
79 ((memq :castle changes)
80 (chess-sound "O-O"))
81 ((memq :long-castle changes)
82 (chess-sound "O-O-O"))
83 ((= t-piece ? )
84 (chess-sound (downcase s-piece))
85 (chess-sound target))
86 (t
87 (chess-sound (downcase s-piece))
88 (chess-sound ?x)
89 (chess-sound (downcase t-piece))
90 (chess-sound target)))
91 (if (memq :check changes)
92 (chess-sound ?+))
93 (if (memq :checkmate changes)
94 (chess-sound ?#))
95 (if (memq :stalemate changes)
96 (chess-sound "smate")))))
97 nil)))
98
99 (provide 'chess-sound)
100
101 ;;; chess-sound.el ends here