diff options
Diffstat (limited to 'speaker.py')
-rw-r--r-- | speaker.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/speaker.py b/speaker.py new file mode 100644 index 0000000..bec926b --- /dev/null +++ b/speaker.py @@ -0,0 +1,71 @@ +#! /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 + +from pgettext import pgettext as _p + + +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 = _p("espeak-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 = _p("espeak-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 = _p("espeak-wgap", "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 = _p("espeak-voice", "en") + + + def speak(self, text): + """Speaks aloud the given text. + """ + text = text.replace("\"", "\\\"") + child = os.popen("espeak -p%s -s%s -g%s -v%s \"%s\"" % (Speaker.PITCH, Speaker.SPEED, Speaker.WORD_GAP, Speaker.VOICE, text)) + data = child.read() + err = child.close() + if err: + print "espeak terminated with return code %d" % err + + +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") + |