Skip to content

Commit c7b3a55

Browse files
committed
Fixing blocks read with overlap for files shorter than block size
1 parent 08750e3 commit c7b3a55

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

soundfile.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,10 +1103,12 @@ def blocks(self, blocksize=None, overlap=0, frames=-1, dtype='float64',
11031103
if 'r' not in self.mode and '+' not in self.mode:
11041104
raise SoundFileRuntimeError("blocks() is not allowed in write-only mode")
11051105

1106+
frames = self._check_frames(frames, fill_value)
11061107
if out is None:
11071108
if blocksize is None:
11081109
raise TypeError("One of {blocksize, out} must be specified")
1109-
out = self._create_empty_array(blocksize, always_2d, dtype)
1110+
out_size = min(blocksize, frames)
1111+
out = self._create_empty_array(out_size, always_2d, dtype)
11101112
copy_out = True
11111113
else:
11121114
if blocksize is not None:
@@ -1116,7 +1118,6 @@ def blocks(self, blocksize=None, overlap=0, frames=-1, dtype='float64',
11161118
copy_out = False
11171119

11181120
overlap_memory = None
1119-
frames = self._check_frames(frames, fill_value)
11201121
while frames > 0:
11211122
if overlap_memory is None:
11221123
output_offset = 0

tests/test_soundfile.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ def test_blocks_mono():
408408
assert_equal_list_of_arrays(blocks, [[0, 1, 2], [-2, -1, 0]])
409409

410410

411+
def test_block_longer_than_file_with_overlap_mono():
412+
blocks = list(sf.blocks(filename_mono, blocksize=20, dtype='int16',
413+
overlap=2))
414+
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2, -1]])
415+
416+
411417
def test_blocks_rplus(sf_stereo_rplus):
412418
blocks = list(sf_stereo_rplus.blocks(blocksize=2))
413419
assert_equal_list_of_arrays(blocks, [data_stereo[0:2], data_stereo[2:4]])

0 commit comments

Comments
 (0)