forked from sugarlabs/speak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.py
1396 lines (1171 loc) · 50.4 KB
/
activity.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
# Speak.activity
# A simple front end to the espeak text-to-speech engine on the XO laptop
# http://wiki.laptop.org/go/Speak
#
# Copyright (C) 2008 Joshua Minor
# Copyright (C) 2014 Walter Bender (major refactoring)
# This file is part of Speak.activity
#
# Parts of Speak.activity are based on code from Measure.activity
# Copyright (C) 2007 Arjun Sarwal - arjun@laptop.org
#
# Speak.activity 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.
#
# Speak.activity 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 Speak.activity. If not, see <http://www.gnu.org/licenses/>.
import logging
import os
import subprocess
import json
import random
from gettext import gettext as _
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
gi.require_version("Gst", "1.0")
gi.require_version('TelepathyGLib', '0.12')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Pango
from gi.repository import GLib
from gi.repository import GObject
from gi.repository import Gst
from gi.repository import TelepathyGLib
GObject.threads_init()
Gst.init(None)
from sugar3.activity import activity
from sugar3.presence import presenceservice
from sugar3.graphics import style
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.radiotoolbutton import RadioToolButton
from sugar3.graphics.toolbarbox import ToolbarBox, ToolbarButton
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton
from sugar3.graphics.objectchooser import ObjectChooser
from sugar3 import mime
from sugar3 import profile
import eye
import glasses
import eyelashes
import halfmoon
import sleepy
import sunglasses
import wireframes
import mouth
import fft_mouth
import waveform_mouth
import face
import photoface
import voice as voice_model
import brain
import chat
from faceselect import FaceSelector
import espeak
SERVICE = 'org.sugarlabs.Speak'
IFACE = SERVICE
PATH = '/org/sugarlabs/Speak'
logger = logging.getLogger('speak')
ACCELEROMETER_DEVICE = '/sys/devices/platform/lis3lv02d/position'
MODE_TYPE = 1
MODE_BOT = 2
MODE_CHAT = 3
FACE_CARTOON = 1
FACE_PHOTO = 2
MOUTHS = [mouth.PeakMouth, waveform_mouth.WaveformMouth, fft_mouth.FFTMouth, ]
NUMBERS = ['one', 'two', 'three', 'four', 'five']
SLEEPY_EYES = sleepy.Sleepy
EYE_DICT = {
'eyes': {'label': _('Round'), 'widget': eye.Eye, 'index': 1},
'glasses': {'label': _('Glasses'), 'widget': glasses.Glasses, 'index': 2},
'halfmoon': {'label': _('Half moon'), 'widget': halfmoon.Halfmoon,
'index': 3},
'eyelashes': {'label': _('Eye lashes'), 'widget': eyelashes.Eyelashes,
'index': 4},
'sunglasses': {'label': _('Sunglasses'), 'widget': sunglasses.Sunglasses,
'index': 5},
'wireframes': {'label': _('Wire frames'), 'widget': wireframes.Wireframes,
'index': 6},
}
DELAY_BEFORE_SPEAKING = 1500 # milleseconds
IDLE_DELAY = 120000 # milleseconds
IDLE_PHRASES = ['zzzzzzzzz', _('I am bored.'), _('Talk to me.'),
_('I am sleepy.'), _('Are you still there?'),
_('Please type something.'),
_('Do you have anything to say to me?'), _('Hello?')]
SIDEWAYS_PHRASES = [_('Whoa! Sideways!'), _("I'm on my side."), _('Uh oh.'),
_('Wheeeee!'), _('Hey! Put me down!'), _('Falling over!')]
SLASH = '-x-SLASH-x-' # slash safe encoding
def _luminance(color):
''' Calculate luminance value '''
return int(color[1:3], 16) * 0.3 + int(color[3:5], 16) * 0.6 + \
int(color[5:7], 16) * 0.1
def _lighter_color(colors):
''' Which color is lighter? Use that one for the text nick color '''
if _luminance(colors[0]) > _luminance(colors[1]):
return 0
return 1
def _has_accelerometer():
return os.path.exists(ACCELEROMETER_DEVICE) and _is_tablet_mode()
def _is_tablet_mode():
try:
fp = open('/dev/input/event4', 'rb')
fp.close()
except IOError:
return False
try:
output = subprocess.call(
['evtest', '--query', '/dev/input/event4', 'EV_SW',
'SW_TABLET_MODE'])
except (OSError, subprocess.CalledProcessError):
return False
if output == 10:
return True
return False
class SpeakActivity(activity.Activity):
def __init__(self, handle):
super(SpeakActivity, self).__init__(handle)
self._notebook = Gtk.Notebook()
self.set_canvas(self._notebook)
self._notebook.show()
self._colors = profile.get_color().to_string().split(',')
lighter = style.Color(self._colors[
_lighter_color(self._colors)])
self._mode = MODE_TYPE
self._tablet_mode = _is_tablet_mode()
self._robot_idle_id = None
self._active_eyes = None
self._active_number_of_eyes = None
self._current_voice = None
self._face_type = FACE_CARTOON
# make an audio device for playing back and rendering audio
self.connect('notify::active', self._active_cb)
self._cfg = {}
# make a box to type into
self._entry_box = Gtk.HBox()
if self._tablet_mode:
self._entry = Gtk.Entry()
self._entry_box.pack_start(self._entry, True, True, 0)
talk_button = ToolButton('microphone')
talk_button.set_tooltip(_('Speak'))
talk_button.connect('clicked', self._talk_cb)
self._entry_box.pack_end(talk_button, False, True, 0)
else:
self._entrycombo = Gtk.ComboBoxText.new_with_entry()
self._entrycombo.connect('changed', self._combo_changed_cb)
self._entry = self._entrycombo.get_child()
self._entry.set_size_request(-1, style.GRID_CELL_SIZE)
self._entry_box.pack_start(self._entrycombo, True, True, 0)
self._entry.set_editable(True)
self._entry.connect('activate', self._entry_activate_cb)
self._entry.connect('key-press-event', self._entry_key_press_cb)
self._entry.modify_font(Pango.FontDescription('sans bold 24'))
self._entry_box.show()
self.face = face.View(fill_color=lighter)
self._cartoon_face = self.face
self.face.set_size_request(
-1, Gdk.Screen.height() - 2 * style.GRID_CELL_SIZE)
self.face.show()
# layout the screen
self._box = Gtk.VBox(homogeneous=False)
if self._tablet_mode:
self._box.pack_start(self._entry_box, False, True, 0)
self._box.pack_start(self.face, True, True, 0)
else:
self._box.pack_start(self.face, True, False, 0)
self._box.pack_start(self._entry_box, True, True, 0)
self.add_events(Gdk.EventMask.POINTER_MOTION_HINT_MASK |
Gdk.EventMask.POINTER_MOTION_MASK)
self.connect('motion_notify_event', self._mouse_moved_cb)
self._box.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self._box.connect('button_press_event', self._mouse_clicked_cb)
# desktop
self._notebook.show()
self._notebook.props.show_border = False
self._notebook.props.show_tabs = False
self._box.show_all()
self._notebook.append_page(self._box, Gtk.Label(''))
self._chat = chat.View()
self._chat.show_all()
self._notebook.append_page(self._chat, Gtk.Label(''))
# make the text box active right away
if not self._tablet_mode:
self._entry.grab_focus()
self._entry.connect('move-cursor', self._cursor_moved_cb)
self._entry.connect('changed', self._cursor_moved_cb)
toolbox = ToolbarBox()
self._activity_button = ActivityToolbarButton(self)
self._activity_button.connect('clicked', self._configure_cb)
toolbox.toolbar.insert(self._activity_button, -1)
self._mode_type = RadioToolButton(
icon_name='mode-type')
self._mode_type.set_tooltip(_('Type something to hear it'))
self._mode_type.connect('toggled', self.__toggled_mode_type_cb)
toolbox.toolbar.insert(self._mode_type, -1)
mode_robot = RadioToolButton(
icon_name='mode-robot',
group=self._mode_type)
mode_robot.set_tooltip(_('Ask robot any question'))
mode_robot.connect('toggled', self.__toggled_mode_robot_cb)
toolbox.toolbar.insert(mode_robot, -1)
self._mode_chat = RadioToolButton(
icon_name='mode-chat',
group=self._mode_type)
self._mode_chat.set_tooltip(_('Voice chat'))
self._mode_chat.connect('toggled', self.__toggled_mode_chat_cb)
toolbox.toolbar.insert(self._mode_chat, -1)
self._voice_button = ToolbarButton(
page=self._make_voice_bar(),
label=_('Voice'),
icon_name='voice')
self._voice_button.connect('clicked', self._configure_cb)
toolbox.toolbar.insert(self._voice_button, -1)
self._face_button = ToolbarButton(
page=self._make_face_bar(),
label=_('Face'),
icon_name='face')
self._face_button.connect('clicked', self._configure_cb)
toolbox.toolbar.insert(self._face_button, -1)
separator = Gtk.SeparatorToolItem()
separator.set_draw(False)
separator.set_expand(True)
toolbox.toolbar.insert(separator, -1)
toolbox.toolbar.insert(StopButton(self), -1)
toolbox.show_all()
self.toolbar_box = toolbox
Gdk.Screen.get_default().connect('size-changed',
self._configure_cb)
self._first_time = True
self._new_instance()
self._configure_cb()
self._poll_accelerometer()
if self.shared_activity:
# we are joining the activity
self.connect('joined', self._joined_cb)
if self.get_shared():
# we have already joined
self._joined_cb(self)
self._mode_chat.set_active(True)
self._setup_chat_mode()
elif handle.uri:
# XMPP non-sugar3 incoming chat, not sharable
self._activity_button.props.page.share.props.visible = \
False
self._one_to_one_connection(handle.uri)
else:
# we are creating the activity
self.connect('shared', self._shared_cb)
def _toolbar_expanded(self):
if self._activity_button.is_expanded():
return True
if self._voice_button.is_expanded():
return True
if self._face_button.is_expanded():
return True
return False
def _configure_cb(self, event=None):
self._entry.set_size_request(-1, style.GRID_CELL_SIZE)
if self._toolbar_expanded():
self.face.set_size_request(
-1, Gdk.Screen.height() - 3 * style.GRID_CELL_SIZE)
self._chat.resize_chat_box(expanded=True)
self._chat.resize_buddy_list()
else:
self.face.set_size_request(
-1, Gdk.Screen.height() - 2 * style.GRID_CELL_SIZE)
self._chat.resize_chat_box()
self._chat.resize_buddy_list()
def _new_instance(self):
if self._first_time:
# self.voices.connect('changed', self.__changed_voices_cb)
self.pitchadj.connect('value_changed', self._pitch_adjusted_cb)
self.rateadj.connect('value_changed', self._rate_adjusted_cb)
if self._active_number_of_eyes is None:
self._number_of_eyes_changed_event_cb(None, None, 'two', True)
if self._active_eyes is None:
self._eyes_changed_event_cb(None, None, 'eyes', True)
self._mouth_changed_cb(None, True)
self.face.look_ahead()
presenceService = presenceservice.get_instance()
self.owner = presenceService.get_owner()
if self._first_time:
# say hello to the user
if self._tablet_mode:
self._entry.props.text = _('Hello %s.') \
% self.owner.props.nick.encode('utf-8', 'ignore')
self.face.say_notification(_('Hello %s. Please Type something.')
% self.owner.props.nick)
else:
if self._tablet_mode:
self._entry.props.text = _('Welcome back %s.') \
% self.owner.props.nick.encode('utf-8', 'ignore')
self.face.say_notification(_('Welcome back %s.')
% self.owner.props.nick)
self._set_idle_phrase(speak=False)
self._first_time = False
def read_file(self, file_path):
self._cfg = json.loads(file(file_path, 'r').read())
current_voice = self.face.status.voice
type_ = self._cfg['face_type']
lighter = style.Color(self._colors[_lighter_color(self._colors)])
if type_ == self._face_type:
status = self.face.status = \
face.Status().deserialize(self._cfg['status'])
elif type_ == FACE_CARTOON:
self._set_face(face.View(fill_color=lighter), FACE_CARTOON)
self._cartoon_face = self.face
status = self.face.status = \
face.Status().deserialize(self._cfg['status'])
else:
status = photoface.Status().deserialize(self._cfg['status'])
view = photoface.View(*status.get_args(), fill_color=lighter)
status = view.status
self._set_face(view, FACE_PHOTO)
found_my_voice = False
for name in self._voice_evboxes.keys():
if self._voice_evboxes[name][1] == current_voice:
self._voice_evboxes[name][0].modify_bg(
0, style.COLOR_BLACK.get_gdk_color())
if self._voice_evboxes[name][1] == status.voice and \
not found_my_voice:
self._voice_evboxes[name][0].modify_bg(
0, style.COLOR_BUTTON_GREY.get_gdk_color())
self.face.set_voice(status.voice)
if self._mode == MODE_BOT:
brain.load(self, status.voice)
found_my_voice = True
self.pitchadj.value = self.face.status.pitch
self.rateadj.value = self.face.status.rate
if self._face_type == FACE_CARTOON:
if status.mouth in MOUTHS:
self._mouth_type[MOUTHS.index(status.mouth)].set_active(True)
self._number_of_eyes_changed_event_cb(
None, None, NUMBERS[len(status.eyes) - 1], True)
for name in EYE_DICT.keys():
if status.eyes[0] == EYE_DICT[name]['widget']:
self._eye_type[name].set_icon_name(name + '-selected')
self._eyes_changed_event_cb(None, None, name, True)
break
self._entry.props.text = self._cfg['text'].encode('utf-8', 'ignore')
if not self._tablet_mode:
for i in self._cfg['history']:
self._entrycombo.append_text(i.encode('utf-8', 'ignore'))
self._new_instance()
def write_file(self, file_path):
if self._tablet_mode:
if 'history' in self._cfg:
history = self._cfg['history'] # retain old history
else:
history = []
else:
history = [unicode(i[0], 'utf-8', 'ignore')
for i in self._entrycombo.get_model()]
cfg = {'status': self.face.status.serialize(),
'face_type': self._face_type,
'text': unicode(self._entry.props.text, 'utf-8', 'ignore'),
'history': history, }
file(file_path, 'w').write(json.dumps(cfg))
def _look_at_cursor(self, entry, *ignored):
# make the eyes track the motion of the text cursor
index = entry.props.cursor_position
layout = entry.get_layout()
pos = layout.get_cursor_pos(index)
x = pos[0].x / Pango.SCALE - entry.props.scroll_offset
y = entry.get_allocation().y
self.face.look_at(pos=(x, y))
return False
def _cursor_moved_cb(self, entry, *ignored):
GLib.timeout_add(50, self._look_at_cursor, entry)
def _poll_accelerometer(self):
if _has_accelerometer():
idle_time = self._test_orientation()
GLib.timeout_add(idle_time, self._poll_accelerometer)
def _test_orientation(self):
if _has_accelerometer():
fh = open(ACCELEROMETER_DEVICE)
string = fh.read()
fh.close()
xyz = string[1:-2].split(',')
x = int(xyz[0])
y = int(xyz[1])
# DO SOMETHING HERE
if ((Gdk.Screen.width() > Gdk.Screen.height() and
abs(x) > abs(y)) or
(Gdk.Screen.width() < Gdk.Screen.height() and
abs(x) < abs(y))):
sideways_phrase = random.randint(0, len(SIDEWAYS_PHRASES) - 1)
self.face.say(SIDEWAYS_PHRASES[sideways_phrase])
return IDLE_DELAY # Don't repeat the message for a while
return 1000 # Test again soon
def get_mouse(self):
display = Gdk.Display.get_default()
screen, mouseX, mouseY, modifiers = display.get_pointer()
return mouseX, mouseY
def _mouse_moved_cb(self, widget, event):
# make the eyes track the motion of the mouse cursor
self.face.look_at()
self._chat.look_at()
def _mouse_clicked_cb(self, widget, event):
pass
def _make_voice_bar(self):
voicebar = Gtk.Toolbar()
all_voices = []
for name in sorted(voice_model.allVoices().keys()):
if len(name) < 26:
friendly_name = name
else:
friendly_name = name[:26] + '...'
all_voices.append([voice_model.allVoices()[name], friendly_name])
# A palette for the voice selection
self._voice_evboxes = {}
self._voice_box = Gtk.HBox()
vboxes = [Gtk.VBox(), Gtk.VBox(), Gtk.VBox()]
count = len(voice_model.allVoices().keys())
found_my_voice = False
for i, voice in enumerate(sorted(all_voices)):
label = Gtk.Label()
label.set_use_markup(True)
label.set_justify(Gtk.Justification.LEFT)
label.set_markup('<span size="large">%s</span>' % voice[1])
alignment = Gtk.Alignment.new(0, 0, 0, 0)
alignment.add(label)
label.show()
evbox = Gtk.EventBox()
self._voice_evboxes[voice[1]] = [evbox, voice[0]]
self._voice_evboxes[voice[1]][0].connect(
'button-press-event', self._voices_changed_event_cb, voice)
if voice[0] == self.face.status.voice and not found_my_voice:
self._current_voice = voice
evbox.modify_bg(
0, style.COLOR_BUTTON_GREY.get_gdk_color())
found_my_voice = True
evbox.add(alignment)
alignment.show()
if i < count / 3:
vboxes[0].pack_start(evbox, True, True, 0)
elif i < 2 * count / 3:
vboxes[1].pack_start(evbox, True, True, 0)
else:
vboxes[2].pack_start(evbox, True, True, 0)
self._voice_box.pack_start(vboxes[0], True, True,
style.DEFAULT_PADDING)
self._voice_box.pack_start(vboxes[1], True, True,
style.DEFAULT_PADDING)
self._voice_box.pack_start(vboxes[2], True, True,
style.DEFAULT_PADDING)
voice_palette_button = ToolButton('module-language')
voice_palette_button.set_tooltip(_('Choose voice:'))
self._voice_palette = voice_palette_button.get_palette()
self._voice_palette.set_content(self._voice_box)
self._voice_box.show_all()
voice_palette_button.connect('clicked', self._face_palette_cb)
voicebar.insert(voice_palette_button, -1)
voice_palette_button.show()
brain_voices = []
for name in sorted(brain.BOTS.keys()):
brain_voices.append([voice_model.allVoices()[name], name])
self._brain_evboxes = {}
self._brain_box = Gtk.HBox()
vboxes = Gtk.VBox()
count = brain.BOTS.keys()
found_my_voice = False
for i, voice in enumerate(brain_voices):
label = Gtk.Label()
label.set_use_markup(True)
label.set_justify(Gtk.Justification.LEFT)
label.set_markup('<span size="large">%s</span>' % voice[1])
alignment = Gtk.Alignment.new(0, 0, 0, 0)
alignment.add(label)
label.show()
evbox = Gtk.EventBox()
self._brain_evboxes[voice[1]] = [evbox, voice[0]]
self._brain_evboxes[voice[1]][0].connect(
'button-press-event', self._voices_changed_event_cb, voice)
if voice[0] == self.face.status.voice and not found_my_voice:
evbox.modify_bg(
0, style.COLOR_BUTTON_GREY.get_gdk_color())
found_my_voice = True
evbox.add(alignment)
alignment.show()
vboxes.pack_start(evbox, True, True, 0)
self._brain_box.pack_start(vboxes, True, True, style.DEFAULT_PADDING)
self._brain_box.show_all()
separator = Gtk.SeparatorToolItem()
separator.set_draw(True)
separator.set_expand(False)
voicebar.insert(separator, -1)
self.pitchadj = Gtk.Adjustment(self.face.status.pitch,
espeak.PITCH_MIN, espeak.PITCH_MAX,
1, espeak.PITCH_MAX / 10, 0)
pitchbar = Gtk.HScale.new(self.pitchadj)
pitchbar.set_draw_value(False)
pitchbar.set_size_request(240, 15)
pitchbar_toolitem = ToolWidget(widget=pitchbar, label_text=_('Pitch:'))
voicebar.insert(pitchbar_toolitem, -1)
self.rateadj = Gtk.Adjustment(self.face.status.rate,
espeak.RATE_MIN, espeak.RATE_MAX,
1, espeak.RATE_MAX / 10, 0)
ratebar = Gtk.HScale.new(self.rateadj)
ratebar.set_draw_value(False)
ratebar.set_size_request(240, 15)
ratebar_toolitem = ToolWidget(widget=ratebar, label_text=_('Rate:'))
voicebar.insert(ratebar_toolitem, -1)
voicebar.show_all()
return voicebar
def _pitch_adjusted_cb(self, adjustment):
self.face.status.pitch = adjustment.get_value()
self.face.say_notification(_('pitch adjusted'))
def _rate_adjusted_cb(self, adjustment):
self.face.status.rate = adjustment.get_value()
self.face.say_notification(_('rate adjusted'))
def _make_face_bar(self):
facebar = Gtk.Toolbar()
self._photo_face = ToolButton('photoface')
self._photo_face.set_tooltip(_('Set face from photo'))
self._photo_face.connect('clicked', self._photo_face_cb)
facebar.insert(self._photo_face, -1)
self._photo_face.show()
self._clear = ToolButton('face')
self._clear.set_tooltip(_('Clear photo face'))
self._clear.set_sensitive(False)
self._clear.connect('clicked', self._clear_photo_cb)
facebar.insert(self._clear, -1)
self._clear.show()
separator = Gtk.SeparatorToolItem()
separator.set_draw(True)
separator.set_expand(False)
facebar.insert(separator, -1)
self._mouth_type = []
button = RadioToolButton(
icon_name='mouth',
group=None)
button.set_tooltip(_('Simple'))
button.connect('clicked', self._mouth_changed_cb, False)
facebar.insert(button, -1)
self._mouth_type.append(button)
button = RadioToolButton(
icon_name='waveform',
group=self._mouth_type[0])
button.set_tooltip(_('Waveform'))
button.connect('clicked', self._mouth_changed_cb, False)
facebar.insert(button, -1)
self._mouth_type.append(button)
button = RadioToolButton(
icon_name='frequency',
group=self._mouth_type[0])
button.set_tooltip(_('Frequency'))
button.connect('clicked', self._mouth_changed_cb, False)
facebar.insert(button, -1)
self._mouth_type.append(button)
separator = Gtk.SeparatorToolItem()
separator.set_draw(True)
separator.set_expand(False)
facebar.insert(separator, -1)
eye_box = Gtk.VBox()
self._eye_type = {}
for name in EYE_DICT.keys():
self._eye_type[name] = ToolButton(name)
self._eye_type[name].connect('clicked',
self._eyes_changed_event_cb,
None, name, False)
label = Gtk.Label(EYE_DICT[name]['label'])
hbox = Gtk.HBox()
hbox.pack_start(self._eye_type[name], True, True, 0)
self._eye_type[name].show()
hbox.pack_start(label, True, True, 0)
label.show()
evbox = Gtk.EventBox()
evbox.connect('button-press-event', self._eyes_changed_event_cb,
name, False)
evbox.add(hbox)
hbox.show()
eye_box.pack_start(evbox, True, True, 0)
eye_palette_button = ToolButton('eyes')
eye_palette_button.set_tooltip(_('Choose eyes:'))
palette = eye_palette_button.get_palette()
palette.set_content(eye_box)
eye_box.show_all()
eye_palette_button.connect('clicked', self._face_palette_cb)
facebar.insert(eye_palette_button, -1)
eye_palette_button.show()
number_of_eyes_box = Gtk.VBox()
self._number_of_eyes_type = {}
for name in NUMBERS:
self._number_of_eyes_type[name] = ToolButton(name)
self._number_of_eyes_type[name].connect(
'clicked', self._number_of_eyes_changed_event_cb,
None, name, False)
label = Gtk.Label(name)
hbox = Gtk.HBox()
hbox.pack_start(self._number_of_eyes_type[name], True, True, 0)
self._number_of_eyes_type[name].show()
hbox.pack_start(label, True, True, 0)
label.show()
evbox = Gtk.EventBox()
evbox.connect('button-press-event',
self._number_of_eyes_changed_event_cb,
name, False)
evbox.add(hbox)
hbox.show()
number_of_eyes_box.pack_start(evbox, True, True, 0)
number_of_eyes_palette_button = ToolButton('number')
number_of_eyes_palette_button.set_tooltip(_('Eyes number:'))
palette = number_of_eyes_palette_button.get_palette()
palette.set_content(number_of_eyes_box)
number_of_eyes_box.show_all()
number_of_eyes_palette_button.connect('clicked', self._face_palette_cb)
facebar.insert(number_of_eyes_palette_button, -1)
number_of_eyes_palette_button.show()
self._cartoon_face_buttons = self._mouth_type + \
[eye_palette_button, number_of_eyes_palette_button]
facebar.show_all()
return facebar
def _photo_face_cb(self, widget):
chooser = ObjectChooser(parent=self,
what_filter=mime.GENERIC_TYPE_IMAGE)
result = chooser.run()
if result == Gtk.ResponseType.ACCEPT:
jobject = chooser.get_selected_object()
if jobject and jobject.file_path:
selector = FaceSelector(jobject.file_path)
selector.connect('face-processed',
self._photo_face_processed_cb)
selector.connect('cancel', self._photo_face_cancel_cb)
self._notebook.append_page(selector, Gtk.Label(''))
selector.show()
num = self._notebook.page_num(selector)
self._notebook.set_current_page(num)
chooser.destroy()
def _photo_face_processed_cb(self, widget, *face_data):
lighter = style.Color(self._colors[_lighter_color(self._colors)])
self._set_face(photoface.View(*face_data, fill_color=lighter),
FACE_PHOTO)
def _photo_face_cancel_cb(self, widget):
self._notebook.set_current_page(0)
def _set_face(self, view, type_):
self._face_type = type_
cartoon = type_ == FACE_CARTOON
self.face.shut_up()
self._box.remove(self.face)
self._box.remove(self._entry_box)
self.face = view
self.face.set_size_request(
-1, Gdk.Screen.height() - 2 * style.GRID_CELL_SIZE)
if self._tablet_mode:
self._box.pack_start(self._entry_box, False, True, 0)
self._box.pack_start(self.face, True, True, 0)
else:
self._box.pack_start(self.face, True, True, 0)
self._box.pack_start(self._entry_box, True, True, 0)
self.face.show()
if not cartoon and self._mode == MODE_CHAT:
self._mode = MODE_TYPE
self._mode_type.set_active(True)
self._mode_chat.set_active(False)
self._chat.shut_up()
self._voice_palette.set_content(self._voice_box)
self._set_voice()
self._notebook.set_current_page(0)
self._photo_face.set_sensitive(cartoon)
self._clear.set_sensitive(not cartoon)
for bnt in self._cartoon_face_buttons:
bnt.set_sensitive(cartoon)
self._mode_chat.set_sensitive(cartoon)
def _clear_photo_cb(self, widget):
self._set_face(self._cartoon_face, FACE_CARTOON)
def _face_palette_cb(self, button):
palette = button.get_palette()
palette.popdown(immediate=True)
def _get_active_mouth(self):
for i, button in enumerate(self._mouth_type):
if button.get_active():
return MOUTHS[i]
def _mouth_changed_cb(self, ignored, quiet):
if self._face_type == FACE_PHOTO:
return
value = self._get_active_mouth()
if value is None:
return
self.face.status.mouth = value
self._update_face()
if not quiet:
self.face.say_notification(_('mouth changed'))
def _voices_changed_event_cb(self, widget, event, voice):
logging.debug('voices_changed_event_cb %r %s' % (voice[0], voice[1]))
if self._mode == MODE_BOT:
evboxes = self._brain_evboxes
else:
evboxes = self._voice_evboxes
for old_voice in evboxes.keys():
if evboxes[old_voice][1] == self.face.status.voice:
evboxes[old_voice][0].modify_bg(
0, style.COLOR_BLACK.get_gdk_color())
break
evboxes[voice[1]][0].modify_bg(
0, style.COLOR_BUTTON_GREY.get_gdk_color())
self.face.set_voice(voice[0])
if self._mode == MODE_BOT:
brain.load(self, voice[0])
else:
self._current_voice = voice
def _get_active_eyes(self):
for name in EYE_DICT.keys():
if EYE_DICT[name]['index'] == self._active_eyes:
return EYE_DICT[name]['widget']
return None
def _eyes_changed_event_cb(self, widget, event, name, quiet):
if self._face_type == FACE_PHOTO:
return
if self._active_eyes is not None:
for old_name in EYE_DICT.keys():
if EYE_DICT[old_name]['index'] == self._active_eyes:
self._eye_type[old_name].set_icon_name(old_name)
break
if self._active_number_of_eyes is None:
self._active_number_of_eyes = 2
if name is not None:
self._active_eyes = EYE_DICT[name]['index']
self._eye_type[name].set_icon_name(name + '-selected')
value = EYE_DICT[name]['widget']
self.face.status.eyes = [value] * self._active_number_of_eyes
self._update_face()
if not quiet:
self.face.say_notification(_('eyes changed'))
def _number_of_eyes_changed_event_cb(self, widget, event, name, quiet):
if self._face_type == FACE_PHOTO:
return
if self._active_number_of_eyes is not None:
old_name = NUMBERS[self._active_number_of_eyes - 1]
self._number_of_eyes_type[old_name].set_icon_name(old_name)
if name in NUMBERS:
self._active_number_of_eyes = NUMBERS.index(name) + 1
self._number_of_eyes_type[name].set_icon_name(name + '-selected')
if self._active_eyes is not None:
for eye_name in EYE_DICT.keys():
if EYE_DICT[eye_name]['index'] == self._active_eyes:
value = EYE_DICT[eye_name]['widget']
self.face.status.eyes = \
[value] * self._active_number_of_eyes
self._update_face()
if not quiet:
self.face.say_notification(_('eyes changed'))
break
def _update_face(self):
self.face.update()
self._chat.update(self.face.status)
def _combo_changed_cb(self, combo):
# when a new item is chosen, make sure the text is selected
if not self._entry.is_focus():
if not self._tablet_mode:
self._entry.grab_focus()
self._entry.select_region(0, -1)
def _entry_key_press_cb(self, combo, event):
# make the up/down arrows navigate through our history
if self._tablet_mode:
return
keyname = Gdk.keyval_name(event.keyval)
if keyname == 'Up':
index = self._entrycombo.get_active()
if index > 0:
index -= 1
self._entrycombo.set_active(index)
self._entry.select_region(0, -1)
return True
elif keyname == 'Down':
index = self._entrycombo.get_active()
if index < len(self._entrycombo.get_model()) - 1:
index += 1
self._entrycombo.set_active(index)
self._entry.select_region(0, -1)
return True
return False
def _entry_activate_cb(self, entry):
# the user pressed Return, say the text and clear it out
text = entry.get_text()
if self._tablet_mode:
self._dismiss_OSK(entry)
timeout = DELAY_BEFORE_SPEAKING
else:
timeout = 100
GLib.timeout_add(timeout, self._speak_the_text, entry, text)
def _dismiss_OSK(self, entry):
entry.hide()
entry.show()
def _talk_cb(self, button):
text = self._entry.props.text
self._speak_the_text(self._entry, text)
def _speak_the_text(self, entry, text):
self._remove_idle()
if text:
self.face.look_ahead()
# speak the text
if self._mode == MODE_BOT:
self.face.say(brain.respond(text))
else:
self.face.say(text)
if text and not self._tablet_mode:
# add this text to our history unless it is the same as
# the last item
history = self._entrycombo.get_model()
if len(history) == 0 or history[-1][0] != text:
self._entrycombo.append_text(text)
# don't let the history get too big
while len(history) > 20:
self._entrycombo.remove(0)
# select the new item
self._entrycombo.set_active(len(history) - 1)
if text:
# select the whole text
entry.select_region(0, -1)
# Launch an robot idle phrase after 2 minutes
self._robot_idle_id = GLib.timeout_add(IDLE_DELAY,
self._set_idle_phrase)
def _load_sleeping_face(self):
if self._face_type == FACE_PHOTO:
return
current_eyes = self.face.status.eyes
self.face.status.eyes = [SLEEPY_EYES] * self._active_number_of_eyes
self._update_face()
self.face.status.eyes = current_eyes
def _set_idle_phrase(self, speak=True):
if speak:
self._load_sleeping_face()
if self.props.active and not self.shared_activity:
idle_phrase = IDLE_PHRASES[random.randint(
0, len(IDLE_PHRASES) - 1)]
self.face.say(idle_phrase)
self._robot_idle_id = GLib.timeout_add(IDLE_DELAY,
self._set_idle_phrase)