Skip to content

Commit

Permalink
Merge pull request #457 from illesguy/fix-fill-value-ignored-when-blo…
Browse files Browse the repository at this point in the history
…cksize-longer-than-file

creating blocksize length output array in blocks reading if fill_value is set regardless of frames in file
  • Loading branch information
bastibe authored Jan 19, 2025
2 parents 9a508cd + b6d7cf1 commit 635bc9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion soundfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ def blocks(self, blocksize=None, overlap=0, frames=-1, dtype='float64',
if out is None:
if blocksize is None:
raise TypeError("One of {blocksize, out} must be specified")
out_size = min(blocksize, frames)
out_size = blocksize if fill_value is not None else min(blocksize, frames)
out = self._create_empty_array(out_size, always_2d, dtype)
copy_out = True
else:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_soundfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,17 +461,40 @@ def test_blocks_inplace_modification(file_stereo_r):


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


def test_blocks_with_fill_value_mono():
blocks = list(sf.blocks(filename_mono, blocksize=3, dtype='int16',
fill_value=0))
assert_equal_list_of_arrays(blocks, [[0, 1, 2], [-2, -1, 0]])


def test_blocks_with_overlap_and_fill_value_mono():
blocks = list(sf.blocks(filename_mono, blocksize=4, dtype='int16',
overlap=2, fill_value=0))
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2], [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_block_longer_than_file_with_fill_value_mono():
blocks = list(sf.blocks(filename_mono, blocksize=10, dtype='int16',
fill_value=0))
assert_equal_list_of_arrays(blocks, [[0, 1, 2, -2, -1, 0, 0, 0, 0, 0]])


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


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 635bc9e

Please sign in to comment.