opuscodec 0.1.2__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.
- opuscodec-0.1.2/LICENSE +162 -0
- opuscodec-0.1.2/PKG-INFO +198 -0
- opuscodec-0.1.2/README.md +166 -0
- opuscodec-0.1.2/opuscodec.egg-info/PKG-INFO +198 -0
- opuscodec-0.1.2/opuscodec.egg-info/SOURCES.txt +14 -0
- opuscodec-0.1.2/opuscodec.egg-info/dependency_links.txt +1 -0
- opuscodec-0.1.2/opuscodec.egg-info/not-zip-safe +1 -0
- opuscodec-0.1.2/opuscodec.egg-info/requires.txt +4 -0
- opuscodec-0.1.2/opuscodec.egg-info/top_level.txt +3 -0
- opuscodec-0.1.2/opusdec.py +5 -0
- opuscodec-0.1.2/opusenc.py +5 -0
- opuscodec-0.1.2/pyproject.toml +69 -0
- opuscodec-0.1.2/setup.cfg +4 -0
- opuscodec-0.1.2/setup.py +74 -0
- opuscodec-0.1.2/src/opuscodec_bindings.cpp +306 -0
- opuscodec-0.1.2/tests/test_bindings.py +83 -0
opuscodec-0.1.2/LICENSE
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the
|
|
13
|
+
copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other
|
|
16
|
+
entities that control, are controlled by, or are under common control with
|
|
17
|
+
that entity. For the purposes of this definition, "control" means (i) the
|
|
18
|
+
power, direct or indirect, to cause the direction or management of such
|
|
19
|
+
entity, whether by contract or otherwise, or (ii) ownership of fifty percent
|
|
20
|
+
(50%) or more of the outstanding shares, or (iii) beneficial ownership of
|
|
21
|
+
such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
24
|
+
permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation source, and
|
|
28
|
+
configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
31
|
+
translation of a Source form, including but not limited to compiled object
|
|
32
|
+
code, generated documentation, and conversions to other media types.
|
|
33
|
+
|
|
34
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
35
|
+
made available under the License, as indicated by a copyright notice that is
|
|
36
|
+
included in or attached to the work (an example is provided in the Appendix
|
|
37
|
+
below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object form,
|
|
40
|
+
that is based on (or derived from) the Work and for which the editorial
|
|
41
|
+
revisions, annotations, elaborations, or other modifications represent, as a
|
|
42
|
+
whole, an original work of authorship. For the purposes of this License,
|
|
43
|
+
Derivative Works shall not include works that remain separable from, or
|
|
44
|
+
merely link (or bind by name) to the interfaces of, the Work and Derivative
|
|
45
|
+
Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean any work of authorship, including the original
|
|
48
|
+
version of the Work and any modifications or additions to that Work or
|
|
49
|
+
Derivative Works thereof, that is intentionally submitted to Licensor for
|
|
50
|
+
inclusion in the Work by the copyright owner or by an individual or Legal
|
|
51
|
+
Entity authorized to submit on behalf of the copyright owner. For the
|
|
52
|
+
purposes of this definition, "submitted" means any form of electronic, verbal,
|
|
53
|
+
or written communication sent to the Licensor or its representatives,
|
|
54
|
+
including but not limited to communication on electronic mailing lists, source
|
|
55
|
+
code control systems, and issue tracking systems that are managed by, or on
|
|
56
|
+
behalf of, the Licensor for the purpose of discussing and improving the Work,
|
|
57
|
+
but excluding communication that is conspicuously marked or otherwise
|
|
58
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
59
|
+
|
|
60
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
61
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
62
|
+
incorporated within the Work.
|
|
63
|
+
|
|
64
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
65
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
66
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
67
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
68
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
69
|
+
Object form.
|
|
70
|
+
|
|
71
|
+
3. Grant of Patent License. Subject to the terms and conditions of this
|
|
72
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
73
|
+
non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this
|
|
74
|
+
section) patent license to make, have made, use, offer to sell, sell, import,
|
|
75
|
+
and otherwise transfer the Work, where such license applies only to those
|
|
76
|
+
patent claims licensable by such Contributor that are necessarily infringed by
|
|
77
|
+
their Contribution(s) alone or by combination of their Contribution(s) with
|
|
78
|
+
the Work to which such Contribution(s) was submitted. If You institute patent
|
|
79
|
+
litigation against any entity (including a cross-claim or counterclaim in a
|
|
80
|
+
lawsuit) alleging that the Work or a Contribution incorporated within the Work
|
|
81
|
+
constitutes direct or contributory patent infringement, then any patent
|
|
82
|
+
licenses granted to You under this License for that Work shall terminate as of
|
|
83
|
+
the date such litigation is filed.
|
|
84
|
+
|
|
85
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
86
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
87
|
+
Source or Object form, provided that You meet the following conditions:
|
|
88
|
+
|
|
89
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy
|
|
90
|
+
of this License; and
|
|
91
|
+
|
|
92
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
93
|
+
You changed the files; and
|
|
94
|
+
|
|
95
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
96
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
97
|
+
Source form of the Work, excluding those notices that do not pertain to any
|
|
98
|
+
part of the Derivative Works; and
|
|
99
|
+
|
|
100
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution,
|
|
101
|
+
then any Derivative Works that You distribute must include a readable copy of
|
|
102
|
+
the attribution notices contained within such NOTICE file, excluding those
|
|
103
|
+
notices that do not pertain to any part of the Derivative Works, in at least
|
|
104
|
+
one of the following places: within a NOTICE text file distributed as part of
|
|
105
|
+
the Derivative Works; within the Source form or documentation, if provided
|
|
106
|
+
along with the Derivative Works; or, within a display generated by the
|
|
107
|
+
Derivative Works, if and wherever such third-party notices normally appear.
|
|
108
|
+
The contents of the NOTICE file are for informational purposes only and do not
|
|
109
|
+
modify the License. You may add Your own attribution notices within Derivative
|
|
110
|
+
Works that You distribute, alongside or as an addendum to the NOTICE text from
|
|
111
|
+
the Work, provided that such additional attribution notices cannot be construed
|
|
112
|
+
as modifying the License.
|
|
113
|
+
|
|
114
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
115
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
116
|
+
distribution of Your modifications, or for any such Derivative Works as a
|
|
117
|
+
whole, provided Your use, reproduction, and distribution of the Work otherwise
|
|
118
|
+
complies with the conditions stated in this License.
|
|
119
|
+
|
|
120
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
121
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
122
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
123
|
+
additional terms or conditions. Notwithstanding the above, nothing herein
|
|
124
|
+
shall supersede or modify the terms of any separate license agreement you may
|
|
125
|
+
have executed with Licensor regarding such Contributions.
|
|
126
|
+
|
|
127
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
128
|
+
trademarks, service marks, or product names of the Licensor, except as
|
|
129
|
+
required for reasonable and customary use in describing the origin of the Work
|
|
130
|
+
and reproducing the content of the NOTICE file.
|
|
131
|
+
|
|
132
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
133
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
134
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
135
|
+
KIND, either express or implied, including, without limitation, any warranties
|
|
136
|
+
or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
137
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
138
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
139
|
+
associated with Your exercise of permissions under this License.
|
|
140
|
+
|
|
141
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
142
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
143
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
144
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
145
|
+
direct, indirect, special, incidental, or consequential damages of any
|
|
146
|
+
character arising as a result of this License or out of the use or inability to
|
|
147
|
+
use the Work (including but not limited to damages for loss of goodwill, work
|
|
148
|
+
stoppage, computer failure or malfunction, or any and all other commercial
|
|
149
|
+
damages or losses), even if such Contributor has been advised of the
|
|
150
|
+
possibility of such damages.
|
|
151
|
+
|
|
152
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work
|
|
153
|
+
or Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
154
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
|
155
|
+
and/or rights consistent with this License. However, in accepting such
|
|
156
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
157
|
+
responsibility, not on behalf of any other Contributor, and only if You agree
|
|
158
|
+
to indemnify, defend, and hold each Contributor harmless for any liability
|
|
159
|
+
incurred by, or claims asserted against, such Contributor by reason of your
|
|
160
|
+
accepting any such warranty or additional liability.
|
|
161
|
+
|
|
162
|
+
END OF TERMS AND CONDITIONS
|
opuscodec-0.1.2/PKG-INFO
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: opuscodec
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Python bindings and self-contained Opus CLI builds with QEXT enabled by default.
|
|
5
|
+
Author-email: Fish Audio <lengyue@fish.audio>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/fishaudio/opuscodec
|
|
8
|
+
Project-URL: Repository, https://github.com/fishaudio/opuscodec
|
|
9
|
+
Project-URL: Issues, https://github.com/fishaudio/opuscodec/issues
|
|
10
|
+
Project-URL: Releases, https://github.com/fishaudio/opuscodec/releases
|
|
11
|
+
Keywords: opus,audio,codec,pybind11,qext
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Programming Language :: C++
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Topic :: Multimedia :: Sound/Audio
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Requires-Python: >=3.9
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: numpy>=1.24
|
|
29
|
+
Provides-Extra: test
|
|
30
|
+
Requires-Dist: pytest>=8.0; extra == "test"
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
# opuscodec
|
|
34
|
+
|
|
35
|
+
Python bindings and self-contained Opus CLI builds, with **QEXT enabled by default**.
|
|
36
|
+
|
|
37
|
+
`opuscodec` packages three things together:
|
|
38
|
+
|
|
39
|
+
- `OpusBufferedEncoder` / `OpusBufferedDecoder` Python bindings
|
|
40
|
+
- self-contained `opusenc` / `opusdec` release binaries
|
|
41
|
+
- vendored Xiph dependencies, built from source when needed
|
|
42
|
+
|
|
43
|
+
Stable dependency set:
|
|
44
|
+
|
|
45
|
+
- `opus` `1.6.1`
|
|
46
|
+
- `opus-tools` `0.2`
|
|
47
|
+
- `libogg` `1.3.6`
|
|
48
|
+
- `opusfile` `0.12`
|
|
49
|
+
- `libopusenc` `0.3`
|
|
50
|
+
|
|
51
|
+
## Why this package
|
|
52
|
+
|
|
53
|
+
- no system Opus install required by default
|
|
54
|
+
- runtime + build-time QEXT control
|
|
55
|
+
- Python API and CLI assets released from one repo
|
|
56
|
+
- PyPI wheels for supported targets, source build fallback everywhere else
|
|
57
|
+
|
|
58
|
+
## Installation
|
|
59
|
+
|
|
60
|
+
### PyPI (recommended)
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
python -m pip install opuscodec==0.1.2
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### GitHub Release asset
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
python -m pip install "https://github.com/fishaudio/opuscodec/releases/download/v0.1.2/<wheel-file-name>.whl"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Example Linux wheel name:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
python -m pip install ./opuscodec-0.1.2-cp312-cp312-manylinux_2_28_x86_64.whl
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Source build
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
make test
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Common commands:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
make install # editable install + test deps
|
|
88
|
+
make test # run pytest
|
|
89
|
+
make wheel # build wheel into dist/wheels
|
|
90
|
+
make binaries # build opusenc/opusdec into dist/bin
|
|
91
|
+
make clean # clean build artifacts
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Python example
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
import numpy as np
|
|
98
|
+
import opuscodec
|
|
99
|
+
|
|
100
|
+
sr = 48000
|
|
101
|
+
x = (0.1 * np.sin(2 * np.pi * 440 * np.arange(sr) / sr) * 32767).astype(np.int16).reshape(-1, 1)
|
|
102
|
+
|
|
103
|
+
enc = opuscodec.OpusBufferedEncoder(sample_rate=sr, channels=1)
|
|
104
|
+
packet = enc.write(x) + enc.flush()
|
|
105
|
+
|
|
106
|
+
dec = opuscodec.OpusBufferedDecoder()
|
|
107
|
+
y = dec.decode(packet)
|
|
108
|
+
|
|
109
|
+
print(y.shape, opuscodec.opus_version(), opuscodec.qext_enabled(), enc.qext_enabled())
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Disable runtime QEXT for one encoder instance:
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
enc = opuscodec.OpusBufferedEncoder(sample_rate=48000, channels=1, qext=False)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Standalone binary usage
|
|
119
|
+
|
|
120
|
+
After downloading release binaries:
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
tar -xzf opuscodec-v0.1.2-linux-amd64-binaries.tar.gz
|
|
124
|
+
chmod +x opusenc opusdec
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### WAV roundtrip
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
./opusenc input.wav output.opus
|
|
131
|
+
./opusdec output.opus roundtrip.wav
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
`opusenc` enables QEXT by default in this repository build. Disable it explicitly for comparison tests:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
./opusenc --set-ctl-int 4056=0 input.wav output-noqext.opus
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Raw PCM roundtrip
|
|
141
|
+
|
|
142
|
+
Encode raw PCM (`mono`, `48k`, `s16le`) to Opus:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
./opusenc --raw --raw-bits 16 --raw-rate 48000 --raw-chan 1 input.pcm output.opus
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Decode Opus back to PCM:
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
./opusdec output.opus decoded.pcm
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Build configuration
|
|
155
|
+
|
|
156
|
+
Defaults: vendored dependencies; QEXT enabled.
|
|
157
|
+
|
|
158
|
+
Optional environment variables:
|
|
159
|
+
|
|
160
|
+
- `OPUSCODEC_ENABLE_QEXT=0` — disable QEXT
|
|
161
|
+
- `OPUSCODEC_USE_SYSTEM_DEPS=1` — use system libraries instead of vendored build
|
|
162
|
+
- `OPUSCODEC_DEPS_PREFIX=/path/to/prefix` — custom dependency prefix
|
|
163
|
+
|
|
164
|
+
When QEXT is enabled at build time, packaged `opusenc` binaries also enable `OPUS_SET_QEXT(1)` by default.
|
|
165
|
+
|
|
166
|
+
## Repository layout
|
|
167
|
+
|
|
168
|
+
```text
|
|
169
|
+
.
|
|
170
|
+
├── .github/workflows/build.yml
|
|
171
|
+
├── Makefile
|
|
172
|
+
├── pyproject.toml
|
|
173
|
+
├── setup.py
|
|
174
|
+
├── src/
|
|
175
|
+
│ └── opuscodec_bindings.cpp
|
|
176
|
+
├── scripts/
|
|
177
|
+
│ ├── build_deps.sh
|
|
178
|
+
│ └── build_binaries.sh
|
|
179
|
+
├── tests/
|
|
180
|
+
│ └── test_bindings.py
|
|
181
|
+
├── opusenc.py
|
|
182
|
+
└── opusdec.py
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Release automation
|
|
186
|
+
|
|
187
|
+
On tag push (for example `v0.1.2`), GitHub Actions will:
|
|
188
|
+
|
|
189
|
+
- run tests on Linux + macOS
|
|
190
|
+
- build PyPI-compatible manylinux wheels for Linux
|
|
191
|
+
- build macOS arm64 wheels
|
|
192
|
+
- build an `sdist`
|
|
193
|
+
- publish wheel + sdist artifacts to PyPI via OIDC
|
|
194
|
+
- create a GitHub Release and upload wheels + binary tarballs
|
|
195
|
+
|
|
196
|
+
## License
|
|
197
|
+
|
|
198
|
+
Apache License 2.0. See [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# opuscodec
|
|
2
|
+
|
|
3
|
+
Python bindings and self-contained Opus CLI builds, with **QEXT enabled by default**.
|
|
4
|
+
|
|
5
|
+
`opuscodec` packages three things together:
|
|
6
|
+
|
|
7
|
+
- `OpusBufferedEncoder` / `OpusBufferedDecoder` Python bindings
|
|
8
|
+
- self-contained `opusenc` / `opusdec` release binaries
|
|
9
|
+
- vendored Xiph dependencies, built from source when needed
|
|
10
|
+
|
|
11
|
+
Stable dependency set:
|
|
12
|
+
|
|
13
|
+
- `opus` `1.6.1`
|
|
14
|
+
- `opus-tools` `0.2`
|
|
15
|
+
- `libogg` `1.3.6`
|
|
16
|
+
- `opusfile` `0.12`
|
|
17
|
+
- `libopusenc` `0.3`
|
|
18
|
+
|
|
19
|
+
## Why this package
|
|
20
|
+
|
|
21
|
+
- no system Opus install required by default
|
|
22
|
+
- runtime + build-time QEXT control
|
|
23
|
+
- Python API and CLI assets released from one repo
|
|
24
|
+
- PyPI wheels for supported targets, source build fallback everywhere else
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
### PyPI (recommended)
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
python -m pip install opuscodec==0.1.2
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### GitHub Release asset
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
python -m pip install "https://github.com/fishaudio/opuscodec/releases/download/v0.1.2/<wheel-file-name>.whl"
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Example Linux wheel name:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
python -m pip install ./opuscodec-0.1.2-cp312-cp312-manylinux_2_28_x86_64.whl
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Source build
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
make test
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Common commands:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
make install # editable install + test deps
|
|
56
|
+
make test # run pytest
|
|
57
|
+
make wheel # build wheel into dist/wheels
|
|
58
|
+
make binaries # build opusenc/opusdec into dist/bin
|
|
59
|
+
make clean # clean build artifacts
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Python example
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
import numpy as np
|
|
66
|
+
import opuscodec
|
|
67
|
+
|
|
68
|
+
sr = 48000
|
|
69
|
+
x = (0.1 * np.sin(2 * np.pi * 440 * np.arange(sr) / sr) * 32767).astype(np.int16).reshape(-1, 1)
|
|
70
|
+
|
|
71
|
+
enc = opuscodec.OpusBufferedEncoder(sample_rate=sr, channels=1)
|
|
72
|
+
packet = enc.write(x) + enc.flush()
|
|
73
|
+
|
|
74
|
+
dec = opuscodec.OpusBufferedDecoder()
|
|
75
|
+
y = dec.decode(packet)
|
|
76
|
+
|
|
77
|
+
print(y.shape, opuscodec.opus_version(), opuscodec.qext_enabled(), enc.qext_enabled())
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Disable runtime QEXT for one encoder instance:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
enc = opuscodec.OpusBufferedEncoder(sample_rate=48000, channels=1, qext=False)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Standalone binary usage
|
|
87
|
+
|
|
88
|
+
After downloading release binaries:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
tar -xzf opuscodec-v0.1.2-linux-amd64-binaries.tar.gz
|
|
92
|
+
chmod +x opusenc opusdec
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### WAV roundtrip
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
./opusenc input.wav output.opus
|
|
99
|
+
./opusdec output.opus roundtrip.wav
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`opusenc` enables QEXT by default in this repository build. Disable it explicitly for comparison tests:
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
./opusenc --set-ctl-int 4056=0 input.wav output-noqext.opus
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Raw PCM roundtrip
|
|
109
|
+
|
|
110
|
+
Encode raw PCM (`mono`, `48k`, `s16le`) to Opus:
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
./opusenc --raw --raw-bits 16 --raw-rate 48000 --raw-chan 1 input.pcm output.opus
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Decode Opus back to PCM:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
./opusdec output.opus decoded.pcm
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Build configuration
|
|
123
|
+
|
|
124
|
+
Defaults: vendored dependencies; QEXT enabled.
|
|
125
|
+
|
|
126
|
+
Optional environment variables:
|
|
127
|
+
|
|
128
|
+
- `OPUSCODEC_ENABLE_QEXT=0` — disable QEXT
|
|
129
|
+
- `OPUSCODEC_USE_SYSTEM_DEPS=1` — use system libraries instead of vendored build
|
|
130
|
+
- `OPUSCODEC_DEPS_PREFIX=/path/to/prefix` — custom dependency prefix
|
|
131
|
+
|
|
132
|
+
When QEXT is enabled at build time, packaged `opusenc` binaries also enable `OPUS_SET_QEXT(1)` by default.
|
|
133
|
+
|
|
134
|
+
## Repository layout
|
|
135
|
+
|
|
136
|
+
```text
|
|
137
|
+
.
|
|
138
|
+
├── .github/workflows/build.yml
|
|
139
|
+
├── Makefile
|
|
140
|
+
├── pyproject.toml
|
|
141
|
+
├── setup.py
|
|
142
|
+
├── src/
|
|
143
|
+
│ └── opuscodec_bindings.cpp
|
|
144
|
+
├── scripts/
|
|
145
|
+
│ ├── build_deps.sh
|
|
146
|
+
│ └── build_binaries.sh
|
|
147
|
+
├── tests/
|
|
148
|
+
│ └── test_bindings.py
|
|
149
|
+
├── opusenc.py
|
|
150
|
+
└── opusdec.py
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Release automation
|
|
154
|
+
|
|
155
|
+
On tag push (for example `v0.1.2`), GitHub Actions will:
|
|
156
|
+
|
|
157
|
+
- run tests on Linux + macOS
|
|
158
|
+
- build PyPI-compatible manylinux wheels for Linux
|
|
159
|
+
- build macOS arm64 wheels
|
|
160
|
+
- build an `sdist`
|
|
161
|
+
- publish wheel + sdist artifacts to PyPI via OIDC
|
|
162
|
+
- create a GitHub Release and upload wheels + binary tarballs
|
|
163
|
+
|
|
164
|
+
## License
|
|
165
|
+
|
|
166
|
+
Apache License 2.0. See [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: opuscodec
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Python bindings and self-contained Opus CLI builds with QEXT enabled by default.
|
|
5
|
+
Author-email: Fish Audio <lengyue@fish.audio>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/fishaudio/opuscodec
|
|
8
|
+
Project-URL: Repository, https://github.com/fishaudio/opuscodec
|
|
9
|
+
Project-URL: Issues, https://github.com/fishaudio/opuscodec/issues
|
|
10
|
+
Project-URL: Releases, https://github.com/fishaudio/opuscodec/releases
|
|
11
|
+
Keywords: opus,audio,codec,pybind11,qext
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
15
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
16
|
+
Classifier: Programming Language :: C++
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Topic :: Multimedia :: Sound/Audio
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Requires-Python: >=3.9
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: numpy>=1.24
|
|
29
|
+
Provides-Extra: test
|
|
30
|
+
Requires-Dist: pytest>=8.0; extra == "test"
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
# opuscodec
|
|
34
|
+
|
|
35
|
+
Python bindings and self-contained Opus CLI builds, with **QEXT enabled by default**.
|
|
36
|
+
|
|
37
|
+
`opuscodec` packages three things together:
|
|
38
|
+
|
|
39
|
+
- `OpusBufferedEncoder` / `OpusBufferedDecoder` Python bindings
|
|
40
|
+
- self-contained `opusenc` / `opusdec` release binaries
|
|
41
|
+
- vendored Xiph dependencies, built from source when needed
|
|
42
|
+
|
|
43
|
+
Stable dependency set:
|
|
44
|
+
|
|
45
|
+
- `opus` `1.6.1`
|
|
46
|
+
- `opus-tools` `0.2`
|
|
47
|
+
- `libogg` `1.3.6`
|
|
48
|
+
- `opusfile` `0.12`
|
|
49
|
+
- `libopusenc` `0.3`
|
|
50
|
+
|
|
51
|
+
## Why this package
|
|
52
|
+
|
|
53
|
+
- no system Opus install required by default
|
|
54
|
+
- runtime + build-time QEXT control
|
|
55
|
+
- Python API and CLI assets released from one repo
|
|
56
|
+
- PyPI wheels for supported targets, source build fallback everywhere else
|
|
57
|
+
|
|
58
|
+
## Installation
|
|
59
|
+
|
|
60
|
+
### PyPI (recommended)
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
python -m pip install opuscodec==0.1.2
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### GitHub Release asset
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
python -m pip install "https://github.com/fishaudio/opuscodec/releases/download/v0.1.2/<wheel-file-name>.whl"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Example Linux wheel name:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
python -m pip install ./opuscodec-0.1.2-cp312-cp312-manylinux_2_28_x86_64.whl
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Source build
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
make test
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Common commands:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
make install # editable install + test deps
|
|
88
|
+
make test # run pytest
|
|
89
|
+
make wheel # build wheel into dist/wheels
|
|
90
|
+
make binaries # build opusenc/opusdec into dist/bin
|
|
91
|
+
make clean # clean build artifacts
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Python example
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
import numpy as np
|
|
98
|
+
import opuscodec
|
|
99
|
+
|
|
100
|
+
sr = 48000
|
|
101
|
+
x = (0.1 * np.sin(2 * np.pi * 440 * np.arange(sr) / sr) * 32767).astype(np.int16).reshape(-1, 1)
|
|
102
|
+
|
|
103
|
+
enc = opuscodec.OpusBufferedEncoder(sample_rate=sr, channels=1)
|
|
104
|
+
packet = enc.write(x) + enc.flush()
|
|
105
|
+
|
|
106
|
+
dec = opuscodec.OpusBufferedDecoder()
|
|
107
|
+
y = dec.decode(packet)
|
|
108
|
+
|
|
109
|
+
print(y.shape, opuscodec.opus_version(), opuscodec.qext_enabled(), enc.qext_enabled())
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Disable runtime QEXT for one encoder instance:
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
enc = opuscodec.OpusBufferedEncoder(sample_rate=48000, channels=1, qext=False)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Standalone binary usage
|
|
119
|
+
|
|
120
|
+
After downloading release binaries:
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
tar -xzf opuscodec-v0.1.2-linux-amd64-binaries.tar.gz
|
|
124
|
+
chmod +x opusenc opusdec
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### WAV roundtrip
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
./opusenc input.wav output.opus
|
|
131
|
+
./opusdec output.opus roundtrip.wav
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
`opusenc` enables QEXT by default in this repository build. Disable it explicitly for comparison tests:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
./opusenc --set-ctl-int 4056=0 input.wav output-noqext.opus
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Raw PCM roundtrip
|
|
141
|
+
|
|
142
|
+
Encode raw PCM (`mono`, `48k`, `s16le`) to Opus:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
./opusenc --raw --raw-bits 16 --raw-rate 48000 --raw-chan 1 input.pcm output.opus
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Decode Opus back to PCM:
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
./opusdec output.opus decoded.pcm
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Build configuration
|
|
155
|
+
|
|
156
|
+
Defaults: vendored dependencies; QEXT enabled.
|
|
157
|
+
|
|
158
|
+
Optional environment variables:
|
|
159
|
+
|
|
160
|
+
- `OPUSCODEC_ENABLE_QEXT=0` — disable QEXT
|
|
161
|
+
- `OPUSCODEC_USE_SYSTEM_DEPS=1` — use system libraries instead of vendored build
|
|
162
|
+
- `OPUSCODEC_DEPS_PREFIX=/path/to/prefix` — custom dependency prefix
|
|
163
|
+
|
|
164
|
+
When QEXT is enabled at build time, packaged `opusenc` binaries also enable `OPUS_SET_QEXT(1)` by default.
|
|
165
|
+
|
|
166
|
+
## Repository layout
|
|
167
|
+
|
|
168
|
+
```text
|
|
169
|
+
.
|
|
170
|
+
├── .github/workflows/build.yml
|
|
171
|
+
├── Makefile
|
|
172
|
+
├── pyproject.toml
|
|
173
|
+
├── setup.py
|
|
174
|
+
├── src/
|
|
175
|
+
│ └── opuscodec_bindings.cpp
|
|
176
|
+
├── scripts/
|
|
177
|
+
│ ├── build_deps.sh
|
|
178
|
+
│ └── build_binaries.sh
|
|
179
|
+
├── tests/
|
|
180
|
+
│ └── test_bindings.py
|
|
181
|
+
├── opusenc.py
|
|
182
|
+
└── opusdec.py
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Release automation
|
|
186
|
+
|
|
187
|
+
On tag push (for example `v0.1.2`), GitHub Actions will:
|
|
188
|
+
|
|
189
|
+
- run tests on Linux + macOS
|
|
190
|
+
- build PyPI-compatible manylinux wheels for Linux
|
|
191
|
+
- build macOS arm64 wheels
|
|
192
|
+
- build an `sdist`
|
|
193
|
+
- publish wheel + sdist artifacts to PyPI via OIDC
|
|
194
|
+
- create a GitHub Release and upload wheels + binary tarballs
|
|
195
|
+
|
|
196
|
+
## License
|
|
197
|
+
|
|
198
|
+
Apache License 2.0. See [LICENSE](./LICENSE).
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
opusdec.py
|
|
4
|
+
opusenc.py
|
|
5
|
+
pyproject.toml
|
|
6
|
+
setup.py
|
|
7
|
+
opuscodec.egg-info/PKG-INFO
|
|
8
|
+
opuscodec.egg-info/SOURCES.txt
|
|
9
|
+
opuscodec.egg-info/dependency_links.txt
|
|
10
|
+
opuscodec.egg-info/not-zip-safe
|
|
11
|
+
opuscodec.egg-info/requires.txt
|
|
12
|
+
opuscodec.egg-info/top_level.txt
|
|
13
|
+
src/opuscodec_bindings.cpp
|
|
14
|
+
tests/test_bindings.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = [
|
|
3
|
+
"setuptools>=77",
|
|
4
|
+
"wheel",
|
|
5
|
+
"pybind11>=2.11",
|
|
6
|
+
"numpy>=1.24",
|
|
7
|
+
]
|
|
8
|
+
build-backend = "setuptools.build_meta"
|
|
9
|
+
|
|
10
|
+
[project]
|
|
11
|
+
name = "opuscodec"
|
|
12
|
+
version = "0.1.2"
|
|
13
|
+
description = "Python bindings and self-contained Opus CLI builds with QEXT enabled by default."
|
|
14
|
+
readme = "README.md"
|
|
15
|
+
requires-python = ">=3.9"
|
|
16
|
+
license = "Apache-2.0"
|
|
17
|
+
license-files = ["LICENSE"]
|
|
18
|
+
authors = [
|
|
19
|
+
{ name = "Fish Audio", email = "lengyue@fish.audio" },
|
|
20
|
+
]
|
|
21
|
+
keywords = ["opus", "audio", "codec", "pybind11", "qext"]
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Development Status :: 4 - Beta",
|
|
24
|
+
"Intended Audience :: Developers",
|
|
25
|
+
"Operating System :: MacOS :: MacOS X",
|
|
26
|
+
"Operating System :: POSIX :: Linux",
|
|
27
|
+
"Programming Language :: C++",
|
|
28
|
+
"Programming Language :: Python :: 3",
|
|
29
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
30
|
+
"Programming Language :: Python :: 3.9",
|
|
31
|
+
"Programming Language :: Python :: 3.10",
|
|
32
|
+
"Programming Language :: Python :: 3.11",
|
|
33
|
+
"Programming Language :: Python :: 3.12",
|
|
34
|
+
"Topic :: Multimedia :: Sound/Audio",
|
|
35
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
36
|
+
]
|
|
37
|
+
dependencies = [
|
|
38
|
+
"numpy>=1.24",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.optional-dependencies]
|
|
42
|
+
test = [
|
|
43
|
+
"pytest>=8.0",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
[project.urls]
|
|
47
|
+
Homepage = "https://github.com/fishaudio/opuscodec"
|
|
48
|
+
Repository = "https://github.com/fishaudio/opuscodec"
|
|
49
|
+
Issues = "https://github.com/fishaudio/opuscodec/issues"
|
|
50
|
+
Releases = "https://github.com/fishaudio/opuscodec/releases"
|
|
51
|
+
|
|
52
|
+
[tool.setuptools]
|
|
53
|
+
py-modules = ["opusenc", "opusdec"]
|
|
54
|
+
|
|
55
|
+
[tool.pytest.ini_options]
|
|
56
|
+
addopts = "-q"
|
|
57
|
+
testpaths = ["tests"]
|
|
58
|
+
|
|
59
|
+
[tool.ruff]
|
|
60
|
+
target-version = "py39"
|
|
61
|
+
|
|
62
|
+
[tool.ruff.lint]
|
|
63
|
+
extend-select = [
|
|
64
|
+
"B",
|
|
65
|
+
"I",
|
|
66
|
+
"PGH",
|
|
67
|
+
"RUF",
|
|
68
|
+
"UP",
|
|
69
|
+
]
|
opuscodec-0.1.2/setup.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import platform
|
|
3
|
+
import re
|
|
4
|
+
import subprocess
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from pybind11.setup_helpers import Pybind11Extension, build_ext
|
|
8
|
+
from setuptools import setup
|
|
9
|
+
|
|
10
|
+
ROOT = Path(__file__).resolve().parent
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def read_project_version() -> str:
|
|
14
|
+
pyproject = (ROOT / "pyproject.toml").read_text(encoding="utf-8")
|
|
15
|
+
match = re.search(r'^version\s*=\s*"([^"]+)"\s*$', pyproject, re.MULTILINE)
|
|
16
|
+
if not match:
|
|
17
|
+
raise RuntimeError("Could not determine package version from pyproject.toml")
|
|
18
|
+
return match.group(1)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
__version__ = read_project_version()
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class OpusCodecBuildExt(build_ext):
|
|
25
|
+
def build_extensions(self):
|
|
26
|
+
use_system_deps = os.environ.get("OPUSCODEC_USE_SYSTEM_DEPS", "0") == "1"
|
|
27
|
+
qext_requested = os.environ.get("OPUSCODEC_ENABLE_QEXT", "1") != "0"
|
|
28
|
+
|
|
29
|
+
include_dirs = []
|
|
30
|
+
library_dirs = []
|
|
31
|
+
qext_enabled = qext_requested
|
|
32
|
+
|
|
33
|
+
if not use_system_deps:
|
|
34
|
+
platform_tag = f"{platform.system().lower()}-{platform.machine().lower()}"
|
|
35
|
+
deps_prefix = Path(
|
|
36
|
+
os.environ.get("OPUSCODEC_DEPS_PREFIX", ROOT / "build" / "deps" / platform_tag)
|
|
37
|
+
).resolve()
|
|
38
|
+
build_script = ROOT / "scripts" / "build_deps.sh"
|
|
39
|
+
env = os.environ.copy()
|
|
40
|
+
env.setdefault("OPUSCODEC_ENABLE_QEXT", "1" if qext_requested else "0")
|
|
41
|
+
env.setdefault("OPUSCODEC_WITH_OPUS_TOOLS", "0")
|
|
42
|
+
subprocess.check_call(["bash", str(build_script), str(deps_prefix)], cwd=ROOT, env=env)
|
|
43
|
+
include_dirs.append(str(deps_prefix / "include"))
|
|
44
|
+
include_dirs.append(str(deps_prefix / "include" / "opus"))
|
|
45
|
+
library_dirs.append(str(deps_prefix / "lib"))
|
|
46
|
+
qext_enabled = (deps_prefix / ".qext-enabled").exists()
|
|
47
|
+
|
|
48
|
+
libraries = ["opusenc", "opusfile", "opus", "ogg"]
|
|
49
|
+
if platform.system() == "Linux":
|
|
50
|
+
libraries.append("m")
|
|
51
|
+
|
|
52
|
+
for ext in self.extensions:
|
|
53
|
+
ext.include_dirs.extend(include_dirs)
|
|
54
|
+
ext.library_dirs.extend(library_dirs)
|
|
55
|
+
ext.libraries.extend(libraries)
|
|
56
|
+
ext.define_macros.append(("OPUSCODEC_QEXT_ENABLED", "1" if qext_enabled else "0"))
|
|
57
|
+
|
|
58
|
+
super().build_extensions()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
ext_modules = [
|
|
62
|
+
Pybind11Extension(
|
|
63
|
+
"opuscodec",
|
|
64
|
+
["src/opuscodec_bindings.cpp"],
|
|
65
|
+
define_macros=[("VERSION_INFO", __version__)],
|
|
66
|
+
cxx_std=17,
|
|
67
|
+
),
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
setup(
|
|
71
|
+
ext_modules=ext_modules,
|
|
72
|
+
cmdclass={"build_ext": OpusCodecBuildExt},
|
|
73
|
+
zip_safe=False,
|
|
74
|
+
)
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
#include <opus/opus.h>
|
|
2
|
+
#include <opus/opusenc.h>
|
|
3
|
+
#include <opus/opusfile.h>
|
|
4
|
+
|
|
5
|
+
#include <pybind11/numpy.h>
|
|
6
|
+
#include <pybind11/pybind11.h>
|
|
7
|
+
|
|
8
|
+
#include <cstdint>
|
|
9
|
+
#include <cstring>
|
|
10
|
+
#include <limits>
|
|
11
|
+
#include <stdexcept>
|
|
12
|
+
#include <string>
|
|
13
|
+
#include <vector>
|
|
14
|
+
|
|
15
|
+
namespace py = pybind11;
|
|
16
|
+
|
|
17
|
+
namespace {
|
|
18
|
+
constexpr int kMaxSamplesPerChannel = 120 * 48;
|
|
19
|
+
#if defined(OPUSCODEC_QEXT_ENABLED)
|
|
20
|
+
constexpr bool kQextDefaultEnabled = true;
|
|
21
|
+
#else
|
|
22
|
+
constexpr bool kQextDefaultEnabled = false;
|
|
23
|
+
#endif
|
|
24
|
+
|
|
25
|
+
[[noreturn]] void throw_value_error(const char *msg) {
|
|
26
|
+
throw py::value_error(msg);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
std::string ope_error_message(int err) {
|
|
30
|
+
const char *err_str = ope_strerror(err);
|
|
31
|
+
if (err_str == nullptr) {
|
|
32
|
+
return "Unknown Opus encoder error";
|
|
33
|
+
}
|
|
34
|
+
return std::string(err_str);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
} // namespace
|
|
38
|
+
|
|
39
|
+
class OpusBufferedEncoder {
|
|
40
|
+
public:
|
|
41
|
+
OpusBufferedEncoder(int sample_rate,
|
|
42
|
+
int channels,
|
|
43
|
+
int bitrate = OPUS_AUTO,
|
|
44
|
+
int signal_type = 0,
|
|
45
|
+
int encoder_complexity = 10,
|
|
46
|
+
int decision_delay = 0,
|
|
47
|
+
bool qext = kQextDefaultEnabled)
|
|
48
|
+
: encoder_(nullptr),
|
|
49
|
+
comments_(nullptr),
|
|
50
|
+
channels_(channels),
|
|
51
|
+
has_written_(false),
|
|
52
|
+
flushed_(false),
|
|
53
|
+
qext_enabled_(false) {
|
|
54
|
+
if (channels < 1 || channels > 8) {
|
|
55
|
+
throw_value_error("Invalid channels, must be in range [1, 8].");
|
|
56
|
+
}
|
|
57
|
+
if ((bitrate < 500 || bitrate > 512000) && bitrate != OPUS_BITRATE_MAX && bitrate != OPUS_AUTO) {
|
|
58
|
+
throw_value_error("Invalid bitrate, must be between 500 and 512000, OPUS_BITRATE_MAX, or OPUS_AUTO.");
|
|
59
|
+
}
|
|
60
|
+
if (sample_rate < 8000 || sample_rate > 48000) {
|
|
61
|
+
throw_value_error("Invalid sample_rate, must be in range [8000, 48000].");
|
|
62
|
+
}
|
|
63
|
+
if (encoder_complexity < 0 || encoder_complexity > 10) {
|
|
64
|
+
throw_value_error("Invalid encoder_complexity, must be in range [0, 10].");
|
|
65
|
+
}
|
|
66
|
+
if (decision_delay < 0) {
|
|
67
|
+
throw_value_error("Invalid decision_delay, must be >= 0.");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
int error = OPE_OK;
|
|
71
|
+
comments_ = ope_comments_create();
|
|
72
|
+
if (comments_ == nullptr) {
|
|
73
|
+
throw_value_error("Failed to allocate Opus comments.");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
encoder_ = ope_encoder_create_pull(comments_, sample_rate, channels, 0, &error);
|
|
77
|
+
if (error != OPE_OK || encoder_ == nullptr) {
|
|
78
|
+
close();
|
|
79
|
+
throw py::value_error(("Failed to create Opus encoder: " + ope_error_message(error)).c_str());
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (ope_encoder_ctl(encoder_, OPUS_SET_BITRATE(bitrate)) != OPE_OK) {
|
|
83
|
+
close();
|
|
84
|
+
throw_value_error("Could not set bitrate.");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
opus_int32 opus_signal_type = OPUS_AUTO;
|
|
88
|
+
switch (signal_type) {
|
|
89
|
+
case 0:
|
|
90
|
+
opus_signal_type = OPUS_AUTO;
|
|
91
|
+
break;
|
|
92
|
+
case 1:
|
|
93
|
+
opus_signal_type = OPUS_SIGNAL_MUSIC;
|
|
94
|
+
break;
|
|
95
|
+
case 2:
|
|
96
|
+
opus_signal_type = OPUS_SIGNAL_VOICE;
|
|
97
|
+
break;
|
|
98
|
+
default:
|
|
99
|
+
close();
|
|
100
|
+
throw_value_error("Invalid signal_type, must be 0 (auto), 1 (music), or 2 (voice).");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (ope_encoder_ctl(encoder_, OPUS_SET_SIGNAL(opus_signal_type)) != OPE_OK) {
|
|
104
|
+
close();
|
|
105
|
+
throw_value_error("Could not set signal type.");
|
|
106
|
+
}
|
|
107
|
+
if (ope_encoder_ctl(encoder_, OPUS_SET_COMPLEXITY(encoder_complexity)) != OPE_OK) {
|
|
108
|
+
close();
|
|
109
|
+
throw_value_error("Could not set encoder complexity.");
|
|
110
|
+
}
|
|
111
|
+
if (ope_encoder_ctl(encoder_, OPE_SET_DECISION_DELAY(decision_delay)) != OPE_OK) {
|
|
112
|
+
close();
|
|
113
|
+
throw_value_error("Could not set decision delay.");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
#if defined(OPUS_SET_QEXT_REQUEST)
|
|
117
|
+
if (qext) {
|
|
118
|
+
if (ope_encoder_ctl(encoder_, OPUS_SET_QEXT(1)) != OPE_OK) {
|
|
119
|
+
close();
|
|
120
|
+
throw_value_error("Could not enable QEXT.");
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
#else
|
|
124
|
+
if (qext) {
|
|
125
|
+
close();
|
|
126
|
+
throw_value_error("QEXT requested but this libopus does not expose OPUS_SET_QEXT.");
|
|
127
|
+
}
|
|
128
|
+
#endif
|
|
129
|
+
|
|
130
|
+
#if defined(OPUS_GET_QEXT_REQUEST)
|
|
131
|
+
opus_int32 qext_value = 0;
|
|
132
|
+
const int qext_ret = ope_encoder_ctl(encoder_, OPUS_GET_QEXT(&qext_value));
|
|
133
|
+
if (qext_ret == OPE_OK) {
|
|
134
|
+
qext_enabled_ = (qext_value != 0);
|
|
135
|
+
} else {
|
|
136
|
+
qext_enabled_ = false;
|
|
137
|
+
}
|
|
138
|
+
#else
|
|
139
|
+
qext_enabled_ = false;
|
|
140
|
+
#endif
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
py::bytes write(const py::array_t<int16_t, py::array::c_style | py::array::forcecast> &buffer) {
|
|
144
|
+
ensure_open();
|
|
145
|
+
if (flushed_) {
|
|
146
|
+
throw_value_error("flush() was already called; create a new encoder instance.");
|
|
147
|
+
}
|
|
148
|
+
if (buffer.ndim() != 2 || buffer.shape(1) != channels_) {
|
|
149
|
+
throw_value_error("Buffer must have shape [samples, channels] matching constructor channels.");
|
|
150
|
+
}
|
|
151
|
+
if (buffer.shape(0) == 0) {
|
|
152
|
+
return py::bytes();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const int16_t *data = buffer.data();
|
|
156
|
+
const auto samples = static_cast<int>(buffer.shape(0));
|
|
157
|
+
const int ret = ope_encoder_write(encoder_, data, samples);
|
|
158
|
+
if (ret != OPE_OK) {
|
|
159
|
+
throw py::value_error(("Encoding failed: " + ope_error_message(ret)).c_str());
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
std::vector<unsigned char> encoded_data;
|
|
163
|
+
unsigned char *packet = nullptr;
|
|
164
|
+
opus_int32 len = 0;
|
|
165
|
+
while (ope_encoder_get_page(encoder_, &packet, &len, 1) != 0) {
|
|
166
|
+
encoded_data.insert(encoded_data.end(), packet, packet + len);
|
|
167
|
+
has_written_ = true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return py::bytes(reinterpret_cast<const char *>(encoded_data.data()), encoded_data.size());
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
py::bytes flush() {
|
|
174
|
+
ensure_open();
|
|
175
|
+
if (!has_written_) {
|
|
176
|
+
throw_value_error("You must call write() at least once before flush().");
|
|
177
|
+
}
|
|
178
|
+
if (flushed_) {
|
|
179
|
+
throw_value_error("flush() can only be called once.");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const int ret = ope_encoder_drain(encoder_);
|
|
183
|
+
if (ret != OPE_OK) {
|
|
184
|
+
throw py::value_error(("Draining failed: " + ope_error_message(ret)).c_str());
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
std::vector<unsigned char> encoded_data;
|
|
188
|
+
unsigned char *packet = nullptr;
|
|
189
|
+
opus_int32 len = 0;
|
|
190
|
+
while (ope_encoder_get_page(encoder_, &packet, &len, 1) != 0) {
|
|
191
|
+
encoded_data.insert(encoded_data.end(), packet, packet + len);
|
|
192
|
+
}
|
|
193
|
+
flushed_ = true;
|
|
194
|
+
return py::bytes(reinterpret_cast<const char *>(encoded_data.data()), encoded_data.size());
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
void close() {
|
|
198
|
+
if (encoder_ != nullptr) {
|
|
199
|
+
ope_encoder_destroy(encoder_);
|
|
200
|
+
encoder_ = nullptr;
|
|
201
|
+
}
|
|
202
|
+
if (comments_ != nullptr) {
|
|
203
|
+
ope_comments_destroy(comments_);
|
|
204
|
+
comments_ = nullptr;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
bool qext_enabled() const { return qext_enabled_; }
|
|
209
|
+
|
|
210
|
+
~OpusBufferedEncoder() { close(); }
|
|
211
|
+
|
|
212
|
+
private:
|
|
213
|
+
void ensure_open() const {
|
|
214
|
+
if (encoder_ == nullptr) {
|
|
215
|
+
throw_value_error("Encoder is closed.");
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
OggOpusEnc *encoder_;
|
|
220
|
+
OggOpusComments *comments_;
|
|
221
|
+
int channels_;
|
|
222
|
+
bool has_written_;
|
|
223
|
+
bool flushed_;
|
|
224
|
+
bool qext_enabled_;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
class OpusBufferedDecoder {
|
|
228
|
+
public:
|
|
229
|
+
OpusBufferedDecoder() = default;
|
|
230
|
+
|
|
231
|
+
py::array_t<int16_t> decode(const py::bytes &opus_data) const {
|
|
232
|
+
std::string encoded = opus_data;
|
|
233
|
+
if (encoded.empty()) {
|
|
234
|
+
return py::array_t<int16_t>(py::array::ShapeContainer{0, 0});
|
|
235
|
+
}
|
|
236
|
+
if (encoded.size() > static_cast<size_t>(std::numeric_limits<opus_int32>::max())) {
|
|
237
|
+
throw_value_error("Input is too large to decode.");
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
int error = 0;
|
|
241
|
+
OggOpusFile *opus_file = op_open_memory(
|
|
242
|
+
reinterpret_cast<const unsigned char *>(encoded.data()), static_cast<opus_int32>(encoded.size()), &error);
|
|
243
|
+
if (opus_file == nullptr) {
|
|
244
|
+
throw py::value_error(("Failed to parse opus stream, op_open_memory error=" + std::to_string(error)).c_str());
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const int channels = op_channel_count(opus_file, -1);
|
|
248
|
+
if (channels <= 0 || channels > 8) {
|
|
249
|
+
op_free(opus_file);
|
|
250
|
+
throw_value_error("Invalid channel count from opus stream.");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
std::vector<int16_t> pcm;
|
|
254
|
+
std::vector<int16_t> frame(static_cast<size_t>(kMaxSamplesPerChannel * channels));
|
|
255
|
+
|
|
256
|
+
while (true) {
|
|
257
|
+
const int samples = op_read(opus_file, frame.data(), static_cast<int>(frame.size()), nullptr);
|
|
258
|
+
if (samples == 0) {
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
if (samples < 0) {
|
|
262
|
+
op_free(opus_file);
|
|
263
|
+
throw py::value_error(("Decode failed, op_read error=" + std::to_string(samples)).c_str());
|
|
264
|
+
}
|
|
265
|
+
pcm.insert(pcm.end(), frame.begin(), frame.begin() + static_cast<size_t>(samples * channels));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
op_free(opus_file);
|
|
269
|
+
|
|
270
|
+
const auto sample_count = static_cast<ssize_t>(pcm.size() / static_cast<size_t>(channels));
|
|
271
|
+
py::array_t<int16_t> output({sample_count, static_cast<ssize_t>(channels)});
|
|
272
|
+
if (!pcm.empty()) {
|
|
273
|
+
std::memcpy(output.mutable_data(), pcm.data(), pcm.size() * sizeof(int16_t));
|
|
274
|
+
}
|
|
275
|
+
return output;
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
PYBIND11_MODULE(opuscodec, m) {
|
|
280
|
+
m.doc() = "Python bindings for opusenc/opusdec with vendored libopus builds";
|
|
281
|
+
|
|
282
|
+
py::class_<OpusBufferedEncoder>(m, "OpusBufferedEncoder")
|
|
283
|
+
.def(py::init<int, int, int, int, int, int, bool>(),
|
|
284
|
+
py::arg("sample_rate"),
|
|
285
|
+
py::arg("channels"),
|
|
286
|
+
py::arg("bitrate") = OPUS_AUTO,
|
|
287
|
+
py::arg("signal_type") = 0,
|
|
288
|
+
py::arg("encoder_complexity") = 10,
|
|
289
|
+
py::arg("decision_delay") = 0,
|
|
290
|
+
py::arg("qext") = kQextDefaultEnabled)
|
|
291
|
+
.def("write", &OpusBufferedEncoder::write, py::arg("buffer"))
|
|
292
|
+
.def("flush", &OpusBufferedEncoder::flush)
|
|
293
|
+
.def("close", &OpusBufferedEncoder::close)
|
|
294
|
+
.def("qext_enabled", &OpusBufferedEncoder::qext_enabled);
|
|
295
|
+
|
|
296
|
+
py::class_<OpusBufferedDecoder>(m, "OpusBufferedDecoder")
|
|
297
|
+
.def(py::init<>())
|
|
298
|
+
.def("decode", &OpusBufferedDecoder::decode, py::arg("opus_data"));
|
|
299
|
+
|
|
300
|
+
m.def("opus_version", []() { return std::string(opus_get_version_string()); });
|
|
301
|
+
#if defined(OPUSCODEC_QEXT_ENABLED)
|
|
302
|
+
m.def("qext_enabled", []() { return true; });
|
|
303
|
+
#else
|
|
304
|
+
m.def("qext_enabled", []() { return false; });
|
|
305
|
+
#endif
|
|
306
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
import opuscodec
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _make_pcm(sample_rate: int = 48_000, seconds: float = 1.0, channels: int = 1) -> np.ndarray:
|
|
10
|
+
t = np.arange(int(sample_rate * seconds), dtype=np.float64) / sample_rate
|
|
11
|
+
mono = 0.2 * np.sin(2 * np.pi * 440 * t)
|
|
12
|
+
pcm = (mono * 32767).astype(np.int16)
|
|
13
|
+
if channels == 1:
|
|
14
|
+
return pcm.reshape(-1, 1)
|
|
15
|
+
return np.repeat(pcm.reshape(-1, 1), channels, axis=1)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _best_aligned_corr(x: np.ndarray, y: np.ndarray) -> float:
|
|
19
|
+
x = x.astype(np.float64)
|
|
20
|
+
y = y.astype(np.float64)
|
|
21
|
+
corr = np.correlate(y, x, mode="full")
|
|
22
|
+
lag = int(np.argmax(np.abs(corr)) - (len(x) - 1))
|
|
23
|
+
if lag >= 0:
|
|
24
|
+
x_aligned = x[: len(y) - lag]
|
|
25
|
+
y_aligned = y[lag : lag + len(x_aligned)]
|
|
26
|
+
else:
|
|
27
|
+
y_aligned = y[: len(x) + lag]
|
|
28
|
+
x_aligned = x[-lag : -lag + len(y_aligned)]
|
|
29
|
+
|
|
30
|
+
n = min(len(x_aligned), len(y_aligned))
|
|
31
|
+
if n < 1000:
|
|
32
|
+
return 0.0
|
|
33
|
+
x_aligned = x_aligned[:n]
|
|
34
|
+
y_aligned = y_aligned[:n]
|
|
35
|
+
return float(np.corrcoef(x_aligned, y_aligned)[0, 1])
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_encode_decode_roundtrip() -> None:
|
|
39
|
+
pcm = _make_pcm(channels=1)
|
|
40
|
+
|
|
41
|
+
encoder = opuscodec.OpusBufferedEncoder(sample_rate=48_000, channels=1, bitrate=64_000)
|
|
42
|
+
encoded = b""
|
|
43
|
+
for i in range(0, len(pcm), 960):
|
|
44
|
+
encoded += encoder.write(pcm[i : i + 960])
|
|
45
|
+
encoded += encoder.flush()
|
|
46
|
+
|
|
47
|
+
decoder = opuscodec.OpusBufferedDecoder()
|
|
48
|
+
decoded = decoder.decode(encoded)
|
|
49
|
+
|
|
50
|
+
assert decoded.dtype == np.int16
|
|
51
|
+
assert decoded.ndim == 2
|
|
52
|
+
assert decoded.shape[1] == 1
|
|
53
|
+
assert decoded.shape[0] > 10_000
|
|
54
|
+
|
|
55
|
+
corr = _best_aligned_corr(pcm[:, 0], decoded[:, 0])
|
|
56
|
+
assert corr > 0.70
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_encoder_rejects_invalid_channels() -> None:
|
|
60
|
+
with pytest.raises(ValueError):
|
|
61
|
+
opuscodec.OpusBufferedEncoder(sample_rate=48_000, channels=0)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_flush_requires_write() -> None:
|
|
65
|
+
encoder = opuscodec.OpusBufferedEncoder(sample_rate=48_000, channels=1)
|
|
66
|
+
with pytest.raises(ValueError):
|
|
67
|
+
encoder.flush()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_qext_default_enabled() -> None:
|
|
71
|
+
expected = os.environ.get("OPUSCODEC_ENABLE_QEXT", "1") != "0"
|
|
72
|
+
assert opuscodec.qext_enabled() is expected
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_encoder_qext_runtime_default_enabled() -> None:
|
|
76
|
+
encoder = opuscodec.OpusBufferedEncoder(sample_rate=48_000, channels=1)
|
|
77
|
+
expected = os.environ.get("OPUSCODEC_ENABLE_QEXT", "1") != "0"
|
|
78
|
+
assert encoder.qext_enabled() is expected
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def test_encoder_qext_runtime_can_disable() -> None:
|
|
82
|
+
encoder = opuscodec.OpusBufferedEncoder(sample_rate=48_000, channels=1, qext=False)
|
|
83
|
+
assert encoder.qext_enabled() is False
|