Anonymous avatar Anonymous committed 557b4f5

playwave and playmus tests added.

Comments (0)

Files changed (5)

         '''
         return SDL_array(self.ptr, (self.count * sizeof(self.ctype)), c_ubyte)
 
+    def as_int16(self):
+        '''Access the array as 16-bit integers, regardless of the underlying
+        data type.
+
+        :rtype: SDL_array
+        '''
+        return SDL_array(self.ptr, 
+                         self.count * sizeof(self.ctype) / 2, 
+                         c_ushort)
+
+    def as_int32(self):
+        '''Access the array as 32-bit integers, regardless of the underlying
+        data type.
+
+        :rtype: SDL_array
+        '''
+        return SDL_array(self.ptr, 
+                         self.count * sizeof(self.ctype) / 4, 
+                         c_uint)
+
     def as_ctypes(self):
         '''Access the array as a ctypes array.
 
 __version__ = '$Id: $'
 
 # TODO enums
-
-# From SDL_ttf.h (inserted manually)
-TTF_STYLE_NORMAL    = 0x00
-TTF_STYLE_BOLD      = 0x01
-TTF_STYLE_ITALIC    = 0x02
-TTF_STYLE_UNDERLINE = 0x04
-
 # enum SDLKey {
 
 #  The keyboard syms have been cleverly chosen to map to ASCII 
 SDL_TIMESLICE = 0x0000000a
 TIMER_RESOLUTION = 0x0000000a
 #END GENERATED CONSTANTS
+
+
+# From SDL_ttf.h (inserted manually)
+TTF_STYLE_NORMAL    = 0x00
+TTF_STYLE_BOLD      = 0x01
+TTF_STYLE_ITALIC    = 0x02
+TTF_STYLE_UNDERLINE = 0x04
+
+# From SDL_mixer.h (inserted manually)
+MIX_CHANNELS            = 8
+MIX_DEFAULT_FREQUENCY   = 22050
+MIX_DEFAULT_FORMAT      = AUDIO_S16LSB  # TODO endianness
+MID_DEFAULT_CHANNELS    = 2
+MIX_MAX_VOLUME          = 128
+MIX_CHANNEL_POST        = -2
+MIX_EFFECTSMAXSPEED     = 'MIX_EFFECTSMAXSPEED'
+
+
 The process of mixing MIDI files to wave output is very CPU intensive, so
 if playing regular WAVE files sounds great, but playing MIDI files sounds
 choppy, try using 8-bit audio, mono audio, or lower frequencies.
+
+:note: The music stream does not resample to the required audio rate.  You
+    must call `Mix_OpenAudio` with the sampling rate of your music track.
 '''
 
 __docformat__ = 'restructuredtext'
 
 class Mix_Chunk(Structure):
     _fields_ = [('allocated', c_int),
-                ('abuf', POINTER(c_ubyte)),
+                ('_abuf', POINTER(c_ubyte)),
                 ('alen', c_uint),
                 ('volume', c_ubyte)]
 
+    def __getattr__(self, attr):
+        if attr == 'abuf':
+            return SDL.array.SDL_array(self._abuf, self.alen, c_ubyte)
+        raise AttributeException, attr
+
 # begin enum Mix_Fading
 (MIX_NO_FADING,
     MIX_FADING_OUT,
     ''',
     args=['file'],
     arg_types=[c_char_p],
-    return_type=_Mix_Music)
+    return_type=_Mix_Music,
+    require_return=True)
 
 Mix_LoadMUS_RW = _dll.function('Mix_LoadMUS_RW',
     '''Load a MID, OGG, MP3 or MOD file from a RWops source.
     ''',
     args=['file'],
     arg_types=[c_char_p],
-    return_type=_Mix_Music)
+    return_type=_Mix_Music,
+    require_return=True)
 
 _Mix_QuickLoad_WAV = _dll.private_function('Mix_QuickLoad_WAV',
     arg_types=[POINTER(c_ubyte)],
         return None
 
 _Mix_SetPostMix = _dll.private_function('Mix_SetPostMix',
-    arg_types=[POINTER(_Mix_FilterFunc), c_void_p],
+    arg_types=[_Mix_FilterFunc, c_void_p],
     return_type=None)
 
 def Mix_SetPostMix(mix_func, udata):
     _Mix_SetPostMix(_make_filter(mix_func, udata), None)
 
 _Mix_HookMusic = _dll.private_function('Mix_HookMusic',
-    arg_types=[POINTER(_Mix_FilterFunc), c_void_p],
+    arg_types=[_Mix_FilterFunc, c_void_p],
     return_type=None)
 
 def Mix_HookMusic(mix_func, udata):
 _Mix_HookMusicFinishedFunc = CFUNCTYPE(None)
 
 _Mix_HookMusicFinished = _dll.private_function('Mix_HookMusicFinished',
-    arg_types=[POINTER(_Mix_HookMusicFinishedFunc)],
+    arg_types=[_Mix_HookMusicFinishedFunc],
     return_type=None)
 
 def Mix_HookMusicFinished(music_finished):
 _Mix_ChannelFinishedFunc = CFUNCTYPE(None, c_int)
 
 _Mix_ChannelFinished = _dll.private_function('Mix_ChannelFinished',
-    arg_types=[POINTER(_Mix_ChannelFinishedFunc)],
+    arg_types=[_Mix_ChannelFinishedFunc],
     return_type=None)
 
 def Mix_ChannelFinished(channel_finished):
     else:
         _Mix_ChannelFinished(None)
 
-MIX_CHANNEL_POST = -2
-
 _Mix_EffectFunc = CFUNCTYPE(None, c_int, POINTER(c_ubyte), c_int, c_void_p)
 def _make_Mix_EffectFunc(func, udata):
     if func:
 
 _Mix_RegisterEffect = _dll.private_function('Mix_RegisterEffect',
     arg_types=\
-     [c_int, POINTER(_Mix_EffectFunc), POINTER(_Mix_EffectDoneFunc), c_void_p],
+     [c_int, _Mix_EffectFunc, _Mix_EffectDoneFunc, c_void_p],
     return_type=c_int,
     error_return=0)
 
     return_type=c_int,
     error_return=0)
 
-MIX_EFFECTSMAXSPEED = 'MIX_EFFECTSMAXSPEED'
-
 Mix_SetPanning = _dll.function('Mix_SetPanning',
     '''Set the panning of a channel.
 
+#!/usr/bin/env python
+
+'''
+'''
+
+__docformat__ = 'restructuredtext'
+__version__ = '$Id: $'
+
+import os
+import sys
+
+from SDL import *
+from SDL.mixer import *
+
+def usage():
+    print >> sys.stderr, 'Usage: %s [-i] [-l] [-8] [-r rate] [-c channels] \
+[-b buffers] [-v N] [-rwops] <musicfile>' % sys.argv[0]
+
+def Menu():
+    print 'Available commands: (p)ause (r)esume (h)alt > ',
+    buf = raw_input()[0].lower()
+    if buf == 'p':
+        Mix_PauseMusic()
+    elif buf == 'r':
+        Mix_ResumeMusic()
+    elif buf == 'h':
+        Mix_HaltMusic()
+    print 'Music playing: %s Paused: %s' % \
+        (Mix_PlayingMusic(), Mix_PausedMusic())
+
+if __name__ == '__main__':
+    audio_rate = MIX_DEFAULT_FREQUENCY
+    audio_format = MIX_DEFAULT_FORMAT
+    audio_volume = MIX_MAX_VOLUME
+    audio_channels = 2
+    looping = 0
+    reverse_stereo = 0
+    reverse_sample = 0
+    interactive = 0
+    rwops = 0
+    audio_buffers = 4096
+
+    i = 1
+    while i < len(sys.argv):
+        arg = sys.argv[i]
+        if arg[0] != '-':
+            break
+        elif arg == '-r':
+            i += 1
+            audio_rate = int(sys.argv[i])
+        elif arg == '-m':
+            audio_channels = 1
+        elif arg == '-b':
+            i += 1
+            audio_buffers = int(sys.argv[i])
+        elif arg == '-v':
+            i += 1
+            audio_volume = int(sys.argv[i])
+        elif arg == '-c':
+            i += 1
+            audio_channels = int(sys.argv[i])
+        elif arg == '-l':
+            looping = -1
+        elif arg == '-i':
+            interactive = 1
+        elif arg == '-8':
+            audio_format = AUDIO_U8
+        elif arg == '-rwops':
+            rwops = 1
+        else:
+            usage()
+            sys.exit(1)
+        i += 1
+
+    if i >= len(sys.argv):
+        usage()
+        sys.exit(1)
+
+    SDL_Init(SDL_INIT_AUDIO)
+    Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096)
+    opened, audio_rate, audio_format, audio_channels = Mix_QuerySpec()
+    channels_s = 'mono'
+    if audio_channels == 2:
+        channels_s = 'stereo'
+    elif audio_channels > 2:
+        channels_s = 'surround'
+    endian = 'LE'
+    if audio_format & 0x1000:
+        endian = 'BE'
+    print 'Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer' % \
+        (audio_rate, audio_format & 0xff, channels_s, endian, audio_buffers)
+
+    Mix_VolumeMusic(audio_volume)
+    Mix_SetMusicCMD(os.getenv('MUSIC_CMD'))
+
+    while i < len(sys.argv):
+        if rwops:
+            rwfp = SDL_RWFromFile(sys.argv[i], 'rb')
+            music = Mix_LoadMUS_RW(rwfp)
+        else:
+            music = Mix_LoadMUS(sys.argv[i])
+            
+        try:
+            print 'Playing %s' % sys.argv[i]
+            Mix_FadeInMusic(music, looping, 2000)
+            while Mix_PlayingMusic() or Mix_PausedMusic():
+                if interactive:
+                    Menu()
+                else:
+                    SDL_Delay(100)
+        except KeyboardInterrupt:
+            if Mix_PlayingMusic():
+                Mix_FadeOutMusic(1500)
+                SDL_Delay(1500)
+
+        Mix_FreeMusic(music)
+        music = None
+        if rwops:
+            SDL_FreeRW(rwfp)
+
+        # If the user presses Ctrl-C more than once, exit.
+        SDL_Delay(500)
+        i += 1
+    
+    if music:
+        Mix_FreeMusic(music)
+    Mix_CloseAudio()
+    SDL_Quit()
+#!/usr/bin/env python
+
+'''Test application for SDL.mixer
+'''
+
+__docformat__ = 'restructuredtext'
+__version__ = '$Id: $'
+
+import sys
+
+from SDL import *
+from SDL.mixer import *
+
+# Various mixer tests; enable the ones you want.
+TEST_MIX_VERSIONS = True
+TEST_MIX_CHANNELFINISHED = True
+TEST_MIX_PANNING = False
+TEST_MIX_DISTANCE = False
+TEST_MIX_POSITION = True
+
+if TEST_MIX_POSITION and (TEST_MIX_DISTANCE or TEST_MIX_PANNING):
+    raise 'TEST_MIX_POSITION cannot be used with TEST_MIX_DISTANCE or PANNING'
+
+channel_is_done = 0
+
+def test_versions():
+    print >> sys.stderr, 'Dyanamically linked against SDL %r and SDL_mixer %r' \
+        % (SDL_Linked_Version(), Mix_Linked_Version())
+
+def channel_complete_callback(chan):
+    global channel_is_done
+    done_chunk = Mix_GetChunk(chan)
+    print >> sys.stderr, 'We were just alerted that Mixer channel %d is done' \
+        % chan
+    channel_is_done = 1
+
+def still_playing():
+    if TEST_MIX_CHANNELFINISHED:
+        return not channel_is_done
+    else:
+        return Mix_Playing(0)
+
+leftvol = 128
+rightvol = 128
+leftincr = -1
+rightincr = 1
+next_panning_update = 0
+def do_panning_update():
+    global leftvol, rightvol, leftincr, rightincr, next_panning_update
+    if SDL_GetTicks() >= next_panning_update:
+        Mix_SetPanning(0, leftvol, rightvol)
+        if leftvol == 255 or leftvol == 0:
+            if leftvol == 255:
+                print 'All the way in the left speaker.'
+            leftincr *= -1
+
+        if rightvol == 255 or rightvol == 0:
+            if rightvol == 255:
+                print 'All the way in the right speaker.'
+            rightincr *= -1
+
+        leftvol += leftincr
+        rightvol += rightincr
+        next_panning_update = SDL_GetTicks() + 10
+
+distance = 1
+distincr = 1
+next_distance_update = 0
+def do_distance_update():
+    global distance, distincr, next_distance_update
+    if SDL_GetTicks() >= next_distance_update:
+        Mix_SetDistance(0, distance)
+        if distance == 0:
+            print 'Distance at nearest point'
+            distincr *= -1
+        elif distance == 255:
+            print 'Distance at furthest point'
+            distincr *= -1
+
+        distance += distincr
+        next_distance_update = SDL_GetTicks() + 15
+
+angle = 0
+angleincr = 1
+next_position_update = 0
+def do_position_update():
+    global distance, distincr, angle, angleincr, next_position_update
+
+    if SDL_GetTicks() >= next_position_update:
+        Mix_SetPosition(0, angle, distance)
+        if angle == 0:
+            print 'Due north; now rotating clockwise...'
+            angleincr = 1
+        elif angle == 360:
+            print 'Due north; now rotating counter-clockwise...'
+            angleincr = -1
+
+        distance += distincr
+
+        if distance < 0:
+            distance = 0
+            distincr = 3
+            print 'Distance is very, very near.  Stepping away by threes...'
+        elif distance > 255:
+            distance = 255
+            distincr = -3
+            print 'Distance is very, very far.  Stepping towards by threes...'
+
+        angle += angleincr
+
+        next_position_update = SDL_GetTicks() + 30
+
+def flip_sample(wave):
+    opened, rate, format, channels = Mix_QuerySpec()
+    incr = (format & 0xff) * channels
+    if incr == 8:
+        buf = wave.abuf.as_bytes()
+    elif incr == 16:
+        buf = wave.abuf.as_int16()
+    elif incr == 32:
+        buf = wave.abuf.as_int32()
+    else:
+        raise 'Unhandled format in sample flipping'
+
+    # SDL_array doesn't have a reverse method, but list does.  Create a
+    # list of the array by slicing.  Reverse the list.  Assign it to the
+    # buffer with another slice.
+    reversed = buf[:]
+    reversed.reverse()
+    buf[:] = reversed
+
+def usage():
+    print >> sys.stderr, 'Usage: %s [-8] [-r rate] [-c channels] [-f] [-F]\
+ [-l] [-m] <wavefile>' % sys.argv[0]
+
+if __name__ == '__main__':
+    audio_rate = MIX_DEFAULT_FREQUENCY
+    audio_format = MIX_DEFAULT_FORMAT
+    audio_channels = 2
+    loops = 0
+    reverse_stereo = 0
+    reverse_sample = 0
+
+    i = 1
+    while i < len(sys.argv):
+        arg = sys.argv[i]
+        if arg[0] != '-':
+            break
+        elif arg == '-r':
+            i += 1
+            audio_rate = int(sys.argv[i])
+        elif arg == '-m':
+            audio_channels = 1
+        elif arg == '-c':
+            i += 1
+            audio_channels = int(sys.argv[i])
+        elif arg == '-l':
+            loops = -1
+        elif arg == '-8':
+            audio_format = AUDIO_U8
+        elif arg == '-f':
+            reverse_stereo = 1
+        elif arg == '-F':
+            reverse_sample = 1
+        else:
+            usage()
+            sys.exit(1)
+        i += 1
+
+    if i >= len(sys.argv):
+        usage()
+        sys.exit(1)
+
+    SDL_Init(SDL_INIT_AUDIO)
+
+    Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096)
+    opened, audio_rate, audio_format, audio_channels = Mix_QuerySpec()
+    channels_s = 'mono'
+    if audio_channels == 2:
+        channels_s = 'stereo'
+    elif audio_channels > 2:
+        channels_s = 'surround'
+    print 'Opened audio at %d Hz %d bit %s' % \
+        (audio_rate, audio_format & 0xff, channels_s),
+    if loops:
+        print ' (looping)',
+    print
+
+    if TEST_MIX_VERSIONS:
+        test_versions()
+
+    wave = Mix_LoadWAV(sys.argv[i])
+    if reverse_sample:
+        flip_sample(wave)
+        pass
+
+    if TEST_MIX_CHANNELFINISHED:
+        Mix_ChannelFinished(channel_complete_callback)
+
+    if reverse_stereo:
+        Mix_SetReverseStereo(MIX_CHANNEL_POST, reverse_stereo)
+
+    Mix_PlayChannel(0, wave, loops)
+
+    while still_playing():
+        if TEST_MIX_PANNING:
+            do_panning_update()
+        if TEST_MIX_DISTANCE:
+            do_distance_update()
+        if TEST_MIX_POSITION:
+            do_position_update()
+
+        SDL_Delay(1)
+
+    Mix_FreeChunk(wave)
+    Mix_CloseAudio()
+    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.