Debian Codegraph @

tests/test_audioresampler.py

raw
1 from fractions import Fraction
2
3 import pytest
4
5 import av
6 from av import AudioFrame, AudioResampler
7
8
9 def test_flush_immediately() -> None:
10 """
11 If we flush the resampler before passing any input, it returns
12 a `None` frame without setting up the graph.
13 """
14
15 resampler = AudioResampler()
16
17 # flush
18 oframes = resampler.resample(None)
19 assert len(oframes) == 0
20
21
22 def test_identity_passthrough() -> None:
23 """
24 If we don't ask it to do anything, it won't.
25 """
26
27 resampler = AudioResampler()
28
29 # resample one frame
30 iframe = AudioFrame("s16", "stereo", 1024)
31
32 oframes = resampler.resample(iframe)
33 assert len(oframes) == 1
34 assert iframe is oframes[0]
35
36 # resample another frame
37 iframe.pts = 1024
38
39 oframes = resampler.resample(iframe)
40 assert len(oframes) == 1
41 assert iframe is oframes[0]
42
43 # flush
44 oframes = resampler.resample(None)
45 assert len(oframes) == 0
46
47
48 def test_matching_passthrough() -> None:
49 """
50 If the frames match, it won't do anything.
51 """
52
53 resampler = AudioResampler("s16", "stereo")
54
55 # resample one frame
56 iframe = AudioFrame("s16", "stereo", 1024)
57
58 oframes = resampler.resample(iframe)
59 assert len(oframes) == 1
60 assert iframe is oframes[0]
61
62 # resample another frame
63 iframe.pts = 1024
64
65 oframes = resampler.resample(iframe)
66 assert len(oframes) == 1
67 assert iframe is oframes[0]
68
69 # flush
70 oframes = resampler.resample(None)
71 assert len(oframes) == 0
72
73
74 def test_pts_assertion_same_rate() -> None:
75 av.logging.set_level(av.logging.VERBOSE)
76 resampler = AudioResampler("s16", "mono")
77
78 # resample one frame
79 iframe = AudioFrame("s16", "stereo", 1024)
80 iframe.sample_rate = 48000
81 iframe.time_base = Fraction(1, 48000)
82 iframe.pts = 0
83
84 oframes = resampler.resample(iframe)
85 assert len(oframes) == 1
86
87 oframe = oframes[0]
88 assert oframe.pts == 0
89 assert oframe.time_base == iframe.time_base
90 assert oframe.sample_rate == iframe.sample_rate
91 assert oframe.samples == iframe.samples
92
93 # resample another frame
94 iframe.pts = 1024
95
96 oframes = resampler.resample(iframe)
97 assert len(oframes) == 1
98
99 oframe = oframes[0]
100 assert oframe.pts == 1024
101 assert oframe.time_base == iframe.time_base
102 assert oframe.sample_rate == iframe.sample_rate
103 assert oframe.samples == iframe.samples
104
105 # resample another frame with a pts gap, do not raise exception
106 iframe.pts = 9999
107 oframes = resampler.resample(iframe)
108 assert len(oframes) == 1
109
110 oframe = oframes[0]
111 assert oframe.pts == 9999
112 assert oframe.time_base == iframe.time_base
113 assert oframe.sample_rate == iframe.sample_rate
114 assert oframe.samples == iframe.samples
115
116 # flush
117 oframes = resampler.resample(None)
118 assert len(oframes) == 0
119 av.logging.set_level(None)
120
121
122 def test_pts_assertion_new_rate_up() -> None:
123 resampler = AudioResampler("s16", "mono", 44100)
124
125 # resample one frame
126 iframe = AudioFrame("s16", "stereo", 1024)
127 iframe.sample_rate = 48000
128 iframe.time_base = Fraction(1, 48000)
129 iframe.pts = 0
130
131 oframes = resampler.resample(iframe)
132 assert len(oframes) == 1
133
134 oframe = oframes[0]
135 assert oframe.pts == 0
136 assert oframe.time_base == Fraction(1, 44100)
137 assert oframe.sample_rate == 44100
138 assert oframe.samples == 925
139
140 iframe = AudioFrame("s16", "stereo", 1024)
141 iframe.sample_rate = 48000
142 iframe.time_base = Fraction(1, 48000)
143 iframe.pts = 1024
144
145 oframes = resampler.resample(iframe)
146 assert len(oframes) == 1
147
148 oframe = oframes[0]
149 assert oframe.pts == 925
150 assert oframe.time_base == Fraction(1, 44100)
151 assert oframe.sample_rate == 44100
152 assert oframe.samples == 941
153
154 # flush
155 oframes = resampler.resample(None)
156 assert len(oframes) == 1
157
158 oframe = oframes[0]
159 assert oframe.pts == 941 + 925
160 assert oframe.time_base == Fraction(1, 44100)
161 assert oframe.sample_rate == 44100
162 assert oframe.samples == 15
163
164
165 def test_pts_assertion_new_rate_down() -> None:
166 resampler = AudioResampler("s16", "mono", 48000)
167
168 # resample one frame
169 iframe = AudioFrame("s16", "stereo", 1024)
170 iframe.sample_rate = 44100
171 iframe.time_base = Fraction(1, 44100)
172 iframe.pts = 0
173
174 oframes = resampler.resample(iframe)
175 assert len(oframes) == 1
176
177 oframe = oframes[0]
178 assert oframe.pts == 0
179 assert oframe.time_base == Fraction(1, 48000)
180 assert oframe.sample_rate == 48000
181 assert oframe.samples == 1098
182
183 iframe = AudioFrame("s16", "stereo", 1024)
184 iframe.sample_rate = 44100
185 iframe.time_base = Fraction(1, 44100)
186 iframe.pts = 1024
187
188 oframes = resampler.resample(iframe)
189 assert len(oframes) == 1
190
191 oframe = oframes[0]
192 assert oframe.pts == 1098
193 assert oframe.time_base == Fraction(1, 48000)
194 assert oframe.sample_rate == 48000
195 assert oframe.samples == 1114
196
197 # flush
198 oframes = resampler.resample(None)
199 assert len(oframes) == 1
200
201 oframe = oframes[0]
202 assert oframe.pts == 1114 + 1098
203 assert oframe.time_base == Fraction(1, 48000)
204 assert oframe.sample_rate == 48000
205 assert oframe.samples == 18
206
207
208 def test_pts_assertion_new_rate_fltp() -> None:
209 resampler = AudioResampler("fltp", "mono", 8000, 1024)
210
211 # resample one frame
212 iframe = AudioFrame("s16", "mono", 1024)
213 iframe.sample_rate = 8000
214 iframe.time_base = Fraction(1, 1000)
215 iframe.pts = 0
216
217 oframes = resampler.resample(iframe)
218 assert len(oframes) == 1
219
220 oframe = oframes[0]
221 assert oframe.pts == 0
222 assert oframe.time_base == Fraction(1, 8000)
223 assert oframe.sample_rate == 8000
224 assert oframe.samples == 1024
225
226 iframe = AudioFrame("s16", "mono", 1024)
227 iframe.sample_rate = 8000
228 iframe.time_base = Fraction(1, 1000)
229 iframe.pts = 8192
230
231 oframes = resampler.resample(iframe)
232 assert len(oframes) == 1
233
234 oframe = oframes[0]
235 assert oframe.pts == 65536
236 assert oframe.time_base == Fraction(1, 8000)
237 assert oframe.sample_rate == 8000
238 assert oframe.samples == 1024
239
240 # flush
241 oframes = resampler.resample(None)
242 assert len(oframes) == 0
243
244
245 def test_pts_missing_time_base() -> None:
246 resampler = AudioResampler("s16", "mono", 44100)
247
248 # resample one frame
249 iframe = AudioFrame("s16", "stereo", 1024)
250 iframe.sample_rate = 48000
251 iframe.pts = 0
252
253 oframes = resampler.resample(iframe)
254 assert len(oframes) == 1
255
256 oframe = oframes[0]
257 assert oframe.pts == 0
258 assert oframe.time_base == Fraction(1, 44100)
259 assert oframe.sample_rate == 44100
260
261 # flush
262 oframes = resampler.resample(None)
263 assert len(oframes) == 1
264
265 oframe = oframes[0]
266 assert oframe.pts == 925
267 assert oframe.time_base == Fraction(1, 44100)
268 assert oframe.sample_rate == 44100
269 assert oframe.samples == 16
270
271
272 def test_mismatched_input() -> None:
273 """
274 Consecutive frames must have the same layout, sample format and sample rate.
275 """
276 resampler = AudioResampler("s16", "mono", 44100)
277
278 # resample one frame
279 iframe = AudioFrame("s16", "stereo", 1024)
280 iframe.sample_rate = 48000
281 resampler.resample(iframe)
282
283 # resample another frame with a sample format
284 iframe = AudioFrame("s16", "mono", 1024)
285 iframe.sample_rate = 48000
286 with pytest.raises(
287 ValueError, match="Frame does not match AudioResampler setup."
288 ) as cm:
289 resampler.resample(iframe)