Package joy :: Module speak
[hide private]
[frames] | no frames]

Source Code for Module joy.speak

 1  from subprocess import Popen, PIPE 
 2  from warnings import warn 
 3   
 4  __all__ = ['say','getEngineName'] 
 5   
 6  """ 
 7  Module speak provides a simple speech synthesis interface under unix-like os-s. 
 8  It works by using the commandline program espeak. 
 9   
10  Typical usage: 
11  >>> from speak import say 
12  >>> say("hello world") 
13  >>> say("it's wonderful to have text to speech working") 
14   
15  If espeak is not supported, the say() function does nothing. 
16   
17  LICENSE GPL 3.0 (c) Shai Revzen, U. Penn, 2010 
18  """ 
19  global __SYNTH 
20  __SYNTH = None 
21   
22 -def _espeak_say(text):
23 """ 24 Uses the espeak speech synthesis engine to read text aloud 25 26 This function uses a single instance of espeak throughout its 27 life. This subprocess is created when the first call is made. 28 """ 29 global __SYNTH 30 s = __SYNTH 31 if s and s.returncode is not None: 32 warn("espeak terminated with code %d" % __SYNTH.returncode ) 33 s=None 34 if s is None: 35 s = Popen( "espeak -v f3 -s 200 >/dev/null 2>/dev/null", shell=True, stdin=PIPE ) 36 if s is None: 37 return 38 try: 39 s.stdin.write(text+"\n") 40 except IOError,ioe: 41 warn("espeak pipe failed with error %s" % ioe) 42 __SYNTH = None 43 # Try again 44 _espeak_say(text) 45 __SYNTH = s
46
47 -def _nospeak_say(text):
48 """ 49 *** Speech synthesis is disabled *** 50 51 Do you have espeak installed? 52 """ 53 pass
54
55 -def getEngineName():
56 """ 57 Returns engine name for speech synthesis engine, or '' if speech is not 58 supported. 59 """ 60 if say == _nospeak_say: 61 return '' 62 return say.__name__[1:-4]
63 64 esp = Popen( "espeak -x -q okay >/dev/null 2>/dev/null", shell=True ) 65 if esp.wait(): 66 say = _nospeak_say 67 else: 68 say = _espeak_say 69