diff 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
line wrap: on
line diff
--- a/pytouhou/ui/music.py
+++ b/pytouhou/ui/music.py
@@ -13,7 +13,9 @@
 ##
 
 
-from pyglet.media import AudioData, AudioFormat
+from os.path import join
+
+from pyglet.media import AudioData, AudioFormat, Player
 from pyglet.media.riff import WaveSource
 
 
@@ -77,3 +79,32 @@ class ZwavSource(InfiniteWaveSource):
         self._file.seek(self._offset)
         self._start = format.intro + format.start
         self._end = format.intro + format.duration
+
+
+class MusicPlayer(object):
+    def __init__(self, resource_loader, bgms):
+        self.bgms = []
+        for bgm in bgms:
+            if not bgm:
+                self.bgms.append(None)
+                continue
+            posname = bgm[1].replace('bgm/', '').replace('.mid', '.pos')
+            track = resource_loader.get_track(posname)
+            wavname = join(resource_loader.game_dir, bgm[1].replace('.mid', '.wav'))
+            try:
+                source = InfiniteWaveSource(wavname, track.start, track.end)
+            except IOError:
+                source = None
+            self.bgms.append(source)
+
+        self.player = Player()
+
+
+    def play(self, index):
+        bgm = self.bgms[index]
+        if self.player.playing:
+            self.player.next()
+        if bgm:
+            self.player.queue(bgm)
+        self.player.play()
+