Skip to content

Commit 635bc9e

Browse files
authored
Merge pull request #457 from illesguy/fix-fill-value-ignored-when-blocksize-longer-than-file
creating blocksize length output array in blocks reading if fill_value is set regardless of frames in file
2 parents 9a508cd + b6d7cf1 commit 635bc9e

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
@@ -1155,7 +1155,7 @@ def blocks(self, blocksize=None, overlap=0, frames=-1, dtype='float64',
11551155
if out is None:
11561156
if blocksize is None:
11571157
raise TypeError("One of {blocksize, out} must be specified")
1158-
out_size = min(blocksize, frames)
1158+
out_size = blocksize if fill_value is not None else min(blocksize, frames)
11591159
out = self._create_empty_array(out_size, always_2d, dtype)
11601160
copy_out = True
11611161
else:

tests/test_soundfile.py

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

462462

463463
def test_blocks_mono():
464+
blocks = list(sf.blocks(filename_mono, blocksize=3, dtype='int16'))
465+
assert_equal_list_of_arrays(blocks, [[0, 1, 2], [-2, -1]])
466+
467+
468+
def test_blocks_with_fill_value_mono():
464469
blocks = list(sf.blocks(filename_mono, blocksize=3, dtype='int16',
465470
fill_value=0))
466471
assert_equal_list_of_arrays(blocks, [[0, 1, 2], [-2, -1, 0]])
467472

468473

474+
def test_blocks_with_overlap_and_fill_value_mono():
475+
blocks = list(sf.blocks(filename_mono, blocksize=4, dtype='int16',
476+
overlap=2, fill_value=0))
477+
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2], [2, -2, -1, 0]])
478+
479+
469480
def test_block_longer_than_file_with_overlap_mono():
470481
blocks = list(sf.blocks(filename_mono, blocksize=20, dtype='int16',
471482
overlap=2))
472483
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2, -1]])
473484

474485

486+
def test_block_longer_than_file_with_fill_value_mono():
487+
blocks = list(sf.blocks(filename_mono, blocksize=10, dtype='int16',
488+
fill_value=0))
489+
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2, -1, 0, 0, 0, 0, 0]])
490+
491+
492+
def test_block_longer_than_file_with_overlap_and_fill_value_mono():
493+
blocks = list(sf.blocks(filename_mono, blocksize=10, dtype='int16',
494+
overlap=2, fill_value=0))
495+
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2, -1, 0, 0, 0, 0, 0]])
496+
497+
475498
def test_blocks_rplus(sf_stereo_rplus):
476499
blocks = list(sf_stereo_rplus.blocks(blocksize=2))
477500
assert_equal_list_of_arrays(blocks, [data_stereo[0:2], data_stereo[2:4]])

0 commit comments

Comments
 (0)