annotate gamelib/sound.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 5213b45fcc7e
children 18ffaaaa27e7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
1 # Sound management for Suspended Sentence
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
2
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
3 # This re-implements some of the albow.resource code to
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
4 # a) work around an annoying bugs
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
5 # b) add some missing functionality (disable_sound)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
6
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
7 from albow.resource import _resource_path, dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
8
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
9 sound_cache = {}
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
10
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
11 def get_sound(*names):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
12 if sound_cache is None:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
13 return dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
14 path = _resource_path("sounds", names)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
15 sound = sound_cache.get(path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
16 if not sound:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
17 try:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
18 from pygame.mixer import Sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
19 except ImportError, e:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
20 no_sound(e)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
21 return dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
22 try:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
23 sound = Sound(path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
24 except pygame.error, e:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
25 missing_sound(e, path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
26 return dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
27 sound_cache[path] = sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
28 return sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
29
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
30
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
31 def no_sound(e):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
32 global sound_cache
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
33 print "get_sound: %s" % e
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
34 print "get_sound: Sound not available, continuing without it"
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
35 sound_cache = None
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
36
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
37 def disable_sound():
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
38 global sound_cache
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
39 sound_cache = None
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
40
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
41 def missing_sound(e, name):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
42 print "albow.resource.get_sound: %s: %s" % (name, e)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
43