changeset 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 db28538cd399
children bd0c15d28dd6
files pytouhou/game/game.pxd pytouhou/game/music.pxd pytouhou/game/music.py pytouhou/games/eosd.py pytouhou/lib/sdl.pyx pytouhou/ui/gamerunner.pyx pytouhou/ui/music.pyx setup.py
diffstat 8 files changed, 39 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/game/game.pxd	Wed Dec 18 18:19:08 2013 +0100
+++ b/pytouhou/game/game.pxd	Wed Dec 18 22:36:23 2013 +0100
@@ -1,13 +1,15 @@
 from pytouhou.game.effect cimport Effect
 from pytouhou.game.player cimport Player
 from pytouhou.game.text cimport Text, NativeText
+from pytouhou.game.music cimport MusicPlayer
 from pytouhou.utils.random cimport Random
 
 cdef class Game:
     cdef public long width, height, nb_bullets_max, stage, rank, difficulty, difficulty_min, difficulty_max, frame
     cdef public list bullet_types, laser_types, item_types, players, enemies, effects, bullets, lasers, cancelled_bullets, players_bullets, players_lasers, items, labels, faces, hints, bonus_list
-    cdef public object interface, boss, msg_runner, sfx_player
+    cdef public object interface, boss, msg_runner
     cdef public dict texts
+    cdef public MusicPlayer sfx_player
     cdef public Random prng
     cdef public double continues
     cdef public Effect spellcard_effect
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytouhou/game/music.pxd	Wed Dec 18 22:36:23 2013 +0100
@@ -0,0 +1,3 @@
+cdef class MusicPlayer:
+    cpdef play(self, name)
+    cpdef set_volume(self, name, float volume)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pytouhou/game/music.py	Wed Dec 18 22:36:23 2013 +0100
@@ -0,0 +1,6 @@
+class MusicPlayer(object):
+    def play(self, name):
+        pass
+
+    def set_volume(self, name, volume):
+        pass
--- a/pytouhou/games/eosd.py	Wed Dec 18 18:19:08 2013 +0100
+++ b/pytouhou/games/eosd.py	Wed Dec 18 22:36:23 2013 +0100
@@ -282,7 +282,8 @@
                 else:
                     timeout_label.set_color('red')
                 if (boss.timeout - boss.frame) % 60 == 0 and boss.timeout != 0:
-                    self.game.sfx_player.play('timeout.wav', volume=1.)
+                    self.game.sfx_player.set_volume('timeout.wav', 1.)
+                    self.game.sfx_player.play('timeout.wav')
             timeout_label.set_text('%02d' % (timeout if timeout >= 0 else 0))
             timeout_label.changed = True
 
--- a/pytouhou/lib/sdl.pyx	Wed Dec 18 18:19:08 2013 +0100
+++ b/pytouhou/lib/sdl.pyx	Wed Dec 18 22:36:23 2013 +0100
@@ -66,7 +66,9 @@
                 logger.error(u'Impossible to set up audio subsystem: %s', error)
                 self.sound = False
             else:
-                mix_allocate_channels(MAX_CHANNELS) #TODO: make it dependent on the SFX number.
+                # TODO: make it dependent on the number of sound files in the
+                # archives.
+                mix_allocate_channels(MAX_SOUNDS)
 
     def __exit__(self, *args):
         if self.sound:
--- a/pytouhou/ui/gamerunner.pyx	Wed Dec 18 18:19:08 2013 +0100
+++ b/pytouhou/ui/gamerunner.pyx	Wed Dec 18 22:36:23 2013 +0100
@@ -17,8 +17,9 @@
 from pytouhou.lib cimport sdl
 
 from .window cimport Window, Runner
-from .music import MusicPlayer, SFXPlayer, NullPlayer
+from .music import BGMPlayer, SFXPlayer
 from pytouhou.game.game cimport Game
+from pytouhou.game.music cimport MusicPlayer
 
 
 cdef class GameRunner(Runner):
@@ -65,9 +66,9 @@
 
         self.save_keystates = save_keystates
 
-        null_player = NullPlayer()
+        null_player = MusicPlayer()
         if bgms is not None:
-            game.music = MusicPlayer(self.resource_loader, bgms)
+            game.music = BGMPlayer(self.resource_loader, bgms)
             game.music.play(0)
         else:
             game.music = null_player
--- a/pytouhou/ui/music.pyx	Wed Dec 18 18:19:08 2013 +0100
+++ b/pytouhou/ui/music.pyx	Wed Dec 18 22:36:23 2013 +0100
@@ -18,11 +18,14 @@
 from pytouhou.lib import sdl
 from pytouhou.lib cimport sdl
 from pytouhou.utils.helpers import get_logger
+from pytouhou.game.music cimport MusicPlayer
 
 logger = get_logger(__name__)
 
 
-class MusicPlayer(object):
+cdef class BGMPlayer(MusicPlayer):
+    cdef list bgms
+
     def __init__(self, resource_loader, bgms):
         self.bgms = []
         for bgm in bgms:
@@ -53,14 +56,19 @@
                 self.bgms.append(None)
                 logger.warn(u'No working music file for ā€œ%sā€, disabling bgm.', globname)
 
-    def play(self, index):
+    cpdef play(self, index):
         cdef sdl.Music bgm
         bgm = self.bgms[index]
         if bgm is not None:
             bgm.play(-1)
 
 
-class SFXPlayer(object):
+cdef class SFXPlayer(MusicPlayer):
+    cdef object loader
+    cdef dict channels, sounds
+    cdef float volume
+    cdef int next_channel
+
     def __init__(self, loader, volume=.42):
         self.loader = loader
         self.channels = {}
@@ -68,13 +76,13 @@
         self.volume = volume
         self.next_channel = 0
 
-    def get_channel(self, name):
+    cdef int get_channel(self, name):
         if name not in self.channels:
             self.channels[name] = self.next_channel
             self.next_channel += 1
         return self.channels[name]
 
-    def get_sound(self, name):
+    cdef sdl.Chunk get_sound(self, name):
         cdef sdl.Chunk chunk
         if name not in self.sounds:
             wave_file = self.loader.get_file(name)
@@ -88,20 +96,13 @@
             self.sounds[name] = chunk
         return self.sounds[name]
 
-    def play(self, name, volume=None):
-        cdef sdl.Chunk sound
+    cpdef play(self, name):
         sound = self.get_sound(name)
         if sound is None:
             return
         channel = self.get_channel(name)
-        if volume:
-            sdl.mix_volume(channel, volume)
         sound.play(channel, 0)
 
-
-class NullPlayer(object):
-    def __init__(self, loader=None, bgms=None):
-        pass
-
-    def play(self, name, volume=None):
-        pass
+    cpdef set_volume(self, name, float volume):
+        chunk = self.get_sound(name)
+        chunk.set_volume(volume)
--- a/setup.py	Wed Dec 18 18:19:08 2013 +0100
+++ b/setup.py	Wed Dec 18 22:36:23 2013 +0100
@@ -125,7 +125,7 @@
                                                  'profile': debug},
                             compile_time_env={'MAX_TEXTURES': 128,
                                               'MAX_ELEMENTS': 640 * 4 * 3,
-                                              'MAX_CHANNELS': 26,
+                                              'MAX_SOUNDS': 26,
                                               'USE_OPENGL': use_opengl,
                                               'USE_GLEW': is_windows}),
       scripts=['eosd'] + (['anmviewer'] if anmviewer else []),