forked from tosca07/picochess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picotalker.py
1207 lines (1082 loc) · 48.1 KB
/
picotalker.py
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
# Copyright (C) 2013-2018 Jean-Francois Romang (jromang@posteo.de)
# Shivkumar Shivaji ()
# Jürgen Précour (LocutusOfPenguin@posteo.de)
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
# This version uses sox. It does not use pygame or ogg123 -RR
# To install sox use:
# sudo apt install sox
import threading
import logging
import subprocess
import queue
from pathlib import Path
from shutil import which
from random import randint
import os
import time
import chess # type: ignore
from utilities import DisplayMsg
from dgt.api import Message
from dgt.util import GameResult, PlayMode, Voice, EBoard
logger = logging.getLogger(__name__)
class PicoTalker(object):
"""Handle the human speaking of events."""
def __init__(self, localisation_id_voice, speed_factor: float):
self.voice_path = None
self.speed_factor = 1.0
self.set_speed_factor(speed_factor)
self.sound = None
logger.debug("molli voice pfad calc.")
try:
(localisation_id, voice_name) = localisation_id_voice.split(":")
voice_path = "talker/voices/" + localisation_id + "/" + voice_name
if Path(voice_path).exists():
self.voice_path = voice_path
else:
logger.warning("voice path [%s] doesnt exist", voice_path)
except ValueError:
logger.warning("not valid voice parameter: %s", localisation_id_voice)
logger.debug("voice pfad: [%s]", self.voice_path)
def set_speed_factor(self, speed_factor: float):
"""Set the speed voice factor."""
self.speed_factor = speed_factor if which("play") else 1.0 # check for "sox" package
def talk(self, sounds):
"""Speak out the sound part by using sox play."""
if not self.voice_path:
logger.debug("picotalker turned off")
return False
vpath = self.voice_path
result = False
for part in sounds:
voice_file = vpath + "/" + part
if Path(voice_file).is_file():
command = ["play", voice_file, "tempo", str(self.speed_factor)]
try: # use blocking call
subprocess.call(
command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
result = True
except OSError as os_exc:
logger.warning("OSError: %s => turn voice OFF", os_exc)
self.voice_path = None
return False
else:
logger.warning("voice file not found %s", voice_file)
return result
class PicoTalkerDisplay(DisplayMsg, threading.Thread):
"""Listen on messages for talking."""
USER = "user"
COMPUTER = "computer"
SYSTEM = "system"
BEEPER = "beeper"
c_taken = False
c_castle = False
c_knight = False
c_rook = False
c_king = False
c_bishop = False
c_pawn = False
c_queen = False
c_check = False
c_mate = False
c_stalemate = False
c_draw = False
# add voice comment-factor
def __init__(
self,
user_voice: str,
computer_voice: str,
speed_factor: int,
setpieces_voice: bool,
comment_factor: int,
sample_beeper: bool,
sample_beeper_level: int,
eboard_type: EBoard,
):
"""
Initialize a PicoTalkerDisplay with voices for the user and/or computer players.
:param user_voice: The voice to use for the user (eg. en:al).
:param computer_voice: The voice to use for the computer (eg. en:christina).
"""
super(PicoTalkerDisplay, self).__init__()
self.user_picotalker = None
self.computer_picotalker = None
self.beeper_picotalker = None
self.eboard_type = eboard_type
self.speed_factor = (90 + (speed_factor % 10) * 5) / 100
self.play_mode = PlayMode.USER_WHITE
self.low_time = False
self.play_game = None
self.setpieces_voice = setpieces_voice
if computer_voice:
self.pico_voice_active = True
else:
self.pico_voice_active = False
self.c_no_beforecmove = 0
self.c_no_beforeumove = 0
self.c_no_cmove = 0
self.c_no_umove = 0
self.c_no_poem = 0
self.c_no_chat = 0
self.c_no_newgame = 0
self.c_no_rmove = 0
self.c_no_uwin = 0
self.c_no_uloose = 0
self.c_no_ublack = 0
self.c_no_uwhite = 0
self.c_no_start = 0
self.c_no_name = 0
self.c_no_shutdown = 0
self.c_no_takeback = 0
self.c_no_taken = 0
self.c_no_check = 0
self.c_no_mate = 0
self.c_no_stalemate = 0
self.c_no_draw = 0
self.c_no_castle = 0
self.c_no_king = 0
self.c_no_queen = 0
self.c_no_rook = 0
self.c_no_bishop = 0
self.c_no_knight = 0
self.c_no_pawn = 0
self.same_cnt = 0
self.c_comment_factor = comment_factor
self.sample_beeper = sample_beeper
self.sample_beeper_level = sample_beeper_level
if user_voice:
logger.debug("creating user voice: [%s]", str(user_voice))
self.set_user(PicoTalker(user_voice, self.speed_factor))
if computer_voice:
logger.debug("creating computer voice: [%s]", str(computer_voice))
self.set_computer(PicoTalker(computer_voice, self.speed_factor))
if self.sample_beeper and self.sample_beeper_level > 0:
beeper_sound = "en:beeper"
logger.debug("creating beeper sound: [%s]", str(beeper_sound))
self.set_beeper(PicoTalker(beeper_sound, self.speed_factor))
def set_comment_factor(self, comment_factor: int):
self.c_comment_factor = comment_factor
def calc_no_group_comments(self, filestring: str):
"""
molli: Calculate number of generic filestring files in voice folder
"""
c_group_no = 0
if self.computer_picotalker is not None:
path = self.computer_picotalker.voice_path
for file in os.listdir(path):
if file.startswith(filestring):
c_group_no += 1
return c_group_no
def set_computer(self, picotalker):
"""Set the computer talker."""
self.computer_picotalker = picotalker
"""molli: set correct number and assign it to voice group comment variables"""
self.c_no_beforecmove = self.calc_no_group_comments("f_beforecmove")
self.c_no_beforeumove = self.calc_no_group_comments("f_beforeumove")
self.c_no_cmove = self.calc_no_group_comments("f_cmove")
self.c_no_umove = self.calc_no_group_comments("f_umove")
self.c_no_poem = self.calc_no_group_comments("f_poem")
self.c_no_chat = self.calc_no_group_comments("f_chat")
self.c_no_newgame = self.calc_no_group_comments("f_newgame")
self.c_no_rmove = self.calc_no_group_comments("f_rmove")
self.c_no_uwin = self.calc_no_group_comments("f_uwin")
self.c_no_uloose = self.calc_no_group_comments("f_uloose")
self.c_no_ublack = self.calc_no_group_comments("f_ublack")
self.c_no_uwhite = self.calc_no_group_comments("f_uwhite")
self.c_no_start = self.calc_no_group_comments("f_start")
self.c_no_name = self.calc_no_group_comments("f_name")
self.c_no_shutdown = self.calc_no_group_comments("f_shutdown")
self.c_no_takeback = self.calc_no_group_comments("f_takeback")
self.c_no_taken = self.calc_no_group_comments("f_taken")
self.c_no_check = self.calc_no_group_comments("f_check")
self.c_no_mate = self.calc_no_group_comments("f_mate")
self.c_no_stalemate = self.calc_no_group_comments("f_stalemate")
self.c_no_draw = self.calc_no_group_comments("f_draw")
self.c_no_castle = self.calc_no_group_comments("f_castle")
self.c_no_king = self.calc_no_group_comments("f_king")
self.c_no_queen = self.calc_no_group_comments("f_queen")
self.c_no_rook = self.calc_no_group_comments("f_rook")
self.c_no_bishop = self.calc_no_group_comments("f_bishop")
self.c_no_knight = self.calc_no_group_comments("f_knight")
self.c_no_pawn = self.calc_no_group_comments("f_pawn")
def set_user(self, picotalker):
"""Set the user talker."""
self.user_picotalker = picotalker
def set_beeper(self, picotalker):
"""Set the beeper talker."""
self.beeper_picotalker = picotalker
def set_factor(self, speed_factor):
"""Set speech factor."""
if self.computer_picotalker:
self.computer_picotalker.set_speed_factor(speed_factor)
if self.user_picotalker:
self.user_picotalker.set_speed_factor(speed_factor)
if self.beeper_picotalker:
self.beeper_picotalker.set_speed_factor(speed_factor)
def talk(self, sounds, dev=SYSTEM):
if self.low_time:
return
if False: # switch-case
pass
elif dev == self.USER:
if self.user_picotalker:
self.user_picotalker.talk(sounds)
elif dev == self.COMPUTER:
if self.computer_picotalker:
self.computer_picotalker.talk(sounds)
elif dev == self.BEEPER:
if self.beeper_picotalker:
self.beeper_picotalker.talk(sounds)
elif dev == self.SYSTEM:
if self.computer_picotalker:
self.computer_picotalker.talk(sounds)
return
if self.user_picotalker:
self.user_picotalker.talk(sounds)
def get_total_cgroup(self, c_group: str):
# molli: define number of possible comments in differrent event groups
# together with a probability factor one can control how
# often a group comment will be spoken
c_number = 0
c_prob = 0
if c_group == "beforeumove":
c_prob = 15
c_number = self.c_no_beforeumove
elif c_group == "beforecmove":
c_prob = 15
c_number = self.c_no_beforecmove
elif c_group == "cmove":
c_prob = 15
c_number = self.c_no_cmove
elif c_group == "umove":
c_prob = 15
c_number = self.c_no_umove
elif c_group == "poem":
c_prob = 15
c_number = self.c_no_poem
elif c_group == "chat":
c_prob = 15
c_number = self.c_no_chat
elif c_group == "newgame":
c_prob = 100
c_number = self.c_no_newgame
elif c_group == "rmove":
c_prob = 15
c_number = self.c_no_rmove
elif c_group == "uwin":
c_prob = 100
c_number = self.c_no_uwin
elif c_group == "uloose":
c_prob = 100
c_number = self.c_no_uloose
elif c_group == "ublack":
c_prob = 50
c_number = self.c_no_ublack
elif c_group == "uwhite":
c_prob = 50
c_number = self.c_no_uwhite
elif c_group == "start":
c_prob = 100
c_number = self.c_no_start
elif c_group == "name":
c_prob = 100
c_number = self.c_no_name
elif c_group == "shutdown":
c_prob = 100
c_number = self.c_no_shutdown
elif c_group == "takeback":
c_prob = 50
c_number = self.c_no_takeback
elif c_group == "taken":
c_prob = 50
c_number = self.c_no_taken
elif c_group == "check":
c_prob = 50
c_number = self.c_no_check
elif c_group == "mate":
c_prob = 100
c_number = self.c_no_mate
elif c_group == "stalemate":
c_prob = 100
c_number = self.c_no_stalemate
elif c_group == "draw":
c_prob = 100
c_number = self.c_no_draw
elif c_group == "castle":
c_prob = 50
c_number = self.c_no_castle
elif c_group == "king":
c_prob = 50
c_number = self.c_no_king
elif c_group == "queen":
c_prob = 50
c_number = self.c_no_queen
elif c_group == "rook":
c_prob = 50
c_number = self.c_no_rook
elif c_group == "bishop":
c_prob = 50
c_number = self.c_no_bishop
elif c_group == "knight":
c_prob = 50
c_number = self.c_no_knight
elif c_group == "pawn":
c_prob = 50
c_number = self.c_no_pawn
else:
c_prob = 0
c_number = 0
return c_number, c_prob
def calc_comment(self, c_group):
# molli: define number of possible comments in differrent event groups
# together with a probability factor one can control how
# often a group comment will be spoken
talkfile = ""
c_rand_str = ""
c_rand = 0
c_number = 0
c_prob = 0
c_total = 0
# get total numbers of possible comments for this event group in dependence of
# selected comment speech and lanuage
c_total, c_prob = self.get_total_cgroup(c_group)
if c_prob == 0 or self.c_comment_factor == 0:
return talkfile
# consider probability factor from picochess.ini
if any(
(
c_group == "start",
c_group == "name",
c_group == "shutdown",
c_group == "mate",
c_group == "stalemate",
c_group == "draw",
c_group == "takeback",
c_group == "check",
)
):
# don't use factor for these events
c_prob = c_prob
else:
c_prob = round(c_prob * (self.c_comment_factor / 100))
c_number = round(c_total * (100 / c_prob))
if c_number > 1:
c_rand = randint(1, c_number)
else:
c_rand = c_number
c_rand_str = str(c_rand)
if c_rand == 0:
talkfile = ""
elif c_rand <= c_total:
talkfile = "f_" + c_group + c_rand_str + ".ogg"
else:
talkfile = ""
return talkfile
def comment(self, c_group):
# molli: define number of possible comments in differrent event groups
# together with a probability factor one can control how
# often a group comment will be spoke
talkfile = ""
# get total numbers of possible comments for this event group in dependence of
# selected comment speech and lanuage
talkfile = self.calc_comment(c_group)
if talkfile != "":
self.talk([talkfile])
def move_comment(self):
talkfile = ""
if PicoTalkerDisplay.c_taken:
talkfile = self.calc_comment("taken")
elif PicoTalkerDisplay.c_bishop:
talkfile = self.calc_comment("bishop")
elif PicoTalkerDisplay.c_queen:
talkfile = self.calc_comment("queen")
elif PicoTalkerDisplay.c_knight:
talkfile = self.calc_comment("knight")
elif PicoTalkerDisplay.c_rook:
talkfile = self.calc_comment("rook")
elif PicoTalkerDisplay.c_king:
talkfile = self.calc_comment("king")
elif PicoTalkerDisplay.c_castle:
talkfile = self.calc_comment("castle")
elif PicoTalkerDisplay.c_pawn:
talkfile = self.calc_comment("pawn")
else:
# pawn piesces are not spoken
# (no flag is set) => but we comment them!
talkfile = self.calc_comment("pawn")
if talkfile != "":
self.talk([talkfile])
if PicoTalkerDisplay.c_mate:
talkfile = ""
elif PicoTalkerDisplay.c_stalemate:
talkfile = ""
elif PicoTalkerDisplay.c_draw:
talkfile = ""
elif PicoTalkerDisplay.c_check:
talkfile = self.calc_comment("check")
else:
talkfile = ""
if talkfile != "":
self.talk([talkfile])
def say_squarepiece(self, fen_result):
logger.debug("molli: talker fen_result = %s", fen_result)
piece_parts = {
"K": "t_king.ogg",
"B": "t_bishop.ogg",
"N": "t_knight.ogg",
"R": "t_rook.ogg",
"Q": "t_queen.ogg",
"P": "t_pawn.ogg",
}
square_parts = {
"a": "t_a.ogg",
"b": "t_b.ogg",
"c": "t_c.ogg",
"d": "t_d.ogg",
"e": "t_e.ogg",
"f": "t_f.ogg",
"g": "t_g.ogg",
"h": "t_h.ogg",
"1": "t_1.ogg",
"2": "t_2.ogg",
"3": "t_3.ogg",
"4": "t_4.ogg",
"5": "t_5.ogg",
"6": "t_6.ogg",
"7": "t_7.ogg",
"8": "t_8.ogg",
}
sound_file = ""
voice_parts = []
rank = fen_result[-2]
file = fen_result[-1]
square_str = rank
square_str = square_str + " " + file
logger.debug("molli: talker square = %s", square_str)
if len(fen_result) > 2:
piece = fen_result[0]
if piece.islower():
voice_parts += ["black.ogg"]
piece = piece.upper()
else:
voice_parts += ["white.ogg"]
logger.debug("molli: talker piece = %s", piece)
try:
sound_file = piece_parts[piece]
except KeyError:
sound_file = ""
if sound_file:
voice_parts += [sound_file]
voice_parts += ["on.ogg"]
for part in square_str:
try:
sound_file = square_parts[part]
except KeyError:
sound_file = ""
if sound_file:
voice_parts += [sound_file]
else:
for part in square_str:
try:
sound_file = square_parts[part]
except KeyError:
sound_file = ""
if sound_file:
voice_parts += [sound_file]
logger.debug("molli: talker voice_parts = %s", voice_parts)
return voice_parts
def run(self):
"""Start listening for Messages on our queue and generate speech as appropriate."""
previous_move = chess.Move.null() # Ignore repeated broadcasts of a move
last_pos_dir = ""
logger.info("msg_queue ready")
while True:
try:
# Check if we have something to say
message = self.msg_queue.get()
if False: # switch-case
pass
elif isinstance(message, Message.ENGINE_FAIL):
logger.debug("announcing ENGINE_FAIL")
self.talk(["error.ogg"])
elif isinstance(message, Message.START_NEW_GAME):
last_pos_dir = ""
if message.newgame:
logger.debug("announcing START_NEW_GAME")
self.talk(["new_game.ogg"], self.BEEPER)
self.talk(["newgame.ogg"])
self.play_game = None
self.comment("newgame")
self.comment("uwhite")
previous_move = chess.Move.null()
elif isinstance(message, Message.COMPUTER_MOVE):
logger.debug("molli: before announcing COMPUTER_MOVE [%s]", message.move)
if message.move and message.game:
game_copy = message.game.copy()
if game_copy.board_fen() == chess.STARTING_BOARD_FEN:
previous_move = chess.Move.null()
if message.move != previous_move:
logger.debug("announcing COMPUTER_MOVE [%s]", message.move)
game_copy.push(message.move)
self.talk(["computer_move.ogg"], self.BEEPER)
if self.eboard_type == EBoard.NOEBOARD:
self.talk(["player_move.ogg"], self.BEEPER)
self.comment("beforecmove")
self.talk(self.say_last_move(game_copy), self.COMPUTER)
self.move_comment()
self.comment("cmove")
previous_move = message.move
self.play_game = game_copy
elif isinstance(message, Message.COMPUTER_MOVE_DONE):
self.play_game = None
if self.eboard_type != EBoard.NOEBOARD:
self.talk(["player_move.ogg"], self.BEEPER)
self.comment("chat")
elif isinstance(message, Message.USER_MOVE_DONE):
if message.move and message.game and message.move != previous_move:
logger.debug("announcing USER_MOVE_DONE [%s]", message.move)
self.talk(["player_move.ogg"], self.BEEPER)
self.comment("beforeumove")
self.talk(self.say_last_move(message.game), self.USER)
previous_move = message.move
self.play_game = None
self.comment("umove")
self.comment("poem")
elif isinstance(message, Message.REVIEW_MOVE_DONE):
if message.move and message.game and message.move != previous_move:
logger.debug("announcing REVIEW_MOVE_DONE [%s]", message.move)
self.talk(["player_move.ogg"], self.BEEPER)
self.talk(self.say_last_move(message.game), self.USER)
previous_move = message.move
self.play_game = None # @todo why thats not set in dgtdisplay?
elif isinstance(message, Message.GAME_ENDS):
previous_move = chess.Move.null()
last_pos_dir = ""
if message.result == GameResult.OUT_OF_TIME:
logger.debug("announcing GAME_ENDS/TIME_CONTROL")
wins = (
"whitewins.ogg"
if message.game.turn == chess.BLACK
else "blackwins.ogg"
)
self.talk(["timelost.ogg", wins])
if wins == "whitewins.ogg":
if self.play_mode == PlayMode.USER_WHITE:
self.comment("uwin")
else:
self.comment("uloose")
else:
if self.play_mode == PlayMode.USER_BLACK:
self.comment("uwin")
else:
self.comment("uloose")
elif message.result == GameResult.INSUFFICIENT_MATERIAL:
logger.debug("announcing GAME_ENDS/INSUFFICIENT_MATERIAL")
self.talk(["material.ogg", "draw.ogg"])
self.comment("draw")
elif message.result == GameResult.MATE:
logger.debug("announcing GAME_ENDS/MATE")
self.comment("mate")
if message.game.turn == chess.BLACK:
# white wins
if self.play_mode == PlayMode.USER_WHITE:
self.talk(["checkmate.ogg"])
self.talk(["whitewins.ogg"])
self.comment("uwin")
else:
self.comment("uloose")
else:
# black wins
if self.play_mode == PlayMode.USER_BLACK:
self.talk(["checkmate.ogg"])
self.talk(["blackwins.ogg"])
self.comment("uwin")
else:
self.comment("uloose")
elif message.result == GameResult.STALEMATE:
logger.debug("announcing GAME_ENDS/STALEMATE")
self.talk(["stalemate.ogg"])
self.comment("stalemate")
elif message.result == GameResult.ABORT:
logger.debug("announcing GAME_ENDS/ABORT")
self.talk(["abort.ogg"])
elif message.result == GameResult.DRAW:
logger.debug("announcing GAME_ENDS/DRAW")
self.talk(["draw.ogg"])
self.comment("draw")
elif message.result == GameResult.WIN_WHITE:
logger.debug("announcing GAME_ENDS/WHITE_WIN")
self.talk(["whitewins.ogg"])
if self.play_mode == PlayMode.USER_WHITE:
self.comment("uwin")
else:
self.comment("uloose")
elif message.result == GameResult.WIN_BLACK:
logger.debug("announcing GAME_ENDS/BLACK_WIN")
self.talk(["blackwins.ogg"])
if self.play_mode == PlayMode.USER_BLACK:
self.comment("uwin")
else:
self.comment("uloose")
elif message.result == GameResult.FIVEFOLD_REPETITION:
logger.debug("announcing GAME_ENDS/FIVEFOLD_REPETITION")
self.talk(["repetition.ogg", "draw.ogg"])
self.comment("draw")
elif isinstance(message, Message.TAKE_BACK):
logger.debug("announcing TAKE_BACK")
self.talk(["takeback.ogg"])
self.play_game = None
previous_move = chess.Move.null()
self.comment("takeback")
elif isinstance(message, Message.TIME_CONTROL):
logger.debug("announcing TIME_CONTROL")
self.talk(["confirm.ogg"], self.BEEPER)
self.talk(["oktime.ogg"])
elif isinstance(message, Message.INTERACTION_MODE):
logger.debug("announcing INTERACTION_MODE")
self.talk(["okmode.ogg"])
elif isinstance(message, Message.LEVEL):
if message.do_speak:
logger.debug("announcing LEVEL")
self.talk(["oklevel.ogg"])
else:
logger.debug("dont announce LEVEL cause its also an engine message")
elif isinstance(message, Message.OPENING_BOOK):
logger.debug("announcing OPENING_BOOK")
self.talk(["okbook.ogg"])
elif isinstance(message, Message.ENGINE_READY):
logger.debug("announcing ENGINE_READY")
self.talk(["confirm.ogg"], self.BEEPER)
self.talk(["okengine.ogg"])
elif isinstance(message, Message.PLAY_MODE):
logger.debug("announcing PLAY_MODE")
self.play_mode = message.play_mode
userplay = (
"userblack.ogg"
if message.play_mode == PlayMode.USER_BLACK
else "userwhite.ogg"
)
self.talk([userplay])
if message.play_mode == PlayMode.USER_BLACK:
self.comment("ublack")
else:
self.comment("uwhite")
elif isinstance(message, Message.STARTUP_INFO):
self.play_mode = message.info["play_mode"]
logger.debug("announcing PICOCHESS")
self.talk(["picoChess.ogg"], self.BEEPER)
self.talk(["picoChess.ogg"])
previous_move = chess.Move.null()
last_pos_dir = ""
self.comment("start")
self.comment("name")
elif isinstance(message, Message.CLOCK_TIME):
self.low_time = message.low_time
if self.low_time:
logger.debug(
"time too low, disable voice - w: %i, b: %i",
message.time_white,
message.time_black,
)
elif isinstance(message, Message.ALTERNATIVE_MOVE):
self.play_mode = message.play_mode
self.play_game = None
self.talk(["alternative_move.ogg"])
elif isinstance(message, Message.SYSTEM_SHUTDOWN):
logger.debug("announcing SHUTDOWN")
self.talk(["goodbye.ogg"])
self.comment("shutdown")
elif isinstance(message, Message.SYSTEM_REBOOT):
logger.debug("announcing REBOOT")
self.talk(["pleasewait.ogg"])
self.comment("shutdown")
time.sleep(3)
elif isinstance(message, Message.MOVE_RETRY):
logger.debug("announcing MOVE_RETRY")
self.talk(["retry_move.ogg"])
elif isinstance(message, Message.MOVE_WRONG):
logger.debug("announcing MOVE_WRONG")
self.talk(["wrong_move.ogg"])
elif isinstance(message, Message.ONLINE_LOGIN):
logger.debug("announcing ONLINE_LOGIN")
self.talk(["online_login.ogg"])
elif isinstance(message, Message.SEEKING):
logger.debug("announcing SEEKING")
self.talk(["seeking.ogg"])
elif isinstance(message, Message.ONLINE_NAMES):
logger.debug("announcing ONLINE_NAMES")
self.talk(["opponent_found.ogg"])
elif isinstance(message, Message.RESTORE_GAME):
logger.debug("announcing RESTORE_GAME")
self.talk(["last_game_restored.ogg"])
elif isinstance(message, Message.ENGINE_SETUP):
logger.debug("announcing ENGINE_SETUP")
self.talk(["engine_setup.ogg"])
elif isinstance(message, Message.ONLINE_FAILED):
self.talk(["server_error.ogg"])
elif isinstance(message, Message.ONLINE_USER_FAILED):
self.talk(["login_error.ogg"])
elif isinstance(message, Message.ONLINE_NO_OPPONENT):
self.talk(["no_opponent.ogg"])
elif isinstance(message, Message.LOST_ON_TIME):
self.talk(["timelost.ogg"])
elif isinstance(message, Message.POSITION_FAIL):
logger.debug("molli: talker orig. fen_result = %s", message.fen_result)
if last_pos_dir == message.fen_result:
self.same_cnt = self.same_cnt + 1
else:
self.same_cnt = 0
last_pos_dir = message.fen_result
if self.same_cnt % 3 == 0:
if "clear" in message.fen_result:
fen_str = message.fen_result[-2:]
self.talk(["remove.ogg"])
self.talk(self.say_squarepiece(fen_str))
elif "put" in message.fen_result:
fen_str = message.fen_result[-4:]
self.talk(["put.ogg"])
self.talk(self.say_squarepiece(fen_str))
else:
pass
elif isinstance(message, Message.PICOTUTOR_MSG):
if "??" == message.eval_str:
if not self.pico_voice_active:
self.talk(["picotutor.ogg"], self.BEEPER)
self.talk(["picotutor_notify.ogg"])
self.talk(["verybadmove.ogg"])
elif "?" == message.eval_str:
self.talk(["picotutor_notify.ogg"])
self.talk(["badmove.ogg"])
elif "!?" == message.eval_str:
self.talk(["picotutor_notify.ogg"])
self.talk(["interestingmove.ogg"])
elif "!!" == message.eval_str:
self.talk(["picotutor_notify.ogg"])
self.talk(["verygoodmove.ogg"])
elif "!" == message.eval_str:
self.talk(["picotutor_notify.ogg"])
self.talk(["goodmove.ogg"])
elif "?!" == message.eval_str:
self.talk(["picotutor_notify.ogg"])
self.talk(["dubiousmove.ogg"])
elif "ER" == message.eval_str:
self.talk(["picotutor_notify.ogg"])
self.talk(["error.ogg"])
elif "ACTIVE" in message.eval_str:
self.talk(["picotutor_notify.ogg"])
self.talk(["picotutor_enabled.ogg"])
elif "ANALYSIS" in message.eval_str:
self.talk(["picotutor_notify.ogg"])
self.talk(["picotutor_analysis.ogg"])
elif "HINT" in message.eval_str:
self.talk(["picotutor_hintmove.ogg"])
self.talk(self.say_tutor_move(message.game))
elif "THREAT" in message.eval_str:
self.talk(["picotutor_threatmove.ogg"])
self.talk(self.say_tutor_move(message.game))
elif "POSOK" in message.eval_str:
last_pos_dir = ""
self.talk(["confirm.ogg"], self.BEEPER)
self.talk(["ok.ogg"])
elif "POS" in message.eval_str:
score = message.score
if abs(score) <= 1:
self.talk(["picotutor_equal_position.ogg"])
elif score > 3:
self.talk(["picotutor_verygood_position.ogg"])
elif score > 1 and score < 3:
self.talk(["picotutor_good_position.ogg"])
elif score < -3:
self.talk(["picotutor_verybad_position.ogg"])
elif score > -3 and score < -1:
self.talk(["picotutor_bad_position.ogg"])
elif "BEST" in message.eval_str:
self.talk(["picotutor_best_move.ogg"])
self.talk(self.say_tutor_move(message.game))
elif "PICMATE" in message.eval_str:
logger.debug("molli in picotutortalker: %s", message.eval_str)
self.talk(["picotutor_pico_mate.ogg"])
list_str = message.eval_str
list_mate = list_str.split("_")
logger.debug("molli in picotutortalker: %s", list_mate[0])
logger.debug("molli in picotutortalker: %s", list_mate[1])
talk_mate = "t_" + list_mate[1] + ".ogg"
logger.debug("talk_mate = %s", talk_mate)
self.talk([talk_mate])
elif "USRMATE" in message.eval_str:
logger.debug("molli in picotutortalker: %s", message.eval_str)
self.talk(["picotutor_player_mate.ogg"])
list_str = message.eval_str
list_mate = list_str.split("_")
logger.debug("molli in picotutortalker: %s", list_mate[0])
logger.debug("molli in picotutortalker: %s", list_mate[1])
talk_mate = "t_" + list_mate[1] + ".ogg"
logger.debug("talk_mate = %s", talk_mate)
self.talk([talk_mate])
elif isinstance(message, Message.PGN_GAME_END): # for pgn replay
logger.debug("announcing PGN GAME END")
previous_move = chess.Move.null()
self.talk(["pgn_game_end.ogg"])
if "1-0" in message.result:
self.talk(["whitewins.ogg"])
elif "0-1" in message.result:
self.talk(["blackwins.ogg"])
elif "0.5-0.5" in message.result or "1/2-1/2" in message.result:
self.talk(["draw.ogg"])
elif "*" in message.result:
self.talk(["game_result_unknown.ogg"])
else:
# default
self.talk(["game_result_unknown.ogg"])
elif isinstance(message, Message.TIMECONTROL_CHECK):
logger.debug("timecontrol check")
self.talk(["picotutor_notify.ogg"])
if message.player:
self.talk(["timecontrol_check_player.ogg"])
else:
self.talk(["timecontrol_check_opp.ogg"])
elif isinstance(message, Message.SHOW_ENGINENAME):
if message.show_enginename:
self.talk(["show_enginename_on.ogg"])
else:
self.talk(["show_enginename_off.ogg"])
elif isinstance(message, Message.SHOW_TEXT):
if message.text_string == "NEW_POSITION_SCAN":
self.talk(["position_setup.ogg"])
elif message.text_string == "NEW_POSITION":
self.talk(["set_pieces_sound.ogg"], self.BEEPER)
if not self.sample_beeper or self.sample_beeper_level == 0:
self.talk(["set_pieces_sound.ogg"])
elif isinstance(message, Message.PICOWATCHER):
if message.picowatcher:
self.talk(["picowatcher_enabled.ogg"])
else:
self.talk(["picowatcher_disabled.ogg"])
self.talk(["picotutor_ok.ogg"])
elif isinstance(message, Message.PICOCOACH):
if message.picocoach:
self.talk(["picocoach_enabled.ogg"])
else:
self.talk(["picocoach_disabled.ogg"])
self.talk(["picotutor_ok.ogg"])
elif isinstance(message, Message.PICOEXPLORER):
if message.picoexplorer:
self.talk(["picoexplorer_enabled.ogg"])
else:
self.talk(["picoexplorer_disabled.ogg"])
self.talk(["picotutor_ok.ogg"])
elif isinstance(message, Message.PICOCOMMENT):
self.talk(["ok.ogg"])
elif isinstance(message, Message.SAVE_GAME):
self.talk(["save_game.ogg"])
elif isinstance(message, Message.READ_GAME):
self.talk(["read_game.ogg"])
elif isinstance(message, Message.CONTLAST):
if message.contlast:
self.talk(["contlast_game_on.ogg"])
else:
self.talk(["contlast_game_off.ogg"])
elif isinstance(message, Message.ALTMOVES):