| 1 |
from fractions import Fraction |
| 2 |
|
| 3 |
import av |
| 4 |
|
| 5 |
from .common import TestCase, fate_suite |
| 6 |
|
| 7 |
|
| 8 |
class TestAudioFifo(TestCase): |
| 9 |
def test_data(self) -> None: |
| 10 |
container = av.open(fate_suite("audio-reference/chorusnoise_2ch_44kHz_s16.wav")) |
| 11 |
stream = container.streams.audio[0] |
| 12 |
|
| 13 |
fifo = av.AudioFifo() |
| 14 |
|
| 15 |
input_ = [] |
| 16 |
output = [] |
| 17 |
|
| 18 |
for i, frame in enumerate(container.decode(stream)): |
| 19 |
input_.append(bytes(frame.planes[0])) |
| 20 |
fifo.write(frame) |
| 21 |
for frame in fifo.read_many(512, partial=i == 10): |
| 22 |
output.append(bytes(frame.planes[0])) |
| 23 |
if i == 10: |
| 24 |
break |
| 25 |
|
| 26 |
input_bytes = b"".join(input_) |
| 27 |
output_bytes = b"".join(output) |
| 28 |
min_len = min(len(input_bytes), len(output_bytes)) |
| 29 |
|
| 30 |
assert min_len > 10 * 512 * 2 * 2 |
| 31 |
assert input_bytes[:min_len] == output_bytes[:min_len] |
| 32 |
|
| 33 |
def test_pts_simple(self) -> None: |
| 34 |
fifo = av.AudioFifo() |
| 35 |
|
| 36 |
assert str(fifo).startswith( |
| 37 |
"<av.AudioFifo uninitialized, use fifo.write(frame), at 0x" |
| 38 |
) |
| 39 |
|
| 40 |
iframe = av.AudioFrame(samples=1024) |
| 41 |
iframe.pts = 0 |
| 42 |
iframe.sample_rate = 48000 |
| 43 |
iframe.time_base = Fraction("1/48000") |
| 44 |
|
| 45 |
fifo.write(iframe) |
| 46 |
|
| 47 |
assert str(fifo).startswith( |
| 48 |
"<av.AudioFifo 1024 samples of 48000hz <av.AudioLayout 'stereo'> <av.AudioFormat s16> at 0x" |
| 49 |
) |
| 50 |
|
| 51 |
oframe = fifo.read(512) |
| 52 |
assert oframe is not None |
| 53 |
assert oframe.pts == 0 |
| 54 |
assert oframe.time_base == iframe.time_base |
| 55 |
|
| 56 |
assert fifo.samples_written == 1024 |
| 57 |
assert fifo.samples_read == 512 |
| 58 |
assert fifo.pts_per_sample == 1.0 |
| 59 |
|
| 60 |
iframe.pts = 1024 |
| 61 |
fifo.write(iframe) |
| 62 |
oframe = fifo.read(512) |
| 63 |
assert oframe is not None |
| 64 |
|
| 65 |
assert oframe.pts == 512 |
| 66 |
assert oframe.time_base == iframe.time_base |
| 67 |
|
| 68 |
iframe.pts = 9999 # Wrong! |
| 69 |
self.assertRaises(ValueError, fifo.write, iframe) |
| 70 |
|
| 71 |
def test_pts_complex(self) -> None: |
| 72 |
fifo = av.AudioFifo() |
| 73 |
|
| 74 |
iframe = av.AudioFrame(samples=1024) |
| 75 |
iframe.pts = 0 |
| 76 |
iframe.sample_rate = 48000 |
| 77 |
iframe.time_base = Fraction("1/96000") |
| 78 |
|
| 79 |
fifo.write(iframe) |
| 80 |
iframe.pts = 2048 |
| 81 |
fifo.write(iframe) |
| 82 |
|
| 83 |
oframe = fifo.read_many(1024)[-1] |
| 84 |
|
| 85 |
assert oframe.pts == 2048 |
| 86 |
assert fifo.pts_per_sample == 2.0 |
| 87 |
|
| 88 |
def test_missing_sample_rate(self) -> None: |
| 89 |
fifo = av.AudioFifo() |
| 90 |
|
| 91 |
iframe = av.AudioFrame(samples=1024) |
| 92 |
iframe.pts = 0 |
| 93 |
iframe.time_base = Fraction("1/48000") |
| 94 |
|
| 95 |
fifo.write(iframe) |
| 96 |
|
| 97 |
oframe = fifo.read(512) |
| 98 |
|
| 99 |
assert oframe is not None |
| 100 |
assert oframe.pts is None |
| 101 |
assert oframe.sample_rate == 0 |
| 102 |
assert oframe.time_base == iframe.time_base |
| 103 |
|
| 104 |
def test_missing_time_base(self) -> None: |
| 105 |
fifo = av.AudioFifo() |
| 106 |
|
| 107 |
iframe = av.AudioFrame(samples=1024) |
| 108 |
iframe.pts = 0 |
| 109 |
iframe.sample_rate = 48000 |
| 110 |
|
| 111 |
fifo.write(iframe) |
| 112 |
|
| 113 |
oframe = fifo.read(512) |
| 114 |
|
| 115 |
assert oframe is not None |
| 116 |
assert oframe.pts is None and oframe.time_base is None |
| 117 |
assert oframe.sample_rate == iframe.sample_rate |