Mercurial > boomslang
annotate gamelib/sound.py @ 546:ad4d6ffd25d7
update tags
author | convert-repo |
---|---|
date | Sat, 21 Jun 2014 17:38:39 +0000 |
parents | 6df527142e69 |
children |
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 | |
534 | 16 |
107 | 17 def get_sound(*names): |
18 if sound_cache is None: | |
19 return dummy_sound | |
20 path = _resource_path("sounds", names) | |
21 sound = sound_cache.get(path) | |
22 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
|
23 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
|
24 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
|
25 return dummy_sound |
107 | 26 try: |
27 from pygame.mixer import Sound | |
28 except ImportError, e: | |
29 no_sound(e) | |
30 return dummy_sound | |
31 try: | |
32 sound = Sound(path) | |
33 except pygame.error, e: | |
34 missing_sound(e, path) | |
35 return dummy_sound | |
36 sound_cache[path] = sound | |
37 return sound | |
38 | |
39 | |
40 def no_sound(e): | |
41 global sound_cache | |
42 print "get_sound: %s" % e | |
43 print "get_sound: Sound not available, continuing without it" | |
44 sound_cache = None | |
188
aebd4fd5b561
no_sound error handler should also disable music
Neil Muller <neil@dip.sun.ac.za>
parents:
154
diff
changeset
|
45 albow.music.music_enabled = False |
107 | 46 |
534 | 47 |
107 | 48 def disable_sound(): |
49 global sound_cache | |
50 sound_cache = None | |
154 | 51 albow.music.music_enabled = False |
107 | 52 |
534 | 53 |
107 | 54 def missing_sound(e, name): |
55 print "albow.resource.get_sound: %s: %s" % (name, e) | |
56 | |
154 | 57 |
58 def start_next_music(): | |
59 """Start playing the next item from the current playlist immediately.""" | |
60 if albow.music.music_enabled and albow.music.current_playlist: | |
61 next_music = albow.music.current_playlist.next() | |
62 if next_music: | |
63 #print "albow.music: loading", repr(next_music) | |
64 music.load(next_music) | |
65 music.play() | |
66 albow.music.next_change_delay = albow.music.change_delay | |
67 albow.music.current_music = next_music | |
68 | |
534 | 69 |
315
7b07ffc37ec0
Suspend background sound for bridge computer detail
Neil Muller <neil@dip.sun.ac.za>
parents:
188
diff
changeset
|
70 def get_current_playlist(): |
7b07ffc37ec0
Suspend background sound for bridge computer detail
Neil Muller <neil@dip.sun.ac.za>
parents:
188
diff
changeset
|
71 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
|
72 return albow.music.current_playlist |
7b07ffc37ec0
Suspend background sound for bridge computer detail
Neil Muller <neil@dip.sun.ac.za>
parents:
188
diff
changeset
|
73 |
154 | 74 # Monkey patch |
75 albow.music.start_next_music = start_next_music |