comparison pyntnclick/speech.py @ 548:ded4324b236e pyntnclick

Moved stuff around, broke everything.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 11 Feb 2012 13:10:18 +0200
parents gamelib/speech.py@a2c09200a433
children 38fb04728ac5
comparison
equal deleted inserted replaced
547:33ce7ff757c3 548:ded4324b236e
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 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()