Exemple #1
0
def play(loops=0, startpos=0.0):
    """play(loops=0, start=0.0): return None
       Start the playback of the music stream"""
    global _current_music, _music_pos, _music_pos_time, \
           _music_frequency, _music_format, _music_channels
    check_mixer()

    if not _current_music:
        raise SDLError("music not loaded")

    sdl.Mix_HookMusicFinished(_endmusic_callback)
    sdl.Mix_SetPostMix(_mixmusic_callback, ffi.NULL)
    frequency, format, channels = (ffi.new('int*'), ffi.new('uint16_t*'),
                                   ffi.new('int*'))
    sdl.Mix_QuerySpec(frequency, format, channels)
    _music_pos = 0
    _music_pos_time = sdl.SDL_GetTicks()
    _music_frequency = frequency[0]
    _music_format = format[0]
    _music_channels = channels[0]
    volume = sdl.Mix_VolumeMusic(-1)
    val = sdl.Mix_FadeInMusicPos(_current_music, loops, 0, startpos)
    sdl.Mix_VolumeMusic(volume)
    if val == -1:
        raise SDLError.from_sdl_error()
Exemple #2
0
def autoinit(frequency=None, size=None, channels=None, chunksize=None):
    if not frequency:
        frequency = _request_frequency
    if not size:
        size = _request_size
    if not channels:
        channels = _request_stereo
    if not chunksize:
        chunksize = _request_chunksize

    if channels >= 2:
        channels = 2
    else:
        channels = 1

    # chunk must be a power of 2
    chunksize = int(math.log(chunksize, 2))
    chunksize = 2**chunksize
    if chunksize < buffer:
        chunksize *= 2

    # fmt is a bunch of flags
    if size == 8:
        fmt = sdl.AUDIO_U8
    elif size == -8:
        fmt = sdl.AUDIO_S8
    elif size == 16:
        fmt = sdl.AUDIO_U16SYS
    elif size == -16:
        fmt = sdl.AUDIO_S16SYS
    else:
        raise ValueError("unsupported size %d" % size)

    global _numchanneldata, _channeldata
    if not sdl.SDL_WasInit(sdl.SDL_INIT_AUDIO):
        register_quit(autoquit)
        # channel stuff
        if not _channeldata:
            _numchanneldata = sdl.MIX_CHANNELS
            _channeldata = [ChannelData() for i in range(_numchanneldata)]

        if sdl.SDL_InitSubSystem(sdl.SDL_INIT_AUDIO) == -1:
            return False

        if sdl.Mix_OpenAudio(frequency, fmt, channels, chunksize) == -1:
            sdl.SDL_QuitSubSystem(sdl.SDL_INIT_AUDIO)
            return False
        sdl.Mix_ChannelFinished(_endsound_callback)
        # TODO: reverse stereo for 8-bit below SDL 1.2.8
        sdl.Mix_VolumeMusic(127)
    return True
Exemple #3
0
def set_volume(volume):
    """set_volume(value): return None
       set the music volume"""
    check_mixer()
    sdl.Mix_VolumeMusic(int(volume * 128))
Exemple #4
0
def get_volume():
    """music.get_volume(): return value
    get the music volume"""
    check_mixer()
    volume = sdl.Mix_VolumeMusic(-1)
    return volume / 128.0