File: test_audiofifo.py

package info (click to toggle)
python-av 16.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,684 kB
  • sloc: python: 7,607; sh: 182; ansic: 174; makefile: 135
file content (117 lines) | stat: -rw-r--r-- 3,240 bytes parent folder | download | duplicates (2)
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
from fractions import Fraction

import av

from .common import TestCase, fate_suite


class TestAudioFifo(TestCase):
    def test_data(self) -> None:
        container = av.open(fate_suite("audio-reference/chorusnoise_2ch_44kHz_s16.wav"))
        stream = container.streams.audio[0]

        fifo = av.AudioFifo()

        input_ = []
        output = []

        for i, frame in enumerate(container.decode(stream)):
            input_.append(bytes(frame.planes[0]))
            fifo.write(frame)
            for frame in fifo.read_many(512, partial=i == 10):
                output.append(bytes(frame.planes[0]))
            if i == 10:
                break

        input_bytes = b"".join(input_)
        output_bytes = b"".join(output)
        min_len = min(len(input_bytes), len(output_bytes))

        assert min_len > 10 * 512 * 2 * 2
        assert input_bytes[:min_len] == output_bytes[:min_len]

    def test_pts_simple(self) -> None:
        fifo = av.AudioFifo()

        assert str(fifo).startswith(
            "<av.AudioFifo uninitialized, use fifo.write(frame), at 0x"
        )

        iframe = av.AudioFrame(samples=1024)
        iframe.pts = 0
        iframe.sample_rate = 48000
        iframe.time_base = Fraction("1/48000")

        fifo.write(iframe)

        assert str(fifo).startswith(
            "<av.AudioFifo 1024 samples of 48000hz <av.AudioLayout 'stereo'> <av.AudioFormat s16> at 0x"
        )

        oframe = fifo.read(512)
        assert oframe is not None
        assert oframe.pts == 0
        assert oframe.time_base == iframe.time_base

        assert fifo.samples_written == 1024
        assert fifo.samples_read == 512
        assert fifo.pts_per_sample == 1.0

        iframe.pts = 1024
        fifo.write(iframe)
        oframe = fifo.read(512)
        assert oframe is not None

        assert oframe.pts == 512
        assert oframe.time_base == iframe.time_base

        iframe.pts = 9999  # Wrong!
        self.assertRaises(ValueError, fifo.write, iframe)

    def test_pts_complex(self) -> None:
        fifo = av.AudioFifo()

        iframe = av.AudioFrame(samples=1024)
        iframe.pts = 0
        iframe.sample_rate = 48000
        iframe.time_base = Fraction("1/96000")

        fifo.write(iframe)
        iframe.pts = 2048
        fifo.write(iframe)

        oframe = fifo.read_many(1024)[-1]

        assert oframe.pts == 2048
        assert fifo.pts_per_sample == 2.0

    def test_missing_sample_rate(self) -> None:
        fifo = av.AudioFifo()

        iframe = av.AudioFrame(samples=1024)
        iframe.pts = 0
        iframe.time_base = Fraction("1/48000")

        fifo.write(iframe)

        oframe = fifo.read(512)

        assert oframe is not None
        assert oframe.pts is None
        assert oframe.sample_rate == 0
        assert oframe.time_base == iframe.time_base

    def test_missing_time_base(self) -> None:
        fifo = av.AudioFifo()

        iframe = av.AudioFrame(samples=1024)
        iframe.pts = 0
        iframe.sample_rate = 48000

        fifo.write(iframe)

        oframe = fifo.read(512)

        assert oframe is not None
        assert oframe.pts is None and oframe.time_base is None
        assert oframe.sample_rate == iframe.sample_rate