@@ -3082,6 +3082,29 @@ def test_downsample_empty_frame(self):
30823082 frames_44100_to_8000 .data , frames_8000 .data , atol = 0.03 , rtol = 0
30833083 )
30843084
3085+ def test_resample_seek_sample_count (self ):
3086+ # Non-regression test for https://github.com/meta-pytorch/torchcodec/issues/1601
3087+ # When resampling, the swresample context buffers samples and tracks a
3088+ # fractional sample position across calls. If it isn't reset on a
3089+ # mid-stream seek, stale state leaks into the next range decode and the
3090+ # output sample count can be off by one.
3091+ # The exact condition in which this happens is unclear to me but claude
3092+ # managed to find this test that reproduces consistently - and the fix
3093+ # was to reset the swresample context on a mid-stream seek, which seems
3094+ # like a very normal thing to do.
3095+ asset = SINE_MONO_S32_44100
3096+ assert asset .sample_rate == 44_100
3097+ assert asset .duration_seconds == 4
3098+
3099+ out_sample_rate = 16_000
3100+ decoder = AudioDecoder (asset .path , sample_rate = out_sample_rate )
3101+
3102+ decoder .get_samples_played_in_range (start_seconds = 2.0 , stop_seconds = 4.8 )
3103+ tail = decoder .get_samples_played_in_range (start_seconds = 3.6 , stop_seconds = 4.8 )
3104+
3105+ # [3.6, 4.0) of audio at out_sample_rate.
3106+ assert tail .data .shape [1 ] == round (0.4 * out_sample_rate )
3107+
30853108 def test_decode_s16_ffmpeg4 (self ):
30863109 # Non-regression test for https://github.com/pytorch/torchcodec/issues/843
30873110 # Ensures that decoding s16 on FFmpeg4 handles
0 commit comments