Mercurial > touhou
comparison pytouhou/ui/music.pyx @ 528:7c3c90468996
Inherit music players from a base class.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 18 Dec 2013 22:36:23 +0100 |
parents | 4778c482f24a |
children | bd0c15d28dd6 |
comparison
equal
deleted
inserted
replaced
527:db28538cd399 | 528:7c3c90468996 |
---|---|
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 import sdl |
19 from pytouhou.lib cimport sdl | 19 from pytouhou.lib cimport sdl |
20 from pytouhou.utils.helpers import get_logger | 20 from pytouhou.utils.helpers import get_logger |
21 from pytouhou.game.music cimport MusicPlayer | |
21 | 22 |
22 logger = get_logger(__name__) | 23 logger = get_logger(__name__) |
23 | 24 |
24 | 25 |
25 class MusicPlayer(object): | 26 cdef class BGMPlayer(MusicPlayer): |
27 cdef list bgms | |
28 | |
26 def __init__(self, resource_loader, bgms): | 29 def __init__(self, resource_loader, bgms): |
27 self.bgms = [] | 30 self.bgms = [] |
28 for bgm in bgms: | 31 for bgm in bgms: |
29 if not bgm: | 32 if not bgm: |
30 self.bgms.append(None) | 33 self.bgms.append(None) |
51 break | 54 break |
52 else: | 55 else: |
53 self.bgms.append(None) | 56 self.bgms.append(None) |
54 logger.warn(u'No working music file for ā%sā, disabling bgm.', globname) | 57 logger.warn(u'No working music file for ā%sā, disabling bgm.', globname) |
55 | 58 |
56 def play(self, index): | 59 cpdef play(self, index): |
57 cdef sdl.Music bgm | 60 cdef sdl.Music bgm |
58 bgm = self.bgms[index] | 61 bgm = self.bgms[index] |
59 if bgm is not None: | 62 if bgm is not None: |
60 bgm.play(-1) | 63 bgm.play(-1) |
61 | 64 |
62 | 65 |
63 class SFXPlayer(object): | 66 cdef class SFXPlayer(MusicPlayer): |
67 cdef object loader | |
68 cdef dict channels, sounds | |
69 cdef float volume | |
70 cdef int next_channel | |
71 | |
64 def __init__(self, loader, volume=.42): | 72 def __init__(self, loader, volume=.42): |
65 self.loader = loader | 73 self.loader = loader |
66 self.channels = {} | 74 self.channels = {} |
67 self.sounds = {} | 75 self.sounds = {} |
68 self.volume = volume | 76 self.volume = volume |
69 self.next_channel = 0 | 77 self.next_channel = 0 |
70 | 78 |
71 def get_channel(self, name): | 79 cdef int get_channel(self, name): |
72 if name not in self.channels: | 80 if name not in self.channels: |
73 self.channels[name] = self.next_channel | 81 self.channels[name] = self.next_channel |
74 self.next_channel += 1 | 82 self.next_channel += 1 |
75 return self.channels[name] | 83 return self.channels[name] |
76 | 84 |
77 def get_sound(self, name): | 85 cdef sdl.Chunk get_sound(self, name): |
78 cdef sdl.Chunk chunk | 86 cdef sdl.Chunk chunk |
79 if name not in self.sounds: | 87 if name not in self.sounds: |
80 wave_file = self.loader.get_file(name) | 88 wave_file = self.loader.get_file(name) |
81 try: | 89 try: |
82 chunk = sdl.load_chunk(wave_file) | 90 chunk = sdl.load_chunk(wave_file) |
86 else: | 94 else: |
87 chunk.set_volume(self.volume) | 95 chunk.set_volume(self.volume) |
88 self.sounds[name] = chunk | 96 self.sounds[name] = chunk |
89 return self.sounds[name] | 97 return self.sounds[name] |
90 | 98 |
91 def play(self, name, volume=None): | 99 cpdef play(self, name): |
92 cdef sdl.Chunk sound | |
93 sound = self.get_sound(name) | 100 sound = self.get_sound(name) |
94 if sound is None: | 101 if sound is None: |
95 return | 102 return |
96 channel = self.get_channel(name) | 103 channel = self.get_channel(name) |
97 if volume: | |
98 sdl.mix_volume(channel, volume) | |
99 sound.play(channel, 0) | 104 sound.play(channel, 0) |
100 | 105 |
101 | 106 cpdef set_volume(self, name, float volume): |
102 class NullPlayer(object): | 107 chunk = self.get_sound(name) |
103 def __init__(self, loader=None, bgms=None): | 108 chunk.set_volume(volume) |
104 pass | |
105 | |
106 def play(self, name, volume=None): | |
107 pass |