Skip to content

Commit b6d7cf1

Browse files
committed
creating blocksize length output array in blocks reading if fill_value is set regardless of frames in file
1 parent c7b3a55 commit b6d7cf1

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

soundfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ def blocks(self, blocksize=None, overlap=0, frames=-1, dtype='float64',
11071107
if out is None:
11081108
if blocksize is None:
11091109
raise TypeError("One of {blocksize, out} must be specified")
1110-
out_size = min(blocksize, frames)
1110+
out_size = blocksize if fill_value is not None else min(blocksize, frames)
11111111
out = self._create_empty_array(out_size, always_2d, dtype)
11121112
copy_out = True
11131113
else:

tests/test_soundfile.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,17 +403,40 @@ def test_blocks_inplace_modification(file_stereo_r):
403403

404404

405405
def test_blocks_mono():
406+
blocks = list(sf.blocks(filename_mono, blocksize=3, dtype='int16'))
407+
assert_equal_list_of_arrays(blocks, [[0, 1, 2], [-2, -1]])
408+
409+
410+
def test_blocks_with_fill_value_mono():
406411
blocks = list(sf.blocks(filename_mono, blocksize=3, dtype='int16',
407412
fill_value=0))
408413
assert_equal_list_of_arrays(blocks, [[0, 1, 2], [-2, -1, 0]])
409414

410415

416+
def test_blocks_with_overlap_and_fill_value_mono():
417+
blocks = list(sf.blocks(filename_mono, blocksize=4, dtype='int16',
418+
overlap=2, fill_value=0))
419+
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2], [2, -2, -1, 0]])
420+
421+
411422
def test_block_longer_than_file_with_overlap_mono():
412423
blocks = list(sf.blocks(filename_mono, blocksize=20, dtype='int16',
413424
overlap=2))
414425
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2, -1]])
415426

416427

428+
def test_block_longer_than_file_with_fill_value_mono():
429+
blocks = list(sf.blocks(filename_mono, blocksize=10, dtype='int16',
430+
fill_value=0))
431+
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2, -1, 0, 0, 0, 0, 0]])
432+
433+
434+
def test_block_longer_than_file_with_overlap_and_fill_value_mono():
435+
blocks = list(sf.blocks(filename_mono, blocksize=10, dtype='int16',
436+
overlap=2, fill_value=0))
437+
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2, -1, 0, 0, 0, 0, 0]])
438+
439+
417440
def test_blocks_rplus(sf_stereo_rplus):
418441
blocks = list(sf_stereo_rplus.blocks(blocksize=2))
419442
assert_equal_list_of_arrays(blocks, [data_stereo[0:2], data_stereo[2:4]])

0 commit comments

Comments
 (0)