Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing blocks read with overlap for files shorter than block size #446

Merged
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions soundfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,10 +1103,12 @@ def blocks(self, blocksize=None, overlap=0, frames=-1, dtype='float64',
if 'r' not in self.mode and '+' not in self.mode:
raise SoundFileRuntimeError("blocks() is not allowed in write-only mode")

frames = self._check_frames(frames, fill_value)
if out is None:
if blocksize is None:
raise TypeError("One of {blocksize, out} must be specified")
out = self._create_empty_array(blocksize, always_2d, dtype)
out_size = min(blocksize, frames)
out = self._create_empty_array(out_size, always_2d, dtype)
copy_out = True
else:
if blocksize is not None:
Expand All @@ -1116,7 +1118,6 @@ def blocks(self, blocksize=None, overlap=0, frames=-1, dtype='float64',
copy_out = False

overlap_memory = None
frames = self._check_frames(frames, fill_value)
while frames > 0:
if overlap_memory is None:
output_offset = 0
Expand Down
6 changes: 6 additions & 0 deletions tests/test_soundfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,12 @@ def test_blocks_mono():
assert_equal_list_of_arrays(blocks, [[0, 1, 2], [-2, -1, 0]])


def test_block_longer_than_file_with_overlap_mono():
blocks = list(sf.blocks(filename_mono, blocksize=20, dtype='int16',
overlap=2))
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2, -1]])


def test_blocks_rplus(sf_stereo_rplus):
blocks = list(sf_stereo_rplus.blocks(blocksize=2))
assert_equal_list_of_arrays(blocks, [data_stereo[0:2], data_stereo[2:4]])
Expand Down
Loading