From a1853f2dc51dd8913b62f397edca9a075ec0cbb4 Mon Sep 17 00:00:00 2001 From: TheM14 Date: Fri, 16 Oct 2026 19:46:24 +0800 Subject: [PATCH] Add JOC downmix configurations 0..4, cross-layer checks, and drop the native core --- .github/workflows/native-release.yml | 99 -- .gitignore | 2 - README.en.md | 59 +- README.md | 56 +- data/README.en.md | 4 +- data/README.md | 4 +- docs/binaural.en.md | 13 +- docs/binaural.md | 7 +- docs/math.en.md | 40 +- docs/math.md | 39 +- docs/native.en.md | 178 ---- docs/native.md | 191 ---- main.py | 218 +++-- native/CMakeLists.txt | 74 -- native/include/eac3joc_core.h | 244 ----- native/src/binaural_renderer.cpp | 664 ------------- native/src/eac3joc_core.cpp | 751 --------------- native/src/joc_huffman_tables.h | 89 -- native/src/qmf_tables.h | 388 -------- native/src/sofa_binaural_renderer.cpp | 1231 ------------------------- native/src/speaker_layouts.h | 534 ----------- native/src/speaker_renderer.cpp | 495 ---------- src/bed_layout.py | 80 ++ src/binaural_native_renderer.py | 214 ----- src/emdf.py | 206 ++++- src/joc_decode.py | 39 +- src/joc_qmf.py | 9 +- src/metadata.py | 304 +++++- src/native_renderer.py | 293 ------ src/oamd_bits.py | 33 + src/oamd_tracks.py | 2 +- src/renderer.py | 102 +- src/rosella_binaural_renderer.py | 105 +-- src/sofa_native_backend.py | 398 -------- src/speaker_backend.py | 25 +- src/speaker_native_renderer.py | 154 ---- src/speaker_renderer.py | 2 +- 37 files changed, 926 insertions(+), 6420 deletions(-) delete mode 100644 .github/workflows/native-release.yml delete mode 100644 docs/native.en.md delete mode 100644 docs/native.md delete mode 100644 native/CMakeLists.txt delete mode 100644 native/include/eac3joc_core.h delete mode 100644 native/src/binaural_renderer.cpp delete mode 100644 native/src/eac3joc_core.cpp delete mode 100644 native/src/joc_huffman_tables.h delete mode 100644 native/src/qmf_tables.h delete mode 100644 native/src/sofa_binaural_renderer.cpp delete mode 100644 native/src/speaker_layouts.h delete mode 100644 native/src/speaker_renderer.cpp create mode 100644 src/bed_layout.py delete mode 100644 src/binaural_native_renderer.py delete mode 100644 src/native_renderer.py delete mode 100644 src/sofa_native_backend.py delete mode 100644 src/speaker_native_renderer.py diff --git a/.github/workflows/native-release.yml b/.github/workflows/native-release.yml deleted file mode 100644 index 3e18fb0..0000000 --- a/.github/workflows/native-release.yml +++ /dev/null @@ -1,99 +0,0 @@ -name: Native builds - -on: - workflow_dispatch: - push: - branches: - - main - tags: - - "v*" - -permissions: - contents: read - -jobs: - build: - name: ${{ matrix.asset }} - runs-on: ${{ matrix.runner }} - - strategy: - fail-fast: false - matrix: - include: - - runner: windows-2022 - asset: windows-x64 - cmake_args: -A x64 - - - runner: ubuntu-22.04 - asset: linux-x64 - cmake_args: "" - - - runner: macos-15-intel - asset: macos-x64 - cmake_args: -DCMAKE_OSX_DEPLOYMENT_TARGET=12.0 - - - runner: macos-15 - asset: macos-arm64 - cmake_args: -DCMAKE_OSX_DEPLOYMENT_TARGET=12.0 - - steps: - - name: Checkout - uses: actions/checkout@v6 - - - name: Configure - run: > - cmake - -S native - -B build/native - -DCMAKE_BUILD_TYPE=Release - -DCMAKE_INSTALL_PREFIX="${{ github.workspace }}/stage" - ${{ matrix.cmake_args }} - - - name: Build - run: cmake --build build/native --config Release --parallel - - - name: Install - run: cmake --install build/native --config Release - - - name: Package - working-directory: stage - run: > - cmake -E tar - cf "../JustOneCacophony-native-${{ matrix.asset }}.zip" - --format=zip - -- . - - - name: Upload workflow artifact - uses: actions/upload-artifact@v4 - with: - name: JustOneCacophony-native-${{ matrix.asset }} - path: JustOneCacophony-native-${{ matrix.asset }}.zip - if-no-files-found: error - retention-days: 14 - - release: - name: Publish GitHub Release - if: startsWith(github.ref, 'refs/tags/v') - needs: build - runs-on: ubuntu-24.04 - - permissions: - contents: write - - steps: - - name: Download native packages - uses: actions/download-artifact@v5 - with: - pattern: JustOneCacophony-native-* - path: dist - merge-multiple: true - - - name: Create release - run: > - gh release create "$GITHUB_REF_NAME" - dist/*.zip - --verify-tag - --generate-notes - env: - GH_TOKEN: ${{ github.token }} - GH_REPO: ${{ github.repository }} diff --git a/.gitignore b/.gitignore index 663e202..d2322a7 100644 --- a/.gitignore +++ b/.gitignore @@ -5,10 +5,8 @@ __pycache__/ .venv/ venv/ -build/ output/ tests/ -lib/ metadata_cache/ *.metadata.json diff --git a/README.en.md b/README.en.md index 7e1eac3..d386c00 100644 --- a/README.en.md +++ b/README.en.md @@ -4,7 +4,7 @@ > JustOneCacophony is an experimental/test implementation of E-AC-3 JOC for studying JOC parsing, reconstruction, rendering, and the associated mathematics. -The project can extract and parse EMDF, ID14 JOC parameters, and ID11 OAMD metadata from common E-AC-3 JOC streams. It combines those data with the core 5.1 PCM decoded by FFmpeg, reconstructs LFE plus 15 object channels, and writes ADM BWF, a WAV file for a selected speaker layout, or direct binaural stereo using a standard SOFA HRTF. +The project can extract and parse EMDF, ID14 JOC parameters, and ID11 OAMD metadata from common E-AC-3 JOC streams. It combines those data with the core PCM decoded by FFmpeg (N=5 or 7 full-band channels), reconstructs LFE plus 15 object channels, and writes ADM BWF, a WAV file for a selected speaker layout, or direct binaural stereo using a standard SOFA HRTF. This is research code, not a complete, standards-compliant, or production-grade JOC decoder. It covers only the stream forms currently implemented. Unknown variants fail explicitly—because when the math goes wrong, all that may remain is the cacophony. @@ -19,14 +19,14 @@ This is research code, not a complete, standards-compliant, or production-grade - Run public SOFA binaural rendering directly from `pcm16 + ID11/OAMD`, without a temporary ADM BWF. - Keep the binaural DSP in float64/complex128, including 961-sample latency compensation, cross-frame state, and the room tail. - Use a shared float32/PCM24 WAV writer and explicit PCM24 clipping policy for direct outputs. -- Use the NumPy backend or an optional C++20 core through `ctypes`; `auto` falls back to Python when the native library is unavailable. +- Implement everything in pure Python on NumPy; - Read or write metadata sidecars and produce metadata, timing, and output reports. ## Processing flow ```text M4A / E-AC-3 - ├─ FFmpeg extracts E-AC-3 and decodes the core 5.1 PCM + ├─ FFmpeg extracts E-AC-3 and decodes the core PCM (N=5 or 7) ├─ EMDF → ID14 JOC parameters → object matrix ├─ core PCM → analysis QMF → parameter interpolation → inverse QMF ├─ ID11 OAMD → object positions and timing @@ -36,7 +36,7 @@ M4A / E-AC-3 └─ direct ID11 timeline + SOFA HRTF → binaural WAV ``` -The Python and C++ backends follow the same mathematics for JOC object reconstruction and speaker rendering. The public SOFA binaural backend currently runs in Python; bitstream parsing, the OAMD timeline, and CLI behavior also remain in Python. +JOC object reconstruction, speaker rendering, and the public SOFA binaural rendering follow the same mathematics: analysis QMF, parameter interpolation, the object matrix, and inverse QMF. Bitstream parsing, the OAMD timeline, and CLI behavior are in Python as well. ## Requirements @@ -45,7 +45,6 @@ The Python and C++ backends follow the same mathematics for JOC object reconstru - h5py 3.8+ - SciPy 1.10+ - A standalone FFmpeg executable; `ffmpeg-python` is not required. FFmpeg is discovered through `PATH` by default or selected with `--ffmpeg`. On startup the decoder options are probed with `ffmpeg -h decoder=eac3`: a missing E-AC-3 decoder or `-drc_scale` is a hard error, while a missing `-target_level` only fails when `--eac3-target-level` is used -- Optional: CMake and a C++20 toolchain to build the native core Install the Python dependency in a project-specific environment: @@ -70,9 +69,7 @@ python main.py input.m4a Select a backend or output path: ```powershell -python main.py input.eac3 -o output.adm.wav --backend python -python main.py input.m4a --backend native --native-threads 2 -python main.py input.m4a --native-library lib/eac3joc_core.dll +python main.py input.eac3 -o output.adm.wav ``` Write a speaker-layout WAV directly: @@ -168,12 +165,10 @@ python main.py input.m4a --eac3-target-level -27 # dialnorm-referenced level - `--eac3-target-level` (`-31`–`0`, default `0` = off) maps to FFmpeg `-target_level`: a static per-frame gain of about `target_level - dialnorm` dB, independent of and stackable with `--eac3-drc-scale`. dialnorm is a per-stream - property (measured Apple Music Atmos streams are about `-18` to `-19` dB, so - `-27` is roughly `8`–`9` dB of attenuation). -- The level change is expected: compared with the FFmpeg default, measured - tracks move by `0` to `-2.15` dB peak and `0` to `-1.69` dB RMS (direction - depends on the stream `dynrng`), so `output_clip.peak` and the PCM24 clipping - decision in `.report.json` change accordingly. + property, so the attenuation that `-27` applies varies from stream to stream. +- The level change is expected: whether the gain is applied depends on the stream + `dynrng`, so `output_clip.peak` and the PCM24 clipping decision in + `.report.json` change accordingly. - `--gain-db` is a static gain applied **after** reconstruction (float64 on the binaural path) and is not the same thing as decode-side DRC, which is block-varying; do not use `--gain-db` to cancel it. @@ -233,36 +228,16 @@ python main.py --help Without `-o`, output still goes to `output/` at the repository root. The directory move intentionally preserves this behavior. -## Native core - -The repository does not include native binaries by default. Download a prebuilt runtime for the current platform from a project Release, or build one locally, then place the runtime library under `lib/` at the repository root; create the directory if it is absent. To build it yourself, run CMake from the repository root: - -```powershell -cmake -S native -B build/cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PWD/lib" -cmake --build build/cmake --config Release -cmake --install build/cmake --config Release -``` - -The runtime lookup order is: - -1. `--native-library`; -2. `EAC3JOC_NATIVE_LIBRARY`; -3. the standard platform library name under `lib/`. - -See the [native-core notes](docs/native.en.md) for ABI, state, and precision details. - ## Repository layout ```text JustOneCacophony/ ├─ main.py command-line entry point ├─ src/ Python implementation modules -├─ native/ C/C++ acceleration core, C ABI, and required table data ├─ data/ Python runtime table data -├─ lib/ native runtime drop-in directory (create as needed) ├─ HRTF/ user HRTF data directory (create as needed, git-ignored) ├─ output/ output directory (create as needed; the .jochrtf cache defaults to its hrtf-cache subdirectory) -├─ docs/ math and native-core notes in both languages +├─ docs/ math and binaural notes in both languages ├─ requirements.txt Python dependency ├─ README.md Chinese documentation └─ README.en.md English documentation @@ -288,16 +263,24 @@ See the [mathematical notes](docs/math.en.md) for the equations used by the deco ## Known limitations - Only the common contiguous EMDF transport is covered. Fragmented transport across multiple audio-block skip fields is not covered. +- All `joc_dmx_config_idx` values 0..4 are covered ($N=5,7,7,5,7$; the phase switch $\sigma(k)$ gates the Ls/Rs $-j$ rotation and the band-0 FIR together). The 7-channel configurations need an 8-channel bed and a dependent-substream `chanmap` to identify the extra pair: a missing independent substream or a missing `chanmap` is still a variant error, while a `chanmap` that disagrees with $k$ about the extra pair, or an OAMD bed mask that does, is recorded as a diagnostic and the frame still renders. The extra pair's **identity** is metadata only and never reaches the DSP (section 10.1); the bed mapping follows the channel codes the `chanmap` declares. +- When one audio frame holds several EMDF containers carrying ID11/ID14 (one of them may be something other than the frame's JOC parameters: its ID14 may declare more objects than its bytes can hold, and its ID11 may carry no group id), the container is chosen by the association rule of ETSI TS 103 420 section 8.2 and its Table 56: **the container whose OAMD and JOC payloads share a group id and whose ID14 parses completely** wins, then one whose ID14 merely parses; only a unique best score is used, the others are recorded as a diagnostic (`emdf/extra_container_ignored`), and a tie or all-zero score stays a variant error. +- The extra pair's identity follows TS 103 420 Table 47/Table 48 ($k=1\to$Lb/Rb, $k\in\{2,4\}\to$Tfl/Tfr, $N=5,7,7,5,7$); Annex E.3 requires a dependent-substream `chanmap`, when present, to indicate one of the Table 47 configurations, so `*_5_1_2top_*` (chanmap says Lts/Rts) does not satisfy that "shall". Such material is tolerated with a diagnostic instead of being refused. +- An audio frame with no JOC EMDF at all (for example when the encoder wrote none for the first frame) emits silent object channels and is recorded in the `[diagnostics]` line and the JSON report's `diagnostics` field. - The speaker and SOFA binaural paths currently cover ordinary point objects; extent, spread, diffuse, divergence, channel lock, and similar controls are outside the supported scope. - OAMD trim elements are boundary-checked and skipped; warp, balance, and trim parameters are not applied to raw object trajectories or speaker rendering. - Multi-data-point streams, uncommon band configurations, and unusual OAMD scheduling have less coverage than common 12-band, single-data-point material. +- `joc_clipgain` is folded into the **analysis-side scalar** (`scalar = 1/16 x g`, with `g` from the + `x:3 | y:5` field), so the x16 at the output cancels the 1/16 and the clamp sits **after** the gain. + The scalar is kept per frame: the first 256 samples ramp linearly from the previous frame's value and + the rest stays constant. For `g = 1` the interpolation is the identity and + `clamp(x)·1 == clamp(x·1)`. - A speaker limiter is outside the current primary formula. - The SOFA importer currently supports the strict `SimpleFreeFieldHRIR` FIR subset; other SOFA conventions require explicit adapters. -- The binaural runtime is fixed at 48 kHz, fifth order, and one measurement-radius shell at a time; the public binaural backend defaults to the native accelerator and falls back to Python when the native library is unavailable. -- ADM output, native binaries, speaker layouts, and binaural models still need broader interoperability checks across platforms, players, and real material. +- The binaural runtime is fixed at 48 kHz, fifth order, and one measurement-radius shell at a time. +- ADM output, speaker layouts, and binaural models still need broader interoperability checks across platforms and players. ## Documentation - [Mathematical notes](docs/math.en.md) · [中文](docs/math.md) -- [Native-core notes](docs/native.en.md) · [中文](docs/native.md) - [Binaural rendering](docs/binaural.en.md) · [中文](docs/binaural.md) diff --git a/README.md b/README.md index 5b14216..7caf17c 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ > JustOneCacophony 是一个 E-AC-3 JOC 的实验性 / 测试实现,用于研究 JOC 的解析、重建、渲染以及相关数学过程。 -项目可以从常见 E-AC-3 JOC 码流中提取并解析 EMDF、ID14 JOC 参数和 ID11 OAMD 元数据,结合 FFmpeg 解码出的核心 5.1 PCM 重建 LFE 与 15 路对象 PCM,并输出 ADM BWF、指定扬声器布局的 WAV,或使用标准 SOFA HRTF 直接输出双耳 WAV。 +项目可以从常见 E-AC-3 JOC 码流中提取并解析 EMDF、ID14 JOC 参数和 ID11 OAMD 元数据,结合 FFmpeg 解码出的核心 PCM(N=5 或 7 个全频段通道)重建 LFE 与 15 路对象 PCM,并输出 ADM BWF、指定扬声器布局的 WAV,或使用标准 SOFA HRTF 直接输出双耳 WAV。 这是研究代码,不是完整、标准兼容或生产级的 JOC 解码器。它只覆盖当前已实现的码流形态;遇到未知变体时会明确报错,而不是假装一切都很和谐——如果哪里算错了,它可能就真的只剩 cacophony 了。 @@ -19,14 +19,14 @@ - 从 `pcm16 + ID11/OAMD` 直接运行公开 SOFA 双耳渲染,不生成临时 ADM BWF; - 双耳 DSP 全程使用 float64/complex128,并保留 961-sample latency compensation、跨帧状态和 room 尾声; - 直接输出统一支持 float32 或 PCM24 WAV,并在 PCM24 削波前提供明确处理策略; -- 使用 NumPy 后端,或通过 `ctypes` 调用可选的 C++20 原生核;`auto` 模式在原生库不可用时回退到 Python; +- 全程使用 NumPy 的纯 Python 实现; - 读取或写入 metadata sidecar,并生成元数据、运行时间和输出摘要。 ## 处理流程 ```text M4A / E-AC-3 - ├─ FFmpeg 提取 E-AC-3 并解码核心 5.1 PCM + ├─ FFmpeg 提取 E-AC-3 并解码核心 PCM(N=5 或 7) ├─ EMDF → ID14 JOC 参数 → 对象矩阵 ├─ 核心 PCM → analysis QMF → 参数插值 → inverse QMF ├─ ID11 OAMD → 对象位置与时间轨迹 @@ -36,9 +36,8 @@ M4A / E-AC-3 └─ ID11 直接时间轴 + SOFA HRTF → 双耳 WAV ``` -Python 与 C++ 后端在 JOC 对象重建、扬声器渲染和公开 SOFA 双耳渲染中使用同一组 -数学过程;native 双耳后端与 Python 参考实现逐值一致(差异 < 1e-9)。位流解析、 -OAMD 时间轴和命令行逻辑在 Python 中。 +JOC 对象重建、扬声器渲染与公开 SOFA 双耳渲染使用同一组数学过程:analysis QMF、 +参数插值、对象矩阵、inverse QMF。位流解析、OAMD 时间轴和命令行逻辑同样在 Python 中。 ## 环境 @@ -47,7 +46,6 @@ OAMD 时间轴和命令行逻辑在 Python 中。 - h5py 3.8+ - SciPy 1.10+ - 独立的 FFmpeg 可执行程序;不需要 `ffmpeg-python`。默认从 `PATH` 查找,也可通过 `--ffmpeg` 指定可执行文件路径。启动时会探测 `ffmpeg -h decoder=eac3`:缺 E-AC-3 解码器或 `-drc_scale` 直接报错,缺 `-target_level` 只在使用 `--eac3-target-level` 时报错 -- 可选:支持 C++20 的 CMake 工具链,用于自行构建原生核 建议在项目专用虚拟环境中安装依赖: @@ -69,12 +67,10 @@ python main.py input.m4a --ffmpeg C:\path\to\ffmpeg.exe python main.py input.m4a ``` -选择后端或输出路径: +指定输出路径: ```powershell -python main.py input.eac3 -o output.adm.wav --backend python -python main.py input.m4a --backend native --native-threads 2 -python main.py input.m4a --native-library lib/eac3joc_core.dll +python main.py input.eac3 -o output.adm.wav ``` 直接输出扬声器 WAV: @@ -152,8 +148,8 @@ python main.py input.m4a --eac3-target-level -27 # 按码流 dialnorm 归一 ``` - `--eac3-drc-scale`(`0`~`6`,默认 `0`)对应 FFmpeg 的 `-drc_scale`:每个 E-AC-3 block 的增益为 `dynrng 因子 ^ 该值`。`0` 关闭 DRC;`1` 为码流作者意图;`>1` 非对称(响处全压、轻处增强)。 -- `--eac3-target-level`(`-31`~`0`,默认 `0` 不施加)对应 FFmpeg 的 `-target_level`:按每帧 dialnorm 施加静态增益,约 `target_level - dialnorm` dB,与 `--eac3-drc-scale` 相互独立、可叠加。dialnorm 是逐码流属性(实测 Apple Music Atmos 流约 `-18`~`-19` dB,故 `-27` 约等于衰减 `8`~`9` dB)。 -- 电平变化是预期的:与 FFmpeg 默认值相比,实测曲目峰值变化 `0`~`-2.15` dB、RMS `0`~`-1.69` dB(方向取决于码流 `dynrng`),`.report.json` 的 `output_clip.peak` 与 int24 削波判定会随之变化。 +- `--eac3-target-level`(`-31`~`0`,默认 `0` 不施加)对应 FFmpeg 的 `-target_level`:按每帧 dialnorm 施加静态增益,约 `target_level - dialnorm` dB,与 `--eac3-drc-scale` 相互独立、可叠加。dialnorm 是逐码流属性,取值随码流而异,因此 `-27` 对应的实际衰减也随码流变化。 +- 电平变化是预期的:是否施加增益取决于码流 `dynrng`,方向与幅度逐码流而异,`.report.json` 的 `output_clip.peak` 与 int24 削波判定会随之变化。 - `--gain-db` 是**重建之后**的静态增益(双耳路径 float64),与解码级 DRC 不是一回事;解码级 DRC 是按 block 时变的,不要用 `--gain-db` 去抵消它。 - `.report.json` 的 `ffmpeg` 字段记录 FFmpeg 版本与实际下发的解码选项(`version`、`eac3_decode_options`)。 @@ -203,36 +199,16 @@ python main.py --help 未指定 `-o` 时,输出仍写入仓库根目录的 `output/`。这是文件移动后特意保持的原有行为。 -## 原生核 - -仓库默认不附带原生二进制。可以从项目 Release 下载适合当前平台的预构建运行库,或自行构建,然后把运行库直接放入仓库根目录的 `lib/`;若该目录不存在,创建即可。自行构建时可从仓库根目录使用 CMake: - -```powershell -cmake -S native -B build/cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PWD/lib" -cmake --build build/cmake --config Release -cmake --install build/cmake --config Release -``` - -运行时查找顺序为: - -1. `--native-library`; -2. `EAC3JOC_NATIVE_LIBRARY`; -3. `lib/` 下当前平台的标准库文件名。 - -详细 ABI、状态与精度说明见[原生核说明](docs/native.md)。 - ## 目录结构 ```text JustOneCacophony/ ├─ main.py 命令行启动入口 ├─ src/ Python 实现模块 -├─ native/ C/C++ 加速核、C ABI 与必要表数据 ├─ data/ Python 运行时表数据 -├─ lib/ 原生运行库投放目录(按需创建) ├─ HRTF/ 用户 HRTF 数据目录(按需创建,git 忽略) ├─ output/ 输出目录(按需创建;.jochrtf 缓存默认在其 hrtf-cache 子目录) -├─ docs/ 数学与原生核文档(中英文) +├─ docs/ 数学与双耳文档(中英文) ├─ requirements.txt Python 依赖 ├─ README.md 中文说明 └─ README.en.md English documentation @@ -258,16 +234,22 @@ JustOneCacophony/ ## 已知限制 - 当前只覆盖常见 continuous EMDF transport;跨多个 audio-block skip field 的碎片化 transport 尚未覆盖。 +- 覆盖 `joc_dmx_config_idx` 0..4 的全部档位($N=5,7,7,5,7$,相位开关 $\sigma(k)$ 同时门控 Ls/Rs 的 $-j$ 旋转与 band-0 FIR)。7 通道档位必须解出 8 通道床,且需要依赖子流的 `chanmap` 才能确定额外那一对:缺少核心子流或缺少 `chanmap` 仍按变体报错;`chanmap` 声明的额外对与 $k$ 不一致、或 OAMD bed 掩码与 $k$ 不一致时,只记入 `diagnostics` 并继续解码——额外那对的**身份**只进元数据、不进 DSP(§10.1),床映射按 `chanmap` 的通道码做。 +- 同一音频帧里出现多个含 ID11/ID14 的 EMDF 容器时(其中一个容器可能不是本帧的 JOC 参数:ID14 声明的对象数超出其字节所能容纳的范围,ID11 也可能没有 groupid),按 ETSI TS 103 420 §8.2 + Table 56 的关联规则打分选取:**OAMD 与 JOC 载荷 groupid 相同且 ID14 能完整解析**的容器优先,其次只看 ID14 能否完整解析;只有唯一最高分才采用,其余记入 `diagnostics`(`emdf/extra_container_ignored`),并列或全部为 0 仍报变体。 +- 额外那一对的身份以 TS 103 420 Table 47/Table 48 为准($k=1\to$Lb/Rb,$k\in\{2,4\}\to$Tfl/Tfr,$N=5,7,7,5,7$);Annex E.3 规定依赖子流的 `chanmap`(若存在)**应**指明 Table 47 的某种配置,因此 `*_5_1_2top_*`(chanmap 写 Lts/Rts)属于不满足 "shall" 的素材,我们按"记诊断 + 继续解码"容错处理。 +- 音频帧内没有任何 JOC EMDF 时(例如编码器未写首帧元数据),该帧的对象通道静音,并在 stderr 的 `[diagnostics]` 与 JSON 报告的 `diagnostics` 里记一笔。 - 扬声器与 SOFA 双耳路径当前只覆盖普通点对象;extent、spread、diffuse、divergence、channel lock 等对象控制不在支持范围内。 - OAMD trim element 会按声明边界校验并跳过;warp、balance 和 trim 参数不应用于当前原始对象轨迹或扬声器渲染。 - 多数据点、少见参数带配置和特殊 OAMD 调度的覆盖度低于常见 12-band、单数据点素材。 +- `joc_clipgain` 在**分析端**并入分析标量:`标量 = 1/16 × g`(`g = 1 + (y/32)·2^(x−4)`),出口的 + ×16 抵消 1/16,净效果是 `×g`,clamp 因此处在增益**之后**。标量按帧保存两个槽(上一帧值 → 本帧值), + 本帧前 256 个采样线性过渡、之后为常数,复位后的第一帧直接取新值;`g = 1` 时插值恒等,且 `clamp(x)·1 == clamp(x·1)`。 - 扬声器 limiter 不属于当前实现的主公式。 - SOFA importer 当前严格支持 `SimpleFreeFieldHRIR` FIR;其它 SOFA convention 需要显式 adapter。 -- 双耳 runtime 固定 48 kHz、五阶和一次选择一个 measurement-radius shell;公开双耳默认走 native 加速,原生库不可用时自动回退 Python。 -- ADM 输出、原生库、扬声器布局和双耳模型仍需在更多平台、播放器与真实素材上确认互操作性。 +- 双耳 runtime 固定 48 kHz、五阶和一次选择一个 measurement-radius shell。 +- ADM 输出、扬声器布局和双耳模型仍需在更多平台与播放器上确认互操作性。 ## 文档 - [数学说明](docs/math.md) · [English](docs/math.en.md) -- [原生核说明](docs/native.md) · [English](docs/native.en.md) - [双耳渲染](docs/binaural.md) · [English](docs/binaural.en.md) diff --git a/data/README.en.md b/data/README.en.md index aafe4ba..4703c35 100644 --- a/data/README.en.md +++ b/data/README.en.md @@ -17,9 +17,7 @@ joc_huff_code_5ch_pos_index_sparse int64[4,2] joc_huff_code_7ch_pos_index_sparse int64[6,2] ``` -`src/joc_qmf.py` loads the QMF tables, while `src/joc_decode.py` loads the JOC Huffman trees. Python does not read C/C++ headers under `native/`. - -The corresponding native data are stored in `native/src/qmf_tables.h` and `native/src/joc_huffman_tables.h`. Changes on either side should update the other and be checked for value-by-value agreement. +`src/joc_qmf.py` loads the QMF tables, while `src/joc_decode.py` loads the JOC Huffman trees. ## Binaural rendering tables diff --git a/data/README.md b/data/README.md index 8d29578..14a223a 100644 --- a/data/README.md +++ b/data/README.md @@ -17,9 +17,7 @@ joc_huff_code_5ch_pos_index_sparse int64[4,2] joc_huff_code_7ch_pos_index_sparse int64[6,2] ``` -`src/joc_qmf.py` 读取 QMF 表,`src/joc_decode.py` 读取 JOC Huffman 树。Python 不读取 `native/` 下的 C/C++ 头文件。 - -原生侧对应数据分别位于 `native/src/qmf_tables.h` 与 `native/src/joc_huffman_tables.h`。修改任何一侧时,应同步更新另一侧并进行逐值一致性检查。 +`src/joc_qmf.py` 读取 QMF 表,`src/joc_decode.py` 读取 JOC Huffman 树。 ## 双耳渲染表 diff --git a/docs/binaural.en.md b/docs/binaural.en.md index 5a4d77d..51a8b31 100644 --- a/docs/binaural.en.md +++ b/docs/binaural.en.md @@ -254,16 +254,9 @@ late sends, the unitary FDN, the 120–180 Hz cosine-squared LFE low-pass, and r calibration are JOC project-defined behavior, not constants published by SOFA or Dolby. -The public SOFA binaural renderer defaults to the C++20 native core under -`--backend auto/native` (`ejoc_sofa_binaural_*` in `lib/eac3joc_core.dll`): the -filterbank, the SH direction-field evaluation, the per-object direct/early -histories and the shared FDN all run natively, while Python only compiles the -SOFA source and issues the per-512-sample metadata updates. When the native -library is unavailable the renderer falls back to the Python/NumPy reference -implementation; the two agree to better than 1e-9. `--backend python` forces -the Python backend. -`--backend` still selects native/Python JOC reconstruction and speaker rendering; -native acceleration for the public binaural DSP is outside the current API. +The filterbank, the SH direction-field evaluation, the per-object direct/early +histories and the shared FDN all run in Python/NumPy, with the binaural DSP kept +in float64/complex128 throughout. ## Technical references and rights boundary diff --git a/docs/binaural.md b/docs/binaural.md index 43bbdff..663a2cf 100644 --- a/docs/binaural.md +++ b/docs/binaural.md @@ -224,11 +224,8 @@ Near/Mid/Far、equal-power direct level、六面 shoebox 一阶 image source、l unitary FDN、LFE 120–180 Hz cosine-squared 低通及 room calibration 都是 JOC 项目定义行为,不是 SOFA 或 Dolby 公布常数。 -公开 SOFA 双耳渲染在 `--backend auto/native` 下默认走 C++20 原生核 -(`lib/eac3joc_core.dll` 的 `ejoc_sofa_binaural_*` 接口:filterbank、SH 方向场求值、 -逐对象 early/direct 历史与共享 FDN 全部在原生侧执行,Python 只做 SOFA 编译与每 -512-sample 的元数据更新);原生库不可用时自动回退 Python/NumPy 参考实现,两者 -逐值一致(差异 < 1e-9)。`--backend python` 强制使用 Python 后端。 +filterbank、SH 方向场求值、逐对象 early/direct 历史与共享 FDN 全部在 Python/NumPy +一侧执行,双耳 DSP 全程 float64/complex128。 ## 技术引用与权利边界 diff --git a/docs/math.en.md b/docs/math.en.md index 20e55b1..b643f2a 100644 --- a/docs/math.en.md +++ b/docs/math.en.md @@ -150,7 +150,12 @@ G_{\mathrm{clip}}= 1+\frac{y}{32}2^{x-4}. $$ -It is applied to object PCM after inverse QMF and does not apply to LFE. +It is folded into the analysis-side scalar $S=\frac{1}{16}G_{\mathrm{clip}}$, which multiplies +every core-bed sample before the QMF analysis; the $\times16$ after the object inverse QMF cancels the +$1/16$, so the net effect is a multiply by $G_{\mathrm{clip}}$ and the $[-1,1]$ clamp sits **after** the +gain. The scalar keeps two per-frame slots: the first 256 samples of a frame ramp linearly from the +previous frame's value to this frame's value and the rest stays constant, and the first frame after a +reset takes the new value directly. LFE bypasses this scalar. ## 3. Parameter-band expansion and time interpolation @@ -231,36 +236,31 @@ This consists of the analysis window/polyphase stage, modulation, a 64-point FFT ## 5. QMF-domain processing of core channels -L, R, and C are delayed by ten QMF slots before entering the object matrix: +$k$ selects the core channel count $N\in\{5,7\}$ ($k=0\ldots4$ maps to $N=5,7,7,5,7$) and the +phase switch $\sigma(k)=1\iff k\in\{3,4\}$. Every core channel is delayed by ten QMF slots +before entering the object matrix; the $-j$ rotation ($b>0$) and the band-0 21-tap complex FIR +are both gated by that same switch and applied to Ls and Rs only: $$ -\widehat X_{c,b,t}=X_{c,b,t-10}, -\qquad c\in\{L,R,C\}. -$$ - -Ls and Rs use the same ten-slot delay and a $-j$ rotation for $b>0$: - -$$ -\widehat X_{c,b,t}=-jX_{c,b,t-10}, -\qquad c\in\{Ls,Rs\},\ b>0. -$$ - -Band 0 of each surround channel additionally passes through a 21-tap complex FIR: - -$$ -\widehat X_{c,0,t}= -\sum_{k=0}^{20}h_kX_{c,0,t-k}. +\widehat X_{c,b,t}= +\begin{cases} +-j\,X_{c,b,t-10}, & \sigma(k)=1,\ c\in\{Ls,Rs\},\ b>0,\\[1mm] +\sum_{m=0}^{20}h_mX_{c,0,t-m}, & \sigma(k)=1,\ c\in\{Ls,Rs\},\ b=0,\\[1mm] +X_{c,b,t-10}, & \text{otherwise}. +\end{cases} $$ +For $N=7$ the sixth and seventh core channels (Lb/Rb or Tfl/Tfr) behave like the front channels +and only take the ten-slot delay: channel identity affects metadata and rendering, not the DSP. These delays and filter histories are decoder state and cannot be reset independently for every frame. ## 6. Object matrix -For each object $o$, subband $b$, and slot $t$, the object's frequency-domain value is a linear combination of the five core channels: +For each object $o$, subband $b$, and slot $t$, the object's frequency-domain value is a linear combination of the $N$ core channels: $$ Z_{o,b,t}= -\sum_{c=0}^{4} +\sum_{c=0}^{N-1} M_{o,c,b,t}\widehat X_{c,b,t}. $$ diff --git a/docs/math.md b/docs/math.md index 27eaf15..7450451 100644 --- a/docs/math.md +++ b/docs/math.md @@ -150,7 +150,10 @@ G_{\mathrm{clip}}= 1+\frac{y}{32}2^{x-4}. $$ -它在对象 inverse QMF 之后作用于对象 PCM,不作用于 LFE。 +它并入分析端标量 $S=\frac{1}{16}G_{\mathrm{clip}}$,乘在核心 bed 的每个采样上(QMF 分析之前); +对象 inverse QMF 之后的 $\times16$ 抵消 $1/16$,净效果是乘 $G_{\mathrm{clip}}$,因此钳位 $[-1,1]$ +处在增益**之后**。标量按帧保留两个槽,本帧前 256 个采样从上一帧值线性过渡到本帧值、之后为常数, +复位后的第一帧直接取新值。LFE 走旁通通路,不参与该标量。 ## 3. 参数带展开与时间插值 @@ -231,36 +234,30 @@ $$ ## 5. 核心声道的 QMF 域处理 -L、R、C 在进入对象矩阵前延迟 10 个 QMF 时槽: +$k$ 决定核心通道数 $N\in\{5,7\}$($k=0\ldots4$ 对应 $N=5,7,7,5,7$)与相位开关 +$\sigma(k)=1\iff k\in\{3,4\}$。所有核心声道在进入对象矩阵前延迟 10 个 QMF 时槽; +$-j$ 旋转($b>0$)与 band-0 的 21-tap 复 FIR **由同一个开关**只作用于 Ls、Rs: $$ -\widehat X_{c,b,t}=X_{c,b,t-10}, -\qquad c\in\{L,R,C\}. +\widehat X_{c,b,t}= +\begin{cases} +-j\,X_{c,b,t-10}, & \sigma(k)=1,\ c\in\{Ls,Rs\},\ b>0,\\[1mm] +\sum_{m=0}^{20}h_mX_{c,0,t-m}, & \sigma(k)=1,\ c\in\{Ls,Rs\},\ b=0,\\[1mm] +X_{c,b,t-10}, & \text{其他情况}. +\end{cases} $$ -Ls、Rs 同样延迟 10 个时槽,并在 $b>0$ 时作 $-j$ 旋转: - -$$ -\widehat X_{c,b,t}=-jX_{c,b,t-10}, -\qquad c\in\{Ls,Rs\},\ b>0. -$$ - -环绕声道的 band 0 还经过 21-tap 复 FIR: - -$$ -\widehat X_{c,0,t}= -\sum_{k=0}^{20}h_kX_{c,0,t-k}. -$$ - -这些延迟和滤波历史属于解码状态,不能按帧独立清零。 +$N=7$ 时第 6、7 个核心通道(Lb/Rb 或 Tfl/Tfr)与前级相同,只做 10 时槽延迟: +通道身份只影响元数据与渲染,不进入 DSP。这些延迟和滤波历史属于解码状态, +不能按帧独立清零。 ## 6. 对象矩阵 -对每个对象 $o$、子带 $b$ 和时槽 $t$,对象频域值为五个核心声道的线性组合: +对每个对象 $o$、子带 $b$ 和时槽 $t$,对象频域值为 $N$ 个核心声道的线性组合: $$ Z_{o,b,t}= -\sum_{c=0}^{4} +\sum_{c=0}^{N-1} M_{o,c,b,t}\widehat X_{c,b,t}. $$ diff --git a/docs/native.en.md b/docs/native.en.md deleted file mode 100644 index d50cad7..0000000 --- a/docs/native.en.md +++ /dev/null @@ -1,178 +0,0 @@ -# JustOneCacophony native-core notes - -[中文](native.md) · [Back to README](../README.en.md) - -## 1. Responsibility boundary - -`native/` contains only the state-heavy, frequently called DSP and speaker-rendering kernels. High-level EMDF/JOC/OAMD parsing, error reporting, ADM assembly, and the CLI remain in Python. - -Python calls a C ABI through the standard-library `ctypes` module. The native core does not use pybind11, Cython, FFTW, MKL, or OpenMP. It is an optional acceleration path and does not expand the set of supported stream variants. - -Main files: - -```text -native/include/eac3joc_core.h C ABI -native/src/eac3joc_core.cpp JOC/QMF object reconstruction -native/src/speaker_renderer.cpp object-to-speaker rendering -native/src/qmf_tables.h QMF tables -native/src/speaker_layouts.h layout tables -native/src/joc_huffman_tables.h JOC Huffman tables -src/native_renderer.py JOC ctypes bridge -src/speaker_native_renderer.py speaker ctypes bridge -``` - -## 2. JOC rendering ABI - -An opaque renderer owns all cross-frame state. Its main call is: - -```c -int ejoc_renderer_process( - ejoc_renderer_handle handle, - const float* bed5_planar, /* [5][1536] */ - const float* lfe, /* [1536] or NULL */ - uint32_t object_mask, - const uint8_t* n_bands, /* [15] */ - const uint8_t* n_dpoints, /* [15] */ - const uint8_t* slope_idx, /* [15] */ - const uint8_t* offset_ts, /* [15][2] */ - const double* dq, /* [15][2][5][23] */ - double clipgain, - float phase_new, - float output_scale, - float* output16_planar); /* [16][1536] */ -``` - -Python performs JOC Huffman decoding, differential reconstruction, and dequantization before the call, with the dense and sparse syntaxes sharing one entry point. The native core consumes the already dequantized `dq` in double precision, and both syntaxes have the same layout at that ABI. - -Thread control is exposed as: - -```c -int ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t total_threads); -uint32_t ejoc_renderer_thread_count(ejoc_renderer_handle handle); -``` - -`total_threads` includes the calling thread. Frames must be submitted sequentially to one renderer instance; the instance may parallelize work across objects and analysis channels. - -## 3. Cross-frame state - -Each JOC renderer stores: - -- analysis FIFO: `double[5][9][64]`; -- L/R/C analysis delay: `float[3][10][64]`; -- Ls/Rs QMF delay: `complex[2][10][64]`; -- Ls/Rs band-0 FIR history: `complex[2][20]`; -- previous matrix interpolation values: `double[15][5][64]`; -- inverse-QMF state: `double[15][640]`; -- LFE delay: `double[1217]`. - -This state belongs to the renderer instance. Processing cannot be arbitrarily segmented or reordered without a corresponding state checkpoint. - -## 4. FFT, QMF, and precision - -The native core contains a fixed 64-point radix-2 complex FFT: - -- analysis QMF uses a forward FFT followed by division by 64; -- inverse QMF uses the fixed reorder, rotation, and 640-value active-window state; -- no external FFT library is called. - -The JOC path uses: - -- float32 core-PCM input; -- double matrices, complex QMF, FFT, FIR, and cross-frame state; -- float32 phase and final gain; -- float32 16-channel object output. - -## 5. Speaker-rendering ABI - -The same shared library exports object-to-speaker rendering: - -```c -uint32_t ejoc_speaker_layout_channel_count(uint32_t speaker_bitfield); - -ejoc_speaker_renderer_handle -ejoc_speaker_renderer_create(uint32_t speaker_bitfield); - -int ejoc_speaker_renderer_process( - ejoc_speaker_renderer_handle handle, - const float* objects16_interleaved, - uint32_t sample_count, - uint32_t metadata_count, - const uint32_t* metadata_offsets, - const uint32_t* ramp_durations, - const uint16_t* positions_q15, - const uint8_t* region_indices, - const uint8_t* height_enabled, - const double* object_gains, - double* output_interleaved); -``` - -Input channel 0 is LFE and channels 1–15 are objects. Each metadata entry is an object-state snapshot. `sample_count` must be a multiple of 32; unfinished gain ramps remain in the handle and continue across calls. - -The speaker path uses float32 object input, double coordinates/gains/accumulation, and interleaved double output. Quantization to float32 or PCM24 happens when the WAV is written. - -Supported layouts: - -```text -2.0 3.1 5.1 7.1 5.1.2 5.1.4 7.1.2 7.1.4 9.1.4 9.1.6 -``` - -## 6. Binaural-rendering ABI - -The shared library provides a 512-sample float64 binaural DSP interface: - -```c -ejoc_binaural_renderer_handle ejoc_binaural_renderer_create(void); -int ejoc_binaural_renderer_configure_kernels(...); -int ejoc_binaural_renderer_configure_room(...); -int ejoc_binaural_renderer_process( - ejoc_binaural_renderer_handle handle, - const double* input16_interleaved, /* [512][16] */ - const double* gains_complex, /* [16][2][77][2] */ - const double* room_sends, /* [16] */ - double output_gain, - double* output_stereo_interleaved); /* [512][2] */ -``` - -Python parses the model, evaluates the OAMD timeline, and supplies complex gains and room sends every 512 samples. The C++ handle owns QMF, hybrid, recursive-room, and QMF-synthesis state. Inputs, state, accumulation, and output are double/complex double. - -## 7. Building - -The CMake definition is `native/CMakeLists.txt`. Run from the repository root: - -```powershell -cmake -S native -B build/cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PWD/lib" -cmake --build build/cmake --config Release -cmake --install build/cmake --config Release -``` - -Platform runtime names: - -```text -Windows lib/eac3joc_core.dll -Linux lib/libeac3joc_core.so -macOS lib/libeac3joc_core.dylib -``` - -The MSVC configuration uses the static CRT. Other runtime dependencies depend on the platform and toolchain and should be checked independently before publishing a prebuilt library. - -The repository does not include native binaries by default. A prebuilt Release runtime or a locally built runtime can be placed directly under `lib/`. - -## 8. Runtime lookup and fallback - -Lookup order: - -1. explicit `--native-library`; -2. `EAC3JOC_NATIVE_LIBRARY`; -3. the standard platform filename under `lib/`. - -`--backend auto` falls back to NumPy when loading fails, and `--backend python` skips native discovery. The current CLI also prints the failure and falls back for `--backend native`; this existing behavior should not be read as successful native execution. - -## 9. Implementation boundaries - -- The native layer accepts only dense-JOC data already parsed by Python. -- The ABI fixes a 1536-sample JOC frame, at most 15 objects, at most 23 parameter bands, and at most 2 data points. -- The shared library and Python bridge must report the same ABI version. -- Only the ABI and data types are specified across platforms; bit-identical float64 results are not guaranteed. -- Private table headers under `native/src/` serve the native side only. The current repository does not include the scripts that generated those headers. - -See the [mathematical notes](math.en.md) for the related formulas. diff --git a/docs/native.md b/docs/native.md deleted file mode 100644 index f5ef3f3..0000000 --- a/docs/native.md +++ /dev/null @@ -1,191 +0,0 @@ -# JustOneCacophony 原生核说明 - -[English](native.en.md) · [返回 README](../README.md) - -## 1. 职责边界 - -`native/` 只承载状态密集、调用频繁的 DSP 与扬声器渲染核。EMDF/JOC/OAMD 高层解析、错误报告、ADM 组装和 CLI 保留在 Python 中。 - -Python 通过标准库 `ctypes` 调用 C ABI;原生核不使用 pybind11、Cython、FFTW、MKL 或 OpenMP。它是可选加速路径,不扩大项目所支持的码流范围。 - -主要文件: - -```text -native/include/eac3joc_core.h C ABI -native/src/eac3joc_core.cpp JOC/QMF 对象重建 -native/src/speaker_renderer.cpp 对象到扬声器渲染 -native/src/qmf_tables.h QMF 表 -native/src/speaker_layouts.h 布局表 -native/src/joc_huffman_tables.h JOC Huffman 表 -src/native_renderer.py JOC ctypes 桥 -src/speaker_native_renderer.py 扬声器 ctypes 桥 -``` - -## 2. JOC 渲染 ABI - -一个 opaque renderer 保存所有跨帧状态。主要调用为: - -```c -int ejoc_renderer_process( - ejoc_renderer_handle handle, - const float* bed5_planar, /* [5][1536] */ - const float* lfe, /* [1536] or NULL */ - uint32_t object_mask, - const uint8_t* n_bands, /* [15] */ - const uint8_t* n_dpoints, /* [15] */ - const uint8_t* slope_idx, /* [15] */ - const uint8_t* offset_ts, /* [15][2] */ - const double* dq, /* [15][2][5][23] */ - double clipgain, - float phase_new, - float output_scale, - float* output16_planar); /* [16][1536] */ -``` - -JOC 的 Huffman 解码、差分还原和去量化先在 Python 中完成,dense 与 sparse 两条语法共用同一条入口。原生核心消费已去量化的 `dq`(double),两条语法在该 ABI 上布局一致。 - -线程接口为: - -```c -int ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t total_threads); -uint32_t ejoc_renderer_thread_count(ejoc_renderer_handle handle); -``` - -`total_threads` 包含调用线程。单个 renderer 实例必须顺序提交帧;实例内部可以按对象和 analysis channel 并行。 - -## 3. 跨帧状态 - -每个 JOC renderer 独立保存: - -- analysis FIFO:`double[5][9][64]`; -- L/R/C analysis delay:`float[3][10][64]`; -- Ls/Rs QMF delay:`complex[2][10][64]`; -- Ls/Rs band-0 FIR history:`complex[2][20]`; -- 矩阵插值 previous:`double[15][5][64]`; -- inverse-QMF state:`double[15][640]`; -- LFE delay:`double[1217]`。 - -这些状态属于 renderer 实例,不能在无 checkpoint 的情况下任意分段或乱序处理。 - -## 4. FFT、QMF 与精度 - -原生核包含固定 64 点 radix-2 complex FFT: - -- analysis QMF 使用 forward FFT 后除以 64; -- inverse QMF 使用固定重排、旋转和 640 项有效窗状态; -- 不调用外部 FFT 库。 - -JOC 路径的数值类型为: - -- 核心 PCM 输入:float32; -- 矩阵、复 QMF、FFT、FIR 和跨帧状态:double; -- phase 与最终 gain:float32; -- 16 声道对象输出:float32。 - -## 5. 扬声器渲染 ABI - -同一个共享库还导出对象到扬声器布局的渲染接口: - -```c -uint32_t ejoc_speaker_layout_channel_count(uint32_t speaker_bitfield); - -ejoc_speaker_renderer_handle -ejoc_speaker_renderer_create(uint32_t speaker_bitfield); - -int ejoc_speaker_renderer_process( - ejoc_speaker_renderer_handle handle, - const float* objects16_interleaved, - uint32_t sample_count, - uint32_t metadata_count, - const uint32_t* metadata_offsets, - const uint32_t* ramp_durations, - const uint16_t* positions_q15, - const uint8_t* region_indices, - const uint8_t* height_enabled, - const double* object_gains, - double* output_interleaved); -``` - -输入声道 0 为 LFE,1–15 为对象。每个 metadata entry 是一份对象状态快照。`sample_count` 必须是 32 的倍数;未完成的增益斜坡保存在 handle 中并跨调用继续。 - -扬声器路径使用 float32 对象输入、double 坐标/增益/累加与 interleaved double 输出;写 WAV 时才量化为 float32 或 PCM24。 - -支持的布局为: - -```text -2.0 3.1 5.1 7.1 5.1.2 5.1.4 7.1.2 7.1.4 9.1.4 9.1.6 -``` - -## 6. 双耳渲染 ABI - -共享库提供 512-sample float64 双耳 DSP: - -```c -ejoc_binaural_renderer_handle ejoc_binaural_renderer_create(void); -int ejoc_binaural_renderer_configure_kernels(...); -int ejoc_binaural_renderer_configure_room(...); -int ejoc_binaural_renderer_process( - ejoc_binaural_renderer_handle handle, - const double* input16_interleaved, /* [512][16] */ - const double* gains_complex, /* [16][2][77][2] */ - const double* room_sends, /* [16] */ - double output_gain, - double* output_stereo_interleaved); /* [512][2] */ -``` - -Python 负责模型解析、OAMD 时间轴和每 512 samples 的 complex gains/room sends。C++ handle 保存 QMF、hybrid、递归 room 和 QMF synthesis 状态。全部输入、状态、乘加和输出均为 double/complex double。 - -## 6.1 公开 SOFA 双耳渲染 ABI - -共享库同时提供完整的原生 SOFA 双耳渲染器(`ejoc_sofa_binaural_*`),它镜像 -Python `SofaBinauralBackend` 的全部数学:64-QMF/77-hybrid analysis/synthesis、 -五阶 ACN/N3D 实球谐方向场求值、whole-QMF-slot 逐对象 delay 历史、六面一阶 -image-source early reflections、共享 unitary FDN late room、LFE 120–180 Hz -低通与 961-sample latency 语义。kernel 表、编译好的 HRTF 场与房间常数通过 -`configure_kernels/configure_field/configure_room` 一次上传;每 512-sample -block 先 `set_source` 更新 16 个 source,再 `process` 输入 PCM;`process` 返回 -裁剪后的 stereo 样本数(首个 961 samples 被丢弃)。`finish` 以 64-sample 对齐的 -块排空尾音。Python 桥位于 `src/sofa_native_backend.py`,与 Python 参考实现逐值 -一致(差异 < 1e-9);原生库缺失时 `main.py` 自动回退 Python。 - -## 7. 构建 - -CMake 定义位于 `native/CMakeLists.txt`。从仓库根目录运行: - -```powershell -cmake -S native -B build/cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$PWD/lib" -cmake --build build/cmake --config Release -cmake --install build/cmake --config Release -``` - -平台运行库文件名: - -```text -Windows lib/eac3joc_core.dll -Linux lib/libeac3joc_core.so -macOS lib/libeac3joc_core.dylib -``` - -MSVC 配置使用静态 CRT。其他运行时依赖由平台和工具链决定,发布预构建库前应对产物独立检查。 - -仓库默认不附带原生二进制。预构建的 Release 运行库或自行构建的运行库均可直接放入 `lib/`。 - -## 8. 运行时查找与回退 - -查找顺序为: - -1. 显式 `--native-library`; -2. `EAC3JOC_NATIVE_LIBRARY`; -3. `lib/` 下当前平台的标准文件名。 - -`--backend auto` 在加载失败时回退到 NumPy;`--backend python` 跳过原生探测。`--backend native` 当前也会打印失败原因后回退,这是现有 CLI 行为,不应理解为原生库已成功使用。 - -## 9. 实现边界 - -- 原生层只接收 Python 已解析的 dense JOC 数据。 -- ABI 固定了 1536-sample JOC 帧、最多 15 个对象、最多 23 个参数带和最多 2 个数据点。 -- 共享库与 Python 桥需要 ABI version 一致。 -- 跨平台只约定 ABI 与数据类型,不保证 float64 结果逐位一致。 -- `native/src/` 中的私有表头只服务于原生侧;当前仓库不包含重新生成这些头文件的脚本。 - -相关公式见[数学说明](math.md)。 diff --git a/main.py b/main.py index f207043..02c920d 100644 --- a/main.py +++ b/main.py @@ -23,10 +23,12 @@ import numpy as np import adm_assemble import adm_atmos from adm_validate import validate +from bed_layout import (BED_EXTRA_SLOTS_BY_CONFIG, bed_order, extra_pair, + joc_input_indices) +from emdf import expand_chanmap from metadata import DirectPayloadIndex, PayloadIndex, write_summary import oamd_tracks from renderer import JocRenderer -from native_renderer import NativeBackendUnavailable, NativeJocRenderer from binaural_renderer import ( DEFAULT_SOFA_HRTF, SofaBinauralRenderer, @@ -243,15 +245,22 @@ def extract_eac3(ffmpeg, source, target): return target -def decode_core(ffmpeg, eac3, target, duration_sec=None, *, options=()): - # 5.1(side) 的 f32le 顺序为 FL FR FC LFE SL SR;JOC 使用其中 0,1,2,4,5。 +def decode_core(ffmpeg, eac3, target, duration_sec=None, *, options=(), channels=6): + """解出床 PCM。 + + ``channels=6``(N=5 档位)沿用 -ac 6;N=7 档位必须传 ``channels=None``, + 让解码器保持自身的声道布局——``-ac 6`` 会把 5.1.2/7.1 下混成 5.1 并丢掉 + 额外那一对,``-ac 8`` 又会把 5.1.2 重映射成 7.1,两种都会丢信息。 + """ command = [ffmpeg, "-hide_banner", "-loglevel", "error", "-y", *options, "-i", str(eac3), "-map", "0:a:0", "-vn"] if duration_sec is not None: command.extend(["-t", f"{duration_sec:.9f}"]) - command.extend(["-ac", "6", "-ar", str(RATE), + if channels is not None: + command.extend(["-ac", str(int(channels))]) + command.extend(["-ar", str(RATE), "-c:a", "pcm_f32le", "-f", "f32le", str(target)]) - run(command, "FFmpeg 解码核心 5.1 PCM") + run(command, f"FFmpeg 解码核心 {channels or '码流默认'} 通道 PCM") return target @@ -335,45 +344,82 @@ def variant_call(output, source, function, *args, **kwargs): raise -def create_renderer(backend, gain, native_library=None, native_threads=None): - """选择整帧 DSP 后端;auto 优先使用 lib 中当前平台的原生构建。""" - if backend in ("auto", "native"): - try: - decoder = NativeJocRenderer( - output_scale=gain, library_path=native_library, threads=native_threads) - info = { - "name": "native", - "library": str(decoder.library_path), - "build": decoder.build_info, - "threads": decoder.threads, - } - print(f"[backend] native: {info['build']} threads={info['threads']} " - f"({info['library']})", flush=True) - return decoder, info - except (NativeBackendUnavailable, OSError) as exc: - print(f"[backend] native unavailable, falling back to Python: {exc}", flush=True) +def create_renderer(gain): + """整帧 DSP 后端:Python/numpy。""" decoder = JocRenderer(output_scale=gain) info = {"name": "python", "library": None, "build": None, "threads": None} print("[backend] python/numpy", flush=True) return decoder, info +def bed_channel_count(index): + """床 PCM 的通道数:N=5 档位沿用 6 通道,N=7 档位保留解码器自身的 8 通道。""" + return 6 if (getattr(index, "n_channels", None) or 5) == 5 else 8 + + +def bed_layout_plan(index): + """按通道码把床 PCM 摆进 JOC 输入序(规格 §3、§10.4)。 + + 返回 {"channels", "joc_indices", "lfe_index"}。N=7 必须同时有核心子流与依赖 + 子流的 chanmap 才能确定额外那一对;缺任何一侧都报变体,不猜一个顺序。 + """ + config_idx = getattr(index, "config_idx", None) + core_channels = getattr(index, "n_channels", None) or 5 + if core_channels == 5: + # 5.1(side) 的 f32le 顺序为 FL FR FC LFE SL SR。 + return {"channels": 6, "joc_indices": [0, 1, 2, 4, 5], "lfe_index": 3, + "extra_pair_slots": list(BED_EXTRA_SLOTS_BY_CONFIG.get(config_idx, ()))} + transport = None + for entry in getattr(index, "transports", None) or (): + if entry["core_locations"] and entry["chanmap"] is not None: + transport = entry + break + if transport is None: + raise UnsupportedVariantError( + "eac3", "core_extension_pair", + "N=7 档位需要核心子流与依赖子流 chanmap 才能确定床通道顺序", + details={"config_idx": getattr(index, "config_idx", None), + "n_channels": core_channels}) + core = list(transport["core_locations"]) + locations = core + (["LFE"] if transport["core_lfe"] else []) + locations += expand_chanmap(transport["chanmap"]) + pair = extra_pair(core, transport["chanmap"]) + order = bed_order(locations) + indices = joc_input_indices(locations, pair) + if order is None or indices is None: + raise UnsupportedVariantError( + "eac3", "bed_channel_mapping_unsupported", + "床的声道位置无法换算成 JOC 输入序", + details={"config_idx": getattr(index, "config_idx", None), + "locations": locations, "extra_pair": list(pair)}) + lfe_index = None + for index, (_position, name) in enumerate(order): + if name == "LFE": + lfe_index = index + break + return {"channels": len(order), "joc_indices": indices, "lfe_index": lfe_index, + "extra_pair_slots": list(BED_EXTRA_SLOTS_BY_CONFIG.get(config_idx, ()))} + + def render(index, bed_path, frame_count, raw_path, gain, progress_every, - backend="auto", native_library=None, native_threads=None, frame_sink=None, + frame_sink=None, speaker_renderer=None, speaker_sink=None, speaker_metadata_offset=1473, binaural_renderer=None, binaural_sink=None, binaural_metadata_offset=1473, raw_scale=1.0): + plan = bed_layout_plan(index) + bed_channels = plan["channels"] values = np.memmap(bed_path, dtype=np.float32, mode="r") - frame_width = FRAME_SAMPLES * 6 + frame_width = FRAME_SAMPLES * bed_channels if values.size % frame_width: - raise ValueError(f"FFmpeg PCM 长度不是 1536×6 的整数倍: {values.size}") - bed = values.reshape(-1, FRAME_SAMPLES, 6) + raise ValueError( + f"FFmpeg PCM 长度不是 1536×{bed_channels} 的整数倍: {values.size}") + bed = values.reshape(-1, FRAME_SAMPLES, bed_channels) if len(bed) < frame_count: raise ValueError(f"PCM 只有 {len(bed)} 帧,元数据需要 {frame_count} 帧") output = (np.memmap(raw_path, dtype=np.float32, mode="w+", shape=(frame_count, FRAME_SAMPLES, 16)) if raw_path is not None else None) - decoder, backend_info = create_renderer(backend, gain, native_library, native_threads) + decoder, backend_info = create_renderer(gain) started = time.perf_counter() dsp_seconds = 0.0 adm_stream_seconds = 0.0 @@ -385,11 +431,17 @@ def render(index, bed_path, frame_count, raw_path, gain, progress_every, elapsed = 0.0 try: for frame_number, row in enumerate(index.rows[:frame_count]): - bed6 = np.asarray(bed[frame_number], dtype=np.float32) + frame = np.asarray(bed[frame_number], dtype=np.float32) + core = frame[:, plan["joc_indices"]].T + lfe = (np.zeros(FRAME_SAMPLES, dtype=np.float32) + if plan["lfe_index"] is None else frame[:, plan["lfe_index"]]) subs = index.subpayloads(row) stage = time.perf_counter() - pcm16, _ = decoder.render_subpayloads( - subs, bed6[:, [0, 1, 2, 4, 5]].T, bed6[:, 3]) + if subs is None: + pcm16, _ = decoder.render_missing_frame( + core, lfe, config_idx=getattr(index, "config_idx", None) or 3) + else: + pcm16, _ = decoder.render_subpayloads(subs, core, lfe) dsp_seconds += time.perf_counter() - stage if output is not None: stage = time.perf_counter() @@ -537,12 +589,7 @@ def build_parser(): parser.add_argument("--eac3-target-level", type=int, default=0, help="E-AC-3 解码器 -target_level:按码流 dialnorm 归一化电平," "增益约 target_level - dialnorm dB;0=不施加,默认 0") - parser.add_argument("--backend", choices=("auto", "native", "python"), default="auto", - help="JOC/扬声器 DSP 后端;SOFA 双耳 DSP 当前使用 Python") - parser.add_argument("--native-library", type=Path, - help="显式指定原生库;默认从单层 lib 目录选择当前平台文件") - parser.add_argument("--native-threads", type=int, - help="原生 DSP 总线程数;默认在 4 核以上使用 2,可用环境变量 EAC3JOC_NATIVE_THREADS 覆盖") + metadata_source = parser.add_mutually_exclusive_group() metadata_source.add_argument("--metadata-dir", type=Path, help="含 frames.csv 和 emdf/ 或 payloads/ 的元数据 sidecar") @@ -680,10 +727,22 @@ def main(argv=None): if args.metadata_only: return 0 + for entry in getattr(index, "diagnostics", None) or (): + print("[diagnostics] " + + json.dumps(entry, ensure_ascii=False, separators=(",", ":")), + file=sys.stderr, flush=True) + core_channels = getattr(index, "n_channels", None) + if core_channels is not None: + print(f"[joc] downmix_config_idx={index.config_idx} " + f"core_channels={core_channels} " + f"bed_channels={bed_channel_count(index)} " + f"extra_pair_slots={bed_layout_plan(index)['extra_pair_slots']}", + flush=True) bed_path = timed_call( timings, "decode_core", decode_core, ffmpeg, eac3, temp_dir / "core51_f32le.raw", duration_sec, - options=decode_options) + options=decode_options, + channels=(6 if core_channels in (None, 5) else None)) raw_path = (output.with_name(output.name + ".objects16.f32le") if args.keep_raw else None) master = None @@ -700,12 +759,7 @@ def main(argv=None): timings["create_binaural_renderer"] = 0.0 layout = get_speaker_layout(args.speaker_layout) speaker_name = speaker_layout_display_name(layout) - speaker_decoder, speaker_backend_info = create_speaker_renderer( - layout, backend=args.backend, native_library=args.native_library) - fallback = speaker_backend_info.get("fallback_reason") - if fallback: - print(f"[speaker] native unavailable, falling back to Python: {fallback}", - flush=True) + speaker_decoder, speaker_backend_info = create_speaker_renderer(layout) print(f"[speaker] layout={speaker_name} backend={speaker_backend_info['name']} " f"channels={layout.channel_count}", flush=True) spool = SpeakerPcmSpool( @@ -715,8 +769,7 @@ def main(argv=None): render_seconds, renderer_backend, render_breakdown = timed_call( timings, "render_and_stream", variant_call, output, source, render, index, bed_path, frame_count, raw_path, gain, - max(1, args.progress_every), args.backend, args.native_library, - args.native_threads, None, speaker_decoder, spool, + max(1, args.progress_every), None, speaker_decoder, spool, args.speaker_metadata_offset) spool.finalize() speaker_actual_format = choose_pcm_output_format( @@ -749,33 +802,14 @@ def main(argv=None): "chunk_frames": args.binaural_chunk_frames, } if hrtf_source["kind"] == "sofa": - binaural_decoder = None - if args.backend in ("auto", "native"): - try: - from sofa_native_backend import create_native_sofa_renderer - binaural_decoder = timed_call( - timings, "create_binaural_renderer", - create_native_sofa_renderer, - hrtf_source["path"], - cache_policy=hrtf_source["cache_policy"], - cache_dir=hrtf_source["cache_dir"], - shell_radius_m=args.hrtf_radius_m, - **common_options) - except (ImportError, OSError, RuntimeError, ValueError) as exc: - print( - f"[binaural] native SOFA backend unavailable " - f"({exc.__class__.__name__}: {exc}); " - f"falling back to Python", flush=True) - binaural_decoder = None - if binaural_decoder is None: - binaural_decoder = timed_call( - timings, "create_binaural_renderer", - SofaBinauralRenderer.from_sofa, - hrtf_source["path"], - cache_policy=hrtf_source["cache_policy"], - cache_dir=hrtf_source["cache_dir"], - shell_radius_m=args.hrtf_radius_m, - **common_options) + binaural_decoder = timed_call( + timings, "create_binaural_renderer", + SofaBinauralRenderer.from_sofa, + hrtf_source["path"], + cache_policy=hrtf_source["cache_policy"], + cache_dir=hrtf_source["cache_dir"], + shell_radius_m=args.hrtf_radius_m, + **common_options) elif hrtf_source["kind"] == "rosella": binaural_decoder = timed_call( timings, "create_binaural_renderer", @@ -785,32 +819,13 @@ def main(argv=None): object_delay_samples=args.object_delay_samples, tail_seconds=args.binaural_tail_seconds, output_gain=gain_float64, - chunk_frames=args.binaural_chunk_frames, - backend=args.backend, - native_library=args.native_library) + chunk_frames=args.binaural_chunk_frames) else: - binaural_decoder = None - if args.backend in ("auto", "native"): - try: - from sofa_native_backend import ( - create_native_compiled_cache_renderer) - binaural_decoder = timed_call( - timings, "create_binaural_renderer", - create_native_compiled_cache_renderer, - hrtf_source["path"], - **common_options) - except (ImportError, OSError, RuntimeError, ValueError) as exc: - print( - f"[binaural] native SOFA backend unavailable " - f"({exc.__class__.__name__}: {exc}); " - f"falling back to Python", flush=True) - binaural_decoder = None - if binaural_decoder is None: - binaural_decoder = timed_call( - timings, "create_binaural_renderer", - SofaBinauralRenderer.from_compiled_cache, - hrtf_source["path"], - **common_options) + binaural_decoder = timed_call( + timings, "create_binaural_renderer", + SofaBinauralRenderer.from_compiled_cache, + hrtf_source["path"], + **common_options) print( f"[binaural] mode={binaural_render_mode} " f"backend={binaural_decoder.dsp_backend} " @@ -835,8 +850,6 @@ def main(argv=None): timings, "render_and_stream", variant_call, output, source, render, index, bed_path, frame_count, raw_path, np.float32(1.0), max(1, args.progress_every), - backend=args.backend, native_library=args.native_library, - native_threads=args.native_threads, binaural_renderer=binaural_decoder, binaural_sink=spool, binaural_metadata_offset=args.object_delay_samples, raw_scale=gain) spool.finalize(minimum_samples=frame_count * FRAME_SAMPLES) @@ -895,8 +908,7 @@ def main(argv=None): render_seconds, renderer_backend, render_breakdown = timed_call( timings, "render_and_stream", variant_call, output, source, render, index, bed_path, frame_count, raw_path, gain, - max(1, args.progress_every), args.backend, args.native_library, - args.native_threads, master) + max(1, args.progress_every), master) tracks = timed_call( timings, "build_adm_tracks", variant_call, output, source, oamd_tracks.build_adm_tracks, @@ -927,6 +939,10 @@ def main(argv=None): "mode": mode_name, "metadata": str(metadata_json) if metadata_json is not None else None, "metadata_backend": metadata_backend, + "joc_downmix_config_idx": getattr(index, "config_idx", None), + "joc_core_channels": getattr(index, "n_channels", None), + "joc_extra_pair_slots": bed_layout_plan(index)["extra_pair_slots"], + "diagnostics": getattr(index, "diagnostics", None), "metadata_cache": str(metadata_cache_dir) if metadata_cache_dir is not None else None, "frames": frame_count, "duration_sec": duration_sec, diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt deleted file mode 100644 index 107d5ae..0000000 --- a/native/CMakeLists.txt +++ /dev/null @@ -1,74 +0,0 @@ -cmake_minimum_required(VERSION 3.20) - -project(eac3joc_core VERSION 1.0.0 LANGUAGES CXX) - -include(GNUInstallDirs) -find_package(Threads REQUIRED) - -add_library(eac3joc_core SHARED - src/eac3joc_core.cpp - src/speaker_renderer.cpp - src/binaural_renderer.cpp - src/sofa_binaural_renderer.cpp - src/joc_huffman_tables.h - src/qmf_tables.h - src/speaker_layouts.h -) - -target_compile_features(eac3joc_core PRIVATE cxx_std_20) -target_include_directories(eac3joc_core PRIVATE - "${CMAKE_CURRENT_SOURCE_DIR}/include" -) -target_link_libraries(eac3joc_core PRIVATE Threads::Threads) - -set_target_properties(eac3joc_core PROPERTIES - OUTPUT_NAME "eac3joc_core" - CXX_VISIBILITY_PRESET hidden - VISIBILITY_INLINES_HIDDEN YES - POSITION_INDEPENDENT_CODE YES -) - -if(MSVC) - set_property(TARGET eac3joc_core PROPERTY - MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") - target_compile_options(eac3joc_core PRIVATE - /W4 /permissive- /Zc:__cplusplus /utf-8 /EHsc /GR- /fp:precise - $<$:/O2> - $<$:/Oi> - $<$:/GL> - ) - target_link_options(eac3joc_core PRIVATE - $<$:/LTCG> - /INCREMENTAL:NO /OPT:REF /OPT:ICF - ) -else() - target_compile_options(eac3joc_core PRIVATE - -Wall -Wextra -Wpedantic -fno-fast-math - $<$:-O3> - ) -endif() - -# Install directly into the chosen prefix. Recommended invocation from the -# repository root: -# cmake -S native -B build/cmake -DCMAKE_BUILD_TYPE=Release \ -# -DCMAKE_INSTALL_PREFIX=/lib -# cmake --build build/cmake --config Release -# cmake --install build/cmake --config Release -# -# Result names are supplied by the platform toolchain: -# Windows: eac3joc_core.dll (+ eac3joc_core.lib import library) -# Linux: libeac3joc_core.so -# macOS: libeac3joc_core.dylib -install(TARGETS eac3joc_core - RUNTIME DESTINATION . - LIBRARY DESTINATION . - ARCHIVE DESTINATION . -) - -if(MSVC) - install(FILES "$" - DESTINATION . - OPTIONAL - ) -endif() - diff --git a/native/include/eac3joc_core.h b/native/include/eac3joc_core.h deleted file mode 100644 index a227d15..0000000 --- a/native/include/eac3joc_core.h +++ /dev/null @@ -1,244 +0,0 @@ -#pragma once - -#include - -#if defined(_WIN32) - #if defined(EJOC_BUILD_DLL) - #define EJOC_API __declspec(dllexport) - #else - #define EJOC_API __declspec(dllimport) - #endif - #define EJOC_CALL __cdecl -#else - #define EJOC_API __attribute__((visibility("default"))) - #define EJOC_CALL -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -enum { - EJOC_ABI_VERSION = 1, - EJOC_FRAME_SAMPLES = 1536, - EJOC_TIMESLOTS = 24, - EJOC_SUBBANDS = 64, - EJOC_CORE_CHANNELS = 5, - EJOC_OUTPUT_CHANNELS = 16, - EJOC_MAX_OBJECTS = 15, - EJOC_MAX_DPOINTS = 2, - EJOC_MAX_PARAMETER_BANDS = 23, - EJOC_SPEAKER_BLOCK_SAMPLES = 32, - EJOC_SPEAKER_COORDINATES = 3, - EJOC_BINAURAL_BLOCK_SAMPLES = 512, - EJOC_BINAURAL_INPUT_CHANNELS = 16, - EJOC_BINAURAL_OUTPUT_CHANNELS = 2, - EJOC_BINAURAL_QMF_BANDS = 64, - EJOC_BINAURAL_HYBRID_BANDS = 77 -}; - -typedef void* ejoc_renderer_handle; -typedef void* ejoc_speaker_renderer_handle; -typedef void* ejoc_binaural_renderer_handle; - -/* -Fixed array layouts used by ejoc_renderer_process(): - bed5_planar [5][1536] - lfe [1536] or NULL - n_bands [15] - n_dpoints [15] - slope_idx [15] - offset_ts [15][2] - dq [15][2][5][23] - output16 [16][1536] - -Only objects selected by object_mask are read from the descriptor arrays. -dq carries already dequantized matrix coefficients in double precision; the -caller performs the JOC bitstream differential decoding for both dense and -sparse objects, so this ABI is identical for both syntaxes. -*/ - -EJOC_API uint32_t EJOC_CALL ejoc_abi_version(void); -EJOC_API const char* EJOC_CALL ejoc_build_info(void); -EJOC_API ejoc_renderer_handle EJOC_CALL ejoc_renderer_create(void); -EJOC_API void EJOC_CALL ejoc_renderer_destroy(ejoc_renderer_handle handle); -EJOC_API int EJOC_CALL ejoc_renderer_reset(ejoc_renderer_handle handle); -EJOC_API int EJOC_CALL ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t total_threads); -/* -Enables or disables the Ls/Rs band-0 21-tap DC compensation. The caller derives -it from the JOC downmix configuration: only configurations 3 and 4 enable the -filter. When disabled, band 0 keeps the common per-band processing (surround -delay plus -j rotation) instead of being overwritten by the FIR. The delay line -and DC history advance either way, so the flag may change between frames. -Defaults to enabled when never called. -*/ -EJOC_API int EJOC_CALL ejoc_renderer_set_dc_filter(ejoc_renderer_handle handle, uint32_t enabled); -EJOC_API uint32_t EJOC_CALL ejoc_renderer_thread_count(ejoc_renderer_handle handle); -EJOC_API const char* EJOC_CALL ejoc_renderer_last_error(ejoc_renderer_handle handle); - -EJOC_API int EJOC_CALL ejoc_renderer_process( - ejoc_renderer_handle handle, - const float* bed5_planar, - const float* lfe, - uint32_t object_mask, - const uint8_t* n_bands, - const uint8_t* n_dpoints, - const uint8_t* slope_idx, - const uint8_t* offset_ts, - const double* dq, - double clipgain, - float phase_new, - float output_scale, - float* output16_planar); - -/* -High-precision object-to-speaker renderer. - -The renderer consumes interleaved float32 input PCM arranged as: - objects16_interleaved [sample_count][16] -where channel 0 is LFE and channels 1..15 are point objects. All spatial -calculations, gain ramps, and accumulation use double. Output is interleaved: - output_interleaved [sample_count][layout_channel_count] - -Each metadata entry is a complete object-state snapshot: - metadata_offsets [metadata_count], relative to this process call - ramp_durations [metadata_count], in samples - positions_q15 [metadata_count][15][3] - region_indices [metadata_count][15] or NULL (all region 0) - height_enabled [metadata_count][15] or NULL (all enabled) - object_gains [metadata_count][15] or NULL (all 1.0) - -metadata_offsets must be nondecreasing and <= sample_count. sample_count must -be a multiple of EJOC_SPEAKER_BLOCK_SAMPLES. State and unfinished ramps are -preserved across calls. -*/ -EJOC_API uint32_t EJOC_CALL ejoc_speaker_layout_channel_count(uint32_t speaker_bitfield); -EJOC_API ejoc_speaker_renderer_handle EJOC_CALL ejoc_speaker_renderer_create(uint32_t speaker_bitfield); -EJOC_API void EJOC_CALL ejoc_speaker_renderer_destroy(ejoc_speaker_renderer_handle handle); -EJOC_API int EJOC_CALL ejoc_speaker_renderer_reset(ejoc_speaker_renderer_handle handle); -EJOC_API const char* EJOC_CALL ejoc_speaker_renderer_last_error(ejoc_speaker_renderer_handle handle); -EJOC_API int EJOC_CALL ejoc_speaker_renderer_process( - ejoc_speaker_renderer_handle handle, - const float* objects16_interleaved, - uint32_t sample_count, - uint32_t metadata_count, - const uint32_t* metadata_offsets, - const uint32_t* ramp_durations, - const uint16_t* positions_q15, - const uint8_t* region_indices, - const uint8_t* height_enabled, - const double* object_gains, - double* output_interleaved); - -EJOC_API ejoc_binaural_renderer_handle EJOC_CALL ejoc_binaural_renderer_create(void); -EJOC_API void EJOC_CALL ejoc_binaural_renderer_destroy(ejoc_binaural_renderer_handle handle); -EJOC_API int EJOC_CALL ejoc_binaural_renderer_reset(ejoc_binaural_renderer_handle handle); -EJOC_API const char* EJOC_CALL ejoc_binaural_renderer_last_error( - ejoc_binaural_renderer_handle handle); -EJOC_API int EJOC_CALL ejoc_binaural_renderer_configure_kernels( - ejoc_binaural_renderer_handle handle, - const double* qmf_analysis, - const double* hybrid_low, - const int16_t* hybrid_indices, - const double* hybrid_values, - uint32_t hybrid_count, - const double* qmf_basis, - const double* qmf_taps); -EJOC_API int EJOC_CALL ejoc_binaural_renderer_configure_room( - ejoc_binaural_renderer_handle handle, - uint32_t bands, - uint32_t allpass_count, - const uint32_t* allpass_delays, - const double* allpass_gains, - const uint32_t* fdn_delays, - const double* fdn_matrix, - uint32_t output_tap_delay, - const double* feedback_complex, - const double* output_taps, - const double* output_complex, - uint32_t extra_count, - const uint32_t* extra_delays, - const double* extra_fields_complex, - const double* extra_matrices); -EJOC_API int EJOC_CALL ejoc_binaural_renderer_process( - ejoc_binaural_renderer_handle handle, - const double* input16_interleaved, - const double* gains_complex, - const double* room_sends, - double output_gain, - double* output_stereo_interleaved); - -/* -Native SOFA binaural renderer. - -The handle owns the complete runtime: 64-QMF/77-hybrid analysis and synthesis, -fifth-order ACN/N3D real spherical-harmonic direction-field evaluation, -per-object whole-QMF-slot delay histories, six first-order image-source early -reflections, the shared unitary-FDN late room, the 120-180 Hz LFE low-pass and -the 961-sample latency compensation. The caller configures the filterbank -tables, the compiled HRTF field and the room constants once, then per 512-sample -block updates every source with ejoc_sofa_binaural_set_source() and calls -ejoc_sofa_binaural_process(). Process returns the number of trimmed stereo -samples written; the first 961 processed samples across calls are discarded. -*/ -typedef void* ejoc_sofa_binaural_handle; - -EJOC_API ejoc_sofa_binaural_handle EJOC_CALL ejoc_sofa_binaural_create(void); -EJOC_API void EJOC_CALL ejoc_sofa_binaural_destroy(ejoc_sofa_binaural_handle handle); -EJOC_API int EJOC_CALL ejoc_sofa_binaural_reset(ejoc_sofa_binaural_handle handle); -EJOC_API const char* EJOC_CALL ejoc_sofa_binaural_last_error( - ejoc_sofa_binaural_handle handle); -EJOC_API int EJOC_CALL ejoc_sofa_binaural_configure_kernels( - ejoc_sofa_binaural_handle handle, - const double* qmf_analysis, - const double* hybrid_low, - const int16_t* hybrid_indices, - const double* hybrid_values, - uint32_t hybrid_count, - const double* qmf_basis, - const double* qmf_taps); -EJOC_API int EJOC_CALL ejoc_sofa_binaural_configure_field( - ejoc_sofa_binaural_handle handle, - const double* coefficients, - const double* delay_coefficients, - const double* delay_bounds, - const double* band_centers, - double measurement_radius_m); -EJOC_API int EJOC_CALL ejoc_sofa_binaural_configure_room( - ejoc_sofa_binaural_handle handle, - const double* room_dims, - const double* listener_pos, - const double* wall_gains, - double speed_of_sound, - const uint32_t* fdn_delays, - const double* fdn_feedback, - double damping, - double fdn_output_gain, - const uint32_t* allpass_delays, - const double* allpass_gains, - uint32_t enable_early_reflections, - uint32_t enable_late_room); -EJOC_API int EJOC_CALL ejoc_sofa_binaural_set_source( - ejoc_sofa_binaural_handle handle, - uint32_t source, - const double* position_adm, - uint32_t profile, - double gain, - uint32_t enabled, - uint32_t special_lfe, - uint32_t fade); -EJOC_API int EJOC_CALL ejoc_sofa_binaural_process( - ejoc_sofa_binaural_handle handle, - const double* input16_interleaved, - uint32_t sample_count, - double output_gain, - double* output_stereo_interleaved); -EJOC_API int EJOC_CALL ejoc_sofa_binaural_finish( - ejoc_sofa_binaural_handle handle, - uint32_t flush_samples, - double* output_stereo_interleaved, - uint32_t capacity); - -#ifdef __cplusplus -} -#endif diff --git a/native/src/binaural_renderer.cpp b/native/src/binaural_renderer.cpp deleted file mode 100644 index 1456d2a..0000000 --- a/native/src/binaural_renderer.cpp +++ /dev/null @@ -1,664 +0,0 @@ -#define EJOC_BUILD_DLL -#include "eac3joc_core.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -namespace ejoc::binaural { - -struct Complex { - double re; - double im; -}; - -inline Complex add(Complex a, Complex b) noexcept { - return {a.re + b.re, a.im + b.im}; -} - -inline Complex mul(Complex a, Complex b) noexcept { - return {a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re}; -} - -inline Complex scale(Complex value, double gain) noexcept { - return {value.re * gain, value.im * gain}; -} - -constexpr double kPi = 3.141592653589793238462643383279502884; -constexpr int kChannels = EJOC_BINAURAL_INPUT_CHANNELS; -constexpr int kEars = EJOC_BINAURAL_OUTPUT_CHANNELS; -constexpr int kBlock = EJOC_BINAURAL_BLOCK_SAMPLES; -constexpr int kSlots = kBlock / 64; -constexpr int kQmf = EJOC_BINAURAL_QMF_BANDS; -constexpr int kHybrid = EJOC_BINAURAL_HYBRID_BANDS; -constexpr int kRank = 4; - -class Renderer final { -public: - Renderer() noexcept { - initialize_fft(); - reset(); - } - - int configure_kernels( - const double* qmf_analysis, - const double* hybrid_low, - const int16_t* hybrid_indices, - const double* hybrid_values, - uint32_t hybrid_count, - const double* qmf_basis, - const double* qmf_taps) noexcept { - if (!qmf_analysis || !hybrid_low || !hybrid_indices || !hybrid_values || - !qmf_basis || !qmf_taps || hybrid_count == 0) { - return fail("invalid binaural kernel configuration"); - } - std::memcpy(qmf_analysis_.data(), qmf_analysis, - qmf_analysis_.size() * sizeof(double)); - hybrid_low_.assign(hybrid_low, hybrid_low + 3 * 2 * 13 * 16 * 2); - hybrid_indices_.assign(hybrid_indices, hybrid_indices + hybrid_count * 4); - hybrid_values_.assign(hybrid_values, hybrid_values + hybrid_count); - std::memcpy(qmf_basis_.data(), qmf_basis, - qmf_basis_.size() * sizeof(double)); - std::memcpy(qmf_taps_.data(), qmf_taps, - qmf_taps_.size() * sizeof(double)); - kernels_ready_ = true; - reset(); - return 0; - } - - int configure_room( - uint32_t bands, - uint32_t allpass_count, - const uint32_t* allpass_delays, - const double* allpass_gains, - const uint32_t* fdn_delays, - const double* fdn_matrix, - uint32_t output_tap_delay, - const double* feedback_complex, - const double* output_taps, - const double* output_complex, - uint32_t extra_count, - const uint32_t* extra_delays, - const double* extra_fields_complex, - const double* extra_matrices) noexcept { - if (bands != 64 || !fdn_delays || !fdn_matrix || !feedback_complex || - !output_taps || !output_complex || - (allpass_count && (!allpass_delays || !allpass_gains)) || - (extra_count && (!extra_delays || !extra_fields_complex || !extra_matrices))) { - return fail("invalid binaural room configuration"); - } - room_bands_ = bands; - if (allpass_count) { - allpass_delays_.assign(allpass_delays, allpass_delays + allpass_count); - allpass_gains_.assign(allpass_gains, allpass_gains + allpass_count); - } else { - allpass_delays_.clear(); - allpass_gains_.clear(); - } - allpass_offsets_.resize(allpass_count); - allpass_positions_.assign(allpass_count, 0); - size_t allpass_size = 0; - for (uint32_t index = 0; index < allpass_count; ++index) { - if (allpass_delays_[index] == 0) { - return fail("binaural allpass delay must be positive"); - } - allpass_offsets_[index] = allpass_size; - allpass_size += static_cast(allpass_delays_[index]) * bands; - } - allpass_memory_.assign(allpass_size, {}); - - room_capacity_ = 0; - for (int branch = 0; branch < 4; ++branch) { - fdn_delays_[branch] = fdn_delays[branch]; - room_capacity_ = std::max(room_capacity_, fdn_delays_[branch]); - } - if (room_capacity_ == 0) { - return fail("binaural room delay must be positive"); - } - std::copy(fdn_matrix, fdn_matrix + 16, fdn_matrix_.begin()); - output_tap_delay_ = output_tap_delay; - for (int band = 0; band < 64; ++band) { - for (int branch = 0; branch < 4; ++branch) { - const size_t complex_index = (static_cast(band) * 4 + branch) * 2; - feedback_[band][branch] = { - feedback_complex[complex_index], feedback_complex[complex_index + 1]}; - output_taps_[band][branch] = output_taps[band * 4 + branch]; - for (int ear = 0; ear < 2; ++ear) { - const size_t output_index = - ((static_cast(ear) * 64 + band) * 4 + branch) * 2; - output_matrix_[ear][band][branch] = { - output_complex[output_index], output_complex[output_index + 1]}; - } - } - } - room_memory_.assign(static_cast(room_capacity_) * 64 * 4, {}); - if (extra_count) { - extra_delays_.assign(extra_delays, extra_delays + extra_count); - } else { - extra_delays_.clear(); - } - extra_fields_.resize(static_cast(extra_count) * 64); - extra_matrices_.resize(static_cast(extra_count) * 16); - for (uint32_t extra = 0; extra < extra_count; ++extra) { - for (int band = 0; band < 64; ++band) { - const size_t source = (static_cast(extra) * 64 + band) * 2; - extra_fields_[static_cast(extra) * 64 + band] = { - extra_fields_complex[source], extra_fields_complex[source + 1]}; - } - std::copy(extra_matrices + static_cast(extra) * 16, - extra_matrices + static_cast(extra + 1) * 16, - extra_matrices_.begin() + static_cast(extra) * 16); - } - room_ready_ = true; - reset(); - return 0; - } - - int reset() noexcept { - qmf_history_.fill(0.0); - hybrid_low_history_.fill({}); - hybrid_high_history_.fill({}); - synthesis_history_.fill(0.0); - std::fill(allpass_memory_.begin(), allpass_memory_.end(), Complex{}); - std::fill(allpass_positions_.begin(), allpass_positions_.end(), 0u); - std::fill(room_memory_.begin(), room_memory_.end(), Complex{}); - room_position_ = 0; - error_[0] = '\0'; - return 0; - } - - const char* error() const noexcept { - return error_[0] ? error_ : ""; - } - - int process( - const double* input, - const double* gains, - const double* room_sends, - double output_gain, - double* output) noexcept { - if (!kernels_ready_ || !room_ready_) { - return fail("binaural renderer is not configured"); - } - if (!input || !gains || !room_sends || !output || !std::isfinite(output_gain)) { - return fail("invalid binaural process arguments"); - } - for (int slot = 0; slot < kSlots; ++slot) { - std::array qmf{}; - std::array hybrid{}; - analyze_qmf(input + static_cast(slot) * 64 * kChannels, qmf); - analyze_hybrid(qmf, hybrid); - - std::array rendered{}; - std::array room_input{}; - for (int source = kChannels - 1; source >= 0; --source) { - for (int band = 0; band < kHybrid; ++band) { - const Complex value = hybrid[source * kHybrid + band]; - room_input[band] = add(room_input[band], scale(value, room_sends[source])); - for (int ear = 0; ear < kEars; ++ear) { - const size_t gain_index = - (((static_cast(source) * kEars + ear) * kHybrid + band) * 2); - const Complex gain{gains[gain_index], gains[gain_index + 1]}; - rendered[ear * kHybrid + band] = add( - rendered[ear * kHybrid + band], mul(value, gain)); - } - } - } - const auto room = process_room(room_input); - for (size_t index = 0; index < rendered.size(); ++index) { - rendered[index] = add(rendered[index], room[index]); - } - - std::array qmf_output{}; - synthesize_hybrid(rendered, qmf_output); - for (int ear = 0; ear < kEars; ++ear) { - std::array samples{}; - synthesize_qmf(qmf_output.data() + ear * kQmf, ear, samples); - for (int sample = 0; sample < 64; ++sample) { - output[(static_cast(slot) * 64 + sample) * 2 + ear] = - samples[sample] * output_gain; - } - } - } - return 0; - } - -private: - int fail(const char* message) noexcept { - std::snprintf(error_, sizeof(error_), "%s", message); - return -1; - } - - void initialize_fft() noexcept { - for (int index = 0; index < 128; ++index) { - int value = index; - int reversed = 0; - for (int bit = 0; bit < 7; ++bit) { - reversed = (reversed << 1) | (value & 1); - value >>= 1; - } - bit_reverse_[index] = static_cast(reversed); - } - for (int phase = 0; phase < 64; ++phase) { - const double angle = -kPi * static_cast(phase) / 128.0; - premod_[phase] = {std::cos(angle), std::sin(angle)}; - const double post_angle = - -3.0 * (static_cast(phase) + 0.5) * kPi / 128.0; - post_[phase] = {std::cos(post_angle), std::sin(post_angle)}; - even_post_[phase] = {0.0, (phase & 1) ? -1.0 : 1.0}; - } - } - - void fft128(std::array& values) const noexcept { - for (int index = 0; index < 128; ++index) { - const int reversed = bit_reverse_[index]; - if (reversed > index) { - std::swap(values[index], values[reversed]); - } - } - for (int length = 2; length <= 128; length <<= 1) { - const double angle = -2.0 * kPi / static_cast(length); - const Complex step{std::cos(angle), std::sin(angle)}; - for (int start = 0; start < 128; start += length) { - Complex rotation{1.0, 0.0}; - for (int offset = 0; offset < length / 2; ++offset) { - const Complex even = values[start + offset]; - const Complex odd = mul(values[start + offset + length / 2], rotation); - values[start + offset] = {even.re + odd.re, even.im + odd.im}; - values[start + offset + length / 2] = { - even.re - odd.re, even.im - odd.im}; - rotation = mul(rotation, step); - } - } - } - } - - void qmf_transform(const std::array& source, - std::array& target) const noexcept { - std::array work{}; - for (int phase = 0; phase < 64; ++phase) { - work[phase] = scale(premod_[phase], source[phase]); - } - fft128(work); - for (int band = 0; band < 64; ++band) { - target[band] = mul(work[band], post_[band]); - } - } - - void analyze_qmf(const double* input, - std::array& output) noexcept { - for (int channel = 0; channel < kChannels; ++channel) { - for (int lag = 9; lag > 0; --lag) { - for (int phase = 0; phase < 64; ++phase) { - qmf_history_[qmf_history_index(lag, channel, phase)] = - qmf_history_[qmf_history_index(lag - 1, channel, phase)]; - } - } - for (int phase = 0; phase < 64; ++phase) { - qmf_history_[qmf_history_index(0, channel, phase)] = - input[phase * kChannels + channel]; - } - std::array even{}; - std::array odd{}; - for (int phase = 0; phase < 64; ++phase) { - for (int lag = 0; lag < 10; ++lag) { - const double value = - qmf_history_[qmf_history_index(lag, channel, phase)] * - qmf_analysis_[phase * 10 + lag]; - (lag & 1 ? odd[phase] : even[phase]) += value; - } - } - std::array even_fft{}; - std::array odd_fft{}; - qmf_transform(even, even_fft); - qmf_transform(odd, odd_fft); - for (int band = 0; band < 64; ++band) { - output[channel * 64 + band] = add( - odd_fft[band], mul(even_fft[band], even_post_[band])); - } - } - } - - void analyze_hybrid( - const std::array& qmf, - std::array& output) noexcept { - for (int channel = 0; channel < kChannels; ++channel) { - for (int lag = 12; lag > 0; --lag) { - for (int band = 0; band < 3; ++band) { - hybrid_low_history_[hybrid_low_history_index(lag, channel, band)] = - hybrid_low_history_[hybrid_low_history_index(lag - 1, channel, band)]; - } - } - for (int band = 0; band < 3; ++band) { - hybrid_low_history_[hybrid_low_history_index(0, channel, band)] = - qmf[channel * 64 + band]; - } - for (int output_band = 0; output_band < 16; ++output_band) { - Complex value{}; - for (int lag = 0; lag < 13; ++lag) { - for (int input_band = 0; input_band < 3; ++input_band) { - const Complex source = hybrid_low_history_[ - hybrid_low_history_index(lag, channel, input_band)]; - const double components[2]{source.re, source.im}; - for (int input_component = 0; input_component < 2; ++input_component) { - value.re += components[input_component] * hybrid_low_[ - hybrid_low_kernel_index(input_band, input_component, lag, - output_band, 0)]; - value.im += components[input_component] * hybrid_low_[ - hybrid_low_kernel_index(input_band, input_component, lag, - output_band, 1)]; - } - } - } - output[channel * kHybrid + output_band] = value; - } - for (int band = 0; band < 61; ++band) { - output[channel * kHybrid + 16 + band] = - hybrid_high_history_[hybrid_high_history_index(0, channel, band)]; - for (int delay = 0; delay < 5; ++delay) { - hybrid_high_history_[hybrid_high_history_index(delay, channel, band)] = - hybrid_high_history_[hybrid_high_history_index(delay + 1, channel, band)]; - } - hybrid_high_history_[hybrid_high_history_index(5, channel, band)] = - qmf[channel * 64 + 3 + band]; - } - } - } - - std::array process_room( - const std::array& input) noexcept { - std::array filtered{}; - for (int band = 0; band < 64; ++band) { - filtered[band] = scale(input[band], 0.70710677); - } - for (size_t stage = 0; stage < allpass_delays_.size(); ++stage) { - const uint32_t position = allpass_positions_[stage]; - const double gain = allpass_gains_[stage]; - for (int band = 0; band < 64; ++band) { - Complex& memory = allpass_memory_[ - allpass_offsets_[stage] + static_cast(position) * 64 + band]; - const Complex residual = add(filtered[band], scale(memory, -gain)); - filtered[band] = add(scale(residual, gain), memory); - memory = residual; - } - allpass_positions_[stage] = (position + 1) % allpass_delays_[stage]; - } - - std::array branches{}; - std::array taps{}; - for (int band = 0; band < 64; ++band) { - for (int branch = 0; branch < 4; ++branch) { - Complex value = filtered[band]; - for (int source = 0; source < 4; ++source) { - const uint32_t position = - (room_position_ + room_capacity_ - fdn_delays_[source]) % room_capacity_; - value = add(value, scale(room_memory_[ - room_memory_index(position, band, source)], - fdn_matrix_[branch * 4 + source])); - } - branches[band * 4 + branch] = value; - const uint32_t tap_position = - (room_position_ + room_capacity_ - - (output_tap_delay_ % room_capacity_)) % room_capacity_; - taps[band * 4 + branch] = - room_memory_[room_memory_index(tap_position, band, branch)]; - } - } - for (int band = 0; band < 64; ++band) { - for (int branch = 0; branch < 4; ++branch) { - room_memory_[room_memory_index(room_position_, band, branch)] = - mul(branches[band * 4 + branch], feedback_[band][branch]); - } - } - room_position_ = (room_position_ + 1) % room_capacity_; - - std::array extra{}; - for (size_t index = 0; index < extra_delays_.size(); ++index) { - const uint32_t position = - (room_position_ + room_capacity_ - - ((extra_delays_[index] + 1) % room_capacity_)) % room_capacity_; - for (int band = 0; band < 64; ++band) { - for (int target = 0; target < 4; ++target) { - Complex mixed{}; - for (int source = 0; source < 4; ++source) { - mixed = add(mixed, scale(room_memory_[ - room_memory_index(position, band, source)], - extra_matrices_[index * 16 + target * 4 + source])); - } - extra[band * 4 + target] = add( - extra[band * 4 + target], - mul(mixed, extra_fields_[index * 64 + band])); - } - } - } - - std::array output{}; - for (int ear = 0; ear < 2; ++ear) { - for (int band = 0; band < 64; ++band) { - Complex value{}; - for (int branch = 0; branch < 4; ++branch) { - const Complex signal = add( - scale(taps[band * 4 + branch], output_taps_[band][branch]), - extra[band * 4 + branch]); - value = add(value, mul( - signal, output_matrix_[ear][band][branch])); - } - output[ear * kHybrid + band] = value; - } - } - return output; - } - - void synthesize_hybrid( - const std::array& input, - std::array& output) const noexcept { - for (size_t mapping = 0; mapping < hybrid_values_.size(); ++mapping) { - const int16_t* index = hybrid_indices_.data() + mapping * 4; - const int input_band = index[0]; - const int input_component = index[1]; - const int output_band = index[2]; - const int output_component = index[3]; - const double gain = hybrid_values_[mapping]; - for (int ear = 0; ear < 2; ++ear) { - const Complex source = input[ear * kHybrid + input_band]; - Complex& target = output[ear * kQmf + output_band]; - const double component = input_component == 0 ? source.re : source.im; - (output_component == 0 ? target.re : target.im) += component * gain; - } - } - } - - void synthesize_qmf(const Complex* input, int ear, - std::array& output) noexcept { - std::array features{}; - std::array flat{}; - for (int band = 0; band < 64; ++band) { - flat[band * 2] = input[band].re; - flat[band * 2 + 1] = input[band].im; - } - for (int phase = 0; phase < 64; ++phase) { - for (int rank = 0; rank < kRank; ++rank) { - double value = 0.0; - const size_t base = (static_cast(phase) * kRank + rank) * 128; - for (int component = 0; component < 128; ++component) { - value += flat[component] * qmf_basis_[base + component]; - } - features[phase * kRank + rank] = value; - } - } - for (int phase = 0; phase < 64; ++phase) { - double value = 0.0; - for (int lag = 0; lag < 10; ++lag) { - for (int rank = 0; rank < kRank; ++rank) { - const double feature = lag == 0 - ? features[phase * kRank + rank] - : synthesis_history_[synthesis_history_index( - ear, lag - 1, phase, rank)]; - value += feature * qmf_taps_[ - ((static_cast(phase) * 10 + lag) * kRank + rank)]; - } - } - output[phase] = value; - } - for (int lag = 8; lag > 0; --lag) { - for (int phase = 0; phase < 64; ++phase) { - for (int rank = 0; rank < kRank; ++rank) { - synthesis_history_[synthesis_history_index(ear, lag, phase, rank)] = - synthesis_history_[synthesis_history_index( - ear, lag - 1, phase, rank)]; - } - } - } - for (int phase = 0; phase < 64; ++phase) { - for (int rank = 0; rank < kRank; ++rank) { - synthesis_history_[synthesis_history_index(ear, 0, phase, rank)] = - features[phase * kRank + rank]; - } - } - } - - static size_t qmf_history_index(int lag, int channel, int phase) noexcept { - return (static_cast(lag) * kChannels + channel) * 64 + phase; - } - - static size_t hybrid_low_history_index(int lag, int channel, int band) noexcept { - return (static_cast(lag) * kChannels + channel) * 3 + band; - } - - static size_t hybrid_high_history_index(int delay, int channel, int band) noexcept { - return (static_cast(delay) * kChannels + channel) * 61 + band; - } - - static size_t hybrid_low_kernel_index( - int input_band, int input_component, int lag, - int output_band, int output_component) noexcept { - return (((static_cast(input_band) * 2 + input_component) * 13 + lag) * - 16 + output_band) * 2 + output_component; - } - - size_t room_memory_index(uint32_t position, int band, int branch) const noexcept { - return (static_cast(position) * 64 + band) * 4 + branch; - } - - static size_t synthesis_history_index( - int ear, int lag, int phase, int rank) noexcept { - return (((static_cast(ear) * 9 + lag) * 64 + phase) * kRank + rank); - } - - bool kernels_ready_ = false; - bool room_ready_ = false; - std::array qmf_analysis_{}; - std::vector hybrid_low_; - std::vector hybrid_indices_; - std::vector hybrid_values_; - std::array qmf_basis_{}; - std::array qmf_taps_{}; - - std::array qmf_history_{}; - std::array hybrid_low_history_{}; - std::array hybrid_high_history_{}; - std::array synthesis_history_{}; - - uint32_t room_bands_ = 0; - std::vector allpass_delays_; - std::vector allpass_gains_; - std::vector allpass_offsets_; - std::vector allpass_positions_; - std::vector allpass_memory_; - std::array fdn_delays_{}; - std::array fdn_matrix_{}; - uint32_t room_capacity_ = 0; - uint32_t output_tap_delay_ = 0; - std::array, 64> feedback_{}; - std::array, 64> output_taps_{}; - std::array, 64>, 2> output_matrix_{}; - std::vector room_memory_; - uint32_t room_position_ = 0; - std::vector extra_delays_; - std::vector extra_fields_; - std::vector extra_matrices_; - - std::array bit_reverse_{}; - std::array premod_{}; - std::array post_{}; - std::array even_post_{}; - char error_[256]{}; -}; - -} // namespace ejoc::binaural - -extern "C" { - -ejoc_binaural_renderer_handle EJOC_CALL ejoc_binaural_renderer_create(void) { - return new (std::nothrow) ejoc::binaural::Renderer(); -} - -void EJOC_CALL ejoc_binaural_renderer_destroy(ejoc_binaural_renderer_handle handle) { - delete static_cast(handle); -} - -int EJOC_CALL ejoc_binaural_renderer_reset(ejoc_binaural_renderer_handle handle) { - return handle ? static_cast(handle)->reset() : -1; -} - -const char* EJOC_CALL ejoc_binaural_renderer_last_error( - ejoc_binaural_renderer_handle handle) { - return handle ? static_cast(handle)->error() - : "null binaural renderer handle"; -} - -int EJOC_CALL ejoc_binaural_renderer_configure_kernels( - ejoc_binaural_renderer_handle handle, - const double* qmf_analysis, - const double* hybrid_low, - const int16_t* hybrid_indices, - const double* hybrid_values, - uint32_t hybrid_count, - const double* qmf_basis, - const double* qmf_taps) { - return handle ? static_cast(handle)->configure_kernels( - qmf_analysis, hybrid_low, hybrid_indices, hybrid_values, - hybrid_count, qmf_basis, qmf_taps) : -1; -} - -int EJOC_CALL ejoc_binaural_renderer_configure_room( - ejoc_binaural_renderer_handle handle, - uint32_t bands, - uint32_t allpass_count, - const uint32_t* allpass_delays, - const double* allpass_gains, - const uint32_t* fdn_delays, - const double* fdn_matrix, - uint32_t output_tap_delay, - const double* feedback_complex, - const double* output_taps, - const double* output_complex, - uint32_t extra_count, - const uint32_t* extra_delays, - const double* extra_fields_complex, - const double* extra_matrices) { - return handle ? static_cast(handle)->configure_room( - bands, allpass_count, allpass_delays, allpass_gains, - fdn_delays, fdn_matrix, output_tap_delay, - feedback_complex, output_taps, output_complex, - extra_count, extra_delays, extra_fields_complex, extra_matrices) : -1; -} - -int EJOC_CALL ejoc_binaural_renderer_process( - ejoc_binaural_renderer_handle handle, - const double* input16_interleaved, - const double* gains_complex, - const double* room_sends, - double output_gain, - double* output_stereo_interleaved) { - return handle ? static_cast(handle)->process( - input16_interleaved, gains_complex, room_sends, - output_gain, output_stereo_interleaved) : -1; -} - -} // extern "C" diff --git a/native/src/eac3joc_core.cpp b/native/src/eac3joc_core.cpp deleted file mode 100644 index 3b79a24..0000000 --- a/native/src/eac3joc_core.cpp +++ /dev/null @@ -1,751 +0,0 @@ -#define EJOC_BUILD_DLL -#include "eac3joc_core.h" -#include "qmf_tables.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace ejoc { - -struct Complex { - double re; - double im; -}; - -inline Complex mul(const Complex a, const Complex b) noexcept { - return {a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re}; -} - -inline double clamp_unit(double value) noexcept { - return value < -1.0 ? -1.0 : (value > 1.0 ? 1.0 : value); -} - -constexpr double kPi = 3.141592653589793238462643383279502884; -constexpr int kLfeDelay = 1217; -constexpr int kMaxBands = EJOC_MAX_PARAMETER_BANDS; - -const uint8_t* parameter_band_map(const int bands) noexcept { - using namespace tables; - switch (bands) { - case 1: return kPbMap1; - case 3: return kPbMap3; - case 5: return kPbMap5; - case 7: return kPbMap7; - case 9: return kPbMap9; - case 12: return kPbMap12; - case 15: return kPbMap15; - case 23: return kPbMap23; - default: return nullptr; - } -} - -class Renderer final { -public: - Renderer() noexcept { - initialize_tables(); - reset(); - } - - ~Renderer() noexcept { - stop_workers(); - } - - int reset() noexcept { - std::memset(analysis_fifo_, 0, sizeof(analysis_fifo_)); - std::memset(analysis_delay_, 0, sizeof(analysis_delay_)); - std::memset(surround_delay_, 0, sizeof(surround_delay_)); - std::memset(surround_history_, 0, sizeof(surround_history_)); - std::memset(lfe_delay_, 0, sizeof(lfe_delay_)); - std::memset(matrix_previous_, 0, sizeof(matrix_previous_)); - std::memset(synthesis_state_, 0, sizeof(synthesis_state_)); - std::memset(x_, 0, sizeof(x_)); - std::memset(z_, 0, sizeof(z_)); - analysis_phase_ = 0.0625f; - error_[0] = '\0'; - return 0; - } - - int set_threads(uint32_t total_threads) noexcept { - if (total_threads < 1) { - total_threads = 1; - } - if (total_threads > EJOC_MAX_OBJECTS) { - total_threads = EJOC_MAX_OBJECTS; - } - stop_workers(); - job_count_ = 0; - next_job_.store(0, std::memory_order_relaxed); - if (total_threads == 1) { - return 0; - } - try { - stop_.store(false, std::memory_order_relaxed); - pool_ready_.store(false, std::memory_order_relaxed); - work_barrier_ = std::make_unique>(static_cast(total_threads)); - workers_.reserve(total_threads - 1); - for (uint32_t index = 1; index < total_threads; ++index) { - workers_.emplace_back([this]() noexcept { worker_loop(); }); - } - pool_ready_.store(true, std::memory_order_release); - // Startup rendezvous: ensure every worker has entered the two-phase - // barrier loop before set_threads returns, so an immediate destroy - // cannot race a worker that exits before reaching the barrier. - work_barrier_->arrive_and_wait(); - work_barrier_->arrive_and_wait(); - } catch (...) { - stop_.store(true, std::memory_order_release); - pool_ready_.store(true, std::memory_order_release); - for (std::thread& worker : workers_) { - if (worker.joinable()) { - worker.join(); - } - } - workers_.clear(); - work_barrier_.reset(); - stop_.store(false, std::memory_order_relaxed); - return fail("failed to create native worker threads"); - } - return 0; - } - - uint32_t thread_count() const noexcept { - return static_cast(workers_.size() + 1); - } - - const char* error() const noexcept { - return error_[0] ? error_ : ""; - } - - void set_dc_filter(const bool enabled) noexcept { - dc_filter_enabled_ = enabled; - } - - int process( - const float* bed5, - const float* lfe, - const uint32_t object_mask, - const uint8_t* n_bands, - const uint8_t* n_dpoints, - const uint8_t* slope_idx, - const uint8_t* offset_ts, - const double* dq, - const double clipgain, - const float phase_new, - const float output_scale, - float* output16) noexcept { - - error_[0] = '\0'; - if (!bed5 || !n_bands || !n_dpoints || !slope_idx || !offset_ts || !dq || !output16) { - return fail("null pointer passed to ejoc_renderer_process"); - } - if (object_mask & ~((1u << EJOC_MAX_OBJECTS) - 1u)) { - return fail("object_mask contains an object index above 14"); - } - if (!std::isfinite(clipgain) || !std::isfinite(phase_new) || !std::isfinite(output_scale)) { - return fail("clipgain, phase_new, and output_scale must be finite"); - } - for (int object = 0; object < EJOC_MAX_OBJECTS; ++object) { - if ((object_mask & (1u << object)) == 0) { - continue; - } - if (!parameter_band_map(n_bands[object])) { - return fail("unsupported parameter-band count"); - } - if (n_dpoints[object] < 1 || n_dpoints[object] > 2) { - return fail("n_dpoints must be 1 or 2"); - } - if (slope_idx[object] > 1) { - return fail("slope_idx must be 0 or 1"); - } - } - - std::memset(output16, 0, sizeof(float) * EJOC_OUTPUT_CHANNELS * EJOC_FRAME_SAMPLES); - analysis(bed5, phase_new); - render_lfe(lfe, output_scale, output16); - process_objects(object_mask, n_bands, n_dpoints, slope_idx, offset_ts, - dq, clipgain, output_scale, output16); - return 0; - } - -private: - void initialize_tables() noexcept { - for (int i = 0; i < 64; ++i) { - int value = i; - int reversed = 0; - for (int bit = 0; bit < 6; ++bit) { - reversed = (reversed << 1) | (value & 1); - value >>= 1; - } - bit_reverse_[i] = static_cast(reversed); - const double theta = kPi * static_cast(i) / 128.0; - rotation_sin_[i] = 0.5 * std::sin(theta); - rotation_cos_[i] = 0.5 * std::cos(theta); - } - for (int i = 0; i < 32; ++i) { - const double angle = -2.0 * kPi * static_cast(i) / 64.0; - fft_twiddle_[i] = {std::cos(angle), std::sin(angle)}; - } - } - - int fail(const char* message) noexcept { - std::snprintf(error_, sizeof(error_), "%s", message); - return -1; - } - - void fft64(Complex* values) const noexcept { - for (int i = 0; i < 64; ++i) { - const int j = bit_reverse_[i]; - if (j > i) { - const Complex temp = values[i]; - values[i] = values[j]; - values[j] = temp; - } - } - for (int length = 2; length <= 64; length <<= 1) { - const int half = length >> 1; - const int twiddle_step = 64 / length; - for (int base = 0; base < 64; base += length) { - for (int j = 0; j < half; ++j) { - const Complex even = values[base + j]; - const Complex odd = mul(values[base + j + half], fft_twiddle_[j * twiddle_step]); - values[base + j] = {even.re + odd.re, even.im + odd.im}; - values[base + j + half] = {even.re - odd.re, even.im - odd.im}; - } - } - } - } - - void analysis_slot(const int channel, const float* ring, const int timeslot) noexcept { - double v36[64]; - double v40[64]; - Complex frequency[64]; - - for (int sample = 0; sample < 64; ++sample) { - v36[sample] = - analysis_fifo_[channel][0][sample] * tables::kAnalysisWindow[8 * 64 + sample] + - analysis_fifo_[channel][2][sample] * tables::kAnalysisWindow[6 * 64 + sample] + - analysis_fifo_[channel][4][sample] * tables::kAnalysisWindow[4 * 64 + sample] + - analysis_fifo_[channel][6][sample] * tables::kAnalysisWindow[2 * 64 + sample] + - analysis_fifo_[channel][8][sample] * tables::kAnalysisWindow[0 * 64 + sample]; - v40[sample] = - analysis_fifo_[channel][1][sample] * tables::kAnalysisWindow[7 * 64 + sample] + - analysis_fifo_[channel][3][sample] * tables::kAnalysisWindow[5 * 64 + sample] + - analysis_fifo_[channel][5][sample] * tables::kAnalysisWindow[3 * 64 + sample] + - analysis_fifo_[channel][7][sample] * tables::kAnalysisWindow[1 * 64 + sample] + - static_cast(ring[sample]) * tables::kAnalysisWindow[9 * 64 + sample]; - } - - for (int k = 0; k < 64; ++k) { - const int source = 63 - k; - const double re = v40[source]; - const double im = v36[source]; - const double a = rotation_sin_[k]; - const double b = rotation_cos_[k]; - frequency[k] = {im * a - re * b, im * b + re * a}; - } - fft64(frequency); - constexpr double scale = 1.0 / 64.0; - for (int k = 0; k < 32; ++k) { - x_[channel][2 * k][timeslot] = {frequency[k].re * scale, -frequency[k].im * scale}; - x_[channel][2 * k + 1][timeslot] = { - frequency[63 - k].re * scale, - frequency[63 - k].im * scale}; - } - - for (int history = 8; history > 0; --history) { - std::memcpy(analysis_fifo_[channel][history], analysis_fifo_[channel][history - 1], - sizeof(analysis_fifo_[channel][history])); - } - for (int sample = 0; sample < 64; ++sample) { - analysis_fifo_[channel][0][sample] = static_cast(ring[sample]); - } - } - - void surround_post() noexcept { - static constexpr double kDcA[21] = { - -.0006242550443857908, -.0019234686624258757, -.0042654648423194885, - -.008168308064341545, -.014327201060950756, -.023759860545396805, - -.03757232800126076, -.05577569454908371, -.07568276673555374, - -.09172472357749939, -.5979374051094055, -.09172472357749939, - -.07568276673555374, -.05577569454908371, -.03757232800126076, - -.023759860545396805, -.014327201060950756, -.008168308064341545, - -.0042654648423194885, -.0019234686624258757, -.0006242550443857908, - }; - static constexpr double kDcB[21] = { - .0013996040215715766, .003839150769636035, .007512642536312342, - .012419373728334904, .018367428332567215, .0249701626598835, - .03167900815606117, .03785000368952751, .04283412545919418, - .04607561603188515, .047200120985507965, .04607561603188515, - .04283412545919418, .03785000368952751, .03167900815606117, - .0249701626598835, .018367428332567215, .012419373728334904, - .007512642536312342, .003839150769636035, .0013996040215715766, - }; - - for (int surround = 0; surround < 2; ++surround) { - const int channel = surround + 3; - for (int group = 0; group < 24; group += 4) { - Complex current[4][64]; - Complex dc_buffer[24]; - for (int slot = 0; slot < 4; ++slot) { - for (int band = 0; band < 64; ++band) { - current[slot][band] = x_[channel][band][group + slot]; - const Complex delayed = surround_delay_[surround][slot][band]; - x_[channel][band][group + slot] = {delayed.im, -delayed.re}; - } - } - - for (int i = 0; i < 20; ++i) { - dc_buffer[i] = surround_history_[surround][i]; - } - for (int i = 0; i < 4; ++i) { - dc_buffer[20 + i] = current[i][0]; - } - if (dc_filter_enabled_) { - for (int slot = 0; slot < 4; ++slot) { - Complex sum{0.0, 0.0}; - for (int tap = 0; tap < 21; ++tap) { - const Complex sample = dc_buffer[slot + tap]; - const double cr = kDcB[tap]; - const double ci = kDcA[tap]; - sum.re += sample.re * cr - sample.im * ci; - sum.im += sample.re * ci + sample.im * cr; - } - x_[channel][0][group + slot] = {2.0 * sum.re, 2.0 * sum.im}; - } - } - for (int i = 0; i < 20; ++i) { - surround_history_[surround][i] = dc_buffer[i + 4]; - } - - std::memmove(&surround_delay_[surround][0][0], - &surround_delay_[surround][4][0], - sizeof(Complex) * 6 * 64); - for (int slot = 0; slot < 4; ++slot) { - std::memcpy(surround_delay_[surround][6 + slot], current[slot], - sizeof(Complex) * 64); - } - } - } - } - - void analysis_channel(const int channel) noexcept { - const float* bed5 = job_bed5_; - const float phase_old = job_phase_old_; - const float phase_new = job_phase_new_; - const bool ramp_phase = phase_old != phase_new; - const float phase_step = static_cast((phase_new - phase_old) / 256.0f); - float scaled[64]; - float delayed[64]; - - for (int timeslot = 0; timeslot < 24; ++timeslot) { - for (int sample = 0; sample < 64; ++sample) { - const int frame_sample = timeslot * 64 + sample; - float gain = phase_new; - if (ramp_phase && frame_sample < 256) { - const float product = static_cast(static_cast(frame_sample) * phase_step); - gain = static_cast(phase_old + product); - } - scaled[sample] = static_cast(bed5[channel * 1536 + frame_sample] * gain); - } - const float* analysis_input = scaled; - if (channel < 3) { - std::memcpy(delayed, analysis_delay_[channel][0], sizeof(delayed)); - std::memmove(&analysis_delay_[channel][0][0], - &analysis_delay_[channel][1][0], - sizeof(float) * 9 * 64); - std::memcpy(analysis_delay_[channel][9], scaled, sizeof(scaled)); - analysis_input = delayed; - } - analysis_slot(channel, analysis_input, timeslot); - } - } - - void analysis(const float* bed5, const float phase_new) noexcept { - job_bed5_ = bed5; - job_phase_old_ = analysis_phase_; - job_phase_new_ = phase_new; - job_kind_ = JobKind::Analysis; - job_count_ = 5; - for (int channel = 0; channel < 5; ++channel) { - job_objects_[channel] = channel; - } - dispatch_jobs(); - analysis_phase_ = phase_new; - surround_post(); - } - - static std::size_t dq_index(const int object, const int point, const int channel, const int band) noexcept { - return static_cast((((object * 2 + point) * 5 + channel) * kMaxBands) + band); - } - - void matrix_object( - const int object, - const uint8_t* n_bands, - const uint8_t* n_dpoints, - const uint8_t* slope_idx, - const uint8_t* offset_ts, - const double* dq) noexcept { - - std::memset(z_[object], 0, sizeof(z_[object])); - const int bands = n_bands[object]; - const int points = n_dpoints[object]; - const int slope = slope_idx[object]; - const uint8_t* pb_map = parameter_band_map(bands); - - for (int channel = 0; channel < 5; ++channel) { - for (int subband = 0; subband < 64; ++subband) { - const int parameter_band = pb_map[subband]; - const double previous = matrix_previous_[object][channel][subband]; - const double target0 = dq[dq_index(object, 0, channel, parameter_band)]; - const double target1 = points == 2 - ? dq[dq_index(object, 1, channel, parameter_band)] - : target0; - double last = previous; - - for (int timeslot = 0; timeslot < 24; ++timeslot) { - double coefficient; - if (slope == 0) { - if (points == 1) { - const double alpha = static_cast(timeslot + 1) / 24.0; - coefficient = previous * (1.0 - alpha) + target0 * alpha; - } else if (timeslot < 12) { - const double alpha = static_cast(timeslot + 1) / 12.0; - coefficient = previous * (1.0 - alpha) + target0 * alpha; - } else { - const double alpha = static_cast(timeslot - 11) / 12.0; - coefficient = target0 * (1.0 - alpha) + target1 * alpha; - } - } else if (points == 1) { - coefficient = timeslot < offset_ts[object * 2] ? previous : target0; - } else { - coefficient = timeslot < offset_ts[object * 2] ? previous : target0; - if (timeslot >= offset_ts[object * 2 + 1]) { - coefficient = target1; - } - } - z_[object][subband][timeslot].re += x_[channel][subband][timeslot].re * coefficient; - z_[object][subband][timeslot].im += x_[channel][subband][timeslot].im * coefficient; - last = coefficient; - } - matrix_previous_[object][channel][subband] = last; - } - } - } - - void qmf5_step(double* state, const double* rotated, double* output) noexcept { - for (int block = 0; block < 16; ++block) { - for (int lane = 0; lane < 4; ++lane) { - const int sample = block * 4 + lane; - const int state_base = block * 36 + lane; - const double even = rotated[block * 8 + lane * 2]; - const double odd = rotated[block * 8 + lane * 2 + 1]; - output[sample] = 2.0 * ( - tables::kQmf5Window[sample] * even + state[state_base]); - - state[state_base] = tables::kQmf5Window[1 * 64 + sample] * odd + state[state_base + 4]; - for (int slot = 0; slot < 7; ++slot) { - const double alternating = (slot & 1) == 0 ? even : odd; - state[state_base + (slot + 1) * 4] = - tables::kQmf5Window[(slot + 2) * 64 + sample] * alternating + - state[state_base + (slot + 2) * 4]; - } - state[state_base + 8 * 4] = tables::kQmf5Window[9 * 64 + sample] * odd; - } - } - } - - void synthesis_object( - const int object, - const double clipgain, - const float output_scale, - float* output16) noexcept { - - Complex frequency[64]; - double rotated[128]; - double pcm64[64]; - double* state = synthesis_state_[object]; - float* destination = output16 + (object + 1) * 1536; - for (int timeslot = 0; timeslot < 24; ++timeslot) { - for (int k = 0; k < 32; ++k) { - const Complex even = z_[object][2 * k][timeslot]; - const Complex odd = z_[object][2 * k + 1][timeslot]; - frequency[k] = {even.re, -even.im}; - frequency[63 - k] = {odd.re, odd.im}; - } - fft64(frequency); - for (int k = 0; k < 64; ++k) { - const double re = frequency[k].re; - const double im = frequency[k].im; - const double sin_component = rotation_sin_[k]; - const double cos_component = rotation_cos_[k]; - rotated[2 * k] = 2.0 * (re * cos_component + im * sin_component); - rotated[2 * k + 1] = 2.0 * (im * cos_component - re * sin_component); - } - qmf5_step(state, rotated, pcm64); - for (int sample = 0; sample < 64; ++sample) { - const double clipped = clamp_unit(16.0 * pcm64[sample]); - const float value = static_cast(clipped * clipgain); - destination[timeslot * 64 + sample] = static_cast(value * output_scale); - } - } - } - - void process_one_object(const int object) noexcept { - matrix_object(object, job_n_bands_, job_n_dpoints_, job_slope_idx_, - job_offset_ts_, job_dq_); - synthesis_object(object, job_clipgain_, job_output_scale_, job_output16_); - } - - void execute_job_loop() noexcept { - while (true) { - const int index = next_job_.fetch_add(1, std::memory_order_relaxed); - if (index >= job_count_) { - break; - } - const int item = job_objects_[index]; - if (job_kind_ == JobKind::Analysis) { - analysis_channel(item); - } else { - process_one_object(item); - } - } - } - - void dispatch_jobs() noexcept { - if (workers_.empty() || job_count_ == 1) { - next_job_.store(0, std::memory_order_relaxed); - execute_job_loop(); - return; - } - next_job_.store(0, std::memory_order_relaxed); - work_barrier_->arrive_and_wait(); - execute_job_loop(); - work_barrier_->arrive_and_wait(); - } - - void worker_loop() noexcept { - while (!pool_ready_.load(std::memory_order_acquire)) { - std::this_thread::yield(); - } - if (stop_.load(std::memory_order_acquire)) { - return; - } - while (true) { - work_barrier_->arrive_and_wait(); - if (stop_.load(std::memory_order_acquire)) { - return; - } - execute_job_loop(); - work_barrier_->arrive_and_wait(); - } - } - - void stop_workers() noexcept { - if (workers_.empty()) { - work_barrier_.reset(); - stop_.store(false, std::memory_order_relaxed); - pool_ready_.store(false, std::memory_order_relaxed); - return; - } - stop_.store(true, std::memory_order_release); - work_barrier_->arrive_and_wait(); - for (std::thread& worker : workers_) { - if (worker.joinable()) { - worker.join(); - } - } - workers_.clear(); - work_barrier_.reset(); - stop_.store(false, std::memory_order_relaxed); - pool_ready_.store(false, std::memory_order_relaxed); - } - - void process_objects( - const uint32_t object_mask, - const uint8_t* n_bands, - const uint8_t* n_dpoints, - const uint8_t* slope_idx, - const uint8_t* offset_ts, - const double* dq, - const double clipgain, - const float output_scale, - float* output16) noexcept { - - int count = 0; - for (int object = 0; object < EJOC_MAX_OBJECTS; ++object) { - if (object_mask & (1u << object)) { - job_objects_[count++] = object; - } - } - if (count == 0) { - return; - } - job_kind_ = JobKind::Objects; - job_count_ = count; - job_n_bands_ = n_bands; - job_n_dpoints_ = n_dpoints; - job_slope_idx_ = slope_idx; - job_offset_ts_ = offset_ts; - job_dq_ = dq; - job_clipgain_ = clipgain; - job_output_scale_ = output_scale; - job_output16_ = output16; - - dispatch_jobs(); - } - - void render_lfe(const float* lfe, const float output_scale, float* output16) noexcept { - if (!lfe) { - return; - } - for (int sample = 0; sample < kLfeDelay; ++sample) { - const float value = static_cast(clamp_unit(lfe_delay_[sample])); - output16[sample] = static_cast(value * output_scale); - } - for (int sample = kLfeDelay; sample < 1536; ++sample) { - const float value = static_cast(clamp_unit(static_cast(lfe[sample - kLfeDelay]))); - output16[sample] = static_cast(value * output_scale); - } - for (int sample = 0; sample < kLfeDelay; ++sample) { - lfe_delay_[sample] = static_cast(lfe[sample + (1536 - kLfeDelay)]); - } - } - - enum class JobKind : uint8_t { Analysis, Objects }; - - std::vector workers_; - std::unique_ptr> work_barrier_; - std::atomic stop_{false}; - std::atomic pool_ready_{false}; - std::atomic next_job_{0}; - int job_objects_[EJOC_MAX_OBJECTS]{}; - int job_count_ = 0; - JobKind job_kind_ = JobKind::Objects; - const float* job_bed5_ = nullptr; - float job_phase_old_ = 0.0625f; - float job_phase_new_ = 0.0625f; - const uint8_t* job_n_bands_ = nullptr; - const uint8_t* job_n_dpoints_ = nullptr; - const uint8_t* job_slope_idx_ = nullptr; - const uint8_t* job_offset_ts_ = nullptr; - const double* job_dq_ = nullptr; - double job_clipgain_ = 1.0; - float job_output_scale_ = 1.0f; - float* job_output16_ = nullptr; - - alignas(64) double analysis_fifo_[5][9][64]; - alignas(64) float analysis_delay_[3][10][64]; - float analysis_phase_; - alignas(64) Complex surround_delay_[2][10][64]; - alignas(64) Complex surround_history_[2][20]; - // band-0 的 21-tap DC 补偿开关;仅 downmix 配置 3/4 由调用方置位。 - bool dc_filter_enabled_ = true; - alignas(64) double lfe_delay_[kLfeDelay]; - alignas(64) double matrix_previous_[15][5][64]; - alignas(64) double synthesis_state_[15][640]; - alignas(64) Complex x_[5][64][24]; - alignas(64) Complex z_[15][64][24]; - - uint8_t bit_reverse_[64]; - Complex fft_twiddle_[32]; - double rotation_sin_[64]; - double rotation_cos_[64]; - char error_[256]; -}; - -} // namespace ejoc - -extern "C" { - -uint32_t EJOC_CALL ejoc_abi_version(void) { - return EJOC_ABI_VERSION; -} - -const char* EJOC_CALL ejoc_build_info(void) { -#if defined(_MSC_VER) - return "eac3joc-core abi=1 compiler=MSVC fft=fixed64 speaker=double binaural=double crt=static-by-build"; -#elif defined(__clang__) - return "eac3joc-core abi=1 compiler=Clang fft=fixed64 speaker=double binaural=double"; -#elif defined(__GNUC__) - return "eac3joc-core abi=1 compiler=GCC fft=fixed64 speaker=double binaural=double"; -#else - return "eac3joc-core abi=1 compiler=unknown fft=fixed64 speaker=double binaural=double"; -#endif -} - -ejoc_renderer_handle EJOC_CALL ejoc_renderer_create(void) { - return new (std::nothrow) ejoc::Renderer(); -} - -void EJOC_CALL ejoc_renderer_destroy(ejoc_renderer_handle handle) { - delete static_cast(handle); -} - -int EJOC_CALL ejoc_renderer_reset(ejoc_renderer_handle handle) { - if (!handle) { - return -1; - } - return static_cast(handle)->reset(); -} - -int EJOC_CALL ejoc_renderer_set_threads(ejoc_renderer_handle handle, uint32_t total_threads) { - if (!handle) { - return -1; - } - return static_cast(handle)->set_threads(total_threads); -} - -int EJOC_CALL ejoc_renderer_set_dc_filter(ejoc_renderer_handle handle, uint32_t enabled) { - if (!handle) { - return -1; - } - static_cast(handle)->set_dc_filter(enabled != 0); - return 0; -} - -uint32_t EJOC_CALL ejoc_renderer_thread_count(ejoc_renderer_handle handle) { - if (!handle) { - return 0; - } - return static_cast(handle)->thread_count(); -} - -const char* EJOC_CALL ejoc_renderer_last_error(ejoc_renderer_handle handle) { - if (!handle) { - return "null renderer handle"; - } - return static_cast(handle)->error(); -} - -int EJOC_CALL ejoc_renderer_process( - ejoc_renderer_handle handle, - const float* bed5_planar, - const float* lfe, - const uint32_t object_mask, - const uint8_t* n_bands, - const uint8_t* n_dpoints, - const uint8_t* slope_idx, - const uint8_t* offset_ts, - const double* dq, - const double clipgain, - const float phase_new, - const float output_scale, - float* output16_planar) { - if (!handle) { - return -1; - } - return static_cast(handle)->process( - bed5_planar, lfe, object_mask, n_bands, n_dpoints, slope_idx, - offset_ts, dq, clipgain, phase_new, output_scale, output16_planar); -} - -} // extern "C" diff --git a/native/src/joc_huffman_tables.h b/native/src/joc_huffman_tables.h deleted file mode 100644 index c3911a6..0000000 --- a/native/src/joc_huffman_tables.h +++ /dev/null @@ -1,89 +0,0 @@ -#pragma once - -// JOC Huffman trees required by the bitstream parser. - -static const int joc_huff_code_coarse_generic[][2] = -{ - { -1, 1}, { 2, -2}, { -96, 3}, { 4, -3}, { -95, 5}, { 6, 7}, { -4, -94}, { 8, 9}, { -5, -93}, { 10, 11}, - { -6, -92}, { 12, 13}, { -7, -91}, { 14, 15}, { 16, -90}, { -8, 17}, { 18, -89}, { -9, 19}, { 20, 21}, { -88, -10}, - { 22, 23}, { -11, -87}, { 24, 25}, { 26, -86}, { -12, 27}, { 28, -85}, { -13, 29}, { 30, 31}, { 32, -84}, { -14, 33}, - { 34, -15}, { -83, 35}, { 36, 37}, { -16, 38}, { -17, -82}, { 39, 40}, { 41, -81}, { 42, 43}, { 44, 45}, { 46, 47}, - { 48, 49}, { 50, 51}, { 52, -18}, { -78, 53}, { -19, 54}, { 55, 56}, { 57, 58}, { -22, 59}, { 60, 61}, { 62, 63}, - { 64, 65}, { 66, 67}, { 68, -20}, { -21, -79}, { -80, -25}, { 69, 70}, { -26, 71}, { 72, 73}, { 74, 75}, { 76, 77}, - { 78, 79}, { 80, 81}, { 82, 83}, { 84, 85}, { 86, 87}, { 88, 89}, { 90, 91}, { 92, 93}, { 94, -23}, { -74, -75}, - { -72, -73}, { -76, -77}, { -34, -35}, { -32, -33}, { -38, -39}, { -36, -37}, { -30, -31}, { -28, -29}, { -50, -51}, { -48, -49}, - { -54, -55}, { -52, -53}, { -42, -43}, { -40, -41}, { -46, -47}, { -44, -45}, { -66, -67}, { -64, -65}, { -70, -71}, { -68, -69}, - { -58, -59}, { -56, -57}, { -62, -63}, { -60, -61}, { -24, -27} -}; - -static const int joc_huff_code_fine_generic[][2] = -{ - { -1, 1}, { 2, 3}, { -2,-192}, { 4, 5}, { 6, -3}, {-191, 7}, { 8, 9}, { -4,-190}, { 10, 11}, { -5,-189}, - { 12, 13}, { -6, 14}, {-188, 15}, { 16, -7}, {-187, 17}, { 18, -8}, {-186, 19}, { 20, -9}, {-185, 21}, { 22, -10}, - {-184, 23}, { 24, -11}, { 25,-183}, { 26, 27}, { -12,-182}, { 28, 29}, { -13,-181}, { 30, 31}, {-180, -14}, { 32, 33}, - { 34,-179}, { -15, 35}, { 36,-178}, { -16, 37}, { 38,-177}, { 39, -17}, { 40, 41}, {-176, 42}, { -18, 43}, { -19, 44}, - {-175, 45}, { 46,-174}, { -20, 47}, {-173, 48}, { 49, -21}, { 50, 51}, { 52, -22}, { 53, 54}, {-172, 55}, {-171, -23}, - { 56, 57}, { 58,-170}, { 59, -24}, { -25, 60}, {-169, 61}, { 62, 63}, { 64, 65}, { 66, 67}, {-168, 68}, { -26, 69}, - {-167, -27}, { 70,-166}, {-165, 71}, { -29, 72}, { 73, 74}, { -30, 75}, { 76, 77}, { 78, 79}, { 80, -28}, { 81, 82}, - { 83,-163}, { -31, -33}, {-164,-161}, { 84, 85}, { 86, 87}, { 88, 89}, { 90, 91}, { 92, 93}, { 94, 95}, { 96, 97}, - { 98, 99}, { -32,-162}, { 100, 101}, { 102, 103}, { 104, 105}, { 106, 107}, { 108, 109}, { 110, 111}, {-160, 112}, { -36, -38}, - { 113, 114}, { 115, 116}, { 117, 118}, { 119, 120}, { 121, 122}, { 123, 124}, { 125, 126}, { 127, 128}, { 129, 130}, { 131, 132}, - { 133, -35}, {-158, 134}, {-155,-156}, { -37, -42}, { 135, 136}, { 137, 138}, { 139, 140}, { 141, 142}, { 143, 144}, { 145, 146}, - { 147, 148}, { 149, 150}, { 151, 152}, { 153, 154}, { 155, 156}, { 157, 158}, { 159, 160}, { 161, 162}, { 163, 164}, { 165, 166}, - { 167, 168}, { 169, 170}, { 171, 172}, { 173, 174}, { 175, 176}, { 177, 178}, { 179, 180}, { 181, 182}, { 183, 184}, {-157, 185}, - { -45, -48}, { 186, 187}, { 188, 189}, { -34, -41}, { 190, -39}, { -60, -61}, { -58, -59}, { -64, -65}, { -62, -63}, { -52, -53}, - { -50, -51}, { -56, -57}, { -54, -55}, { -76, -77}, { -74, -75}, { -80, -81}, { -78, -79}, { -68, -69}, { -66, -67}, { -72, -73}, - { -70, -71}, { -47, -49}, { -44, -46}, {-124,-125}, {-122,-123}, {-128,-129}, {-126,-127}, {-116,-117}, {-114,-115}, {-120,-121}, - {-118,-119}, {-140,-141}, {-138,-139}, {-144,-145}, {-142,-143}, {-132,-133}, {-130,-131}, {-136,-137}, {-134,-135}, { -92, -93}, - { -90, -91}, { -96, -97}, { -94, -95}, { -84, -85}, { -82, -83}, { -88, -89}, { -86, -87}, {-108,-109}, {-106,-107}, {-112,-113}, - {-110,-111}, {-100,-101}, { -98, -99}, {-104,-105}, {-102,-103}, {-154,-159}, {-148,-149}, {-146,-147}, {-152,-153}, {-150,-151}, - { -40, -43} -}; - -static const int joc_huff_code_coarse_coeff_sparse[][2] = -{ - { -1, 1}, { 2, 3}, { -2, -96}, { 4, 5}, { 6, -95}, { -3, 7}, { 8, 9}, { -4, 10}, { -94, 11}, { 12, -5}, - { -93, 13}, { 14, 15}, { -6, -92}, { 16, 17}, { 18, -7}, { -91, 19}, { 20, -8}, { -90, 21}, { 22, 23}, { -9, -89}, - { 24, 25}, { 26, -10}, { -88, 27}, { 28, 29}, { 30, -11}, { -87, 31}, { 32, 33}, { 34, 35}, { -12, -86}, { 36, 37}, - { 38, -13}, { 39, -85}, { 40, 41}, { 42, 43}, { -14, -84}, { 44, 45}, { 46, 47}, { -83, -15}, { 48, 49}, { 50, -16}, - { 51, 52}, { -82, 53}, { 54, -81}, { 55, 56}, { -17, 57}, { 58, -80}, { 59, 60}, { -18, 61}, { 62, 63}, { -79, 64}, - { -19, -78}, { 65, 66}, { 67, 68}, { 69, -20}, { -77, -21}, { 70, 71}, { 72, 73}, { 74, -76}, { 75, -22}, { 76, 77}, - { -75, 78}, { 79, 80}, { -54, -74}, { -73, 81}, { -23, 82}, { -50, -24}, { -55, -25}, { 83, -47}, { -49, -44}, { -71, 84}, - { -48, -51}, { 85, -72}, { -26, -53}, { -70, -27}, { 86, -45}, { 87, 88}, { -68, 89}, { -29, -43}, { 90, -30}, { -46, -69}, - { 91, -28}, { -52, -31}, { 92, -32}, { 93, -64}, { -67, 94}, { -36, -33}, { -63, -37}, { -65, -61}, { -66, -59}, { -34, -38}, - { -41, -42}, { -35, -60}, { -39, -57}, { -56, -40}, { -62, -58} -}; - -static const int joc_huff_code_fine_coeff_sparse[][2] = -{ - { 1, -1}, { 2, 3}, { 4, -2}, {-192, 5}, { 6, 7}, { 8, -3}, {-191, 9}, { 10, 11}, { 12,-190}, { -4, 13}, - { 14, 15}, {-189, -5}, { 16, 17}, { 18, -6}, {-188, 19}, { 20, 21}, { -7,-187}, { 22, 23}, { -8, 24}, {-186, 25}, - { -9, 26}, { 27,-185}, { 28, -10}, { 29, 30}, {-184, 31}, { -11, 32}, { 33,-183}, { 34, -12}, { 35,-182}, { 36, 37}, - { 38, -13}, {-181, 39}, { 40, -14}, { 41,-180}, { 42, 43}, {-179, -15}, { 44, -16}, { 45,-178}, { 46, 47}, { 48, 49}, - { 50,-177}, { -17, 51}, { -18, 52}, {-176, 53}, { 54, 55}, {-175, -19}, { 56, 57}, { 58, -20}, { 59,-174}, { 60, 61}, - { -21, 62}, { 63,-173}, { 64, 65}, { 66,-172}, { 67, 68}, { -22, 69}, { 70, 71}, { -23, 72}, {-171, 73}, { 74, 75}, - { 76, -24}, { 77,-170}, { -25, 78}, { 79, 80}, { 81,-169}, { 82, 83}, { 84, -26}, { 85,-168}, { 86, 87}, { 88, 89}, - {-167, 90}, { -27, 91}, { 92, -28}, { 93,-166}, { 94, -29}, { 95, 96}, { 97, 98}, {-165, 99}, { 100, -30}, {-164, 101}, - { 102, 103}, { 104, 105}, {-163, 106}, { -31, 107}, { -32, 108}, { 109, 110}, {-161, 111}, {-160,-162}, { 112, -34}, { -33, 113}, - { 114, 115}, { 116, 117}, { 118, 119}, { 120,-159}, { 121, 122}, { 123,-158}, { 124, 125}, { -36,-155}, { 126, 127}, { -35, 128}, - { 129, 130}, {-157, 131}, {-156, 132}, { -37, 133}, { 134, 135}, {-154, -38}, { 136, 137}, { -39, -41}, { 138,-153}, { 139, -40}, - {-149, 140}, { 141, 142}, { 143, 144}, {-151, 145}, { 146, 147}, { 148, -42}, { -43, 149}, { 150, 151}, {-152, 152}, { -46, -98}, - { 153, 154}, { 155,-147}, { 156, 157}, { 158,-107}, { 159, 160}, {-145,-150}, { -96, 161}, { 162, -45}, {-146, 163}, { 164, -97}, - {-108,-105}, {-148,-106}, { -44, 165}, { -94,-141}, { -99, 166}, { -89, 167}, { -50, -95}, {-100, -48}, {-144, 168}, { 169, 170}, - { -51,-142}, { -90, -91}, { -47, -49}, { 171, -53}, { -93,-143}, {-137,-138}, { -55,-101}, { 172, 173}, { -54, -86}, { -88, -87}, - {-103, 174}, { 175, -61}, {-109, 176}, { 177, 178}, { -52,-139}, { -57,-140}, { 179, 180}, { -56,-136}, { -58,-102}, { 181, 182}, - { -60,-135}, { 183,-104}, {-128,-134}, { -92, 184}, { -59, -62}, { 185, 186}, { -71,-133}, { 187,-127}, {-126, 188}, { -63, -64}, - { -85,-132}, { 189, -66}, {-121,-125}, { 190, -68}, { -74, -75}, { -70, -73}, { -81, -65}, {-118,-131}, { -72,-110}, {-119,-120}, - { -76, -84}, {-122,-130}, { -83,-117}, { -69, -78}, { -80, -82}, {-123,-124}, { -67,-116}, {-129, -77}, {-113,-114}, {-112,-115}, - { -79,-111} -}; - -static const int joc_huff_code_5ch_pos_index_sparse[][2] = -{ - { -1, 1}, { 2, 3}, { -4, -3}, { -2, -5} -}; - -static const int joc_huff_code_7ch_pos_index_sparse[][2] = -{ - { -1, 1}, { 2, 3}, { 4, 5}, { -4, -3}, { -2, -5}, { -6, -7} -}; diff --git a/native/src/qmf_tables.h b/native/src/qmf_tables.h deleted file mode 100644 index e63f0e3..0000000 --- a/native/src/qmf_tables.h +++ /dev/null @@ -1,388 +0,0 @@ -#pragma once - -#include - -// Generated table data; do not hand-edit. -namespace ejoc::tables { -inline constexpr double kAnalysisWindow[640] = { - 0.00019903187057934701, 0.00024947625934146345, 0.00030217695166356862, 0.00035484600812196732, - 0.0004058915947098285, 0.00045464080176316202, 0.00050126801943406463, 0.00054649583762511611, - 0.00059120741207152605, 0.0006361178238876164, 0.00068160606315359473, 0.00072772568091750145, - 0.00077434181002900004, 0.00082129903603345156, 0.00086853635730221868, 0.00091610715026035905, - 0.00096411682898178697, 0.0010126305278390646, 0.0010616052895784378, 0.0011108826147392392, - 0.0011602368904277682, 0.0012094489065930247, 0.0012583627831190825, 0.0013069023843854666, - 0.0013550462899729609, 0.0014027846045792103, 0.0014500867109745741, 0.0014968989416956902, - 0.0015431707724928856, 0.0015888890484347939, 0.0016340982401743531, 0.0016788924112915993, - 0.0017233812250196934, 0.0017676511779427528, 0.0018117419676855206, 0.0018556505674496293, - 0.0018993609119206667, 0.0019428766099736094, 0.0019862416666001081, 0.0020295341964811087, - 0.0020728406962007284, 0.0021162291523069143, 0.0021597379818558693, 0.0022033930290490389, - 0.0022472396958619356, 0.0022913739085197449, 0.0023359460756182671, 0.0023811329156160355, - 0.0024270866997539997, 0.0024738919455558062, 0.0025215502828359604, 0.0025700139813125134, - 0.0026192441582679749, 0.0026692659594118595, 0.002720177173614502, 0.0027720888610929251, - 0.0028250094037503004, 0.0028787164483219385, 0.0029326770454645157, 0.0029860674403607845, - 0.0030379060190171003, 0.0030872693751007318, 0.0031335193198174238, 0.0031764607410877943, - 0.0032163741998374462, 0.0032539025414735079, 0.0032898378558456898, 0.0033248732797801495, - 0.003359407652169466, 0.0033934540115296841, 0.0034266682341694832, 0.0034584659151732922, - 0.0034881711471825838, 0.0035151413176208735, 0.0035388274118304253, 0.0035587677266448736, - 0.0035745392087846994, 0.0035856980830430984, 0.0035917432978749275, 0.0035921167582273483, - 0.0035862282384186983, 0.0035734928678721189, 0.0035533567424863577, 0.0035253004170954227, - 0.0034888240043073893, 0.003443423192948103, 0.0033885682933032513, 0.0033236993476748466, - 0.0032482317183166742, 0.0031615688931196928, 0.0030631136614829302, 0.0029522709082812071, - 0.0028284420259296894, 0.0026910160668194294, 0.0025393660180270672, 0.0023728485684841871, - 0.0021908141206949949, 0.0019926181994378567, 0.0017776311142370105, 0.0015452421503141522, - 0.001294855959713459, 0.0010258855763822794, 0.00073774566408246756, 0.00042984966421499848, - 0.00010161137470277026, -0.00024754938203841448, -0.00061819725669920444, -0.0010108760325238109, - -0.001426108181476593, -0.0018643926596269011, -0.0023262077011168003, -0.002812013728544116, - -0.0033222525380551815, -0.003857344388961792, -0.0044176783412694931, -0.0050036045722663403, - -0.0056154225021600723, -0.0062533821910619736, -0.0069176913239061832, -0.0076085370965301991, - -0.008326113224029541, -0.0090706516057252884, -0.0098424339666962624, -0.010641784407198429, - -0.011469035409390926, -0.012324465438723564, -0.013208229094743729, -0.014120301231741905, - 0.015060451813042164, 0.016028247773647308, 0.017023105174303055, 0.018044359982013702, - 0.019091326743364334, 0.020163353532552719, 0.021259821951389313, 0.022380130365490913, - 0.02352365106344223, 0.024689681828022003, 0.025877414271235466, 0.027085918933153152, - 0.028314167633652687, 0.029561035335063934, 0.030825328081846237, 0.032105788588523865, - 0.033401083201169968, 0.034709792584180832, 0.036030396819114685, 0.037361271679401398, - 0.03870067372918129, 0.040046781301498413, 0.041397668421268463, 0.042751342058181763, - 0.044105727225542068, 0.045458663254976273, 0.046807888895273209, 0.048151064664125443, - 0.049485750496387482, 0.050809424370527267, 0.05211947113275528, 0.053413204848766327, - 0.054687850177288055, 0.055940557271242142, 0.057168368250131607, 0.058368254452943802, - 0.059537097811698914, 0.06067170575261116, 0.061768818646669388, 0.062825113534927368, - 0.063837200403213501, 0.064801648259162903, 0.065714947879314423, 0.066573545336723328, - 0.067373812198638916, 0.068112112581729889, 0.068784743547439575, 0.069387979805469513, - 0.069918066263198853, 0.070371203124523163, 0.070743560791015625, 0.071031264960765839, - 0.071230456233024597, 0.071337237954139709, 0.071347743272781372, 0.071258097887039185, - 0.07106444239616394, 0.070762887597084045, 0.070349536836147308, 0.069820456206798553, - 0.069171726703643799, 0.068399444222450256, 0.067499779164791107, 0.066468983888626099, - 0.065303429961204529, 0.063999593257904053, 0.062554046511650085, 0.060963429510593414, - 0.059224434196949005, 0.057333782315254211, 0.055288247764110565, 0.05308464914560318, - 0.050719890743494034, 0.04819098487496376, 0.045495055615901947, 0.042629346251487732, - 0.039591230452060699, 0.036378197371959686, 0.032987859100103378, 0.029417969286441803, - 0.025666400790214539, 0.0217311792075634, 0.017610486596822739, 0.013302664272487164, - 0.0088062174618244171, 0.0041198157705366611, -0.00075770384864881635, -0.0058273370377719402, - -0.011089906096458435, -0.016546055674552917, -0.022196246311068535, -0.028040755540132523, - -0.034079667180776596, -0.040312871336936951, -0.046740073710680008, -0.053360763937234879, - -0.06017424538731575, -0.0671796053647995, -0.074375726282596588, -0.081761270761489868, - -0.089334696531295776, -0.097094230353832245, -0.10503791272640228, -0.1131635457277298, - -0.12146873027086258, -0.12995083630084991, -0.13860704004764557, -0.1474342942237854, - -0.15642932057380676, -0.1655886322259903, -0.17490856349468231, -0.18438516557216644, - -0.19401434063911438, -0.20379173755645752, -0.21371282637119293, -0.22377283871173859, - -0.23396681249141693, -0.24428960680961609, -0.25473582744598389, -0.26529994606971741, - -0.27597621083259583, -0.2867586612701416, -0.29764124751091003, -0.30861768126487732, - -0.31968152523040771, -0.33082622289657593, -0.34204500913619995, -0.35333094000816345, - 0.36467701196670532, 0.37607598304748535, 0.38752046227455139, 0.39900293946266174, - 0.41051584482192993, 0.42205137014389038, 0.4336017370223999, 0.44515895843505859, - 0.45671501755714417, 0.46826183795928955, 0.47979119420051575, 0.49129492044448853, - 0.50276464223861694, 0.51419198513031006, 0.52556860446929932, 0.53688603639602661, - 0.54813587665557861, 0.55930960178375244, 0.57039880752563477, 0.58139497041702271, - 0.59228962659835815, 0.60307443141937256, 0.61374092102050781, 0.62428075075149536, - 0.63468557596206665, 0.64494723081588745, 0.65505754947662354, 0.66500836610794067, - 0.67479169368743896, 0.68439966440200806, 0.69382447004318237, 0.70305842161178589, - 0.71209394931793213, 0.72092366218566895, 0.72954028844833374, 0.73793661594390869, - 0.74610573053359985, 0.75404083728790283, 0.76173526048660278, 0.76918256282806396, - 0.77637648582458496, 0.78331100940704346, 0.78998017311096191, 0.79637837409973145, - 0.80250018835067749, 0.80834043025970459, 0.81389403343200684, 0.81915634870529175, - 0.82412278652191162, 0.82878917455673218, 0.83315145969390869, 0.83720588684082031, - 0.84094899892807007, 0.84437751770019531, 0.84748858213424683, 0.85027939081192017, - 0.85274755954742432, 0.85489106178283691, 0.85670793056488037, 0.8581966757774353, - 0.85935592651367188, 0.86018466949462891, 0.86068224906921387, 0.86084812879562378, - 0.86068224906921387, 0.86018466949462891, 0.85935592651367188, 0.8581966757774353, - 0.85670793056488037, 0.85489106178283691, 0.85274755954742432, 0.85027939081192017, - 0.84748858213424683, 0.84437751770019531, 0.84094899892807007, 0.83720588684082031, - 0.83315145969390869, 0.82878917455673218, 0.82412278652191162, 0.81915634870529175, - 0.81389403343200684, 0.80834043025970459, 0.80250018835067749, 0.79637837409973145, - 0.78998017311096191, 0.78331100940704346, 0.77637648582458496, 0.76918256282806396, - 0.76173526048660278, 0.75404083728790283, 0.74610573053359985, 0.73793661594390869, - 0.72954028844833374, 0.72092366218566895, 0.71209394931793213, 0.70305842161178589, - 0.69382447004318237, 0.68439966440200806, 0.67479169368743896, 0.66500836610794067, - 0.65505754947662354, 0.64494723081588745, 0.63468557596206665, 0.62428075075149536, - 0.61374092102050781, 0.60307443141937256, 0.59228962659835815, 0.58139497041702271, - 0.57039880752563477, 0.55930960178375244, 0.54813587665557861, 0.53688603639602661, - 0.52556860446929932, 0.51419198513031006, 0.50276464223861694, 0.49129492044448853, - 0.47979119420051575, 0.46826183795928955, 0.45671501755714417, 0.44515895843505859, - 0.4336017370223999, 0.42205137014389038, 0.41051584482192993, 0.39900293946266174, - 0.38752046227455139, 0.37607598304748535, 0.36467701196670532, 0.35333094000816345, - -0.34204500913619995, -0.33082622289657593, -0.31968152523040771, -0.30861768126487732, - -0.29764124751091003, -0.2867586612701416, -0.27597621083259583, -0.26529994606971741, - -0.25473582744598389, -0.24428960680961609, -0.23396681249141693, -0.22377283871173859, - -0.21371282637119293, -0.20379173755645752, -0.19401434063911438, -0.18438516557216644, - -0.17490856349468231, -0.1655886322259903, -0.15642932057380676, -0.1474342942237854, - -0.13860704004764557, -0.12995083630084991, -0.12146873027086258, -0.1131635457277298, - -0.10503791272640228, -0.097094230353832245, -0.089334696531295776, -0.081761270761489868, - -0.074375726282596588, -0.0671796053647995, -0.06017424538731575, -0.053360763937234879, - -0.046740073710680008, -0.040312871336936951, -0.034079667180776596, -0.028040755540132523, - -0.022196246311068535, -0.016546055674552917, -0.011089906096458435, -0.0058273370377719402, - -0.00075770384864881635, 0.0041198157705366611, 0.0088062174618244171, 0.013302664272487164, - 0.017610486596822739, 0.0217311792075634, 0.025666400790214539, 0.029417969286441803, - 0.032987859100103378, 0.036378197371959686, 0.039591230452060699, 0.042629346251487732, - 0.045495055615901947, 0.04819098487496376, 0.050719890743494034, 0.05308464914560318, - 0.055288247764110565, 0.057333782315254211, 0.059224434196949005, 0.060963429510593414, - 0.062554046511650085, 0.063999593257904053, 0.065303429961204529, 0.066468983888626099, - 0.067499779164791107, 0.068399444222450256, 0.069171726703643799, 0.069820456206798553, - 0.070349536836147308, 0.070762887597084045, 0.07106444239616394, 0.071258097887039185, - 0.071347743272781372, 0.071337237954139709, 0.071230456233024597, 0.071031264960765839, - 0.070743560791015625, 0.070371203124523163, 0.069918066263198853, 0.069387979805469513, - 0.068784743547439575, 0.068112112581729889, 0.067373812198638916, 0.066573545336723328, - 0.065714947879314423, 0.064801648259162903, 0.063837200403213501, 0.062825113534927368, - 0.061768818646669388, 0.06067170575261116, 0.059537097811698914, 0.058368254452943802, - 0.057168368250131607, 0.055940557271242142, 0.054687850177288055, 0.053413204848766327, - 0.05211947113275528, 0.050809424370527267, 0.049485750496387482, 0.048151064664125443, - 0.046807888895273209, 0.045458663254976273, 0.044105727225542068, 0.042751342058181763, - 0.041397668421268463, 0.040046781301498413, 0.03870067372918129, 0.037361271679401398, - 0.036030396819114685, 0.034709792584180832, 0.033401083201169968, 0.032105788588523865, - 0.030825328081846237, 0.029561035335063934, 0.028314167633652687, 0.027085918933153152, - 0.025877414271235466, 0.024689681828022003, 0.02352365106344223, 0.022380130365490913, - 0.021259821951389313, 0.020163353532552719, 0.019091326743364334, 0.018044359982013702, - 0.017023105174303055, 0.016028247773647308, 0.015060451813042164, 0.014120301231741905, - -0.013208229094743729, -0.012324465438723564, -0.011469035409390926, -0.010641784407198429, - -0.0098424339666962624, -0.0090706516057252884, -0.008326113224029541, -0.0076085370965301991, - -0.0069176913239061832, -0.0062533821910619736, -0.0056154225021600723, -0.0050036045722663403, - -0.0044176783412694931, -0.003857344388961792, -0.0033222525380551815, -0.002812013728544116, - -0.0023262077011168003, -0.0018643926596269011, -0.001426108181476593, -0.0010108760325238109, - -0.00061819725669920444, -0.00024754938203841448, 0.00010161137470277026, 0.00042984966421499848, - 0.00073774566408246756, 0.0010258855763822794, 0.001294855959713459, 0.0015452421503141522, - 0.0017776311142370105, 0.0019926181994378567, 0.0021908141206949949, 0.0023728485684841871, - 0.0025393660180270672, 0.0026910160668194294, 0.0028284420259296894, 0.0029522709082812071, - 0.0030631136614829302, 0.0031615688931196928, 0.0032482317183166742, 0.0033236993476748466, - 0.0033885682933032513, 0.003443423192948103, 0.0034888240043073893, 0.0035253004170954227, - 0.0035533567424863577, 0.0035734928678721189, 0.0035862282384186983, 0.0035921167582273483, - 0.0035917432978749275, 0.0035856980830430984, 0.0035745392087846994, 0.0035587677266448736, - 0.0035388274118304253, 0.0035151413176208735, 0.0034881711471825838, 0.0034584659151732922, - 0.0034266682341694832, 0.0033934540115296841, 0.003359407652169466, 0.0033248732797801495, - 0.0032898378558456898, 0.0032539025414735079, 0.0032163741998374462, 0.0031764607410877943, - 0.0031335193198174238, 0.0030872693751007318, 0.0030379060190171003, 0.0029860674403607845, - 0.0029326770454645157, 0.0028787164483219385, 0.0028250094037503004, 0.0027720888610929251, - 0.002720177173614502, 0.0026692659594118595, 0.0026192441582679749, 0.0025700139813125134, - 0.0025215502828359604, 0.0024738919455558062, 0.0024270866997539997, 0.0023811329156160355, - 0.0023359460756182671, 0.0022913739085197449, 0.0022472396958619356, 0.0022033930290490389, - 0.0021597379818558693, 0.0021162291523069143, 0.0020728406962007284, 0.0020295341964811087, - 0.0019862416666001081, 0.0019428766099736094, 0.0018993609119206667, 0.0018556505674496293, - 0.0018117419676855206, 0.0017676511779427528, 0.0017233812250196934, 0.0016788924112915993, - 0.0016340982401743531, 0.0015888890484347939, 0.0015431707724928856, 0.0014968989416956902, - 0.0014500867109745741, 0.0014027846045792103, 0.0013550462899729609, 0.0013069023843854666, - 0.0012583627831190825, 0.0012094489065930247, 0.0011602368904277682, 0.0011108826147392392, - 0.0010616052895784378, 0.0010126305278390646, 0.00096411682898178697, 0.00091610715026035905, - 0.00086853635730221868, 0.00082129903603345156, 0.00077434181002900004, 0.00072772568091750145, - 0.00068160606315359473, 0.0006361178238876164, 0.00059120741207152605, 0.00054649583762511611, - 0.00050126801943406463, 0.00045464080176316202, 0.0004058915947098285, 0.00035484600812196732, - 0.00030217695166356862, 0.00024947625934146345, 0.00019903187057934701, 0, -}; - -inline constexpr double kQmf5Window[640] = { - 0, 0.00019903187057934701, 0.00024947625934146345, 0.00030217695166356862, - 0.00035484600812196732, 0.0004058915947098285, 0.00045464080176316202, 0.00050126801943406463, - 0.00054649583762511611, 0.00059120741207152605, 0.0006361178238876164, 0.00068160606315359473, - 0.00072772568091750145, 0.00077434181002900004, 0.00082129903603345156, 0.00086853635730221868, - 0.00091610715026035905, 0.00096411682898178697, 0.0010126305278390646, 0.0010616052895784378, - 0.0011108826147392392, 0.0011602368904277682, 0.0012094489065930247, 0.0012583627831190825, - 0.0013069023843854666, 0.0013550462899729609, 0.0014027846045792103, 0.0014500867109745741, - 0.0014968989416956902, 0.0015431707724928856, 0.0015888890484347939, 0.0016340982401743531, - 0.0016788924112915993, 0.0017233812250196934, 0.0017676511779427528, 0.0018117419676855206, - 0.0018556505674496293, 0.0018993609119206667, 0.0019428766099736094, 0.0019862416666001081, - 0.0020295341964811087, 0.0020728406962007284, 0.0021162291523069143, 0.0021597379818558693, - 0.0022033930290490389, 0.0022472396958619356, 0.0022913739085197449, 0.0023359460756182671, - 0.0023811329156160355, 0.0024270866997539997, 0.0024738919455558062, 0.0025215502828359604, - 0.0025700139813125134, 0.0026192441582679749, 0.0026692659594118595, 0.002720177173614502, - 0.0027720888610929251, 0.0028250094037503004, 0.0028787164483219385, 0.0029326770454645157, - 0.0029860674403607845, 0.0030379060190171003, 0.0030872693751007318, 0.0031335193198174238, - 0.0031764607410877943, 0.0032163741998374462, 0.0032539025414735079, 0.0032898378558456898, - 0.0033248732797801495, 0.003359407652169466, 0.0033934540115296841, 0.0034266682341694832, - 0.0034584659151732922, 0.0034881711471825838, 0.0035151413176208735, 0.0035388274118304253, - 0.0035587677266448736, 0.0035745392087846994, 0.0035856980830430984, 0.0035917432978749275, - 0.0035921167582273483, 0.0035862282384186983, 0.0035734928678721189, 0.0035533567424863577, - 0.0035253004170954227, 0.0034888240043073893, 0.003443423192948103, 0.0033885682933032513, - 0.0033236993476748466, 0.0032482317183166742, 0.0031615688931196928, 0.0030631136614829302, - 0.0029522709082812071, 0.0028284420259296894, 0.0026910160668194294, 0.0025393660180270672, - 0.0023728485684841871, 0.0021908141206949949, 0.0019926181994378567, 0.0017776311142370105, - 0.0015452421503141522, 0.001294855959713459, 0.0010258855763822794, 0.00073774566408246756, - 0.00042984966421499848, 0.00010161137470277026, -0.00024754938203841448, -0.00061819725669920444, - -0.0010108760325238109, -0.001426108181476593, -0.0018643926596269011, -0.0023262077011168003, - -0.002812013728544116, -0.0033222525380551815, -0.003857344388961792, -0.0044176783412694931, - -0.0050036045722663403, -0.0056154225021600723, -0.0062533821910619736, -0.0069176913239061832, - -0.0076085370965301991, -0.008326113224029541, -0.0090706516057252884, -0.0098424339666962624, - -0.010641784407198429, -0.011469035409390926, -0.012324465438723564, -0.013208229094743729, - 0.014120301231741905, 0.015060451813042164, 0.016028247773647308, 0.017023105174303055, - 0.018044359982013702, 0.019091326743364334, 0.020163353532552719, 0.021259821951389313, - 0.022380130365490913, 0.02352365106344223, 0.024689681828022003, 0.025877414271235466, - 0.027085918933153152, 0.028314167633652687, 0.029561035335063934, 0.030825328081846237, - 0.032105788588523865, 0.033401083201169968, 0.034709792584180832, 0.036030396819114685, - 0.037361271679401398, 0.03870067372918129, 0.040046781301498413, 0.041397668421268463, - 0.042751342058181763, 0.044105727225542068, 0.045458663254976273, 0.046807888895273209, - 0.048151064664125443, 0.049485750496387482, 0.050809424370527267, 0.05211947113275528, - 0.053413204848766327, 0.054687850177288055, 0.055940557271242142, 0.057168368250131607, - 0.058368254452943802, 0.059537097811698914, 0.06067170575261116, 0.061768818646669388, - 0.062825113534927368, 0.063837200403213501, 0.064801648259162903, 0.065714947879314423, - 0.066573545336723328, 0.067373812198638916, 0.068112112581729889, 0.068784743547439575, - 0.069387979805469513, 0.069918066263198853, 0.070371203124523163, 0.070743560791015625, - 0.071031264960765839, 0.071230456233024597, 0.071337237954139709, 0.071347743272781372, - 0.071258097887039185, 0.07106444239616394, 0.070762887597084045, 0.070349536836147308, - 0.069820456206798553, 0.069171726703643799, 0.068399444222450256, 0.067499779164791107, - 0.066468983888626099, 0.065303429961204529, 0.063999593257904053, 0.062554046511650085, - 0.060963429510593414, 0.059224434196949005, 0.057333782315254211, 0.055288247764110565, - 0.05308464914560318, 0.050719890743494034, 0.04819098487496376, 0.045495055615901947, - 0.042629346251487732, 0.039591230452060699, 0.036378197371959686, 0.032987859100103378, - 0.029417969286441803, 0.025666400790214539, 0.0217311792075634, 0.017610486596822739, - 0.013302664272487164, 0.0088062174618244171, 0.0041198157705366611, -0.00075770384864881635, - -0.0058273370377719402, -0.011089906096458435, -0.016546055674552917, -0.022196246311068535, - -0.028040755540132523, -0.034079667180776596, -0.040312871336936951, -0.046740073710680008, - -0.053360763937234879, -0.06017424538731575, -0.0671796053647995, -0.074375726282596588, - -0.081761270761489868, -0.089334696531295776, -0.097094230353832245, -0.10503791272640228, - -0.1131635457277298, -0.12146873027086258, -0.12995083630084991, -0.13860704004764557, - -0.1474342942237854, -0.15642932057380676, -0.1655886322259903, -0.17490856349468231, - -0.18438516557216644, -0.19401434063911438, -0.20379173755645752, -0.21371282637119293, - -0.22377283871173859, -0.23396681249141693, -0.24428960680961609, -0.25473582744598389, - -0.26529994606971741, -0.27597621083259583, -0.2867586612701416, -0.29764124751091003, - -0.30861768126487732, -0.31968152523040771, -0.33082622289657593, -0.34204500913619995, - 0.35333094000816345, 0.36467701196670532, 0.37607598304748535, 0.38752046227455139, - 0.39900293946266174, 0.41051584482192993, 0.42205137014389038, 0.4336017370223999, - 0.44515895843505859, 0.45671501755714417, 0.46826183795928955, 0.47979119420051575, - 0.49129492044448853, 0.50276464223861694, 0.51419198513031006, 0.52556860446929932, - 0.53688603639602661, 0.54813587665557861, 0.55930960178375244, 0.57039880752563477, - 0.58139497041702271, 0.59228962659835815, 0.60307443141937256, 0.61374092102050781, - 0.62428075075149536, 0.63468557596206665, 0.64494723081588745, 0.65505754947662354, - 0.66500836610794067, 0.67479169368743896, 0.68439966440200806, 0.69382447004318237, - 0.70305842161178589, 0.71209394931793213, 0.72092366218566895, 0.72954028844833374, - 0.73793661594390869, 0.74610573053359985, 0.75404083728790283, 0.76173526048660278, - 0.76918256282806396, 0.77637648582458496, 0.78331100940704346, 0.78998017311096191, - 0.79637837409973145, 0.80250018835067749, 0.80834043025970459, 0.81389403343200684, - 0.81915634870529175, 0.82412278652191162, 0.82878917455673218, 0.83315145969390869, - 0.83720588684082031, 0.84094899892807007, 0.84437751770019531, 0.84748858213424683, - 0.85027939081192017, 0.85274755954742432, 0.85489106178283691, 0.85670793056488037, - 0.8581966757774353, 0.85935592651367188, 0.86018466949462891, 0.86068224906921387, - 0.86084812879562378, 0.86068224906921387, 0.86018466949462891, 0.85935592651367188, - 0.8581966757774353, 0.85670793056488037, 0.85489106178283691, 0.85274755954742432, - 0.85027939081192017, 0.84748858213424683, 0.84437751770019531, 0.84094899892807007, - 0.83720588684082031, 0.83315145969390869, 0.82878917455673218, 0.82412278652191162, - 0.81915634870529175, 0.81389403343200684, 0.80834043025970459, 0.80250018835067749, - 0.79637837409973145, 0.78998017311096191, 0.78331100940704346, 0.77637648582458496, - 0.76918256282806396, 0.76173526048660278, 0.75404083728790283, 0.74610573053359985, - 0.73793661594390869, 0.72954028844833374, 0.72092366218566895, 0.71209394931793213, - 0.70305842161178589, 0.69382447004318237, 0.68439966440200806, 0.67479169368743896, - 0.66500836610794067, 0.65505754947662354, 0.64494723081588745, 0.63468557596206665, - 0.62428075075149536, 0.61374092102050781, 0.60307443141937256, 0.59228962659835815, - 0.58139497041702271, 0.57039880752563477, 0.55930960178375244, 0.54813587665557861, - 0.53688603639602661, 0.52556860446929932, 0.51419198513031006, 0.50276464223861694, - 0.49129492044448853, 0.47979119420051575, 0.46826183795928955, 0.45671501755714417, - 0.44515895843505859, 0.4336017370223999, 0.42205137014389038, 0.41051584482192993, - 0.39900293946266174, 0.38752046227455139, 0.37607598304748535, 0.36467701196670532, - -0.35333094000816345, -0.34204500913619995, -0.33082622289657593, -0.31968152523040771, - -0.30861768126487732, -0.29764124751091003, -0.2867586612701416, -0.27597621083259583, - -0.26529994606971741, -0.25473582744598389, -0.24428960680961609, -0.23396681249141693, - -0.22377283871173859, -0.21371282637119293, -0.20379173755645752, -0.19401434063911438, - -0.18438516557216644, -0.17490856349468231, -0.1655886322259903, -0.15642932057380676, - -0.1474342942237854, -0.13860704004764557, -0.12995083630084991, -0.12146873027086258, - -0.1131635457277298, -0.10503791272640228, -0.097094230353832245, -0.089334696531295776, - -0.081761270761489868, -0.074375726282596588, -0.0671796053647995, -0.06017424538731575, - -0.053360763937234879, -0.046740073710680008, -0.040312871336936951, -0.034079667180776596, - -0.028040755540132523, -0.022196246311068535, -0.016546055674552917, -0.011089906096458435, - -0.0058273370377719402, -0.00075770384864881635, 0.0041198157705366611, 0.0088062174618244171, - 0.013302664272487164, 0.017610486596822739, 0.0217311792075634, 0.025666400790214539, - 0.029417969286441803, 0.032987859100103378, 0.036378197371959686, 0.039591230452060699, - 0.042629346251487732, 0.045495055615901947, 0.04819098487496376, 0.050719890743494034, - 0.05308464914560318, 0.055288247764110565, 0.057333782315254211, 0.059224434196949005, - 0.060963429510593414, 0.062554046511650085, 0.063999593257904053, 0.065303429961204529, - 0.066468983888626099, 0.067499779164791107, 0.068399444222450256, 0.069171726703643799, - 0.069820456206798553, 0.070349536836147308, 0.070762887597084045, 0.07106444239616394, - 0.071258097887039185, 0.071347743272781372, 0.071337237954139709, 0.071230456233024597, - 0.071031264960765839, 0.070743560791015625, 0.070371203124523163, 0.069918066263198853, - 0.069387979805469513, 0.068784743547439575, 0.068112112581729889, 0.067373812198638916, - 0.066573545336723328, 0.065714947879314423, 0.064801648259162903, 0.063837200403213501, - 0.062825113534927368, 0.061768818646669388, 0.06067170575261116, 0.059537097811698914, - 0.058368254452943802, 0.057168368250131607, 0.055940557271242142, 0.054687850177288055, - 0.053413204848766327, 0.05211947113275528, 0.050809424370527267, 0.049485750496387482, - 0.048151064664125443, 0.046807888895273209, 0.045458663254976273, 0.044105727225542068, - 0.042751342058181763, 0.041397668421268463, 0.040046781301498413, 0.03870067372918129, - 0.037361271679401398, 0.036030396819114685, 0.034709792584180832, 0.033401083201169968, - 0.032105788588523865, 0.030825328081846237, 0.029561035335063934, 0.028314167633652687, - 0.027085918933153152, 0.025877414271235466, 0.024689681828022003, 0.02352365106344223, - 0.022380130365490913, 0.021259821951389313, 0.020163353532552719, 0.019091326743364334, - 0.018044359982013702, 0.017023105174303055, 0.016028247773647308, 0.015060451813042164, - -0.014120301231741905, -0.013208229094743729, -0.012324465438723564, -0.011469035409390926, - -0.010641784407198429, -0.0098424339666962624, -0.0090706516057252884, -0.008326113224029541, - -0.0076085370965301991, -0.0069176913239061832, -0.0062533821910619736, -0.0056154225021600723, - -0.0050036045722663403, -0.0044176783412694931, -0.003857344388961792, -0.0033222525380551815, - -0.002812013728544116, -0.0023262077011168003, -0.0018643926596269011, -0.001426108181476593, - -0.0010108760325238109, -0.00061819725669920444, -0.00024754938203841448, 0.00010161137470277026, - 0.00042984966421499848, 0.00073774566408246756, 0.0010258855763822794, 0.001294855959713459, - 0.0015452421503141522, 0.0017776311142370105, 0.0019926181994378567, 0.0021908141206949949, - 0.0023728485684841871, 0.0025393660180270672, 0.0026910160668194294, 0.0028284420259296894, - 0.0029522709082812071, 0.0030631136614829302, 0.0031615688931196928, 0.0032482317183166742, - 0.0033236993476748466, 0.0033885682933032513, 0.003443423192948103, 0.0034888240043073893, - 0.0035253004170954227, 0.0035533567424863577, 0.0035734928678721189, 0.0035862282384186983, - 0.0035921167582273483, 0.0035917432978749275, 0.0035856980830430984, 0.0035745392087846994, - 0.0035587677266448736, 0.0035388274118304253, 0.0035151413176208735, 0.0034881711471825838, - 0.0034584659151732922, 0.0034266682341694832, 0.0033934540115296841, 0.003359407652169466, - 0.0033248732797801495, 0.0032898378558456898, 0.0032539025414735079, 0.0032163741998374462, - 0.0031764607410877943, 0.0031335193198174238, 0.0030872693751007318, 0.0030379060190171003, - 0.0029860674403607845, 0.0029326770454645157, 0.0028787164483219385, 0.0028250094037503004, - 0.0027720888610929251, 0.002720177173614502, 0.0026692659594118595, 0.0026192441582679749, - 0.0025700139813125134, 0.0025215502828359604, 0.0024738919455558062, 0.0024270866997539997, - 0.0023811329156160355, 0.0023359460756182671, 0.0022913739085197449, 0.0022472396958619356, - 0.0022033930290490389, 0.0021597379818558693, 0.0021162291523069143, 0.0020728406962007284, - 0.0020295341964811087, 0.0019862416666001081, 0.0019428766099736094, 0.0018993609119206667, - 0.0018556505674496293, 0.0018117419676855206, 0.0017676511779427528, 0.0017233812250196934, - 0.0016788924112915993, 0.0016340982401743531, 0.0015888890484347939, 0.0015431707724928856, - 0.0014968989416956902, 0.0014500867109745741, 0.0014027846045792103, 0.0013550462899729609, - 0.0013069023843854666, 0.0012583627831190825, 0.0012094489065930247, 0.0011602368904277682, - 0.0011108826147392392, 0.0010616052895784378, 0.0010126305278390646, 0.00096411682898178697, - 0.00091610715026035905, 0.00086853635730221868, 0.00082129903603345156, 0.00077434181002900004, - 0.00072772568091750145, 0.00068160606315359473, 0.0006361178238876164, 0.00059120741207152605, - 0.00054649583762511611, 0.00050126801943406463, 0.00045464080176316202, 0.0004058915947098285, - 0.00035484600812196732, 0.00030217695166356862, 0.00024947625934146345, 0.00019903187057934701, -}; - -inline constexpr uint8_t kPbMap1[64] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -}; - -inline constexpr uint8_t kPbMap3[64] = { - 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, -}; - -inline constexpr uint8_t kPbMap5[64] = { - 0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, - 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, -}; - -inline constexpr uint8_t kPbMap7[64] = { - 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, - 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, - 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, -}; - -inline constexpr uint8_t kPbMap9[64] = { - 0, 1, 2, 3, 3, 3, 4, 5, 5, 6, 6, 6, 7, 7, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, -}; - -inline constexpr uint8_t kPbMap12[64] = { - 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, - 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, - 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, - 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, -}; - -inline constexpr uint8_t kPbMap15[64] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 10, 11, 11, 11, - 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, - 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, -}; - -inline constexpr uint8_t kPbMap23[64] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 13, 13, - 14, 14, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 18, 19, 19, - 19, 19, 19, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, - 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, -}; -} // namespace ejoc::tables diff --git a/native/src/sofa_binaural_renderer.cpp b/native/src/sofa_binaural_renderer.cpp deleted file mode 100644 index cbf699b..0000000 --- a/native/src/sofa_binaural_renderer.cpp +++ /dev/null @@ -1,1231 +0,0 @@ -#define EJOC_BUILD_DLL -#include "eac3joc_core.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -namespace ejoc::sofa_binaural { - -constexpr double kPi = 3.141592653589793238462643383279502884; -constexpr int kChannels = EJOC_BINAURAL_INPUT_CHANNELS; -constexpr int kHop = 64; -constexpr int kQmf = EJOC_BINAURAL_QMF_BANDS; -constexpr int kHybrid = EJOC_BINAURAL_HYBRID_BANDS; -constexpr int kRank = 4; -constexpr int kLatency = 961; -constexpr int kOrder = 5; -constexpr int kTerms = (kOrder + 1) * (kOrder + 1); -constexpr int kEarlyHistory = 256; -constexpr int kTransitionSlots = 8; -constexpr double kSampleRate = 48000.0; -constexpr int kFftSize = 128; -constexpr int kMaxBlockSamples = 512; - -struct Complex { - double re; - double im; -}; - -inline Complex add(Complex a, Complex b) noexcept { - return {a.re + b.re, a.im + b.im}; -} - -inline Complex mul(Complex a, Complex b) noexcept { - return {a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re}; -} - -inline Complex mulr(Complex a, double b) noexcept { - return {a.re * b, a.im * b}; -} - -inline Complex addmul(Complex acc, Complex b, Complex c) noexcept { - return {acc.re + b.re * c.re - b.im * c.im, acc.im + b.re * c.im + b.im * c.re}; -} - -// --------------------------------------------------------------------------- -// 128-point FFT, forward: X[k] = sum_p x[p] exp(-2j pi k p / N) (radix-2 DIT) -// --------------------------------------------------------------------------- -class Fft128 final { -public: - Fft128() noexcept { - for (int k = 0; k < kFftSize; ++k) { - const double angle = -2.0 * kPi * k / kFftSize; - twiddle_[k] = {std::cos(angle), std::sin(angle)}; - } - } - - void forward(const Complex* input, Complex* output) const noexcept { - // bit-reversal permutation for the 7-bit index (DIT) - for (int index = 0; index < kFftSize; ++index) { - unsigned int reversed = 0; - for (int bit = 0; bit < 7; ++bit) { - reversed = (reversed << 1) - | ((static_cast(index) >> bit) & 1u); - } - output[static_cast(reversed)] = input[index]; - } - for (int size = 2; size <= kFftSize; size <<= 1) { - const int half = size >> 1; - const int step = kFftSize / size; - for (int base = 0; base < kFftSize; base += size) { - for (int offset = 0; offset < half; ++offset) { - const Complex w = twiddle_[offset * step]; - const Complex even = output[base + offset]; - const Complex odd = mul(output[base + offset + half], w); - output[base + offset] = add(even, odd); - output[base + offset + half] = { - even.re - odd.re, even.im - odd.im}; - } - } - } - } - -private: - Complex twiddle_[kFftSize]; -}; - -// --------------------------------------------------------------------------- -// QMF analysis (mirrors QmfAnalysis: joined history, lag 0..9, modulations) -// --------------------------------------------------------------------------- -class QmfAnalysis final { -public: - void configure(const double* coefficients) noexcept { - std::memcpy(coeff_, coefficients, sizeof(coeff_)); - } - - void reset() noexcept { - std::memset(history_, 0, sizeof(history_)); - } - - // input: interleaved [samples][16]; output: [slots][16][64] complex - void process(const double* input, int slots, Complex* output) noexcept { - // joined[9 + slots][16][64] - for (int lag = 0; lag < 9; ++lag) { - std::memcpy(joined_[lag], history_[lag], sizeof(joined_[0])); - } - for (int slot = 0; slot < slots; ++slot) { - for (int channel = 0; channel < kChannels; ++channel) { - for (int p = 0; p < 64; ++p) { - joined_[9 + slot][channel][p] = - input[(static_cast(slot) * 64 + p) * kChannels - + channel]; - } - } - } - for (int slot = 0; slot < slots; ++slot) { - for (int channel = 0; channel < kChannels; ++channel) { - Complex even[kFftSize]; - Complex odd[kFftSize]; - for (int p = 0; p < kFftSize; ++p) { - even[p] = {0.0, 0.0}; - odd[p] = {0.0, 0.0}; - } - // 64 polyphase positions; the 128-point FFT zero-pads the rest - for (int p = 0; p < kQmf; ++p) { - double even_re = 0.0; - double odd_re = 0.0; - for (int lag = 0; lag < 5; ++lag) { - even_re += joined_[9 + slot - 2 * lag][channel][p] - * coeff_[p][2 * lag]; - } - for (int lag = 0; lag < 5; ++lag) { - odd_re += joined_[9 + slot - (2 * lag + 1)][channel][p] - * coeff_[p][2 * lag + 1]; - } - const double pre_angle = -kPi * p / kFftSize; - even[p] = {even_re * std::cos(pre_angle), - even_re * std::sin(pre_angle)}; - odd[p] = {odd_re * std::cos(pre_angle), odd_re * std::sin(pre_angle)}; - } - Complex even_fft[kFftSize]; - Complex odd_fft[kFftSize]; - fft_.forward(even, even_fft); - fft_.forward(odd, odd_fft); - Complex* band = output - + (static_cast(slot) * kChannels + channel) * kQmf; - for (int b = 0; b < kQmf; ++b) { - const double post_angle = -3.0 * (b + 0.5) * kPi / kFftSize; - const Complex post = {std::cos(post_angle), std::sin(post_angle)}; - const Complex even_post = {0.0, (b % 2 == 0) ? 1.0 : -1.0}; - // python: (odd_fft + even_fft * even_post) * post - band[b] = mul(post, add(odd_fft[b], - mul(even_fft[b], even_post))); - } - } - } - for (int lag = 0; lag < 9; ++lag) { - std::memcpy(history_[lag], joined_[slots + lag], sizeof(history_[0])); - } - } - -private: - double coeff_[kQmf][10]; - double history_[9][kChannels][64]; - double joined_[9 + kMaxBlockSamples / kHop][kChannels][64]; - Fft128 fft_; -}; - -// --------------------------------------------------------------------------- -// Hybrid analysis (mirrors HybridAnalysis: 13-tap low join, 6-slot high join) -// --------------------------------------------------------------------------- -class HybridAnalysis final { -public: - void configure(const double* low_kernel) noexcept { - std::memcpy(low_, low_kernel, sizeof(low_)); - } - - void reset() noexcept { - std::memset(history_, 0, sizeof(history_)); - std::memset(high_history_, 0, sizeof(high_history_)); - } - - // qmf: [slots][16][64] complex; output: [slots][16][77] complex - void process(const Complex* qmf, int slots, Complex* output) noexcept { - for (int lag = 0; lag < 12; ++lag) { - std::memcpy(low_joined_[lag], history_[lag], sizeof(low_joined_[0])); - } - for (int lag = 0; lag < 6; ++lag) { - std::memcpy(high_joined_[lag], high_history_[lag], - sizeof(high_joined_[0])); - } - for (int slot = 0; slot < slots; ++slot) { - for (int channel = 0; channel < kChannels; ++channel) { - const Complex* band = qmf - + (static_cast(slot) * kChannels + channel) * kQmf; - for (int parent = 0; parent < 3; ++parent) { - low_joined_[12 + slot][channel][parent][0] = band[parent].re; - low_joined_[12 + slot][channel][parent][1] = band[parent].im; - } - for (int b = 0; b < 61; ++b) { - high_joined_[6 + slot][channel][b] = band[3 + b]; - } - } - } - for (int slot = 0; slot < slots; ++slot) { - for (int channel = 0; channel < kChannels; ++channel) { - Complex* out = output - + (static_cast(slot) * kChannels + channel) * kHybrid; - for (int hb = 0; hb < 16; ++hb) { - Complex value = {0.0, 0.0}; - for (int parent = 0; parent < 3; ++parent) { - for (int comp = 0; comp < 2; ++comp) { - for (int lag = 0; lag < 13; ++lag) { - const double source = - low_joined_[12 + slot - lag][channel][parent][comp]; - value.re += source * low_[parent][comp][lag][hb][0]; - value.im += source * low_[parent][comp][lag][hb][1]; - } - } - } - out[hb] = value; - } - for (int b = 0; b < 61; ++b) { - out[16 + b] = high_joined_[slot][channel][b]; - } - } - } - for (int lag = 0; lag < 12; ++lag) { - std::memcpy(history_[lag], low_joined_[slots + lag], sizeof(history_[0])); - } - for (int lag = 0; lag < 6; ++lag) { - std::memcpy(high_history_[lag], high_joined_[slots + lag], - sizeof(high_history_[0])); - } - } - -private: - double low_[3][2][13][16][2]; - double history_[12][kChannels][3][2]; - double low_joined_[12 + kMaxBlockSamples / kHop][kChannels][3][2]; - Complex high_history_[6][kChannels][61]; - Complex high_joined_[6 + kMaxBlockSamples / kHop][kChannels][61]; -}; - -// --------------------------------------------------------------------------- -// Hybrid synthesis sparse map (mirrors HybridSynthesis) -// --------------------------------------------------------------------------- -struct SynthesisEntry { - int in_band; - int in_comp; - int out_band; - int out_comp; - double gain; -}; - -class HybridSynthesis final { -public: - void configure(const int16_t* indices, const double* values, - uint32_t count) noexcept { - entries_.clear(); - for (uint32_t i = 0; i < count; ++i) { - entries_.push_back({static_cast(indices[i * 4]), - static_cast(indices[i * 4 + 1]), - static_cast(indices[i * 4 + 2]), - static_cast(indices[i * 4 + 3]), - values[i]}); - } - } - - // hybrid: [slots][2][77]; output: [slots][2][64] - void process(const Complex* hybrid, int slots, Complex* output) noexcept { - for (int slot = 0; slot < slots; ++slot) { - for (int channel = 0; channel < 2; ++channel) { - const Complex* source = hybrid - + (static_cast(slot) * 2 + channel) * kHybrid; - Complex* target = output - + (static_cast(slot) * 2 + channel) * kQmf; - for (int b = 0; b < kQmf; ++b) { - target[b] = {0.0, 0.0}; - } - for (const auto& entry : entries_) { - const Complex value = source[entry.in_band]; - const double component = - (entry.in_comp == 0) ? value.re : value.im; - if (entry.out_comp == 0) { - target[entry.out_band].re += component * entry.gain; - } else { - target[entry.out_band].im += component * entry.gain; - } - } - } - } - } - -private: - std::vector entries_; -}; - -// --------------------------------------------------------------------------- -// QMF synthesis, rank-4 (mirrors QmfSynthesis: joined history, lag 0..9) -// --------------------------------------------------------------------------- -class QmfSynthesis final { -public: - void configure(const double* basis, const double* taps) noexcept { - std::memcpy(basis_, basis, sizeof(basis_)); - std::memcpy(taps_, taps, sizeof(taps_)); - } - - void reset() noexcept { - std::memset(history_, 0, sizeof(history_)); - } - - // qmf: [slots][2][64]; output: [slots][2][64] real - void process(const Complex* qmf, int slots, double* output) noexcept { - for (int lag = 0; lag < 9; ++lag) { - std::memcpy(joined_[lag], history_[lag], sizeof(joined_[0])); - } - for (int slot = 0; slot < slots; ++slot) { - for (int channel = 0; channel < 2; ++channel) { - const Complex* values = qmf - + (static_cast(slot) * 2 + channel) * kQmf; - double flat[kFftSize]; - for (int b = 0; b < kQmf; ++b) { - flat[2 * b] = values[b].re; - flat[2 * b + 1] = values[b].im; - } - for (int b = 0; b < kQmf; ++b) { - for (int r = 0; r < kRank; ++r) { - double value = 0.0; - for (int j = 0; j < kFftSize; ++j) { - value += flat[j] * basis_[b][r][j]; - } - joined_[9 + slot][channel][b][r] = value; - } - } - } - } - for (int slot = 0; slot < slots; ++slot) { - for (int channel = 0; channel < 2; ++channel) { - for (int b = 0; b < kQmf; ++b) { - double value = 0.0; - for (int lag = 0; lag < 10; ++lag) { - for (int r = 0; r < kRank; ++r) { - value += joined_[9 + slot - lag][channel][b][r] - * taps_[b][lag][r]; - } - } - output[(static_cast(slot) * 2 + channel) * 64 + b] - = value; - } - } - } - for (int lag = 0; lag < 9; ++lag) { - std::memcpy(history_[lag], joined_[slots + lag], sizeof(history_[0])); - } - } - -private: - double basis_[kQmf][kRank][kFftSize]; - double taps_[kQmf][10][kRank]; - double history_[9][2][kQmf][kRank]; - double joined_[9 + kMaxBlockSamples / kHop][2][kQmf][kRank]; -}; - -// --------------------------------------------------------------------------- -// Fifth-order ACN/N3D real spherical harmonics -// --------------------------------------------------------------------------- -class RealSh final { -public: - static void evaluate(const double* direction, double* basis) noexcept { - const double azimuth = std::atan2(direction[1], direction[0]); - const double x = std::max(-1.0, std::min(1.0, direction[2])); - int index = 0; - for (int degree = 0; degree <= kOrder; ++degree) { - for (int order = -degree; order <= degree; ++order) { - const int absolute = std::abs(order); - const double normalization = std::sqrt( - (2.0 * degree + 1.0) / (4.0 * kPi) - * factorial_ratio(degree, absolute)); - const double legendre = associated_legendre( - degree, absolute, x, pmm_value(absolute, x)); - if (order > 0) { - basis[index] = std::sqrt(2.0) * normalization * legendre - * std::cos(order * azimuth); - } else if (order < 0) { - basis[index] = std::sqrt(2.0) * normalization * legendre - * std::sin(absolute * azimuth); - } else { - basis[index] = normalization * legendre; - } - ++index; - } - } - } - -private: - static double factorial_ratio(int degree, int order) noexcept { - double ratio = 1.0; - for (int value = degree - order + 1; value <= degree + order; ++value) { - ratio /= static_cast(value); - } - return ratio; - } - - static double pmm_value(int m, double x) noexcept { - if (m == 0) { - return 1.0; - } - double double_factorial = 1.0; - for (int value = 1; value <= 2 * m - 1; value += 2) { - double_factorial *= static_cast(value); - } - double value = double_factorial - * std::pow(std::max(0.0, 1.0 - x * x), 0.5 * m); - if (m % 2 == 1) { - value = -value; - } - return value; - } - - static double associated_legendre(int degree, int order, double x, - double pmm) noexcept { - if (degree == order) { - return pmm; - } - double pm1 = pmm; - double pmm1 = (2.0 * order + 1.0) * x * pmm; - if (degree == order + 1) { - return pmm1; - } - double result = 0.0; - for (int l = order + 2; l <= degree; ++l) { - result = ((2.0 * l - 1.0) * x * pmm1 - - (l + order - 1.0) * pm1) / (l - order); - pm1 = pmm1; - pmm1 = result; - } - return result; - } -}; - -// --------------------------------------------------------------------------- -// Renderer -// --------------------------------------------------------------------------- -struct Path { - int delay_slots[2]; - Complex transfer[2][kHybrid]; -}; - -struct SourceState { - double position[3]; - int profile; - double gain; - int enabled; - int special_lfe; - std::vector current; - std::vector target; - int fade_position; - int fade_total; - double late_current; - double late_start; - double late_target; - int64_t late_fade_position; - int64_t late_fade_total; -}; - -constexpr double kDistanceM[3] = {1.00000465, 2.19327927, 6.40177584}; -constexpr double kMinimumDistance = 0.10; -constexpr double kCoupling = 0.01318359375; -constexpr double kRoomCalibration = 1.4; -constexpr double kLateSend[3] = {0.06, 0.16, 0.28}; -constexpr double kAir = 0.002; - -class Renderer final { -public: - Renderer() noexcept { - reset_state(); - } - - int configure_kernels(const double* qmf_analysis, const double* hybrid_low, - const int16_t* hybrid_indices, const double* hybrid_values, - uint32_t hybrid_count, const double* qmf_basis, - const double* qmf_taps) noexcept { - if (!qmf_analysis || !hybrid_low || !hybrid_indices || !hybrid_values - || !qmf_basis || !qmf_taps || hybrid_count == 0) { - return fail("invalid sofa binaural kernel configuration"); - } - analysis_.configure(qmf_analysis); - hybrid_analysis_.configure(hybrid_low); - hybrid_synthesis_.configure(hybrid_indices, hybrid_values, hybrid_count); - synthesis_.configure(qmf_basis, qmf_taps); - kernels_ready_ = true; - reset(); - return 0; - } - - int configure_field(const double* coefficients, const double* delay_coefficients, - const double* delay_bounds, const double* band_centers, - double measurement_radius_m) noexcept { - if (!coefficients || !delay_coefficients || !delay_bounds || !band_centers - || !std::isfinite(measurement_radius_m) || measurement_radius_m <= 0.0) { - return fail("invalid sofa binaural field configuration"); - } - for (int term = 0; term < kTerms; ++term) { - for (int ear = 0; ear < 2; ++ear) { - for (int band = 0; band < kHybrid; ++band) { - const size_t source = - ((static_cast(term) * 2 + ear) * kHybrid + band) * 2; - field_coeff_[term][ear][band] = { - coefficients[source], coefficients[source + 1]}; - } - delay_coeff_[term][ear] = - delay_coefficients[static_cast(term) * 2 + ear]; - } - } - std::memcpy(delay_bounds_, delay_bounds, sizeof(delay_bounds_)); - std::memcpy(centers_, band_centers, sizeof(centers_)); - measurement_radius_ = measurement_radius_m; - const double maximum = std::max(delay_bounds_[0][1], delay_bounds_[1][1]); - hrtf_slots_ = static_cast( - std::ceil(maximum / static_cast(kHop))); - field_ready_ = true; - reset(); - return 0; - } - - int configure_room(const double* dims, const double* listener, - const double* wall_gains, double speed, - const uint32_t* fdn_delays, const double* fdn_feedback, - double damping, double fdn_gain, - const uint32_t* allpass_delays, const double* allpass_gains, - uint32_t enable_early, uint32_t enable_late) noexcept { - if (!dims || !listener || !wall_gains || !fdn_delays || !fdn_feedback - || !allpass_delays || !allpass_gains || !(speed > 0.0) - || !(damping >= 0.0 && damping < 1.0)) { - return fail("invalid sofa binaural room configuration"); - } - std::memcpy(dims_, dims, sizeof(dims_)); - std::memcpy(listener_, listener, sizeof(listener_)); - std::memcpy(wall_gain_, wall_gains, sizeof(wall_gain_)); - speed_ = speed; - std::memcpy(fdn_delays_, fdn_delays, sizeof(fdn_delays_)); - std::memcpy(fdn_feedback_, fdn_feedback, sizeof(fdn_feedback_)); - damping_ = damping; - fdn_gain_ = fdn_gain; - std::memcpy(allpass_delays_, allpass_delays, sizeof(allpass_delays_)); - std::memcpy(allpass_gains_, allpass_gains, sizeof(allpass_gains_)); - enable_early_reflections_ = enable_early != 0; - enable_late_room_ = enable_late != 0; - for (int line = 0; line < 4; ++line) { - fdn_buffers_[line].assign(fdn_delays_[line], 0.0); - } - for (int line = 0; line < 2; ++line) { - allpass_buffers_[line].assign(allpass_delays_[line], 0.0); - } - room_ready_ = true; - reset(); - return 0; - } - - int reset() noexcept { - analysis_.reset(); - hybrid_analysis_.reset(); - synthesis_.reset(); - reset_state(); - return 0; - } - - const char* error() const noexcept { - return error_[0] ? error_ : ""; - } - - int set_source(uint32_t source, const double* position, uint32_t profile, - double gain, uint32_t enabled, uint32_t special_lfe, - uint32_t fade) noexcept { - if (!kernels_ready_ || !field_ready_ || !room_ready_) { - return fail("sofa binaural renderer is not configured"); - } - if (source >= kChannels || !position || profile > 2) { - return fail("invalid sofa binaural source update"); - } - SourceState& state = sources_[source]; - state.position[0] = position[0]; - state.position[1] = position[1]; - state.position[2] = position[2]; - state.profile = static_cast(profile); - state.gain = gain; - state.enabled = enabled != 0; - state.special_lfe = special_lfe != 0; - - const double effective = enabled ? gain : 0.0; - std::vector paths; - double late_send = 0.0; - double direction[3]; - double radius; - normalize_adm(position, direction, radius); - const double distance = - std::max(kMinimumDistance, radius * kDistanceM[state.profile]); - if (state.special_lfe) { - paths.push_back(lfe_path(effective)); - } else { - paths = ordinary_paths(direction, distance, state.profile, effective); - if (enabled && enable_late_room_) { - const double base = kLateSend[state.profile]; - const double radial = std::min( - std::max(std::sqrt(std::max(radius, 0.0)), 0.25), 1.5); - late_send = effective * kRoomCalibration * base * radial; - } - } - set_late_target(source, late_send, fade != 0); - - const int fade_slots = (fade && processed_slots_ > 0) ? kTransitionSlots : 0; - if (!state.target.empty()) { - state.current = state.target; - state.target.clear(); - } - if (processed_slots_ == 0 || fade_slots == 0) { - state.current = paths; - state.target.clear(); - state.fade_position = 0; - state.fade_total = 0; - } else { - state.target = paths; - state.fade_position = 0; - state.fade_total = fade_slots; - } - return 0; - } - - int process(const double* input, uint32_t sample_count, double output_gain, - double* output) noexcept { - if (!kernels_ready_ || !field_ready_ || !room_ready_) { - return fail("sofa binaural renderer is not configured"); - } - if (sample_count == 0 || sample_count > kMaxBlockSamples - || sample_count % kHop != 0 || !output) { - return fail("sofa binaural process requires 64-sample alignment"); - } - const int slots = static_cast(sample_count / kHop); - process_internal(input, slots, output_gain); - uint32_t skip = std::min(latency_to_discard_, sample_count); - latency_to_discard_ -= skip; - processed_input_samples_ += sample_count; - std::memcpy(output, output_.data() + static_cast(skip) * 2, - static_cast(sample_count - skip) * 2 * sizeof(double)); - return static_cast(sample_count - skip); - } - - int finish(uint32_t flush_samples, double* output, uint32_t capacity) noexcept { - if (!kernels_ready_ || !field_ready_ || !room_ready_) { - return fail("sofa binaural renderer is not configured"); - } - if (flush_samples == 0 || flush_samples > kMaxBlockSamples - || flush_samples % kHop != 0 || !output || capacity < flush_samples) { - return fail("invalid sofa binaural finish request"); - } - const int slots = static_cast(flush_samples / kHop); - process_internal(nullptr, slots, 1.0); - uint32_t skip = std::min(latency_to_discard_, flush_samples); - latency_to_discard_ -= skip; - processed_input_samples_ += flush_samples; - std::memcpy(output, output_.data() + static_cast(skip) * 2, - static_cast(flush_samples - skip) * 2 * sizeof(double)); - return static_cast(flush_samples - skip); - } - -private: - static void normalize_adm(const double* position, double* direction, - double& radius) noexcept { - const double r = std::sqrt(position[0] * position[0] - + position[1] * position[1] - + position[2] * position[2]); - radius = r; - if (r > 1.0e-15) { - direction[0] = position[0] / r; - direction[1] = position[1] / r; - direction[2] = position[2] / r; - } else { - direction[0] = 0.0; - direction[1] = 1.0; - direction[2] = 0.0; - } - } - - static double direct_level_gain(int profile, double distance) noexcept { - if (profile == 0) { - return 1.0; - } - return 1.0 / std::sqrt(1.0 + kCoupling * distance * distance); - } - - double inverse_distance_gain(double path_distance) const noexcept { - return measurement_radius_ / path_distance; - } - - Path make_path(const double* direction, double extra_delay, - double amplitude) noexcept { - double basis[kTerms]; - // ADM (+X right, +Y front, +Z up) -> SOFA listener (+X front, +Y left): - // (x, y, z)_sofa = (y_adm, -x_adm, z_adm) - const double listener_direction[3] = { - direction[1], -direction[0], direction[2]}; - RealSh::evaluate(listener_direction, basis); - Complex aligned[2][kHybrid]; - double delay[2]; - for (int ear = 0; ear < 2; ++ear) { - double delay_value = 0.0; - for (int band = 0; band < kHybrid; ++band) { - aligned[ear][band] = {0.0, 0.0}; - } - for (int term = 0; term < kTerms; ++term) { - delay_value += basis[term] * delay_coeff_[term][ear]; - for (int band = 0; band < kHybrid; ++band) { - aligned[ear][band].re += - field_coeff_[term][ear][band].re * basis[term]; - aligned[ear][band].im += - field_coeff_[term][ear][band].im * basis[term]; - } - } - delay[ear] = std::min(std::max(delay_value, delay_bounds_[ear][0]), - delay_bounds_[ear][1]); - } - Path path; - for (int ear = 0; ear < 2; ++ear) { - const double total = delay[ear] + extra_delay; - const double slots = std::floor(total / kHop); - path.delay_slots[ear] = static_cast(slots); - const double residual = total - slots * kHop; - for (int band = 0; band < kHybrid; ++band) { - // Python transfer = raw aligned gains x residual-delay phase x - // amplitude (the integer-slot part is the delay_slots history). - const double phase = -2.0 * kPi * centers_[band] - * residual / kSampleRate; - const Complex rotated = { - aligned[ear][band].re * std::cos(phase) - - aligned[ear][band].im * std::sin(phase), - aligned[ear][band].re * std::sin(phase) - + aligned[ear][band].im * std::cos(phase)}; - path.transfer[ear][band] = mulr(rotated, amplitude); - } - } - return path; - } - - Path lfe_path(double gain) noexcept { - Path path; - path.delay_slots[0] = 0; - path.delay_slots[1] = 0; - for (int band = 0; band < kHybrid; ++band) { - const double frequency = centers_[band]; - double lowpass = 1.0; - if (frequency >= 180.0) { - lowpass = 0.0; - } else if (frequency > 120.0) { - const double amount = (frequency - 120.0) / 60.0; - const double value = std::cos(0.5 * kPi * amount); - lowpass = value * value; - } - path.transfer[0][band] = {gain * lowpass / std::sqrt(2.0), 0.0}; - path.transfer[1][band] = {gain * lowpass / std::sqrt(2.0), 0.0}; - } - return path; - } - - std::vector ordinary_paths(const double* direction, double distance, - int profile, double gain) noexcept { - std::vector paths; - paths.push_back( - make_path(direction, 0.0, gain * direct_level_gain(profile, distance))); - if (enable_early_reflections_) { - double source[3] = { - listener_[0] + direction[0] * distance, - listener_[1] + direction[1] * distance, - listener_[2] + direction[2] * distance, - }; - for (int axis = 0; axis < 3; ++axis) { - for (int side = 0; side < 2; ++side) { - double image[3] = {source[0], source[1], source[2]}; - image[axis] = (side == 0) ? -image[axis] - : 2.0 * dims_[axis] - image[axis]; - double vector[3] = { - image[0] - listener_[0], - image[1] - listener_[1], - image[2] - listener_[2], - }; - const double path_distance = std::sqrt( - vector[0] * vector[0] + vector[1] * vector[1] - + vector[2] * vector[2]); - double path_direction[3] = { - vector[0] / path_distance, - vector[1] / path_distance, - vector[2] / path_distance, - }; - const double extra = std::max( - 0.0, (path_distance - distance) * kSampleRate / speed_); - const double air = - std::exp(-kAir * std::max(path_distance - distance, 0.0)); - const double amplitude = gain * kRoomCalibration - * wall_gain_[axis * 2 + side] * air - * inverse_distance_gain(path_distance); - paths.push_back(make_path(path_direction, extra, amplitude)); - maximum_early_delay_ = std::max(maximum_early_delay_, extra); - } - } - } - return paths; - } - - void set_late_target(int source, double value, int fade) noexcept { - const int64_t fade_samples = - (fade && processed_input_samples_ > 0) ? kTransitionSlots * kHop : 0; - SourceState& state = sources_[source]; - if (fade_samples == 0) { - state.late_current = value; - state.late_start = value; - state.late_target = value; - state.late_fade_position = 0; - state.late_fade_total = 0; - } else { - state.late_start = state.late_current; - state.late_target = value; - state.late_fade_position = 0; - state.late_fade_total = fade_samples; - } - } - - void process_internal(const double* input, int slots, double output_gain) noexcept { - const uint32_t sample_count = static_cast(slots) * kHop; - // 1) late send envelopes -> mono - std::vector mono(sample_count, 0.0); - for (int source = 0; source < kChannels; ++source) { - SourceState& state = sources_[source]; - const int64_t total = state.late_fade_total; - if (total == 0) { - if (input && state.late_current != 0.0) { - for (uint32_t s = 0; s < sample_count; ++s) { - mono[s] += input[static_cast(s) * kChannels + source] - * state.late_current; - } - } - continue; - } - int64_t position = state.late_fade_position; - for (uint32_t s = 0; s < sample_count; ++s) { - position += 1; - double amount = static_cast(position) - / static_cast(total); - amount = std::min(std::max(amount, 0.0), 1.0); - const double value = state.late_start * (1.0 - amount) - + state.late_target * amount; - if (input) { - mono[s] += input[static_cast(s) * kChannels + source] - * value; - } - } - if (position >= total) { - state.late_current = state.late_target; - state.late_start = state.late_target; - state.late_fade_position = 0; - state.late_fade_total = 0; - } else { - double amount = static_cast(position) - / static_cast(total); - amount = std::min(std::max(amount, 0.0), 1.0); - state.late_current = - state.late_start * (1.0 - amount) + state.late_target * amount; - state.late_fade_position = position; - } - } - - // 2) FDN + 961-sample stereo delay - std::vector late_pcm(static_cast(sample_count) * 2, 0.0); - if (enable_late_room_) { - std::vector diffused = mono; - for (int line = 0; line < 2; ++line) { - diffused = allpass(line, diffused); - } - for (uint32_t s = 0; s < sample_count; ++s) { - const double value = diffused[s]; - double delayed[4]; - for (int line = 0; line < 4; ++line) { - delayed[line] = fdn_buffers_[line][fdn_positions_[line]]; - } - double damping_state[4]; - for (int line = 0; line < 4; ++line) { - damping_state[line] = damping_ * damping_state_[line] - + (1.0 - damping_) * delayed[line]; - damping_state_[line] = damping_state[line]; - } - const double fdn_left = damping_state[0] + damping_state[1] - - damping_state[2] - damping_state[3]; - const double fdn_right = damping_state[0] - damping_state[1] - + damping_state[2] - damping_state[3]; - const double out_left = - late_ring_[static_cast(late_pos_) * 2]; - const double out_right = - late_ring_[static_cast(late_pos_) * 2 + 1]; - late_pcm[s * 2] = out_left; - late_pcm[s * 2 + 1] = out_right; - late_ring_[static_cast(late_pos_) * 2] = - fdn_gain_ * 0.5 * fdn_left; - late_ring_[static_cast(late_pos_) * 2 + 1] = - fdn_gain_ * 0.5 * fdn_right; - late_pos_ = (late_pos_ + 1) % kLatency; - // feedback = hadamard4/2 @ (damping_state * feedback_gain) - // H4[i][j] = -1 when popcount(i & j) is odd - double feedback[4]; - for (int target = 0; target < 4; ++target) { - double acc = 0.0; - for (int source_line = 0; source_line < 4; ++source_line) { - const bool odd = (static_cast(target & source_line) - ? (std::popcount(static_cast( - target & source_line)) & 1u) != 0u - : false); - const double sign = odd ? -1.0 : 1.0; - acc += sign * damping_state[source_line] - * fdn_feedback_[source_line]; - } - feedback[target] = 0.5 * acc; - } - const double input_vector[4] = {0.5, -0.5, 0.5, 0.5}; - for (int line = 0; line < 4; ++line) { - const double write = - input_vector[line] * value + feedback[line]; - fdn_buffers_[line][fdn_positions_[line]] = write; - fdn_positions_[line] = - (fdn_positions_[line] + 1) % fdn_delays_[line]; - } - } - } - - // 3) analysis chain - analysis_.process(input ? input : zeros_.data(), slots, qmf_work_.data()); - hybrid_analysis_.process(qmf_work_.data(), slots, hybrid_work_.data()); - // 4) per-object direct/early with crossfade - std::vector direct_and_early( - static_cast(slots) * 2 * kHybrid, Complex{0.0, 0.0}); - for (int slot = 0; slot < slots; ++slot) { - const Complex* hybrid_slot = - hybrid_work_.data() + static_cast(slot) * kChannels * kHybrid; - for (int source = 0; source < kChannels; ++source) { - std::memcpy(history_[source].data() - + static_cast(position_) * kHybrid, - hybrid_slot + static_cast(source) * kHybrid, - kHybrid * sizeof(Complex)); - } - Complex* out_slot = - direct_and_early.data() + static_cast(slot) * 2 * kHybrid; - for (int source = kChannels - 1; source >= 0; --source) { - SourceState& state = sources_[source]; - if (state.target.empty()) { - render_paths(source, state.current, 1.0, out_slot); - continue; - } - state.fade_position += 1; - double amount = static_cast(state.fade_position) - / static_cast(state.fade_total); - amount = std::min(std::max(amount, 0.0), 1.0); - render_paths(source, state.current, 1.0 - amount, out_slot); - render_paths(source, state.target, amount, out_slot); - if (state.fade_position >= state.fade_total) { - state.current = state.target; - state.target.clear(); - state.fade_position = 0; - state.fade_total = 0; - } - } - position_ = (position_ + 1) % history_slots_; - processed_slots_ += 1; - } - // 5) synthesis chain - hybrid_synthesis_.process(direct_and_early.data(), slots, - qmf_synth_work_.data()); - synthesis_.process(qmf_synth_work_.data(), slots, pcm_work_.data()); - // 6) mix, gain, interleave [samples][2] - for (uint32_t s = 0; s < sample_count; ++s) { - const int slot = static_cast(s / kHop); - const int b = static_cast(s % kHop); - const double direct_left = - pcm_work_[(static_cast(slot) * 2) * 64 + b]; - const double direct_right = - pcm_work_[(static_cast(slot) * 2 + 1) * 64 + b]; - output_[static_cast(s) * 2] = - (direct_left + late_pcm[static_cast(s) * 2]) * output_gain; - output_[static_cast(s) * 2 + 1] = - (direct_right + late_pcm[static_cast(s) * 2 + 1]) * output_gain; - } - } - - void render_paths(int source, const std::vector& paths, double scale, - Complex* out) const noexcept { - for (const Path& path : paths) { - const int slots = static_cast(history_slots_); - const int index0 = (static_cast(position_) + slots - - path.delay_slots[0]) % slots; - const int index1 = (static_cast(position_) + slots - - path.delay_slots[1]) % slots; - const Complex* history = history_[source].data(); - for (int band = 0; band < kHybrid; ++band) { - const Complex source0 = history[index0 * kHybrid + band]; - const Complex source1 = history[index1 * kHybrid + band]; - out[band].re += source0.re * path.transfer[0][band].re * scale - - source0.im * path.transfer[0][band].im * scale; - out[band].im += source0.re * path.transfer[0][band].im * scale - + source0.im * path.transfer[0][band].re * scale; - out[kHybrid + band].re += - source1.re * path.transfer[1][band].re * scale - - source1.im * path.transfer[1][band].im * scale; - out[kHybrid + band].im += - source1.re * path.transfer[1][band].im * scale - + source1.im * path.transfer[1][band].re * scale; - } - } - } - - std::vector allpass(int line, const std::vector& source) noexcept { - std::vector output(source.size(), 0.0); - const double gain = allpass_gains_[line]; - const uint32_t delay = allpass_delays_[line]; - for (size_t i = 0; i < source.size(); ++i) { - const double delayed = allpass_buffers_[line][allpass_positions_[line]]; - const double result = delayed - gain * source[i]; - allpass_buffers_[line][allpass_positions_[line]] = - source[i] + gain * result; - allpass_positions_[line] = (allpass_positions_[line] + 1) % delay; - output[i] = result; - } - return output; - } - - int fail(const char* message) noexcept { - std::snprintf(error_, sizeof(error_), "%s", message); - return -1; - } - - void reset_state() noexcept { - latency_to_discard_ = kLatency; - processed_input_samples_ = 0; - processed_slots_ = 0; - position_ = 0; - maximum_early_delay_ = 0.0; - history_slots_ = kEarlyHistory + hrtf_slots_; - history_.assign(kChannels, - std::vector( - static_cast(history_slots_) * kHybrid, - Complex{0.0, 0.0})); - sources_.assign(kChannels, SourceState{}); - for (int source = 0; source < kChannels; ++source) { - sources_[source].position[0] = 0.0; - sources_[source].position[1] = 1.0; - sources_[source].position[2] = 0.0; - sources_[source].profile = 1; - sources_[source].gain = 1.0; - sources_[source].enabled = 1; - sources_[source].special_lfe = 0; - sources_[source].fade_position = 0; - sources_[source].fade_total = 0; - sources_[source].late_current = 0.0; - sources_[source].late_start = 0.0; - sources_[source].late_target = 0.0; - sources_[source].late_fade_position = 0; - sources_[source].late_fade_total = 0; - } - for (int line = 0; line < 4; ++line) { - fdn_positions_[line] = 0; - damping_state_[line] = 0.0; - if (!fdn_buffers_[line].empty()) { - std::fill(fdn_buffers_[line].begin(), fdn_buffers_[line].end(), 0.0); - } - } - for (int line = 0; line < 2; ++line) { - allpass_positions_[line] = 0; - if (!allpass_buffers_[line].empty()) { - std::fill(allpass_buffers_[line].begin(), - allpass_buffers_[line].end(), 0.0); - } - } - late_pos_ = 0; - late_ring_.fill(0.0); - error_[0] = '\0'; - } - - QmfAnalysis analysis_; - HybridAnalysis hybrid_analysis_; - HybridSynthesis hybrid_synthesis_; - QmfSynthesis synthesis_; - bool kernels_ready_ = false; - bool field_ready_ = false; - bool room_ready_ = false; - - Complex field_coeff_[kTerms][2][kHybrid]; - double delay_coeff_[kTerms][2]; - double delay_bounds_[2][2]; - double centers_[kHybrid]; - double measurement_radius_ = 1.0; - uint32_t hrtf_slots_ = 0; - - double dims_[3]; - double listener_[3]; - double wall_gain_[6]; - double speed_ = 343.3; - uint32_t fdn_delays_[4]; - double fdn_feedback_[4]; - double damping_ = 0.32; - double fdn_gain_ = 0.22; - uint32_t allpass_delays_[2]; - double allpass_gains_[2]; - - uint32_t latency_to_discard_ = kLatency; - uint64_t processed_input_samples_ = 0; - int64_t processed_slots_ = 0; - uint32_t history_slots_ = kEarlyHistory; - uint32_t position_ = 0; - double maximum_early_delay_ = 0.0; - std::vector> history_; - std::vector sources_; - - std::vector fdn_buffers_[4]; - uint32_t fdn_positions_[4]; - double damping_state_[4]; - std::vector allpass_buffers_[2]; - uint32_t allpass_positions_[2]; - std::array late_ring_{}; - uint32_t late_pos_ = 0; - - std::array qmf_work_{}; - std::array hybrid_work_{}; - std::array qmf_synth_work_{}; - std::array pcm_work_{}; - std::array output_{}; - std::array zeros_{}; - - char error_[256]; - bool enable_early_reflections_ = true; - bool enable_late_room_ = true; -}; - -} // namespace ejoc::sofa_binaural - -using ejoc::sofa_binaural::Renderer; - -extern "C" { - -ejoc_sofa_binaural_handle EJOC_CALL ejoc_sofa_binaural_create(void) { - try { - return new (std::nothrow) Renderer(); - } catch (...) { - return nullptr; - } -} - -void EJOC_CALL ejoc_sofa_binaural_destroy(ejoc_sofa_binaural_handle handle) { - delete static_cast(handle); -} - -int EJOC_CALL ejoc_sofa_binaural_reset(ejoc_sofa_binaural_handle handle) { - return handle ? static_cast(handle)->reset() : -1; -} - -const char* EJOC_CALL ejoc_sofa_binaural_last_error( - ejoc_sofa_binaural_handle handle) { - return handle ? static_cast(handle)->error() : "null handle"; -} - -int EJOC_CALL ejoc_sofa_binaural_configure_kernels( - ejoc_sofa_binaural_handle handle, const double* qmf_analysis, - const double* hybrid_low, const int16_t* hybrid_indices, - const double* hybrid_values, uint32_t hybrid_count, const double* qmf_basis, - const double* qmf_taps) { - return handle ? static_cast(handle)->configure_kernels( - qmf_analysis, hybrid_low, hybrid_indices, hybrid_values, hybrid_count, - qmf_basis, qmf_taps) : -1; -} - -int EJOC_CALL ejoc_sofa_binaural_configure_field( - ejoc_sofa_binaural_handle handle, const double* coefficients, - const double* delay_coefficients, const double* delay_bounds, - const double* band_centers, double measurement_radius_m) { - return handle ? static_cast(handle)->configure_field( - coefficients, delay_coefficients, delay_bounds, band_centers, - measurement_radius_m) : -1; -} - -int EJOC_CALL ejoc_sofa_binaural_configure_room( - ejoc_sofa_binaural_handle handle, const double* room_dims, - const double* listener_pos, const double* wall_gains, double speed_of_sound, - const uint32_t* fdn_delays, const double* fdn_feedback, double damping, - double fdn_output_gain, const uint32_t* allpass_delays, - const double* allpass_gains, uint32_t enable_early, uint32_t enable_late) { - return handle ? static_cast(handle)->configure_room( - room_dims, listener_pos, wall_gains, speed_of_sound, fdn_delays, - fdn_feedback, damping, fdn_output_gain, allpass_delays, allpass_gains, - enable_early, enable_late) : -1; -} - -int EJOC_CALL ejoc_sofa_binaural_set_source( - ejoc_sofa_binaural_handle handle, uint32_t source, const double* position_adm, - uint32_t profile, double gain, uint32_t enabled, uint32_t special_lfe, - uint32_t fade) { - return handle ? static_cast(handle)->set_source( - source, position_adm, profile, gain, enabled, special_lfe, fade) : -1; -} - -int EJOC_CALL ejoc_sofa_binaural_process( - ejoc_sofa_binaural_handle handle, const double* input16_interleaved, - uint32_t sample_count, double output_gain, double* output_stereo_interleaved) { - return handle ? static_cast(handle)->process( - input16_interleaved, sample_count, output_gain, output_stereo_interleaved) - : -1; -} - -int EJOC_CALL ejoc_sofa_binaural_finish( - ejoc_sofa_binaural_handle handle, uint32_t flush_samples, - double* output_stereo_interleaved, uint32_t capacity) { - return handle ? static_cast(handle)->finish( - flush_samples, output_stereo_interleaved, capacity) : -1; -} - - - - - - - - -} // extern "C" diff --git a/native/src/speaker_layouts.h b/native/src/speaker_layouts.h deleted file mode 100644 index cbd7f89..0000000 --- a/native/src/speaker_layouts.h +++ /dev/null @@ -1,534 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace ejoc::speaker_tables { - -inline constexpr std::size_t kMaxPoints = 15; -inline constexpr std::size_t kMaxGroups = 4; -inline constexpr std::size_t kMaxGroupSize = 3; -inline constexpr std::size_t kMaxChannels = 16; - -struct SpeakerPoint { - std::array coordinate_q15{}; - std::uint8_t speaker_id{}; -}; - -struct AxisGroup { - std::uint8_t size{}; - std::array indices{}; -}; - -struct RegionGeometry { - std::uint8_t point_count{}; - std::uint8_t mode{}; - std::uint8_t axis0_group_count{}; - std::uint8_t axis1_group_count{}; - std::array points{}; - std::array axis0_groups{}; - std::array axis1_groups{}; -}; - -struct LayoutGeometry { - std::uint32_t speaker_bitfield{}; - std::uint8_t out_ch_config{}; - std::uint8_t channel_count{}; - std::array standard_from_internal{}; - std::array regions{}; -}; - -inline constexpr std::array kLayouts{{ - LayoutGeometry{ - 0x1u, 0, 2, - {{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, - {{ - RegionGeometry{ - 2, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 2, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 2, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 2, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 2, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 2, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 2, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - } - }} - }, - LayoutGeometry{ - 0x7u, 3, 4, - {{0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, - {{ - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 1, 1, 1, 0, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{1, {0, 0, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - } - }} - }, - LayoutGeometry{ - 0xFu, 7, 6, - {{0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, - {{ - RegionGeometry{ - 5, 2, 2, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 5, 2, 2, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 5, 2, 2, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 2, 2, 0, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 2, 1, 1, 0, - {{SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - } - }} - }, - LayoutGeometry{ - 0x1Fu, 11, 8, - {{0, 1, 2, 3, 6, 7, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0}}, - {{ - RegionGeometry{ - 7, 2, 3, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 5, 2, 2, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 5, 2, 2, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 2, 2, 0, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 4, 2, 2, 0, - {{SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - } - }} - }, - LayoutGeometry{ - 0x40Fu, 13, 8, - {{0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0}}, - {{ - RegionGeometry{ - 7, 3, 2, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 2, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 2, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 5, 3, 2, 1, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 5, 3, 1, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 4, 3, 1, 1, - {{SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 6}, SpeakerPoint{{24840, 16384, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - } - }} - }, - LayoutGeometry{ - 0xA0Fu, 14, 10, - {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0}}, - {{ - RegionGeometry{ - 9, 3, 2, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 9, 3, 2, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 9, 3, 2, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 2, 2, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 1, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 6, 3, 1, 2, - {{SpeakerPoint{{0, 32767, 0}, 4}, SpeakerPoint{{32767, 32767, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{{7928, 24840, 32767}, 8}, SpeakerPoint{{24840, 24840, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {2, 3, 0}}, AxisGroup{2, {4, 5, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 5, 3, 1, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{7928, 7928, 32767}, 6}, SpeakerPoint{{24840, 7928, 32767}, 7}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - } - }} - }, - LayoutGeometry{ - 0x41Fu, 15, 10, - {{0, 1, 2, 3, 6, 7, 4, 5, 8, 9, 0, 0, 0, 0, 0, 0}}, - {{ - RegionGeometry{ - 9, 3, 3, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 16384, 32767}, 8}, SpeakerPoint{{24840, 16384, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{}}}, - {{AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 2, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 16384, 32767}, 8}, SpeakerPoint{{24840, 16384, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 2, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 16384, 32767}, 8}, SpeakerPoint{{24840, 16384, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 5, 3, 2, 1, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 16384, 32767}, 8}, SpeakerPoint{{24840, 16384, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 5, 3, 1, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{7928, 16384, 32767}, 8}, SpeakerPoint{{24840, 16384, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 6, 3, 2, 1, - {{SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 16384, 32767}, 8}, SpeakerPoint{{24840, 16384, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {4, 5, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 3, 1, 1, 0, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - } - }} - }, - LayoutGeometry{ - 0xA1Fu, 16, 12, - {{0, 1, 2, 3, 6, 7, 4, 5, 8, 9, 10, 11, 0, 0, 0, 0}}, - {{ - RegionGeometry{ - 11, 3, 3, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 7928, 32767}, 8}, SpeakerPoint{{24840, 7928, 32767}, 9}, SpeakerPoint{{7928, 24840, 32767}, 10}, SpeakerPoint{{24840, 24840, 32767}, 11}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{}}}, - {{AxisGroup{2, {7, 8, 0}}, AxisGroup{2, {9, 10, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 9, 3, 2, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{7928, 7928, 32767}, 8}, SpeakerPoint{{24840, 7928, 32767}, 9}, SpeakerPoint{{7928, 24840, 32767}, 10}, SpeakerPoint{{24840, 24840, 32767}, 11}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 9, 3, 2, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 7928, 32767}, 8}, SpeakerPoint{{24840, 7928, 32767}, 9}, SpeakerPoint{{7928, 24840, 32767}, 10}, SpeakerPoint{{24840, 24840, 32767}, 11}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 2, 2, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 7928, 32767}, 8}, SpeakerPoint{{24840, 7928, 32767}, 9}, SpeakerPoint{{7928, 24840, 32767}, 10}, SpeakerPoint{{24840, 24840, 32767}, 11}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 1, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{7928, 7928, 32767}, 8}, SpeakerPoint{{24840, 7928, 32767}, 9}, SpeakerPoint{{7928, 24840, 32767}, 10}, SpeakerPoint{{24840, 24840, 32767}, 11}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 8, 3, 2, 2, - {{SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 7928, 32767}, 8}, SpeakerPoint{{24840, 7928, 32767}, 9}, SpeakerPoint{{7928, 24840, 32767}, 10}, SpeakerPoint{{24840, 24840, 32767}, 11}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {4, 5, 0}}, AxisGroup{2, {6, 7, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 5, 3, 1, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{7928, 7928, 32767}, 8}, SpeakerPoint{{24840, 7928, 32767}, 9}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - } - }} - }, - LayoutGeometry{ - 0xA9Fu, 19, 14, - {{0, 1, 2, 3, 6, 7, 4, 5, 10, 11, 12, 13, 8, 9, 0, 0}}, - {{ - RegionGeometry{ - 13, 3, 4, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{0, 5285, 0}, 8}, SpeakerPoint{{32767, 5285, 0}, 9}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 24840, 32767}, 12}, SpeakerPoint{{24840, 24840, 32767}, 13}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}}}, - {{AxisGroup{2, {9, 10, 0}}, AxisGroup{2, {11, 12, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 11, 3, 3, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 5285, 0}, 8}, SpeakerPoint{{32767, 5285, 0}, 9}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 24840, 32767}, 12}, SpeakerPoint{{24840, 24840, 32767}, 13}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}}}, - {{AxisGroup{2, {7, 8, 0}}, AxisGroup{2, {9, 10, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 9, 3, 2, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 24840, 32767}, 12}, SpeakerPoint{{24840, 24840, 32767}, 13}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 2, 2, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 24840, 32767}, 12}, SpeakerPoint{{24840, 24840, 32767}, 13}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 1, 2, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 24840, 32767}, 12}, SpeakerPoint{{24840, 24840, 32767}, 13}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 8, 3, 2, 2, - {{SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 24840, 32767}, 12}, SpeakerPoint{{24840, 24840, 32767}, 13}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {4, 5, 0}}, AxisGroup{2, {6, 7, 0}}, AxisGroup{}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 2, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 5285, 0}, 8}, SpeakerPoint{{32767, 5285, 0}, 9}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - } - }} - }, - LayoutGeometry{ - 0xE9Fu, 20, 16, - {{0, 1, 2, 3, 6, 7, 4, 5, 10, 11, 14, 15, 12, 13, 8, 9}}, - {{ - RegionGeometry{ - 15, 3, 4, 3, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{0, 5285, 0}, 8}, SpeakerPoint{{32767, 5285, 0}, 9}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 16384, 32767}, 12}, SpeakerPoint{{24840, 16384, 32767}, 13}, SpeakerPoint{{7928, 24840, 32767}, 14}, SpeakerPoint{{24840, 24840, 32767}, 15}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}}}, - {{AxisGroup{2, {9, 10, 0}}, AxisGroup{2, {11, 12, 0}}, AxisGroup{2, {13, 14, 0}}, AxisGroup{}}} - }, - RegionGeometry{ - 13, 3, 3, 3, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 5285, 0}, 8}, SpeakerPoint{{32767, 5285, 0}, 9}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 16384, 32767}, 12}, SpeakerPoint{{24840, 16384, 32767}, 13}, SpeakerPoint{{7928, 24840, 32767}, 14}, SpeakerPoint{{24840, 24840, 32767}, 15}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}}}, - {{AxisGroup{2, {7, 8, 0}}, AxisGroup{2, {9, 10, 0}}, AxisGroup{2, {11, 12, 0}}, AxisGroup{}}} - }, - RegionGeometry{ - 11, 3, 2, 3, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 16384, 32767}, 12}, SpeakerPoint{{24840, 16384, 32767}, 13}, SpeakerPoint{{7928, 24840, 32767}, 14}, SpeakerPoint{{24840, 24840, 32767}, 15}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{2, {9, 10, 0}}, AxisGroup{}}} - }, - RegionGeometry{ - 9, 3, 2, 3, - {{SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 16384, 32767}, 12}, SpeakerPoint{{24840, 16384, 32767}, 13}, SpeakerPoint{{7928, 24840, 32767}, 14}, SpeakerPoint{{24840, 24840, 32767}, 15}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{1, {0, 0, 0}}, AxisGroup{2, {1, 2, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}}} - }, - RegionGeometry{ - 9, 3, 1, 3, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 16384, 32767}, 12}, SpeakerPoint{{24840, 16384, 32767}, 13}, SpeakerPoint{{7928, 24840, 32767}, 14}, SpeakerPoint{{24840, 24840, 32767}, 15}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {3, 4, 0}}, AxisGroup{2, {5, 6, 0}}, AxisGroup{2, {7, 8, 0}}, AxisGroup{}}} - }, - RegionGeometry{ - 10, 3, 2, 3, - {{SpeakerPoint{{0, 16384, 0}, 4}, SpeakerPoint{{32767, 16384, 0}, 5}, SpeakerPoint{{0, 32767, 0}, 6}, SpeakerPoint{{32767, 32767, 0}, 7}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{{7928, 16384, 32767}, 12}, SpeakerPoint{{24840, 16384, 32767}, 13}, SpeakerPoint{{7928, 24840, 32767}, 14}, SpeakerPoint{{24840, 24840, 32767}, 15}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{2, {0, 1, 0}}, AxisGroup{2, {2, 3, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {4, 5, 0}}, AxisGroup{2, {6, 7, 0}}, AxisGroup{2, {8, 9, 0}}, AxisGroup{}}} - }, - RegionGeometry{ - 7, 3, 2, 1, - {{SpeakerPoint{{0, 0, 0}, 0}, SpeakerPoint{{32767, 0, 0}, 1}, SpeakerPoint{{16384, 0, 0}, 2}, SpeakerPoint{{0, 5285, 0}, 8}, SpeakerPoint{{32767, 5285, 0}, 9}, SpeakerPoint{{7928, 7928, 32767}, 10}, SpeakerPoint{{24840, 7928, 32767}, 11}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}, SpeakerPoint{}}}, - {{AxisGroup{3, {0, 2, 1}}, AxisGroup{2, {3, 4, 0}}, AxisGroup{}, AxisGroup{}}}, - {{AxisGroup{2, {5, 6, 0}}, AxisGroup{}, AxisGroup{}, AxisGroup{}}} - } - }} - } -}}; - -inline constexpr const LayoutGeometry* find_layout(const std::uint32_t speaker_bitfield) noexcept { - for (const auto& layout : kLayouts) { - if (layout.speaker_bitfield == speaker_bitfield) { - return &layout; - } - } - return nullptr; -} - -} // namespace ejoc::speaker_tables diff --git a/native/src/speaker_renderer.cpp b/native/src/speaker_renderer.cpp deleted file mode 100644 index 385cce5..0000000 --- a/native/src/speaker_renderer.cpp +++ /dev/null @@ -1,495 +0,0 @@ -#define EJOC_BUILD_DLL -#include "eac3joc_core.h" -#include "speaker_layouts.h" - -#include -#include -#include -#include -#include -#include -#include - -namespace ejoc::speaker { - -using speaker_tables::AxisGroup; -using speaker_tables::LayoutGeometry; -using speaker_tables::RegionGeometry; - -constexpr double kPi = 3.141592653589793238462643383279502884; -constexpr double kQ15Scale = 32768.0; -constexpr double kQ15Max = 32767.0 / kQ15Scale; -constexpr double kGainSnapThreshold = 1.0e-4; -constexpr std::size_t kObjects = EJOC_MAX_OBJECTS; -constexpr std::size_t kChannels = EJOC_OUTPUT_CHANNELS; -constexpr std::size_t kBlock = EJOC_SPEAKER_BLOCK_SAMPLES; - -using PointGains = std::array; -using ChannelGains = std::array; -using ObjectChannelGains = std::array; -using RemainingCounts = std::array, kObjects>; - -inline double clamp(const double value, const double low, const double high) noexcept { - return value < low ? low : (value > high ? high : value); -} - -inline double coordinate(const RegionGeometry& region, const std::size_t point, - const std::size_t component) noexcept { - return static_cast(region.points[point].coordinate_q15[component]) / kQ15Scale; -} - -std::uint64_t expand_speaker_bitfield(const std::uint32_t compact) noexcept { - constexpr std::array expansions{{ - 0x00000003ULL, 0x00000004ULL, 0x00000008ULL, 0x00000030ULL, - 0x000000C0ULL, 0x00000100ULL, 0x00000600ULL, 0x00001800ULL, - 0x00006000ULL, 0x00018000ULL, 0x00060000ULL, 0x00180000ULL, - 0x00600000ULL, 0x01800000ULL, 0x06000000ULL, 0x18000000ULL, - 0x60000000ULL, 0x080000000ULL, 0x600000000ULL, 0x800000000ULL, - 0x1000000000ULL, 0x2000000000ULL, - }}; - std::uint64_t expanded = 0; - for (std::size_t bit_index = 0; bit_index < expansions.size(); ++bit_index) { - if ((compact & (1u << bit_index)) != 0) { - expanded |= expansions[bit_index]; - } - } - return expanded; -} - -inline int bit(const std::uint64_t value, const unsigned index) noexcept { - return static_cast((value >> index) & 1ULL); -} - -double layout_attenuation_db(const std::uint32_t compact) noexcept { - const std::uint64_t expanded = expand_speaker_bitfield(compact); - const int height_channels = 2 * ( - bit(expanded, 13) + bit(expanded, 15) + bit(expanded, 17) + - bit(expanded, 19) + bit(expanded, 21)); - const int floor_channels = bit(expanded, 8) + 2 * ( - bit(expanded, 31) + bit(expanded, 4) + bit(expanded, 6) + - bit(expanded, 11) + bit(expanded, 25) + bit(expanded, 27) + - bit(expanded, 29) + bit(expanded, 33)); - const double height_factor = std::min(static_cast(height_channels) / 4.0, 1.0); - const double floor_factor = std::min(static_cast(floor_channels) / 4.0, 1.0); - return -std::max(4.5 - 1.5 * height_factor - 3.0 * floor_factor, 0.0); -} - -int floor_y_exponent(const std::uint32_t compact) noexcept { - const std::uint32_t low = static_cast(expand_speaker_bitfield(compact)); - return ((low & 0x130u) != 0 && (low & 0x18C0u) == 0) ? 1 : 0; -} - -inline void equal_power_pair(const double position, double& lower, double& upper) noexcept { - const double angle = (kPi * 0.5) * position; - lower = std::cos(angle); - upper = std::sin(angle); -} - -void axis0_gains(const RegionGeometry& region, - const std::array& groups, - const std::uint8_t group_count, - const double value, - PointGains& output) noexcept { - output.fill(0.0); - for (std::size_t row = 0; row < group_count; ++row) { - const auto& group = groups[row]; - if (group.size == 0) { - continue; - } - const std::size_t first = group.indices[0]; - const std::size_t last = group.indices[group.size - 1]; - const double first_value = coordinate(region, first, 0); - const double last_value = coordinate(region, last, 0); - if (value <= first_value) { - output[first] = 1.0; - continue; - } - if (value >= last_value) { - output[last] = 1.0; - continue; - } - for (std::size_t index = 0; index + 1 < group.size; ++index) { - const std::size_t lower_index = group.indices[index]; - const std::size_t upper_index = group.indices[index + 1]; - const double lower_value = coordinate(region, lower_index, 0); - const double upper_value = coordinate(region, upper_index, 0); - if (value > lower_value && value <= upper_value) { - const double position = (value - lower_value) / (upper_value - lower_value); - equal_power_pair(position, output[lower_index], output[upper_index]); - break; - } - } - } -} - -void axis1_gains(const RegionGeometry& region, - const std::array& groups, - const std::uint8_t group_count, - const double value, - PointGains& output) noexcept { - output.fill(0.0); - if (group_count == 0) { - return; - } - const auto& first_group = groups[0]; - const auto& last_group = groups[group_count - 1]; - const double first_value = coordinate(region, first_group.indices[0], 1); - const double last_value = coordinate(region, last_group.indices[0], 1); - if (value <= first_value) { - for (std::size_t index = 0; index < first_group.size; ++index) { - output[first_group.indices[index]] = 1.0; - } - return; - } - if (value > last_value) { - for (std::size_t index = 0; index < last_group.size; ++index) { - output[last_group.indices[index]] = 1.0; - } - return; - } - for (std::size_t row = 0; row + 1 < group_count; ++row) { - const auto& lower_group = groups[row]; - const auto& upper_group = groups[row + 1]; - const double lower_value = coordinate(region, lower_group.indices[0], 1); - const double upper_value = coordinate(region, upper_group.indices[0], 1); - if (value >= lower_value && value <= upper_value) { - const double position = (value - lower_value) / (upper_value - lower_value); - double lower_gain = 0.0; - double upper_gain = 0.0; - equal_power_pair(position, lower_gain, upper_gain); - for (std::size_t index = 0; index < lower_group.size; ++index) { - output[lower_group.indices[index]] = lower_gain; - } - for (std::size_t index = 0; index < upper_group.size; ++index) { - output[upper_group.indices[index]] = upper_gain; - } - return; - } - } -} - -void plane_gains(const RegionGeometry& region, - const std::array& groups, - const std::uint8_t group_count, - const double u, - const double v, - const std::uint8_t mode, - PointGains& output) noexcept { - axis0_gains(region, groups, group_count, u, output); - if (mode >= 2) { - PointGains vertical{}; - axis1_gains(region, groups, group_count, v, vertical); - for (std::size_t point = 0; point < region.point_count; ++point) { - output[point] *= vertical[point]; - } - } -} - -class Renderer { -public: - explicit Renderer(const LayoutGeometry* layout) noexcept : layout_(layout) { - for (std::size_t standard = 0; standard < layout_->channel_count; ++standard) { - standard_index_for_internal_[layout_->standard_from_internal[standard]] = - static_cast(standard); - } - attenuation_db_ = layout_attenuation_db(layout_->speaker_bitfield); - floor_y_exponent_ = floor_y_exponent(layout_->speaker_bitfield); - reset_state(); - } - - int reset() noexcept { - reset_state(); - error_[0] = '\0'; - return 0; - } - - const char* last_error() const noexcept { - return error_[0] ? error_.data() : ""; - } - - int process(const float* input, - const std::uint32_t sample_count, - const std::uint32_t metadata_count, - const std::uint32_t* metadata_offsets, - const std::uint32_t* ramp_durations, - const std::uint16_t* positions_q15, - const std::uint8_t* region_indices, - const std::uint8_t* height_enabled, - const double* object_gains, - double* output) noexcept { - error_[0] = '\0'; - if (!input || !output) { - return fail("null PCM pointer passed to ejoc_speaker_renderer_process"); - } - if ((sample_count % kBlock) != 0) { - return fail("sample_count must be a multiple of 32"); - } - if (metadata_count && (!metadata_offsets || !ramp_durations || !positions_q15)) { - return fail("metadata arrays are null while metadata_count is nonzero"); - } - if (!validate_metadata(sample_count, metadata_count, metadata_offsets, - positions_q15, region_indices, object_gains)) { - return -1; - } - - std::fill(output, output + static_cast(sample_count) * layout_->channel_count, 0.0); - const bool has_lfe = (layout_->speaker_bitfield & 0x4u) != 0; - const std::size_t total_blocks = sample_count / kBlock; - std::size_t event = 0; - for (std::size_t block = 0; block < total_blocks; ++block) { - while (event < metadata_count && aligned_block(metadata_offsets[event]) == block) { - apply_event(event, ramp_durations, positions_q15, region_indices, - height_enabled, object_gains); - ++event; - } - mix_block(input, output, block, has_lfe); - } - while (event < metadata_count && aligned_block(metadata_offsets[event]) == total_blocks) { - apply_event(event, ramp_durations, positions_q15, region_indices, - height_enabled, object_gains); - ++event; - } - if (event != metadata_count) { - return fail("metadata alignment produced an event outside this process call"); - } - return 0; - } - -private: - void reset_state() noexcept { - for (auto& row : current_) row.fill(0.0); - for (auto& row : target_) row.fill(0.0); - for (auto& row : step_) row.fill(0.0); - for (auto& row : remaining_) row.fill(0); - } - - int fail(const char* message) noexcept { - std::snprintf(error_.data(), error_.size(), "%s", message); - return -1; - } - - bool validate_metadata(const std::uint32_t sample_count, - const std::uint32_t metadata_count, - const std::uint32_t* metadata_offsets, - const std::uint16_t* positions_q15, - const std::uint8_t* region_indices, - const double* object_gains) noexcept { - for (std::size_t event = 0; event < metadata_count; ++event) { - if (metadata_offsets[event] > sample_count) { - fail("metadata offset exceeds sample_count"); - return false; - } - if (event && metadata_offsets[event] < metadata_offsets[event - 1]) { - fail("metadata offsets must be nondecreasing"); - return false; - } - for (std::size_t object = 0; object < kObjects; ++object) { - const std::size_t object_event = event * kObjects + object; - if (region_indices && region_indices[object_event] >= 7) { - fail("region index is above 6"); - return false; - } - if (object_gains && !std::isfinite(object_gains[object_event])) { - fail("object gain is not finite"); - return false; - } - const std::size_t coordinate_base = object_event * EJOC_SPEAKER_COORDINATES; - for (std::size_t component = 0; component < EJOC_SPEAKER_COORDINATES; ++component) { - if (positions_q15[coordinate_base + component] > 32767u) { - fail("Q15 object coordinate is above 32767"); - return false; - } - } - } - } - return true; - } - - static std::size_t aligned_block(const std::uint32_t sample) noexcept { - return (static_cast(sample) + kBlock / 2 - 1) / kBlock; - } - - static std::uint32_t ramp_blocks(const std::uint32_t duration) noexcept { - return static_cast( - (static_cast(duration) + kBlock / 2 - 1) / kBlock); - } - - void render_point(const std::uint16_t* position, - const std::uint8_t region_index, - const bool enable_height, - const double object_gain, - ChannelGains& output) const noexcept { - output.fill(0.0); - const auto& region = layout_->regions[region_index]; - const double u = static_cast(position[0]) / kQ15Scale; - const double v = static_cast(position[1]) / kQ15Scale; - const double w = static_cast(position[2]) / kQ15Scale; - const double floor_v = clamp(std::ldexp(v, floor_y_exponent_), 0.0, 1.0); - PointGains floor{}; - plane_gains(region, region.axis0_groups, region.axis0_group_count, - u, floor_v, region.mode, floor); - PointGains point = floor; - if (region.mode == 3) { - PointGains height{}; - plane_gains(region, region.axis1_groups, region.axis1_group_count, - u, v, 3, height); - const double z = enable_height ? clamp(w, 0.0, kQ15Max) : 0.0; - if (z >= kQ15Max) { - point = height; - } else if (z > 0.0) { - double floor_weight = 0.0; - double height_weight = 0.0; - equal_power_pair(z, floor_weight, height_weight); - for (std::size_t index = 0; index < region.point_count; ++index) { - point[index] = floor[index] * floor_weight + height[index] * height_weight; - } - } - } - const double y_term = clamp(v / 0.6, 0.0, 1.0); - const double z_term = clamp((w - 0.2) / 0.8, 0.0, 1.0); - const double amount = clamp(y_term + z_term, 0.0, 1.0); - const double gain = std::pow(10.0, attenuation_db_ * amount / 20.0) * object_gain; - for (std::size_t index = 0; index < region.point_count; ++index) { - output[region.points[index].speaker_id] = point[index] * gain; - } - } - - void apply_event(const std::size_t event, - const std::uint32_t* ramp_durations, - const std::uint16_t* positions_q15, - const std::uint8_t* region_indices, - const std::uint8_t* height_enabled, - const double* object_gains) noexcept { - const std::uint32_t blocks = ramp_blocks(ramp_durations[event]); - for (std::size_t object = 0; object < kObjects; ++object) { - const std::size_t object_event = event * kObjects + object; - const auto* position = positions_q15 + object_event * EJOC_SPEAKER_COORDINATES; - const std::uint8_t region = region_indices ? region_indices[object_event] : 0; - const bool height = !height_enabled || height_enabled[object_event] != 0; - const double object_gain = object_gains ? object_gains[object_event] : 1.0; - ChannelGains next{}; - render_point(position, region, height, object_gain, next); - for (std::size_t channel = 0; channel < layout_->channel_count; ++channel) { - const double difference = next[channel] - current_[object][channel]; - target_[object][channel] = next[channel]; - if (std::abs(difference) >= kGainSnapThreshold && blocks != 0) { - step_[object][channel] = difference / static_cast(blocks); - remaining_[object][channel] = blocks; - } else { - current_[object][channel] = next[channel]; - step_[object][channel] = 0.0; - remaining_[object][channel] = 0; - } - } - } - } - - void mix_block(const float* input, double* output, const std::size_t block, - const bool has_lfe) noexcept { - const std::size_t start = block * kBlock; - if (has_lfe) { - for (std::size_t sample = 0; sample < kBlock; ++sample) { - output[(start + sample) * layout_->channel_count + 3] = - static_cast(input[(start + sample) * EJOC_OUTPUT_CHANNELS]); - } - } - for (std::size_t object = 0; object < kObjects; ++object) { - for (std::size_t internal = 0; internal < layout_->channel_count; ++internal) { - const bool active = remaining_[object][internal] != 0; - const double fixed_gain = target_[object][internal]; - if (!active && fixed_gain == 0.0) { - continue; - } - const std::size_t standard = standard_index_for_internal_[internal]; - for (std::size_t sample = 0; sample < kBlock; ++sample) { - const double gain = active - ? current_[object][internal] + - (static_cast(sample) / static_cast(kBlock)) * - step_[object][internal] - : fixed_gain; - output[(start + sample) * layout_->channel_count + standard] += - static_cast( - input[(start + sample) * EJOC_OUTPUT_CHANNELS + object + 1]) * gain; - } - if (active) { - current_[object][internal] += step_[object][internal]; - --remaining_[object][internal]; - if (remaining_[object][internal] == 0) { - current_[object][internal] = target_[object][internal]; - } - } else { - current_[object][internal] = target_[object][internal]; - } - } - } - } - - const LayoutGeometry* layout_; - double attenuation_db_{}; - int floor_y_exponent_{}; - ObjectChannelGains current_{}; - ObjectChannelGains target_{}; - ObjectChannelGains step_{}; - RemainingCounts remaining_{}; - std::array standard_index_for_internal_{}; - std::array error_{}; -}; - -} // namespace ejoc::speaker - -extern "C" { - -uint32_t EJOC_CALL ejoc_speaker_layout_channel_count(const uint32_t speaker_bitfield) { - const auto* layout = ejoc::speaker_tables::find_layout(speaker_bitfield); - return layout ? layout->channel_count : 0; -} - -ejoc_speaker_renderer_handle EJOC_CALL ejoc_speaker_renderer_create( - const uint32_t speaker_bitfield) { - const auto* layout = ejoc::speaker_tables::find_layout(speaker_bitfield); - if (!layout) { - return nullptr; - } - return new (std::nothrow) ejoc::speaker::Renderer(layout); -} - -void EJOC_CALL ejoc_speaker_renderer_destroy(ejoc_speaker_renderer_handle handle) { - delete static_cast(handle); -} - -int EJOC_CALL ejoc_speaker_renderer_reset(ejoc_speaker_renderer_handle handle) { - if (!handle) { - return -1; - } - return static_cast(handle)->reset(); -} - -const char* EJOC_CALL ejoc_speaker_renderer_last_error(ejoc_speaker_renderer_handle handle) { - if (!handle) { - return "speaker renderer handle is null"; - } - return static_cast(handle)->last_error(); -} - -int EJOC_CALL ejoc_speaker_renderer_process( - ejoc_speaker_renderer_handle handle, - const float* objects16_interleaved, - const uint32_t sample_count, - const uint32_t metadata_count, - const uint32_t* metadata_offsets, - const uint32_t* ramp_durations, - const uint16_t* positions_q15, - const uint8_t* region_indices, - const uint8_t* height_enabled, - const double* object_gains, - double* output_interleaved) { - if (!handle) { - return -1; - } - return static_cast(handle)->process( - objects16_interleaved, sample_count, metadata_count, - metadata_offsets, ramp_durations, positions_q15, - region_indices, height_enabled, object_gains, output_interleaved); -} - -} // extern "C" diff --git a/src/bed_layout.py b/src/bed_layout.py new file mode 100644 index 0000000..d9defbe --- /dev/null +++ b/src/bed_layout.py @@ -0,0 +1,80 @@ +"""JOC 核心通道的输入序、床声明与 ADM 落位。 + +这里只有"位置/身份"层:按通道码把 E-AC-3 解出的 PCM 摆进 JOC 输入序,并把额外 +那一对落到 ADM 骨架的槽位。JOC 的 DSP 不读取这些信息(规格 §10.1),它只用 +``(N, sigma)``。 +""" +from emdf import expand_chanmap + +# JOC 核心通道序列的前 5 位(规格 §2):L R C Ls Rs。 +JOC_CORE_POSITIONS = ("L", "R", "C", "Ls", "Rs") + +# 额外那一对的身份由 k 决定(规格 §2):k=1 是后环绕,k∈{2,4} 是前上方。 +EXTRA_PAIR_BY_CONFIG = {1: ("Lrs", "Rrs"), 2: ("Vhl", "Vhr"), 4: ("Vhl", "Vhr")} + +# 额外那一对在 10 槽 ADM 骨架里的槽位(规格 §10.2 的 RC_Lrs/RC_Rrs 与 RC_Lts/RC_Rts)。 +# ADM 现契约是"除 LFE 槽外全部静音"(床通道不作为输出通道),因此这些槽位只被记录, +# 不承载音频;DSP 也不读它们。 +BED_EXTRA_SLOTS_BY_CONFIG = {1: (6, 7), 2: (8, 9), 4: (8, 9)} + +# 位置 → ffmpeg 声道名与其声道位。ffmpeg 的帧内声道顺序就是声道位的升序, +# 因此这张表同时给出"位置集合 → 帧内下标"的换算。 +_FFMPEG_POSITIONS = { + "L": ("FL", 0x1), + "R": ("FR", 0x2), + "C": ("FC", 0x4), + "LFE": ("LFE", 0x8), + "Lrs": ("BL", 0x10), + "Rrs": ("BR", 0x20), + "Lc": ("FLC", 0x40), + "Rc": ("FRC", 0x80), + "Cs": ("BC", 0x100), + "Ls": ("SL", 0x200), + "Rs": ("SR", 0x400), + "Vhl": ("TFL", 0x1000), + "Vhc": ("TFC", 0x2000), + "Vhr": ("TFR", 0x4000), + "Lts": ("TBL", 0x8000), + "Rts": ("TBR", 0x20000), +} + + +def bed_order(locations): + """把位置集合按 ffmpeg 的声道位顺序排好,返回 (位置, ffmpeg 名) 列表。 + + 未知位置返回 None,由调用方决定报哪种变体。 + """ + unique = [] + for position in locations: + if position not in unique: + unique.append(position) + if any(position not in _FFMPEG_POSITIONS for position in unique): + return None + return [(position, _FFMPEG_POSITIONS[position][0]) + for position in sorted(unique, key=lambda p: _FFMPEG_POSITIONS[p][1])] + + +def extra_pair(core_locations, chanmap): + """依赖子流的 chanmap 里超出核心声道集合的那一对位置。 + + chanmap 会把核心已有的声道一并列出(例如 5.1 核心 + 前上方对会写 + ``Ls/Rs/Lrs/Rrs``),因此额外对是"chanmap 位置 − 核心位置"。 + """ + core = set(core_locations) + return tuple(position for position in expand_chanmap(chanmap) if position not in core) + + +def joc_input_indices(locations, pair): + """返回 JOC 输入序 (L, R, C, Ls, Rs, 额外对…) 在床 PCM 里的下标。""" + if pair is None: + pair = () + order = bed_order(locations) + if order is None: + return None + index_of = {position: index for index, (position, _name) in enumerate(order)} + required = list(JOC_CORE_POSITIONS) + list(pair) + if any(position not in index_of for position in required): + return None + if len(required) != len(set(required)): + return None + return [index_of[position] for position in required] diff --git a/src/binaural_native_renderer.py b/src/binaural_native_renderer.py deleted file mode 100644 index 3075a1e..0000000 --- a/src/binaural_native_renderer.py +++ /dev/null @@ -1,214 +0,0 @@ -"""ctypes bridge for the native float64 binaural DSP.""" -from __future__ import annotations - -import ctypes -from pathlib import Path - -import numpy as np - -from native_renderer import ABI_VERSION, find_native_library -from rosella_filterbank import DEFAULT_KERNEL_DATA, load_kernel_tables -from rosella_model import RosellaModel - -BLOCK_SAMPLES = 512 -INPUT_CHANNELS = 16 -OUTPUT_CHANNELS = 2 -HYBRID_BANDS = 77 - - -class NativeBinauralDsp: - def __init__(self, model: RosellaModel, *, library_path=None, - kernel_data: str | Path = DEFAULT_KERNEL_DATA): - self.library_path = find_native_library(library_path) - self._lib = ctypes.CDLL(str(self.library_path)) - self._bind() - version = int(self._lib.ejoc_abi_version()) - if version != ABI_VERSION: - raise RuntimeError( - f"native ABI mismatch: expected {ABI_VERSION}, got {version}") - self._handle = self._lib.ejoc_binaural_renderer_create() - if not self._handle: - raise RuntimeError("native binaural renderer creation failed") - try: - self._configure_kernels(kernel_data) - self._configure_room(model) - except Exception: - self.close() - raise - - def _bind(self): - void_p = ctypes.c_void_p - f64_p = ctypes.POINTER(ctypes.c_double) - i16_p = ctypes.POINTER(ctypes.c_int16) - u32_p = ctypes.POINTER(ctypes.c_uint32) - self._lib.ejoc_abi_version.argtypes = [] - self._lib.ejoc_abi_version.restype = ctypes.c_uint32 - self._lib.ejoc_binaural_renderer_create.argtypes = [] - self._lib.ejoc_binaural_renderer_create.restype = void_p - self._lib.ejoc_binaural_renderer_destroy.argtypes = [void_p] - self._lib.ejoc_binaural_renderer_destroy.restype = None - self._lib.ejoc_binaural_renderer_reset.argtypes = [void_p] - self._lib.ejoc_binaural_renderer_reset.restype = ctypes.c_int - self._lib.ejoc_binaural_renderer_last_error.argtypes = [void_p] - self._lib.ejoc_binaural_renderer_last_error.restype = ctypes.c_char_p - self._lib.ejoc_binaural_renderer_configure_kernels.argtypes = [ - void_p, f64_p, f64_p, i16_p, f64_p, ctypes.c_uint32, f64_p, f64_p] - self._lib.ejoc_binaural_renderer_configure_kernels.restype = ctypes.c_int - self._lib.ejoc_binaural_renderer_configure_room.argtypes = [ - void_p, ctypes.c_uint32, ctypes.c_uint32, u32_p, f64_p, - u32_p, f64_p, ctypes.c_uint32, f64_p, f64_p, f64_p, - ctypes.c_uint32, u32_p, f64_p, f64_p] - self._lib.ejoc_binaural_renderer_configure_room.restype = ctypes.c_int - self._lib.ejoc_binaural_renderer_process.argtypes = [ - void_p, f64_p, f64_p, f64_p, ctypes.c_double, f64_p] - self._lib.ejoc_binaural_renderer_process.restype = ctypes.c_int - - def _raise(self, operation, status): - message = self._lib.ejoc_binaural_renderer_last_error(self._handle) - detail = (message or b"").decode("utf-8", "replace") - raise RuntimeError( - f"native binaural renderer {operation} failed ({status}): {detail}") - - @staticmethod - def _f64_pointer(values): - return values.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) - - def _configure_kernels(self, kernel_data): - tables = load_kernel_tables(kernel_data) - qmf_analysis = np.ascontiguousarray( - tables["qmf_analysis_coefficients"], dtype=np.float64) - hybrid_low = np.ascontiguousarray( - tables["hybrid_analysis_low_kernel"], dtype=np.float64) - hybrid_indices = np.ascontiguousarray( - tables["hybrid_synthesis_indices"], dtype=np.int16) - hybrid_values = np.ascontiguousarray( - tables["hybrid_synthesis_values"], dtype=np.float64) - qmf_basis = np.ascontiguousarray( - tables["qmf_synthesis_basis"], dtype=np.float64) - qmf_taps = np.ascontiguousarray( - tables["qmf_synthesis_taps"], dtype=np.float64) - status = self._lib.ejoc_binaural_renderer_configure_kernels( - self._handle, - self._f64_pointer(qmf_analysis), - self._f64_pointer(hybrid_low), - hybrid_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int16)), - self._f64_pointer(hybrid_values), - len(hybrid_values), - self._f64_pointer(qmf_basis), - self._f64_pointer(qmf_taps), - ) - if status: - self._raise("configure_kernels", status) - - def _configure_room(self, model: RosellaModel): - if float(model.table_a_scalar) >= 0.5: - raise NotImplementedError("alternate table-A room mode") - bands = min(64, model.table_a_dimension) - allpass_delays = np.ascontiguousarray( - model.table_a_option_ids, dtype=np.uint32) - allpass_gains = np.ascontiguousarray( - model.table_a_option_values, dtype=np.float64) - fdn_delays = np.ascontiguousarray( - model.table_a_four_integers, dtype=np.uint32) - fdn_matrix = np.ascontiguousarray( - np.asarray(model.table_a_vector16, dtype=np.float64).reshape( - 4, 4, order="F")) - - filter8 = np.asarray( - model.table_a_filter_8x64_padded, dtype=np.float64).reshape(20, 4, 2, 4) - filter4 = np.asarray( - model.table_a_filter_4x64_padded, dtype=np.float64).reshape(20, 4, 4) - filter16 = np.asarray( - model.table_a_filter_16x64_padded, dtype=np.float64).reshape(20, 4, 4, 4) - feedback = np.empty((64, 4, 2), dtype=np.float64) - output_taps = np.empty((64, 4), dtype=np.float64) - output_matrix = np.empty((2, 64, 4, 2), dtype=np.float64) - for band in range(64): - group, lane = divmod(band, 4) - feedback[band, :, 0] = filter8[group, :, 0, lane] - feedback[band, :, 1] = filter8[group, :, 1, lane] - output_taps[band] = filter4[group, :, lane] - output_matrix[0, band, :, 0] = filter16[group, :, 0, lane] - output_matrix[0, band, :, 1] = filter16[group, :, 1, lane] - output_matrix[1, band, :, 0] = filter16[group, :, 2, lane] - output_matrix[1, band, :, 1] = filter16[group, :, 3, lane] - - extra_count = int(model.table_a_extra) - extra_delays = np.ascontiguousarray( - model.table_a_extra_indices, dtype=np.uint32) - extra_fields = np.empty((extra_count, 64, 2), dtype=np.float64) - extra_source = np.asarray( - model.table_a_extra_fields_padded, dtype=np.float64).reshape( - extra_count, 20, 2, 4) - for extra in range(extra_count): - for band in range(64): - group, lane = divmod(band, 4) - extra_fields[extra, band] = extra_source[extra, group, :, lane] - extra_matrices = np.empty((extra_count, 4, 4), dtype=np.float64) - for extra in range(extra_count): - extra_matrices[extra] = np.asarray( - model.table_a_extra_vectors[extra], dtype=np.float64).reshape( - 4, 4, order="F") - - null_u32 = ctypes.POINTER(ctypes.c_uint32)() - null_f64 = ctypes.POINTER(ctypes.c_double)() - status = self._lib.ejoc_binaural_renderer_configure_room( - self._handle, - bands, - len(allpass_delays), - allpass_delays.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)), - self._f64_pointer(allpass_gains), - fdn_delays.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)), - self._f64_pointer(fdn_matrix), - int(model.table_a_integer), - self._f64_pointer(feedback), - self._f64_pointer(output_taps), - self._f64_pointer(output_matrix), - extra_count, - (extra_delays.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)) - if extra_count else null_u32), - self._f64_pointer(extra_fields) if extra_count else null_f64, - self._f64_pointer(extra_matrices) if extra_count else null_f64, - ) - if status: - self._raise("configure_room", status) - - def reset(self): - if not self._handle: - raise RuntimeError("native binaural renderer is closed") - status = self._lib.ejoc_binaural_renderer_reset(self._handle) - if status: - self._raise("reset", status) - - def process_block(self, pcm16, gains, room_sends, output_gain=1.0): - if not self._handle: - raise RuntimeError("native binaural renderer is closed") - source = np.ascontiguousarray(pcm16, dtype=np.float64) - gain_values = np.asarray(gains) - sends = np.ascontiguousarray(room_sends, dtype=np.float64) - if source.shape != (BLOCK_SAMPLES, INPUT_CHANNELS): - raise ValueError(f"pcm16 block must be (512,16), got {source.shape}") - if gain_values.shape != (INPUT_CHANNELS, OUTPUT_CHANNELS, HYBRID_BANDS): - raise ValueError(f"gains must be (16,2,77), got {gain_values.shape}") - direct = np.ascontiguousarray( - gain_values, dtype=np.complex128).view(np.float64) - if sends.shape != (INPUT_CHANNELS,): - raise ValueError(f"room_sends must be (16,), got {sends.shape}") - output = np.empty((BLOCK_SAMPLES, OUTPUT_CHANNELS), dtype=np.float64) - status = self._lib.ejoc_binaural_renderer_process( - self._handle, - self._f64_pointer(source), - self._f64_pointer(direct), - self._f64_pointer(sends), - float(output_gain), - self._f64_pointer(output), - ) - if status: - self._raise("process", status) - return output - - def close(self): - handle = getattr(self, "_handle", None) - if handle: - self._lib.ejoc_binaural_renderer_destroy(handle) - self._handle = None diff --git a/src/emdf.py b/src/emdf.py index 80d26c1..370b4f6 100644 --- a/src/emdf.py +++ b/src/emdf.py @@ -73,6 +73,9 @@ class EmdfContainer: raw: bytes payloads: dict sample_offsets: dict + # payload_id -> group_id(未声明 groupid 的载荷为 None)。规格 §8.2/Table 56: + # 同一帧的 OAMD 与 JOC 载荷属于同一 group,凭此把一对载荷关联起来。 + group_ids: dict = None def _parse_at(data, start_bit): @@ -99,6 +102,7 @@ def _parse_at(data, start_bit): payloads = {} sample_offsets = {} + group_ids = {} terminated = False while reader.position + 5 <= body_end: payload_id = reader.read(5) @@ -115,7 +119,9 @@ def _parse_at(data, start_bit): if reader.read(1): variable_bits(reader, 11) # duration if reader.read(1): - variable_bits(reader, 2) # group id + group_ids[payload_id] = variable_bits(reader, 2) # group id + else: + group_ids[payload_id] = None if reader.read(1): reader.skip(8) # codec data @@ -140,7 +146,7 @@ def _parse_at(data, start_bit): total_bytes = 4 + length raw_reader = BitReader(data, start_bit, start_bit + total_bytes * 8) raw = raw_reader.read_bytes(total_bytes) - return EmdfContainer(start_bit, raw, payloads, sample_offsets) + return EmdfContainer(start_bit, raw, payloads, sample_offsets, group_ids) def _marker_offsets(data): @@ -162,13 +168,18 @@ def _marker_offsets(data): return sorted(offsets) -def find_joc_emdf(frame): +def find_joc_emdf(frame, usable=None, diagnostics=None, frame_number=None): """返回同步帧中唯一、顶层连续且包含 ID11/ID14 的 JOC EMDF 容器。 EMDF payload 是不透明字节串,其中可能自然出现另一个 ``0x5838``。若从这个 内嵌 marker 开始的后续随机位恰好也能通过容器语法探测,它仍不是一个独立的 transport 容器。因此,候选的起点一旦落在较早 JOC 容器的声明范围内,就只把 它记作内嵌伪候选,不参与“多个容器”的判定。 + + 一个同步帧里可能出现多个顶层容器(同一帧可能同时带两套 ID11/ID14)。 + ``usable`` 给出“这个容器能否作为本帧元数据”的分值(0 = 不是,越大越可信): + 只有唯一最高分时用它,其余记入 ``diagnostics``;没有候选或最高分并列仍按变体 + 报错,不猜。 """ matches = [] offsets = _marker_offsets(frame) @@ -216,6 +227,22 @@ def find_joc_emdf(frame): }) if len(top_level_matches) != 1: starts = [item.start_bit for item in top_level_matches] + if usable is not None: + scored = [(usable(item), item) for item in top_level_matches] + best = max(score for score, _item in scored) + winners = [item for score, item in scored if score == best] + if best > 0 and len(winners) == 1: + if diagnostics is not None: + diagnostics.append({ + "stage": "emdf", + "variant": "extra_container_ignored", + "frame": frame_number, + "message": "同步帧内有多个含 ID11/ID14 的 EMDF 容器,只有一个能作为本帧元数据", + "details": {"joc_container_start_bits": starts, + "used_start_bit": winners[0].start_bit, + "pair_score": best}, + }) + return winners[0] raise UnsupportedVariantError( "emdf_transport", "multiple_joc_containers", "同步帧中存在多个可用 JOC EMDF,当前无法自动选择", @@ -235,31 +262,184 @@ def parse_container(data): return container -def iter_eac3_frames(data): - """按 E-AC-3 ``frmsiz`` 遍历同步帧,拒绝静默重同步。""" +# AC-3 同步帧长度表(16-bit word),索引为 fscod(0=48 kHz、1=44.1 kHz、2=32 kHz) +# 再取 frmsizecod;与 A/52 的比特率表等价。 +_AC3_FRAME_WORDS = ( + (64, 64, 80, 80, 96, 96, 112, 112, 128, 128, 160, 160, 192, 192, 224, 224, + 256, 256, 320, 320, 384, 384, 448, 448, 512, 512, 640, 640, 768, 768, + 896, 896, 1024, 1024, 1152, 1152, 1280, 1280), + (69, 70, 87, 88, 104, 105, 121, 122, 139, 140, 174, 175, 208, 209, 243, 244, + 278, 278, 348, 348, 417, 418, 487, 487, 557, 557, 696, 696, 835, 835, + 975, 975, 1114, 1114, 1253, 1253, 1393, 1393), + (96, 96, 120, 120, 144, 144, 168, 168, 192, 192, 240, 240, 288, 288, 336, 336, + 384, 384, 480, 480, 576, 576, 672, 672, 768, 768, 960, 960, 1152, 1152, + 1344, 1344, 1536, 1536, 1728, 1728, 1920, 1920), +) + + +# E-AC-3 chanmap(TS 102 366 Annex E Table E2.5):字段的最高位对应 bit 0。 +CHANMAP_LOCATIONS = ( + "L", "C", "R", "Ls", "Rs", "Lc/Rc", "Lrs/Rrs", "Cs", "Ts", "Lsd/Rsd", + "Lw/Rw", "Vhl/Vhr", "Vhc", "Lts/Rts", "LFE2", "LFE", +) +# 单个 bit 展开为两个相邻声道的成对位置;展开顺序为先左后右。 +CHANMAP_PAIR_BITS = frozenset((5, 6, 9, 10, 11, 13)) +# A/52 acmod 对应的全频段声道位置(不含 LFE,按解码顺序)。 +ACMOD_LOCATIONS = { + 0: ("C",), # 1+1 双单声道 + 1: ("C",), + 2: ("L", "R"), + 3: ("L", "C", "R"), + 4: ("L", "R", "Cs"), + 5: ("L", "C", "R", "Cs"), + 6: ("L", "R", "Ls", "Rs"), + 7: ("L", "C", "R", "Ls", "Rs"), +} + + +def expand_chanmap(chanmap): + """按 Table E2.5 把 16 bit chanmap 展开为有序声道位置列表。""" + locations = [] + for bit in range(16): + if not (int(chanmap) >> (15 - bit)) & 1: + continue + locations.extend(CHANMAP_LOCATIONS[bit].split("/")) + return locations + + +@dataclass(frozen=True) +class SyncFrame: + """一个同步帧及其传输层(L2)字段。""" + + offset: int + size: int + codec: str # "ac3" 或 "eac3" + bsid: int + strmtyp: int # AC-3 没有子流类型,固定记为 0 + substreamid: int + acmod: int + lfeon: int + chanmap: object # 依赖子流的 16 bit chanmap;未声明为 None + + @property + def key(self): + """同一音频帧内唯一的子流标识。""" + return (self.codec, self.strmtyp, self.substreamid) + + +def _read_bits(data, start, count): + value = 0 + for index in range(start, start + count): + value = (value << 1) | ((data[index >> 3] >> (7 - (index & 7))) & 1) + return value + + +def sync_frame_at(data, offset): + """解析 offset 处同步帧的传输层头。 + + 区分 AC-3 与 E-AC-3 的依据是 bsid(bit 40 起 5 bit):bsid <= 8 为 AC-3, + 其帧长由 fscod/frmsizecod 的比特率表给出;bsid >= 16 为 E-AC-3,帧长由 + frmsiz 给出。依赖子流(strmtyp=1)在这里顺带解析出 chanmap。 + """ + if offset + 6 > len(data) or data[offset] != 0x0B or data[offset + 1] != 0x77: + return None + base = offset * 8 + bsid = _read_bits(data, base + 40, 5) + if bsid <= 8: + fscod = _read_bits(data, base + 32, 2) + frmsizecod = _read_bits(data, base + 34, 6) + if fscod > 2 or frmsizecod >= 38: + return None + size = _AC3_FRAME_WORDS[fscod][frmsizecod] * 2 + acmod = _read_bits(data, base + 48, 3) + bit = 51 + if acmod & 0x1 and acmod != 0x1: + bit += 2 # cmixlev + if acmod & 0x4: + bit += 2 # surmixlev + if acmod == 0x2: + bit += 2 # dsurmod + lfeon = _read_bits(data, base + bit, 1) + return SyncFrame(offset, size, "ac3", bsid, 0, 0, acmod, lfeon, None) + + strmtyp = _read_bits(data, base + 16, 2) + substreamid = _read_bits(data, base + 18, 3) + frmsiz = _read_bits(data, base + 21, 11) + # fscod==3 时的 fscod2 同样是 2 bit,因此 acmod 恒在 bit 36。 + acmod = _read_bits(data, base + 36, 3) + lfeon = _read_bits(data, base + 39, 1) + bit = 45 # bsid 之后 + bit += 5 # dialnorm + compre = _read_bits(data, base + bit, 1) + bit += 1 + if compre: + bit += 8 # compr + if acmod == 0: + bit += 5 # dialnorm2 + compr2 = _read_bits(data, base + bit, 1) + bit += 1 + if compr2: + bit += 8 + chanmap = None + if strmtyp == 1: + if _read_bits(data, base + bit, 1): + chanmap = _read_bits(data, base + bit + 1, 16) + return SyncFrame(offset, (frmsiz + 1) * 2, "eac3", bsid, strmtyp, substreamid, + acmod, lfeon, chanmap) + + +def iter_syncframes(data): + """按同步字长度遍历输入中的 AC-3 与 E-AC-3 同步帧。""" pos = 0 while pos < len(data): - if pos + 4 > len(data) or data[pos:pos + 2] != b"\x0b\x77": + frame = sync_frame_at(data, pos) + if frame is None: raise UnsupportedVariantError( "eac3_transport", "syncframe_header", - "E-AC-3 同步帧头无效或出现了未处理的子流排列", + "同步帧头无效或出现了未处理的子流排列", details={ "byte_offset": pos, "remaining_bytes": len(data) - pos, "next_16_bytes_hex": data[pos:pos + 16].hex(), }) - size = ((((data[pos + 2] & 7) << 8) | data[pos + 3]) + 1) * 2 - if pos + size > len(data): + if pos + frame.size > len(data): raise UnsupportedVariantError( "eac3_transport", "truncated_syncframe", - "E-AC-3 末帧长度超过输入剩余数据", + "末帧长度超过输入剩余数据", details={ "byte_offset": pos, - "declared_frame_bytes": size, + "codec": frame.codec, + "declared_frame_bytes": frame.size, "remaining_bytes": len(data) - pos, }) - yield data[pos:pos + size] - pos += size + yield frame + pos += frame.size + + +def audio_frame_groups(frames): + """把同步帧序列按音频帧分组。 + + 一个音频帧内每个 (编码, strmtyp, substreamid) 组合只出现一次,因此再次遇到 + 已出现过的组合即表示下一个音频帧开始。独立子流缺席(只有依赖子流)或核心是 + AC-3 时同样成立。 + """ + group = [] + keys = set() + for frame in frames: + if frame.key in keys: + yield group + group = [] + keys = set() + keys.add(frame.key) + group.append(frame) + if group: + yield group + + +def iter_eac3_frames(data): + """兼容入口:只产出同步帧字节。""" + for frame in iter_syncframes(data): + yield data[frame.offset:frame.offset + frame.size] def extract_index(eac3_path, output_dir, max_frames=None): diff --git a/src/joc_decode.py b/src/joc_decode.py index b883476..f902615 100644 --- a/src/joc_decode.py +++ b/src/joc_decode.py @@ -35,8 +35,10 @@ def _load_huff_tables(): H = _load_huff_tables() JOC_NUM_CHANNELS = {0: 5, 1: 7, 2: 7, 3: 5, 4: 7} # Table 33 -# band-0 的 21-tap DC 补偿只在 downmix 配置 3/4 下启用。 -DC_FILTER_DMX_CONFIGS = (3, 4) +# σ(k) = 1 当且仅当 k∈{3,4}:同一个开关同时门控 Ls/Rs 的 -j 旋转与 band-0 FIR。 +SIGMA_DMX_CONFIGS = (3, 4) +# band-0 的 21-tap DC 补偿只在 σ(k)=1 的 downmix 配置下启用。 +DC_FILTER_DMX_CONFIGS = SIGMA_DMX_CONFIGS JOC_NUM_BANDS = {0: 1, 1: 3, 2: 5, 3: 7, 4: 9, 5: 12, 6: 15, 7: 23} # Table 35 JOC_NUM_QUANT = {0: 96, 1: 192} # Table 51 # dense 的量化零点就是 nquant/2;sparse 的递推起点比它高 2 个量化步。 @@ -77,18 +79,33 @@ def get_huff_code(mode, typ, nch): else "joc_huff_code_fine_generic"] # MTX +def parse_joc_header(payload): + """只读 joc_header 的 3+6+3 bit,不做 Huffman 解码。 + + 跨层一致性校验(规格 §11.2)只需要这些字段,因此不必为校验再走一遍完整解析。 + """ + br = BR(payload) + out = { + "dmx_config_idx": br.bits(3), + "num_objects_bits": br.bits(6), + "ext_config_idx": br.bits(3), + } + out["n_channels"] = JOC_NUM_CHANNELS.get(out["dmx_config_idx"]) + if out["n_channels"] is None: + raise ValueError(f"未知的 JOC downmix 配置 {out['dmx_config_idx']}") + # 声明对象数超过 15 时截断到 15;截断本身另行记入 diagnostics。 + declared = out["num_objects_bits"] + 1 + out["objects_clamped"] = declared > 15 + out["n_objects"] = min(declared, 15) + return out + + def parse_joc(payload): """解析 id14 载荷(joc() 位流)。返回字段 dict + 解析后剩余位数。""" br = BR(payload) - out = {} - out["dmx_config_idx"] = br.bits(3) - out["num_objects_bits"] = br.bits(6) - out["ext_config_idx"] = br.bits(3) - n_objects = out["num_objects_bits"] + 1 - n_channels = JOC_NUM_CHANNELS.get(out["dmx_config_idx"]) - if n_channels is None: - raise ValueError(f"未知的 JOC downmix 配置 {out['dmx_config_idx']}") - out["n_objects"], out["n_channels"] = n_objects, n_channels + out = parse_joc_header(payload) + n_objects, n_channels = out["n_objects"], out["n_channels"] + br.p = 12 out["clipgain_x_bits"] = br.bits(3) out["clipgain_y_bits"] = br.bits(5) out["seq_count_bits"] = br.bits(10) diff --git a/src/joc_qmf.py b/src/joc_qmf.py index 220324c..b7f0d76 100644 --- a/src/joc_qmf.py +++ b/src/joc_qmf.py @@ -130,11 +130,12 @@ _SURROUND_DC_B = np.array([ _SURROUND_DC_C = _SURROUND_DC_B + 1j * _SURROUND_DC_A -def surround_post_frame(x, delay, dc_hist, apply_dc_filter=True): +def surround_post_frame(x, delay, dc_hist, apply_dc_filter=True, rotate=True): """处理 Ls/Rs 的 10 槽延迟、-j 旋转和 band-0 FIR。 - ``apply_dc_filter=False`` 时跳过 band-0 的 21-tap DC 补偿,只做延迟与 - -j 旋转;延迟线与 DC 历史仍照常推进,便于逐帧切换。 + ``rotate`` 与 ``apply_dc_filter`` 由同一个开关 σ(k) 门控(规格 §4): + σ(k)=0(k∈{0,1,2})时 c=3,4 退化为普通延迟,既不旋转也不做 FIR。 + 延迟线与 DC 历史无论开关如何都照常推进,便于逐帧切换。 """ src = np.asarray(x, dtype=np.complex128) qdelay = np.asarray(delay, dtype=np.complex128).copy() @@ -145,7 +146,7 @@ def surround_post_frame(x, delay, dc_hist, apply_dc_filter=True): for group in range(0, 24, 4): current = src[:, :, group:group + 4].transpose(0, 2, 1) queued = np.concatenate((qdelay, current), axis=1) - block = -1j * queued[:, :4, :] + block = -1j * queued[:, :4, :] if rotate else queued[:, :4, :].copy() qdelay = queued[:, 4:, :] dc_buf = np.concatenate((hist, current[:, :, 0]), axis=1) if apply_dc_filter: diff --git a/src/metadata.py b/src/metadata.py index 996af5d..8f3406b 100644 --- a/src/metadata.py +++ b/src/metadata.py @@ -6,26 +6,218 @@ import json from pathlib import Path from adm_atmos import q_to_adm_xyz +from bed_layout import EXTRA_PAIR_BY_CONFIG, extra_pair from evo_unpack import unpack_evolution -from emdf import EmdfError, find_joc_emdf, iter_eac3_frames, parse_container -from joc_decode import parse_joc -from oamd_bits import JocFieldState, frame_update_values +from emdf import (ACMOD_LOCATIONS, EmdfError, audio_frame_groups, expand_chanmap, + find_joc_emdf, iter_syncframes, parse_container) +from joc_decode import parse_joc, parse_joc_header +from oamd_bits import JocFieldState, frame_update_values, program_bed_channels from variant_error import UnsupportedVariantError, bytes_descriptor +# 规格 §11.2 校验 2:OAMD 标准 bed 掩码(Table 12)里的额外对标签。 +_BED_PAIR_GROUPS = ( + (frozenset(("RC_LB", "RC_RB")), ("Lrs", "Rrs")), + (frozenset(("RC_TFL", "RC_TFR")), ("Vhl", "Vhr")), + (frozenset(("RC_TBL", "RC_TBR")), ("Lts", "Rts")), +) + + +def _joc_pair_score(container): + """这个容器作为本帧 JOC 元数据的可信度(规格 §8.2 / Table 56)。 + + §8.2:OAMD 与 JOC 载荷放在同一个 EMDF 容器里;Table 56:两者的 groupid + 必须相同。同一个同步帧里可能出现多个含 ID11/ID14 的顶层容器,其中不构成一对的那个 + (ID14 声明的对象数超出其字节所能容纳的范围、ID11 未声明 groupid)既不是一对 + 合法载荷,也不可能是本帧的 JOC 参数。 + + 2 = 两个载荷 groupid 相同且 ID14 完整解析;1 = ID14 完整解析;0 = 其余。 + """ + payload = container.payloads.get(14) + if payload is None or 11 not in container.payloads: + return 0 + parses = True + try: + parse_joc(payload) + except (IndexError, ValueError): + parses = False + group_ids = getattr(container, "group_ids", None) or {} + first = group_ids.get(11) + second = group_ids.get(14) + associated = first is not None and second is not None and first == second + if associated and parses: + return 2 + return 1 if parses else 0 + + +def _transport_record(group): + """从一个音频帧的同步帧组提取规格 §11.2 需要的 L2 声明。""" + core_locations = () + core_lfe = False + chanmap = None + for sync in group: + if sync.codec == "ac3" or sync.strmtyp == 0: + core_locations = ACMOD_LOCATIONS.get(sync.acmod, ()) + core_lfe = bool(sync.lfeon) + elif sync.chanmap is not None: + chanmap = sync.chanmap + return { + "core_locations": tuple(core_locations), + "core_lfe": core_lfe, + "chanmap": chanmap, + "syncframes": [ + { + "offset": sync.offset, + "size": sync.size, + "codec": sync.codec, + "strmtyp": sync.strmtyp, + "substreamid": sync.substreamid, + "acmod": sync.acmod, + "lfeon": sync.lfeon, + } + for sync in group + ], + } + + +def _declared_bed_pairs(bed_channels): + """标准 bed 掩码里声明的"额外那一对",按 Table 12 的标签判断。""" + labels = {name for instance in bed_channels for name in instance} + pairs = [] + for members, pair in _BED_PAIR_GROUPS: + if members.issubset(labels): + pairs.append(pair) + return pairs + + +def check_cross_layer(parsed, *, frame, transport=None, bed_channels=None, + diagnostics=None): + """规格 §11.2 的跨层一致性校验。 + + 校验 1/2(额外那对的身份)不一致时只记 diagnostics:通道身份不进 DSP + (§10.1),床映射按 chanmap 给出的通道码做,音频与 k 一致时逐样本相同。 + 校验 3(核心通道数)与 joc_ext_config_idx 会改变 DSP 输入,仍按变体报错。 + """ + config_idx = parsed["dmx_config_idx"] + n_channels = parsed["n_channels"] + expected_pair = EXTRA_PAIR_BY_CONFIG.get(config_idx) + if parsed["ext_config_idx"] != 0: + raise UnsupportedVariantError( + "joc", "ext_config_idx_unsupported", + "joc_ext_config_idx 取到当前未映射的值", + frame=frame, + details={ + "downmix_config": config_idx, + "extension_config_idx": parsed["ext_config_idx"], + "payload": bytes_descriptor(parsed.get("payload", b"")), + }) + if diagnostics is not None and parsed.get("objects_clamped"): + diagnostics.append({ + "stage": "joc", + "variant": "num_objects_clamped", + "frame": frame, + "message": "对象数超过 15,已截断", + "details": {"declared": parsed["num_objects_bits"] + 1, + "used": parsed["n_objects"]}, + }) + + if transport is not None: + chanmap = transport["chanmap"] + core_locations = transport["core_locations"] + details = { + "config_idx": config_idx, + "n_channels": n_channels, + "syncframes": transport["syncframes"], + } + if n_channels == 7: + if not core_locations or chanmap is None: + raise UnsupportedVariantError( + "eac3", "core_extension_pair", + "7 通道配置需要独立子流与依赖子流 chanmap 同时在场", + frame=frame, details=details) + pair = extra_pair(core_locations, chanmap) + details["chanmap"] = chanmap + details["chanmap_locations"] = expand_chanmap(chanmap) + details["extra_pair"] = list(pair) + if pair != expected_pair: + if diagnostics is not None: + diagnostics.append({ + "stage": "eac3", + "variant": "chanmap_cfg_mismatch", + "frame": frame, + "message": "依赖子流 chanmap 声明的额外对与 joc_dmx_config_idx 不一致," + "按 chanmap 的通道码映射,音频不受影响", + "details": {**details, "expected_pair": list(expected_pair or ())}, + }) + elif chanmap is not None and extra_pair(core_locations, chanmap): + raise UnsupportedVariantError( + "eac3", "core_channel_count_mismatch", + "5 通道配置的依赖子流声明了额外通道", + frame=frame, + details={**details, "chanmap_locations": expand_chanmap(chanmap)}) + # 校验 3:核心通道数(子流声明汇总,去掉 LFE) + declared = set(core_locations) + if chanmap is not None: + declared |= set(expand_chanmap(chanmap)) + declared -= {"LFE", "LFE2"} + details["declared_full_band_channels"] = len(declared) + if len(declared) != n_channels: + raise UnsupportedVariantError( + "eac3", "core_channel_count_mismatch", + "子流声明的全频段通道数与 joc_dmx_config_idx 不一致", + frame=frame, details=details) + + if bed_channels is not None: + declared_pairs = _declared_bed_pairs(bed_channels) + if declared_pairs and (expected_pair is None or + any(pair != expected_pair for pair in declared_pairs)): + if diagnostics is not None: + diagnostics.append({ + "stage": "oamd", + "variant": "bed_mask_cfg_mismatch", + "frame": frame, + "message": "OAMD 标准 bed 掩码声明的额外对与 joc_dmx_config_idx 不一致," + "只影响 bed 槽位记录,音频不受影响", + "details": { + "config_idx": config_idx, + "bed_channels": [list(instance) for instance in bed_channels], + "declared_pairs": [list(pair) for pair in declared_pairs], + "expected_pair": list(expected_pair or ()), + }, + }) + + class DirectPayloadIndex: """One-pass in-memory view of contiguous EMDF containers in an E-AC-3 file. The optional cache directory keeps the existing ``frames.csv + emdf/`` contract, but normal processing reuses the containers already parsed during the scan instead of reading and parsing thousands of small files again. + + 音频帧 = 同一 (编码, strmtyp, substreamid) 组合出现一次为止的同步帧组; + JOC 通常只在依赖子流里,核心子流(AC-3 或 E-AC-3 独立子流)没有 EMDF。 """ - def __init__(self, rows, subpayloads, sample_offsets, directory=None): + def __init__(self, rows, subpayloads, sample_offsets, directory=None, + transports=None, diagnostics=None, config_idx=None, n_channels=None): self.rows = rows self._subpayloads = subpayloads self._sample_offsets = sample_offsets self.directory = Path(directory) if directory is not None else None + self.transports = transports if transports is not None else [] + self.diagnostics = diagnostics if diagnostics is not None else [] + self.config_idx = config_idx + self.n_channels = n_channels + + def transport(self, row): + return self.transports[int(row["frame"])] if self.transports else None + + def subpayloads(self, row): + return self._subpayloads[int(row["frame"])] + + def subpayload_sample_offset(self, row, payload_id): + offsets = self._sample_offsets[int(row["frame"])] + return 0 if offsets is None else int(offsets.get(payload_id, 0)) @classmethod def from_eac3(cls, eac3_path, max_frames=None, cache_dir=None): @@ -34,24 +226,68 @@ class DirectPayloadIndex: if cache_dir is not None: emdf_dir = cache_dir / "emdf" emdf_dir.mkdir(parents=True, exist_ok=True) + data = Path(eac3_path).read_bytes() rows = [] payloads = [] offsets = [] - frames = iter_eac3_frames(Path(eac3_path).read_bytes()) - for frame_number, frame in enumerate(frames): + transports = [] + diagnostics = [] + config_idx = None + n_channels = None + missing_joc_frames = 0 + groups = audio_frame_groups(iter_syncframes(data)) + for frame_number, group in enumerate(groups): if max_frames is not None and frame_number >= max_frames: break - try: - container = find_joc_emdf(frame) - except UnsupportedVariantError as exc: - exc.add_context(frame=frame_number, details={"syncframe_bytes": len(frame)}) - raise - except EmdfError as exc: - raise UnsupportedVariantError( - "emdf_transport", "container_syntax", - "EMDF 容器语法无法解析", - frame=frame_number, - details={"syncframe_bytes": len(frame), "parser_error": str(exc)}) from exc + transport = _transport_record(group) + transports.append(transport) + container = None + for sync in group: + chunk = data[sync.offset:sync.offset + sync.size] + try: + container = find_joc_emdf(chunk, usable=_joc_pair_score, + diagnostics=diagnostics, + frame_number=frame_number) + except UnsupportedVariantError as exc: + if exc.variant != "no_contiguous_joc_container": + exc.add_context(frame=frame_number, + details={"syncframe_bytes": len(chunk)}) + raise + continue + except EmdfError as exc: + raise UnsupportedVariantError( + "emdf_transport", "container_syntax", + "EMDF 容器语法无法解析", + frame=frame_number, + details={"syncframe_bytes": len(chunk), + "parser_error": str(exc)}) from exc + break + if container is None: + missing_joc_frames += 1 + rows.append({ + "frame": frame_number, + "emdf_hash": "", + "emdf_size": 0, + "emdf_start_bit": "", + "payload_ids": "", + "error": "no_joc", + }) + payloads.append(None) + offsets.append(None) + continue + + if 14 in container.payloads: + parsed = parse_joc_header(container.payloads[14]) + parsed["payload"] = container.payloads[14] + check_cross_layer(parsed, frame=frame_number, transport=transport, + bed_channels=( + program_bed_channels(container.payloads[11]) + if 11 in container.payloads else None), + diagnostics=diagnostics) + if config_idx is None: + config_idx = parsed["dmx_config_idx"] + n_channels = parsed["n_channels"] + digest = hashlib.sha256(container.raw).hexdigest() if emdf_dir is not None: target = emdf_dir / f"{digest}.bin" @@ -71,19 +307,27 @@ class DirectPayloadIndex: print(f"[metadata] {frame_number + 1} frames", flush=True) if not rows: raise EmdfError("E-AC-3 输入中没有可处理的同步帧") + if config_idx is None: + raise UnsupportedVariantError( + "emdf_transport", "no_contiguous_joc_container", + "输入中没有找到可连续解析且同时包含 ID11/ID14 的 EMDF 容器", + details={"audio_frames": len(rows), "syncframe_bytes": len(data)}) + if missing_joc_frames: + diagnostics.append({ + "stage": "eac3", + "variant": "joc_frame_missing", + "frame": None, + "message": "部分音频帧没有 JOC 元数据,这些帧的对象通道静音", + "details": {"frames": missing_joc_frames, "total": len(rows)}, + }) if cache_dir is not None: with (cache_dir / "frames.csv").open("w", encoding="utf-8", newline="") as fp: fields = ("frame", "emdf_hash", "emdf_size", "emdf_start_bit", "payload_ids", "error") writer = csv.DictWriter(fp, fieldnames=fields) writer.writeheader() writer.writerows(rows) - return cls(rows, payloads, offsets, cache_dir) - - def subpayloads(self, row): - return self._subpayloads[int(row["frame"])] - - def subpayload_sample_offset(self, row, payload_id): - return int(self._sample_offsets[int(row["frame"])].get(payload_id, 0)) + return cls(rows, payloads, offsets, cache_dir, transports, diagnostics, + config_idx, n_channels) def __len__(self): return len(self.rows) @@ -194,6 +438,10 @@ def inspect(index, limit=None, print_frames=False): "元数据容器无法解析", frame=frame_number, details={"exception_type": type(exc).__name__, "parser_error": str(exc)}) from exc + if subs is None: + # 该音频帧没有任何 JOC EMDF(例如编码器未写首帧元数据):对象静音。 + frame_records.append({"frame": frame_number, "no_joc": True}) + continue if 11 not in subs or 14 not in subs: raise UnsupportedVariantError( "emdf_payloads", "missing_required_payload", @@ -246,10 +494,10 @@ def inspect(index, limit=None, print_frames=False): "parser_error": str(exc), "repair_hint": "检查 JOC header、对象数、参数带、Huffman 或扩展字段", }) from exc - if parsed["n_channels"] != 5 or parsed["n_objects"] > 15: + if parsed["n_channels"] not in (5, 7): raise UnsupportedVariantError( "joc", "unsupported_configuration", - "JOC 核心通道数或对象数超出当前渲染器范围", + "JOC 核心通道数超出当前渲染器范围", frame=frame_number, details={ "downmix_config": parsed["dmx_config_idx"], @@ -258,6 +506,10 @@ def inspect(index, limit=None, print_frames=False): "extension_config": parsed["ext_config_idx"], "payload": bytes_descriptor(subs[14]), }) + check_cross_layer( + parsed, frame=frame_number, + transport=(index.transport(row) if hasattr(index, "transport") else None), + bed_channels=program_bed_channels(subs[11])) rec = _frame_record(frame_number, parsed, oamd) configs[(parsed["dmx_config_idx"], parsed["ext_config_idx"], parsed["n_channels"], parsed["n_objects"])] += 1 diff --git a/src/native_renderer.py b/src/native_renderer.py deleted file mode 100644 index 5982cb2..0000000 --- a/src/native_renderer.py +++ /dev/null @@ -1,293 +0,0 @@ -"""ctypes bridge for the dependency-free MSVC C++ JOC DSP core. - -Metadata parsing intentionally stays in Python. One C call consumes a complete -1536-sample frame, so Python is not involved in the hot 24-timeslot x 15-object -DSP loops. -""" -from __future__ import annotations - -import ctypes -import os -from pathlib import Path -import sys - -import numpy as np - -from evo_unpack import unpack_evolution -from joc_decode import DC_FILTER_DMX_CONFIGS, dequantize, diff_decode, parse_joc - -FRAME_SAMPLES = 1536 -MAX_OBJECTS = 15 -MAX_DPOINTS = 2 -CORE_CHANNELS = 5 -MAX_BANDS = 23 -ABI_VERSION = 1 - - -class NativeBackendUnavailable(RuntimeError): - pass - - -def native_library_filename(): - if sys.platform == "win32": - return "eac3joc_core.dll" - if sys.platform == "darwin": - return "libeac3joc_core.dylib" - if sys.platform.startswith("linux"): - return "libeac3joc_core.so" - raise NativeBackendUnavailable(f"unsupported native platform: {sys.platform}") - - -def _candidate_libraries(): - override = os.environ.get("EAC3JOC_NATIVE_LIBRARY") - if override: - yield Path(override).expanduser() - root = Path(__file__).resolve().parent.parent - yield root / "lib" / native_library_filename() - - -def find_native_library(explicit=None): - if explicit is not None: - path = Path(explicit).expanduser().resolve() - if not path.is_file(): - raise NativeBackendUnavailable(f"native library not found: {path}") - return path - checked = [] - for item in _candidate_libraries(): - path = item.resolve() - checked.append(str(path)) - if path.is_file(): - return path - raise NativeBackendUnavailable("native library not found; checked: " + "; ".join(checked)) - - -def default_native_threads(): - override = os.environ.get("EAC3JOC_NATIVE_THREADS") - if override is not None: - value = int(override) - if value < 1: - raise ValueError("EAC3JOC_NATIVE_THREADS must be at least 1") - return min(value, MAX_OBJECTS) - return 2 if (os.cpu_count() or 1) >= 4 else 1 - -def _load_library(path): - lib = ctypes.CDLL(str(path)) - float_p = ctypes.POINTER(ctypes.c_float) - u8_p = ctypes.POINTER(ctypes.c_uint8) - double_p = ctypes.POINTER(ctypes.c_double) - - lib.ejoc_abi_version.argtypes = [] - lib.ejoc_abi_version.restype = ctypes.c_uint32 - lib.ejoc_build_info.argtypes = [] - lib.ejoc_build_info.restype = ctypes.c_char_p - lib.ejoc_renderer_create.argtypes = [] - lib.ejoc_renderer_create.restype = ctypes.c_void_p - lib.ejoc_renderer_destroy.argtypes = [ctypes.c_void_p] - lib.ejoc_renderer_destroy.restype = None - lib.ejoc_renderer_reset.argtypes = [ctypes.c_void_p] - lib.ejoc_renderer_reset.restype = ctypes.c_int - lib.ejoc_renderer_set_threads.argtypes = [ctypes.c_void_p, ctypes.c_uint32] - lib.ejoc_renderer_set_threads.restype = ctypes.c_int - if hasattr(lib, "ejoc_renderer_set_dc_filter"): - lib.ejoc_renderer_set_dc_filter.argtypes = [ctypes.c_void_p, ctypes.c_uint32] - lib.ejoc_renderer_set_dc_filter.restype = ctypes.c_int - lib.ejoc_renderer_thread_count.argtypes = [ctypes.c_void_p] - lib.ejoc_renderer_thread_count.restype = ctypes.c_uint32 - lib.ejoc_renderer_last_error.argtypes = [ctypes.c_void_p] - lib.ejoc_renderer_last_error.restype = ctypes.c_char_p - lib.ejoc_renderer_process.argtypes = [ - ctypes.c_void_p, - float_p, - float_p, - ctypes.c_uint32, - u8_p, - u8_p, - u8_p, - u8_p, - double_p, - ctypes.c_double, - ctypes.c_float, - ctypes.c_float, - float_p, - ] - lib.ejoc_renderer_process.restype = ctypes.c_int - abi = int(lib.ejoc_abi_version()) - if abi != ABI_VERSION: - raise NativeBackendUnavailable(f"native ABI mismatch: library={abi}, Python={ABI_VERSION}") - return lib - - -class NativeJocRenderer: - """Stateful whole-frame native DSP renderer with the Python renderer API shape.""" - - def __init__(self, output_scale=1.0, library_path=None, threads=None): - self.output_scale = np.float32(output_scale) - if not np.isfinite(self.output_scale): - raise ValueError("output_scale must be finite") - self.library_path = find_native_library(library_path) - self._lib = _load_library(self.library_path) - self._handle = self._lib.ejoc_renderer_create() - if not self._handle: - raise MemoryError("ejoc_renderer_create failed") - requested_threads = default_native_threads() if threads is None else int(threads) - if requested_threads < 1: - raise ValueError("threads must be at least 1") - result = self._lib.ejoc_renderer_set_threads(self._handle, requested_threads) - if result: - self._raise_native("set_threads", result) - self.threads = int(self._lib.ejoc_renderer_thread_count(self._handle)) - self._n_bands = np.zeros(MAX_OBJECTS, dtype=np.uint8) - self._n_dpoints = np.zeros(MAX_OBJECTS, dtype=np.uint8) - self._slope_idx = np.zeros(MAX_OBJECTS, dtype=np.uint8) - self._offset_ts = np.zeros((MAX_OBJECTS, MAX_DPOINTS), dtype=np.uint8) - self._dq = np.zeros( - (MAX_OBJECTS, MAX_DPOINTS, CORE_CHANNELS, MAX_BANDS), - dtype=np.float64, - ) - self._output = np.zeros((16, FRAME_SAMPLES), dtype=np.float32) - - @property - def build_info(self): - value = self._lib.ejoc_build_info() - return value.decode("utf-8", "replace") if value else "" - - @staticmethod - def decode_payload(payload_bytes): - subs, _ = unpack_evolution(payload_bytes, loose=True) - return NativeJocRenderer.decode_subpayloads(subs) - - @staticmethod - def decode_subpayloads(subs): - if 14 not in subs: - raise ValueError("EMDF missing ID14/JOC") - out = parse_joc(subs[14]) - mix_q = diff_decode(out) - mix_dq = dequantize(out, mix_q) - return out, mix_q, mix_dq - - def reset(self): - self._require_open() - result = self._lib.ejoc_renderer_reset(self._handle) - if result: - self._raise_native("reset", result) - - def close(self): - handle = getattr(self, "_handle", None) - if handle: - self._lib.ejoc_renderer_destroy(handle) - self._handle = None - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc, tb): - self.close() - - def __del__(self): - try: - self.close() - except Exception: - pass - - def _require_open(self): - if not self._handle: - raise RuntimeError("native renderer is closed") - - def _raise_native(self, operation, code): - raw = self._lib.ejoc_renderer_last_error(self._handle) - detail = raw.decode("utf-8", "replace") if raw else "unknown native error" - raise RuntimeError(f"native {operation} failed ({code}): {detail}") - - def _pack_frame(self, out, mix_dq): - if out["n_channels"] != CORE_CHANNELS: - raise ValueError(f"native core requires 5 JOC channels, got {out['n_channels']}") - if out["n_objects"] > MAX_OBJECTS: - raise ValueError(f"native core supports at most 15 objects, got {out['n_objects']}") - self._n_bands.fill(0) - self._n_dpoints.fill(0) - self._slope_idx.fill(0) - self._offset_ts.fill(0) - mask = 0 - for object_index, info in enumerate(out["objs"]): - if not info["present"]: - continue - bands = int(info["n_bands"]) - points = int(info["n_dpoints"]) - if bands > MAX_BANDS or points > MAX_DPOINTS: - raise ValueError(f"native descriptor out of range: bands={bands}, points={points}") - values = np.asarray(mix_dq[object_index], dtype=np.float64) - expected = (points, CORE_CHANNELS, bands) - if values.shape != expected: - raise ValueError(f"object {object_index} dq shape {values.shape}, expected {expected}") - mask |= 1 << object_index - self._n_bands[object_index] = bands - self._n_dpoints[object_index] = points - self._slope_idx[object_index] = int(info["slope_idx"]) - offsets = info.get("offset_ts", ()) - self._offset_ts[object_index, :len(offsets)] = offsets - self._dq[object_index, :points, :, :bands] = values - return mask - - def _set_dc_filter(self, dmx_config_idx): - """band-0 的 21-tap DC 补偿只在 downmix 配置 3/4 下启用。""" - enabled = dmx_config_idx in DC_FILTER_DMX_CONFIGS - setter = getattr(self._lib, "ejoc_renderer_set_dc_filter", None) - if setter is None: - if not enabled: - raise RuntimeError( - "native library has no ejoc_renderer_set_dc_filter; rebuild " - "eac3joc_core to disable the band-0 DC filter for " - f"dmx_config_idx={dmx_config_idx}") - return - result = setter(self._handle, 1 if enabled else 0) - if result != 0: - self._raise_native("set_dc_filter", result) - - def render_frame(self, payload_bytes, bed5_pcm, lfe_pcm=None): - subs, _ = unpack_evolution(payload_bytes, loose=True) - return self.render_subpayloads(subs, bed5_pcm, lfe_pcm) - - def render_subpayloads(self, subs, bed5_pcm, lfe_pcm=None): - self._require_open() - out, _, mix_dq = self.decode_subpayloads(subs) - object_mask = self._pack_frame(out, mix_dq) - self._set_dc_filter(out["dmx_config_idx"]) - bed5 = np.ascontiguousarray(bed5_pcm, dtype=np.float32) - if bed5.shape != (CORE_CHANNELS, FRAME_SAMPLES): - raise ValueError(f"core PCM shape must be (5,1536), got {bed5.shape}") - if lfe_pcm is None: - lfe = None - lfe_ptr = ctypes.POINTER(ctypes.c_float)() - else: - lfe = np.ascontiguousarray(lfe_pcm, dtype=np.float32) - if lfe.shape != (FRAME_SAMPLES,): - raise ValueError(f"LFE shape must be (1536,), got {lfe.shape}") - lfe_ptr = lfe.ctypes.data_as(ctypes.POINTER(ctypes.c_float)) - - result = self._lib.ejoc_renderer_process( - self._handle, - bed5.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), - lfe_ptr, - object_mask, - self._n_bands.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)), - self._n_dpoints.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)), - self._slope_idx.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)), - self._offset_ts.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)), - self._dq.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), - float(out["clipgain"]), - ctypes.c_float(0.0625), - ctypes.c_float(self.output_scale), - self._output.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), - ) - if result: - self._raise_native("process", result) - return self._output, None - - -def native_available(library_path=None): - try: - path = find_native_library(library_path) - lib = _load_library(path) - return True, str(path), (lib.ejoc_build_info() or b"").decode("utf-8", "replace") - except Exception as exc: - return False, None, str(exc) diff --git a/src/oamd_bits.py b/src/oamd_bits.py index 011fea5..d571d71 100644 --- a/src/oamd_bits.py +++ b/src/oamd_bits.py @@ -458,6 +458,39 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa return object_element, elements +def program_bed_channels(bits_one): + """只走 OAMD 头与 program_assignment(),返回声明为 bed 的声道标签列表。 + + 跨层一致性校验(规格 §11.2 校验 2)只需要这一段;完整帧解析见 + :func:`frame_update`,它还要继续解析全部对象 element。 + """ + bits, raw_payload = _payload_bits(bits_one) + try: + version = bits[0] << 1 | bits[1] + except IndexError: + raise UnsupportedVariantError( + "oamd", "header_truncated", + "OAMD payload 不足以容纳 header", + details={"payload_bits": len(bits), + "payload": bytes_descriptor(raw_payload)}) from None + reader = _BitReader(bits, 2) + try: + if version == 3: + version += reader.read(3) + object_count_bits = reader.read(5) + if object_count_bits == 0x1F: + object_count_bits += reader.read(7) + program = _parse_program_assignment(reader, raw_payload) + except UnsupportedVariantError: + raise + except ValueError as exc: + raise _syntax_error( + "header_truncated", "OAMD header 字段越界或不完整", + raw_payload, exc, payload_bits=len(bits)) from exc + return [list(assignment["channels"]) + for assignment in program["bed_assignments"]] + + def frame_update(bits_one): """单帧 OAMD → 位置字段增量及其 sample offset/ramp duration。""" bits, raw_payload = _payload_bits(bits_one) diff --git a/src/oamd_tracks.py b/src/oamd_tracks.py index 746fb17..00742b3 100644 --- a/src/oamd_tracks.py +++ b/src/oamd_tracks.py @@ -212,7 +212,7 @@ def build_adm_tracks(index, frames=None, rate=48000, frame_samples=1536, for seq, row in enumerate(frames): subs = index.subpayloads(row) timing = None - if 11 in subs: + if subs is not None and 11 in subs: timing = frame_update(subs[11]) state.apply(timing["values"]) diff --git a/src/renderer.py b/src/renderer.py index 516ba89..eece4c7 100644 --- a/src/renderer.py +++ b/src/renderer.py @@ -1,7 +1,7 @@ """把 E-AC-3 核心 PCM 和 JOC 元数据重建为 LFE 加 15 路对象 PCM。 管线(每帧): - 核心 5.1 PCM → QMF 分析 x → 填充器 z = Σ m·x(每对象) + 核心 PCM(N=5 或 7 路全频段通道,按 k 查表)→ QMF 分析 x → 填充器 z = Σ m·x(每对象) → 对象时域组装(前步、64 点 FFT、旋转、合成窗)→ ×16 + clamp → 对象波形 × joc_clipgain + LFE 专用 1217-sample 环形延迟 @@ -13,7 +13,7 @@ output_scale: """ import numpy as np -from joc_decode import DC_FILTER_DMX_CONFIGS, parse_joc, diff_decode, dequantize +from joc_decode import SIGMA_DMX_CONFIGS, parse_joc, diff_decode, dequantize from joc_qmf import (N, QMF5_WINDOW, qmf_analysis_frame, surround_post_frame, interp_matrix) from evo_unpack import unpack_evolution @@ -29,10 +29,12 @@ class JocRenderer: self.output_scale = np.float32(output_scale) self._prev = {} # 每对象插值 prev(m 的帧末 sb 值) self._synthesis_state = np.zeros((15, 640), dtype=np.float64) - # 分析 QMF:五路各自保留 9×64 FIFO;L/R/C 另有 10-timeslot 延迟。 - self._analysis_fifo = np.zeros((5, 9, 64), dtype=np.float64) - self._analysis_delay = np.zeros((3, 10, 64), dtype=np.float32) - self._analysis_phase = np.float32(0.0625) + # 分析 QMF:最多七路,各保留 9×64 FIFO。L/R/C 与 N=7 时的第 6、7 路 + # (额外那一对)在这里做 10 时隙延迟;Ls/Rs 的同一延迟在环绕通路内完成。 + self._analysis_fifo = np.zeros((7, 9, 64), dtype=np.float64) + self._analysis_delay = np.zeros((5, 10, 64), dtype=np.float32) + # 哨兵:复位后(或整条流的第一帧)直接用新标量,不做帧内过渡。 + self._analysis_phase = np.float32(-0.5) self._surround_qmf_delay = np.zeros((2, 10, 64), dtype=np.complex128) self._surround_dc_hist = np.zeros((2, 20), dtype=np.complex128) # ch0/LFE 使用循环历史,读指针恒落后写指针 1217 个采样。 @@ -41,6 +43,13 @@ class JocRenderer: self._last_x = None # 仅供逐槽诊断读取,不参与状态推进 self._rot = None # 每带旋转表 + def held_analysis_scalar(self): + """当前分析端标量;还没有过 JOC 帧时用结构常数 1/16。""" + value = self._analysis_phase + if value <= 0.0 and value != 0.0: # 哨兵 + return np.float32(0.0625) + return value + @staticmethod def decode_payload(payload_bytes): """id14 载荷 → (parse 结果, mix_q, mix_dq)。""" @@ -60,36 +69,45 @@ class JocRenderer: mix_dq = dequantize(out, mix_q) return out, mix_q, mix_dq - def qmf_x(self, bed5, phase_new=0.0625, apply_dc_filter=True): - """核心 5ch PCM → 对象矩阵使用的复数 QMF ``x``。 + def qmf_x(self, bed_pcm, phase_new=0.0625, apply_dc_filter=True, rotate=True): + """核心 N ch PCM → 对象矩阵使用的复数 QMF ``x``。 - 先以 float32 对当前帧应用 phase;phase 变化时仅前 256 个样本从旧值 - 线性过渡。缩放后的 L/R/C 延迟 10 槽,Ls/Rs 不延迟,再进入分析 QMF。 - ``apply_dc_filter`` 控制 Ls/Rs band-0 的 21-tap DC 补偿。 + phase_new 是分析端标量:结构常数 1/16 × 本帧 joc_clipgain。先以 float32 + 对当前帧应用它;相比上一帧变化时仅前 256 个样本从旧值线性过渡。缩放后的 + L/R/C 与 N=7 时的第 6、7 路延迟 10 槽,Ls/Rs 不延迟,再进入分析 QMF; + 出口的 ×16 抵消 1/16,净效果是 ×joc_clipgain。``apply_dc_filter`` 与 ``rotate`` 是规格 §4 的同一个 + σ(k) 开关,控制 Ls/Rs 的 band-0 21-tap DC 补偿与 -j 旋转。 """ - pcm = np.asarray(bed5, dtype=np.float32) - if pcm.shape != (5, 1536): - raise ValueError(f"核心 5ch 帧应为 (5,1536),实际 {pcm.shape}") + pcm = np.asarray(bed_pcm, dtype=np.float32) + n_channels = pcm.shape[0] if pcm.ndim == 2 else 0 + if n_channels not in (5, 7) or pcm.shape != (n_channels, 1536): + raise ValueError(f"核心帧应为 (5,1536) 或 (7,1536),实际 {pcm.shape}") new_phase = np.float32(phase_new) old_phase = self._analysis_phase + if old_phase <= 0.0 and old_phase != 0.0: # 哨兵:首帧/复位后不做过过渡 + old_phase = new_phase gains = np.full(1536, new_phase, dtype=np.float32) if old_phase != new_phase: step = np.float32((new_phase - old_phase) / np.float32(256.0)) gains[:256] = old_phase + np.arange(256, dtype=np.float32) * step - scaled = np.multiply(pcm, gains[None, :], dtype=np.float32).reshape(5, 24, 64) + scaled = np.multiply(pcm, gains[None, :], dtype=np.float32).reshape( + n_channels, 24, 64) + # 走外部延迟线的通道:前级 L/R/C,以及 N=7 时额外那一对(索引 5、6)。 + plain = [0, 1, 2] + ([5, 6] if n_channels == 7 else []) blocks = np.empty_like(scaled) - for ch in range(3): - delayed = np.concatenate((self._analysis_delay[ch], scaled[ch]), axis=0) + for lane, ch in enumerate(plain): + delayed = np.concatenate((self._analysis_delay[lane], scaled[ch]), axis=0) blocks[ch] = delayed[:24] - self._analysis_delay[ch] = delayed[24:] - blocks[3:] = scaled[3:] + self._analysis_delay[lane] = delayed[24:] + blocks[3:5] = scaled[3:5] - x, self._analysis_fifo = qmf_analysis_frame(self._analysis_fifo, blocks) + fifo = self._analysis_fifo[:n_channels] + x, self._analysis_fifo[:n_channels] = qmf_analysis_frame(fifo, blocks) self._analysis_phase = new_phase - x[3:], self._surround_qmf_delay, self._surround_dc_hist = surround_post_frame( - x[3:], self._surround_qmf_delay, self._surround_dc_hist, - apply_dc_filter=apply_dc_filter) + x[3:5], self._surround_qmf_delay, self._surround_dc_hist = surround_post_frame( + x[3:5], self._surround_qmf_delay, self._surround_dc_hist, + apply_dc_filter=apply_dc_filter, rotate=rotate) return x def object_z(self, out, mix_dq, x): @@ -234,29 +252,49 @@ class JocRenderer: self._lfe_delay = delayed[-1217:].copy() return out - def render_frame(self, payload_bytes, bed5_pcm, lfe_pcm=None): - """payload_bytes = evolution 载荷(含 id14 JOC);bed5_pcm = 5×1536 核心 PCM。 + def render_missing_frame(self, bed_pcm, lfe_pcm=None, config_idx=3): + """没有 JOC 元数据的音频帧:对象通道静音,只保留 LFE 旁通。 + + QMF 分析与对象合成状态照常推进,否则后续帧的时间轴会整体少一格。 + """ + sigma = config_idx in SIGMA_DMX_CONFIGS + # 本帧没有 JOC 元数据,分析端标量保持上一帧的值(槽位不被改写)。 + x = self.qmf_x(bed_pcm, phase_new=self.held_analysis_scalar(), + apply_dc_filter=sigma, rotate=sigma) + self._last_x = x.copy() + z_all = {obj: np.zeros((x.shape[1], x.shape[2]), dtype=np.complex128) + for obj in range(15)} + self.dll_synth_objects(z_all) + pcm16 = np.zeros((16, 1536), dtype=np.float64) + if lfe_pcm is not None: + pcm16[0] = self.decode_lfe(lfe_pcm) + pcm16_f32 = np.asarray(pcm16, dtype=np.float32) + return np.multiply(pcm16_f32, self.output_scale, dtype=np.float32), z_all + + def render_frame(self, payload_bytes, bed_pcm, lfe_pcm=None): + """payload_bytes = evolution 载荷(含 id14 JOC);bed_pcm = N×1536 核心 PCM。 lfe_pcm = 1536 核心 LFE;提供时生成 ch0,省略时 ch0 静音 (保留给只验证对象/z 链的诊断脚本)。 返回 (pcm16, z_all):pcm16 = 16×1536(ch0 LFE + 15 对象)f32, 已用 FP32 系数乘 output_scale。""" subs, _ = unpack_evolution(payload_bytes, loose=True) - return self.render_subpayloads(subs, bed5_pcm, lfe_pcm) + return self.render_subpayloads(subs, bed_pcm, lfe_pcm) - def render_subpayloads(self, subs, bed5_pcm, lfe_pcm=None): + def render_subpayloads(self, subs, bed_pcm, lfe_pcm=None): """以已拆出的 EMDF payload 字典渲染一帧,避免绑定 transport 容器。""" out, mix_q, mix_dq = self.decode_subpayloads(subs) - x = self.qmf_x( - bed5_pcm, - apply_dc_filter=out["dmx_config_idx"] in DC_FILTER_DMX_CONFIGS) + # σ(k) 同时门控 -j 旋转与 band-0 FIR(规格 §4)。 + sigma = out["dmx_config_idx"] in SIGMA_DMX_CONFIGS + x = self.qmf_x(bed_pcm, phase_new=0.0625 * out["clipgain"], + apply_dc_filter=sigma, rotate=sigma) self._last_x = x.copy() z_all = self.object_z(out, mix_dq, x) - # joc_clipgain 在对象逆 QMF 后应用,并与用户 output_scale 分离。 + # joc_clipgain 已在分析端进入 qmf_x,与用户 output_scale 分离。 objs_pcm = self.dll_synth_objects(z_all) pcm16 = np.zeros((16, 1536), dtype=np.float64) pcm16[0] = self.decode_lfe(lfe_pcm) if lfe_pcm is not None else 0.0 for obj, p in objs_pcm.items(): - pcm16[obj + 1] = p * out["clipgain"] + pcm16[obj + 1] = p pcm16_f32 = np.asarray(pcm16, dtype=np.float32) return np.multiply(pcm16_f32, self.output_scale, dtype=np.float32), z_all diff --git a/src/rosella_binaural_renderer.py b/src/rosella_binaural_renderer.py index 5701228..5524655 100644 --- a/src/rosella_binaural_renderer.py +++ b/src/rosella_binaural_renderer.py @@ -15,7 +15,6 @@ from pathlib import Path import numpy as np from binaural_metadata import OamdPositionTimeline -from binaural_native_renderer import NativeBinauralDsp from rosella_core import RosellaRenderer from rosella_direct import BINAURAL_PROFILE_NAMES from rosella_filterbank import ( @@ -72,9 +71,7 @@ class RosellaBinauralRenderer: tail_seconds: float = 5.0, output_gain: float = 1.0, chunk_frames: int = 64, - room_impulse_slots: int = 4096, - backend: str = "python", - native_library=None): + room_impulse_slots: int = 4096): if mode not in BINAURAL_PROFILE_NAMES: raise ValueError("binaural mode must be near, mid, or far") if int(object_delay_samples) < 0: @@ -85,8 +82,6 @@ class RosellaBinauralRenderer: raise ValueError("chunk_frames must be positive") if not math.isfinite(float(output_gain)): raise ValueError("output_gain must be finite") - if backend not in ("auto", "native", "python"): - raise ValueError("backend must be auto, native, or python") if isinstance(personalized_headphone, RosellaModel): self.model = personalized_headphone @@ -108,34 +103,14 @@ class RosellaBinauralRenderer: self.chunk_frames = int(chunk_frames) self.chunk_samples = self.chunk_frames * FRAME_SAMPLES - self.native_dsp = None - self.backend_fallback = None - if backend in ("auto", "native"): - try: - self.native_dsp = NativeBinauralDsp( - self.model, library_path=native_library, - kernel_data=self.kernel_data) - except (AttributeError, OSError, RuntimeError) as exc: - if backend == "native": - raise RuntimeError(f"native binaural backend unavailable: {exc}") from exc - self.backend_fallback = str(exc) - if self.native_dsp is not None: - self.dsp_backend = "native" - self.qmf_analysis = None - self.hybrid_analysis = None - self.hybrid_synthesis = None - self.qmf_synthesis = None - self.core = RosellaRenderer( - self.model, SOURCE_CHANNELS, create_room=False) - else: - self.dsp_backend = "python" - self.qmf_analysis = QmfAnalysis(SOURCE_CHANNELS, self.kernel_data) - self.hybrid_analysis = HybridAnalysis(SOURCE_CHANNELS, self.kernel_data) - self.core = RosellaRenderer( - self.model, SOURCE_CHANNELS, - room_impulse_slots=room_impulse_slots) - self.hybrid_synthesis = HybridSynthesis(OUTPUT_CHANNELS, self.kernel_data) - self.qmf_synthesis = QmfSynthesis(OUTPUT_CHANNELS, self.kernel_data) + self.dsp_backend = "python" + self.qmf_analysis = QmfAnalysis(SOURCE_CHANNELS, self.kernel_data) + self.hybrid_analysis = HybridAnalysis(SOURCE_CHANNELS, self.kernel_data) + self.core = RosellaRenderer( + self.model, SOURCE_CHANNELS, + room_impulse_slots=room_impulse_slots) + self.hybrid_synthesis = HybridSynthesis(OUTPUT_CHANNELS, self.kernel_data) + self.qmf_synthesis = QmfSynthesis(OUTPUT_CHANNELS, self.kernel_data) self.timeline = OamdPositionTimeline(15) self._input_buffer = np.empty( @@ -210,40 +185,29 @@ class RosellaBinauralRenderer: blocks = len(values) // ROSSELLA_BLOCK_SAMPLES block_base = self.processed_input_samples - if self.native_dsp is not None: - stereo = np.empty((len(values), OUTPUT_CHANNELS), dtype=np.float64) - for block in range(blocks): - sample = block_base + block * ROSSELLA_BLOCK_SAMPLES - self._set_block_parameters(sample) - start = block * ROSSELLA_BLOCK_SAMPLES - stop = start + ROSSELLA_BLOCK_SAMPLES - stereo[start:stop] = self.native_dsp.process_block( - values[start:stop], self.core.gains, self.core.room_sends, - self.output_gain) - else: - hops = values.reshape( - blocks, ROSSELLA_BLOCK_SAMPLES // QMF_HOP_SAMPLES, - QMF_HOP_SAMPLES, SOURCE_CHANNELS, - ).transpose(0, 1, 3, 2).reshape( - blocks * (ROSSELLA_BLOCK_SAMPLES // QMF_HOP_SAMPLES), - SOURCE_CHANNELS, QMF_HOP_SAMPLES) - hybrid = self.hybrid_analysis.process_chunk( - self.qmf_analysis.process_chunk(hops)) - direct = np.empty((blocks * 8, OUTPUT_CHANNELS, 77), dtype=np.complex128) - room_send = np.empty((blocks * 8, 77), dtype=np.complex128) - for block in range(blocks): - sample = block_base + block * ROSSELLA_BLOCK_SAMPLES - self._set_block_parameters(sample) - start = block * 8 - stop = start + 8 - direct[start:stop], room_send[start:stop] = ( - self.core.direct_and_send_static(hybrid[start:stop])) - rendered = direct + self.core.room.process_chunk(room_send) - time_bands = self.qmf_synthesis.process_chunk( - self.hybrid_synthesis.process_chunk(rendered)) - stereo = time_bands.transpose(0, 2, 1).reshape( - blocks * ROSSELLA_BLOCK_SAMPLES, OUTPUT_CHANNELS) - stereo *= self.output_gain + hops = values.reshape( + blocks, ROSSELLA_BLOCK_SAMPLES // QMF_HOP_SAMPLES, + QMF_HOP_SAMPLES, SOURCE_CHANNELS, + ).transpose(0, 1, 3, 2).reshape( + blocks * (ROSSELLA_BLOCK_SAMPLES // QMF_HOP_SAMPLES), + SOURCE_CHANNELS, QMF_HOP_SAMPLES) + hybrid = self.hybrid_analysis.process_chunk( + self.qmf_analysis.process_chunk(hops)) + direct = np.empty((blocks * 8, OUTPUT_CHANNELS, 77), dtype=np.complex128) + room_send = np.empty((blocks * 8, 77), dtype=np.complex128) + for block in range(blocks): + sample = block_base + block * ROSSELLA_BLOCK_SAMPLES + self._set_block_parameters(sample) + start = block * 8 + stop = start + 8 + direct[start:stop], room_send[start:stop] = ( + self.core.direct_and_send_static(hybrid[start:stop])) + rendered = direct + self.core.room.process_chunk(room_send) + time_bands = self.qmf_synthesis.process_chunk( + self.hybrid_synthesis.process_chunk(rendered)) + stereo = time_bands.transpose(0, 2, 1).reshape( + blocks * ROSSELLA_BLOCK_SAMPLES, OUTPUT_CHANNELS) + stereo *= self.output_gain skip = max(0, min( len(stereo), ROSSELLA_LATENCY_SAMPLES - self.raw_output_samples)) @@ -278,8 +242,6 @@ class RosellaBinauralRenderer: return np.concatenate(nonempty, axis=0) def close(self): - if self.native_dsp is not None: - self.native_dsp.close() self.finished = True @property @@ -287,9 +249,6 @@ class RosellaBinauralRenderer: return { "name": self.dsp_backend, "precision": "float64/complex128", - "fallback_reason": self.backend_fallback, - "library": (str(self.native_dsp.library_path) - if self.native_dsp is not None else None), "model": str(self.model_path.resolve()), "model_coefficients": int(len(self.model.coefficients)), "model_coefficient_sha256": self.model.coefficient_sha256, diff --git a/src/sofa_native_backend.py b/src/sofa_native_backend.py deleted file mode 100644 index be45bac..0000000 --- a/src/sofa_native_backend.py +++ /dev/null @@ -1,398 +0,0 @@ -"""Native float64 SOFA binaural DSP (ctypes bridge to eac3joc_core). - -The C++ side mirrors the Python :class:`sofa_binaural_backend.SofaBinauralBackend` -mathematics: 64-QMF/77-hybrid analysis and synthesis, fifth-order ACN/N3D real -spherical-harmonic field evaluation, whole-QMF-slot per-object delay histories, -six image-source early reflections, the shared unitary FDN late room, the LFE -low-pass and the 961-sample latency policy. The compiled HRTF field, the -filterbank tables and the room constants are uploaded once; per 512-sample -block the adapter updates every source and streams PCM through the DLL. -""" -from __future__ import annotations - -import ctypes -import math -from pathlib import Path - -import numpy as np - -from native_renderer import ABI_VERSION, find_native_library -from public_filterbank import DEFAULT_FILTERBANK_DATA, load_filterbank_tables -from public_room import LateFdnConfig, SharedUnitaryFdn, ShoeboxRoomConfig -from reference_distance import ReferenceDistanceProfileV1 -from sofa_binaural_backend import SofaBinauralBackend -from sofa_hrtf_field import ( - DEFAULT_HRTF_CACHE_DIR, - SofaHrtfField, - compile_sofa_hrtf, -) - -BLOCK_SAMPLES = 512 -INPUT_CHANNELS = 16 -OUTPUT_CHANNELS = 2 -QMF_HOP = 64 -LATENCY_SAMPLES = 961 - -_PROFILE_INDEX = {"near": 0, "mid": 1, "far": 2} - - -def _room_numbers(fdn_config: LateFdnConfig) -> dict: - """Derive the FDN delays/feedback with the same arithmetic as the Python room.""" - fdn = SharedUnitaryFdn(fdn_config) - return { - "fdn_delays": np.asarray(fdn.delays, dtype=np.uint32), - "fdn_feedback": np.asarray(fdn.feedback_gain, dtype=np.float64), - "damping": fdn.damping, - "output_gain": fdn.output_gain, - "allpass_delays": np.asarray( - [diffuser.delay_samples for diffuser in fdn.diffusers], dtype=np.uint32), - "allpass_gains": np.asarray(fdn_config.allpass_gain, dtype=np.float64), - "tail_samples": fdn.tail_samples, - } - - -class NativeSofaBinauralDsp: - """Duck-type compatible with SofaBinauralBackend for the JOC adapter.""" - - def __init__( - self, - field: SofaHrtfField, - *, - source_count: int = INPUT_CHANNELS, - default_profile: str = "mid", - enable_early_reflections: bool = True, - enable_late_room: bool = True, - room_config: ShoeboxRoomConfig = ShoeboxRoomConfig(), - fdn_config: LateFdnConfig | None = None, - library_path: str | Path | None = None): - if not isinstance(field, SofaHrtfField): - raise TypeError("field must be SofaHrtfField") - self.source_count = int(source_count) - self.default_profile = ReferenceDistanceProfileV1.validate_profile(default_profile) - self.enable_early_reflections = bool(enable_early_reflections) - self.enable_late_room = bool(enable_late_room) - self.field = field - if self.source_count != INPUT_CHANNELS: - raise ValueError(f"native SOFA backend requires {INPUT_CHANNELS} sources") - if abs(self.field.sample_rate_hz - 48000.0) > 1.0e-9: - raise ValueError("the native SOFA binaural runtime requires 48 kHz") - self.room_config = room_config - self.room_config.validate() - self.dsp_backend = "native-sofa" - self.hrtf_input_kind = "field" - self.hrtf_input_path = None - self.cache_policy = None - - self.library_path = find_native_library(library_path) - self._lib = ctypes.CDLL(str(self.library_path)) - self._bind() - version = int(self._lib.ejoc_abi_version()) - if version != ABI_VERSION: - raise RuntimeError( - f"native ABI mismatch: expected {ABI_VERSION}, got {version}") - self._handle = self._lib.ejoc_sofa_binaural_create() - if not self._handle: - raise RuntimeError("native SOFA binaural renderer creation failed") - try: - self._configure_kernels() - self._configure_field() - self._configure_room(fdn_config) - except Exception: - self.close() - raise - - self.positions = np.zeros((self.source_count, 3), dtype=np.float64) - self.positions[:, 1] = 1.0 - self.profiles = [self.default_profile] * self.source_count - self.user_gain = np.ones(self.source_count, dtype=np.float64) - self.special_lfe = np.zeros(self.source_count, dtype=bool) - self.parameter_updates = 0 - self.finished = False - for source in range(self.source_count): - self.set_source( - source, self.positions[source], profile=self.default_profile, - fade=False) - - def _bind(self): - void_p = ctypes.c_void_p - f64_p = ctypes.POINTER(ctypes.c_double) - i16_p = ctypes.POINTER(ctypes.c_int16) - u32_p = ctypes.POINTER(ctypes.c_uint32) - self._lib.ejoc_abi_version.argtypes = [] - self._lib.ejoc_abi_version.restype = ctypes.c_uint32 - self._lib.ejoc_sofa_binaural_create.argtypes = [] - self._lib.ejoc_sofa_binaural_create.restype = void_p - self._lib.ejoc_sofa_binaural_destroy.argtypes = [void_p] - self._lib.ejoc_sofa_binaural_destroy.restype = None - self._lib.ejoc_sofa_binaural_reset.argtypes = [void_p] - self._lib.ejoc_sofa_binaural_reset.restype = ctypes.c_int - self._lib.ejoc_sofa_binaural_last_error.argtypes = [void_p] - self._lib.ejoc_sofa_binaural_last_error.restype = ctypes.c_char_p - self._lib.ejoc_sofa_binaural_configure_kernels.argtypes = [ - void_p, f64_p, f64_p, i16_p, f64_p, ctypes.c_uint32, f64_p, f64_p] - self._lib.ejoc_sofa_binaural_configure_kernels.restype = ctypes.c_int - self._lib.ejoc_sofa_binaural_configure_field.argtypes = [ - void_p, f64_p, f64_p, f64_p, f64_p, ctypes.c_double] - self._lib.ejoc_sofa_binaural_configure_field.restype = ctypes.c_int - self._lib.ejoc_sofa_binaural_configure_room.argtypes = [ - void_p, f64_p, f64_p, f64_p, ctypes.c_double, u32_p, f64_p, - ctypes.c_double, ctypes.c_double, u32_p, f64_p, - ctypes.c_uint32, ctypes.c_uint32] - self._lib.ejoc_sofa_binaural_configure_room.restype = ctypes.c_int - self._lib.ejoc_sofa_binaural_set_source.argtypes = [ - void_p, ctypes.c_uint32, f64_p, ctypes.c_uint32, ctypes.c_double, - ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32] - self._lib.ejoc_sofa_binaural_set_source.restype = ctypes.c_int - self._lib.ejoc_sofa_binaural_process.argtypes = [ - void_p, f64_p, ctypes.c_uint32, ctypes.c_double, f64_p] - self._lib.ejoc_sofa_binaural_process.restype = ctypes.c_int - self._lib.ejoc_sofa_binaural_finish.argtypes = [ - void_p, ctypes.c_uint32, f64_p, ctypes.c_uint32] - self._lib.ejoc_sofa_binaural_finish.restype = ctypes.c_int - - def _raise(self, operation, status): - message = self._lib.ejoc_sofa_binaural_last_error(self._handle) - detail = (message or b"").decode("utf-8", "replace") - raise RuntimeError( - f"native SOFA binaural renderer {operation} failed ({status}): {detail}") - - @staticmethod - def _f64_pointer(values): - return values.ctypes.data_as(ctypes.POINTER(ctypes.c_double)) - - def _configure_kernels(self): - tables = load_filterbank_tables(DEFAULT_FILTERBANK_DATA) - qmf_analysis = np.ascontiguousarray( - tables["qmf_analysis_coefficients"], dtype=np.float64) - hybrid_low = np.ascontiguousarray( - tables["hybrid_analysis_low_kernel"], dtype=np.float64) - hybrid_indices = np.ascontiguousarray( - tables["hybrid_synthesis_indices"], dtype=np.int16) - hybrid_values = np.ascontiguousarray( - tables["hybrid_synthesis_values"], dtype=np.float64) - qmf_basis = np.ascontiguousarray( - tables["qmf_synthesis_basis"], dtype=np.float64) - qmf_taps = np.ascontiguousarray( - tables["qmf_synthesis_taps"], dtype=np.float64) - status = self._lib.ejoc_sofa_binaural_configure_kernels( - self._handle, - self._f64_pointer(qmf_analysis), - self._f64_pointer(hybrid_low), - hybrid_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int16)), - self._f64_pointer(hybrid_values), - len(hybrid_indices), - self._f64_pointer(qmf_basis), - self._f64_pointer(qmf_taps)) - if status: - self._raise("configure_kernels", status) - self._keepalive = (qmf_analysis, hybrid_low, hybrid_indices, - hybrid_values, qmf_basis, qmf_taps) - - def _configure_field(self): - coefficients = np.ascontiguousarray( - self.field.coefficients, dtype=np.complex128).view(np.float64) - delay_coefficients = np.ascontiguousarray( - self.field.delay_coefficients, dtype=np.float64) - delay_bounds = np.ascontiguousarray( - self.field.delay_bounds, dtype=np.float64) - centers = np.ascontiguousarray( - self.field.band_center_frequencies_hz, dtype=np.float64) - status = self._lib.ejoc_sofa_binaural_configure_field( - self._handle, - self._f64_pointer(coefficients), - self._f64_pointer(delay_coefficients), - self._f64_pointer(delay_bounds), - self._f64_pointer(centers), - float(self.field.measurement_radius_m)) - if status: - self._raise("configure_field", status) - - def _configure_room(self, fdn_config: LateFdnConfig | None): - actual = fdn_config or LateFdnConfig(sample_rate_hz=48000.0) - numbers = _room_numbers(actual) - dims = np.asarray(self.room_config.dimensions_m, dtype=np.float64) - listener = np.asarray(self.room_config.listener_position_m, dtype=np.float64) - walls = np.asarray(self.room_config.wall_reflection_gain, dtype=np.float64) - status = self._lib.ejoc_sofa_binaural_configure_room( - self._handle, - self._f64_pointer(dims), - self._f64_pointer(listener), - self._f64_pointer(walls), - float(self.room_config.speed_of_sound_m_s), - numbers["fdn_delays"].ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)), - self._f64_pointer(numbers["fdn_feedback"]), - float(numbers["damping"]), - float(numbers["output_gain"]), - numbers["allpass_delays"].ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)), - self._f64_pointer(numbers["allpass_gains"]), - 1 if self.enable_early_reflections else 0, - 1 if self.enable_late_room else 0) - if status: - self._raise("configure_room", status) - self._fdn = SharedUnitaryFdn(actual) - self._fdn_tail_samples = numbers["tail_samples"] - - def set_source(self, source: int, position_adm, *, profile: str | None = None, - gain: float = 1.0, enabled: bool = True, - special_lfe: bool = False, fade: bool = True) -> None: - if self.finished: - raise RuntimeError("SOFA renderer is finished") - source = int(source) - if not 0 <= source < self.source_count: - raise IndexError(source) - name = self.default_profile if profile is None else profile - position = np.asarray(position_adm, dtype=np.float64) - if position.shape != (3,): - raise ValueError("ADM position must contain three Cartesian values") - status = self._lib.ejoc_sofa_binaural_set_source( - self._handle, source, - self._f64_pointer(np.ascontiguousarray(position)), - _PROFILE_INDEX[ReferenceDistanceProfileV1.validate_profile(name)], - float(gain), 1 if enabled else 0, 1 if special_lfe else 0, - 1 if fade else 0) - if status: - self._raise("set_source", status) - self.positions[source] = position - self.profiles[source] = name - self.user_gain[source] = float(gain) - self.special_lfe[source] = bool(special_lfe) - self.parameter_updates += 1 - - def process(self, sources) -> np.ndarray: - if self.finished: - raise RuntimeError("SOFA renderer is finished") - values = np.ascontiguousarray(sources, dtype=np.float64) - if values.ndim != 2 or values.shape[1] != self.source_count: - raise ValueError(f"sources must have shape [samples,{self.source_count}]") - if len(values) % QMF_HOP or len(values) > BLOCK_SAMPLES: - raise ValueError("native SOFA backend input must be a 64-aligned block") - output = np.empty((len(values), OUTPUT_CHANNELS), dtype=np.float64) - count = self._lib.ejoc_sofa_binaural_process( - self._handle, self._f64_pointer(values), len(values), 1.0, - self._f64_pointer(output)) - if count < 0: - self._raise("process", count) - return output[:count] - - def finish(self, *, tail_seconds: float | None = None) -> np.ndarray: - if self.finished: - return np.zeros((0, OUTPUT_CHANNELS), dtype=np.float64) - if tail_seconds is not None and ( - not math.isfinite(float(tail_seconds)) or float(tail_seconds) < 0.0): - raise ValueError("tail_seconds must be finite and non-negative") - flush = self.finish_output_capacity(tail_seconds) - pieces = [] - remaining = flush - while remaining > 0: - chunk = min(BLOCK_SAMPLES, remaining) - output = np.empty((chunk, OUTPUT_CHANNELS), dtype=np.float64) - count = self._lib.ejoc_sofa_binaural_finish( - self._handle, chunk, self._f64_pointer(output), chunk) - if count < 0: - self._raise("finish", count) - pieces.append(output[:count]) - remaining -= chunk - self.finished = True - nonempty = [piece for piece in pieces if len(piece)] - if not nonempty: - return np.zeros((0, OUTPUT_CHANNELS), dtype=np.float64) - return np.concatenate(nonempty, axis=0) - - def finish_output_capacity(self, tail_seconds: float | None = None) -> int: - if tail_seconds is not None and ( - not math.isfinite(float(tail_seconds)) or float(tail_seconds) < 0.0): - raise ValueError("tail_seconds must be finite and non-negative") - requested = (self._fdn_tail_samples if tail_seconds is None - else int(math.ceil(float(tail_seconds) * 48000.0))) - maximum_hrtf = float(np.max(self.field.delay_bounds[:, 1], initial=0.0)) - hrtf_slots = int(math.ceil(maximum_hrtf / QMF_HOP)) - hrtf_bound = hrtf_slots * QMF_HOP - early_bound = hrtf_bound + 2048 - if self.enable_early_reflections: - early_bound += 256 * QMF_HOP - drain = max(requested if self.enable_late_room else 0, early_bound) - drain += LATENCY_SAMPLES - return int(math.ceil(drain / QMF_HOP) * QMF_HOP) - - def reset(self) -> None: - if self._lib.ejoc_sofa_binaural_reset(self._handle): - self._raise("reset", -1) - self.finished = False - for source in range(self.source_count): - self.set_source( - source, self.positions[source], profile=self.profiles[source], - gain=float(self.user_gain[source]), - special_lfe=bool(self.special_lfe[source]), fade=False) - - def info(self) -> dict: - return { - "name": "NativeSofaBinauralDsp", - "source_count": self.source_count, - "sample_rate_hz": 48000.0, - "precision": "float64/complex128", - "signal_path": ( - "native 64-QMF -> native 77-hybrid -> SOFA order-5 real-SH " - "direct/early -> native synthesis + shared unitary FDN"), - "hrtf_input_kind": self.hrtf_input_kind, - "hrtf_input_path": self.hrtf_input_path, - "cache_policy": self.cache_policy, - "latency_compensated_samples": LATENCY_SAMPLES, - "enable_early_reflections": self.enable_early_reflections, - "enable_late_room": self.enable_late_room, - "early_history_slots": 256, - "hrtf_history_slots": int(math.ceil( - float(np.max(self.field.delay_bounds[:, 1], initial=0.0)) / QMF_HOP)), - "maximum_hrtf_delay_samples": float( - np.max(self.field.delay_bounds[:, 1], initial=0.0)), - "parameter_updates": self.parameter_updates, - "distance": ReferenceDistanceProfileV1.info(), - "field": self.field.info(), - "library_path": str(self.library_path), - "native_backend": True, - } - - def close(self) -> None: - handle = getattr(self, "_handle", None) - if handle: - self._lib.ejoc_sofa_binaural_destroy(handle) - self._handle = None - self.finished = True - - -def create_native_sofa_renderer( - sofa, *, mode="mid", cache_policy="memory", cache_dir=None, - shell_radius_m=1.0, object_delay_samples=1473, tail_seconds=5.0, - output_gain=1.0, chunk_frames=64): - """Compile a SOFA source and build a JOC adapter over the native DSP.""" - from binaural_renderer import SofaBinauralRenderer, resolve_sofa_hrtf - - source = resolve_sofa_hrtf(sofa) - field = compile_sofa_hrtf( - source, - shell_radius_m=shell_radius_m, - cache_policy=cache_policy, - cache_dir=cache_dir) - backend = NativeSofaBinauralDsp(field, default_profile=mode) - backend.hrtf_input_kind = "sofa" - backend.hrtf_input_path = str(source) - backend.cache_policy = str(cache_policy).lower() - return SofaBinauralRenderer( - backend, mode=mode, object_delay_samples=object_delay_samples, - tail_seconds=tail_seconds, chunk_frames=chunk_frames) - - -def create_native_compiled_cache_renderer( - cache, *, mode="mid", object_delay_samples=1473, tail_seconds=5.0, - output_gain=1.0, chunk_frames=64): - """Load a compiled cache and build a JOC adapter over the native DSP.""" - from binaural_renderer import SofaBinauralRenderer, resolve_compiled_hrtf_cache - - source = resolve_compiled_hrtf_cache(cache) - field = SofaHrtfField.load(source) - backend = NativeSofaBinauralDsp(field, default_profile=mode) - backend.hrtf_input_kind = "compiled_cache" - backend.hrtf_input_path = str(source) - backend.cache_policy = None - return SofaBinauralRenderer( - backend, mode=mode, object_delay_samples=object_delay_samples, - tail_seconds=tail_seconds, chunk_frames=chunk_frames) diff --git a/src/speaker_backend.py b/src/speaker_backend.py index 07cfc19..ef7b65c 100644 --- a/src/speaker_backend.py +++ b/src/speaker_backend.py @@ -1,4 +1,4 @@ -"""Unified auto/native/python entry point for float64 speaker rendering.""" +"""Float64 speaker rendering entry point.""" from __future__ import annotations import time @@ -6,39 +6,21 @@ import time import numpy as np from speaker_layouts import SpeakerLayout, get_speaker_layout -from speaker_native_renderer import NativeSpeakerRenderer from speaker_renderer import PythonSpeakerRenderer, payload_events_from_index, render_objects16 FRAME_SAMPLES = 1536 -def create_speaker_renderer(layout: str | SpeakerLayout, *, backend="auto", native_library=None): +def create_speaker_renderer(layout: str | SpeakerLayout): """Create a stateful speaker renderer and return ``(renderer, info)``.""" - if backend not in ("auto", "native", "python"): - raise ValueError(f"unknown speaker backend: {backend}") target = get_speaker_layout(layout) if isinstance(layout, str) else layout - fallback_reason = None - if backend in ("auto", "native"): - try: - renderer = NativeSpeakerRenderer(target, native_library) - return renderer, { - "name": "native", - "layout": target.name, - "library": str(renderer.library_path), - "fallback_reason": None, - } - except (OSError, RuntimeError) as exc: - fallback_reason = str(exc) return PythonSpeakerRenderer(target), { "name": "python", "layout": target.name, - "library": None, - "fallback_reason": fallback_reason, } def render_speaker_layout(objects16, index, layout: str | SpeakerLayout, *, - backend="auto", native_library=None, metadata_offset=1473): """Render a complete indexed object stream and return ``(pcm64, info)``.""" target = get_speaker_layout(layout) if isinstance(layout, str) else layout @@ -51,8 +33,7 @@ def render_speaker_layout(objects16, index, layout: str | SpeakerLayout, *, if frames > len(index.rows): raise ValueError(f"metadata has {len(index.rows)} frames, PCM needs {frames}") - renderer, info = create_speaker_renderer( - target, backend=backend, native_library=native_library) + renderer, info = create_speaker_renderer(target) output = np.empty((len(source), target.channel_count), dtype=np.float64) started = time.perf_counter() try: diff --git a/src/speaker_native_renderer.py b/src/speaker_native_renderer.py deleted file mode 100644 index ea13c52..0000000 --- a/src/speaker_native_renderer.py +++ /dev/null @@ -1,154 +0,0 @@ -"""ctypes bridge for the float64 native object-to-speaker renderer.""" -from __future__ import annotations - -import ctypes -from pathlib import Path - -import numpy as np - -from native_renderer import ABI_VERSION, find_native_library -from oamd_bits import JocFieldState, frame_update -from speaker_layouts import SpeakerLayout, get_speaker_layout - -FRAME_SAMPLES = 1536 -INPUT_CHANNELS = 16 -OBJECTS = 15 -COORDINATES = 3 - - -class NativeSpeakerRenderer: - def __init__(self, layout: str | SpeakerLayout, library_path=None): - self.layout = get_speaker_layout(layout) if isinstance(layout, str) else layout - self.library_path = find_native_library(library_path) - self._lib = ctypes.CDLL(str(self.library_path)) - self._bind() - version = int(self._lib.ejoc_abi_version()) - if version != ABI_VERSION: - raise RuntimeError(f"native ABI mismatch: expected {ABI_VERSION}, got {version}") - channels = int(self._lib.ejoc_speaker_layout_channel_count(self.layout.speaker_bitfield)) - if channels != self.layout.channel_count: - raise RuntimeError( - f"native layout channel count mismatch: expected {self.layout.channel_count}, got {channels}" - ) - self._handle = self._lib.ejoc_speaker_renderer_create(self.layout.speaker_bitfield) - if not self._handle: - raise RuntimeError(f"native speaker renderer rejected mask 0x{self.layout.speaker_bitfield:X}") - self._state = JocFieldState() - - def _bind(self): - void_p = ctypes.c_void_p - self._lib.ejoc_abi_version.argtypes = [] - self._lib.ejoc_abi_version.restype = ctypes.c_uint32 - self._lib.ejoc_speaker_layout_channel_count.argtypes = [ctypes.c_uint32] - self._lib.ejoc_speaker_layout_channel_count.restype = ctypes.c_uint32 - self._lib.ejoc_speaker_renderer_create.argtypes = [ctypes.c_uint32] - self._lib.ejoc_speaker_renderer_create.restype = void_p - self._lib.ejoc_speaker_renderer_destroy.argtypes = [void_p] - self._lib.ejoc_speaker_renderer_destroy.restype = None - self._lib.ejoc_speaker_renderer_reset.argtypes = [void_p] - self._lib.ejoc_speaker_renderer_reset.restype = ctypes.c_int - self._lib.ejoc_speaker_renderer_last_error.argtypes = [void_p] - self._lib.ejoc_speaker_renderer_last_error.restype = ctypes.c_char_p - self._lib.ejoc_speaker_renderer_process.argtypes = [ - void_p, - ctypes.POINTER(ctypes.c_float), - ctypes.c_uint32, - ctypes.c_uint32, - ctypes.POINTER(ctypes.c_uint32), - ctypes.POINTER(ctypes.c_uint32), - ctypes.POINTER(ctypes.c_uint16), - ctypes.POINTER(ctypes.c_uint8), - ctypes.POINTER(ctypes.c_uint8), - ctypes.POINTER(ctypes.c_double), - ctypes.POINTER(ctypes.c_double), - ] - self._lib.ejoc_speaker_renderer_process.restype = ctypes.c_int - - def _raise(self, operation, status): - message = self._lib.ejoc_speaker_renderer_last_error(self._handle) - detail = (message or b"").decode("utf-8", "replace") - raise RuntimeError(f"native speaker renderer {operation} failed ({status}): {detail}") - - def reset(self): - if not self._handle: - raise RuntimeError("native speaker renderer is closed") - status = self._lib.ejoc_speaker_renderer_reset(self._handle) - if status: - self._raise("reset", status) - self._state = JocFieldState() - - def close(self): - if self._handle: - self._lib.ejoc_speaker_renderer_destroy(self._handle) - self._handle = None - - def __enter__(self): - return self - - def __exit__(self, exc_type, exc, tb): - self.close() - - def __del__(self): - try: - self.close() - except Exception: - pass - - def _metadata_arrays(self, events): - events = list(events) - count = len(events) - if not count: - return count, None, None, None - offsets = np.empty(count, dtype=np.uint32) - ramps = np.empty(count, dtype=np.uint32) - positions = np.empty((count, OBJECTS, COORDINATES), dtype=np.uint16) - for event_index, (outer_offset, payload) in enumerate(events): - update = frame_update(payload) - self._state.apply(update["values"]) - offsets[event_index] = int(outer_offset) + int(update["block_offset_samples"]) - ramps[event_index] = int(update["ramp_duration_samples"]) - for object_index in range(1, OBJECTS + 1): - positions[event_index, object_index - 1] = ( - self._state.q[(object_index, "q1")], - self._state.q[(object_index, "q2")], - self._state.q[(object_index, "q3")], - ) - return count, offsets, ramps, positions - - def process(self, objects16, events=()): - if not self._handle: - raise RuntimeError("native speaker renderer is closed") - source = np.ascontiguousarray(objects16, dtype=np.float32) - if source.ndim != 2 or source.shape[1] != INPUT_CHANNELS: - raise ValueError(f"objects16 must have shape [samples,16], got {source.shape}") - if len(source) % 32: - raise ValueError("sample count must be a multiple of 32") - count, offsets, ramps, positions = self._metadata_arrays(events) - output = np.empty((len(source), self.layout.channel_count), dtype=np.float64) - null_u32 = ctypes.POINTER(ctypes.c_uint32)() - null_u16 = ctypes.POINTER(ctypes.c_uint16)() - null_u8 = ctypes.POINTER(ctypes.c_uint8)() - null_f64 = ctypes.POINTER(ctypes.c_double)() - status = self._lib.ejoc_speaker_renderer_process( - self._handle, - source.ctypes.data_as(ctypes.POINTER(ctypes.c_float)), - len(source), - count, - offsets.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)) if count else null_u32, - ramps.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)) if count else null_u32, - positions.ctypes.data_as(ctypes.POINTER(ctypes.c_uint16)) if count else null_u16, - null_u8, - null_u8, - null_f64, - output.ctypes.data_as(ctypes.POINTER(ctypes.c_double)), - ) - if status: - self._raise("process", status) - return output - - def render_frame(self, objects16, payload=None, metadata_offset=1473): - source = np.asarray(objects16) - if source.shape != (FRAME_SAMPLES, INPUT_CHANNELS): - raise ValueError(f"frame must have shape (1536,16), got {source.shape}") - events = () if payload is None else ((int(metadata_offset), bytes(payload)),) - return self.process(source, events) diff --git a/src/speaker_renderer.py b/src/speaker_renderer.py index 209642f..b5f2451 100644 --- a/src/speaker_renderer.py +++ b/src/speaker_renderer.py @@ -190,7 +190,7 @@ def _all_object_targets(layout: SpeakerLayout, state: JocFieldState) -> np.ndarr class PythonSpeakerRenderer: - """Stateful float64 speaker renderer with the same frame API as native.""" + """Stateful float64 speaker renderer.""" def __init__(self, layout: str | SpeakerLayout, block_size: int = 32): self.layout = get_speaker_layout(layout) if isinstance(layout, str) else layout