This file is indexed.

/usr/lib/python2.7/dist-packages/sugar3/speech.py is in python-sugar3 0.112-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# Copyright (C) 2011 One Laptop Per Child
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import logging
from gettext import gettext as _

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gio
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject

_HAS_GST = True
try:
    gi.require_version('Gst', '1.0')
    from gi.repository import Gst
    Gst.init(None)
    Gst.parse_launch('espeak')
except:
    logging.error('Gst or the espeak plugin is not installed in the system.')
    _HAS_GST = False

from sugar3 import power

DEFAULT_PITCH = 0


DEFAULT_RATE = 0


_SAVE_TIMEOUT = 500


# This voice names are use dto allow the translation of the voice names.
# If espeak add new voices, we need update this list.

translated_espeak_voices = {
    # Translators:  http://en.wikipedia.org/wiki/Afrikaans_language
    'af': _('Afrikaans'),
    # Translators:  http://en.wikipedia.org/wiki/Amharic
    'am': _('Amharic'),
    # Translators:  http://en.wikipedia.org/wiki/Aragonese_language
    'an': _('Aragonese'),
    # Translators:  http://en.wikipedia.org/wiki/Arabic
    'ar': _('Arabic'),
    # Translators:  http://en.wikipedia.org/wiki/Assamese_language
    'as': _('Assamese'),
    # Translators:  http://en.wikipedia.org/wiki/Azerbaijani_language
    'az': _('Azerbaijani'),
    # Translators:  http://en.wikipedia.org/wiki/Bulgarian_language
    'bg': _('Bulgarian'),
    # Translators:  http://en.wikipedia.org/wiki/Bengali_language
    'bn': _('Bengali'),
    # Translators:  http://en.wikipedia.org/wiki/Bosnian_language
    'bs': _('Bosnian'),
    # Translators:  http://en.wikipedia.org/wiki/Catalan_language
    'ca': _('Catalan'),
    # Translators:  http://en.wikipedia.org/wiki/Czech_language
    'cs': _('Czech'),
    # Translators:  http://en.wikipedia.org/wiki/Welsh_language
    'cy': _('Welsh'),
    # Translators:  http://en.wikipedia.org/wiki/Danish_language
    'da': _('Danish'),
    # Translators:  http://en.wikipedia.org/wiki/German_language
    'de': _('German'),
    # Translators:  http://en.wikipedia.org/wiki/Modern_Greek
    'el': _('Greek'),
    'en': _('Default'),
    # Translators:  http://en.wikipedia.org/wiki/British_English
    'en_gb': _('English Britain'),
    # Translators:  http://en.wikipedia.org/wiki/Scottish_English
    'en_gb-scotland': _('English scottish'),
    'en_gb-x-gbclan': _('English-north'),
    # Translators:  http://en.wikipedia.org/wiki/Received_Pronunciation
    'en_gb-x-rp': _('English_rp'),
    # Translators:  http://en.wikipedia.org/wiki/West_Midlands_English
    'en_gb-x-gbcwmd': _('English_wmids'),
    # Translators:  http://en.wikipedia.org/wiki/American_English
    'en_us': _('English USA'),
    # Translators:  http://en.wikipedia.org/wiki/Caribbean_English
    'en_029': _('English West Indies'),
    # Translators:  http://en.wikipedia.org/wiki/Esperanto
    'eo': _('Esperanto'),
    # Translators:  http://en.wikipedia.org/wiki/Spanish_language
    'es': _('Spanish'),
    'es_419': _('Spanish latin american'),
    # Translators:  http://en.wikipedia.org/wiki/Estonian_language
    'et': _('Estonian'),
    # Translators:  http://en.wikipedia.org/wiki/Basque_language
    'eu': _('Basque'),
    # Translators:  http://en.wikipedia.org/wiki/Persian_language
    # TODO: distinguish between Persian+English-UK and Persian+English-US
    'fa': _('Farsi'),
    # Translators:  http://en.wikipedia.org/wiki/Fingilish
    'fa_latn': _('Farsi-pinglish'),
    # Translators:  http://en.wikipedia.org/wiki/Finnish_language
    'fi': _('Finnish'),
    # Translators:  http://en.wikipedia.org/wiki/Belgian_French
    'fr_be': _('French belgium'),
    # Translators:  http://en.wikipedia.org/wiki/French_language
    'fr_fr': _('French'),
    # Translators:  http://en.wikipedia.org/wiki/Irish_language
    'ga': _('Irish-gaeilge'),
    # Translators:  https://en.wikipedia.org/wiki/Scottish_Gaelic
    'gd': _('Scottish-gaelic'),
    # Translators:  https://en.wikipedia.org/wiki/Guarani_language
    'gn': _('Guarani'),
    # Translators:  http://en.wikipedia.org/wiki/Ancient_Greek
    'grc': _('Greek-ancient'),
    # Translators:  https://en.wikipedia.org/wiki/Gujarati_language
    'gu': _('Gujarati'),
    # Translators:  http://en.wikipedia.org/wiki/Hindi
    'hi': _('Hindi'),
    # Translators:  http://en.wikipedia.org/wiki/Croatian_language
    'hr': _('Croatian'),
    # Translators:  http://en.wikipedia.org/wiki/Hungarian_language
    'hu': _('Hungarian'),
    # Translators:  http://en.wikipedia.org/wiki/Armenian_language
    'hy': _('Armenian'),
    # Translators:  http://en.wikipedia.org/wiki/Western_Armenian
    'hy_arevmda': _('Armenian (west)'),
    # Translators:  http://en.wikipedia.org/wiki/Interlingua
    'ia': _('Interlingua'),
    # Translators:  http://en.wikipedia.org/wiki/Indonesian_language
    'id': _('Indonesian'),
    # Translators:  http://en.wikipedia.org/wiki/Icelandic_language
    'is': _('Icelandic'),
    # Translators:  http://en.wikipedia.org/wiki/Italian_language
    'it': _('Italian'),
    # Translators:  http://en.wikipedia.org/wiki/Lojban
    'jbo': _('Lojban'),
    # Translators:  http://en.wikipedia.org/wiki/Japanese_language
    'jp': _('Japanese'),
    # Translators:  http://en.wikipedia.org/wiki/Georgian_language
    'ka': _('Georgian'),
    # Translators:  http://en.wikipedia.org/wiki/Greenlandic_language
    'kl': _('Greenlandic'),
    # Translators:  http://en.wikipedia.org/wiki/Kannada_language
    'kn': _('Kannada'),
    # Translators:  http://en.wikipedia.org/wiki/Korean_language
    'ko': _('Korean'),
    # Translators:  http://en.wikipedia.org/wiki/Kurdish_language
    'ku': _('Kurdish'),
    # Translators:  http://en.wikipedia.org/wiki/Kyrgyz_language
    'ky': _('Kyrgyz'),
    # Translators:  http://en.wikipedia.org/wiki/Latin
    'la': _('Latin'),
    # Translators:  https://en.wikipedia.org/wiki/Lingua_Franca_Nova
    'lfn': _('Lingua Franca Nova'),
    # Translators:  http://en.wikipedia.org/wiki/Lithuanian_language
    'lt': _('Lithuanian'),
    # Translators:  http://en.wikipedia.org/wiki/Latvian_language
    'lv': _('Latvian'),
    # Translators:  http://en.wikipedia.org/wiki/Macedonian_language
    'mk': _('Macedonian'),
    # Translators:  http://en.wikipedia.org/wiki/Malayalam
    'ml': _('Malayalam'),
    # Translators:  http://en.wikipedia.org/wiki/Marathi_language
    'mr': _('Marathi'),
    # Translators:  http://en.wikipedia.org/wiki/Malay_language
    'ms': _('Malay'),
    # Translators:  http://en.wikipedia.org/wiki/Maltese_language
    'mt': _('Maltese'),
    # Translators:  http://en.wikipedia.org/wiki/Burmese_language
    'my': _('Burmese'),
    # Translators:  https://en.wikipedia.org/wiki/Nahuatl
    'nci': _('Nahuatl (classical)'),
    # Translators:  http://en.wikipedia.org/wiki/Nepali_language
    'ne': _('Nepali'),
    # Translators:  http://en.wikipedia.org/wiki/Dutch_language
    'nl': _('Dutch'),
    # Translators:  http://en.wikipedia.org/wiki/Norwegian_language
    'no': _('Norwegian'),
    # Translators:  http://en.wikipedia.org/wiki/Oromo_language
    'om': _('Oromo'),
    # Translators:  https://en.wikipedia.org/wiki/Odia_language
    'or': _('Oriya'),
    # Translators:  http://en.wikipedia.org/wiki/Punjabi_language
    'pa': _('Punjabi'),
    # Translators:  https://en.wikipedia.org/wiki/Papiamento
    'pap': _('Papiamento'),
    # Translators:  http://en.wikipedia.org/wiki/Polish_language
    'pl': _('Polish'),
    # Translators:  http://en.wikipedia.org/wiki/Brazilian_Portuguese
    'pt_br': _('Portuguese (Brazil)'),
    # Translators:  http://en.wikipedia.org/wiki/Portuguese_language
    'pt_pt': _('Portuguese (Portugal)'),
    # Translators:  http://en.wikipedia.org/wiki/Romanian_language
    'ro': _('Romanian'),
    # Translators:  http://en.wikipedia.org/wiki/Russian_language
    'ru': _('Russian'),
    # Translators:  https://en.wikipedia.org/wiki/Sinhalese_language
    'si': _('Sinhala'),
    # Translators:  http://en.wikipedia.org/wiki/Slovak_language
    'sk': _('Slovak'),
    # Translators:  https://en.wikipedia.org/wiki/Slovene_language
    'sl': _('Slovenian'),
    # Translators:  http://en.wikipedia.org/wiki/Albanian_language
    'sq': _('Albanian'),
    # Translators:  http://en.wikipedia.org/wiki/Serbian_language
    'sr': _('Serbian'),
    # Translators:  http://en.wikipedia.org/wiki/Swedish_language
    'sv': _('Swedish'),
    # Translators:  http://en.wikipedia.org/wiki/Swahili_language
    'sw': _('Swahili'),
    # Translators:  http://en.wikipedia.org/wiki/Tamil_language
    'ta': _('Tamil'),
    # Translators:  http://en.wikipedia.org/wiki/Telugu_language
    'te': _('Telugu'),
    # Translators:  https://en.wikipedia.org/wiki/Tswana_language
    'tn': _('Setswana'),
    # Translators:  http://en.wikipedia.org/wiki/Turkish_language
    'tr': _('Turkish'),
    # Translators:  http://en.wikipedia.org/wiki/Tatar_language
    'tt': _('Tatar'),
    # Translators:  http://en.wikipedia.org/wiki/Urdu
    'ur': _('Urdu'),
    # Translators:  http://en.wikipedia.org/wiki/Vietnamese_language
    'vi': _('Vietnam'),
    'vi_vn-x-central': _('Vietnam_hue'),
    'vi_vn-x-south': _('Vietnam_sgn'),
    # Translators:  http://en.wikipedia.org/wiki/Mandarin_Chinese
    'zh': _('Mandarin'),
    # Translators:  http://en.wikipedia.org/wiki/Cantonese
    'zh_yue': _('Cantonese')
}


class SpeechManager(GObject.GObject):

    __gtype_name__ = 'SpeechManager'

    __gsignals__ = {
        'play': (GObject.SignalFlags.RUN_FIRST, None, []),
        'pause': (GObject.SignalFlags.RUN_FIRST, None, []),
        'stop': (GObject.SignalFlags.RUN_FIRST, None, [])
    }

    MIN_PITCH = -100
    MAX_PITCH = 100

    MIN_RATE = -100
    MAX_RATE = 100

    def __init__(self, **kwargs):
        GObject.GObject.__init__(self, **kwargs)
        self._player = None
        if not self.enabled():
            return

        self._player = _GstSpeechPlayer()
        self._player.connect('play', self._update_state, 'play')
        self._player.connect('stop', self._update_state, 'stop')
        self._player.connect('pause', self._update_state, 'pause')
        self._default_voice_name = self._player.get_default_voice()
        self._pitch = DEFAULT_PITCH
        self._rate = DEFAULT_RATE
        self._is_playing = False
        self._is_paused = False
        self._save_timeout_id = -1
        self.restore()

    def enabled(self):
        return _HAS_GST

    def _update_state(self, player, signal):
        self._is_playing = (signal == 'play')
        self._is_paused = (signal == 'pause')
        self.emit(signal)

    def get_is_playing(self):
        return self._is_playing

    is_playing = GObject.property(type=bool, getter=get_is_playing,
                                  setter=None, default=False)

    def get_is_paused(self):
        return self._is_paused

    is_paused = GObject.property(type=bool, getter=get_is_paused,
                                 setter=None, default=False)

    def get_pitch(self):
        return self._pitch

    def get_rate(self):
        return self._rate

    def set_pitch(self, pitch):
        self._pitch = pitch
        if self._save_timeout_id != -1:
            GObject.source_remove(self._save_timeout_id)
        self._save_timeout_id = GObject.timeout_add(_SAVE_TIMEOUT, self.save)

    def set_rate(self, rate):
        self._rate = rate
        if self._save_timeout_id != -1:
            GObject.source_remove(self._save_timeout_id)
        self._save_timeout_id = GObject.timeout_add(_SAVE_TIMEOUT, self.save)

    def say_text(self, text, pitch=None, rate=None, lang_code=None):
        if pitch is None:
            pitch = self._pitch
        if rate is None:
            rate = self._rate
        if lang_code is None:
            voice_name = self._default_voice_name
        else:
            voice_name = self._player.get_all_voices()[lang_code]
        if text:
            logging.debug('PLAYING "%s" lang %s', text, voice_name)
            self._player.speak(pitch, rate, voice_name, text)

    def say_selected_text(self):
        clipboard = Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY)
        clipboard.request_text(self.__primary_selection_cb, None)

    def pause(self):
        self._player.pause_sound_device()

    def restart(self):
        self._player.restart_sound_device()

    def stop(self):
        self._player.stop_sound_device()

    def __primary_selection_cb(self, clipboard, text, user_data):
        self.say_text(text)

    def save(self):
        self._save_timeout_id = -1
        # DEPRECATED
        from gi.repository import GConf
        client = GConf.Client.get_default()
        client.set_int('/desktop/sugar/speech/pitch', self._pitch)
        client.set_int('/desktop/sugar/speech/rate', self._rate)

        settings = Gio.Settings('org.sugarlabs.speech')
        settings.set_int('pitch', self._pitch)
        settings.set_int('rate', self._rate)
        logging.debug('saving speech configuration pitch %s rate %s',
                      self._pitch, self._rate)
        return False

    def restore(self):
        settings = Gio.Settings('org.sugarlabs.speech')
        self._pitch = settings.get_int('pitch')
        self._rate = settings.get_int('rate')
        logging.debug('loading speech configuration pitch %s rate %s',
                      self._pitch, self._rate)

    def get_all_voices(self):
        if self._player:
            return self._player.get_all_voices()
        return None

    def get_all_traslated_voices(self):
        """ deprecated after 0.112, due to method name spelling error """
        if self._player:
            return self._player.get_all_translated_voices()
        return None

    def get_all_translated_voices(self):
        if self._player:
            return self._player.get_all_translated_voices()
        return None


class _GstSpeechPlayer(GObject.GObject):

    __gsignals__ = {
        'play': (GObject.SignalFlags.RUN_FIRST, None, []),
        'pause': (GObject.SignalFlags.RUN_FIRST, None, []),
        'stop': (GObject.SignalFlags.RUN_FIRST, None, [])
    }

    def __init__(self):
        GObject.GObject.__init__(self)
        self._pipeline = None
        self._all_voices = None
        self._all_translated_voices = None

    def restart_sound_device(self):
        if self._pipeline is None:
            logging.debug('Trying to restart not initialized sound device')
            return

        power.get_power_manager().inhibit_suspend()
        self._pipeline.set_state(Gst.State.PLAYING)
        self.emit('play')

    def pause_sound_device(self):
        if self._pipeline is None:
            return

        self._pipeline.set_state(Gst.State.PAUSED)
        power.get_power_manager().restore_suspend()
        self.emit('pause')

    def stop_sound_device(self):
        if self._pipeline is None:
            return

        self._pipeline.set_state(Gst.State.NULL)
        power.get_power_manager().restore_suspend()
        self.emit('stop')

    def make_pipeline(self, command):
        if self._pipeline is not None:
            self.stop_sound_device()
            del self._pipeline

        self._pipeline = Gst.parse_launch(command)

        bus = self._pipeline.get_bus()
        bus.add_signal_watch()
        bus.connect('message', self.__pipe_message_cb)

    def __pipe_message_cb(self, bus, message):
        if message.type in (Gst.MessageType.EOS, Gst.MessageType.ERROR):
            self._pipeline.set_state(Gst.State.NULL)
            self._pipeline = None
            power.get_power_manager().restore_suspend()
            self.emit('stop')

    def speak(self, pitch, rate, voice_name, text):
        # TODO workaround for http://bugs.sugarlabs.org/ticket/1801
        if not [i for i in text if i.isalnum()]:
            return

        self.make_pipeline('espeak name=espeak ! autoaudiosink')
        src = self._pipeline.get_by_name('espeak')

        src.props.text = text
        src.props.pitch = pitch
        src.props.rate = rate
        src.props.voice = voice_name
        src.props.track = 2  # track for marks

        self.restart_sound_device()

    def get_all_voices(self):
        if self._all_voices is not None:
            return self._all_voices
        self._init_voices()
        return self._all_voices

    def get_all_translated_voices(self):
        if self._all_translated_voices is not None:
            return self._all_translated_voices
        self._init_voices()
        return self._all_translated_voices

    def _init_voices(self):
        self._all_voices = {}
        self._all_translated_voices = {}

        for voice in Gst.ElementFactory.make('espeak', None).props.voices:
            name, language, dialect = voice
            if dialect != 'none':
                lang_code = language + '_' + dialect
            else:
                lang_code = language

            self._all_voices[lang_code] = name
            if lang_code in translated_espeak_voices:
                self._all_translated_voices[lang_code] = \
                    translated_espeak_voices[lang_code]
            else:
                self._all_translated_voices[lang_code] = name

    def get_default_voice(self):
        """Try to figure out the default voice, from the current locale ($LANG)
           Fall back to espeak's voice called Default."""
        voices = self.get_all_voices()

        locale = os.environ.get('LANG', '')
        language_location = locale.split('.', 1)[0].lower()
        language = language_location.split('_')[0]
        # if the language is es but not es_es default to es_la (latin voice)
        if language == 'es' and language_location != 'es_es':
            language_location = 'es_419'

        best = voices.get(language_location) or voices.get(language) \
            or 'default'
        return best