@@ -1112,7 +1112,8 @@ def test_start_equals_stop(self, asset):
1112
1112
1113
1113
def test_frame_start_is_not_zero (self ):
1114
1114
# For NASA_AUDIO_MP3, the first frame is not at 0, it's at 0.138125.
1115
- # So if we request start = 0.05, we shouldn't be truncating anything.
1115
+ # So if we request (start, stop) = (0.05, None), we shouldn't be
1116
+ # truncating anything.
1116
1117
1117
1118
asset = NASA_AUDIO_MP3
1118
1119
start_seconds = 0.05 # this is less than the first frame's pts
@@ -1128,6 +1129,35 @@ def test_frame_start_is_not_zero(self):
1128
1129
reference_frames = asset .get_frame_data_by_range (start = 0 , stop = stop_frame_index )
1129
1130
torch .testing .assert_close (samples .data , reference_frames )
1130
1131
1132
+ # Non-regression test for https://github.com/pytorch/torchcodec/issues/567
1133
+ # If we ask for start < stop <= first_frame_pts, we should raise.
1134
+ with pytest .raises (RuntimeError , match = "No audio frames were decoded" ):
1135
+ decoder .get_samples_played_in_range (start_seconds = 0 , stop_seconds = 0.05 )
1136
+
1137
+ first_frame_pts_seconds = asset .get_frame_info (idx = 0 ).pts_seconds
1138
+ with pytest .raises (RuntimeError , match = "No audio frames were decoded" ):
1139
+ decoder .get_samples_played_in_range (
1140
+ start_seconds = 0 , stop_seconds = first_frame_pts_seconds
1141
+ )
1142
+
1143
+ # Documenting an edge case: we ask for samples barely beyond the start
1144
+ # of the first frame. The C++ decoder returns the first frame, which
1145
+ # gets (correctly!) truncated by the AudioDecoder, and we end up with
1146
+ # empty data.
1147
+ samples = decoder .get_samples_played_in_range (
1148
+ start_seconds = 0 , stop_seconds = first_frame_pts_seconds + 1e-5
1149
+ )
1150
+ assert samples .data .shape == (2 , 0 )
1151
+ assert samples .pts_seconds == first_frame_pts_seconds
1152
+ assert samples .duration_seconds == 0
1153
+
1154
+ # if we ask for a little bit more samples, we get non-empty data
1155
+ samples = decoder .get_samples_played_in_range (
1156
+ start_seconds = 0 , stop_seconds = first_frame_pts_seconds + 1e-3
1157
+ )
1158
+ assert samples .data .shape == (2 , 8 )
1159
+ assert samples .pts_seconds == first_frame_pts_seconds
1160
+
1131
1161
def test_single_channel (self ):
1132
1162
asset = SINE_MONO_S32
1133
1163
decoder = AudioDecoder (asset .path )
0 commit comments