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 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()
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