Example #1
0
    def basic_writer(data, filename, fs = def_fs, enc = format.encoding):
        """Common "template" to all write functions."""
        if np.ndim(data) <= 1:
            nc      = 1
        elif np.ndim(data) == 2:
            nc      = data.shape[1]
        else:
            RuntimeError("Only rank 0, 1, and 2 arrays supported as audio data")

        uformat = Format(format.file_format, encoding=enc,
                             endianness=format.endianness)
        hdl = Sndfile(filename, 'w', uformat, nc, fs)
        try:
            hdl.write_frames(data)
        finally:
            hdl.close()
Example #2
0
    def __init__(self,
                 filename,
                 mode='read',
                 format=None,
                 channels=0,
                 samplerate=0):
        """Create an instance of sndfile.

        :Parameters:
            filename : string or int
                name of the file to open (string), or file descriptor (integer)
            mode : string
                'read' for read, 'write' for write, or 'rwrite' for read and
                write.
            format : formatinfo
                when opening a new file for writing, give the format to write
                in.
            channels : int
                number of channels.
            samplerate : int
                sampling rate.

        :Returns:
            sndfile: a valid sndfile object

        Notes
        -----

        format, channels and samplerate need to be given only in the write
        modes and for raw files.  """
        warnings.warn(
            "sndfile class is deprecated, please use Sndfile instead",
            DeprecationWarning)
        if format is not None and not isinstance(format, formatinfo):
            raise ValueError("format argument must be None or " \
                             "formatinfo instance.")
        if format is None:
            f = None
        else:
            f = format._format

        self._sndfile = Sndfile(filename, _COMPAT_MODES[mode], f, channels,
                                samplerate)

        # We create a formatinfo instance from a Format info
        self._format = formatinfo()
        self._format._format = self._sndfile.format
Example #3
0
    def __init__(self, filename, mode='read', format=None, channels=0,
                 samplerate=0):
        """Create an instance of sndfile.

        :Parameters:
            filename : string or int
                name of the file to open (string), or file descriptor (integer)
            mode : string
                'read' for read, 'write' for write, or 'rwrite' for read and
                write.
            format : formatinfo
                when opening a new file for writing, give the format to write
                in.
            channels : int
                number of channels.
            samplerate : int
                sampling rate.

        :Returns:
            sndfile : Sndfile
                a valid sndfile object

        Notes
        -----

        format, channels and samplerate need to be given only in the write
        modes and for raw files.  """
        warnings.warn("sndfile class is deprecated, please use Sndfile instead",
                      DeprecationWarning)
        if format is not None and not isinstance(format, formatinfo):
            raise ValueError("format argument must be None or " \
                             "formatinfo instance.")
        if format is None:
            f = None
        else:
            f = format._format
        
        self._sndfile = Sndfile(filename, _COMPAT_MODES[mode], f, channels,
                                samplerate)

        # We create a formatinfo instance from a Format info
        self._format = formatinfo()
        self._format._format = self._sndfile.format
Example #4
0
    def basic_writer(data, filename, fs=def_fs, enc=format.encoding):
        """Common "template" to all write functions."""
        if np.ndim(data) <= 1:
            nc = 1
        elif np.ndim(data) == 2:
            nc = data.shape[1]
        else:
            RuntimeError(
                "Only rank 0, 1, and 2 arrays supported as audio data")

        uformat = Format(format.file_format,
                         encoding=enc,
                         endianness=format.endianness)
        hdl = Sndfile(filename, 'w', uformat, nc, fs)
        try:
            hdl.write_frames(data)
        finally:
            hdl.close()
Example #5
0
    def basic_reader(filename, last=None, first=0):
        """Common "template" to all read functions."""
        hdl = Sndfile(filename, 'r')
        try:
            if not hdl.format.file_format == filetype:
                raise ValueError, "%s is not a %s file (is %s)" \
                      % (filename, filetype, hdl.format.file_format)

            fs = hdl.samplerate
            enc = hdl.encoding
            # Set the pointer to start position
            nf = hdl.seek(first, 1)
            if not nf == first:
                raise IOError("Error while seeking at starting position")

            if last is None:
                nframes = hdl.nframes - first
                data = hdl.read_frames(nframes)
            else:
                data = hdl.read_frames(last)
        finally:
            hdl.close()

        return data, fs, enc
Example #6
0
    def basic_reader(filename, last = None, first = 0):
        """Common "template" to all read functions."""
        hdl = Sndfile(filename, 'r')
        try:
            if not hdl.format.file_format == filetype:
                raise ValueError, "%s is not a %s file (is %s)" \
                      % (filename, filetype, hdl.format.file_format)

            fs = hdl.samplerate
            enc = hdl.encoding
            # Set the pointer to start position
            nf  = hdl.seek(first, 1)
            if not nf == first:
                raise IOError("Error while seeking at starting position")

            if last is None:
                nframes = hdl.nframes - first
                data    = hdl.read_frames(nframes)
            else:
                data    = hdl.read_frames(last)
        finally:
            hdl.close()

        return data, fs, enc
Example #7
0
class sndfile:
    """Main class to open, read and write audio files"""
    def __init__(self, filename, mode='read', format=None, channels=0,
                 samplerate=0):
        """Create an instance of sndfile.

        :Parameters:
            filename : string or int
                name of the file to open (string), or file descriptor (integer)
            mode : string
                'read' for read, 'write' for write, or 'rwrite' for read and
                write.
            format : formatinfo
                when opening a new file for writing, give the format to write
                in.
            channels : int
                number of channels.
            samplerate : int
                sampling rate.

        :Returns:
            sndfile: a valid sndfile object

        Notes
        -----

        format, channels and samplerate need to be given only in the write
        modes and for raw files.  """
        warnings.warn("sndfile class is deprecated, please use Sndfile instead",
                      DeprecationWarning)
        if format is not None and not isinstance(format, formatinfo):
            raise ValueError("format argument must be None or " \
                             "formatinfo instance.")
        if format is None:
            f = None
        else:
            f = format._format
        
        self._sndfile = Sndfile(filename, _COMPAT_MODES[mode], f, channels,
                                samplerate)

        # We create a formatinfo instance from a Format info
        self._format = formatinfo()
        self._format._format = self._sndfile.format

    def close(self):
        """close the file."""
        self._sndfile.close()

    def sync(self):
        """call the operating system's function to force the writing of all
        file cache buffers to disk the file.

        No effect if file is open as read"""
        self._sndfile.sync()

    def seek(self, offset, whence=0, mode='rw'):
        """similar to python seek function, taking only in account audio data.

        :Parameters:
            offset : int
                the number of frames (eg two samples for stereo files) to move
                relatively to position set by whence.
            whence : int
                only 0 (beginning), 1 (current) and 2 (end of the file) are
                valid.
            mode : string
                If set to 'rw', both read and write pointers are updated. If
                'r' is given, only read pointer is updated, if 'w', only the
                write one is (this may of course make sense only if you open
                the file in a certain mode).

        Notes
        -----

        - one only takes into accound audio data.
        - if an invalid seek is given (beyond or before the file), a
          PyaudioIOError is launched."""
        try:
            st = self._sndfile.seek(offset, whence, mode)
        except IOError, e:
            raise PyaudioIOError(str(e))
        return st
Example #8
0
class sndfile:
    """Main class to open, read and write audio files"""
    def __init__(self,
                 filename,
                 mode='read',
                 format=None,
                 channels=0,
                 samplerate=0):
        """Create an instance of sndfile.

        :Parameters:
            filename : string or int
                name of the file to open (string), or file descriptor (integer)
            mode : string
                'read' for read, 'write' for write, or 'rwrite' for read and
                write.
            format : formatinfo
                when opening a new file for writing, give the format to write
                in.
            channels : int
                number of channels.
            samplerate : int
                sampling rate.

        :Returns:
            sndfile: a valid sndfile object

        Notes
        -----

        format, channels and samplerate need to be given only in the write
        modes and for raw files.  """
        warnings.warn(
            "sndfile class is deprecated, please use Sndfile instead",
            DeprecationWarning)
        if format is not None and not isinstance(format, formatinfo):
            raise ValueError("format argument must be None or " \
                             "formatinfo instance.")
        if format is None:
            f = None
        else:
            f = format._format

        self._sndfile = Sndfile(filename, _COMPAT_MODES[mode], f, channels,
                                samplerate)

        # We create a formatinfo instance from a Format info
        self._format = formatinfo()
        self._format._format = self._sndfile.format

    def close(self):
        """close the file."""
        self._sndfile.close()

    def sync(self):
        """call the operating system's function to force the writing of all
        file cache buffers to disk the file.

        No effect if file is open as read"""
        self._sndfile.sync()

    def seek(self, offset, whence=0, mode='rw'):
        """similar to python seek function, taking only in account audio data.

        :Parameters:
            offset : int
                the number of frames (eg two samples for stereo files) to move
                relatively to position set by whence.
            whence : int
                only 0 (beginning), 1 (current) and 2 (end of the file) are
                valid.
            mode : string
                If set to 'rw', both read and write pointers are updated. If
                'r' is given, only read pointer is updated, if 'w', only the
                write one is (this may of course make sense only if you open
                the file in a certain mode).

        Notes
        -----

        - one only takes into accound audio data.
        - if an invalid seek is given (beyond or before the file), a
          PyaudioIOError is launched."""
        try:
            st = self._sndfile.seek(offset, whence, mode)
        except IOError, e:
            raise PyaudioIOError(str(e))
        return st