Anonymous avatar Anonymous committed e0ded55

Added loopwave test.

Comments (0)

Files changed (4)

                 ctypes type if each element, e.g., c_ubyte, c_int, etc.
 
         '''
+        count = int(count)
         if not ptr:
             ptr = (ctype * count)()
         self.ptr = ptr
     arg_types=[POINTER(c_ubyte), POINTER(c_ubyte), c_uint, c_int],
     return_type=None)
 
-def SDL_MixAudio(dst, src, volume):
+def SDL_MixAudio(dst, src, length, volume):
     '''Mix two audio buffers.
 
     This takes two audio buffers of the playing audio format and mixes
     :Parameters:
      - `dst`: `SDL_array`
      - `src`: `SDL_array`
+     - `length`: int
      - `volume`: int
 
     '''
     dst = SDL.array.to_ctypes(dst, len(dst), c_ubyte)
     src = SDL.array.to_ctypes(src, len(src), c_ubyte)
-    if len(dst) != len(src):
-        raise TypeError, 'Length of buffers must be equal'
-    _SDL_MixAudio(dst, src, len(dst), volume)
+    if len(dst) < length:
+        raise TypeError, 'Destination buffer too small'
+    elif len(src) < length:
+        raise TypeError, 'Source buffer too small'
+    _SDL_MixAudio(dst, src, length, volume)
 
 SDL_LockAudio = SDL.dll.function('SDL_LockAudio',
     '''Guarantee the callback function is not running.
 SetCaption unicode
 byte swapping in array, esp for audio
 
-SDL_audio.h
-SDL_joystick.h
 SDL_syswm.h - platform dependent
-
-looppwave.c
+#!/usr/bin/env python
+
+'''
+'''
+
+__docformat__ = 'restructuredtext'
+__version__ = '$Id: $'
+
+import os
+import sys
+
+from SDL import *
+
+class Wave:
+    pass
+wave = Wave()
+
+def fillerup(unused, stream):
+    # It is not possible to do pointer arithmetic in Python (even with
+    # ctypes, so SDL_MixAudio cannot be used.  Instead, standard slice
+    # assignment is used for the same outcome.
+
+    waveleft = wave.soundlen - wave.soundpos
+    offset = 0
+    length = len(stream)
+    
+    while waveleft <= length:
+        stream[offset:waveleft + offset] = wave.sound[wave.soundpos:]
+        offset += waveleft
+        length -= waveleft
+        waveleft = wave.soundlen
+        wave.soundpos = 0
+    stream[offset:] = wave.sound[wave.soundpos:wave.soundpos + length]
+    wave.soundpos += length
+
+if __name__ == '__main__':
+    SDL_Init(SDL_INIT_AUDIO)
+
+    if len(sys.argv) > 1:
+        filename = sys.argv[1]
+    else:
+        filename = os.path.join(os.path.dirname(sys.argv[0]), 'sample.wav')
+    
+    wave.spec, wave.sound = SDL_LoadWAV(filename)
+    wave.soundlen = len(wave.sound)
+    wave.spec.callback = fillerup
+    wave.soundpos = 0
+
+    SDL_OpenAudio(wave.spec, None)
+    SDL_PauseAudio(False)
+
+    print 'Using audio driver: %s' % SDL_AudioDriverName()
+    while SDL_GetAudioStatus() == SDL_AUDIO_PLAYING:
+        try:
+            SDL_Delay(1000)
+        except KeyboardInterrupt:
+            break
+
+    SDL_CloseAudio()
+    SDL_FreeWAV(wave.sound)
+    SDL_Quit()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.