annotate gamelib/sound.py @ 111:18ffaaaa27e7

Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
author Simon Cross <simon@simonx>
date Tue, 24 Aug 2010 14:43:33 +0200
parents 5213b45fcc7e
children 3b293e3b8829
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
111
18ffaaaa27e7 Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents: 107
diff changeset
7 import os
18ffaaaa27e7 Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents: 107
diff changeset
8
107
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
9 from albow.resource import _resource_path, dummy_sound
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 sound_cache = {}
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
12
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
13 def get_sound(*names):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
14 if sound_cache is None:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
15 return dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
16 path = _resource_path("sounds", names)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
17 sound = sound_cache.get(path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
18 if not sound:
111
18ffaaaa27e7 Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents: 107
diff changeset
19 if not os.path.isfile(path):
18ffaaaa27e7 Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents: 107
diff changeset
20 missing_sound("File does not exist", path)
18ffaaaa27e7 Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents: 107
diff changeset
21 return dummy_sound
107
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 from pygame.mixer import Sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
24 except ImportError, e:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
25 no_sound(e)
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 try:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
28 sound = Sound(path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
29 except pygame.error, e:
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
30 missing_sound(e, path)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
31 return dummy_sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
32 sound_cache[path] = sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
33 return sound
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
34
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
35
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
36 def no_sound(e):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
37 global sound_cache
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
38 print "get_sound: %s" % e
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
39 print "get_sound: Sound not available, continuing without it"
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
40 sound_cache = None
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
41
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
42 def disable_sound():
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
43 global sound_cache
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
44 sound_cache = None
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
45
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
46 def missing_sound(e, name):
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
47 print "albow.resource.get_sound: %s: %s" % (name, e)
5213b45fcc7e Initial event sound support
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
48