codec: add allocation-free Rust Codec2 backend

This commit is contained in:
DeFiDude committed 2026-10-01 13:27:33 -06:00
1 parent b1b87860c4
commit d65e26bf57
34 files changed
+7465 -10

No files matched your search

+6
View File
@@ -4,6 +4,12 @@
### Codec foundations
- Add an opt-in, allocation-free `no_std` native Rust Codec2 crate for 1600/3200
bit/s. Initialize in caller-owned storage; isolate decoder randomness; use the
same Rust math on host and MCU; reject wrong native frame sizes. Correct the
inherited real-FFT input-copy truncation. Preserve upstream source/license
provenance. Target audio, CPU/stack and live integration remain separate gates.
- Add a fixed-profile Codec2 packet adapter over caller-owned PCM16/output
buffers and an application-supplied native backend. Reject malformed lengths,
unexpected modes and insufficient capacity before codec processing; stop
Generated
+13
View File
@@ -526,6 +526,12 @@ version = "0.2.186"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
[[package]]
name = "libm"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
[[package]]
name = "linux-raw-sys"
version = "0.12.1"
@@ -547,6 +553,13 @@ version = "0.4.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897"
[[package]]
name = "lxst-codec2"
version = "0.2.0"
dependencies = [
"libm",
]
[[package]]
name = "lxst-core"
version = "0.2.0"
+1
View File
@@ -2,6 +2,7 @@
resolver = "2"
members = [
"crates/lxst-core",
"crates/lxst-codec2",
"crates/lxst-rns",
"crates/lxst-telephony",
]
+8
View File
@@ -257,6 +257,14 @@ Applications still own platform integration:
Ratspeak uses this boundary for its native voice-call feature.
## Embedded Codec2 backend
[`lxst-codec2`](crates/lxst-codec2/README.md) supplies bounded native Rust
1600/3200 bit/s encode/decode with in-place initialization and no allocator.
It is separate from the live telephony runtime and requires device timing,
stack and audio qualification before use on hardware. It does not implement
700C or compatible Opus voice memo containers.
## Contributing
If the issue or contribution belongs upstream as well, start there. Python LXST
+26 -2
View File
@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"scope": "All registry third-party normal/build dependencies reachable from rsLXST workspace members across declared targets; dev-only and first-party path packages are excluded.",
"cargoLockSha256": "a852bb18196793d321a908544d9b1beaafe7947073f7477ae6b6c9e2823c3218",
"scope": "Adapted Codec2 and all registry third-party normal/build dependencies reachable from rsLXST workspace members across declared targets; dev-only and first-party path packages are excluded.",
"cargoLockSha256": "ea2327255af9e42663e04f2a30e3aeca494b339c1ad118205f91636af776bbb5",
"packages": [
{
"name": "aes",
@@ -101,6 +101,23 @@
"repository": "https://github.com/RustCrypto/traits",
"source": "registry+https://github.com/rust-lang/crates.io-index"
},
{
"name": "codec2 (adapted as lxst-codec2)",
"version": "0.3.1",
"license": "LGPL-2.1-only AND MIT AND BSD-3-Clause",
"repository": "https://github.com/scriptjunkie/codec2",
"source": "local-adaptation",
"upstreamCommit": "24ec3b7c5b9892c268e180802ee3a5ce7737f214",
"crateChecksum": "3c3c8ba70382a99c0b5ebadf6ad50e7d98fb6eb9bcc4c0e30d422f6321798d99",
"provenance": "crates/lxst-codec2/UPSTREAM.json",
"notices": {
"crates/lxst-codec2/NOTICE": "d0b296649684d3c365a6afd5737fd7183ad3a7b6fa15057333390600f0f920e2",
"crates/lxst-codec2/licenses/KISS-FFT-BSD-3-Clause.txt": "49eea0111dfb7038dc18f74034cc517690395ebac8f7768f999133b16e4bf064",
"crates/lxst-codec2/licenses/LGPL-2.1.txt": "9ebb6f82b7380a62ac74c5f0322c88e6744dedf2ebe1f54d6f088282b39844bf",
"crates/lxst-codec2/licenses/upstream-Cargo.toml": "939e2818dcb2945f52eceb71b37b512bf1796da53a05e0a69276d5a2e55f9eed",
"crates/lxst-codec2/licenses/upstream-README.md": "76fee2b7d3455903f4246e7412ac230a856f58b098861c4c4c9dbdecd371f94a"
}
},
{
"name": "combine",
"version": "4.6.7",
@@ -297,6 +314,13 @@
"repository": "https://github.com/rust-lang/libc",
"source": "registry+https://github.com/rust-lang/crates.io-index"
},
{
"name": "libm",
"version": "0.2.16",
"license": "MIT",
"repository": "https://github.com/rust-lang/compiler-builtins",
"source": "registry+https://github.com/rust-lang/crates.io-index"
},
{
"name": "lock_api",
"version": "0.4.14",
+10 -1
View File
@@ -5,7 +5,7 @@ Third-party components retain their own licenses; the rsLXST license does not
replace those terms or notices.
[`THIRD_PARTY_LICENSES.json`](THIRD_PARTY_LICENSES.json) is the complete,
CI-checked inventory of registry third-party normal/build
CI-checked inventory of adapted Codec2 and registry third-party normal/build
dependencies reachable from the locked workspace across declared targets. It
excludes dev-only packages and first-party path packages. The inventory is
generated from Cargo metadata and `Cargo.lock` with:
@@ -31,3 +31,12 @@ heap-backed codec state. The preserved license and Opus patent notice are in
This inventory records source and license evidence; it is not legal advice and
does not by itself replace any third-party notice required for a distributed
binary or source bundle.
## Adapted Codec2 implementation
`crates/lxst-codec2` retains the pure-Rust codec2 0.3.1 implementation's
LGPL-2.1-only AND MIT declaration and embedded KISS FFT BSD-3-Clause notice.
Its [NOTICE](crates/lxst-codec2/NOTICE) and
[immutable provenance](crates/lxst-codec2/UPSTREAM.json) describe the bounded
storage, math and state-ownership modifications. This path dependency is
explicitly inventoried; it is not treated as original first-party DSP.
+14
View File
@@ -376,6 +376,12 @@ version = "0.2.189"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"
[[package]]
name = "libm"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
[[package]]
name = "lock_api"
version = "0.4.14"
@@ -391,6 +397,13 @@ version = "0.4.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"
[[package]]
name = "lxst-codec2"
version = "0.2.0"
dependencies = [
"libm",
]
[[package]]
name = "lxst-core"
version = "0.2.0"
@@ -435,6 +448,7 @@ dependencies = [
name = "lxst-telephony-api-fixture"
version = "0.0.0"
dependencies = [
"lxst-codec2",
"lxst-core",
"lxst-telephony",
"rns-identity",
+1
View File
@@ -7,6 +7,7 @@ publish = false
[workspace]
[dependencies]
lxst-codec2 = { path = "../../crates/lxst-codec2" }
lxst-core = { path = "../../crates/lxst-core" }
lxst-telephony = { path = "../../crates/lxst-telephony" }
rns-identity = { path = "../../../rsReticulum/crates/rns-identity" }
+18
View File
@@ -53,3 +53,21 @@ pub mod codec2 {
codec.decode_into(packet, output)
}
}
pub mod bounded_codec2 {
use lxst_codec2::{Codec, Error, Mode};
use std::mem::MaybeUninit;
pub fn initialise(storage: &mut MaybeUninit<Codec>, mode: Mode) -> &mut Codec {
Codec::initialise(storage, mode)
}
pub fn roundtrip(
codec: &mut Codec,
pcm: &[i16],
packet: &mut [u8],
output: &mut [i16],
) -> Result<(), Error> {
codec.encode(pcm, packet)?;
codec.decode(packet, output)
}
}
+32
View File
@@ -2,6 +2,38 @@
"schemaVersion": 1,
"scope": "library Cargo manifests, public features, targets, and non-dev dependencies",
"packages": [
{
"name": "lxst-codec2",
"version": "0.2.0",
"rustVersion": "1.87",
"edition": "2024",
"features": {},
"targets": [
{
"name": "lxst_codec2",
"kind": [
"lib"
],
"crateTypes": [
"lib"
],
"requiredFeatures": []
}
],
"dependencies": [
{
"name": "libm",
"rename": null,
"kind": null,
"target": null,
"requirement": "=0.2.16",
"optional": false,
"defaultFeatures": false,
"features": [],
"sourceKind": "registry"
}
]
},
{
"name": "lxst-core",
"version": "0.2.0",
+17 -2
View File
@@ -23,6 +23,7 @@
"capturedOn": "2026-10-01",
"review": {
"areas": [
"Allocation-free native 1600/3200 Codec2 state initialized in caller storage",
"Bounded fixed-profile Codec2 packet processing and caller-owned PCM/output buffers",
"Native Codec2 backend contract and failure-state ownership",
"Existing Opus and telephony APIs retained"
@@ -39,7 +40,7 @@
"TelephonyService::new",
"TelephonyService::with_config"
],
"classification": "additive experimental Codec2 backend boundary; no bundled native backend or telephony activation",
"classification": "additive experimental native Codec2 backend and packet boundary; no automatic telephony activation",
"publicApiDiff": {
"added": 33,
"removed": 0
@@ -52,11 +53,25 @@
"stable and Rust 1.87 strict Clippy",
"compiled external Codec2 consumer",
"24 actual Rust Codec2 packet-boundary cases against native frames and C reference data; codec quality and allocation qualification remain separate",
"separate Rust candidate no_std compile on pinned ESP32-S3 toolchain; no on-device runtime claim"
"separate Rust candidate no_std compile on pinned ESP32-S3 toolchain; no on-device runtime claim",
"Native zero-allocation contract, independent duplex histories and 20000 arbitrary decoder frames",
"12 bounded backend/C reference packet cases, exact locked ESP32-S3 compiler build and stable/MSRV strict Clippy"
],
"newPackages": [
"lxst-codec2"
]
}
},
"packages": [
{
"name": "lxst-codec2",
"manifestPath": "Cargo.toml",
"tier": "experimental",
"compatibility": "reviewed-snapshot",
"snapshot": "api/snapshots/lxst-codec2.txt",
"itemCount": 0,
"sha256": "0000000000000000000000000000000000000000000000000000000000000000"
},
{
"name": "lxst-core",
"manifestPath": "Cargo.toml",
+16
View File
@@ -0,0 +1,16 @@
[package]
name = "lxst-codec2"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
publish.workspace = true
license = "LGPL-2.1-only AND MIT AND BSD-3-Clause"
description = "Bounded no_std Codec2 speech backend for LXST"
[dependencies]
libm = { version = "=0.2.16", default-features = false }
[[test]]
name = "contract"
harness = false
+18
View File
@@ -0,0 +1,18 @@
This is a modified version of the pure-Rust codec2 0.3.1 implementation by
David Rowe and Matt Weeks, https://github.com/scriptjunkie/codec2.
The original manifest declares LGPL-2.1-only AND MIT. The original package
and pinned repository have no separate MIT license file; the original manifest
and README are retained under licenses/. Do not erase the upstream declaration.
Codec2's LGPL-2.1 text and the embedded KISS FFT BSD-3-Clause notice are retained
there as well. Original author/function notices remain in the adapted sources.
Modified for rsLXST on 2026-10-01: bounded storage and scratch; in-place state
initialisation; per-instance synthesis RNG; exact libm math on all targets;
checked public frame shapes; corrected real-FFT input copying. The public API
exposes only native 1600/3200 modes at 8 kHz mono. Other private upstream modes
are not a supported API. See UPSTREAM.json for immutable original provenance.
New wrapper, tests and storage adaptation are provided under LGPL-2.1-only.
Third-party libm retains its own MIT license and appears in the workspace's
locked third-party inventory. Packaging must retain the applicable notices and
provide the corresponding modified Codec2 source and relinking materials.
+24
View File
@@ -0,0 +1,24 @@
# Bounded Rust Codec2
Allocation-free `no_std` native 1600/3200 bit/s speech at mono 8 kHz PCM16.
This is the pinned Rust Codec2 port with fixed storage, per-instance synthesis
randomness and pure-Rust `libm` math. It has no allocator dependency.
Allocate `MaybeUninit<Codec>` in final aligned storage, then call
`Codec::initialise`. Do not construct a codec temporary on a small task stack.
One serialized owner can alternate encoding and decoding; predictor histories
are independent. Independent sessions require separate states. `reset` erases
histories in place. Native input/output frame lengths must match `Mode` exactly;
length errors do not advance state or modify output.
LXST framing and negotiation belong to `lxst-core`, not this backend. 700C,
Opus, memo containers, audio drivers and real-time target qualification are not
provided here. Codec state plus worst-case nested DSP stack and I/O buffers must
fit the selected device budget. A target build is not a timing/stack measurement.
`cargo test -p lxst-codec2 --release` checks allocation-free initialization,
reset and streaming; rejected inputs; independent duplex histories; hostile
correctly-sized decoder frames; and the real-FFT second-half input regression.
See [NOTICE](NOTICE), [UPSTREAM.json](UPSTREAM.json) and [licenses](licenses/)
for source provenance, modifications and original license notices.
+19
View File
@@ -0,0 +1,19 @@
{
"upstream_crate": "codec2",
"version": "0.3.1",
"repository": "https://github.com/scriptjunkie/codec2",
"commit": "24ec3b7c5b9892c268e180802ee3a5ce7737f214",
"crate_sha256": "3c3c8ba70382a99c0b5ebadf6ad50e7d98fb6eb9bcc4c0e30d422f6321798d99",
"declared_license": "LGPL-2.1-only AND MIT",
"additional_embedded_notice": "BSD-3-Clause KISS FFT notice preserved in src/native/kiss_fft.rs",
"original_source_sha256": {
"kiss_fft.rs": "33f9a3f1c2caddaa8e7536d655fd0c20f85d9dd78224f5e117a2c5bec1ca5671",
"codebookd.rs": "5527f6217500f303a551a6e6f2425cf7ea1abe95e43093f73eea467a45c7ba08",
"codebook.rs": "8de6ea51a391982d26a724c3b7e8ef2e6fefed066f295e7f4a745a6aceb0a6d2",
"lib.rs": "5a468ed1b26854be5f1992a215959151e7d3435fe154d212b2c468e4f10744bb",
"codebookge.rs": "0d36228e468f74eb65bbcbbadb9cb66a599466f347f1b7ee436abdb29d4c4378",
"quantise.rs": "f1e831fddee5a657e4139ac0808339cede5f5a8b6e9784eda600f3a714bb81bb",
"codebookjvm.rs": "6a5aba7b9be606b2d7b16054e897d8862f3bd4ec24fa3f78ff5659b361bd7266",
"nlp.rs": "d86f14abc7ce709d1a36a5b4dce3a2bab30ed5999a04548b61548c433897a0c6"
}
}
@@ -0,0 +1,11 @@
Copyright (c) 2003-2010, Mark Borgerding
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+502
View File
@@ -0,0 +1,502 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 2.1, February 1999
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the Lesser GPL. It also counts
as the successor of the GNU Library Public License, version 2, hence
the version number 2.1.]
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This license, the Lesser General Public License, applies to some
specially designated software packages--typically libraries--of the
Free Software Foundation and other authors who decide to use it. You
can use it too, but we suggest you first think carefully about whether
this license or the ordinary General Public License is the better
strategy to use in any particular case, based on the explanations below.
When we speak of free software, we are referring to freedom of use,
not price. Our General Public Licenses are designed to make sure that
you have the freedom to distribute copies of free software (and charge
for this service if you wish); that you receive source code or can get
it if you want it; that you can change the software and use pieces of
it in new free programs; and that you are informed that you can do
these things.
To protect your rights, we need to make restrictions that forbid
distributors to deny you these rights or to ask you to surrender these
rights. These restrictions translate to certain responsibilities for
you if you distribute copies of the library or if you modify it.
For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you. You must make sure that they, too, receive or can get the source
code. If you link other code with the library, you must provide
complete object files to the recipients, so that they can relink them
with the library after making changes to the library and recompiling
it. And you must show them these terms so they know their rights.
We protect your rights with a two-step method: (1) we copyright the
library, and (2) we offer you this license, which gives you legal
permission to copy, distribute and/or modify the library.
To protect each distributor, we want to make it very clear that
there is no warranty for the free library. Also, if the library is
modified by someone else and passed on, the recipients should know
that what they have is not the original version, so that the original
author's reputation will not be affected by problems that might be
introduced by others.
Finally, software patents pose a constant threat to the existence of
any free program. We wish to make sure that a company cannot
effectively restrict the users of a free program by obtaining a
restrictive license from a patent holder. Therefore, we insist that
any patent license obtained for a version of the library must be
consistent with the full freedom of use specified in this license.
Most GNU software, including some libraries, is covered by the
ordinary GNU General Public License. This license, the GNU Lesser
General Public License, applies to certain designated libraries, and
is quite different from the ordinary General Public License. We use
this license for certain libraries in order to permit linking those
libraries into non-free programs.
When a program is linked with a library, whether statically or using
a shared library, the combination of the two is legally speaking a
combined work, a derivative of the original library. The ordinary
General Public License therefore permits such linking only if the
entire combination fits its criteria of freedom. The Lesser General
Public License permits more lax criteria for linking other code with
the library.
We call this license the "Lesser" General Public License because it
does Less to protect the user's freedom than the ordinary General
Public License. It also provides other free software developers Less
of an advantage over competing non-free programs. These disadvantages
are the reason we use the ordinary General Public License for many
libraries. However, the Lesser license provides advantages in certain
special circumstances.
For example, on rare occasions, there may be a special need to
encourage the widest possible use of a certain library, so that it becomes
a de-facto standard. To achieve this, non-free programs must be
allowed to use the library. A more frequent case is that a free
library does the same job as widely used non-free libraries. In this
case, there is little to gain by limiting the free library to free
software only, so we use the Lesser General Public License.
In other cases, permission to use a particular library in non-free
programs enables a greater number of people to use a large body of
free software. For example, permission to use the GNU C Library in
non-free programs enables many more people to use the whole GNU
operating system, as well as its variant, the GNU/Linux operating
system.
Although the Lesser General Public License is Less protective of the
users' freedom, it does ensure that the user of a program that is
linked with the Library has the freedom and the wherewithal to run
that program using a modified version of the Library.
The precise terms and conditions for copying, distribution and
modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, whereas the latter must
be combined with the library in order to run.
GNU LESSER GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any software library or other
program which contains a notice placed by the copyright holder or
other authorized party saying it may be distributed under the terms of
this Lesser General Public License (also called "this License").
Each licensee is addressed as "you".
A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.
The "Library", below, refers to any such software library or work
which has been distributed under these terms. A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language. (Hereinafter, translation is
included without limitation in the term "modification".)
"Source code" for a work means the preferred form of the work for
making modifications to it. For a library, complete source code means
all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation
and installation of the library.
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it). Whether that is true depends on what the Library does
and what the program that uses the Library does.
1. You may copy and distribute verbatim copies of the Library's
complete source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact
all the notices that refer to this License and to the absence of any
warranty; and distribute a copy of this License along with the
Library.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.
2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices
stating that you changed the files and the date of any change.
c) You must cause the whole of the work to be licensed at no
charge to all third parties under the terms of this License.
d) If a facility in the modified Library refers to a function or a
table of data to be supplied by an application program that uses
the facility, other than as an argument passed when the facility
is invoked, then you must make a good faith effort to ensure that,
in the event an application does not supply such function or
table, the facility still operates, and performs whatever part of
its purpose remains meaningful.
(For example, a function in a library to compute square roots has
a purpose that is entirely well-defined independent of the
application. Therefore, Subsection 2d requires that any
application-supplied function or table used by this function must
be optional: if the application does not supply it, the square
root function must still compute square roots.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Library,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Library, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Library.
In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library. To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.
Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you accompany
it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange.
If distribution of object code is made by offering access to copy
from a designated place, then offering equivalent access to copy the
source code from the same place satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.
5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library". Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.
However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library". The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.
When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library. The
threshold for this to be true is not precisely defined by law.
If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work. (Executables containing this object code plus portions of the
Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also combine or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.
You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License. You must supply a copy of this License. If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License. Also, you must do one
of these things:
a) Accompany the work with the complete corresponding
machine-readable source code for the Library including whatever
changes were used in the work (which must be distributed under
Sections 1 and 2 above); and, if the work is an executable linked
with the Library, with the complete machine-readable "work that
uses the Library", as object code and/or source code, so that the
user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood
that the user who changes the contents of definitions files in the
Library will not necessarily be able to recompile the application
to use the modified definitions.)
b) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (1) uses at run time a
copy of the library already present on the user's computer system,
rather than copying library functions into the executable, and (2)
will operate properly with a modified version of the library, if
the user installs one, as long as the modified version is
interface-compatible with the version that the work was made with.
c) Accompany the work with a written offer, valid for at
least three years, to give the same user the materials
specified in Subsection 6a, above, for a charge no more
than the cost of performing this distribution.
d) If distribution of the work is made by offering access to copy
from a designated place, offer equivalent access to copy the above
specified materials from the same place.
e) Verify that the user has already received a copy of these
materials or that you have already sent this user a copy.
For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it. However, as a special exception,
the materials to be distributed need not include anything that is
normally distributed (in either source or binary form) with the major
components (compiler, kernel, and so on) of the operating system on
which the executable runs, unless that component itself accompanies
the executable.
It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.
7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:
a) Accompany the combined library with a copy of the same work
based on the Library, uncombined with any other library
facilities. This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined library of the fact
that part of it is a work based on the Library, and explaining
where to find the accompanying uncombined form of the same work.
8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License. Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library is void, and will automatically terminate your
rights under this License. However, parties who have received copies,
or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
9. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Library or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
subject to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties with
this License.
11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Library at all. For example, if a patent
license would not permit royalty-free redistribution of the Library by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Library.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply,
and the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library under this License may add
an explicit geographical distribution limitation excluding those countries,
so that distribution is permitted only in or among countries not thus
excluded. In such case, this License incorporates the limitation as if
written in the body of this License.
13. The Free Software Foundation may publish revised and/or new
versions of the Lesser General Public License from time to time.
Such new versions will be similar in spirit to the present version,
but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission. For software which is
copyrighted by the Free Software Foundation, write to the Free
Software Foundation; we sometimes make exceptions for this. Our
decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing
and reuse of software generally.
NO WARRANTY
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Libraries
If you develop a new library, and you want it to be of the greatest
possible use to the public, we recommend making it free software that
everyone can redistribute and change. You can do so by permitting
redistribution under these terms (or, alternatively, under the terms of the
ordinary General Public License).
To apply these terms, attach the following notices to the library. It is
safest to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the library's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see
<http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the library, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
library `Frob' (a library for tweaking knobs) written by James Random Hacker.
<signature of Ty Coon>, 1 April 1990
Ty Coon, President of Vice
That's all there is to it!
@@ -0,0 +1,20 @@
[package]
name = "codec2"
version = "0.3.1"
description = "A pure-Rust implementation of the Codec2 low-bitrate speech compression codec"
authors = ["David Rowe", "Matt Weeks <scriptjunkie@scriptjunkie.us>"]
license = "LGPL-2.1-only AND MIT"
edition = "2018"
repository = "https://github.com/scriptjunkie/codec2"
keywords = ["audio", "speech", "compression", "codec"]
categories = ["compression", "multimedia::audio", "multimedia::encoding", "algorithms"]
readme = "README.md"
[dependencies]
micromath = {version = "2.1.0", optional = true}
[dev-dependencies]
zerocopy = "0.3.0"
[features]
micromath = ["dep:micromath"]
@@ -0,0 +1,61 @@
# Codec2
Add this to your `Cargo.toml`:
```toml
[dependencies]
codec2 = "0.3"
```
## Usage
Currently 3200, 2400, 1600, 1400, 1300 and 1200 bitrates encoding and decoding are implemented.
Create a Codec2 object e.g. `Codec2::new(Codec2Mode::MODE_3200)` then repeatedly obtain raw 8khz
16-bit audio samples and call `encode` to encode blocks of `samples_per_frame()` samples into
`bits_per_frame()` compressed bits, in order. On the receiving end, create a Codec2 object and
repeatedly call `decode` to decompress each chunk of received bytes into the next samples.
Complete example below. This example uses zerocopy to interpret the `&[i16]` slices as `&[u8]` for
I/O.
Cargo.toml:
```toml
[dependencies]
zerocopy = "0.3.0"
codec2 = "0"
```
main.rs:
```rust
use codec2::*;
use std::env::args;
use std::io::prelude::*;
use zerocopy::AsBytes;
fn main() -> std::io::Result<()> {
if args().len() != 4 || (args().nth(1).unwrap() != "enc" && args().nth(1).unwrap() != "dec") {
eprintln!("Usage: {} (enc|dec) inputfile outputfilename", args().nth(0).unwrap());
eprintln!("Files should be raw 16-bit signed 8khz audio");
return Ok(());
}
let mut fin = std::fs::File::open(args().nth(2).unwrap())?;
let mut fout = std::fs::File::create(args().nth(3).unwrap())?;
let mut c2 = Codec2::new(Codec2Mode::MODE_3200);
let mut samps = vec![0; c2.samples_per_frame()]; //i16 I/O buffer
let mut packed = vec![0; (c2.bits_per_frame() + 7) / 8]; //u8 I/O buffer for encoded bits
if args().nth(1).unwrap() == "enc" {
while fin.read_exact(samps.as_bytes_mut()).is_ok() {
c2.encode(&mut packed, &samps[..]);
fout.write_all(&packed)?;
}
} else {
while fin.read_exact(&mut packed).is_ok() {
c2.decode(&mut samps[..], &packed);
fout.write_all(samps.as_bytes())?;
}
}
Ok(())
}
```
+38
View File
@@ -0,0 +1,38 @@
use core::ops::{Deref, DerefMut};
/// Private fixed storage preserving the upstream DSP's slice lengths.
/// Zero initialisation is valid: a zero length exposes no uninitialised data.
#[derive(Clone, Debug)]
pub(crate) struct Buffer<T: Copy, const N: usize> {
length: usize,
values: [T; N],
}
impl<T: Copy, const N: usize> Buffer<T, N> {
pub(crate) fn filled(length: usize, value: T) -> Self {
assert!(length <= N);
Self {
length,
values: [value; N],
}
}
pub(crate) fn initialise(&mut self, length: usize, value: T) {
assert!(length <= N);
self.length = length;
self.values[..length].fill(value);
}
}
impl<T: Copy, const N: usize> Deref for Buffer<T, N> {
type Target = [T];
fn deref(&self) -> &[T] {
&self.values[..self.length]
}
}
impl<T: Copy, const N: usize> DerefMut for Buffer<T, N> {
fn deref_mut(&mut self) -> &mut [T] {
&mut self.values[..self.length]
}
}
+119
View File
@@ -0,0 +1,119 @@
//! Fixed-storage, allocation-free Codec2 at 8 kHz mono PCM16.
//!
//! Adapted from the pinned pure-Rust Codec2 port recorded in `UPSTREAM.json`.
//! Initialise directly in caller-owned storage; the state is too large to create
//! as a temporary on an MCU task stack. Encoding/decoding and reset never allocate.
#![no_std]
mod buffer;
mod math;
use buffer::Buffer;
use math::FloatMath;
// Preserve the upstream DSP's names, numeric constants and indexing style so
// algorithm changes remain reviewable separately from the storage adaptation.
#[allow(
non_snake_case,
non_camel_case_types,
non_upper_case_globals,
dead_code
)]
#[allow(clippy::all)]
mod native;
use core::mem::MaybeUninit;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Mode {
Rate1600,
Rate3200,
}
impl Mode {
pub const fn samples(self) -> usize {
match self {
Self::Rate1600 => 320,
Self::Rate3200 => 160,
}
}
pub const fn bytes(self) -> usize {
8
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
PcmLength,
EncodedLength,
}
/// Independent encoder and decoder histories at one native mode.
///
/// One serialized audio owner may alternate transmit and receive with this
/// instance; FFT scratch is reused and the histories remain independent. It
/// must never be accessed concurrently. Independent sessions need independent
/// instances. `Mode` is a codec configuration, not an LXST/LXMF wire identifier.
pub struct Codec {
native: native::Codec2,
}
impl Codec {
/// Initialise in final, aligned caller storage without a whole-state copy.
pub fn initialise(storage: &mut MaybeUninit<Self>, mode: Mode) -> &mut Self {
// SAFETY: all native fields are integers, floats, arrays, fixed buffers
// (zero length is valid), or Codec2Mode (repr(u8), first variant is 0).
// There are no references, pointers, heap owners or Drop implementations.
// Thus zero bytes form a valid value. We finish logical initialisation
// before publishing it. The exclusive borrow prevents concurrent access.
let value = unsafe {
storage.as_mut_ptr().write_bytes(0, 1);
storage.assume_init_mut()
};
value.initialise_native(mode);
value
}
fn initialise_native(&mut self, mode: Mode) {
self.native.initialise(match mode {
Mode::Rate1600 => native::Codec2Mode::MODE_1600,
Mode::Rate3200 => native::Codec2Mode::MODE_3200,
});
}
pub fn reset(&mut self, mode: Mode) {
// SAFETY: same zero-valid representation as initialise; this type owns
// no resources requiring Drop and self is exclusively borrowed.
unsafe { core::ptr::write_bytes(self, 0, 1) };
self.initialise_native(mode);
}
pub fn mode(&self) -> Mode {
match self.native.samples_per_frame() {
320 => Mode::Rate1600,
_ => Mode::Rate3200,
}
}
pub fn encode(&mut self, pcm: &[i16], output: &mut [u8]) -> Result<(), Error> {
if pcm.len() != self.mode().samples() {
return Err(Error::PcmLength);
}
if output.len() != self.mode().bytes() {
return Err(Error::EncodedLength);
}
self.native.encode(output, pcm);
Ok(())
}
pub fn decode(&mut self, encoded: &[u8], output: &mut [i16]) -> Result<(), Error> {
if encoded.len() != self.mode().bytes() {
return Err(Error::EncodedLength);
}
if output.len() != self.mode().samples() {
return Err(Error::PcmLength);
}
self.native.decode(output, encoded);
Ok(())
}
}
+47
View File
@@ -0,0 +1,47 @@
// Same pure-Rust libm functions on host and MCU. Do not replace with approximate
// intrinsics without comparing stateful speech and target execution budgets.
pub(crate) trait FloatMath {
fn dsp_acos(self) -> Self;
fn dsp_atan2(self, other: Self) -> Self;
fn dsp_ceil(self) -> Self;
fn dsp_cos(self) -> Self;
fn dsp_floor(self) -> Self;
fn dsp_log10(self) -> Self;
fn dsp_powf(self, other: Self) -> Self;
fn dsp_round(self) -> Self;
fn dsp_sin(self) -> Self;
fn dsp_sqrt(self) -> Self;
}
impl FloatMath for f32 {
fn dsp_acos(self) -> Self {
libm::acosf(self)
}
fn dsp_atan2(self, other: Self) -> Self {
libm::atan2f(self, other)
}
fn dsp_ceil(self) -> Self {
libm::ceilf(self)
}
fn dsp_cos(self) -> Self {
libm::cosf(self)
}
fn dsp_floor(self) -> Self {
libm::floorf(self)
}
fn dsp_log10(self) -> Self {
libm::log10f(self)
}
fn dsp_powf(self, other: Self) -> Self {
libm::powf(self, other)
}
fn dsp_round(self) -> Self {
libm::roundf(self)
}
fn dsp_sin(self) -> Self {
libm::sinf(self)
}
fn dsp_sqrt(self) -> Self {
libm::sqrtf(self)
}
}
+127
View File
@@ -0,0 +1,127 @@
use super::*;
/* THIS IS A GENERATED FILE. Edit generate_codebook.c and its input */
/*
* This intermediary file and the files that used to create it are under
* The LGPL. See the file COPYING.
*/
/* codebook/lsp1.txt */
const codes0: [f32; 16] = [
225.0, 250.0, 275.0, 300.0, 325.0, 350.0, 375.0, 400.0, 425.0, 450.0, 475.0, 500.0, 525.0,
550.0, 575.0, 600.0,
];
/* codebook/lsp2.txt */
const codes1: [f32; 16] = [
325.0, 350.0, 375.0, 400.0, 425.0, 450.0, 475.0, 500.0, 525.0, 550.0, 575.0, 600.0, 625.0,
650.0, 675.0, 700.0,
];
/* codebook/lsp3.txt */
const codes2: [f32; 16] = [
500.0, 550.0, 600.0, 650.0, 700.0, 750.0, 800.0, 850.0, 900.0, 950.0, 1000.0, 1050.0, 1100.0,
1150.0, 1200.0, 1250.0,
];
/* codebook/lsp4.txt */
const codes3: [f32; 16] = [
700.0, 800.0, 900.0, 1000.0, 1100.0, 1200.0, 1300.0, 1400.0, 1500.0, 1600.0, 1700.0, 1800.0,
1900.0, 2000.0, 2100.0, 2200.0,
];
/* codebook/lsp5.txt */
const codes4: [f32; 16] = [
950.0, 1050.0, 1150.0, 1250.0, 1350.0, 1450.0, 1550.0, 1650.0, 1750.0, 1850.0, 1950.0, 2050.0,
2150.0, 2250.0, 2350.0, 2450.0,
];
/* codebook/lsp6.txt */
const codes5: [f32; 16] = [
1100.0, 1200.0, 1300.0, 1400.0, 1500.0, 1600.0, 1700.0, 1800.0, 1900.0, 2000.0, 2100.0, 2200.0,
2300.0, 2400.0, 2500.0, 2600.0,
];
/* codebook/lsp7.txt */
const codes6: [f32; 16] = [
1500.0, 1600.0, 1700.0, 1800.0, 1900.0, 2000.0, 2100.0, 2200.0, 2300.0, 2400.0, 2500.0, 2600.0,
2700.0, 2800.0, 2900.0, 3000.0,
];
/* codebook/lsp8.txt */
const codes7: [f32; 8] = [
2300.0, 2400.0, 2500.0, 2600.0, 2700.0, 2800.0, 2900.0, 3000.0,
];
/* codebook/lsp9.txt */
const codes8: [f32; 8] = [
2500.0, 2600.0, 2700.0, 2800.0, 2900.0, 3000.0, 3100.0, 3200.0,
];
/* codebook/lsp10.txt */
const codes9: [f32; 4] = [2900.0, 3100.0, 3300.0, 3500.0];
pub const lsp_cb: [lsp_codebook; 10] = [
/* codebook/lsp1.txt */
lsp_codebook {
k: 1,
log2m: 4,
m: 16,
cb: &codes0,
},
/* codebook/lsp2.txt */
lsp_codebook {
k: 1,
log2m: 4,
m: 16,
cb: &codes1,
},
/* codebook/lsp3.txt */
lsp_codebook {
k: 1,
log2m: 4,
m: 16,
cb: &codes2,
},
/* codebook/lsp4.txt */
lsp_codebook {
k: 1,
log2m: 4,
m: 16,
cb: &codes3,
},
/* codebook/lsp5.txt */
lsp_codebook {
k: 1,
log2m: 4,
m: 16,
cb: &codes4,
},
/* codebook/lsp6.txt */
lsp_codebook {
k: 1,
log2m: 4,
m: 16,
cb: &codes5,
},
/* codebook/lsp7.txt */
lsp_codebook {
k: 1,
log2m: 4,
m: 16,
cb: &codes6,
},
/* codebook/lsp8.txt */
lsp_codebook {
k: 1,
log2m: 3,
m: 8,
cb: &codes7,
},
/* codebook/lsp9.txt */
lsp_codebook {
k: 1,
log2m: 3,
m: 8,
cb: &codes8,
},
/* codebook/lsp10.txt */
lsp_codebook {
k: 1,
log2m: 2,
m: 4,
cb: &codes9,
},
];
+143
View File
@@ -0,0 +1,143 @@
use super::*;
/* codebook/dlsp1.txt */
const codes0: [f32; 32] = [
25.0, 50.0, 75.0, 100.0, 125.0, 150.0, 175.0, 200.0, 225.0, 250.0, 275.0, 300.0, 325.0, 350.0,
375.0, 400.0, 425.0, 450.0, 475.0, 500.0, 525.0, 550.0, 575.0, 600.0, 625.0, 650.0, 675.0,
700.0, 725.0, 750.0, 775.0, 800.0,
];
/* codebook/dlsp2.txt */
const codes1: [f32; 32] = [
25.0, 50.0, 75.0, 100.0, 125.0, 150.0, 175.0, 200.0, 225.0, 250.0, 275.0, 300.0, 325.0, 350.0,
375.0, 400.0, 425.0, 450.0, 475.0, 500.0, 525.0, 550.0, 575.0, 600.0, 625.0, 650.0, 675.0,
700.0, 725.0, 750.0, 775.0, 800.0,
];
/* codebook/dlsp3.txt */
const codes2: [f32; 32] = [
25.0, 50.0, 75.0, 100.0, 125.0, 150.0, 175.0, 200.0, 225.0, 250.0, 275.0, 300.0, 325.0, 350.0,
375.0, 400.0, 425.0, 450.0, 475.0, 500.0, 525.0, 550.0, 575.0, 600.0, 625.0, 650.0, 675.0,
700.0, 725.0, 750.0, 775.0, 800.0,
];
/* codebook/dlsp4.txt */
const codes3: [f32; 32] = [
25.0, 50.0, 75.0, 100.0, 125.0, 150.0, 175.0, 200.0, 250.0, 300.0, 350.0, 400.0, 450.0, 500.0,
550.0, 600.0, 650.0, 700.0, 750.0, 800.0, 850.0, 900.0, 950.0, 1000.0, 1050.0, 1100.0, 1150.0,
1200.0, 1250.0, 1300.0, 1350.0, 1400.0,
];
/* codebook/dlsp5.txt */
const codes4: [f32; 32] = [
25.0, 50.0, 75.0, 100.0, 125.0, 150.0, 175.0, 200.0, 250.0, 300.0, 350.0, 400.0, 450.0, 500.0,
550.0, 600.0, 650.0, 700.0, 750.0, 800.0, 850.0, 900.0, 950.0, 1000.0, 1050.0, 1100.0, 1150.0,
1200.0, 1250.0, 1300.0, 1350.0, 1400.0,
];
/* codebook/dlsp6.txt */
const codes5: [f32; 32] = [
25.0, 50.0, 75.0, 100.0, 125.0, 150.0, 175.0, 200.0, 250.0, 300.0, 350.0, 400.0, 450.0, 500.0,
550.0, 600.0, 650.0, 700.0, 750.0, 800.0, 850.0, 900.0, 950.0, 1000.0, 1050.0, 1100.0, 1150.0,
1200.0, 1250.0, 1300.0, 1350.0, 1400.0,
];
/* codebook/dlsp7.txt */
const codes6: [f32; 32] = [
25.0, 50.0, 75.0, 100.0, 125.0, 150.0, 175.0, 200.0, 225.0, 250.0, 275.0, 300.0, 325.0, 350.0,
375.0, 400.0, 425.0, 450.0, 475.0, 500.0, 525.0, 550.0, 575.0, 600.0, 625.0, 650.0, 675.0,
700.0, 725.0, 750.0, 775.0, 800.0,
];
/* codebook/dlsp8.txt */
const codes7: [f32; 32] = [
25.0, 50.0, 75.0, 100.0, 125.0, 150.0, 175.0, 200.0, 225.0, 250.0, 275.0, 300.0, 325.0, 350.0,
375.0, 400.0, 425.0, 450.0, 475.0, 500.0, 525.0, 550.0, 575.0, 600.0, 625.0, 650.0, 675.0,
700.0, 725.0, 750.0, 775.0, 800.0,
];
/* codebook/dlsp9.txt */
const codes8: [f32; 32] = [
25.0, 50.0, 75.0, 100.0, 125.0, 150.0, 175.0, 200.0, 225.0, 250.0, 275.0, 300.0, 325.0, 350.0,
375.0, 400.0, 425.0, 450.0, 475.0, 500.0, 525.0, 550.0, 575.0, 600.0, 625.0, 650.0, 675.0,
700.0, 725.0, 750.0, 775.0, 800.0,
];
/* codebook/dlsp10.txt */
const codes9: [f32; 32] = [
25.0, 50.0, 75.0, 100.0, 125.0, 150.0, 175.0, 200.0, 225.0, 250.0, 275.0, 300.0, 325.0, 350.0,
375.0, 400.0, 425.0, 450.0, 475.0, 500.0, 525.0, 550.0, 575.0, 600.0, 625.0, 650.0, 675.0,
700.0, 725.0, 750.0, 775.0, 800.0,
];
const codesnone: [f32; 0] = [];
pub const lsp_cbd: [lsp_codebook; 11] = [
/* codebook/dlsp1.txt */
lsp_codebook {
k: 1,
log2m: 5,
m: 32,
cb: &codes0,
},
/* codebook/dlsp2.txt */
lsp_codebook {
k: 1,
log2m: 5,
m: 32,
cb: &codes1,
},
/* codebook/dlsp3.txt */
lsp_codebook {
k: 1,
log2m: 5,
m: 32,
cb: &codes2,
},
/* codebook/dlsp4.txt */
lsp_codebook {
k: 1,
log2m: 5,
m: 32,
cb: &codes3,
},
/* codebook/dlsp5.txt */
lsp_codebook {
k: 1,
log2m: 5,
m: 32,
cb: &codes4,
},
/* codebook/dlsp6.txt */
lsp_codebook {
k: 1,
log2m: 5,
m: 32,
cb: &codes5,
},
/* codebook/dlsp7.txt */
lsp_codebook {
k: 1,
log2m: 5,
m: 32,
cb: &codes6,
},
/* codebook/dlsp8.txt */
lsp_codebook {
k: 1,
log2m: 5,
m: 32,
cb: &codes7,
},
/* codebook/dlsp9.txt */
lsp_codebook {
k: 1,
log2m: 5,
m: 32,
cb: &codes8,
},
/* codebook/dlsp10.txt */
lsp_codebook {
k: 1,
log2m: 5,
m: 32,
cb: &codes9,
},
lsp_codebook {
k: 0,
log2m: 0,
m: 0,
cb: &codesnone,
},
];
@@ -0,0 +1,72 @@
/* THIS IS A GENERATED FILE. Edit generate_codebook.c and its input */
/*
* This intermediary file and the files that used to create it are under
* The LGPL. See the file COPYING.
*/
use super::*;
// codebook/gecb.txt
const codes0: [f32; 512] = [
2.71, 12.0184, 0.04675, -2.73881, 0.120993, 8.38895, -1.58028, -0.892307, 1.19307, -1.91561,
0.187101, -3.27679, 0.332251, -7.66455, -1.47944, 31.2461, 1.52761, 27.7095, -0.524379,
5.25012, 0.55333, 7.4388, -0.843451, -1.95299, 2.26389, 8.61029, 0.143143, 2.36549, 0.616506,
1.28427, -1.71133, 22.0967, 1.00813, 17.3965, -0.106718, 1.41891, -0.136246, 14.2736, -1.70909,
-20.5319, 1.65787, -3.39107, 0.138049, -4.95785, 0.536729, -1.94375, 0.196307, 36.8519,
1.27248, 22.5565, -0.670219, -1.90604, 0.382092, 6.40113, -0.756911, -4.90102, 1.82931, 4.6138,
0.318794, 0.73683, 0.612815, -2.07505, -0.410151, 24.7871, 1.77602, 13.1909, 0.106457,
-0.104492, 0.192206, 10.1838, -1.82442, -7.71565, 0.931346, 4.34835, 0.308813, -4.086,
0.397143, -11.8089, -0.048715, 41.2273, 0.877342, 35.8503, -0.759794, 0.476634, 0.978593,
7.67467, -1.19506, 3.03883, 2.63989, -3.41106, 0.191127, 3.60351, 0.402932, 1.0843, -2.15202,
18.1076, 1.5468, 8.32271, -0.143089, -4.07592, -0.150142, 5.86674, -1.40844, -3.2507, 1.56615,
-10.4132, 0.178171, -10.2267, 0.362164, -0.028556, -0.070125, 24.3907, 0.594752, 17.4828,
-0.28698, -6.90407, 0.464818, 10.2055, -1.00684, -14.3572, 2.32957, -3.69161, 0.335745,
2.40714, 1.01966, -3.15565, -1.25945, 7.9919, 2.38369, 19.6806, -0.094947, -2.41374, 0.20933,
6.66477, -2.22103, 1.37986, 1.29239, 2.04633, 0.243626, -0.890741, 0.428773, -7.19366,
-1.11374, 41.3414, 2.6098, 31.1405, -0.446468, 2.53419, 0.490104, 4.62757, -1.11723, -3.24174,
1.79156, 8.41493, 0.156012, 0.183336, 0.532447, 3.15455, -0.764484, 18.514, 0.952395, 11.7713,
-0.332567, 0.346987, 0.202165, 14.7168, -2.12924, -15.559, 1.35358, -1.92679, -0.010963,
-16.3364, 0.399053, -2.79057, 0.750657, 31.1483, 0.655743, 24.4819, -0.45321, -0.735879,
0.2869, 6.5467, -0.715673, -12.3578, 1.54849, 3.87217, 0.271874, 0.802339, 0.502073, -4.85485,
-0.497037, 17.7619, 1.19116, 13.9544, 0.01563, 1.33157, 0.341867, 8.93537, -2.31601, -5.39506,
0.75861, 1.9645, 0.24132, -3.23769, 0.267151, -11.2344, -0.273126, 32.6248, 1.75352, 40.432,
-0.784011, 3.04576, 0.705987, 5.66118, -1.3864, 1.35356, 2.37646, 1.67485, 0.242973, 4.73218,
0.491227, 0.354061, -1.60676, 8.65895, 1.16711, 5.9871, -0.137601, -12.0417, -0.251375,
10.3972, -1.43151, -8.90411, 0.98828, -13.209, 0.261484, -6.35497, 0.395932, -0.702529,
0.283704, 26.8996, 0.420959, 15.4418, -0.355804, -13.7278, 0.527372, 12.3985, -1.16956,
-15.9985, 1.90669, -5.81605, 0.354492, 3.85157, 0.82576, -4.16264, -0.49019, 13.0572, 2.25577,
13.5264, -0.004956, -3.23713, 0.026709, 7.86645, -1.81037, -0.451183, 1.08383, -0.18362,
0.135836, -2.26658, 0.375812, -5.51225, -1.96644, 38.6829, 1.97799, 24.5655, -0.704656,
6.35881, 0.480786, 7.05175, -0.976417, -2.42273, 2.50215, 6.75935, 0.083588, 3.2588, 0.543629,
0.910013, -1.23196, 23.0915, 0.785492, 14.807, -0.213554, 1.688, 0.004748, 18.1718, -1.54719,
-16.1168, 1.50104, -3.28114, 0.080133, -4.63472, 0.476592, -2.18093, 0.44247, 40.304, 1.07277,
27.592, -0.594738, -4.16681, 0.42248, 7.61609, -0.927521, -7.27441, 1.99162, 1.29636, 0.291307,
2.39878, 0.721081, -1.95062, -0.804256, 24.9295, 1.64839, 19.1197, 0.060852, -0.590639,
0.266085, 9.10325, -1.9574, -2.88461, 1.11693, 2.6724, 0.35458, -2.74854, 0.330733, -14.1561,
-0.527851, 39.5756, 0.991152, 43.195, -0.589619, 1.26919, 0.787401, 8.73071, -1.0138, 1.02507,
2.8254, 1.89538, 0.24089, 2.74557, 0.427195, 2.54446, -1.95311, 12.244, 1.44862, 12.0607,
-0.210492, -3.37906, -0.056713, 10.204, -1.65237, -5.10274, 1.29475, -12.2708, 0.111608,
-8.67592, 0.326634, -1.16763, 0.021781, 31.1258, 0.455335, 21.4684, -0.37544, -3.37121,
0.39362, 11.302, -0.851456, -19.4149, 2.10703, -2.22886, 0.373233, 1.92406, 0.884438, -1.72058,
-0.975127, 9.84013, 2.0033, 17.3954, -0.036915, -1.11137, 0.148456, 5.39997, -1.91441, 4.77382,
1.44791, 0.537122, 0.194979, -1.03818, 0.495771, -9.95502, -1.05899, 32.9471, 2.01122, 32.4544,
-0.30965, 4.71911, 0.436082, 4.63552, -1.23711, -1.25428, 2.02274, 9.42834, 0.190342, 1.46077,
0.479017, 2.48479, -1.07848, 16.2217, 1.20764, 9.65421, -0.258087, -1.67236, 0.071852, 13.416,
-1.87723, -16.072, 1.28957, -4.87118, 0.067713, -13.4427, 0.435551, -4.1655, 0.46614, 30.5895,
0.904895, 21.598, -0.518369, -2.53205, 0.337363, 5.63726, -0.554975, -17.4005, 1.69188,
1.14574, 0.227934, 0.889297, 0.587303, -5.72973, -0.262133, 18.6666, 1.39505, 17.0029,
-0.01909, 4.30838, 0.304235, 12.6699, -2.07406, -6.46084, 0.920546, 1.21296, 0.284927,
-1.78547, 0.209724, -16.024, -0.636067, 31.5768, 1.34989, 34.6775, -0.971625, 5.30086,
0.590249, 4.44971, -1.56787, 3.60239, 2.1455, 4.51666, 0.296022, 4.12017, 0.445299, 0.868772,
-1.44193, 14.1284, 1.35575, 6.0074, -0.012814, -7.49657, -0.43, 8.50012, -1.20469, -7.11326,
1.10102, -6.83682, 0.196463, -6.234, 0.436747, -1.12979, 0.141052, 22.8549, 0.290821, 18.8114,
-0.529536, -7.73251, 0.63428, 10.7898, -1.33472, -20.3258, 1.81564, -1.90332, 0.394778,
3.79758, 0.732682, -8.18382, -0.741244, 11.7683,
];
pub const ge_cb: [lsp_codebook; 1] = [lsp_codebook {
k: 2,
log2m: 8,
m: 256,
cb: &codes0,
}];
File diff suppressed because it is too large. Load diff
+432
View File
@@ -0,0 +1,432 @@
/*
Copyright (c) 2003-2010, Mark Borgerding
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
use super::*;
/*
Explanation of functions dealing with complex math:
C_MUL(m,a,b) : m = a*b
C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
C_SUB( res, a,b) : res = a - b
C_SUBFROM( res , a) : res -= a
C_ADDTO( res , a) : res += a
* */
fn C_MUL(a: &kiss_fft_cpx, b: &kiss_fft_cpx) -> kiss_fft_cpx {
kiss_fft_cpx {
r: a.r * b.r - a.i * b.i,
i: a.r * b.i + a.i * b.r,
}
}
//# define C_FIXDIV(c,div) /* NOOP */
fn C_MULBYSCALAR(c: &mut kiss_fft_cpx, s: f32) {
c.r *= s;
c.i *= s;
}
/*#ifndef CHECK_OVERFLOW_OP
# define CHECK_OVERFLOW_OP(a,op,b) /* noop */
#endif*/
fn C_ADD(a: &kiss_fft_cpx, b: &kiss_fft_cpx) -> kiss_fft_cpx {
kiss_fft_cpx {
r: a.r + b.r,
i: a.i + b.i,
}
}
fn C_SUB(a: &kiss_fft_cpx, b: &kiss_fft_cpx) -> kiss_fft_cpx {
kiss_fft_cpx {
r: a.r - b.r,
i: a.i - b.i,
}
}
fn C_ADDTO(res: &mut kiss_fft_cpx, a: &kiss_fft_cpx) {
res.r += a.r;
res.i += a.i;
}
fn HALF_OF(x: f32) -> f32 {
x / 2.0
}
fn kf_bfly2<const N: usize>(
Fout: &mut [kiss_fft_cpx],
fstride: usize,
st: &kiss_fft_state<N>,
mut m: usize,
) {
let mut tw1 = &st.twiddles[..];
let (mut Fout, mut Fout2) = Fout.split_at_mut(m);
while m > 0 {
//C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2);
let t = C_MUL(&Fout2[0], &tw1[0]);
tw1 = &tw1[fstride..];
Fout2[0] = C_SUB(&Fout[0], &t);
C_ADDTO(&mut Fout[0], &t);
Fout2 = &mut Fout2[1..];
Fout = &mut Fout[1..];
m = m - 1;
}
}
fn kf_bfly4<const N: usize>(
mut Fout: &mut [kiss_fft_cpx],
fstride: usize,
st: &kiss_fft_state<N>,
m: usize,
) {
let mut scratch = [kiss_fft_cpx::new(); 6];
let mut k = m;
let m2 = 2 * m;
let m3 = 3 * m;
let mut tw1 = &st.twiddles[..];
let mut tw2 = &st.twiddles[..];
let mut tw3 = &st.twiddles[..];
loop {
// C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4);
scratch[0] = C_MUL(&Fout[m], &tw1[0]);
scratch[1] = C_MUL(&Fout[m2], &tw2[0]);
scratch[2] = C_MUL(&Fout[m3], &tw3[0]);
scratch[5] = C_SUB(&Fout[0], &scratch[1]);
C_ADDTO(&mut Fout[0], &scratch[1]);
scratch[3] = C_ADD(&scratch[0], &scratch[2]);
scratch[4] = C_SUB(&scratch[0], &scratch[2]);
Fout[m2] = C_SUB(&Fout[0], &scratch[3]);
tw1 = &tw1[fstride..];
tw2 = &tw2[fstride * 2..];
tw3 = &tw3[fstride * 3..];
C_ADDTO(&mut Fout[0], &scratch[3]);
if st.inverse != 0 {
Fout[m].r = scratch[5].r - scratch[4].i;
Fout[m].i = scratch[5].i + scratch[4].r;
Fout[m3].r = scratch[5].r + scratch[4].i;
Fout[m3].i = scratch[5].i - scratch[4].r;
} else {
Fout[m].r = scratch[5].r + scratch[4].i;
Fout[m].i = scratch[5].i - scratch[4].r;
Fout[m3].r = scratch[5].r - scratch[4].i;
Fout[m3].i = scratch[5].i + scratch[4].r;
}
Fout = &mut Fout[1..];
k -= 1;
if k == 0 {
break;
}
}
}
fn kf_bfly3<const N: usize>(
mut Fout: &mut [kiss_fft_cpx],
fstride: usize,
st: &kiss_fft_state<N>,
m: usize,
) {
let mut k = m;
let m2 = 2 * m;
let mut scratch = [kiss_fft_cpx::new(); 5];
let epi3 = st.twiddles[fstride * m];
let mut tw1 = 0;
let mut tw2 = 0;
while k > 0 {
// C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
scratch[1] = C_MUL(&Fout[m], &st.twiddles[tw1]);
scratch[2] = C_MUL(&Fout[m2], &st.twiddles[tw2]);
scratch[3] = C_ADD(&scratch[1], &scratch[2]);
scratch[0] = C_SUB(&scratch[1], &scratch[2]);
tw1 += fstride;
tw2 += fstride * 2;
Fout[m].r = Fout[0].r - (scratch[3].r / 2.0);
Fout[m].i = Fout[0].i - (scratch[3].i / 2.0);
C_MULBYSCALAR(&mut scratch[0], epi3.i);
C_ADDTO(&mut Fout[0], &scratch[3]);
Fout[m2].r = Fout[m].r + scratch[0].i;
Fout[m2].i = Fout[m].i - scratch[0].r;
Fout[m].r -= scratch[0].i;
Fout[m].i += scratch[0].r;
Fout = &mut Fout[1..];
k -= 1;
}
}
fn kf_bfly5<const N: usize>(
Fout: &mut [kiss_fft_cpx],
fstride: usize,
st: &kiss_fft_state<N>,
m: usize,
) {
let mut scratch = [kiss_fft_cpx::new(); 13];
let ya = st.twiddles[fstride * m];
let yb = st.twiddles[fstride * 2 * m];
let (mut Fout0, Fout1) = Fout.split_at_mut(m);
let (mut Fout1, Fout2) = Fout1.split_at_mut(m);
let (mut Fout2, Fout3) = Fout2.split_at_mut(m);
let (mut Fout3, mut Fout4) = Fout3.split_at_mut(m);
let tw = &st.twiddles[..];
for u in 0..m {
// C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
scratch[0] = Fout0[0];
scratch[1] = C_MUL(&Fout1[0], &tw[u * fstride]);
scratch[2] = C_MUL(&Fout2[0], &tw[2 * u * fstride]);
scratch[3] = C_MUL(&Fout3[0], &tw[3 * u * fstride]);
scratch[4] = C_MUL(&Fout4[0], &tw[4 * u * fstride]);
scratch[7] = C_ADD(&scratch[1], &scratch[4]);
scratch[10] = C_SUB(&scratch[1], &scratch[4]);
scratch[8] = C_ADD(&scratch[2], &scratch[3]);
scratch[9] = C_SUB(&scratch[2], &scratch[3]);
Fout0[0].r += scratch[7].r + scratch[8].r;
Fout0[0].i += scratch[7].i + scratch[8].i;
scratch[5].r = scratch[0].r + (scratch[7].r * ya.r) + (scratch[8].r * yb.r);
scratch[5].i = scratch[0].i + (scratch[7].i * ya.r) + (scratch[8].i * yb.r);
scratch[6].r = (scratch[10].i * ya.i) + (scratch[9].i * yb.i);
scratch[6].i = -(scratch[10].r * ya.i) - (scratch[9].r * yb.i);
Fout1[0] = C_SUB(&scratch[5], &scratch[6]);
Fout4[0] = C_ADD(&scratch[5], &scratch[6]);
scratch[11].r = scratch[0].r + (scratch[7].r * yb.r) + (scratch[8].r * ya.r);
scratch[11].i = scratch[0].i + (scratch[7].i * yb.r) + (scratch[8].i * ya.r);
scratch[12].r = -(scratch[10].i * yb.i) + (scratch[9].i * ya.i);
scratch[12].i = (scratch[10].r * yb.i) - (scratch[9].r * ya.i);
Fout2[0] = C_ADD(&scratch[11], &scratch[12]);
Fout3[0] = C_SUB(&scratch[11], &scratch[12]);
Fout0 = &mut Fout0[1..];
Fout1 = &mut Fout1[1..];
Fout2 = &mut Fout2[1..];
Fout3 = &mut Fout3[1..];
Fout4 = &mut Fout4[1..];
}
}
/* perform the butterfly for one stage of a mixed radix FFT */
fn kf_bfly_generic<const N: usize>(
Fout: &mut [kiss_fft_cpx],
fstride: usize,
st: &kiss_fft_state<N>,
m: usize,
p: usize,
) {
let Norig = st.nfft;
let mut scratch = Buffer::<kiss_fft_cpx, 5>::filled(p, kiss_fft_cpx::new());
for u in 0..m {
let mut k = u;
for q1 in 0..p {
scratch[q1] = Fout[k];
// C_FIXDIV(scratch[q1],p);
k += m;
}
k = u;
for _q1 in 0..p {
let mut twidx = 0;
Fout[k] = scratch[0];
for q in 1..p {
twidx += fstride * k;
if twidx >= Norig {
twidx -= Norig;
}
let t = C_MUL(&scratch[q], &st.twiddles[twidx]);
C_ADDTO(&mut Fout[k], &t);
}
k += m;
}
}
}
fn kf_work<const N: usize>(
Fout: &mut [kiss_fft_cpx],
mut f: &[kiss_fft_cpx],
fstride: usize,
in_stride: i32,
factors: &[usize],
st: &kiss_fft_state<N>,
) {
let p = factors[0]; /* the radix */
let m = factors[1]; /* stage's fft length/p */
if m == 1 {
for fout_idx in 0..p * m {
Fout[fout_idx] = f[0];
f = &f[(fstride * in_stride as usize).min(f.len())..];
}
} else {
for fout_idx in (0..p * m).step_by(m) {
// recursive call:
// DFT of size m*p performed by doing
// p instances of smaller DFTs of size m,
// each one takes a decimated version of the input
kf_work(
&mut Fout[fout_idx..],
f,
fstride * p,
in_stride,
&factors[2..],
st,
);
f = &f[(fstride * in_stride as usize).min(f.len())..];
}
}
// recombine the p smaller DFTs
match p {
2 => {
kf_bfly2(Fout, fstride, st, m);
}
3 => {
kf_bfly3(Fout, fstride, st, m);
}
4 => {
kf_bfly4(Fout, fstride, st, m);
}
5 => {
kf_bfly5(Fout, fstride, st, m);
}
_ => {
kf_bfly_generic(Fout, fstride, st, m, p);
}
}
}
fn kiss_fft_stride<const N: usize>(
st: &kiss_fft_state<N>,
fin: &[kiss_fft_cpx],
fout: &mut [kiss_fft_cpx],
in_stride: i32,
) {
kf_work(fout, fin, 1, in_stride, &st.factors[..], st);
}
pub fn kiss_fft<const N: usize>(
cfg: &kiss_fft_state<N>,
fin: &[kiss_fft_cpx],
fout: &mut [kiss_fft_cpx],
) {
kiss_fft_stride(cfg, fin, fout, 1);
}
pub fn kiss_fftr(st: &mut kiss_fftr_cfg, timedata: &[f32], freqdata: &mut [kiss_fft_cpx]) {
/* input buffer timedata is stored row-wise */
// assert(st.substate.inverse==0);
let ncfft = st.substate.nfft;
/*perform the parallel fft of two real signals packed in real,imag*/
//could transmute this but UGH so we copy
let mut timedatac =
Buffer::<kiss_fft_cpx, 256>::filled(timedata.len() / 2, kiss_fft_cpx::new());
for i in 0..timedatac.len() {
timedatac[i] = kiss_fft_cpx {
r: timedata[i * 2],
i: timedata[i * 2 + 1],
};
}
kiss_fft(&st.substate, &timedatac, &mut st.tmpbuf);
/* The real part of the DC element of the frequency spectrum in st.tmpbuf
* contains the sum of the even-numbered elements of the input time sequence
* The imag part is the sum of the odd-numbered elements
*
* The sum of tdc.r and tdc.i is the sum of the input time sequence.
* yielding DC of input time sequence
* The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
* yielding Nyquist bin of input time sequence
*/
let mut tdc = kiss_fft_cpx::new();
tdc.r = st.tmpbuf[0].r;
tdc.i = st.tmpbuf[0].i;
// C_FIXDIV(tdc,2);
// CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i);
// CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
freqdata[0].r = tdc.r + tdc.i;
freqdata[ncfft].r = tdc.r - tdc.i;
freqdata[ncfft].i = 0.0;
freqdata[0].i = 0.0;
for k in 1..ncfft / 2 + 1 {
let fpk = st.tmpbuf[k];
let mut fpnk = kiss_fft_cpx::new();
fpnk.r = st.tmpbuf[ncfft - k].r;
fpnk.i = 0.0 - st.tmpbuf[ncfft - k].i;
// C_FIXDIV(fpk,2);
// C_FIXDIV(fpnk,2);
let f1k = C_ADD(&fpk, &fpnk);
let f2k = C_SUB(&fpk, &fpnk);
let tw = C_MUL(&f2k, &st.super_twiddles[k - 1]);
freqdata[k].r = HALF_OF(f1k.r + tw.r);
freqdata[k].i = HALF_OF(f1k.i + tw.i);
freqdata[ncfft - k].r = HALF_OF(f1k.r - tw.r);
freqdata[ncfft - k].i = HALF_OF(tw.i - f1k.i);
}
}
pub fn kiss_fftri(st: &mut kiss_fftr_cfg, freqdata: &[kiss_fft_cpx], timedata: &mut [f32]) {
/* input buffer timedata is stored row-wise */
// assert(st.substate.inverse == 1);
let ncfft = st.substate.nfft;
st.tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r;
st.tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r;
// C_FIXDIV(st.tmpbuf[0],2);
for k in 1..ncfft / 2 + 1 {
let fk = freqdata[k];
let mut fnkc = kiss_fft_cpx::new();
fnkc.r = freqdata[ncfft - k].r;
fnkc.i = 0.0 - freqdata[ncfft - k].i;
// C_FIXDIV( fk , 2 );
// C_FIXDIV( fnkc , 2 );
let fek = C_ADD(&fk, &fnkc);
let tmp = C_SUB(&fk, &fnkc);
let fok = C_MUL(&tmp, &st.super_twiddles[k - 1]);
st.tmpbuf[k] = C_ADD(&fek, &fok);
st.tmpbuf[ncfft - k] = C_SUB(&fek, &fok);
st.tmpbuf[ncfft - k].i *= -1.0;
}
//could transmute this but UGH so we copy
let mut timedatac =
Buffer::<kiss_fft_cpx, 256>::filled(timedata.len() / 2, kiss_fft_cpx::new());
kiss_fft(&st.substate, &st.tmpbuf, &mut timedatac);
for i in 0..timedatac.len() {
timedata[i * 2] = timedatac[i].r;
timedata[i * 2 + 1] = timedatac[i].i;
}
}
File diff suppressed because it is too large. Load diff
+415
View File
@@ -0,0 +1,415 @@
use super::*;
const fdmdv_os_filter: [f32; 48] = [
-0.0008215855034550382,
-0.0007833023901802921,
0.001075563790768233,
0.001199092367787555,
-0.001765309502928316,
-0.002055372115328064,
0.002986877604154257,
0.003462567920638414,
-0.004856570111126334,
-0.005563143845031497,
0.007533613299748122,
0.008563932468880897,
-0.01126857129039911,
-0.01280782411693687,
0.01651443896361847,
0.01894875110322284,
-0.02421604439474981,
-0.02845107338464062,
0.03672973563400258,
0.04542046150312214,
-0.06189165826716491,
-0.08721876380763803,
0.1496157094199961,
0.4497962274137046,
0.4497962274137046,
0.1496157094199961,
-0.08721876380763803,
-0.0618916582671649,
0.04542046150312216,
0.03672973563400257,
-0.02845107338464062,
-0.02421604439474984,
0.01894875110322284,
0.01651443896361848,
-0.01280782411693687,
-0.0112685712903991,
0.008563932468880899,
0.007533613299748123,
-0.005563143845031501,
-0.004856570111126346,
0.003462567920638419,
0.002986877604154259,
-0.002055372115328063,
-0.001765309502928318,
0.001199092367787557,
0.001075563790768233,
-0.0007833023901802925,
-0.0008215855034550383,
];
const nlp_fir: [f32; 48] = [
-1.0818124e-03,
-1.1008344e-03,
-9.2768838e-04,
-4.2289438e-04,
5.5034190e-04,
2.0029849e-03,
3.7058509e-03,
5.1449415e-03,
5.5924666e-03,
4.3036754e-03,
8.0284511e-04,
-4.8204610e-03,
-1.1705810e-02,
-1.8199275e-02,
-2.2065282e-02,
-2.0920610e-02,
-1.2808831e-02,
3.2204775e-03,
2.6683811e-02,
5.5520624e-02,
8.6305944e-02,
1.1480192e-01,
1.3674206e-01,
1.4867556e-01,
1.4867556e-01,
1.3674206e-01,
1.1480192e-01,
8.6305944e-02,
5.5520624e-02,
2.6683811e-02,
3.2204775e-03,
-1.2808831e-02,
-2.0920610e-02,
-2.2065282e-02,
-1.8199275e-02,
-1.1705810e-02,
-4.8204610e-03,
8.0284511e-04,
4.3036754e-03,
5.5924666e-03,
5.1449415e-03,
3.7058509e-03,
2.0029849e-03,
5.5034190e-04,
-4.2289438e-04,
-9.2768838e-04,
-1.1008344e-03,
-1.0818124e-03,
];
//const PMAX_M: i32 = 320; /* maximum NLP analysis window size */
const COEFF: f32 = 0.95; /* notch filter parameter */
const PE_FFT_SIZE: i32 = 512; /* DFT size for pitch estimation */
const DEC: i32 = 5; /* decimation factor */
const SAMPLE_RATE: i32 = 8000;
//const PI: f32 = 3.141592654; /* mathematical constant */
//const T: f32 = 0.1; /* threshold for local minima candidate */
//const F0_MAX: i32 = 500;
const CNLP: f32 = 0.3; /* post processor constant */
const NLP_NTAP: i32 = 48; /* Decimation LPF order */
const FDMDV_OS: i32 = 2; /* oversampling rate */
pub const FDMDV_OS_TAPS_16K: i32 = 48; /* number of OS filter taps at 16kHz */
//const FDMDV_OS_TAPS_8K: i32 = FDMDV_OS_TAPS_16K / FDMDV_OS; /* number of OS filter taps at 8kHz */
/*---------------------------------------------------------------------------*\
post_process_sub_multiples()
Given the global maximma of Fw[] we search integer submultiples for
local maxima. If local maxima exist and they are above an
experimentally derived threshold (OK a magic number I pulled out of
the air) we choose the submultiple as the F0 estimate.
The rational for this is that the lowest frequency peak of Fw[]
should be F0, as Fw[] can be considered the autocorrelation function
of Sw[] (the speech spectrum). However sometimes due to phase
effects the lowest frequency maxima may not be the global maxima.
This works OK in practice and favours low F0 values in the presence
of background noise which means the sinusoidal codec does an OK job
of synthesising the background noise. High F0 in background noise
tends to sound more periodic introducing annoying artifacts.
\*---------------------------------------------------------------------------*/
fn post_process_sub_multiples(
Fw: &[COMP],
_pmin: i32,
pmax: i32,
gmax: f32,
gmax_bin: i32,
prev_f0: &f32,
) -> f32 {
/* post process estimate by searching submultiples */
let mut mult = 2;
let min_bin = PE_FFT_SIZE * DEC / pmax;
let mut cmax_bin = gmax_bin;
let prev_f0_bin = (*prev_f0) * ((PE_FFT_SIZE * DEC) as f32) / (SAMPLE_RATE as f32);
while gmax_bin / mult >= min_bin {
let b = gmax_bin / mult; /* determine search interval */
let mut bmin = (0.8 * (b as f32)) as usize;
let bmax = (1.2 * (b as f32)) as usize;
if bmin < min_bin as usize {
bmin = min_bin as usize;
}
/* lower threshold to favour previous frames pitch estimate,
this is a form of pitch tracking */
let thresh = if (prev_f0_bin > bmin as f32) && (prev_f0_bin < bmax as f32) {
CNLP * 0.5 * gmax
} else {
CNLP * gmax
};
let mut lmax = 0.0;
let mut lmax_bin = bmin;
for b in bmin..bmax {
/* look for maximum in interval */
if Fw[b].r > lmax {
lmax = Fw[b].r;
lmax_bin = b;
}
}
if lmax > thresh {
if (lmax > Fw[lmax_bin - 1].r) && (lmax > Fw[lmax_bin + 1].r) {
cmax_bin = lmax_bin as i32;
}
}
mult += 1;
}
cmax_bin as f32 * SAMPLE_RATE as f32 / (PE_FFT_SIZE as f32 * DEC as f32)
}
/*---------------------------------------------------------------------------*\
FUNCTION....: fdmdv_16_to_8()
AUTHOR......: David Rowe, conversion by Matt Weeks
DATE CREATED: 9 May 2012
Changes the sample rate of a signal from 16 to 8 kHz.
n is the number of samples at the 8 kHz rate, there are FDMDV_OS*n
samples at the 48 kHz rate. As above however a memory of
FDMDV_OS_TAPS samples is reqd for in16k[] (see t16_8.c unit test as example).
Low pass filter the 16 kHz signal at 4 kHz using the same filter as
the upsampler, then just output every FDMDV_OS-th filtered sample.
Note: this function copied from fdmdv.c, included in nlp.c as a convenience
to avoid linking with another source file.
\*---------------------------------------------------------------------------*/
fn fdmdv_16_to_8(out8k: &mut [f32], in16k: &mut [f32], in16koff: usize, n: i32) {
let mut i = 0;
for k in 0..n as usize {
let mut acc = 0.0;
for j in 0..FDMDV_OS_TAPS_16K as usize {
acc += fdmdv_os_filter[j] * in16k[in16koff + i - j];
}
out8k[k] = acc;
i += FDMDV_OS as usize;
}
/* update filter memory */
for i in -FDMDV_OS_TAPS_16K..0 {
in16k[(in16koff as i32 + i) as usize] =
in16k[(in16koff as isize + i as isize + n as isize * FDMDV_OS as isize) as usize];
}
}
/*---------------------------------------------------------------------------*\
nlp()
Determines the pitch in samples using the Non Linear Pitch (NLP)
algorithm [1]. Returns the fundamental in Hz. Note that the actual
pitch estimate is for the centre of the M sample Sn[] vector, not
the current N sample input vector. This is (I think) a delay of 2.5
frames with N=80 samples. You should align further analysis using
this pitch estimate to be centred on the middle of Sn[].
Two post processors have been tried, the MBE version (as discussed
in [1]), and a post processor that checks sub-multiples. Both
suffer occasional gross pitch errors (i.e. neither are perfect). In
the presence of background noise the sub-multiple algorithm tends
towards low F0 which leads to better sounding background noise than
the MBE post processor.
A good way to test and develop the NLP pitch estimator is using the
tnlp (codec2/unittest) and the codec2/octave/plnlp.m Octave script.
A pitch tracker searching a few frames forward and backward in time
would be a useful addition.
References:
[1] http://rowetel.com/downloads/1997_rowe_phd_thesis.pdf Chapter 4
\*---------------------------------------------------------------------------*/
pub fn nlp(
nlp: &mut NLP,
Sn: &[f32], /* input speech vector */
mut n: usize, /* frames shift (no. new samples in Sn[]) */
pitch: &mut f32, /* estimated pitch period in samples at current Fs */
_Sw: &[COMP], /* Freq domain version of Sn[] */
_W: &[f32], /* Freq domain window */
prev_f0: &mut f32, /* previous pitch f0 in Hz, memory for pitch tracking */
) -> f32 {
// notch: f32; /* current notch filter output */
// let mut Fw[PE_FFT_SIZE]; /* DFT of squared signal (input/output) */
// PROFILE_VAR(start, tnotch, filter, peakpick, window, fft, magsq, shiftmem);
// assert(nlp_state != NULL);
// nlp = (NLP*)nlp_state;
let mut m = nlp.m;
/* Square, notch filter at DC, and LP filter vector */
/* If running at 16 kHz decimate to 8 kHz, as NLP ws designed for
Fs = 8kHz. The decimating filter introduces about 3ms of delay,
that shouldn't be a problem as pitch changes slowly. */
if nlp.Fs == 8000 {
/* Square latest input samples */
for i in m - n..m {
nlp.sq[i] = Sn[i] * Sn[i];
}
} else {
//assert(nlp.Fs == 16000);
/* re-sample at 8 KHz */
for i in 0..n {
nlp.Sn16k[FDMDV_OS_TAPS_16K as usize + i] = Sn[m + i - n];
}
m /= 2;
n /= 2;
let mut Sn8k = Buffer::<f32, PMAX_M>::filled(n, 0.0);
fdmdv_16_to_8(
&mut Sn8k,
&mut nlp.Sn16k,
FDMDV_OS_TAPS_16K as usize,
n as i32,
);
/* Square latest input samples */
let mut j = 0;
for i in m - n..m {
nlp.sq[i] = Sn8k[j] * Sn8k[j];
j += 1;
}
// assert(j <= n);
}
//fprintf(stderr, "n: %d m: %d\n", n, m);
// PROFILE_SAMPLE(start);
for i in m - n..m {
/* notch filter at DC */
let mut notch = nlp.sq[i] - nlp.mem_x;
notch += COEFF * nlp.mem_y;
nlp.mem_x = nlp.sq[i];
nlp.mem_y = notch;
nlp.sq[i] = notch + 1.0; /* With 0 input vectors to codec,
kiss_fft() would take a long
time to execute when running in
real time. Problem was traced
to kiss_fft function call in
this function. Adding this small
constant fixed problem. Not
exactly sure why. */
}
// PROFILE_SAMPLE_AND_LOG(tnotch, start, " square and notch");
for i in m - n..m {
/* FIR filter vector */
for j in 0..NLP_NTAP as usize - 1 {
nlp.mem_fir[j] = nlp.mem_fir[j + 1];
}
nlp.mem_fir[NLP_NTAP as usize - 1] = nlp.sq[i];
nlp.sq[i] = 0.0;
for j in 0..NLP_NTAP as usize {
nlp.sq[i] += nlp.mem_fir[j] * nlp_fir[j];
}
}
// PROFILE_SAMPLE_AND_LOG(filter, tnotch, " filter");
/* Decimate and DFT */
let mut Fw = [COMP::new(); PE_FFT_SIZE as usize];
for i in 0..m / DEC as usize {
Fw[i].r = nlp.sq[i * DEC as usize] * nlp.w[i];
}
// PROFILE_SAMPLE_AND_LOG(window, filter, " window");
// #ifdef DUMP
// dump_dec(Fw);
// #endif
// FIXME: check if this can be converted to a real fft
// since all imag inputs are 0
super::codec2_fft_inplace(&nlp.fft_cfg, &mut Fw);
// PROFILE_SAMPLE_AND_LOG(fft, window, " fft");
for i in 0..PE_FFT_SIZE as usize {
Fw[i].r = Fw[i].r * Fw[i].r + Fw[i].i * Fw[i].i;
}
// PROFILE_SAMPLE_AND_LOG(magsq, fft, " mag sq");
// #ifdef DUMP
// dump_sq(m, nlp.sq);
// dump_Fw(Fw);
// #endif
/* todo: express everything in f0, as pitch in samples is dep on Fs */
let pmin = (SAMPLE_RATE as f32 * P_MIN_S).dsp_floor() as i32;
let pmax = (SAMPLE_RATE as f32 * P_MAX_S).dsp_floor() as i32;
/* find global peak */
let mut gmax = 0.0;
let mut gmax_bin = PE_FFT_SIZE * DEC / pmax;
for i in (PE_FFT_SIZE * DEC / pmax) as usize..(PE_FFT_SIZE * DEC / pmin) as usize + 1 {
if Fw[i].r > gmax {
gmax = Fw[i].r;
gmax_bin = i as i32;
}
}
// PROFILE_SAMPLE_AND_LOG(peakpick, magsq, " peak pick");
let best_f0 = post_process_sub_multiples(&Fw, pmin, pmax, gmax, gmax_bin, prev_f0);
// PROFILE_SAMPLE_AND_LOG(shiftmem, peakpick, " post process");
/* Shift samples in buffer to make room for new samples */
for i in 0..m - n {
nlp.sq[i] = nlp.sq[i + n];
}
/* return pitch period in samples and F0 estimate */
*pitch = nlp.Fs as f32 / best_f0;
// PROFILE_SAMPLE_AND_LOG2(shiftmem, " shift mem");
// PROFILE_SAMPLE_AND_LOG2(start, " nlp int");
*prev_f0 = best_f0;
best_f0
}
File diff suppressed because it is too large. Load diff
+124
View File
@@ -0,0 +1,124 @@
use lxst_codec2::{Codec, Error, Mode};
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
struct Meter;
static ALLOCATIONS: AtomicUsize = AtomicUsize::new(0);
unsafe impl GlobalAlloc for Meter {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ALLOCATIONS.fetch_add(1, Relaxed);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
unsafe { System.dealloc(pointer, layout) }
}
}
#[global_allocator]
static ALLOCATOR: Meter = Meter;
// One process/thread: unrelated parallel test allocations cannot contaminate
// the count. Large states are allocated before measurement and initialised in place.
fn main() {
for mode in [Mode::Rate1600, Mode::Rate3200] {
let mut duplex_storage = Box::<Codec>::new_uninit();
let mut encoder_storage = Box::<Codec>::new_uninit();
let mut decoder_storage = Box::<Codec>::new_uninit();
let before = ALLOCATIONS.load(Relaxed);
let duplex = Codec::initialise(&mut duplex_storage, mode);
let encoder = Codec::initialise(&mut encoder_storage, mode);
let decoder = Codec::initialise(&mut decoder_storage, mode);
let samples = mode.samples();
let mut input = [0_i16; 320];
let mut expected = [0_i16; 320];
let mut actual = [0_i16; 320];
let mut encoded = [0_u8; 8];
let mut same = [0_u8; 8];
let mut rng = 71_u32;
for frame in 0..1000 {
for sample in &mut input[..samples] {
rng = rng.wrapping_mul(1664525).wrapping_add(1013904223);
*sample = match frame % 5 {
0 => 0,
1 => (rng >> 16) as i16,
2 => {
if rng & 1 == 0 {
i16::MIN
} else {
i16::MAX
}
}
_ => ((rng >> 16) as i16) / 4,
};
}
// Rejected input cannot alter state or caller outputs.
same.fill(0xA5);
assert_eq!(
duplex.encode(&input[..samples - 1], &mut same),
Err(Error::PcmLength)
);
assert_eq!(same, [0xA5; 8]);
assert_eq!(
duplex.encode(&input[..samples], &mut same[..7]),
Err(Error::EncodedLength)
);
actual.fill(123);
assert_eq!(
duplex.decode(&encoded[..7], &mut actual[..samples]),
Err(Error::EncodedLength)
);
assert_eq!(
duplex.decode(&encoded, &mut actual[..samples - 1]),
Err(Error::PcmLength)
);
assert!(actual.iter().all(|v| *v == 123));
encoder.encode(&input[..samples], &mut encoded).unwrap();
duplex.encode(&input[..samples], &mut same).unwrap();
assert_eq!(encoded, same, "receive activity changed encoder history");
decoder.decode(&encoded, &mut expected[..samples]).unwrap();
duplex.decode(&encoded, &mut actual[..samples]).unwrap();
assert_eq!(
expected[..samples],
actual[..samples],
"another decoder changed random/history state"
);
if frame % 127 == 0 {
duplex.reset(mode);
encoder.reset(mode);
decoder.reset(mode);
}
}
// Every 64-bit payload is valid codec data. Exercise decoder indices on
// hostile but correctly-sized frames, including both extrema.
for frame in 0..10000 {
for byte in &mut encoded {
rng = rng.wrapping_mul(1664525).wrapping_add(1013904223);
*byte = match frame % 10 {
0 => 0,
1 => 255,
_ => (rng >> 24) as u8,
};
}
duplex.decode(&encoded, &mut actual[..samples]).unwrap();
}
assert_eq!(
ALLOCATIONS.load(Relaxed),
before,
"initialisation/reset/stream allocated"
);
let other = match mode {
Mode::Rate1600 => Mode::Rate3200,
Mode::Rate3200 => Mode::Rate1600,
};
duplex.reset(other);
assert_eq!(duplex.mode(), other);
assert_eq!(ALLOCATIONS.load(Relaxed), before);
println!(
"{mode:?}: allocation-free construction/reset; isolated duplex histories; 1000 stream pairs; 10000 arbitrary decoder frames PASS"
);
}
println!(
"codec fixed storage: {} bytes, alignment {}",
size_of::<Codec>(),
align_of::<Codec>()
);
}
+8 -3
View File
@@ -59,14 +59,19 @@ def main() -> None:
floor_snapshots = {
package["name"]: package["snapshot"] for package in floor_ledger["packages"]
}
current_names = {package["name"] for package in ledger["packages"]}
removed_packages = set(floor_snapshots) - current_names
if removed_packages:
fail(f"compatibility-floor packages removed: {sorted(removed_packages)}")
new_packages = sorted(current_names - set(floor_snapshots))
if ledger["snapshotSource"]["review"].get("newPackages", []) != new_packages:
fail("new packages require an exact explicit snapshot review")
total_added = 0
total_removed = 0
for package in ledger["packages"]:
path = package["snapshot"]
floor_path = floor_snapshots.get(package["name"])
if not isinstance(floor_path, str):
fail(f"{package['name']} is absent from the compatibility floor")
before = set(git_show(floor, floor_path).splitlines())
before = set(git_show(floor, floor_path).splitlines()) if floor_path else set()
after = set((ROOT / path).read_text(encoding="utf-8").splitlines())
added = sorted(after - before)
removed = sorted(before - after)
+1 -1
View File
@@ -11,7 +11,7 @@ import sys
ROOT = Path(__file__).resolve().parents[1]
EXPECTED_PACKAGES = {"lxst-core", "lxst-rns", "lxst-telephony"}
EXPECTED_PACKAGES = {"lxst-core", "lxst-codec2", "lxst-rns", "lxst-telephony"}
EXPECTED_MSRV = "1.87"
EXPECTED_RETICULUM_VERSION = "1.3.0"
EXPECTED_RETICULUM_COMMIT = "d4183434fcb969799c208ddcf140bae7550c14fe"
+17 -1
View File
@@ -129,12 +129,28 @@ def build_inventory() -> dict:
if copying_hash != UPSTREAM_OPUS_COPYING_SHA256:
fail(f"preserved opus-rs COPYING changed (found {copying_hash})")
adapted = ROOT / "crates/lxst-codec2"
provenance = json.loads((adapted / "UPSTREAM.json").read_text())
if (provenance["version"], provenance["commit"], provenance["crate_sha256"]) != (
"0.3.1", "24ec3b7c5b9892c268e180802ee3a5ce7737f214",
"3c3c8ba70382a99c0b5ebadf6ad50e7d98fb6eb9bcc4c0e30d422f6321798d99",
):
fail("adapted Codec2 immutable upstream provenance changed")
entries.append({
"name": "codec2 (adapted as lxst-codec2)", "version": provenance["version"],
"license": "LGPL-2.1-only AND MIT AND BSD-3-Clause",
"repository": provenance["repository"], "source": "local-adaptation",
"upstreamCommit": provenance["commit"], "crateChecksum": provenance["crate_sha256"],
"provenance": "crates/lxst-codec2/UPSTREAM.json",
"notices": {str(p.relative_to(ROOT)): hashlib.sha256(p.read_bytes()).hexdigest()
for p in [adapted / "NOTICE", *sorted((adapted / "licenses").iterdir())]},
})
entries.sort(key=lambda entry: (entry["name"], entry["version"], entry["source"]))
lockfile_hash = hashlib.sha256((ROOT / "Cargo.lock").read_bytes()).hexdigest()
return {
"schemaVersion": 1,
"scope": (
"All registry third-party normal/build dependencies "
"Adapted Codec2 and all registry third-party normal/build dependencies "
"reachable from rsLXST workspace members across declared targets; "
"dev-only and first-party path packages are excluded."
),