Exemple #1
0
    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()
Exemple #2
0
def save_tga(surf, filename, rle=False):
    rwops = rwops_from_file_path(filename, 'wb')
    result = sdl._pygame_SaveTGA_RW(surf, rwops, 1 if rle else 0)
    sdl.SDL_RWclose(rwops)
    return result
Exemple #3
0
def save_tga(surf, filename, rle=False):
    rwops = rwops_from_file_path(filename, 'wb')
    result = sdl._pygame_SaveTGA_RW(surf, rwops, 1 if rle else 0)
    sdl.SDL_RWclose(rwops)
    return result
Exemple #4
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()
Exemple #5
0
    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()
Exemple #6
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()