File: decoder.py

package info (click to toggle)
python-opuslib 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: python: 1,201; makefile: 47
file content (260 lines) | stat: -rw-r--r-- 8,305 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=missing-docstring
#

import sys
import unittest

import opuslib.api
import opuslib.api.decoder
import opuslib.api.ctl

__author__ = 'Никита Кузнецов <self@svartalf.info>'
__copyright__ = 'Copyright (c) 2012, SvartalF'
__license__ = 'BSD 3-Clause License'


class DecoderTest(unittest.TestCase):
    """Decoder basic API tests

    From the `tests/test_opus_api.c`
    """

    def test_get_size(self):
        """Invalid configurations which should fail"""

        for csx in range(4):
            ixx = opuslib.api.decoder.libopus_get_size(csx)
            if csx in (1, 2):
                self.assertFalse(1 << 16 < ixx <= 2048)
            else:
                self.assertEqual(ixx, 0)

    def _test_unsupported_sample_rates(self):
        """
        Unsupported sample rates

        TODO: make the same test with a opus_decoder_init() function
        """
        for csx in range(4):
            for ixx in range(-7, 96000):
                if ixx in (8000, 12000, 16000, 24000, 48000) and csx in (1, 2):
                    continue

                if ixx == -5:
                    fsx = -8000
                elif ixx == -6:
                    fsx = sys.maxsize  # TODO: should be a INT32_MAX
                elif ixx == -7:
                    fsx = -1 * (sys.maxsize - 1)  # Emulation of the INT32_MIN
                else:
                    fsx = ixx

                try:
                    dec = opuslib.api.decoder.create_state(fsx, csx)
                except opuslib.OpusError as exc:
                    self.assertEqual(exc.code, opuslib.BAD_ARG)
                else:
                    opuslib.api.decoder.destroy(dec)

    @classmethod
    def test_create(cls):
        try:
            dec = opuslib.api.decoder.create_state(48000, 2)
        except opuslib.OpusError:
            raise AssertionError()
        else:
            opuslib.api.decoder.destroy(dec)

            # TODO: rewrite this code
        # VG_CHECK(dec,opus_decoder_get_size(2));

    @classmethod
    def test_get_final_range(cls):
        dec = opuslib.api.decoder.create_state(48000, 2)
        opuslib.api.decoder.decoder_ctl(dec, opuslib.api.ctl.get_final_range)
        opuslib.api.decoder.destroy(dec)

    def test_unimplemented(self):
        dec = opuslib.api.decoder.create_state(48000, 2)

        try:
            opuslib.api.decoder.decoder_ctl(
                dec, opuslib.api.ctl.unimplemented)
        except opuslib.OpusError as exc:
            self.assertEqual(exc.code, opuslib.UNIMPLEMENTED)

        opuslib.api.decoder.destroy(dec)

    def test_get_bandwidth(self):
        dec = opuslib.api.decoder.create_state(48000, 2)
        value = opuslib.api.decoder.decoder_ctl(
            dec, opuslib.api.ctl.get_bandwidth)
        self.assertEqual(value, 0)
        opuslib.api.decoder.destroy(dec)

    def test_get_pitch(self):
        dec = opuslib.api.decoder.create_state(48000, 2)

        i = opuslib.api.decoder.decoder_ctl(dec, opuslib.api.ctl.get_pitch)
        self.assertIn(i, (-1, 0))

        packet = bytes([252, 0, 0])
        opuslib.api.decoder.decode(dec, packet, 3, 960, False)
        i = opuslib.api.decoder.decoder_ctl(dec, opuslib.api.ctl.get_pitch)
        self.assertIn(i, (-1, 0))

        packet = bytes([1, 0, 0])
        opuslib.api.decoder.decode(dec, packet, 3, 960, False)
        i = opuslib.api.decoder.decoder_ctl(dec, opuslib.api.ctl.get_pitch)
        self.assertIn(i, (-1, 0))

        opuslib.api.decoder.destroy(dec)

    def test_gain(self):
        dec = opuslib.api.decoder.create_state(48000, 2)

        i = opuslib.api.decoder.decoder_ctl(dec, opuslib.api.ctl.get_gain)
        self.assertEqual(i, 0)

        try:
            opuslib.api.decoder.decoder_ctl(
                dec, opuslib.api.ctl.set_gain, -32769)
        except opuslib.OpusError as exc:
            self.assertEqual(exc.code, opuslib.BAD_ARG)

        try:
            opuslib.api.decoder.decoder_ctl(
                dec, opuslib.api.ctl.set_gain, 32768)
        except opuslib.OpusError as exc:
            self.assertEqual(exc.code, opuslib.BAD_ARG)

        opuslib.api.decoder.decoder_ctl(dec, opuslib.api.ctl.set_gain, -15)
        i = opuslib.api.decoder.decoder_ctl(dec, opuslib.api.ctl.get_gain)
        self.assertEqual(i, -15)

        opuslib.api.decoder.destroy(dec)

    @classmethod
    def test_reset_state(cls):
        dec = opuslib.api.decoder.create_state(48000, 2)
        opuslib.api.decoder.decoder_ctl(dec, opuslib.api.ctl.reset_state)
        opuslib.api.decoder.destroy(dec)

    def test_get_nb_samples(self):
        """opus_decoder_get_nb_samples()"""

        dec = opuslib.api.decoder.create_state(48000, 2)

        self.assertEqual(
            480, opuslib.api.decoder.get_nb_samples(dec, bytes([0]), 1))

        packet = bytes()
        for xxc in ((63 << 2) | 3, 63):
            packet += bytes([xxc])

        # TODO: check for exception code
        self.assertRaises(
            opuslib.OpusError,
            lambda: opuslib.api.decoder.get_nb_samples(dec, packet, 2)
        )

        opuslib.api.decoder.destroy(dec)

    def test_packet_get_nb_frames(self):
        """opus_packet_get_nb_frames()"""

        packet = bytes()
        for xxc in ((63 << 2) | 3, 63):
            packet += bytes([xxc])

        self.assertRaises(
            opuslib.OpusError,
            lambda: opuslib.api.decoder.packet_get_nb_frames(packet, 0)
        )

        l1res = (1, 2, 2, opuslib.INVALID_PACKET)

        for ixc in range(0, 256):
            packet = bytes([ixc])
            expected_result = l1res[ixc & 3]

            try:
                self.assertEqual(
                    expected_result,
                    opuslib.api.decoder.packet_get_nb_frames(packet, 1)
                )
            except opuslib.OpusError as exc:
                if exc.code == expected_result:
                    continue

            for jxc in range(0, 256):
                packet = bytes([ixc, jxc])

                self.assertEqual(
                    expected_result if expected_result != 3 else (packet[1] & 63),  # NOQA
                    opuslib.api.decoder.packet_get_nb_frames(packet, 2)
                )

    def test_packet_get_bandwidth(self):
        """Tests `opuslib.api.decoder.opus_packet_get_bandwidth()`"""

        for ixc in range(0, 256):
            packet = bytes([ixc])
            bwx = ixc >> 4

            # Very cozy code from the test_opus_api.c
            _bwx = opuslib.BANDWIDTH_NARROWBAND + (((((bwx & 7) * 9) & (63 - (bwx & 8))) + 2 + 12 * ((bwx & 8) != 0)) >> 4)  # NOQA pylint: disable=line-too-long

            self.assertEqual(
                _bwx, opuslib.api.decoder.packet_get_bandwidth(packet)
            )

    def test_decode(self):
        """opus_decode()"""

        packet = bytes([255, 49])
        for _ in range(2, 51):
            packet += bytes([0])

        dec = opuslib.api.decoder.create_state(48000, 2)
        try:
            opuslib.api.decoder.decode(dec, packet, 51, 960, 0)
        except opuslib.OpusError as exc:
            self.assertEqual(exc.code, opuslib.INVALID_PACKET)

        packet = bytes([252, 0, 0])
        try:
            opuslib.api.decoder.decode(dec, packet, -1, 960, 0)
        except opuslib.OpusError as exc:
            self.assertEqual(exc.code, opuslib.BAD_ARG)

        try:
            opuslib.api.decoder.decode(dec, packet, 3, 60, 0)
        except opuslib.OpusError as exc:
            self.assertEqual(exc.code, opuslib.BUFFER_TOO_SMALL)

        try:
            opuslib.api.decoder.decode(dec, packet, 3, 480, 0)
        except opuslib.OpusError as exc:
            self.assertEqual(exc.code, opuslib.BUFFER_TOO_SMALL)

        try:
            opuslib.api.decoder.decode(dec, packet, 3, 960, 0)
        except opuslib.OpusError:
            self.fail('Decode failed')

        opuslib.api.decoder.destroy(dec)

    def test_decode_float(self):
        dec = opuslib.api.decoder.create_state(48000, 2)

        packet = bytes([252, 0, 0])

        try:
            opuslib.api.decoder.decode_float(dec, packet, 3, 960, 0)
        except opuslib.OpusError:
            self.fail('Decode failed')

        opuslib.api.decoder.destroy(dec)