Mercurial > pyntnclick
comparison gamelib/speech.py @ 109:66898d810247
Add hackish speech support (run regen-speech.py to generate files -- needs espeak and oggenc).
author | Simon Cross <simon@simonx> |
---|---|
date | Tue, 24 Aug 2010 14:32:52 +0200 |
parents | |
children | 545dee3bd8e9 |
comparison
equal
deleted
inserted
replaced
108:ab11689aec36 | 109:66898d810247 |
---|---|
1 # speech.py | |
2 # Copyright Boomslang team, 2010 (see COPYING File) | |
3 # Speech playing and cache | |
4 | |
5 import re | |
6 | |
7 from 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 print filename, sound | |
39 return sound | |
40 | |
41 | |
42 def say(thing_name, text): | |
43 """Play text as speech.""" | |
44 sound = get_speech(thing_name, text) | |
45 if sound is not None: | |
46 sound.play() |