Mercurial > boomslang
annotate gamelib/sound.py @ 370:8936c043d7a1
Fix bliken lights description
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Sat, 28 Aug 2010 17:39:00 +0200 |
parents | 7b07ffc37ec0 |
children | 6df527142e69 |
rev | line source |
---|---|
107 | 1 # Sound management for Suspended Sentence |
2 | |
3 # This re-implements some of the albow.resource code to | |
4 # a) work around an annoying bugs | |
5 # b) add some missing functionality (disable_sound) | |
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 |
123 | 9 import pygame |
154 | 10 from pygame.mixer import music |
107 | 11 from albow.resource import _resource_path, dummy_sound |
154 | 12 import albow.music |
107 | 13 |
14 sound_cache = {} | |
15 | |
16 def get_sound(*names): | |
17 if sound_cache is None: | |
18 return dummy_sound | |
19 path = _resource_path("sounds", names) | |
20 sound = sound_cache.get(path) | |
21 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
|
22 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
|
23 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
|
24 return dummy_sound |
107 | 25 try: |
26 from pygame.mixer import Sound | |
27 except ImportError, e: | |
28 no_sound(e) | |
29 return dummy_sound | |
30 try: | |
31 sound = Sound(path) | |
32 except pygame.error, e: | |
33 missing_sound(e, path) | |
34 return dummy_sound | |
35 sound_cache[path] = sound | |
36 return sound | |
37 | |
38 | |
39 def no_sound(e): | |
40 global sound_cache | |
41 print "get_sound: %s" % e | |
42 print "get_sound: Sound not available, continuing without it" | |
43 sound_cache = None | |
188
aebd4fd5b561
no_sound error handler should also disable music
Neil Muller <neil@dip.sun.ac.za>
parents:
154
diff
changeset
|
44 albow.music.music_enabled = False |
107 | 45 |
46 def disable_sound(): | |
47 global sound_cache | |
48 sound_cache = None | |
154 | 49 albow.music.music_enabled = False |
107 | 50 |
51 def missing_sound(e, name): | |
52 print "albow.resource.get_sound: %s: %s" % (name, e) | |
53 | |
154 | 54 |
55 def start_next_music(): | |
56 """Start playing the next item from the current playlist immediately.""" | |
57 if albow.music.music_enabled and albow.music.current_playlist: | |
58 next_music = albow.music.current_playlist.next() | |
59 if next_music: | |
60 #print "albow.music: loading", repr(next_music) | |
61 music.load(next_music) | |
62 music.play() | |
63 albow.music.next_change_delay = albow.music.change_delay | |
64 albow.music.current_music = next_music | |
65 | |
315
7b07ffc37ec0
Suspend background sound for bridge computer detail
Neil Muller <neil@dip.sun.ac.za>
parents:
188
diff
changeset
|
66 def get_current_playlist(): |
7b07ffc37ec0
Suspend background sound for bridge computer detail
Neil Muller <neil@dip.sun.ac.za>
parents:
188
diff
changeset
|
67 if albow.music.music_enabled and albow.music.current_playlist: |
7b07ffc37ec0
Suspend background sound for bridge computer detail
Neil Muller <neil@dip.sun.ac.za>
parents:
188
diff
changeset
|
68 return albow.music.current_playlist |
7b07ffc37ec0
Suspend background sound for bridge computer detail
Neil Muller <neil@dip.sun.ac.za>
parents:
188
diff
changeset
|
69 |
154 | 70 # Monkey patch |
71 albow.music.start_next_music = start_next_music |