From c7b3a553e55db8ae9c5947eba66507234c48c2ee Mon Sep 17 00:00:00 2001 From: Guy Illes Date: Thu, 7 Nov 2024 14:58:57 +0100 Subject: [PATCH] Fixing blocks read with overlap for files shorter than block size --- soundfile.py | 5 +++-- tests/test_soundfile.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/soundfile.py b/soundfile.py index cc13192..1fd2b73 100644 --- a/soundfile.py +++ b/soundfile.py @@ -1103,10 +1103,12 @@ def blocks(self, blocksize=None, overlap=0, frames=-1, dtype='float64', if 'r' not in self.mode and '+' not in self.mode: raise SoundFileRuntimeError("blocks() is not allowed in write-only mode") + frames = self._check_frames(frames, fill_value) if out is None: if blocksize is None: raise TypeError("One of {blocksize, out} must be specified") - out = self._create_empty_array(blocksize, always_2d, dtype) + out_size = min(blocksize, frames) + out = self._create_empty_array(out_size, always_2d, dtype) copy_out = True else: if blocksize is not None: @@ -1116,7 +1118,6 @@ def blocks(self, blocksize=None, overlap=0, frames=-1, dtype='float64', copy_out = False overlap_memory = None - frames = self._check_frames(frames, fill_value) while frames > 0: if overlap_memory is None: output_offset = 0 diff --git a/tests/test_soundfile.py b/tests/test_soundfile.py index 9eaa5f7..07115af 100644 --- a/tests/test_soundfile.py +++ b/tests/test_soundfile.py @@ -408,6 +408,12 @@ def test_blocks_mono(): assert_equal_list_of_arrays(blocks, [[0, 1, 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_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]])