Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/speaker.py
blob: 196374720ee20702cd4104f429302e5e7cfc8054 (plain)
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Code released in the Public Domain. You can do whatever you want with this package.
# Look at NOTES file to see how to adapt this program.
# Originally written by Pierre Métras <pierre@alterna.tv> for the OLPC XO laptop.


"""
Speak aloud the text given in the XO configured language.

Controls the espeak program available on the OLPC XO laptop.
"""

import sys
import os
import tempfile

from gettext import gettext as _


class Speaker:
    """Speak aloud the given text.
    """

    """espeak parameter: language pitch.
    """
    # TRANS: The language pitch (range [0 - 99], default 50 for English)
    # Look at http://espeak.sourceforge.net/commands.html for details
    PITCH = _("50")


    """espeak parameter: diction speed (average words per minute).
    """
    # TRANS: The diction speed, in average words per minute (range [80 - 390],
    # default 170 for English).
    # Look at http://espeak.sourceforge.net/commands.html for details
    SPEED = _("170")


    """espeak parameter: word gap in units of 10 ms.
    """
    # TRANS: The pause duration between words, in units of 10 ms.
    # Look at http://espeak.sourceforge.net/commands.html for details
    WORD_GAP = _("0")


    """espeak parameter: the language and voice variant.
    """
    # TRANS: The language and voice variant
    # Look at http://espeak.sourceforge.net/commands.html for details, and
    # http://espeak.sourceforge.net/languages.html to see if your language is supported.
    VOICE = _("en")

    # Temporary file to save the 'espeak -w' output and then play it
    # with 'playwave' command
    WAV_TEMP_FILE = tempfile.mktemp()

    # 'espeak' command to generate the wav file to be played with 'playwave'
    ESPEAK_COMMAND = 'espeak -p%s -s%s -g%s -v%s "%s" -w %s'

    # 'playwave' command that plays the wav generated by 'espeak'
    PLAYWAVE_COMMAND = 'playwave %s' % WAV_TEMP_FILE

    def speak(self, text):
        """Speaks aloud the given text.
        """
        text = text.replace("\"", "\\\"")
        child = os.popen(Speaker.ESPEAK_COMMAND % (
                Speaker.PITCH, Speaker.SPEED, Speaker.WORD_GAP, Speaker.VOICE,
                text, Speaker.WAV_TEMP_FILE))
        data = child.read()
        child.close()

        # This is a workaround for SL #4079
        child = os.popen(Speaker.PLAYWAVE_COMMAND)
        data = child.read()
        child.close()


if __name__ == "__main__":
    s = Speaker()
    s.speak("It's two o'clock in the morning")
    s.speak("It's seven hours and thirty-four minutes PM")
    #s.speak("Il est quinze heures et vingt-neuf minutes")
    #s.speak("vingt-deux heures dix-huit minutes")