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