Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions soundfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,16 +832,12 @@ def read(self, frames=-1, dtype='float64', always_2d=True,
frames = len(out)
if not out.flags.c_contiguous:
raise ValueError("out must be C-contiguous")

self._check_array(out)
frames = self._read_or_write('sf_readf_', out, frames)

if len(out) > frames:
if fill_value is None:
out = out[:frames]
else:
out[frames:] = fill_value

return out

def write(self, data):
Expand All @@ -859,11 +855,8 @@ def write(self, data):
"""
# no copy is made if data has already the correct memory layout:
data = _np.ascontiguousarray(data)

self._check_array(data)
written = self._read_or_write('sf_writef_', data, len(data))
assert written == len(data)

if self.seekable():
curr = self.tell()
self._info.frames = self.seek(0, SEEK_END)
Expand Down Expand Up @@ -1116,17 +1109,6 @@ def _check_frames(self, frames, fill_value):
raise ValueError("frames must be specified for non-seekable files")
return frames

def _check_array(self, array):
"""Do some error checking."""
if (array.ndim not in (1, 2) or
array.ndim == 1 and self.channels != 1 or
array.ndim == 2 and array.shape[1] != self.channels):
raise ValueError("Invalid shape: {0!r}".format(array.shape))

if array.dtype not in _ffi_types:
raise ValueError("dtype must be one of {0!r}".format(
sorted(dt.name for dt in _ffi_types)))

def _create_empty_array(self, frames, always_2d, dtype):
"""Create an empty array with appropriate shape."""
if always_2d or self.channels > 1:
Expand All @@ -1136,10 +1118,18 @@ def _create_empty_array(self, frames, always_2d, dtype):
return _np.empty(shape, dtype, order='C')

def _read_or_write(self, funcname, array, frames):
"""Call into libsndfile."""
"""Do some error checking and call into libsndfile."""
self._check_if_closed()
if (array.ndim not in (1, 2) or
array.ndim == 1 and self.channels != 1 or
array.ndim == 2 and array.shape[1] != self.channels):
raise ValueError("Invalid shape: {0!r}".format(array.shape))
try:
ffi_type = _ffi_types[array.dtype]
except KeyError:
raise ValueError("dtype must be one of {0!r}".format(
sorted(dt.name for dt in _ffi_types)))

ffi_type = _ffi_types[array.dtype]
assert array.flags.c_contiguous
assert array.dtype.itemsize == _ffi.sizeof(ffi_type)
assert array.size >= frames * self.channels
Expand Down