opuslib-next 1.2.0__tar.gz → 1.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- opuslib_next-1.3.1/.gitignore +9 -0
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/PKG-INFO +2 -2
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/README.md +1 -1
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/benchmarks/compare_versions.py +0 -2
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/benchmarks/render_summary.py +0 -2
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/opuslib_next/__init__.py +5 -12
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/opuslib_next/api/__init__.py +2 -11
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/opuslib_next/api/ctl.py +39 -15
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/opuslib_next/api/decoder.py +4 -13
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/opuslib_next/api/encoder.py +2 -11
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/opuslib_next/api/info.py +1 -10
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/opuslib_next/api/multistream_decoder.py +3 -12
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/opuslib_next/api/multistream_encoder.py +2 -11
- opuslib_next-1.3.1/opuslib_next/api/projection_decoder.py +223 -0
- opuslib_next-1.3.1/opuslib_next/api/projection_encoder.py +208 -0
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/opuslib_next/classes.py +423 -7
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/opuslib_next/constants.py +5 -7
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/opuslib_next/exceptions.py +0 -7
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/pyproject.toml +2 -6
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/tests/decoder.py +3 -15
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/tests/encoder.py +1 -19
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/tests/hl_decoder.py +0 -9
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/tests/hl_encoder.py +0 -9
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/tests/hl_multistream.py +1 -6
- opuslib_next-1.3.1/tests/hl_projection.py +116 -0
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/tests/multistream.py +1 -6
- opuslib_next-1.3.1/tests/projection.py +165 -0
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/tests/test_exceptions.py +0 -3
- opuslib_next-1.3.1/uv.lock +156 -0
- opuslib_next-1.2.0/.gitignore +0 -34
- opuslib_next-1.2.0/Makefile +0 -19
- opuslib_next-1.2.0/tests/__init__.py +0 -8
- opuslib_next-1.2.0/uv.lock +0 -695
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/.github/workflows/ci.yml +0 -0
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/.github/workflows/performance.yml +0 -0
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/.github/workflows/release.yml +0 -0
- {opuslib_next-1.2.0 → opuslib_next-1.3.1}/LICENSE +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: opuslib-next
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.1
|
|
4
4
|
Summary: Python bindings to the libopus, IETF low-delay audio codec
|
|
5
5
|
Project-URL: Homepage, https://github.com/kalicyh/opuslib-next
|
|
6
6
|
Project-URL: Documentation, https://github.com/kalicyh/opuslib-next
|
|
@@ -25,7 +25,7 @@ Classifier: Topic :: Software Development :: Libraries
|
|
|
25
25
|
Requires-Python: >=3.10
|
|
26
26
|
Description-Content-Type: text/markdown
|
|
27
27
|
|
|
28
|
-
#
|
|
28
|
+
# opuslib-next
|
|
29
29
|
|
|
30
30
|
Python bindings to the libopus, IETF low-delay audio codec.
|
|
31
31
|
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
|
|
4
1
|
# OpusLib Python Module.
|
|
5
2
|
|
|
6
3
|
"""
|
|
@@ -16,19 +13,15 @@ Python bindings to the libopus, IETF low-delay audio codec
|
|
|
16
13
|
|
|
17
14
|
"""
|
|
18
15
|
|
|
19
|
-
from .exceptions import OpusError
|
|
20
|
-
|
|
21
|
-
from .constants import * # NOQA
|
|
16
|
+
from .exceptions import OpusError
|
|
22
17
|
|
|
23
|
-
from .constants import
|
|
18
|
+
from .constants import *
|
|
24
19
|
|
|
25
|
-
from .classes import (
|
|
20
|
+
from .classes import (
|
|
26
21
|
Decoder,
|
|
27
22
|
Encoder,
|
|
28
23
|
MultiStreamDecoder,
|
|
29
24
|
MultiStreamEncoder,
|
|
25
|
+
ProjectionDecoder,
|
|
26
|
+
ProjectionEncoder,
|
|
30
27
|
)
|
|
31
|
-
|
|
32
|
-
__author__ = 'kalicyh <kalicyh@qq.com>'
|
|
33
|
-
__copyright__ = 'Copyright (c) 2025, Kalicyh'
|
|
34
|
-
__license__ = 'BSD 3-Clause License'
|
|
@@ -1,18 +1,9 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
# pylint: disable=invalid-name
|
|
4
|
-
#
|
|
5
|
-
|
|
6
1
|
"""OpusLib Package."""
|
|
7
2
|
|
|
8
|
-
import ctypes
|
|
3
|
+
import ctypes
|
|
9
4
|
import os
|
|
10
5
|
import platform
|
|
11
|
-
from ctypes.util import find_library
|
|
12
|
-
|
|
13
|
-
__author__ = "kalicyh <kalicyh@qq.com>"
|
|
14
|
-
__copyright__ = "Copyright (c) 2025, Kalicyh"
|
|
15
|
-
__license__ = "BSD 3-Clause License"
|
|
6
|
+
from ctypes.util import find_library
|
|
16
7
|
|
|
17
8
|
|
|
18
9
|
lib_location = find_library("opus")
|
|
@@ -1,25 +1,17 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
# pylint: disable=invalid-name
|
|
4
|
-
|
|
5
1
|
"""CTL macros rewritten to Python
|
|
6
2
|
|
|
7
3
|
Usage example:
|
|
8
4
|
|
|
9
|
-
>>> import
|
|
10
|
-
>>> import
|
|
11
|
-
>>> dec =
|
|
12
|
-
>>>
|
|
13
|
-
>>> gain_value =
|
|
5
|
+
>>> import opuslib_next.api.decoder
|
|
6
|
+
>>> import opuslib_next.api.ctl
|
|
7
|
+
>>> dec = opuslib_next.api.decoder.create_state(48000, 2)
|
|
8
|
+
>>> opuslib_next.api.decoder.decoder_ctl(dec, opuslib_next.api.ctl.set_gain, -15)
|
|
9
|
+
>>> gain_value = opuslib_next.api.decoder.decoder_ctl(dec, opuslib_next.api.ctl.get_gain)
|
|
14
10
|
|
|
15
11
|
"""
|
|
16
12
|
|
|
17
|
-
__author__ = 'kalicyh <kalicyh@qq.com>'
|
|
18
|
-
__copyright__ = 'Copyright (c) 2025, Kalicyh'
|
|
19
|
-
__license__ = 'BSD 3-Clause License'
|
|
20
|
-
|
|
21
13
|
|
|
22
|
-
import ctypes
|
|
14
|
+
import ctypes
|
|
23
15
|
|
|
24
16
|
import opuslib_next.api
|
|
25
17
|
import opuslib_next.exceptions
|
|
@@ -72,7 +64,7 @@ def ctl_set(request):
|
|
|
72
64
|
#
|
|
73
65
|
|
|
74
66
|
# Resets the codec state to be equivalent to a freshly initialized state
|
|
75
|
-
reset_state = query(opuslib_next.RESET_STATE)
|
|
67
|
+
reset_state = query(opuslib_next.RESET_STATE)
|
|
76
68
|
|
|
77
69
|
# Gets the final state of the codec's entropy coder
|
|
78
70
|
get_final_range = get(
|
|
@@ -192,6 +184,38 @@ set_dtx = ctl_set(opuslib_next.SET_DTX_REQUEST)
|
|
|
192
184
|
# Gets encoder's configured use of discontinuous transmission
|
|
193
185
|
get_dtx = get(opuslib_next.GET_DTX_REQUEST, ctypes.c_int)
|
|
194
186
|
|
|
187
|
+
#
|
|
188
|
+
# Projection related CTLs
|
|
189
|
+
#
|
|
190
|
+
|
|
191
|
+
# Gets the gain of the demixing matrix from the encoder
|
|
192
|
+
get_demixing_matrix_gain = get(
|
|
193
|
+
opuslib_next.PROJECTION_GET_DEMIXING_MATRIX_GAIN_REQUEST,
|
|
194
|
+
ctypes.c_int
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
# Gets the size in bytes of the demixing matrix from the encoder
|
|
198
|
+
get_demixing_matrix_size = get(
|
|
199
|
+
opuslib_next.PROJECTION_GET_DEMIXING_MATRIX_SIZE_REQUEST,
|
|
200
|
+
ctypes.c_int
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def get_demixing_matrix(func, obj, size):
|
|
205
|
+
"""Gets the demixing matrix from a projection encoder."""
|
|
206
|
+
result = (ctypes.c_ubyte * size)()
|
|
207
|
+
result_code = func(
|
|
208
|
+
obj,
|
|
209
|
+
opuslib_next.PROJECTION_GET_DEMIXING_MATRIX_REQUEST,
|
|
210
|
+
result,
|
|
211
|
+
size
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
if result_code != opuslib_next.OK:
|
|
215
|
+
raise opuslib_next.exceptions.OpusError(result_code)
|
|
216
|
+
|
|
217
|
+
return bytes(result)
|
|
218
|
+
|
|
195
219
|
#
|
|
196
220
|
# Other stuff
|
|
197
221
|
#
|
|
@@ -1,23 +1,14 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
# pylint: disable=invalid-name,too-few-public-methods
|
|
4
|
-
#
|
|
5
|
-
|
|
6
1
|
"""
|
|
7
2
|
CTypes mapping between libopus functions and Python.
|
|
8
3
|
"""
|
|
9
4
|
|
|
10
5
|
import array
|
|
11
|
-
import ctypes
|
|
6
|
+
import ctypes
|
|
12
7
|
import typing
|
|
13
8
|
|
|
14
9
|
import opuslib_next
|
|
15
10
|
import opuslib_next.api
|
|
16
11
|
|
|
17
|
-
__author__ = 'kalicyh <kalicyh@qq.com>'
|
|
18
|
-
__copyright__ = 'Copyright (c) 2025, Kalicyh'
|
|
19
|
-
__license__ = 'BSD 3-Clause License'
|
|
20
|
-
|
|
21
12
|
|
|
22
13
|
class Decoder(ctypes.Structure):
|
|
23
14
|
"""Opus decoder state.
|
|
@@ -95,7 +86,7 @@ def packet_get_bandwidth(data: bytes) -> typing.Union[int, typing.Any]:
|
|
|
95
86
|
return result
|
|
96
87
|
|
|
97
88
|
|
|
98
|
-
libopus_packet_get_nb_channels = opuslib_next.api.libopus.opus_packet_get_nb_channels
|
|
89
|
+
libopus_packet_get_nb_channels = opuslib_next.api.libopus.opus_packet_get_nb_channels
|
|
99
90
|
# `argtypes` must be a sequence (,) of types!
|
|
100
91
|
libopus_packet_get_nb_channels.argtypes = (ctypes.c_char_p,)
|
|
101
92
|
libopus_packet_get_nb_channels.restype = ctypes.c_int
|
|
@@ -215,7 +206,7 @@ libopus_decode.restype = ctypes.c_int
|
|
|
215
206
|
|
|
216
207
|
|
|
217
208
|
# FIXME: Remove typing.Any once we have a stub for ctypes
|
|
218
|
-
def decode(
|
|
209
|
+
def decode(
|
|
219
210
|
decoder_state: ctypes.Structure,
|
|
220
211
|
opus_data: bytes,
|
|
221
212
|
length: int,
|
|
@@ -264,7 +255,7 @@ libopus_decode_float.restype = ctypes.c_int
|
|
|
264
255
|
|
|
265
256
|
|
|
266
257
|
# FIXME: Remove typing.Any once we have a stub for ctypes
|
|
267
|
-
def decode_float(
|
|
258
|
+
def decode_float(
|
|
268
259
|
decoder_state: ctypes.Structure,
|
|
269
260
|
opus_data: bytes,
|
|
270
261
|
length: int,
|
|
@@ -1,25 +1,16 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
# pylint: disable=invalid-name
|
|
4
|
-
#
|
|
5
|
-
|
|
6
1
|
"""
|
|
7
2
|
CTypes mapping between libopus functions and Python.
|
|
8
3
|
"""
|
|
9
4
|
|
|
10
5
|
import array
|
|
11
|
-
import ctypes
|
|
6
|
+
import ctypes
|
|
12
7
|
import typing
|
|
13
8
|
|
|
14
9
|
import opuslib_next
|
|
15
10
|
import opuslib_next.api
|
|
16
11
|
|
|
17
|
-
__author__ = 'kalicyh <kalicyh@qq.com>'
|
|
18
|
-
__copyright__ = 'Copyright (c) 2025, Kalicyh'
|
|
19
|
-
__license__ = 'BSD 3-Clause License'
|
|
20
|
-
|
|
21
12
|
|
|
22
|
-
class Encoder(ctypes.Structure):
|
|
13
|
+
class Encoder(ctypes.Structure):
|
|
23
14
|
"""Opus encoder state.
|
|
24
15
|
This contains the complete state of an Opus encoder.
|
|
25
16
|
"""
|
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
# pylint: disable=invalid-name
|
|
4
|
-
#
|
|
5
|
-
|
|
6
|
-
import ctypes # type: ignore
|
|
1
|
+
import ctypes
|
|
7
2
|
|
|
8
3
|
import opuslib_next.api
|
|
9
4
|
|
|
10
|
-
__author__ = 'kalicyh <kalicyh@qq.com>'
|
|
11
|
-
__copyright__ = 'Copyright (c) 2025, Kalicyh'
|
|
12
|
-
__license__ = 'BSD 3-Clause License'
|
|
13
|
-
|
|
14
5
|
|
|
15
6
|
strerror = opuslib_next.api.libopus.opus_strerror
|
|
16
7
|
strerror.argtypes = (ctypes.c_int,) # must be sequence (,) of types!
|
|
@@ -1,24 +1,15 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
# pylint: disable=invalid-name,too-few-public-methods
|
|
4
|
-
#
|
|
5
|
-
|
|
6
1
|
"""
|
|
7
2
|
CTypes mapping between libopus multistream decoder functions and Python.
|
|
8
3
|
"""
|
|
9
4
|
|
|
10
5
|
import array
|
|
11
|
-
import ctypes
|
|
6
|
+
import ctypes
|
|
12
7
|
import functools
|
|
13
8
|
import typing
|
|
14
9
|
|
|
15
10
|
import opuslib_next
|
|
16
11
|
import opuslib_next.api
|
|
17
12
|
|
|
18
|
-
__author__ = 'kalicyh <kalicyh@qq.com>'
|
|
19
|
-
__copyright__ = 'Copyright (c) 2025, Kalicyh'
|
|
20
|
-
__license__ = 'BSD 3-Clause License'
|
|
21
|
-
|
|
22
13
|
|
|
23
14
|
class MultiStreamDecoder(ctypes.Structure):
|
|
24
15
|
"""Opus multistream decoder state."""
|
|
@@ -113,7 +104,7 @@ def _libopus_decode():
|
|
|
113
104
|
|
|
114
105
|
|
|
115
106
|
# FIXME: Remove typing.Any once we have a stub for ctypes
|
|
116
|
-
def decode(
|
|
107
|
+
def decode(
|
|
117
108
|
decoder_state: ctypes.Structure,
|
|
118
109
|
opus_data: bytes,
|
|
119
110
|
length: int,
|
|
@@ -158,7 +149,7 @@ def _libopus_decode_float():
|
|
|
158
149
|
|
|
159
150
|
|
|
160
151
|
# FIXME: Remove typing.Any once we have a stub for ctypes
|
|
161
|
-
def decode_float(
|
|
152
|
+
def decode_float(
|
|
162
153
|
decoder_state: ctypes.Structure,
|
|
163
154
|
opus_data: bytes,
|
|
164
155
|
length: int,
|
|
@@ -1,26 +1,17 @@
|
|
|
1
|
-
#!/usr/bin/env python
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
# pylint: disable=invalid-name
|
|
4
|
-
#
|
|
5
|
-
|
|
6
1
|
"""
|
|
7
2
|
CTypes mapping between libopus multistream encoder functions and Python.
|
|
8
3
|
"""
|
|
9
4
|
|
|
10
5
|
import array
|
|
11
|
-
import ctypes
|
|
6
|
+
import ctypes
|
|
12
7
|
import functools
|
|
13
8
|
import typing
|
|
14
9
|
|
|
15
10
|
import opuslib_next
|
|
16
11
|
import opuslib_next.api
|
|
17
12
|
|
|
18
|
-
__author__ = 'kalicyh <kalicyh@qq.com>'
|
|
19
|
-
__copyright__ = 'Copyright (c) 2025, Kalicyh'
|
|
20
|
-
__license__ = 'BSD 3-Clause License'
|
|
21
|
-
|
|
22
13
|
|
|
23
|
-
class MultiStreamEncoder(ctypes.Structure):
|
|
14
|
+
class MultiStreamEncoder(ctypes.Structure):
|
|
24
15
|
"""Opus multistream encoder state."""
|
|
25
16
|
pass
|
|
26
17
|
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CTypes mapping between libopus projection decoder functions and Python.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import array
|
|
6
|
+
import ctypes
|
|
7
|
+
import functools
|
|
8
|
+
import typing
|
|
9
|
+
|
|
10
|
+
import opuslib_next
|
|
11
|
+
import opuslib_next.api
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ProjectionDecoder(ctypes.Structure):
|
|
15
|
+
"""Opus projection decoder state."""
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
ProjectionDecoderPointer = ctypes.POINTER(ProjectionDecoder)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _require_function(name, argtypes, restype):
|
|
23
|
+
try:
|
|
24
|
+
func = getattr(opuslib_next.api.libopus, name)
|
|
25
|
+
except AttributeError as exc:
|
|
26
|
+
raise opuslib_next.OpusError(opuslib_next.UNIMPLEMENTED) from exc
|
|
27
|
+
|
|
28
|
+
func.argtypes = argtypes
|
|
29
|
+
func.restype = restype
|
|
30
|
+
return func
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@functools.lru_cache(maxsize=None)
|
|
34
|
+
def _libopus_get_size():
|
|
35
|
+
return _require_function(
|
|
36
|
+
'opus_projection_decoder_get_size',
|
|
37
|
+
(ctypes.c_int, ctypes.c_int, ctypes.c_int),
|
|
38
|
+
ctypes.c_int32
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# FIXME: Remove typing.Any once we have a stub for ctypes
|
|
43
|
+
def get_size(
|
|
44
|
+
channels: int,
|
|
45
|
+
streams: int,
|
|
46
|
+
coupled_streams: int
|
|
47
|
+
) -> typing.Union[int, typing.Any]:
|
|
48
|
+
"""Gets the size of an OpusProjectionDecoder structure."""
|
|
49
|
+
result = _libopus_get_size()(channels, streams, coupled_streams)
|
|
50
|
+
|
|
51
|
+
if result < 0:
|
|
52
|
+
raise opuslib_next.OpusError(result)
|
|
53
|
+
|
|
54
|
+
return result
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@functools.lru_cache(maxsize=None)
|
|
58
|
+
def _libopus_create():
|
|
59
|
+
return _require_function(
|
|
60
|
+
'opus_projection_decoder_create',
|
|
61
|
+
(
|
|
62
|
+
ctypes.c_int32,
|
|
63
|
+
ctypes.c_int,
|
|
64
|
+
ctypes.c_int,
|
|
65
|
+
ctypes.c_int,
|
|
66
|
+
opuslib_next.api.c_ubyte_pointer,
|
|
67
|
+
ctypes.c_int32,
|
|
68
|
+
opuslib_next.api.c_int_pointer
|
|
69
|
+
),
|
|
70
|
+
ProjectionDecoderPointer
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def create_state(
|
|
75
|
+
fs: int,
|
|
76
|
+
channels: int,
|
|
77
|
+
streams: int,
|
|
78
|
+
coupled_streams: int,
|
|
79
|
+
demixing_matrix: typing.Sequence[int]
|
|
80
|
+
) -> ctypes.Structure:
|
|
81
|
+
"""Allocates and initializes a projection decoder state."""
|
|
82
|
+
result_code = ctypes.c_int()
|
|
83
|
+
demixing_matrix_array = \
|
|
84
|
+
(ctypes.c_ubyte * len(demixing_matrix))(*demixing_matrix)
|
|
85
|
+
|
|
86
|
+
decoder_state = _libopus_create()(
|
|
87
|
+
fs,
|
|
88
|
+
channels,
|
|
89
|
+
streams,
|
|
90
|
+
coupled_streams,
|
|
91
|
+
demixing_matrix_array,
|
|
92
|
+
len(demixing_matrix),
|
|
93
|
+
ctypes.byref(result_code)
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
if result_code.value != opuslib_next.OK:
|
|
97
|
+
raise opuslib_next.OpusError(result_code.value)
|
|
98
|
+
|
|
99
|
+
return decoder_state
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@functools.lru_cache(maxsize=None)
|
|
103
|
+
def _libopus_decode():
|
|
104
|
+
return _require_function(
|
|
105
|
+
'opus_projection_decode',
|
|
106
|
+
(
|
|
107
|
+
ProjectionDecoderPointer,
|
|
108
|
+
ctypes.c_char_p,
|
|
109
|
+
ctypes.c_int32,
|
|
110
|
+
opuslib_next.api.c_int16_pointer,
|
|
111
|
+
ctypes.c_int,
|
|
112
|
+
ctypes.c_int
|
|
113
|
+
),
|
|
114
|
+
ctypes.c_int
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# FIXME: Remove typing.Any once we have a stub for ctypes
|
|
119
|
+
def decode(
|
|
120
|
+
decoder_state: ctypes.Structure,
|
|
121
|
+
opus_data: bytes,
|
|
122
|
+
length: int,
|
|
123
|
+
frame_size: int,
|
|
124
|
+
decode_fec: bool,
|
|
125
|
+
channels: int = 2
|
|
126
|
+
) -> typing.Union[bytes, typing.Any]:
|
|
127
|
+
"""Decodes a projection Opus packet to signed 16-bit PCM."""
|
|
128
|
+
pcm_size = frame_size * channels
|
|
129
|
+
pcm = (ctypes.c_int16 * pcm_size)()
|
|
130
|
+
pcm_pointer = ctypes.cast(pcm, opuslib_next.api.c_int16_pointer)
|
|
131
|
+
|
|
132
|
+
result = _libopus_decode()(
|
|
133
|
+
decoder_state,
|
|
134
|
+
opus_data,
|
|
135
|
+
length,
|
|
136
|
+
pcm_pointer,
|
|
137
|
+
frame_size,
|
|
138
|
+
int(decode_fec)
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
if result < 0:
|
|
142
|
+
raise opuslib_next.OpusError(result)
|
|
143
|
+
|
|
144
|
+
return array.array('h', pcm_pointer[:result * channels]).tobytes()
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
@functools.lru_cache(maxsize=None)
|
|
148
|
+
def _libopus_decode_float():
|
|
149
|
+
return _require_function(
|
|
150
|
+
'opus_projection_decode_float',
|
|
151
|
+
(
|
|
152
|
+
ProjectionDecoderPointer,
|
|
153
|
+
ctypes.c_char_p,
|
|
154
|
+
ctypes.c_int32,
|
|
155
|
+
opuslib_next.api.c_float_pointer,
|
|
156
|
+
ctypes.c_int,
|
|
157
|
+
ctypes.c_int
|
|
158
|
+
),
|
|
159
|
+
ctypes.c_int
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
# FIXME: Remove typing.Any once we have a stub for ctypes
|
|
164
|
+
def decode_float(
|
|
165
|
+
decoder_state: ctypes.Structure,
|
|
166
|
+
opus_data: bytes,
|
|
167
|
+
length: int,
|
|
168
|
+
frame_size: int,
|
|
169
|
+
decode_fec: bool,
|
|
170
|
+
channels: int = 2
|
|
171
|
+
) -> typing.Union[bytes, typing.Any]:
|
|
172
|
+
"""Decodes a projection Opus packet to floating point PCM."""
|
|
173
|
+
pcm_size = frame_size * channels
|
|
174
|
+
pcm = (ctypes.c_float * pcm_size)()
|
|
175
|
+
pcm_pointer = ctypes.cast(pcm, opuslib_next.api.c_float_pointer)
|
|
176
|
+
|
|
177
|
+
result = _libopus_decode_float()(
|
|
178
|
+
decoder_state,
|
|
179
|
+
opus_data,
|
|
180
|
+
length,
|
|
181
|
+
pcm_pointer,
|
|
182
|
+
frame_size,
|
|
183
|
+
int(decode_fec)
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
if result < 0:
|
|
187
|
+
raise opuslib_next.OpusError(result)
|
|
188
|
+
|
|
189
|
+
return array.array('f', pcm[:result * channels]).tobytes()
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
@functools.lru_cache(maxsize=None)
|
|
193
|
+
def _libopus_ctl():
|
|
194
|
+
return _require_function(
|
|
195
|
+
'opus_projection_decoder_ctl',
|
|
196
|
+
(ProjectionDecoderPointer, ctypes.c_int),
|
|
197
|
+
ctypes.c_int
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
# FIXME: Remove typing.Any once we have a stub for ctypes
|
|
202
|
+
def decoder_ctl(
|
|
203
|
+
decoder_state: ctypes.Structure,
|
|
204
|
+
request,
|
|
205
|
+
value=None
|
|
206
|
+
) -> typing.Union[int, typing.Any]:
|
|
207
|
+
if value is not None:
|
|
208
|
+
return request(_libopus_ctl(), decoder_state, value)
|
|
209
|
+
return request(_libopus_ctl(), decoder_state)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
@functools.lru_cache(maxsize=None)
|
|
213
|
+
def _libopus_destroy():
|
|
214
|
+
return _require_function(
|
|
215
|
+
'opus_projection_decoder_destroy',
|
|
216
|
+
(ProjectionDecoderPointer,),
|
|
217
|
+
None
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def destroy(decoder_state: ctypes.Structure) -> None:
|
|
222
|
+
"""Frees a projection decoder allocated by create_state()."""
|
|
223
|
+
_libopus_destroy()(decoder_state)
|