comparison pytouhou/ui/music.pyx @ 455:6864a38b2413

Make pytouhou.lib.sdl cimportable, and convert pytouhou.ui.window.* to extension types.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 02 Sep 2013 22:16:38 +0200
parents pytouhou/ui/music.py@b1248bab2d0f
children ca22df9e70bc
comparison
equal deleted inserted replaced
454:a502887557ac 455:6864a38b2413
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15
16 from os.path import join
17 from glob import glob
18 from pytouhou.lib cimport sdl
19 from pytouhou.utils.helpers import get_logger
20
21 logger = get_logger(__name__)
22
23
24 class MusicPlayer(object):
25 def __init__(self, resource_loader, bgms):
26 self.bgms = []
27 for bgm in bgms:
28 if not bgm:
29 self.bgms.append(None)
30 continue
31 posname = bgm[1].replace('bgm/', '').replace('.mid', '.pos')
32 try:
33 track = resource_loader.get_track(posname)
34 except KeyError:
35 self.bgms.append(None)
36 logger.warn(u'Music description “%s” not found.', posname)
37 continue
38 globname = join(resource_loader.game_dir, bgm[1]).replace('.mid', '.*')
39 filenames = glob(globname)
40 for filename in reversed(filenames):
41 try:
42 source = sdl.load_music(filename)
43 except sdl.SDLError as error:
44 logger.debug(u'Music file “%s” unreadable: %s', filename, error)
45 continue
46 else:
47 source.set_loop_points(track.start / 44100., track.end / 44100.) #TODO: retrieve the sample rate from the actual track.
48 self.bgms.append(source)
49 logger.debug(u'Music file “%s” opened.', filename)
50 break
51 else:
52 self.bgms.append(None)
53 logger.warn(u'No working music file for “%s”, disabling bgm.', globname)
54
55 def play(self, index):
56 cdef sdl.Music bgm
57 bgm = self.bgms[index]
58 if bgm:
59 bgm.play(-1)
60
61
62 class SFXPlayer(object):
63 def __init__(self, loader, volume=.42):
64 self.loader = loader
65 self.channels = {}
66 self.sounds = {}
67 self.volume = volume
68 self.next_channel = 0
69
70 def get_channel(self, name):
71 if name not in self.channels:
72 self.channels[name] = self.next_channel
73 self.next_channel += 1
74 return self.channels[name]
75
76 def get_sound(self, name):
77 if name not in self.sounds:
78 wave_file = self.loader.get_file(name)
79 chunk = sdl.load_chunk(wave_file)
80 chunk.set_volume(self.volume)
81 self.sounds[name] = chunk
82 return self.sounds[name]
83
84 def play(self, name, volume=None):
85 cdef sdl.Chunk sound
86 sound = self.get_sound(name)
87 channel = self.get_channel(name)
88 if volume:
89 sdl.mix_volume(channel, volume)
90 sound.play(channel, 0)
91
92
93 class NullPlayer(object):
94 def __init__(self, loader=None, bgms=None):
95 pass
96
97 def play(self, name, volume=None):
98 pass