comparison pyntnclick/speech.py @ 854:79b5c1be9a5e default tip

Remove pyntnclick, it's its own library, now
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 21 Jun 2014 22:06:09 +0200
parents f95830b58336
children
comparison
equal deleted inserted replaced
852:f95830b58336 854:79b5c1be9a5e
1 # speech.py
2 # Copyright Boomslang team, 2010 (see COPYING File)
3 # Speech playing and cache
4
5 import re
6
7 from pyntnclick.sound import get_sound
8
9
10 # cache of string -> sound object mappings
11 _SPEECH_CACHE = {}
12
13 # characters not to allow in filenames
14 _REPLACE_RE = re.compile(r"[^a-z0-9-]+")
15
16
17 class SpeechError(RuntimeError):
18 pass
19
20
21 def get_filename(key, text):
22 """Simplify text to filename."""
23 filename = "%s-%s" % (key, text)
24 filename = filename.lower()
25 filename = _REPLACE_RE.sub("_", filename)
26 filename = filename[:30]
27 filename = "%s.ogg" % filename
28 return filename
29
30
31 def get_speech(thing_name, text):
32 """Load a sound object from the cache."""
33 key = (thing_name, text)
34 if key in _SPEECH_CACHE:
35 return _SPEECH_CACHE[key]
36 filename = get_filename(thing_name, text)
37 _SPEECH_CACHE[key] = sound = get_sound("speech", filename)
38 return sound
39
40
41 def say(thing_name, text):
42 """Play text as speech."""
43 sound = get_speech(thing_name, text)
44 sound.play()