comparison pytouhou/ui/music.py @ 341:61caded6b4f5

Clean music playback API a little.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 04 Jul 2012 23:05:21 +0200
parents cddfd3cb4797
children 83c9354ff3ef
comparison
equal deleted inserted replaced
340:39bc59953dfa 341:61caded6b4f5
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 15
16 from pyglet.media import AudioData, AudioFormat 16 from os.path import join
17
18 from pyglet.media import AudioData, AudioFormat, Player
17 from pyglet.media.riff import WaveSource 19 from pyglet.media.riff import WaveSource
18 20
19 21
20 class InfiniteWaveSource(WaveSource): 22 class InfiniteWaveSource(WaveSource):
21 def __init__(self, filename, start, end, file=None): 23 def __init__(self, filename, start, end, file=None):
75 self._offset = format.intro 77 self._offset = format.intro
76 78
77 self._file.seek(self._offset) 79 self._file.seek(self._offset)
78 self._start = format.intro + format.start 80 self._start = format.intro + format.start
79 self._end = format.intro + format.duration 81 self._end = format.intro + format.duration
82
83
84 class MusicPlayer(object):
85 def __init__(self, resource_loader, bgms):
86 self.bgms = []
87 for bgm in bgms:
88 if not bgm:
89 self.bgms.append(None)
90 continue
91 posname = bgm[1].replace('bgm/', '').replace('.mid', '.pos')
92 track = resource_loader.get_track(posname)
93 wavname = join(resource_loader.game_dir, bgm[1].replace('.mid', '.wav'))
94 try:
95 source = InfiniteWaveSource(wavname, track.start, track.end)
96 except IOError:
97 source = None
98 self.bgms.append(source)
99
100 self.player = Player()
101
102
103 def play(self, index):
104 bgm = self.bgms[index]
105 if self.player.playing:
106 self.player.next()
107 if bgm:
108 self.player.queue(bgm)
109 self.player.play()
110