Mercurial > pyntnclick
annotate gamelib/sound.py @ 158:7a21a594ad57
r150 Screenshot
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Wed, 25 Aug 2010 00:14:55 +0200 |
parents | d2f94f42edf3 |
children | aebd4fd5b561 |
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 | |
44 | |
45 def disable_sound(): | |
46 global sound_cache | |
47 sound_cache = None | |
154 | 48 albow.music.music_enabled = False |
107 | 49 |
50 def missing_sound(e, name): | |
51 print "albow.resource.get_sound: %s: %s" % (name, e) | |
52 | |
154 | 53 |
54 def start_next_music(): | |
55 """Start playing the next item from the current playlist immediately.""" | |
56 if albow.music.music_enabled and albow.music.current_playlist: | |
57 next_music = albow.music.current_playlist.next() | |
58 if next_music: | |
59 #print "albow.music: loading", repr(next_music) | |
60 music.load(next_music) | |
61 music.play() | |
62 albow.music.next_change_delay = albow.music.change_delay | |
63 albow.music.current_music = next_music | |
64 | |
65 # Monkey patch | |
66 albow.music.start_next_music = start_next_music |