Mercurial > touhou
comparison pytouhou/ui/music.pyx @ 501:4778c482f24a
Make SDL(sound=False) work again, and disable sound if an exception occurs while setting it up.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 23 Oct 2013 18:24:07 +0200 |
parents | ca22df9e70bc |
children | 7c3c90468996 |
comparison
equal
deleted
inserted
replaced
500:777544d11578 | 501:4778c482f24a |
---|---|
13 ## | 13 ## |
14 | 14 |
15 | 15 |
16 from os.path import join | 16 from os.path import join |
17 from glob import glob | 17 from glob import glob |
18 from pytouhou.lib import sdl | |
18 from pytouhou.lib cimport sdl | 19 from pytouhou.lib cimport sdl |
19 from pytouhou.utils.helpers import get_logger | 20 from pytouhou.utils.helpers import get_logger |
20 | 21 |
21 logger = get_logger(__name__) | 22 logger = get_logger(__name__) |
22 | 23 |
53 logger.warn(u'No working music file for “%s”, disabling bgm.', globname) | 54 logger.warn(u'No working music file for “%s”, disabling bgm.', globname) |
54 | 55 |
55 def play(self, index): | 56 def play(self, index): |
56 cdef sdl.Music bgm | 57 cdef sdl.Music bgm |
57 bgm = self.bgms[index] | 58 bgm = self.bgms[index] |
58 if bgm: | 59 if bgm is not None: |
59 bgm.play(-1) | 60 bgm.play(-1) |
60 | 61 |
61 | 62 |
62 class SFXPlayer(object): | 63 class SFXPlayer(object): |
63 def __init__(self, loader, volume=.42): | 64 def __init__(self, loader, volume=.42): |
72 self.channels[name] = self.next_channel | 73 self.channels[name] = self.next_channel |
73 self.next_channel += 1 | 74 self.next_channel += 1 |
74 return self.channels[name] | 75 return self.channels[name] |
75 | 76 |
76 def get_sound(self, name): | 77 def get_sound(self, name): |
78 cdef sdl.Chunk chunk | |
77 if name not in self.sounds: | 79 if name not in self.sounds: |
78 wave_file = self.loader.get_file(name) | 80 wave_file = self.loader.get_file(name) |
79 chunk = sdl.load_chunk(wave_file) | 81 try: |
80 chunk.set_volume(self.volume) | 82 chunk = sdl.load_chunk(wave_file) |
83 except sdl.SDLError as error: | |
84 logger.warn(u'Sound “%s” not found: %s', name, error) | |
85 chunk = None | |
86 else: | |
87 chunk.set_volume(self.volume) | |
81 self.sounds[name] = chunk | 88 self.sounds[name] = chunk |
82 return self.sounds[name] | 89 return self.sounds[name] |
83 | 90 |
84 def play(self, name, volume=None): | 91 def play(self, name, volume=None): |
85 cdef sdl.Chunk sound | 92 cdef sdl.Chunk sound |
86 sound = self.get_sound(name) | 93 sound = self.get_sound(name) |
94 if sound is None: | |
95 return | |
87 channel = self.get_channel(name) | 96 channel = self.get_channel(name) |
88 if volume: | 97 if volume: |
89 sdl.mix_volume(channel, volume) | 98 sdl.mix_volume(channel, volume) |
90 sound.play(channel, 0) | 99 sound.play(channel, 0) |
91 | 100 |