speex-codec 1.0.0__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.
Files changed (98) hide show
  1. speex_codec-1.0.0/LICENSE +28 -0
  2. speex_codec-1.0.0/MANIFEST.in +4 -0
  3. speex_codec-1.0.0/PKG-INFO +49 -0
  4. speex_codec-1.0.0/README.md +31 -0
  5. speex_codec-1.0.0/pyproject.toml +38 -0
  6. speex_codec-1.0.0/setup.cfg +4 -0
  7. speex_codec-1.0.0/setup.py +38 -0
  8. speex_codec-1.0.0/src/speex_codec/__init__.py +11 -0
  9. speex_codec-1.0.0/src/speex_codec/_speex.c +351 -0
  10. speex_codec-1.0.0/src/speex_codec/_speex.pyi +17 -0
  11. speex_codec-1.0.0/src/speex_codec/py.typed +0 -0
  12. speex_codec-1.0.0/src/speex_codec.egg-info/PKG-INFO +49 -0
  13. speex_codec-1.0.0/src/speex_codec.egg-info/SOURCES.txt +96 -0
  14. speex_codec-1.0.0/src/speex_codec.egg-info/dependency_links.txt +1 -0
  15. speex_codec-1.0.0/src/speex_codec.egg-info/top_level.txt +1 -0
  16. speex_codec-1.0.0/tests/test_codec.py +71 -0
  17. speex_codec-1.0.0/vendor/README.md +14 -0
  18. speex_codec-1.0.0/vendor/speex/COPYING +35 -0
  19. speex_codec-1.0.0/vendor/speex/include/speex/speex.h +425 -0
  20. speex_codec-1.0.0/vendor/speex/include/speex/speex_bits.h +174 -0
  21. speex_codec-1.0.0/vendor/speex/include/speex/speex_callbacks.h +134 -0
  22. speex_codec-1.0.0/vendor/speex/include/speex/speex_config_types.h +12 -0
  23. speex_codec-1.0.0/vendor/speex/include/speex/speex_header.h +94 -0
  24. speex_codec-1.0.0/vendor/speex/include/speex/speex_stereo.h +92 -0
  25. speex_codec-1.0.0/vendor/speex/include/speex/speex_types.h +126 -0
  26. speex_codec-1.0.0/vendor/speex/libspeex/_kiss_fft_guts.h +160 -0
  27. speex_codec-1.0.0/vendor/speex/libspeex/arch.h +237 -0
  28. speex_codec-1.0.0/vendor/speex/libspeex/bfin.h +15 -0
  29. speex_codec-1.0.0/vendor/speex/libspeex/bits.c +372 -0
  30. speex_codec-1.0.0/vendor/speex/libspeex/cb_search.c +623 -0
  31. speex_codec-1.0.0/vendor/speex/libspeex/cb_search.h +103 -0
  32. speex_codec-1.0.0/vendor/speex/libspeex/cb_search_arm4.h +137 -0
  33. speex_codec-1.0.0/vendor/speex/libspeex/cb_search_bfin.h +111 -0
  34. speex_codec-1.0.0/vendor/speex/libspeex/cb_search_sse.h +84 -0
  35. speex_codec-1.0.0/vendor/speex/libspeex/exc_10_16_table.c +50 -0
  36. speex_codec-1.0.0/vendor/speex/libspeex/exc_10_32_table.c +66 -0
  37. speex_codec-1.0.0/vendor/speex/libspeex/exc_20_32_table.c +66 -0
  38. speex_codec-1.0.0/vendor/speex/libspeex/exc_5_256_table.c +290 -0
  39. speex_codec-1.0.0/vendor/speex/libspeex/exc_5_64_table.c +98 -0
  40. speex_codec-1.0.0/vendor/speex/libspeex/exc_8_128_table.c +162 -0
  41. speex_codec-1.0.0/vendor/speex/libspeex/fftwrap.h +58 -0
  42. speex_codec-1.0.0/vendor/speex/libspeex/filters.c +836 -0
  43. speex_codec-1.0.0/vendor/speex/libspeex/filters.h +105 -0
  44. speex_codec-1.0.0/vendor/speex/libspeex/filters_arm4.h +96 -0
  45. speex_codec-1.0.0/vendor/speex/libspeex/filters_bfin.h +520 -0
  46. speex_codec-1.0.0/vendor/speex/libspeex/filters_sse.h +336 -0
  47. speex_codec-1.0.0/vendor/speex/libspeex/fixed_arm4.h +148 -0
  48. speex_codec-1.0.0/vendor/speex/libspeex/fixed_arm5e.h +178 -0
  49. speex_codec-1.0.0/vendor/speex/libspeex/fixed_bfin.h +176 -0
  50. speex_codec-1.0.0/vendor/speex/libspeex/fixed_debug.h +487 -0
  51. speex_codec-1.0.0/vendor/speex/libspeex/fixed_generic.h +106 -0
  52. speex_codec-1.0.0/vendor/speex/libspeex/gain_table.c +160 -0
  53. speex_codec-1.0.0/vendor/speex/libspeex/gain_table_lbr.c +64 -0
  54. speex_codec-1.0.0/vendor/speex/libspeex/hexc_10_32_table.c +66 -0
  55. speex_codec-1.0.0/vendor/speex/libspeex/hexc_table.c +162 -0
  56. speex_codec-1.0.0/vendor/speex/libspeex/high_lsp_tables.c +163 -0
  57. speex_codec-1.0.0/vendor/speex/libspeex/kiss_fft.h +108 -0
  58. speex_codec-1.0.0/vendor/speex/libspeex/kiss_fftr.h +51 -0
  59. speex_codec-1.0.0/vendor/speex/libspeex/lpc.c +198 -0
  60. speex_codec-1.0.0/vendor/speex/libspeex/lpc.h +53 -0
  61. speex_codec-1.0.0/vendor/speex/libspeex/lpc_bfin.h +134 -0
  62. speex_codec-1.0.0/vendor/speex/libspeex/lsp.c +641 -0
  63. speex_codec-1.0.0/vendor/speex/libspeex/lsp.h +62 -0
  64. speex_codec-1.0.0/vendor/speex/libspeex/lsp_bfin.h +89 -0
  65. speex_codec-1.0.0/vendor/speex/libspeex/lsp_tables_nb.c +360 -0
  66. speex_codec-1.0.0/vendor/speex/libspeex/ltp.c +850 -0
  67. speex_codec-1.0.0/vendor/speex/libspeex/ltp.h +145 -0
  68. speex_codec-1.0.0/vendor/speex/libspeex/ltp_arm4.h +187 -0
  69. speex_codec-1.0.0/vendor/speex/libspeex/ltp_bfin.h +414 -0
  70. speex_codec-1.0.0/vendor/speex/libspeex/ltp_sse.h +92 -0
  71. speex_codec-1.0.0/vendor/speex/libspeex/math_approx.h +332 -0
  72. speex_codec-1.0.0/vendor/speex/libspeex/misc_bfin.h +56 -0
  73. speex_codec-1.0.0/vendor/speex/libspeex/modes.c +390 -0
  74. speex_codec-1.0.0/vendor/speex/libspeex/modes.h +160 -0
  75. speex_codec-1.0.0/vendor/speex/libspeex/modes_wb.c +322 -0
  76. speex_codec-1.0.0/vendor/speex/libspeex/nb_celp.c +1821 -0
  77. speex_codec-1.0.0/vendor/speex/libspeex/nb_celp.h +199 -0
  78. speex_codec-1.0.0/vendor/speex/libspeex/os_support.h +169 -0
  79. speex_codec-1.0.0/vendor/speex/libspeex/quant_lsp.c +386 -0
  80. speex_codec-1.0.0/vendor/speex/libspeex/quant_lsp.h +74 -0
  81. speex_codec-1.0.0/vendor/speex/libspeex/quant_lsp_bfin.h +168 -0
  82. speex_codec-1.0.0/vendor/speex/libspeex/sb_celp.c +1443 -0
  83. speex_codec-1.0.0/vendor/speex/libspeex/sb_celp.h +154 -0
  84. speex_codec-1.0.0/vendor/speex/libspeex/smallft.h +46 -0
  85. speex_codec-1.0.0/vendor/speex/libspeex/speex.c +253 -0
  86. speex_codec-1.0.0/vendor/speex/libspeex/speex_callbacks.c +144 -0
  87. speex_codec-1.0.0/vendor/speex/libspeex/speex_header.c +202 -0
  88. speex_codec-1.0.0/vendor/speex/libspeex/stack_alloc.h +115 -0
  89. speex_codec-1.0.0/vendor/speex/libspeex/stereo.c +303 -0
  90. speex_codec-1.0.0/vendor/speex/libspeex/vbr.c +272 -0
  91. speex_codec-1.0.0/vendor/speex/libspeex/vbr.h +69 -0
  92. speex_codec-1.0.0/vendor/speex/libspeex/vorbis_psy.h +97 -0
  93. speex_codec-1.0.0/vendor/speex/libspeex/vq.c +147 -0
  94. speex_codec-1.0.0/vendor/speex/libspeex/vq.h +54 -0
  95. speex_codec-1.0.0/vendor/speex/libspeex/vq_arm4.h +115 -0
  96. speex_codec-1.0.0/vendor/speex/libspeex/vq_bfin.h +110 -0
  97. speex_codec-1.0.0/vendor/speex/libspeex/vq_sse.h +120 -0
  98. speex_codec-1.0.0/vendor/speex/libspeex/window.c +102 -0
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, willheisenberg
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,4 @@
1
+ include LICENSE README.md
2
+ recursive-include vendor *.c *.h COPYING README.md
3
+ include src/speex_codec/*.pyi src/speex_codec/py.typed
4
+ recursive-include tests *.py
@@ -0,0 +1,49 @@
1
+ Metadata-Version: 2.4
2
+ Name: speex-codec
3
+ Version: 1.0.0
4
+ Summary: Speex speech codec (encoder and decoder) for Python, with libspeex built in
5
+ Author: willheisenberg
6
+ License-Expression: BSD-3-Clause
7
+ Project-URL: Source, https://github.com/willheisenberg/speex-codec
8
+ Keywords: speex,codec,audio,voip,sip
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: C
11
+ Classifier: Topic :: Multimedia :: Sound/Audio :: Conversion
12
+ Classifier: Topic :: Communications :: Telephony
13
+ Requires-Python: >=3.10
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ License-File: vendor/speex/COPYING
17
+ Dynamic: license-file
18
+
19
+ # speex-codec
20
+
21
+ The [Speex](https://speex.org/) speech codec for Python, **with libspeex built in**: no system
22
+ library to install. Wheels for Linux (glibc and musl/Alpine, x86_64 and aarch64) and macOS,
23
+ one per platform for every Python from 3.10 on (stable ABI).
24
+
25
+ Made for SIP door entry systems and other VoIP devices that only speak Speex, for example from
26
+ a Home Assistant integration, whose container image has no libspeex.
27
+
28
+ ```python
29
+ from speex_codec import Decoder, Encoder
30
+
31
+ enc = Encoder() # narrowband: 8 kHz, frames of 160 samples (20 ms)
32
+ dec = Decoder()
33
+
34
+ packet = enc.encode(pcm) # pcm: 320 bytes, signed 16-bit little-endian mono
35
+ pcm = dec.decode(packet) # one or more frames
36
+ pcm = dec.decode(None) # lost packet: one concealed frame
37
+ ```
38
+
39
+ - `Encoder(mode=0, quality=8, complexity=-1, vbr=False)`: mode 0 narrowband (8 kHz),
40
+ 1 wideband (16 kHz), 2 ultra-wideband (32 kHz); quality 0–10.
41
+ - `Decoder(mode=0, enhance=True)`: `enhance` turns on the perceptual enhancer.
42
+ - `frame_size` and `sample_rate` on both; `SPEEX_VERSION` gives the libspeex version.
43
+
44
+ Encoding and decoding release the GIL.
45
+
46
+ ## Licenses
47
+
48
+ The bindings are BSD-3-Clause (`LICENSE`). Speex 1.2.1 is vendored unchanged under its revised
49
+ BSD license (`vendor/speex/COPYING`); see `vendor/README.md` for its origin and checksum.
@@ -0,0 +1,31 @@
1
+ # speex-codec
2
+
3
+ The [Speex](https://speex.org/) speech codec for Python, **with libspeex built in**: no system
4
+ library to install. Wheels for Linux (glibc and musl/Alpine, x86_64 and aarch64) and macOS,
5
+ one per platform for every Python from 3.10 on (stable ABI).
6
+
7
+ Made for SIP door entry systems and other VoIP devices that only speak Speex, for example from
8
+ a Home Assistant integration, whose container image has no libspeex.
9
+
10
+ ```python
11
+ from speex_codec import Decoder, Encoder
12
+
13
+ enc = Encoder() # narrowband: 8 kHz, frames of 160 samples (20 ms)
14
+ dec = Decoder()
15
+
16
+ packet = enc.encode(pcm) # pcm: 320 bytes, signed 16-bit little-endian mono
17
+ pcm = dec.decode(packet) # one or more frames
18
+ pcm = dec.decode(None) # lost packet: one concealed frame
19
+ ```
20
+
21
+ - `Encoder(mode=0, quality=8, complexity=-1, vbr=False)`: mode 0 narrowband (8 kHz),
22
+ 1 wideband (16 kHz), 2 ultra-wideband (32 kHz); quality 0–10.
23
+ - `Decoder(mode=0, enhance=True)`: `enhance` turns on the perceptual enhancer.
24
+ - `frame_size` and `sample_rate` on both; `SPEEX_VERSION` gives the libspeex version.
25
+
26
+ Encoding and decoding release the GIL.
27
+
28
+ ## Licenses
29
+
30
+ The bindings are BSD-3-Clause (`LICENSE`). Speex 1.2.1 is vendored unchanged under its revised
31
+ BSD license (`vendor/speex/COPYING`); see `vendor/README.md` for its origin and checksum.
@@ -0,0 +1,38 @@
1
+ [build-system]
2
+ requires = ["setuptools>=70", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "speex-codec"
7
+ version = "1.0.0"
8
+ description = "Speex speech codec (encoder and decoder) for Python, with libspeex built in"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "BSD-3-Clause"
12
+ license-files = ["LICENSE", "vendor/speex/COPYING"]
13
+ authors = [{ name = "willheisenberg" }]
14
+ keywords = ["speex", "codec", "audio", "voip", "sip"]
15
+ classifiers = [
16
+ "Programming Language :: Python :: 3",
17
+ "Programming Language :: C",
18
+ "Topic :: Multimedia :: Sound/Audio :: Conversion",
19
+ "Topic :: Communications :: Telephony",
20
+ ]
21
+
22
+ [project.urls]
23
+ Source = "https://github.com/willheisenberg/speex-codec"
24
+
25
+ [tool.setuptools.packages.find]
26
+ where = ["src"]
27
+
28
+ [tool.setuptools.package-data]
29
+ speex_codec = ["py.typed", "*.pyi"]
30
+
31
+ [tool.setuptools]
32
+ include-package-data = false
33
+
34
+ [tool.cibuildwheel]
35
+ build = "cp310-*"
36
+ skip = "*-win32 *_i686"
37
+ test-requires = "pytest"
38
+ test-command = "pytest {project}/tests -q"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,38 @@
1
+ """Build the extension from the vendored libspeex sources (see vendor/README.md)."""
2
+ import glob
3
+ import sys
4
+
5
+ from setuptools import Extension, setup
6
+
7
+ LIBSPEEX = sorted(glob.glob("vendor/speex/libspeex/*.c"))
8
+
9
+ # What upstream's configure defines (config.h); Py_LIMITED_API is set in _speex.c
10
+ define_macros = [
11
+ ("FLOATING_POINT", None),
12
+ ("EXPORT", ""),
13
+ ("SPEEX_VERSION", '"1.2.1"'),
14
+ ("SPEEX_MAJOR_VERSION", "1"),
15
+ ("SPEEX_MINOR_VERSION", "2"),
16
+ ("SPEEX_MICRO_VERSION", "1"),
17
+ ("SPEEX_EXTRA_VERSION", '""'),
18
+ ]
19
+ extra_compile_args = []
20
+ if sys.platform == "win32":
21
+ define_macros.append(("USE_ALLOCA", None)) # MSVC has no variable-length arrays
22
+ else:
23
+ define_macros.append(("VAR_ARRAYS", None))
24
+ extra_compile_args += ["-O2", "-w"] # upstream code: keep the build log readable
25
+
26
+ setup(
27
+ ext_modules=[
28
+ Extension(
29
+ "speex_codec._speex",
30
+ sources=["src/speex_codec/_speex.c", *LIBSPEEX],
31
+ include_dirs=["vendor/speex/include", "vendor/speex/libspeex"],
32
+ define_macros=define_macros,
33
+ extra_compile_args=extra_compile_args,
34
+ py_limited_api=True,
35
+ )
36
+ ],
37
+ options={"bdist_wheel": {"py_limited_api": "cp310"}},
38
+ )
@@ -0,0 +1,11 @@
1
+ """Speex speech codec with libspeex built in.
2
+
3
+ >>> from speex_codec import Encoder, Decoder
4
+ >>> enc, dec = Encoder(), Decoder() # narrowband: 8 kHz, 20 ms = 160 samples
5
+ >>> packet = enc.encode(b"\\x00\\x00" * enc.frame_size)
6
+ >>> len(dec.decode(packet)) == 2 * dec.frame_size
7
+ True
8
+ """
9
+ from ._speex import SPEEX_VERSION, Decoder, Encoder
10
+
11
+ __all__ = ["SPEEX_VERSION", "Decoder", "Encoder"]
@@ -0,0 +1,351 @@
1
+ /* Python bindings for the Speex codec (libspeex 1.2.1, vendored).
2
+ *
3
+ * Built against the stable ABI (Py_LIMITED_API, Python 3.10+): one wheel per platform.
4
+ */
5
+ #define PY_SSIZE_T_CLEAN
6
+ #define Py_LIMITED_API 0x030A0000
7
+ #include <Python.h>
8
+
9
+ #include <speex/speex.h>
10
+
11
+ /* Largest packet a frame can produce (ultra-wideband, quality 10, plus headroom). */
12
+ #define MAX_PACKET 1024
13
+ /* A packet never holds more frames than this (guards against corrupt input). */
14
+ #define MAX_FRAMES_PER_PACKET 32
15
+
16
+ static const SpeexMode *
17
+ get_mode(int mode)
18
+ {
19
+ if (mode < 0 || mode >= SPEEX_NB_MODES) {
20
+ PyErr_Format(PyExc_ValueError, "mode must be 0 (narrowband), 1 (wideband) or 2 (ultra-wideband), not %d",
21
+ mode);
22
+ return NULL;
23
+ }
24
+ return speex_lib_get_mode(mode);
25
+ }
26
+
27
+ /* ---------------------------------------------------------------------------------------------- Encoder */
28
+
29
+ typedef struct {
30
+ PyObject_HEAD
31
+ void *state;
32
+ SpeexBits bits;
33
+ int frame_size;
34
+ int sample_rate;
35
+ } EncoderObject;
36
+
37
+ static int
38
+ Encoder_init(EncoderObject *self, PyObject *args, PyObject *kwds)
39
+ {
40
+ static char *kwlist[] = {"mode", "quality", "complexity", "vbr", NULL};
41
+ int mode = 0, quality = 8, complexity = -1, vbr = 0;
42
+
43
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iiip", kwlist, &mode, &quality, &complexity, &vbr))
44
+ return -1;
45
+ const SpeexMode *m = get_mode(mode);
46
+ if (m == NULL)
47
+ return -1;
48
+ if (quality < 0 || quality > 10) {
49
+ PyErr_SetString(PyExc_ValueError, "quality must be between 0 and 10");
50
+ return -1;
51
+ }
52
+ if (complexity != -1 && (complexity < 1 || complexity > 10)) {
53
+ PyErr_SetString(PyExc_ValueError, "complexity must be between 1 and 10");
54
+ return -1;
55
+ }
56
+ if (self->state != NULL) {
57
+ PyErr_SetString(PyExc_RuntimeError, "Encoder already initialized");
58
+ return -1;
59
+ }
60
+ self->state = speex_encoder_init(m);
61
+ if (self->state == NULL) {
62
+ PyErr_NoMemory();
63
+ return -1;
64
+ }
65
+ speex_bits_init(&self->bits);
66
+ speex_encoder_ctl(self->state, SPEEX_SET_QUALITY, &quality);
67
+ if (complexity != -1)
68
+ speex_encoder_ctl(self->state, SPEEX_SET_COMPLEXITY, &complexity);
69
+ if (vbr) {
70
+ speex_encoder_ctl(self->state, SPEEX_SET_VBR, &vbr);
71
+ float vbr_quality = (float)quality;
72
+ speex_encoder_ctl(self->state, SPEEX_SET_VBR_QUALITY, &vbr_quality);
73
+ }
74
+ speex_encoder_ctl(self->state, SPEEX_GET_FRAME_SIZE, &self->frame_size);
75
+ speex_encoder_ctl(self->state, SPEEX_GET_SAMPLING_RATE, &self->sample_rate);
76
+ return 0;
77
+ }
78
+
79
+ static void
80
+ Encoder_dealloc(EncoderObject *self)
81
+ {
82
+ if (self->state != NULL) {
83
+ speex_encoder_destroy(self->state);
84
+ speex_bits_destroy(&self->bits);
85
+ self->state = NULL;
86
+ }
87
+ PyTypeObject *tp = Py_TYPE(self);
88
+ freefunc tp_free = (freefunc)PyType_GetSlot(tp, Py_tp_free);
89
+ tp_free(self);
90
+ Py_DECREF(tp);
91
+ }
92
+
93
+ static PyObject *
94
+ Encoder_encode(EncoderObject *self, PyObject *args)
95
+ {
96
+ const char *pcm;
97
+ Py_ssize_t len;
98
+
99
+ if (!PyArg_ParseTuple(args, "y#:encode", &pcm, &len))
100
+ return NULL;
101
+ if (self->state == NULL) {
102
+ PyErr_SetString(PyExc_RuntimeError, "Encoder not initialized");
103
+ return NULL;
104
+ }
105
+ if (len != (Py_ssize_t)self->frame_size * 2) {
106
+ PyErr_Format(PyExc_ValueError, "expected one frame of %d samples (%d bytes of s16le), got %zd bytes",
107
+ self->frame_size, self->frame_size * 2, len);
108
+ return NULL;
109
+ }
110
+ spx_int16_t samples[640]; /* ultra-wideband frame = 640 samples */
111
+ for (int i = 0; i < self->frame_size; i++) /* explicit little-endian, independent of the host */
112
+ samples[i] = (spx_int16_t)((unsigned char)pcm[2 * i] | ((unsigned char)pcm[2 * i + 1] << 8));
113
+
114
+ char out[MAX_PACKET];
115
+ int n;
116
+ Py_BEGIN_ALLOW_THREADS
117
+ speex_bits_reset(&self->bits);
118
+ speex_encode_int(self->state, samples, &self->bits);
119
+ speex_bits_insert_terminator(&self->bits);
120
+ n = speex_bits_write(&self->bits, out, MAX_PACKET);
121
+ Py_END_ALLOW_THREADS
122
+ return PyBytes_FromStringAndSize(out, n);
123
+ }
124
+
125
+ static PyObject *
126
+ Encoder_get_frame_size(EncoderObject *self, void *closure)
127
+ {
128
+ return PyLong_FromLong(self->frame_size);
129
+ }
130
+
131
+ static PyObject *
132
+ Encoder_get_sample_rate(EncoderObject *self, void *closure)
133
+ {
134
+ return PyLong_FromLong(self->sample_rate);
135
+ }
136
+
137
+ static PyMethodDef Encoder_methods[] = {
138
+ {"encode", (PyCFunction)Encoder_encode, METH_VARARGS,
139
+ "encode(pcm: bytes) -> bytes\n\nEncode one frame of signed 16-bit little-endian mono PCM "
140
+ "(frame_size samples) into one Speex packet."},
141
+ {NULL, NULL, 0, NULL},
142
+ };
143
+
144
+ static PyGetSetDef Encoder_getset[] = {
145
+ {"frame_size", (getter)Encoder_get_frame_size, NULL, "Samples per frame (160 narrowband, 320 wideband).",
146
+ NULL},
147
+ {"sample_rate", (getter)Encoder_get_sample_rate, NULL, "Sampling rate in Hz.", NULL},
148
+ {NULL},
149
+ };
150
+
151
+ static PyType_Slot Encoder_slots[] = {
152
+ {Py_tp_doc, "Encoder(mode=0, quality=8, complexity=None, vbr=False)\n\n"
153
+ "Speex encoder. mode: 0 narrowband (8 kHz), 1 wideband (16 kHz), 2 ultra-wideband (32 kHz)."},
154
+ {Py_tp_new, PyType_GenericNew},
155
+ {Py_tp_init, Encoder_init},
156
+ {Py_tp_dealloc, Encoder_dealloc},
157
+ {Py_tp_methods, Encoder_methods},
158
+ {Py_tp_getset, Encoder_getset},
159
+ {0, NULL},
160
+ };
161
+
162
+ static PyType_Spec Encoder_spec = {
163
+ "speex_codec._speex.Encoder", sizeof(EncoderObject), 0, Py_TPFLAGS_DEFAULT, Encoder_slots,
164
+ };
165
+
166
+ /* ---------------------------------------------------------------------------------------------- Decoder */
167
+
168
+ typedef struct {
169
+ PyObject_HEAD
170
+ void *state;
171
+ SpeexBits bits;
172
+ int frame_size;
173
+ int sample_rate;
174
+ } DecoderObject;
175
+
176
+ static int
177
+ Decoder_init(DecoderObject *self, PyObject *args, PyObject *kwds)
178
+ {
179
+ static char *kwlist[] = {"mode", "enhance", NULL};
180
+ int mode = 0, enhance = 1;
181
+
182
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ip", kwlist, &mode, &enhance))
183
+ return -1;
184
+ const SpeexMode *m = get_mode(mode);
185
+ if (m == NULL)
186
+ return -1;
187
+ if (self->state != NULL) {
188
+ PyErr_SetString(PyExc_RuntimeError, "Decoder already initialized");
189
+ return -1;
190
+ }
191
+ self->state = speex_decoder_init(m);
192
+ if (self->state == NULL) {
193
+ PyErr_NoMemory();
194
+ return -1;
195
+ }
196
+ speex_bits_init(&self->bits);
197
+ speex_decoder_ctl(self->state, SPEEX_SET_ENH, &enhance);
198
+ speex_decoder_ctl(self->state, SPEEX_GET_FRAME_SIZE, &self->frame_size);
199
+ speex_decoder_ctl(self->state, SPEEX_GET_SAMPLING_RATE, &self->sample_rate);
200
+ return 0;
201
+ }
202
+
203
+ static void
204
+ Decoder_dealloc(DecoderObject *self)
205
+ {
206
+ if (self->state != NULL) {
207
+ speex_decoder_destroy(self->state);
208
+ speex_bits_destroy(&self->bits);
209
+ self->state = NULL;
210
+ }
211
+ PyTypeObject *tp = Py_TYPE(self);
212
+ freefunc tp_free = (freefunc)PyType_GetSlot(tp, Py_tp_free);
213
+ tp_free(self);
214
+ Py_DECREF(tp);
215
+ }
216
+
217
+ static void
218
+ to_le(char *dst, const spx_int16_t *src, int n)
219
+ {
220
+ for (int i = 0; i < n; i++) {
221
+ unsigned short v = (unsigned short)src[i];
222
+ dst[2 * i] = (char)(v & 0xFF);
223
+ dst[2 * i + 1] = (char)(v >> 8);
224
+ }
225
+ }
226
+
227
+ static PyObject *
228
+ Decoder_decode(DecoderObject *self, PyObject *args)
229
+ {
230
+ PyObject *arg;
231
+ if (!PyArg_ParseTuple(args, "O:decode", &arg))
232
+ return NULL;
233
+ if (self->state == NULL) {
234
+ PyErr_SetString(PyExc_RuntimeError, "Decoder not initialized");
235
+ return NULL;
236
+ }
237
+ spx_int16_t samples[640];
238
+ int fs = self->frame_size;
239
+
240
+ if (arg == Py_None) { /* lost packet: the decoder fills in one frame */
241
+ char out[1280];
242
+ Py_BEGIN_ALLOW_THREADS
243
+ speex_decode_int(self->state, NULL, samples);
244
+ Py_END_ALLOW_THREADS
245
+ to_le(out, samples, fs);
246
+ return PyBytes_FromStringAndSize(out, (Py_ssize_t)fs * 2);
247
+ }
248
+
249
+ const char *packet;
250
+ Py_ssize_t len;
251
+ if (!PyArg_Parse(arg, "y#", &packet, &len))
252
+ return NULL;
253
+ if (len > MAX_PACKET * MAX_FRAMES_PER_PACKET) {
254
+ PyErr_SetString(PyExc_ValueError, "packet too large");
255
+ return NULL;
256
+ }
257
+ char *out = PyMem_Malloc((size_t)fs * 2 * MAX_FRAMES_PER_PACKET);
258
+ if (out == NULL)
259
+ return PyErr_NoMemory();
260
+ int frames = 0;
261
+ Py_BEGIN_ALLOW_THREADS
262
+ speex_bits_read_from(&self->bits, (char *)packet, (int)len);
263
+ while (frames < MAX_FRAMES_PER_PACKET && speex_bits_remaining(&self->bits) >= 5) {
264
+ if (speex_decode_int(self->state, &self->bits, samples) != 0)
265
+ break; /* -1 end of stream (terminator), -2 corrupt */
266
+ to_le(out + (size_t)frames * fs * 2, samples, fs);
267
+ frames++;
268
+ }
269
+ Py_END_ALLOW_THREADS
270
+ PyObject *result = PyBytes_FromStringAndSize(out, (Py_ssize_t)frames * fs * 2);
271
+ PyMem_Free(out);
272
+ return result;
273
+ }
274
+
275
+ static PyObject *
276
+ Decoder_get_frame_size(DecoderObject *self, void *closure)
277
+ {
278
+ return PyLong_FromLong(self->frame_size);
279
+ }
280
+
281
+ static PyObject *
282
+ Decoder_get_sample_rate(DecoderObject *self, void *closure)
283
+ {
284
+ return PyLong_FromLong(self->sample_rate);
285
+ }
286
+
287
+ static PyMethodDef Decoder_methods[] = {
288
+ {"decode", (PyCFunction)Decoder_decode, METH_VARARGS,
289
+ "decode(packet: bytes | None) -> bytes\n\nDecode one Speex packet (one or more frames) into signed "
290
+ "16-bit little-endian mono PCM. None stands for a lost packet: one frame is concealed."},
291
+ {NULL, NULL, 0, NULL},
292
+ };
293
+
294
+ static PyGetSetDef Decoder_getset[] = {
295
+ {"frame_size", (getter)Decoder_get_frame_size, NULL, "Samples per frame (160 narrowband, 320 wideband).",
296
+ NULL},
297
+ {"sample_rate", (getter)Decoder_get_sample_rate, NULL, "Sampling rate in Hz.", NULL},
298
+ {NULL},
299
+ };
300
+
301
+ static PyType_Slot Decoder_slots[] = {
302
+ {Py_tp_doc, "Decoder(mode=0, enhance=True)\n\n"
303
+ "Speex decoder. mode: 0 narrowband (8 kHz), 1 wideband (16 kHz), 2 ultra-wideband (32 kHz)."},
304
+ {Py_tp_new, PyType_GenericNew},
305
+ {Py_tp_init, Decoder_init},
306
+ {Py_tp_dealloc, Decoder_dealloc},
307
+ {Py_tp_methods, Decoder_methods},
308
+ {Py_tp_getset, Decoder_getset},
309
+ {0, NULL},
310
+ };
311
+
312
+ static PyType_Spec Decoder_spec = {
313
+ "speex_codec._speex.Decoder", sizeof(DecoderObject), 0, Py_TPFLAGS_DEFAULT, Decoder_slots,
314
+ };
315
+
316
+ /* ---------------------------------------------------------------------------------------------- module */
317
+
318
+ static int
319
+ module_exec(PyObject *module)
320
+ {
321
+ PyObject *enc = PyType_FromSpec(&Encoder_spec);
322
+ if (enc == NULL || PyModule_AddObject(module, "Encoder", enc) < 0) {
323
+ Py_XDECREF(enc);
324
+ return -1;
325
+ }
326
+ PyObject *dec = PyType_FromSpec(&Decoder_spec);
327
+ if (dec == NULL || PyModule_AddObject(module, "Decoder", dec) < 0) {
328
+ Py_XDECREF(dec);
329
+ return -1;
330
+ }
331
+ const char *version = NULL;
332
+ speex_lib_ctl(SPEEX_LIB_GET_VERSION_STRING, &version);
333
+ if (PyModule_AddStringConstant(module, "SPEEX_VERSION", version ? version : "unknown") < 0)
334
+ return -1;
335
+ return 0;
336
+ }
337
+
338
+ static PyModuleDef_Slot module_slots[] = {
339
+ {Py_mod_exec, module_exec},
340
+ {0, NULL},
341
+ };
342
+
343
+ static struct PyModuleDef module_def = {
344
+ PyModuleDef_HEAD_INIT, "_speex", "Speex codec (libspeex 1.2.1).", 0, NULL, module_slots,
345
+ };
346
+
347
+ PyMODINIT_FUNC
348
+ PyInit__speex(void)
349
+ {
350
+ return PyModuleDef_Init(&module_def);
351
+ }
@@ -0,0 +1,17 @@
1
+ SPEEX_VERSION: str
2
+
3
+ class Encoder:
4
+ def __init__(self, mode: int = 0, quality: int = 8, complexity: int = -1, vbr: bool = False) -> None: ...
5
+ @property
6
+ def frame_size(self) -> int: ...
7
+ @property
8
+ def sample_rate(self) -> int: ...
9
+ def encode(self, pcm: bytes, /) -> bytes: ...
10
+
11
+ class Decoder:
12
+ def __init__(self, mode: int = 0, enhance: bool = True) -> None: ...
13
+ @property
14
+ def frame_size(self) -> int: ...
15
+ @property
16
+ def sample_rate(self) -> int: ...
17
+ def decode(self, packet: bytes | None, /) -> bytes: ...
File without changes
@@ -0,0 +1,49 @@
1
+ Metadata-Version: 2.4
2
+ Name: speex-codec
3
+ Version: 1.0.0
4
+ Summary: Speex speech codec (encoder and decoder) for Python, with libspeex built in
5
+ Author: willheisenberg
6
+ License-Expression: BSD-3-Clause
7
+ Project-URL: Source, https://github.com/willheisenberg/speex-codec
8
+ Keywords: speex,codec,audio,voip,sip
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: C
11
+ Classifier: Topic :: Multimedia :: Sound/Audio :: Conversion
12
+ Classifier: Topic :: Communications :: Telephony
13
+ Requires-Python: >=3.10
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ License-File: vendor/speex/COPYING
17
+ Dynamic: license-file
18
+
19
+ # speex-codec
20
+
21
+ The [Speex](https://speex.org/) speech codec for Python, **with libspeex built in**: no system
22
+ library to install. Wheels for Linux (glibc and musl/Alpine, x86_64 and aarch64) and macOS,
23
+ one per platform for every Python from 3.10 on (stable ABI).
24
+
25
+ Made for SIP door entry systems and other VoIP devices that only speak Speex, for example from
26
+ a Home Assistant integration, whose container image has no libspeex.
27
+
28
+ ```python
29
+ from speex_codec import Decoder, Encoder
30
+
31
+ enc = Encoder() # narrowband: 8 kHz, frames of 160 samples (20 ms)
32
+ dec = Decoder()
33
+
34
+ packet = enc.encode(pcm) # pcm: 320 bytes, signed 16-bit little-endian mono
35
+ pcm = dec.decode(packet) # one or more frames
36
+ pcm = dec.decode(None) # lost packet: one concealed frame
37
+ ```
38
+
39
+ - `Encoder(mode=0, quality=8, complexity=-1, vbr=False)`: mode 0 narrowband (8 kHz),
40
+ 1 wideband (16 kHz), 2 ultra-wideband (32 kHz); quality 0–10.
41
+ - `Decoder(mode=0, enhance=True)`: `enhance` turns on the perceptual enhancer.
42
+ - `frame_size` and `sample_rate` on both; `SPEEX_VERSION` gives the libspeex version.
43
+
44
+ Encoding and decoding release the GIL.
45
+
46
+ ## Licenses
47
+
48
+ The bindings are BSD-3-Clause (`LICENSE`). Speex 1.2.1 is vendored unchanged under its revised
49
+ BSD license (`vendor/speex/COPYING`); see `vendor/README.md` for its origin and checksum.