Skip to content

Commit

Permalink
Fixing blocks read with overlap for files shorter than block size
Browse files Browse the repository at this point in the history
  • Loading branch information
illesguy committed Nov 7, 2024
1 parent 08750e3 commit c7b3a55
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
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

0 comments on commit c7b3a55

Please sign in to comment.