def get_raw(self): """ get_raw() -> bytes return a bytestring copy of the Sound samples. """ check_mixer() return ffi.buffer(ffi.cast('char*', self.chunk.abuf), self.chunk.alen)[:]
def get_volume(self): """get_volume(): return value get the playback volume""" check_mixer() volume = sdl.Mix_VolumeChunk(self.chunk, -1) return volume / 128.0
def set_num_channels(count): """ set_num_channels(count) -> None set the total number of playback channels """ check_mixer() global _numchanneldata, _channeldata if count > _numchanneldata: _channeldata.extend([ChannelData() for i in range(count - _numchanneldata)]) _numchanneldata = count sdl.Mix_AllocateChannels(count)
def set_num_channels(count): """ set_num_channels(count) -> None set the total number of playback channels """ check_mixer() global _numchanneldata, _channeldata if count > _numchanneldata: _channeldata.extend( [ChannelData() for i in range(count - _numchanneldata)]) _numchanneldata = count sdl.Mix_AllocateChannels(count)
def find_channel(force=False): """find_channel(force=False): return Channel find an unused channel """ check_mixer() chan = sdl.Mix_GroupAvailable(-1) if chan == -1: if not force: return None chan = sdl.Mix_GroupOldest(-1) return Channel(chan)
def get_length(self): """ get_length() -> seconds get the length of the Sound """ check_mixer() frequency, format, channels = (ffi.new('int*'), ffi.new('uint16_t*'), ffi.new('int*')) sdl.Mix_QuerySpec(frequency, format, channels) if format == sdl.AUDIO_S8 or format == sdl.AUDIO_U8: mixerbytes = 1.0 else: mixerbytes = 2.0 numsamples = self.chunk.alen / mixerbytes / channels[0] return numsamples / frequency[0]
def set_volume(self, lvolume, rvolume=None): check_mixer() # This logic differs a bit from pygames because we can use a better # sentinal value if rvolume is None: # No Panning if sdl.Mix_SetPanning(self.chan, 255, 255) == 0: raise SDLError.from_sdl_error() volume = int(lvolume * 128) else: # Panning left = int(lvolume * 255) right = int(rvolume * 255) if sdl.Mix_SetPanning(self.chan, left, right) == 0: raise SDLError.from_sdl_error() volume = 128 sdl.Mix_Volume(self.chan, volume)
def __init__(self, obj=None, **kwargs): check_mixer() self.chunk = None self._mem = None # nasty mangling of parameters! # if 1 position arg: could be filename, file or buffer # if 1 keyword arg: could be filename, file, buffer or array where # filename and file use the same keyword 'file' if obj is not None: if kwargs: raise TypeError("Sound takes either 1 positional or " "1 keyword argument") filename = None buff = None err = None if isinstance(obj, string_types): filename = obj if not isinstance(obj, unicode_): buff = obj elif isinstance(obj, bytes): # For python3, we need to try both paths filename = obj buff = obj elif isinstance(obj, IOBase): rwops = rwops_from_file(obj) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) else: buff = obj if filename is not None: try: filename = rwops_encode_file_path(filename) rwops = rwops_from_file_path(filename) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) except SDLError as e: err = e if not self.chunk and buff is not None: if isinstance(buff, unicode_): raise TypeError("Unicode object not allowed as " "buffer object") try: self._load_from_buffer(buff) except TypeError: # Pygame is special here, and falls through to a # different error if the object doesn't support # the buffer interface. pass else: if len(kwargs) != 1: raise TypeError("Sound takes either 1 positional or " "1 keyword argument") # Py3k Dictionary Views are iterables, not iterators arg_name, arg_value = next(iter(kwargs.items())) if arg_name == 'file': if isinstance(arg_value, string_types): filename = rwops_encode_file_path(arg_value) rwops = rwops_from_file_path(filename, 'rb') elif isinstance(arg_value, bytes): # Needed for python 3 filename = rwops_encode_file_path(arg_value) rwops = rwops_from_file_path(filename, 'rb') else: rwops = rwops_from_file(arg_value) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) elif arg_name == 'buffer': if isinstance(arg_value, unicode_): raise TypeError("Unicode object not allowed as " "buffer object") self._load_from_buffer(arg_value) elif arg_name == 'array': raise NotImplementedError("Loading from array not " "implemented yet") else: raise TypeError("Unrecognized keyword argument '%s'" % arg_name) # pygame uses the pointer address as the tag to ensure # uniqueness, we use id for the same effect # Since we don't have the some automatic casting rules as # C, we explicitly cast to int here. This matches pygames # behaviour, so we're bug-compatible self._chunk_tag = ffi.cast("int", id(self.chunk)) if not self.chunk: if not err: raise TypeError("Unrecognized argument (type %s)" % type(obj).__name__) raise SDLError.from_sdl_error()
def fadeout(self, time): """ fadeout(time) -> None stop playback after fading channel out """ check_mixer() sdl.Mix_FadeOutChannel(self.chan, time)
def get_num_channels(self): """ get_num_channels() -> count count how many times this Sound is playing """ check_mixer() return sdl.Mix_GroupCount(self._chunk_tag)
def get_num_channels(): """get the total number of playback channels""" check_mixer() return sdl.Mix_GroupCount(-1)
def stop(self): """stop() -> None stop sound playback """ check_mixer() sdl.Mix_HaltGroup(self._chunk_tag)
def set_volume(self, volume): """set_volume(value): return None set the playback volume for this Sound""" check_mixer() sdl.Mix_VolumeChunk(self.chunk, int(volume * 128))
def pause(): """pause(): return None temporarily stop playback of all sound channels""" check_mixer() sdl.Mix_Pause(-1)
def set_reserved(count): """ set_reserved(count) -> None reserve channels from being automatically used """ check_mixer() sdl.Mix_ReserveChannels(count)
def fadeout(time): """ fadeout(time) -> None fade out the volume on all sounds before stopping """ check_mixer() sdl.Mix_FadeOutChannel(-1, time)
def unpause(): """unpause(): return None resume paused playback of sound channels""" check_mixer() sdl.Mix_Resume(-1)
def stop(): """stop(): return None stop playback of all sound channels""" check_mixer() sdl.Mix_HaltChannel(-1)
def fadeout(self, time): """ fadeout(time) -> None stop sound playback after fading out """ check_mixer() sdl.Mix_FadeOutGroup(self._chunk_tag, time)
def stop(self): check_mixer() sdl.Mix_HaltChannel(self.chan)
def unpause(self): check_mixer() sdl.Mix_Resume(self.chan)
def get_busy(self): check_mixer() return sdl.Mix_Playing(self.chan) != 0
def pause(self): check_mixer() sdl.Mix_Pause(self.chan)
def get_volume(self): check_mixer() volume = sdl.Mix_Volume(self.chan, -1) return volume / 128.0
def __init__(self, obj=None, **kwargs): check_mixer() self.chunk = None # nasty mangling of parameters! # if 1 position arg: could be filename, file or buffer # if 1 keyword arg: could be filename, file, buffer or array where # filename and file use the same keyword 'file' if obj is not None: if kwargs: raise TypeError("Sound takes either 1 positional or " "1 keyword argument") filename = None buff = None err = None if isinstance(obj, basestring): filename = obj if not isinstance(obj, unicode): buff = obj elif isinstance(obj, file): rwops = rwops_from_file(obj) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) else: buff = obj if filename is not None: try: filename = rwops_encode_file_path(filename) rwops = rwops_from_file_path(filename) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) except SDLError as e: err = e if not self.chunk and buff is not None: raise NotImplementedError("Loading from buffer not " "implemented yet") # TODO: check if buff implements buffer interface. # If it does, load from buffer. If not, re-raise # error from filename if filename is not None. else: if len(kwargs) != 1: raise TypeError("Sound takes either 1 positional or " "1 keyword argument") arg_name = kwargs.keys()[0] arg_value = kwargs[arg_name] if arg_name == 'file': if isinstance(arg_value, basestring): filename = rwops_encode_file_path(arg_value) rwops = rwops_from_file_path(filename, 'rb') else: rwops = rwops_from_file(arg_value) self.chunk = sdl.Mix_LoadWAV_RW(rwops, 1) elif arg_name == 'buffer': if isinstance(arg_name, unicode): raise TypeError("Unicode object not allowed as " "buffer object") raise NotImplementedError("Loading from buffer not " "implemented yet") elif arg_name == 'array': raise NotImplementedError("Loading from array not " "implemented yet") else: raise TypeError("Unrecognized keyword argument '%s'" % arg_name) # pygame uses the pointer address as the tag to ensure # uniqueness, we use id for the same effect # Since we don't have the some automatic casting rules as # C, we explicitly cast to int here. This matches pygames # behaviour, so we're bug-compatible self._chunk_tag = ffi.cast("int", id(self.chunk)) if not self.chunk: raise SDLError.from_sdl_error()