diff pytouhou/lib/sdl.pyx @ 421:b1248bab2d0f

Add back music and SFX playback using SDL_mixer instead of pyglet, and add FLAC and Vorbis support.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 16 Jul 2013 21:07:15 +0200
parents 3a7b36324611
children 52829ebe2561
line wrap: on
line diff
--- a/pytouhou/lib/sdl.pyx
+++ b/pytouhou/lib/sdl.pyx
@@ -37,6 +37,8 @@ SCANCODE_ESCAPE = SDL_SCANCODE_ESCAPE
 KEYDOWN = SDL_KEYDOWN
 QUIT = SDL_QUIT
 
+DEFAULT_FORMAT = MIX_DEFAULT_FORMAT
+
 
 class SDLError(Exception):
     pass
@@ -97,6 +99,36 @@ cdef class Surface:
             image[3+4*i] = alpha[3*i]
 
 
+cdef class Music:
+    cdef Mix_Music *music
+
+    def __dealloc__(self):
+        if self.music != NULL:
+            Mix_FreeMusic(self.music)
+
+    def play(self, int loops):
+        Mix_PlayMusic(self.music, loops)
+
+    def set_loop_points(self, double start, double end):
+        #Mix_SetLoopPoints(self.music, start, end)
+        pass
+
+
+cdef class Chunk:
+    cdef Mix_Chunk *chunk
+
+    def __dealloc__(self):
+        if self.chunk != NULL:
+            Mix_FreeChunk(self.chunk)
+
+    property volume:
+        def __set__(self, float volume):
+            Mix_VolumeChunk(self.chunk, int(volume * 128))
+
+    def play(self, int channel, int loops):
+        Mix_PlayChannel(channel, self.chunk, loops)
+
+
 def init(Uint32 flags):
     if SDL_Init(flags) < 0:
         raise SDLError(SDL_GetError())
@@ -107,6 +139,11 @@ def img_init(Uint32 flags):
         raise SDLError(SDL_GetError())
 
 
+def mix_init(int flags):
+    if Mix_Init(flags) != flags:
+        raise SDLError(SDL_GetError())
+
+
 def quit():
     SDL_Quit()
 
@@ -115,6 +152,10 @@ def img_quit():
     IMG_Quit()
 
 
+def mix_quit():
+    Mix_Quit()
+
+
 def gl_set_attribute(SDL_GLattr attr, int value):
     if SDL_GL_SetAttribute(attr, value) < 0:
         raise SDLError(SDL_GetError())
@@ -158,6 +199,47 @@ def create_rgb_surface(int width, int he
     return surface
 
 
+def mix_open_audio(int frequency, Uint16 format_, int channels, int chunksize):
+    if Mix_OpenAudio(frequency, format_, channels, chunksize) < 0:
+        raise SDLError(SDL_GetError())
+
+
+def mix_close_audio():
+    Mix_CloseAudio()
+
+
+def mix_allocate_channels(int numchans):
+    if Mix_AllocateChannels(numchans) != numchans:
+        raise SDLError(SDL_GetError())
+
+
+def mix_volume(int channel, float volume):
+    return Mix_Volume(channel, int(volume * 128))
+
+
+def mix_volume_music(float volume):
+    return Mix_VolumeMusic(int(volume * 128))
+
+
+def load_music(const char *filename):
+    music = Music()
+    music.music = Mix_LoadMUS(filename)
+    if music.music == NULL:
+        raise SDLError(SDL_GetError())
+    return music
+
+
+def load_chunk(file_):
+    cdef SDL_RWops *rwops
+    chunk = Chunk()
+    data = file_.read()
+    rwops = SDL_RWFromConstMem(<char*>data, len(data))
+    chunk.chunk = Mix_LoadWAV_RW(rwops, 1)
+    if chunk.chunk == NULL:
+        raise SDLError(SDL_GetError())
+    return chunk
+
+
 def get_ticks():
     return SDL_GetTicks()