From b6d7cf1bdac1b65635d2ebbb67bc03c3cd932f10 Mon Sep 17 00:00:00 2001 From: Guy Illes Date: Wed, 15 Jan 2025 10:08:23 +0000 Subject: [PATCH] creating blocksize length output array in blocks reading if fill_value is set regardless of frames in file --- soundfile.py | 2 +- tests/test_soundfile.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/soundfile.py b/soundfile.py index 1fd2b73..0ea82f4 100644 --- a/soundfile.py +++ b/soundfile.py @@ -1107,7 +1107,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: diff --git a/tests/test_soundfile.py b/tests/test_soundfile.py index 07115af..b56ac8d 100644 --- a/tests/test_soundfile.py +++ b/tests/test_soundfile.py @@ -403,17 +403,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]])