Keep only the Rosella and .jochrtf binaural paths
The runtime renders two inputs now: the Rosella .personalized_headphone model and an already compiled .jochrtf field. SOFA import, the full-HRIR direct renderer and the SOFA -> .jochrtf fitting compiler leave this checkout; they continue as an offline tool that only produces caches this repository loads. The .jochrtf renderer goes back to the implementation that predates the direct SOFA work: 64-QMF analysis, 77 hybrid bands, per-object hybrid path delays and the project room (six first-order images plus the shared FDN tail) computed in code. It is restored verbatim under the .jochrtf vocabulary (SofaBinauralBackend -> JochrtfBackend) and gains one switch: --jochrtf-room on|off, off by default. With the room off the renderer outputs only the field's direct binaural sound, and in either mode it reads no audio assets. Because the room is computed in code again, the generated sofa-room*.npz assets and the modules only the SOFA path used are removed (sofa_canonical, sofa_hrir, sofa_direct_backend, sofa_room, hybrid_paths, partitioned_convolution, point_source, compile_hrtf, generate_sofa_room). data/ keeps the decode tables and the Rosella filterbanks. The CLI drops --sofa-hrtf, --hrtf-radius-m, --sofa-interpolation, --hrtf-cache-policy, --hrtf-cache-dir, --sofa-room, --sofa-room-preset and --source-reference-distance-m. A .jochrtf is still explicit through --compiled-hrtf-cache, and the default binaural input is the personalized model. Rosella parsing, filterbank, room and timing are untouched and its output stays bit-identical. The .jochrtf path reproduces the pre-SOFA renderer sample for sample, with the room on and with it off.
This commit is contained in:
1 parent
3ea285e407
commit
07a26b41fe
21 files changed
+688
-2305
No files matched your search
+22
-45
@@ -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.
|
> 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 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.
|
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 binaural stereo (a Rosella personalized model, or a compiled `.jochrtf`).
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
@@ -23,8 +23,8 @@ This is research code, not a complete, standards-compliant, or production-grade
|
|||||||
- Render directly to `2.0`, `3.1`, `4.0`, `5.1`, `7.1`, `5.1.2`, `5.1.4`, `7.1.2`, `7.1.4`, `9.1.4`
|
- Render directly to `2.0`, `3.1`, `4.0`, `5.1`, `7.1`, `5.1.2`, `5.1.4`, `7.1.2`, `7.1.4`, `9.1.4`
|
||||||
or `9.1.6` (`4.0` is the quad layout, a front and a back pair; see
|
or `9.1.6` (`4.0` is the quad layout, a front and a back pair; see
|
||||||
[Speaker layouts and channel order](#speaker-layouts-and-channel-order)).
|
[Speaker layouts and channel order](#speaker-layouts-and-channel-order)).
|
||||||
- Run public SOFA binaural rendering directly from `pcm16 + ID11/OAMD`, without a temporary ADM BWF.
|
- Run binaural rendering directly from `pcm16 + ID11/OAMD`, without a temporary ADM BWF.
|
||||||
- Keep binaural DSP in float64/complex128: full SOFA HRIR convolution and independent reference state; the personalized route retains its existing filterbank and latency rules.
|
- Keep binaural DSP in float64/complex128: the personalized route retains its existing filterbank and latency rules, the `.jochrtf` route keeps its 77-band field and cross-frame state.
|
||||||
- Use a shared float32/PCM24 WAV writer and explicit PCM24 clipping policy for direct outputs.
|
- Use a shared float32/PCM24 WAV writer and explicit PCM24 clipping policy for direct outputs.
|
||||||
- Implement everything in pure Python on NumPy;
|
- Implement everything in pure Python on NumPy;
|
||||||
- Read or write metadata sidecars and produce metadata, timing, and output reports.
|
- Read or write metadata sidecars and produce metadata, timing, and output reports.
|
||||||
@@ -40,10 +40,10 @@ M4A / E-AC-3
|
|||||||
└─ LFE + 15 objects
|
└─ LFE + 15 objects
|
||||||
├─ 25ch ADM BWF
|
├─ 25ch ADM BWF
|
||||||
├─ speaker WAV for the selected layout
|
├─ speaker WAV for the selected layout
|
||||||
└─ direct ID11 timeline + SOFA HRTF → binaural WAV
|
└─ direct ID11 timeline + HRTF (Rosella / .jochrtf) → binaural WAV
|
||||||
```
|
```
|
||||||
|
|
||||||
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.
|
JOC object reconstruction, speaker rendering, and 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
|
## Requirements
|
||||||
|
|
||||||
@@ -87,59 +87,36 @@ python main.py input.m4a --speaker-layout 5.1 --speaker-format int24
|
|||||||
python main.py input.m4a --speaker-layout 7.1.2 --speaker-output output.7.1.2.wav
|
python main.py input.m4a --speaker-layout 7.1.2 --speaker-output output.7.1.2.wav
|
||||||
```
|
```
|
||||||
|
|
||||||
Write binaural stereo directly. SOFA and compact inputs render direct HRTFs by default;
|
Write binaural stereo directly. Two kinds of input are available:
|
||||||
`--sofa-room` enables the existing project room. There is no synthetic radial head filtering. Personalized rendering
|
|
||||||
retains its existing Near/Mid/Far modes and room processing.
|
|
||||||
The HRTF input accepts three sources:
|
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
# 1) Full SOFA HRIR (defaults to HRTF/binaural.sofa, or an explicit path)
|
# 1) Rosella .personalized_headphone (default; HRTF/binaural.personalized_headphone)
|
||||||
python main.py input.m4a --binaural
|
python main.py input.m4a --binaural
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa
|
|
||||||
|
|
||||||
# 2) Rosella .personalized_headphone (defaults to HRTF/binaural.personalized_headphone)
|
|
||||||
python main.py input.m4a --binaural --personalized-headphone
|
|
||||||
python main.py input.m4a --binaural --personalized-headphone C:\HRTF\subject.personalized_headphone
|
python main.py input.m4a --binaural --personalized-headphone C:\HRTF\subject.personalized_headphone
|
||||||
|
python main.py input.m4a --binaural --personalized-headphone --binaural-mode far --rosella-filterbank 2
|
||||||
|
|
||||||
# 3) .jochrtf compiled cache
|
# 2) .jochrtf compiled field (reserved for high-precision offline rendering)
|
||||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
||||||
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room off
|
||||||
# Common options
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa `
|
|
||||||
--sofa-interpolation lf_mca_zero
|
|
||||||
python src\compile_hrtf.py C:\HRTF\subject.sofa C:\HRTF\subject.jochrtf
|
|
||||||
python main.py input.m4a --binaural --personalized-headphone --binaural-mode near
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --source-reference-distance-m 2.193
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --sofa-room
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --sofa-room --sofa-room-preset legacy
|
|
||||||
python main.py input.m4a --binaural --personalized-headphone `
|
|
||||||
--rosella-filterbank 2
|
|
||||||
python main.py input.m4a --binaural --binaural-output output.binaural.wav
|
python main.py input.m4a --binaural --binaural-output output.binaural.wav
|
||||||
```
|
```
|
||||||
|
|
||||||
With no explicit input, use `HRTF/binaural.sofa` under the project directory.
|
|
||||||
If missing, try the original personalized default. Use `--sofa-hrtf PATH` for
|
|
||||||
other locations. Compact input is always explicit, with no automatic export or fallback.
|
|
||||||
|
|
||||||
- `.sofa` is the portable source of truth; it can hold self-scanned or any
|
|
||||||
generic HRTF data.
|
|
||||||
- `.personalized_headphone` is a model produced by Dolby's official
|
- `.personalized_headphone` is a model produced by Dolby's official
|
||||||
personalization scan; its JSON parsing is implemented by this project
|
personalization scan; its JSON parsing is implemented by this project
|
||||||
(`src/rosella_model.py`) and does not invoke any Dolby software.
|
(`src/rosella_model.py`) and does not invoke any Dolby software. Near/Mid/Far
|
||||||
- `.jochrtf` is an explicitly exported compact field; its format and loader/writer
|
and the room handling are unchanged.
|
||||||
remain available. High-precision rendering reads SOFA directly.
|
- `.jochrtf` is a directional field exported offline from a SOFA. This repository
|
||||||
|
loads and renders it; the compiler is a separate offline tool and its output
|
||||||
|
loads through `--compiled-hrtf-cache`.
|
||||||
|
|
||||||
The personalized default remains `HRTF/binaural.personalized_headphone`.
|
The `.jochrtf` route keeps Near/Mid/Far point-source scales of approximately
|
||||||
SOFA and compact routes retain Near/Mid/Far point-source scales of approximately
|
1, 2.193 and 6.402 m, default Mid. It renders only the field's direct binaural
|
||||||
1, 2.193 and 6.402 m, default Mid. Direct-only rendering does not alter the measured
|
sound by default; `--jochrtf-room on` adds the project room (six first-order image
|
||||||
radius, HRIR, programme gain or flight time. `--sofa-room` enables the original project
|
reflections plus an FDN tail, computed in code with no asset files).
|
||||||
image-source/FDN coupling. Near/Mid default to the compact room, Far to the original
|
|
||||||
large room. `--sofa-room-preset compact|legacy` selects either explicitly; both FIR
|
|
||||||
assets ship with the code.
|
|
||||||
Free-field HRIR SOFA is supported; BRIR loading is not implemented yet. Future BRIR
|
|
||||||
input will retain its recorded room and ignore the additional room switch.
|
input will retain its recorded room and ignore the additional room switch.
|
||||||
See [Binaural rendering](docs/binaural.en.md).
|
See [Binaural rendering](docs/binaural.en.md).
|
||||||
Because the cache contains transformed
|
Because a cache contains transformed
|
||||||
HRTF data, its use and redistribution remain subject to the source dataset's
|
HRTF data, its use and redistribution remain subject to the source dataset's
|
||||||
terms. See [Binaural Rendering](docs/binaural.en.md) and
|
terms. See [Binaural Rendering](docs/binaural.en.md) and
|
||||||
[Third-party notices](THIRD_PARTY_NOTICES.md) for format boundaries, formulas,
|
[Third-party notices](THIRD_PARTY_NOTICES.md) for format boundaries, formulas,
|
||||||
@@ -151,7 +128,7 @@ Speaker and binaural output share peak analysis, the WAV writer, and clipping po
|
|||||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action abort
|
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action abort
|
||||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action float32
|
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action float32
|
||||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action continue
|
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action continue
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa `
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf `
|
||||||
--binaural-format int24 --clip-action abort
|
--binaural-format int24 --clip-action abort
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
> JustOneCacophony 是一个 E-AC-3 JOC 的实验性 / 测试实现,用于研究 JOC 的解析、重建、渲染以及相关数学过程。
|
> JustOneCacophony 是一个 E-AC-3 JOC 的实验性 / 测试实现,用于研究 JOC 的解析、重建、渲染以及相关数学过程。
|
||||||
|
|
||||||
项目可以从常见 E-AC-3 JOC 码流中提取并解析 EMDF、ID14 JOC 参数和 ID11 OAMD 元数据,结合 FFmpeg 解码出的核心 PCM(N=5 或 7 个全频段通道)重建 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,或输出双耳 WAV(Rosella 个性化模型,或已编译的 `.jochrtf`)。
|
||||||
|
|
||||||
这是研究代码,不是完整、标准兼容或生产级的 JOC 解码器。它只覆盖当前已实现的码流形态;遇到未知变体时会明确报错,而不是假装一切都很和谐——如果哪里算错了,它可能就真的只剩 cacophony 了。
|
这是研究代码,不是完整、标准兼容或生产级的 JOC 解码器。它只覆盖当前已实现的码流形态;遇到未知变体时会明确报错,而不是假装一切都很和谐——如果哪里算错了,它可能就真的只剩 cacophony 了。
|
||||||
|
|
||||||
@@ -20,8 +20,8 @@
|
|||||||
- 输出 25 声道 ADM BWF:10 声道 7.1.2 bed(除 LFE 外静音)+ 15 个对象;
|
- 输出 25 声道 ADM BWF:10 声道 7.1.2 bed(除 LFE 外静音)+ 15 个对象;
|
||||||
- 直接渲染 `2.0`、`3.1`、`4.0`、`5.1`、`7.1`、`5.1.2`、`5.1.4`、`7.1.2`、`7.1.4`、`9.1.4`、
|
- 直接渲染 `2.0`、`3.1`、`4.0`、`5.1`、`7.1`、`5.1.2`、`5.1.4`、`7.1.2`、`7.1.4`、`9.1.4`、
|
||||||
`9.1.6`(`4.0` 是 Quad 布局,前后各一对;声道排列见[扬声器布局与声道顺序](#扬声器布局与声道顺序));
|
`9.1.6`(`4.0` 是 Quad 布局,前后各一对;声道排列见[扬声器布局与声道顺序](#扬声器布局与声道顺序));
|
||||||
- 从 `pcm16 + ID11/OAMD` 直接运行公开 SOFA 双耳渲染,不生成临时 ADM BWF;
|
- 从 `pcm16 + ID11/OAMD` 直接运行双耳渲染,不生成临时 ADM BWF;
|
||||||
- 双耳 DSP 全程使用 float64/complex128;SOFA保留完整HRIR、跨帧卷积和独立参考环境状态,个性化路线保留原滤波器组与延迟规则;
|
- 双耳 DSP 全程使用 float64/complex128;个性化路线保留原滤波器组与延迟规则,`.jochrtf` 路线保留 77 带方向场与跨帧状态;
|
||||||
- 直接输出统一支持 float32 或 PCM24 WAV,并在 PCM24 削波前提供明确处理策略;
|
- 直接输出统一支持 float32 或 PCM24 WAV,并在 PCM24 削波前提供明确处理策略;
|
||||||
- 全程使用 NumPy 的纯 Python 实现;
|
- 全程使用 NumPy 的纯 Python 实现;
|
||||||
- 读取或写入 metadata sidecar,并生成元数据、运行时间和输出摘要。
|
- 读取或写入 metadata sidecar,并生成元数据、运行时间和输出摘要。
|
||||||
@@ -37,10 +37,10 @@ M4A / E-AC-3
|
|||||||
└─ LFE + 15 objects
|
└─ LFE + 15 objects
|
||||||
├─ 25ch ADM BWF
|
├─ 25ch ADM BWF
|
||||||
├─ 指定布局的扬声器 WAV
|
├─ 指定布局的扬声器 WAV
|
||||||
└─ ID11 直接时间轴 + SOFA HRTF → 双耳 WAV
|
└─ ID11 直接时间轴 + HRTF(Rosella / .jochrtf)→ 双耳 WAV
|
||||||
```
|
```
|
||||||
|
|
||||||
JOC 对象重建、扬声器渲染与公开 SOFA 双耳渲染使用同一组数学过程:analysis QMF、
|
JOC 对象重建、扬声器渲染与双耳渲染使用同一组数学过程:analysis QMF、
|
||||||
参数插值、对象矩阵、inverse QMF。位流解析、OAMD 时间轴和命令行逻辑同样在 Python 中。
|
参数插值、对象矩阵、inverse QMF。位流解析、OAMD 时间轴和命令行逻辑同样在 Python 中。
|
||||||
|
|
||||||
## 环境
|
## 环境
|
||||||
@@ -85,46 +85,33 @@ python main.py input.m4a --speaker-layout 5.1 --speaker-format int24
|
|||||||
python main.py input.m4a --speaker-layout 7.1.2 --speaker-output output.7.1.2.wav
|
python main.py input.m4a --speaker-layout 7.1.2 --speaker-output output.7.1.2.wav
|
||||||
```
|
```
|
||||||
|
|
||||||
直接输出双耳渲染 WAV。SOFA与压缩输入默认仅渲染所选HRTF的直达声,可用`--sofa-room`启用项目自有房间;不做球头径向合成。个性化路线保持原Near/Mid/Far及房间处理。HRTF 输入支持三种来源:
|
直接输出双耳渲染 WAV。双耳有两条输入:
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
# 1) 完整SOFA HRIR(默认 HRTF/binaural.sofa,可显式指定)
|
# 1) Rosella .personalized_headphone(默认,缺省取 HRTF/binaural.personalized_headphone)
|
||||||
python main.py input.m4a --binaural
|
python main.py input.m4a --binaural
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa
|
|
||||||
|
|
||||||
# 2) Rosella .personalized_headphone(缺省取 HRTF/binaural.personalized_headphone)
|
|
||||||
python main.py input.m4a --binaural --personalized-headphone
|
|
||||||
python main.py input.m4a --binaural --personalized-headphone C:\HRTF\subject.personalized_headphone
|
python main.py input.m4a --binaural --personalized-headphone C:\HRTF\subject.personalized_headphone
|
||||||
|
python main.py input.m4a --binaural --personalized-headphone --binaural-mode far --rosella-filterbank 2
|
||||||
|
|
||||||
# 3) .jochrtf 编译缓存
|
# 2) .jochrtf 编译缓存(保留项:高精度离线渲染用)
|
||||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
||||||
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room off
|
||||||
# 常用选项
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa `
|
|
||||||
--sofa-interpolation lf_mca_zero
|
|
||||||
python src\compile_hrtf.py C:\HRTF\subject.sofa C:\HRTF\subject.jochrtf
|
|
||||||
python main.py input.m4a --binaural --personalized-headphone --binaural-mode near
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --source-reference-distance-m 2.193
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --sofa-room
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --sofa-room --sofa-room-preset legacy
|
|
||||||
python main.py input.m4a --binaural --personalized-headphone `
|
|
||||||
--rosella-filterbank 2
|
|
||||||
python main.py input.m4a --binaural --binaural-output output.binaural.wav
|
python main.py input.m4a --binaural --binaural-output output.binaural.wav
|
||||||
```
|
```
|
||||||
|
|
||||||
三者都不指定时先选择项目目录下的 `HRTF/binaural.sofa`;
|
|
||||||
缺失时尝试原个性化默认文件。其他位置通过 `--sofa-hrtf PATH` 显式指定。
|
|
||||||
`.jochrtf` 只显式选择,不自动生成或回退。
|
|
||||||
|
|
||||||
- `.sofa` 是可移植的 source of truth;可以是自行扫描或任何来源的通用 HRTF 数据。
|
|
||||||
- `.personalized_headphone` 是杜比官方软件个性化扫描得到的模型,其 JSON 解析由
|
- `.personalized_headphone` 是杜比官方软件个性化扫描得到的模型,其 JSON 解析由
|
||||||
本项目自行实现(`src/rosella_model.py`),不调用杜比软件。
|
本项目自行实现(`src/rosella_model.py`),不调用杜比软件;Near/Mid/Far 与房间
|
||||||
- `.jochrtf` 是从SOFA显式离线导出的轻量方向场,原格式及读写器保留;高精度路线直接读取SOFA。
|
处理保持原行为。
|
||||||
|
- `.jochrtf` 是从 SOFA 离线导出的方向场,本仓库负责加载与渲染;编译器是独立
|
||||||
|
的离线工具,产出的缓存用 `--compiled-hrtf-cache` 加载。
|
||||||
|
|
||||||
个性化默认模型仍为 `HRTF/binaural.personalized_headphone`。SOFA与压缩路线沿用Near约1 m、Mid约2.193 m、Far约6.402 m的点声源参考尺度,默认Mid。
|
`.jochrtf` 路线沿用 Near 约 1 m、Mid 约 2.193 m、Far 约 6.402 m 的点声源参考尺度,
|
||||||
默认不改变测量半径、HRIR、节目增益或传播延迟;开启`--sofa-room`后,使用原项目镜像早反射及FDN房间耦合。Near/Mid默认采用紧凑房间,Far默认采用原大房间;`--sofa-room-preset compact|legacy`可显式选择。两套晚期FIR均随代码提供。
|
默认 Mid。默认只输出方向场的双耳直达声;`--jochrtf-room on` 启用项目自有房间
|
||||||
当前支持自由场HRIR SOFA,BRIR尚未接入;后续BRIR会直接使用文件内房间响应并忽略额外房间开关。细节见[双耳渲染](docs/binaural.md)。cache 含有源 HRTF 的变换数据,使用与再分发
|
(六路一阶镜像早反射 + FDN 晚期,直接在代码里计算,不读取资产文件)。
|
||||||
仍受源数据许可约束;格式边界、计算公式、状态、时间轴及发布注意事项见
|
|
||||||
|
cache 含有源 HRTF 的变换数据,使用与再分发仍受源数据许可约束;
|
||||||
|
格式边界、计算公式、状态、时间轴及发布注意事项见
|
||||||
[双耳渲染](docs/binaural.md) 和 [第三方通知](THIRD_PARTY_NOTICES.md)。
|
[双耳渲染](docs/binaural.md) 和 [第三方通知](THIRD_PARTY_NOTICES.md)。
|
||||||
|
|
||||||
扬声器和双耳输出共享峰值检查、writer 与削波策略。在非交互环境请求 PCM24 且可能削波时,需要显式选择处理方式:
|
扬声器和双耳输出共享峰值检查、writer 与削波策略。在非交互环境请求 PCM24 且可能削波时,需要显式选择处理方式:
|
||||||
@@ -133,7 +120,7 @@ python main.py input.m4a --binaural --binaural-output output.binaural.wav
|
|||||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action abort
|
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action abort
|
||||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action float32
|
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action float32
|
||||||
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action continue
|
python main.py input.m4a --speaker-layout 5.1 --speaker-format int24 --clip-action continue
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa `
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf `
|
||||||
--binaural-format int24 --clip-action abort
|
--binaural-format int24 --clip-action abort
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -4,26 +4,6 @@
|
|||||||
|
|
||||||
This directory contains static production tables. It does not contain user HRTFs.
|
This directory contains static production tables. It does not contain user HRTFs.
|
||||||
|
|
||||||
SOFA and compact HRTF inputs render direct sound by default. Optional `--sofa-room`
|
|
||||||
loads generated FIR assets from the original four-line FDN in `public_room.py`.
|
|
||||||
It contains no HRTF or external-model coefficients. Early reflections use the same
|
|
||||||
module's six-wall image model and the selected full HRIRs or compact field. Personalized rendering
|
|
||||||
continues to use its model's room parameters.
|
|
||||||
|
|
||||||
Both archives use `sofa-project-room-v1`, 48 kHz and float64. `sofa-room-compact.npz`
|
|
||||||
contains `[2,37383]` taps and is the Near/Mid default; `sofa-room.npz` contains
|
|
||||||
`[2,81600]` taps for the Far default and explicit original-room selection. JSON records
|
|
||||||
FDN/geometry parameters, coefficient SHA-256 and omitted tail energy.
|
|
||||||
|
|
||||||
Compact dimensions follow [model 9 in Zahorik 2009, Table 1](https://pmc.ncbi.nlm.nih.gov/articles/PMC2730711/).
|
|
||||||
Late time scales follow the V/S ratio of the original project preset; this does not
|
|
||||||
reproduce the paper's BRIR algorithm. Regenerate both assets with:
|
|
||||||
|
|
||||||
```powershell
|
|
||||||
python src\generate_sofa_room.py
|
|
||||||
python src\generate_sofa_room.py --preset compact
|
|
||||||
```
|
|
||||||
|
|
||||||
`tables.npz` contains the JOC core decoding tables:
|
`tables.npz` contains the JOC core decoding tables:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
|
|||||||
+2
-19
@@ -4,22 +4,6 @@
|
|||||||
|
|
||||||
本目录保存 Python 生产路径使用的静态表数据,不保存用户 HRTF。
|
本目录保存 Python 生产路径使用的静态表数据,不保存用户 HRTF。
|
||||||
|
|
||||||
SOFA与压缩HRTF输入默认仅渲染直达声。可选`--sofa-room`读取下列资产,由原项目`public_room.py`中的同一四路FDN独立生成,不包含HRTF或外部模型系数。早反射按资产记录的几何使用同一六面镜像算法计算,随后使用所选完整HRIR或压缩方向场。个性化路线继续读取其模型自身的房间参数。
|
|
||||||
|
|
||||||
格式均为`sofa-project-room-v1`,采样率48 kHz。JSON记录FDN及几何参数、系数SHA-256和截断尾能量。
|
|
||||||
|
|
||||||
| 文件 | 预设 | `late_fir` |
|
|
||||||
|---|---|---|
|
|
||||||
| `sofa-room-compact.npz` | Near/Mid默认紧凑房间 | `float64[2,37383]` |
|
|
||||||
| `sofa-room.npz` | Far默认/可显式选择的原大房间 | `float64[2,81600]` |
|
|
||||||
|
|
||||||
紧凑预设的尺寸取自[Zahorik 2009表1的模型9](https://pmc.ncbi.nlm.nih.gov/articles/PMC2730711/),晚期时间尺度按本项目原预设的`V/S`比例缩放;它不是对论文BRIR的完整复现。可重生成两套资产:
|
|
||||||
|
|
||||||
```powershell
|
|
||||||
python src\generate_sofa_room.py
|
|
||||||
python src\generate_sofa_room.py --preset compact
|
|
||||||
```
|
|
||||||
|
|
||||||
`tables.npz` 保存 JOC 核心解码表:
|
`tables.npz` 保存 JOC 核心解码表:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
@@ -37,7 +21,7 @@ joc_huff_code_7ch_pos_index_sparse int64[6,2]
|
|||||||
|
|
||||||
## 双耳渲染表
|
## 双耳渲染表
|
||||||
|
|
||||||
`rosella_kernels.npz` 保存`.jochrtf`压缩渲染和个性化bank 1使用的64-QMF/77-hybrid固定表;完整SOFA路线直接在PCM域卷积:
|
`rosella_kernels.npz` 保存`.jochrtf`压缩渲染和个性化bank 1使用的64-QMF/77-hybrid固定表:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
format_version little-endian int32[1]
|
format_version little-endian int32[1]
|
||||||
@@ -99,6 +83,5 @@ HRTF 或 room 调参。
|
|||||||
|
|
||||||
标准可公开获取不等于获准实施相关专利。
|
标准可公开获取不等于获准实施相关专利。
|
||||||
|
|
||||||
`.sofa` 是用户可见的 source of truth;`.jochrtf` 是可删除、可从 SOFA 重建的
|
`.jochrtf` 是可删除、可从 SOFA 重建的 JOC compiled HRTF cache。cache 含有源 HRTF 的变换数据,仍受源 SOFA/HRTF
|
||||||
JOC compiled HRTF cache。cache 含有源 HRTF 的变换数据,仍受源 SOFA/HRTF
|
|
||||||
数据集的许可与再分发限制约束。
|
数据集的许可与再分发限制约束。
|
||||||
Binary file not shown.
Binary file not shown.
+55
-58
@@ -2,91 +2,88 @@
|
|||||||
|
|
||||||
[中文](binaural.md) · [README](../README.en.md)
|
[中文](binaural.md) · [README](../README.en.md)
|
||||||
|
|
||||||
## Independent input routes
|
JOC renders binaural audio from two kinds of input:
|
||||||
|
|
||||||
| Input | Rendering | Distance and environment |
|
| Input | Renderer | Distance and room |
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
| SOFA | Full complex directional HRIR interpolation, adaptive FIR length, 128-sample partitioned convolution | Direct by default; optional project room; no synthetic radial head filtering |
|
| `.personalized_headphone` (default) | Rosella DSP, filterbank and model parameters | the model's own Near/Mid/Far and room handling, unchanged |
|
||||||
| `.jochrtf` | Existing 77-band, fifth-order field and delay representation | Direct by default; same optional room |
|
| `.jochrtf` (reserved) | 77-band directional field, 64-QMF/hybrid analysis and synthesis | direct sound only by default; `--jochrtf-room on` adds the project room |
|
||||||
| `.personalized_headphone` | Existing DSP, filterbank and model parameters | Existing Near/Mid/Far and room processing unchanged |
|
|
||||||
|
|
||||||
SOFA never invokes the compact compiler, writes a compact cache or falls back to it. The compact format, validation and offline compiler remain available. SOFA and compact input use `DirectSofaBackend` with the object timeline.
|
A `.jochrtf` is fitted offline from SOFA measurements by a separate compiler;
|
||||||
|
this repository loads and renders the result through `--compiled-hrtf-cache`.
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
python main.py input.m4a --binaural
|
python main.py input.m4a --binaural
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --sofa-interpolation lf_mca_zero
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --source-reference-distance-m 2.193
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --sofa-room
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --sofa-room --sofa-room-preset legacy
|
|
||||||
python src\compile_hrtf.py C:\HRTF\subject.sofa C:\HRTF\subject.jochrtf
|
|
||||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
|
||||||
python main.py input.m4a --binaural --personalized-headphone --binaural-mode far --rosella-filterbank 2
|
python main.py input.m4a --binaural --personalized-headphone --binaural-mode far --rosella-filterbank 2
|
||||||
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
||||||
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||||
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||||
```
|
```
|
||||||
|
|
||||||
The default SOFA is `HRTF/binaural.sofa` under the project directory, independent of the working directory. Use `--sofa-hrtf PATH` for other locations. A single measurement shell is selected automatically; multiple shells require `--hrtf-radius-m`. Radial interpolation is not implemented. The personalized default remains `HRTF/binaural.personalized_headphone`.
|
Without an explicit input the renderer uses `HRTF/binaural.personalized_headphone`
|
||||||
|
from the project directory. A `.jochrtf` is never generated or guessed: it has to
|
||||||
SOFA and compact inputs retain the Near/Mid/Far point-source scales, default Mid. Personalized presets and ADM DBMD hints retain their existing behavior.
|
be named. Binaural DSP runs in float64/complex128 and updates parameters every 128
|
||||||
|
samples.
|
||||||
|
|
||||||
## Point-source scale
|
## Point-source scale
|
||||||
|
|
||||||
Near/Mid/Far reference distances are 1.00000465, 2.19327927 and 6.40177584 m. `--source-reference-distance-m` overrides the selected scale. For ADM right/front/up coordinates `p=(x,y,z)`:
|
The `.jochrtf` route keeps the Near/Mid/Far point-source scales of about 1 m,
|
||||||
|
2.193 m and 6.402 m, defaulting to Mid. For an ADM right/front/up position
|
||||||
|
`p = (x, y, z)`:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
q_SOFA = (y, -x, z) / ||p||
|
q_SOFA = (y, -x, z) / ||p||
|
||||||
d_nominal = reference_distance_m * ||p||
|
d_nominal = reference_distance_m * ||p||
|
||||||
H_render(ear, f) = H_selected_shell(ear, f, q_SOFA)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
This defines nominal metric object coordinates, without turning a 1.2 m measurement into measured 2.193 m data or changing SOFA metadata. Direct-only rendering adds no inverse-distance attenuation, radial sphere ratio, synthetic centre response, room send, dry/wet coupling or distance-dependent flight time. Programme gain and the SOFA response scale are retained. The optional room uses the original project coupling; explicit output gain is applied once.
|
The reference distance only gives object coordinates a nominal metric scale: it
|
||||||
|
does not change the field response and adds no 1/r attenuation or propagation
|
||||||
|
delay. A coincident source keeps its last valid direction, or front when there is
|
||||||
|
no history.
|
||||||
|
|
||||||
A coincident source has no defined direction. Retain that object's last valid direction, or use front when no history exists. No zero-ITD response is synthesized. Finite outside coordinates retain the metadata distance factor rather than being clamped into the unit box.
|
## The `.jochrtf` room
|
||||||
|
|
||||||
SOFA requires no personalized model or machine-local configuration. Direct-only rendering does not read a room asset. Reports record input identity, measured radius, interpolation and the room switch.
|
Off by default. `--jochrtf-room on` enables the project's own room, the same algorithm
|
||||||
|
the earlier public route used, computed in code with no asset files:
|
||||||
|
|
||||||
## Optional project room
|
- six first-order image reflections (`public_room.first_order_image_sources`, the
|
||||||
|
original 18x18x14 m geometry);
|
||||||
|
- a shared FDN tail (four orthogonal Hadamard feedback lines, two all-pass diffusers,
|
||||||
|
low-pass damping, RT60 0.85 s);
|
||||||
|
- early reflections scaled by measurement radius over path distance, late send scaled
|
||||||
|
by mode and normalised radius.
|
||||||
|
|
||||||
`--sofa-room` is off by default. It reuses the original `public_room.py` and `reference_distance.py` algorithms with original and compact parameter sets: six first-order image reflections plus a shared four-line orthogonal Hadamard FDN with two all-pass diffusers and low-pass damping. The project listening preset aims to approximate an ideal listening environment dominated by direct sound. It is not a measured BRIR or a mandated standard room. No Rosella room table is required.
|
It is not a measured BRIR and not an external standard room. With the switch off the
|
||||||
|
renderer skips room processing and outputs only the field's direct binaural sound; the
|
||||||
Near/Mid default to `compact`; Far defaults to `legacy` to retain its unit-coordinate
|
room parameters are fixed at construction, with no preset and no external file.
|
||||||
source range. Select either with `--sofa-room-preset compact|legacy`. Compact dimensions
|
|
||||||
come from [model 9 in Zahorik 2009, Table 1](https://pmc.ncbi.nlm.nih.gov/articles/PMC2730711/).
|
|
||||||
The same project algorithm scales late delays, diffusion times and the RT60 parameter
|
|
||||||
by V/S. Only the geometry is borrowed; this does not reproduce the paper's BRIR model.
|
|
||||||
|
|
||||||
Full-HRIR rendering evaluates the selected SOFA at each reflected direction. Fractional delays use phase ramps across the full FFT grid, with extra delay `(L_path-L_direct)/c`; native `Data.Delay` is retained once. Direct, early and late gains follow the original project policy. Sources must lie inside the optional room geometry; outside positions are not silently clipped.
|
|
||||||
|
|
||||||
Both FDNs are excited by a unit impulse at 48 kHz and stored as stereo float64 FIRs.
|
|
||||||
`data/sofa-room-compact.npz` has 37,383 taps and an RT60 parameter of about 0.3894 s;
|
|
||||||
`data/sofa-room.npz` retains the original 81,600 taps and 0.85 s parameter. Omitted probe
|
|
||||||
tail energy is about `2.2e-13` in both. PCM FFT convolution realizes each time-invariant
|
|
||||||
system. Early reflections use the selected SOFA; late output retains the original
|
|
||||||
stereo FDN definition.
|
|
||||||
|
|
||||||
Late FIR alignment includes the convolution implementation shift and the selected SOFA's common recording-delay estimate. Enabled room processing uses 1536-sample batches while retaining 128-sample parameter updates; extra flush padding is discarded. Early reflections in `.jochrtf` retain their 77-band approximation. Regenerate both assets with `python src\generate_sofa_room.py`, or select one using `--preset compact|legacy`; the archives record parameters, provenance, coefficient hash and tail budget.
|
|
||||||
|
|
||||||
## Full HRIR and time reference
|
|
||||||
|
|
||||||
The importer validates `SimpleFreeFieldHRIR` versions 0.4/1.0/1.1, FIR data, two consistently identifiable left/right receivers, zero emitter offset, free-field metadata, units and finite numbers. Processing uses 48 kHz; resampling scales sample-valued `Data.Delay` accordingly. Receivers must remain fixed across measurements; angular interpolation no longer fits a symmetric spherical head.
|
|
||||||
|
|
||||||
BRIR and SOFA conventions other than `SimpleFreeFieldHRIR` are not implemented. Unsupported files are rejected. The backend has a response-type guard to announce BRIR and ignore `--sofa-room` once BRIR loading is implemented; current tests cover that policy interface, not a BRIR loader.
|
|
||||||
|
|
||||||
A 3-D convex hull of measured unit directions defines local triangles. Ray/triangle barycentric weights are nonnegative and sum to one. There is no longitude/latitude planar interpolation or extrapolation beyond directional coverage. Measured nodes retain their complex response.
|
|
||||||
|
|
||||||
Low-frequency alignment fits delay over 200–1500 Hz. Default `lf_logmag_phase` interpolates log magnitude and reconstructs phase from aligned complex residuals and interpolated delay. Optional `lf_mca_zero` applies auditory-band magnitude correction. Neither replaces the selected full FIR with a minimum-phase response.
|
|
||||||
|
|
||||||
`Data.Delay` is included once. A common half-FFT shift stores interpolation pre-ringing; output compensates only convolution/filterbank implementation latency. The file's common recording delay, relative ear delays and frequency-dependent phase remain intact. The fitted recording origin is no longer subtracted, and target distance adds no flight time. DC and Nyquist use real endpoints for a real output sequence.
|
|
||||||
|
|
||||||
Each object retains its own convolution history. Updates crossfade old/new outputs over 128 samples using complete input history for both filters. Filter updates are delayed by the common artificial FIR shift so removing that shift does not advance metadata changes. Full HRIR uses PCM convolution; QMF/hybrid is confined to explicit compact input. Arithmetic is float64/complex128, without implicit limiting.
|
|
||||||
|
|
||||||
## Object input
|
## Object input
|
||||||
|
|
||||||
Each reconstructed frame is `[1536,16]` PCM: LFE at slot 0 and fifteen reconstructed object/bed slots at 1–15. The ID11 timeline feeds these directly, without another decode or an intermediate loudspeaker render. The existing 1473-sample object compensation and 32-sample metadata alignment are retained. Parameters update every 128 samples.
|
Each reconstructed frame is `[1536,16]` PCM: LFE at slot 0 and fifteen
|
||||||
|
reconstructed object/bed slots at 1–15. The ID11 timeline feeds these directly,
|
||||||
|
without another decode or an intermediate loudspeaker render. The existing
|
||||||
|
1473-sample object compensation and 32-sample metadata alignment are retained.
|
||||||
|
Parameters update every 128 samples.
|
||||||
|
|
||||||
LFE retains separate low-pass treatment and no room send. SOFA metadata preserves negative elevation, finite outside factors and anchoring flags; personalized parsing and timing are unchanged. Multi-position blocks, ISF, explicit infinity and anchors requiring unavailable screen calibration are rejected. `finish()` drains once. The tail option bounds FIR/filterbank and optional room draining.
|
LFE keeps its separate low-pass treatment and no room send. Multi-position blocks,
|
||||||
|
ISF, explicit infinity and anchors requiring unavailable screen calibration are
|
||||||
|
rejected. `finish()` drains once. `--binaural-tail-seconds` bounds filterbank and
|
||||||
|
room draining.
|
||||||
|
|
||||||
## Compact representation
|
## Compact representation
|
||||||
|
|
||||||
`.jochrtf` v1 remains a pickle-free NumPy ZIP containing JSON metadata, 77 band centres, `complex128[36,2,77]` directional coefficients, `float64[36,2]` delay coefficients and `float64[2,2]` bounds. Its format, cache key, hashes, atomic writing and source-data licensing remain unchanged. Its angular/frequency approximation is not equivalent to full-HRIR precision.
|
`.jochrtf` v1 is a pickle-free NumPy ZIP holding JSON metadata, 77 band centres,
|
||||||
|
`complex128[36,2,77]` directional coefficients, `float64[36,2]` delay coefficients
|
||||||
|
and `float64[2,2]` bounds. Loading checks the format version, shapes and scalar
|
||||||
|
ranges, rehashes every member, and recomputes the cache key to confirm that the
|
||||||
|
coefficients match their configuration; a mismatch rejects the file.
|
||||||
|
|
||||||
Filterbank provenance is documented in [data notes](../data/README.en.md). See the [MCA paper](https://doi.org/10.1109/TASLP.2023.3313908) for the correction model. Algorithms are independently implemented; external measurements retain their own licensing terms.
|
This repository owns the format, the cache key and the hash validation; the
|
||||||
|
fitting compiler lives in the separate checkout and shares the same format module
|
||||||
|
instead of duplicating it. The compact representation keeps its angular and
|
||||||
|
band-wise approximation error and is not equivalent to per-measurement precision.
|
||||||
|
|
||||||
|
Filterbank provenance is documented in [data notes](../data/README.en.md). See the
|
||||||
|
[MCA paper](https://doi.org/10.1109/TASLP.2023.3313908) for the correction model.
|
||||||
|
Algorithms are independently implemented; external measurements retain their own
|
||||||
|
licensing terms.
|
||||||
+38
-50
@@ -2,84 +2,72 @@
|
|||||||
|
|
||||||
[English](binaural.en.md) · [返回 README](../README.md)
|
[English](binaural.en.md) · [返回 README](../README.md)
|
||||||
|
|
||||||
## 三条独立路线
|
JOC 的双耳输出有两条输入路线:
|
||||||
|
|
||||||
| 输入 | 渲染 | 距离与环境 |
|
| 输入 | 渲染 | 距离与环境 |
|
||||||
|---|---|---|
|
|---|---|---|
|
||||||
| SOFA | 完整复数HRIR方向插值、自适应长度FIR、128样本分区卷积 | 默认纯直达声;`--sofa-room`可选项目房间;不做径向人头合成 |
|
| `.personalized_headphone`(默认) | Rosella DSP、滤波器组与模型参数 | 模型自身的 Near/Mid/Far 与房间处理,行为不变 |
|
||||||
| `.jochrtf` | 原77带/五阶方向场及延迟表示 | 同样默认纯直达声,可选相同房间 |
|
| `.jochrtf`(保留项) | 77 带方向场、64-QMF/hybrid 分析与合成 | 默认只输出直达声;`--jochrtf-room on` 开启项目房间 |
|
||||||
| `.personalized_headphone` | 原DSP、滤波器组及模型参数 | 原Near/Mid/Far及房间处理保持不变 |
|
|
||||||
|
|
||||||
SOFA不会调用编译器、生成压缩缓存或自动回退。`.jochrtf`格式、校验器和离线编译能力保留。SOFA与压缩输入通过`DirectSofaBackend`接入对象时间线。
|
`.jochrtf` 由独立的离线编译器从 SOFA 测量数据拟合得到;本仓库负责加载和渲染
|
||||||
|
编译结果,编译器产出的缓存用 `--compiled-hrtf-cache` 加载。
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
python main.py input.m4a --binaural
|
python main.py input.m4a --binaural
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --sofa-interpolation lf_mca_zero
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --source-reference-distance-m 2.193
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --sofa-room
|
|
||||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --sofa-room --sofa-room-preset legacy
|
|
||||||
python src\compile_hrtf.py C:\HRTF\subject.sofa C:\HRTF\subject.jochrtf
|
|
||||||
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
|
||||||
python main.py input.m4a --binaural --personalized-headphone --binaural-mode far --rosella-filterbank 2
|
python main.py input.m4a --binaural --personalized-headphone --binaural-mode far --rosella-filterbank 2
|
||||||
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf
|
||||||
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||||
|
python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrtf --jochrtf-room on
|
||||||
```
|
```
|
||||||
|
|
||||||
默认SOFA为项目目录下的`HRTF/binaural.sofa`,不依赖当前工作目录;其他位置用`--sofa-hrtf PATH`指定。单球壳自动选择,多球壳须用`--hrtf-radius-m`选择一个测量球壳,当前不做径向插值。默认个性化路径仍为`HRTF/binaural.personalized_headphone`。
|
不指定输入时使用项目目录下的 `HRTF/binaural.personalized_headphone`;`.jochrtf` 只能
|
||||||
|
显式选择,不自动生成,也不回退。双耳 DSP 全程 float64/complex128,参数每 128 样本更新。
|
||||||
SOFA与压缩输入沿用Near/Mid/Far的点声源参考尺度,默认Mid。个性化的Near/Mid/Far和ADM输出的DBMD提示保持原行为。
|
|
||||||
|
|
||||||
## 点声源参考尺度
|
## 点声源参考尺度
|
||||||
|
|
||||||
Near/Mid/Far的参考距离分别为1.00000465/2.19327927/6.40177584 m,可用`--source-reference-distance-m`覆盖所选档位。对于ADM右/前/上坐标`p=(x,y,z)`:
|
`.jochrtf` 路线沿用 Near 约 1 m、Mid 约 2.193 m、Far 约 6.402 m 的点声源参考尺度,默认
|
||||||
|
Mid。对于 ADM 右/前/上坐标 `p=(x,y,z)`:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
q_SOFA = (y, -x, z) / ||p||
|
q_SOFA = (y, -x, z) / ||p||
|
||||||
d_nominal = reference_distance_m * ||p||
|
d_nominal = reference_distance_m * ||p||
|
||||||
H_render(ear, f) = H_selected_shell(ear, f, q_SOFA)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
参考距离给对象坐标规定标称米制尺度,不会把测量半径为1.2 m的HRIR合成为2.193 m真值,也不会更改SOFA测量元数据。默认直达声模式不额外应用1/r衰减、球头距离比值、人工中心响应、房间发送、direct/room能量耦合或目标距离传播延迟;节目增益和SOFA响应刻度保持不变。开启房间后采用原项目距离耦合,用户指定输出增益仍只应用一次。
|
参考距离只给对象坐标规定标称米制尺度,不改变方向场的响应,也不额外增加 1/r 衰减或
|
||||||
|
传播延迟。声源与听者重合时沿用该对象最后一个有效方向,没有历史时取正前方。
|
||||||
|
|
||||||
声源与听者重合时方向没有定义,沿用该对象最后一个有效方向;没有历史时取正前方。该规则不合成零ITD响应。有限盒外坐标保留元数据距离因子,不裁回单位盒。
|
## `.jochrtf` 房间
|
||||||
|
|
||||||
SOFA输入不需要个性化模型或本机配置;关闭房间时也不读取房间资产。报告记录输入路径、SHA-256、测量半径、插值方法与房间开关。
|
默认关闭。`--jochrtf-room on` 启用项目自有房间,算法与 Rosella 之前的公开路线一致,
|
||||||
|
直接在代码里计算,不读取任何资产:
|
||||||
|
|
||||||
## 可选项目房间
|
- 六个一阶镜像反射(`public_room.first_order_image_sources`,18×18×14 m 原房间几何);
|
||||||
|
- 四路正交 Hadamard 反馈、两级全通扩散、低通阻尼的共享 FDN 晚期(RT60 0.85 s);
|
||||||
|
- 早反射按测量半径/路径距离给增益,晚期发送按档位与归一化半径给增益。
|
||||||
|
|
||||||
`--sofa-room`默认关闭,复用原`.jochrtf`路线的`public_room.py`和`reference_distance.py`算法,提供原始与紧凑两套参数:六个一阶镜像反射,以及四路正交Hadamard反馈、两级全通扩散、低通阻尼的共享FDN。它是项目自有听感预设,目标是提供接近理想听音环境、以直达声为主的房间感,不是测量BRIR或强制标准房间。它不依赖Rosella房间表。
|
它不是测量 BRIR,也不是外部标准房间。关闭时跳过房间处理,只输出方向场的双耳直达声;
|
||||||
|
房间参数在构造时确定,没有预设、没有外部文件。
|
||||||
启用后Near/Mid默认使用`compact`紧凑房间,Far默认使用`legacy`原大房间,以保留Far单位坐标盒的空间范围。`--sofa-room-preset compact|legacy`可显式选择;`legacy`指原项目大房间。紧凑房间尺寸引用[Zahorik 2009表1模型9](https://pmc.ncbi.nlm.nih.gov/articles/PMC2730711/),保留原镜像/FDN算法,将晚期延迟、扩散时间与RT60参数按`V/S`比例缩短。参数及来源记录在资产元数据中。这仅借用文献几何,不是该论文BRIR算法的复现。
|
|
||||||
|
|
||||||
完整HRIR路线对六个到达方向分别查询HRIR,以完整频率网格上的相位斜坡实现分数采样延迟。反射路径相对直达路径的额外时间为`(L_path-L_direct)/c`,保留原HRIR的`Data.Delay`,不再量化到77带。直接声、反射与晚期发送采用原项目的增益公式。房间几何只覆盖其内部声源;开启房间时不静默裁剪盒外位置。
|
|
||||||
|
|
||||||
晚期FDN在48 kHz下用单位脉冲生成双声道float64 FIR:紧凑版为`data/sofa-room-compact.npz`,37,383点、RT60参数约0.3894秒;原版为`data/sofa-room.npz`,81,600点、RT60参数0.85秒。两者截断后的探测尾能量比均约`2.2e-13`。运行时通过PCM域FFT卷积实现各自同一线性时不变系统。早反射继续使用所选SOFA;晚期输出保持原项目的双声道FDN定义。
|
|
||||||
|
|
||||||
晚期分支补齐卷积实现移位和所选SOFA的共同录音延迟估计后,与直接声及早反射相加。房间关闭时不创建这些状态;开启时按1536样本计算批次处理,参数仍每128样本更新,排空时裁去额外批处理填充。`.jochrtf`的早反射保持其77带表示误差。
|
|
||||||
|
|
||||||
可用`python src\generate_sofa_room.py`重生成两套资产,或以`--preset compact|legacy`只生成一套。原算法来源、参数、系数哈希及尾部精度预算写入NPZ元数据。
|
|
||||||
|
|
||||||
## 完整HRIR数学与时间基准
|
|
||||||
|
|
||||||
导入器校验`SimpleFreeFieldHRIR` 0.4/1.0/1.1、FIR、两个可由位置识别左右的耳、零Emitter偏移、自由场、单位和有限数值。采样率统一为48 kHz;重采样时`Data.Delay`的采样点数同比例变化。耳位须在测量之间固定;方向插值不再要求用对称耳位拟合球头。
|
|
||||||
|
|
||||||
当前没有实现`SimpleFreeFieldHRIR`以外的BRIR或其他SOFA Convention。不支持的文件明确拒绝。后端已按响应类型预留BRIR提示及忽略`--sofa-room`的规则;当前验证的是该策略接口,不代表已实现BRIR读取。
|
|
||||||
|
|
||||||
测量单位方向的三维凸包构成局部三角网格,查询使用射线与面三角形的非负、和为1的重心权重。不使用经纬平面插值,不外推未覆盖半球。精确节点返回测量复数响应。
|
|
||||||
|
|
||||||
低频对齐延迟在200–1500 Hz拟合。默认`lf_logmag_phase`对对数幅度插值,原相位通过时间对齐的复数残差及插值延迟恢复;备选`lf_mca_zero`使用听觉带幅度校正。两种方法都保留所选FFT长度的完整FIR,不替换为最小相位。
|
|
||||||
|
|
||||||
`Data.Delay`在频域只叠加一次。公共移位为FFT长度的一半,用于保存插值前振铃;输出只补偿卷积/滤波器组的实现延迟。文件提供的共同录音延迟、左右耳相对延迟和频率相关相位关系均保留,不再减去拟合录音原点,也不根据目标距离增加飞行时间。真实序列的DC/Nyquist频点按实数端点处理。
|
|
||||||
|
|
||||||
每对象拥有独立卷积历史。更新时旧/新输出在128样本内线性交叉淡化,两个滤波器均使用完整输入历史。滤波器更新同样延后公共FIR实现移位,避免移除实现延迟后将元数据更新提前。完整HRIR路线直接在PCM域卷积;QMF/hybrid仅用于显式压缩输入。内部精度为float64/complex128,不做隐式限幅。
|
|
||||||
|
|
||||||
## 对象接入
|
## 对象接入
|
||||||
|
|
||||||
输入为每帧`[1536,16]` PCM:0是LFE,1–15是已经重建的对象/bed槽。直接使用ID11时间线,不重新解码,也不先渲染到扬声器布局。保留1473样本对象时间补偿和32样本元数据对齐,参数每128样本更新。
|
输入为每帧 `[1536,16]` PCM:0 是 LFE,1–15 是已经重建的对象/bed 槽。直接使用 ID11
|
||||||
|
时间线,不重新解码,也不先渲染到扬声器布局。保留 1473 样本对象时间补偿和 32 样本元数据
|
||||||
|
对齐,参数每 128 样本更新。
|
||||||
|
|
||||||
LFE继续使用独立低通处理,不发送至可选房间。SOFA专用解析保留负高度、边界外因子及锚定字段;个性化解析与时间线不变。当前多位置块、ISF、显式无穷远以及需要屏幕校准的锚定明确报错。`finish()`仅排空一次,`--binaural-tail-seconds`规定FIR/滤波器组及可选房间尾声的排空预算。
|
LFE 继续使用独立低通处理,不发送至房间。多位置块、ISF、显式无穷远以及需要屏幕校准的
|
||||||
|
锚定明确报错。`finish()` 仅排空一次,`--binaural-tail-seconds` 规定滤波器组与房间尾声的
|
||||||
|
排空预算。
|
||||||
|
|
||||||
## 轻量格式
|
## 轻量格式
|
||||||
|
|
||||||
`.jochrtf` v1仍为无pickle的NumPy ZIP,保存JSON元数据、77个频带中心、`complex128[36,2,77]`方向系数、`float64[36,2]`延迟系数及`float64[2,2]`边界。原格式、cache key、哈希校验、原子写入及源数据许可关系不变。压缩输入保留自身的方向与频率表示误差,不等同于完整HRIR精度。
|
`.jochrtf` v1 是无 pickle 的 NumPy ZIP,保存 JSON 元数据、77 个频带中心、
|
||||||
|
`complex128[36,2,77]` 方向系数、`float64[36,2]` 延迟系数及 `float64[2,2]` 边界。加载时
|
||||||
|
校验格式版本、形状、标量范围,按成员重算载荷哈希,并重新计算 cache key 以确认系数与
|
||||||
|
配置一致;不匹配的文件直接拒绝。
|
||||||
|
|
||||||
滤波器组来源见[data说明](../data/README.md)。方向插值和MCA的参考文献见[研究实现依据](https://doi.org/10.1109/TASLP.2023.3313908);算法独立实现,外部测量数据仍适用其自身许可。
|
格式、cache key 与哈希校验由本仓库维护;拟合编译器在独立目录里,两者共用同一个格式
|
||||||
|
模块,不复制定义。压缩表示保留自身的角向与频带近似误差,不等同于逐点测量的精度。
|
||||||
|
|
||||||
|
滤波器组来源见[data说明](../data/README.md)。方向插值与 MCA 的参考文献见
|
||||||
|
[研究实现依据](https://doi.org/10.1109/TASLP.2023.3313908);算法独立实现,外部测量数据
|
||||||
|
仍适用其自身许可。
|
||||||
@@ -30,10 +30,8 @@ from metadata import DirectPayloadIndex, PayloadIndex, write_summary
|
|||||||
import oamd_tracks
|
import oamd_tracks
|
||||||
from renderer import JocRenderer
|
from renderer import JocRenderer
|
||||||
from binaural_renderer import (
|
from binaural_renderer import (
|
||||||
DEFAULT_SOFA_HRTF,
|
JochrtfBinauralRenderer,
|
||||||
SofaBinauralRenderer,
|
|
||||||
resolve_compiled_hrtf_cache,
|
resolve_compiled_hrtf_cache,
|
||||||
resolve_sofa_hrtf,
|
|
||||||
)
|
)
|
||||||
from rosella_binaural_renderer import (
|
from rosella_binaural_renderer import (
|
||||||
DEFAULT_PERSONALIZED_HEADPHONE,
|
DEFAULT_PERSONALIZED_HEADPHONE,
|
||||||
@@ -42,7 +40,6 @@ from rosella_binaural_renderer import (
|
|||||||
resolve_personalized_headphone,
|
resolve_personalized_headphone,
|
||||||
)
|
)
|
||||||
from rosella_bank import DEFAULT_FILTERBANK, FILTERBANK_CHOICES, FILTERBANK_NAMES
|
from rosella_bank import DEFAULT_FILTERBANK, FILTERBANK_CHOICES, FILTERBANK_NAMES
|
||||||
from sofa_hrtf_field import DEFAULT_HRTF_CACHE_DIR
|
|
||||||
from speaker_backend import create_speaker_renderer
|
from speaker_backend import create_speaker_renderer
|
||||||
from speaker_layouts import (SPEAKER_LAYOUT_CHOICES, get_speaker_layout,
|
from speaker_layouts import (SPEAKER_LAYOUT_CHOICES, get_speaker_layout,
|
||||||
speaker_layout_display_name)
|
speaker_layout_display_name)
|
||||||
@@ -73,74 +70,29 @@ def resolve_output(source, requested=None, speaker_layout=None, *, binaural=Fals
|
|||||||
|
|
||||||
|
|
||||||
def resolve_binaural_hrtf_input(args, *, required):
|
def resolve_binaural_hrtf_input(args, *, required):
|
||||||
"""解析 binaural 的 HRTF 输入。
|
"""解析 binaural 的输入:显式 .jochrtf,或 Rosella 个性化模型。
|
||||||
|
|
||||||
默认选择完整SOFA;若缺失则尝试原个性化默认文件。压缩输入只能显式选择。
|
默认使用 HRTF/binaural.personalized_headphone;.jochrtf 只能显式选择。
|
||||||
只校验路径,不做编译。
|
只校验路径,不做编译。
|
||||||
"""
|
"""
|
||||||
sofa = args.sofa_hrtf
|
|
||||||
compiled = args.compiled_hrtf_cache
|
compiled = args.compiled_hrtf_cache
|
||||||
private = args.personalized_headphone
|
private = args.personalized_headphone
|
||||||
cache_policy = args.hrtf_cache_policy
|
|
||||||
cache_dir = args.hrtf_cache_dir
|
|
||||||
radius = args.hrtf_radius_m
|
|
||||||
|
|
||||||
if compiled is not None and cache_policy is not None:
|
if required and compiled is None and private is None:
|
||||||
raise ValueError("显式 .jochrtf 输入不能再指定 --hrtf-cache-policy")
|
|
||||||
if compiled is not None and radius is not None:
|
|
||||||
raise ValueError("显式 .jochrtf 输入不能再选择 SOFA radius shell")
|
|
||||||
if private is not None and (cache_policy is not None or cache_dir is not None
|
|
||||||
or radius is not None):
|
|
||||||
raise ValueError(
|
|
||||||
"Rosella 模型输入不能使用 "
|
|
||||||
"--hrtf-cache-policy/--hrtf-cache-dir/--hrtf-radius-m")
|
|
||||||
|
|
||||||
if required and sofa is None and compiled is None and private is None:
|
|
||||||
if DEFAULT_SOFA_HRTF.is_file():
|
|
||||||
sofa = DEFAULT_SOFA_HRTF
|
|
||||||
else:
|
|
||||||
if DEFAULT_PERSONALIZED_HEADPHONE.is_file():
|
if DEFAULT_PERSONALIZED_HEADPHONE.is_file():
|
||||||
private = DEFAULT_PERSONALIZED_HEADPHONE
|
private = DEFAULT_PERSONALIZED_HEADPHONE
|
||||||
|
|
||||||
if sofa is None and compiled is None and private is None:
|
if compiled is None and private is None:
|
||||||
if cache_policy is not None or cache_dir is not None or radius is not None:
|
|
||||||
raise ValueError("HRTF cache/radius 选项需要 --sofa-hrtf")
|
|
||||||
if required:
|
if required:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"--binaural 未找到 HRTF 输入:默认 "
|
"--binaural 未找到 HRTF 输入:默认 "
|
||||||
f"{DEFAULT_SOFA_HRTF}、{DEFAULT_PERSONALIZED_HEADPHONE} 与 "
|
f"{DEFAULT_PERSONALIZED_HEADPHONE} 不存在;请用 "
|
||||||
f"{DEFAULT_HRTF_CACHE_DIR} 下的 .jochrtf 缓存都不存在;请用 "
|
"--compiled-hrtf-cache PATH 或 --personalized-headphone PATH 指定")
|
||||||
"--sofa-hrtf PATH、--compiled-hrtf-cache PATH 或 "
|
|
||||||
"--personalized-headphone PATH 指定")
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if sofa is None and (cache_policy is not None or cache_dir is not None
|
|
||||||
or radius is not None):
|
|
||||||
raise ValueError("HRTF cache/radius 选项需要 --sofa-hrtf")
|
|
||||||
if cache_policy == "disk" or cache_dir is not None:
|
|
||||||
raise ValueError("SOFA直接渲染完整HRIR;请用 src/compile_hrtf.py 显式导出 .jochrtf")
|
|
||||||
if sofa is not None:
|
|
||||||
return {
|
|
||||||
"kind": "sofa",
|
|
||||||
"path": resolve_sofa_hrtf(sofa),
|
|
||||||
"cache_policy": None,
|
|
||||||
"cache_dir": None,
|
|
||||||
}
|
|
||||||
if compiled is not None:
|
if compiled is not None:
|
||||||
return {
|
return {"kind": "compiled_cache", "path": resolve_compiled_hrtf_cache(compiled)}
|
||||||
"kind": "compiled_cache",
|
return {"kind": "rosella", "path": resolve_personalized_headphone(private)}
|
||||||
"path": resolve_compiled_hrtf_cache(compiled),
|
|
||||||
"cache_policy": None,
|
|
||||||
"cache_dir": None,
|
|
||||||
}
|
|
||||||
if private is not None:
|
|
||||||
return {
|
|
||||||
"kind": "rosella",
|
|
||||||
"path": resolve_personalized_headphone(private),
|
|
||||||
"cache_policy": None,
|
|
||||||
"cache_dir": None,
|
|
||||||
}
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def executable(value, name):
|
def executable(value, name):
|
||||||
@@ -505,7 +457,7 @@ def _layout_choice(value: str) -> str:
|
|||||||
def build_parser():
|
def build_parser():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description=("JustOneCacophony (JOC):E-AC-3 JOC → 25ch ADM BWF、"
|
description=("JustOneCacophony (JOC):E-AC-3 JOC → 25ch ADM BWF、"
|
||||||
"扬声器 WAV 或公开 SOFA 双耳 WAV"))
|
"扬声器 WAV 或双耳 WAV"))
|
||||||
parser.add_argument("input", type=Path, help="输入 .m4a/.eac3/.ec3")
|
parser.add_argument("input", type=Path, help="输入 .m4a/.eac3/.ec3")
|
||||||
parser.add_argument("-o", "--output", type=Path, help="输出文件;默认按模式和布局命名")
|
parser.add_argument("-o", "--output", type=Path, help="输出文件;默认按模式和布局命名")
|
||||||
parser.add_argument("--speaker-output", type=Path,
|
parser.add_argument("--speaker-output", type=Path,
|
||||||
@@ -517,7 +469,7 @@ def build_parser():
|
|||||||
choices=SPEAKER_LAYOUT_CHOICES,
|
choices=SPEAKER_LAYOUT_CHOICES,
|
||||||
help="直接扬声器渲染布局,例如 2.0、4.0、5.1、7.1.2")
|
help="直接扬声器渲染布局,例如 2.0、4.0、5.1、7.1.2")
|
||||||
direct_mode.add_argument("--binaural", action="store_true",
|
direct_mode.add_argument("--binaural", action="store_true",
|
||||||
help="直接 SOFA 双耳渲染;不生成临时 ADM BWF")
|
help="直接双耳渲染;不生成临时 ADM BWF")
|
||||||
parser.add_argument("--speaker-format", choices=("float32", "int24"), default="float32",
|
parser.add_argument("--speaker-format", choices=("float32", "int24"), default="float32",
|
||||||
help="扬声器 WAV 格式,默认 float32")
|
help="扬声器 WAV 格式,默认 float32")
|
||||||
parser.add_argument("--binaural-format", choices=("float32", "int24"), default="float32",
|
parser.add_argument("--binaural-format", choices=("float32", "int24"), default="float32",
|
||||||
@@ -532,12 +484,9 @@ def build_parser():
|
|||||||
help="双耳参考距离档位或ADM DBMD提示,默认mid;支持near/mid/far。"
|
help="双耳参考距离档位或ADM DBMD提示,默认mid;支持near/mid/far。"
|
||||||
"off仅用于ADM BWF关闭DBMD双耳提示")
|
"off仅用于ADM BWF关闭DBMD双耳提示")
|
||||||
hrtf_input = parser.add_mutually_exclusive_group()
|
hrtf_input = parser.add_mutually_exclusive_group()
|
||||||
hrtf_input.add_argument(
|
|
||||||
"--sofa-hrtf", type=Path,
|
|
||||||
help=f"完整HRIR SOFA;默认 {DEFAULT_SOFA_HRTF};缺失时尝试原个性化默认文件")
|
|
||||||
hrtf_input.add_argument(
|
hrtf_input.add_argument(
|
||||||
"--compiled-hrtf-cache", type=Path,
|
"--compiled-hrtf-cache", type=Path,
|
||||||
help="高级入口:显式读取 JOC .jochrtf compiled cache")
|
help="显式读取 JOC .jochrtf compiled cache(保留给高精度离线渲染)")
|
||||||
hrtf_input.add_argument(
|
hrtf_input.add_argument(
|
||||||
"--personalized-headphone", type=Path, nargs="?",
|
"--personalized-headphone", type=Path, nargs="?",
|
||||||
const=DEFAULT_PERSONALIZED_HEADPHONE,
|
const=DEFAULT_PERSONALIZED_HEADPHONE,
|
||||||
@@ -547,23 +496,8 @@ def build_parser():
|
|||||||
"--rosella-filterbank", type=int, choices=FILTERBANK_CHOICES, default=None,
|
"--rosella-filterbank", type=int, choices=FILTERBANK_CHOICES, default=None,
|
||||||
help="Rosella 模型的滤波器组:1=13 taps/6 slots(默认),"
|
help="Rosella 模型的滤波器组:1=13 taps/6 slots(默认),"
|
||||||
"2=11 taps/3 slots;仅作用于个性化模型")
|
"2=11 taps/3 slots;仅作用于个性化模型")
|
||||||
parser.add_argument(
|
parser.add_argument("--jochrtf-room", choices=("on", "off"), default=None,
|
||||||
"--hrtf-cache-policy", choices=("none", "memory", "disk"), default=None,
|
help="压缩输入的项目房间:默认off;on 启用镜像早反射与FDN晚期")
|
||||||
help=argparse.SUPPRESS)
|
|
||||||
parser.add_argument(
|
|
||||||
"--hrtf-cache-dir", type=Path,
|
|
||||||
help=argparse.SUPPRESS)
|
|
||||||
parser.add_argument(
|
|
||||||
"--hrtf-radius-m", type=float, default=None,
|
|
||||||
help="选择最近的SOFA测量球壳;单球壳默认自动选择,多球壳必须指定")
|
|
||||||
parser.add_argument("--sofa-interpolation", choices=("lf_logmag_phase", "lf_mca_zero"),
|
|
||||||
default=None, help="完整HRIR局部插值,默认 lf_logmag_phase")
|
|
||||||
parser.add_argument("--source-reference-distance-m", type=float,
|
|
||||||
help="覆盖SOFA/压缩输入所选档位的点声源参考距离;默认mid约2.193 m")
|
|
||||||
parser.add_argument("--sofa-room", action="store_true",
|
|
||||||
help="可选:为SOFA/压缩输入启用项目自有房间模型;默认关闭")
|
|
||||||
parser.add_argument("--sofa-room-preset", choices=("compact", "legacy"),
|
|
||||||
help="房间预设;默认Near/Mid紧凑房间,Far原大房间;需要--sofa-room")
|
|
||||||
parser.add_argument("--binaural-tail-seconds", type=float, default=5.0,
|
parser.add_argument("--binaural-tail-seconds", type=float, default=5.0,
|
||||||
help="双耳 FIR/filterbank 排空上限,默认 5 秒")
|
help="双耳 FIR/filterbank 排空上限,默认 5 秒")
|
||||||
parser.add_argument("--binaural-tail-threshold", type=float, default=1.0e-8,
|
parser.add_argument("--binaural-tail-threshold", type=float, default=1.0e-8,
|
||||||
@@ -634,19 +568,12 @@ def main(argv=None):
|
|||||||
if args.speaker_metadata_offset < 0:
|
if args.speaker_metadata_offset < 0:
|
||||||
raise ValueError("speaker-metadata-offset 不能为负数")
|
raise ValueError("speaker-metadata-offset 不能为负数")
|
||||||
hrtf_options_used = any((
|
hrtf_options_used = any((
|
||||||
args.sofa_hrtf is not None,
|
|
||||||
args.compiled_hrtf_cache is not None,
|
args.compiled_hrtf_cache is not None,
|
||||||
args.personalized_headphone is not None,
|
args.personalized_headphone is not None,
|
||||||
args.hrtf_cache_policy is not None,
|
args.jochrtf_room is not None,
|
||||||
args.hrtf_cache_dir is not None,
|
|
||||||
args.hrtf_radius_m is not None,
|
|
||||||
args.sofa_interpolation is not None,
|
|
||||||
args.source_reference_distance_m is not None,
|
|
||||||
args.sofa_room,
|
|
||||||
args.sofa_room_preset is not None,
|
|
||||||
))
|
))
|
||||||
if hrtf_options_used and not binaural_mode:
|
if hrtf_options_used and not binaural_mode:
|
||||||
raise ValueError("SOFA/HRTF 选项仅与 --binaural 一起使用")
|
raise ValueError("HRTF/房间选项仅与 --binaural 一起使用")
|
||||||
if args.rosella_filterbank is not None and not binaural_mode:
|
if args.rosella_filterbank is not None and not binaural_mode:
|
||||||
raise ValueError("--rosella-filterbank 仅与 --binaural 一起使用")
|
raise ValueError("--rosella-filterbank 仅与 --binaural 一起使用")
|
||||||
rosella_filterbank = (DEFAULT_FILTERBANK if args.rosella_filterbank is None
|
rosella_filterbank = (DEFAULT_FILTERBANK if args.rosella_filterbank is None
|
||||||
@@ -659,8 +586,6 @@ def main(argv=None):
|
|||||||
raise ValueError("binaural-tail-threshold 必须是非负有限值")
|
raise ValueError("binaural-tail-threshold 必须是非负有限值")
|
||||||
if args.binaural_chunk_frames <= 0:
|
if args.binaural_chunk_frames <= 0:
|
||||||
raise ValueError("binaural-chunk-frames 必须大于 0")
|
raise ValueError("binaural-chunk-frames 必须大于 0")
|
||||||
if args.hrtf_radius_m is not None and (not math.isfinite(args.hrtf_radius_m) or args.hrtf_radius_m <= 0.0):
|
|
||||||
raise ValueError("hrtf-radius-m 必须是正有限值")
|
|
||||||
requested_output = (args.speaker_output if args.speaker_output is not None
|
requested_output = (args.speaker_output if args.speaker_output is not None
|
||||||
else args.binaural_output if args.binaural_output is not None
|
else args.binaural_output if args.binaural_output is not None
|
||||||
else args.output)
|
else args.output)
|
||||||
@@ -684,27 +609,14 @@ def main(argv=None):
|
|||||||
raise ValueError("eac3-target-level 必须在 -31..0 之间")
|
raise ValueError("eac3-target-level 必须在 -31..0 之间")
|
||||||
binaural_hrtf_input = resolve_binaural_hrtf_input(
|
binaural_hrtf_input = resolve_binaural_hrtf_input(
|
||||||
args, required=binaural_mode and not args.metadata_only)
|
args, required=binaural_mode and not args.metadata_only)
|
||||||
if args.source_reference_distance_m is not None:
|
if args.jochrtf_room is not None and (
|
||||||
if (not math.isfinite(args.source_reference_distance_m)
|
binaural_hrtf_input is None or binaural_hrtf_input["kind"] == "rosella"):
|
||||||
or args.source_reference_distance_m <= 0):
|
raise ValueError("--jochrtf-room 仅用于压缩输入;个性化模型使用自身房间处理")
|
||||||
raise ValueError("source-reference-distance-m 必须是正有限值")
|
|
||||||
if binaural_hrtf_input is None or binaural_hrtf_input["kind"] == "rosella":
|
|
||||||
raise ValueError("--source-reference-distance-m 仅用于 SOFA/压缩输入")
|
|
||||||
if binaural_hrtf_input is not None:
|
|
||||||
kind = binaural_hrtf_input["kind"]
|
|
||||||
if kind == "rosella" and args.sofa_interpolation is not None:
|
|
||||||
raise ValueError("HRIR插值选项仅用于SOFA路径")
|
|
||||||
if kind == "compiled_cache" and args.sofa_interpolation is not None:
|
|
||||||
raise ValueError("--sofa-interpolation不适用于已编译输入")
|
|
||||||
if args.sofa_room and (binaural_hrtf_input is None or binaural_hrtf_input["kind"] == "rosella"):
|
|
||||||
raise ValueError("--sofa-room 仅用于SOFA/压缩输入;个性化模型使用自身房间处理")
|
|
||||||
if args.sofa_room_preset is not None and not args.sofa_room:
|
|
||||||
raise ValueError("--sofa-room-preset 需要 --sofa-room")
|
|
||||||
if (args.rosella_filterbank is not None and binaural_hrtf_input is not None
|
if (args.rosella_filterbank is not None and binaural_hrtf_input is not None
|
||||||
and binaural_hrtf_input["kind"] != "rosella"):
|
and binaural_hrtf_input["kind"] != "rosella"):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"--rosella-filterbank 只作用于 Rosella .personalized_headphone 输入;"
|
"--rosella-filterbank 只作用于 Rosella .personalized_headphone 输入;"
|
||||||
"当前 SOFA/.jochrtf 路径不使用该滤波器组")
|
"当前压缩路径不使用该滤波器组")
|
||||||
ffmpeg = executable(args.ffmpeg, "FFmpeg")
|
ffmpeg = executable(args.ffmpeg, "FFmpeg")
|
||||||
decode_options = ()
|
decode_options = ()
|
||||||
decode_option_info = None
|
decode_option_info = None
|
||||||
@@ -831,22 +743,8 @@ def main(argv=None):
|
|||||||
"chunk_frames": args.binaural_chunk_frames,
|
"chunk_frames": args.binaural_chunk_frames,
|
||||||
"bed_channels": getattr(index, "bed_channels", ()),
|
"bed_channels": getattr(index, "bed_channels", ()),
|
||||||
}
|
}
|
||||||
if args.source_reference_distance_m is not None:
|
common_options["room"] = args.jochrtf_room == "on"
|
||||||
common_options["reference_distance_m"] = args.source_reference_distance_m
|
if hrtf_source["kind"] == "rosella":
|
||||||
if args.sofa_room:
|
|
||||||
common_options["sofa_room"] = True
|
|
||||||
common_options["room_preset"] = args.sofa_room_preset
|
|
||||||
if hrtf_source["kind"] == "sofa":
|
|
||||||
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,
|
|
||||||
interpolation=args.sofa_interpolation or "lf_logmag_phase",
|
|
||||||
**common_options)
|
|
||||||
elif hrtf_source["kind"] == "rosella":
|
|
||||||
binaural_decoder = timed_call(
|
binaural_decoder = timed_call(
|
||||||
timings, "create_binaural_renderer",
|
timings, "create_binaural_renderer",
|
||||||
RosellaBinauralRenderer,
|
RosellaBinauralRenderer,
|
||||||
@@ -861,7 +759,7 @@ def main(argv=None):
|
|||||||
else:
|
else:
|
||||||
binaural_decoder = timed_call(
|
binaural_decoder = timed_call(
|
||||||
timings, "create_binaural_renderer",
|
timings, "create_binaural_renderer",
|
||||||
SofaBinauralRenderer.from_compiled_cache,
|
JochrtfBinauralRenderer.from_compiled_cache,
|
||||||
hrtf_source["path"],
|
hrtf_source["path"],
|
||||||
**common_options)
|
**common_options)
|
||||||
print(
|
print(
|
||||||
@@ -870,8 +768,9 @@ def main(argv=None):
|
|||||||
f"precision=float64/complex128 "
|
f"precision=float64/complex128 "
|
||||||
f"hrtf={hrtf_source['kind']}:{hrtf_source['path']}", flush=True)
|
f"hrtf={hrtf_source['kind']}:{hrtf_source['path']}", flush=True)
|
||||||
if hrtf_source["kind"] != "rosella":
|
if hrtf_source["kind"] != "rosella":
|
||||||
room = binaural_decoder.backend.room
|
room = getattr(binaural_decoder.backend, "room_enabled", None)
|
||||||
print(f"[binaural] room={'off' if room is None else room.preset}", flush=True)
|
if room is not None:
|
||||||
|
print(f"[binaural] room={'on' if room else 'off'}", flush=True)
|
||||||
if hrtf_source["kind"] == "rosella":
|
if hrtf_source["kind"] == "rosella":
|
||||||
print(
|
print(
|
||||||
f"[binaural] filterbank={FILTERBANK_NAMES[binaural_decoder.filterbank]}"
|
f"[binaural] filterbank={FILTERBANK_NAMES[binaural_decoder.filterbank]}"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""JOC frame adapter for independent SOFA and compact HRTF rendering."""
|
"""JOC frame adapter for the compiled .jochrtf renderer."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import math
|
import math
|
||||||
@@ -8,11 +8,6 @@ import numpy as np
|
|||||||
|
|
||||||
from binaural_metadata import OamdPositionTimeline
|
from binaural_metadata import OamdPositionTimeline
|
||||||
from public_filterbank import ANALYSIS_SYNTHESIS_LATENCY_SAMPLES
|
from public_filterbank import ANALYSIS_SYNTHESIS_LATENCY_SAMPLES
|
||||||
from sofa_hrtf_field import (
|
|
||||||
DEFAULT_HRTF_CACHE_DIR,
|
|
||||||
DEFAULT_PROJECTION_RIDGE,
|
|
||||||
DEFAULT_SH_RIDGE,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
SAMPLE_RATE = 48000
|
SAMPLE_RATE = 48000
|
||||||
@@ -24,7 +19,6 @@ SOURCE_CHANNELS = 16
|
|||||||
OUTPUT_CHANNELS = 2
|
OUTPUT_CHANNELS = 2
|
||||||
PROJECT_DIR = Path(__file__).resolve().parent.parent
|
PROJECT_DIR = Path(__file__).resolve().parent.parent
|
||||||
DEFAULT_HRTF_DIR = PROJECT_DIR / "HRTF"
|
DEFAULT_HRTF_DIR = PROJECT_DIR / "HRTF"
|
||||||
DEFAULT_SOFA_HRTF = DEFAULT_HRTF_DIR / "binaural.sofa"
|
|
||||||
|
|
||||||
|
|
||||||
def _resolve_hrtf_file(path: str | Path, suffix: str, label: str) -> Path:
|
def _resolve_hrtf_file(path: str | Path, suffix: str, label: str) -> Path:
|
||||||
@@ -36,17 +30,12 @@ def _resolve_hrtf_file(path: str | Path, suffix: str, label: str) -> Path:
|
|||||||
return target
|
return target
|
||||||
|
|
||||||
|
|
||||||
def resolve_sofa_hrtf(path: str | Path) -> Path:
|
|
||||||
"""Resolve an explicitly selected public SOFA source."""
|
|
||||||
return _resolve_hrtf_file(path, ".sofa", "SOFA HRTF")
|
|
||||||
|
|
||||||
|
|
||||||
def resolve_compiled_hrtf_cache(path: str | Path) -> Path:
|
def resolve_compiled_hrtf_cache(path: str | Path) -> Path:
|
||||||
"""Resolve an explicitly selected JOC compiled HRTF cache."""
|
"""Resolve an explicitly selected JOC compiled HRTF cache."""
|
||||||
return _resolve_hrtf_file(path, ".jochrtf", "compiled HRTF cache")
|
return _resolve_hrtf_file(path, ".jochrtf", "compiled HRTF cache")
|
||||||
|
|
||||||
|
|
||||||
class SofaBinauralRenderer:
|
class JochrtfBinauralRenderer:
|
||||||
"""Render interleaved LFE plus fifteen JOC objects to stereo.
|
"""Render interleaved LFE plus fifteen JOC objects to stereo.
|
||||||
|
|
||||||
The adapter owns frame buffering and sample-timed OAMD updates; the
|
The adapter owns frame buffering and sample-timed OAMD updates; the
|
||||||
@@ -103,46 +92,6 @@ class SofaBinauralRenderer:
|
|||||||
self.finished = False
|
self.finished = False
|
||||||
self.metadata_block_updates = 0
|
self.metadata_block_updates = 0
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_sofa(
|
|
||||||
cls, sofa: str | Path, *,
|
|
||||||
mode: str | None = None,
|
|
||||||
cache_policy: str | None = None,
|
|
||||||
cache_dir: str | Path | None = None,
|
|
||||||
shell_radius_m: float | None = None,
|
|
||||||
projection_ridge: float = DEFAULT_PROJECTION_RIDGE,
|
|
||||||
sh_ridge: float = DEFAULT_SH_RIDGE,
|
|
||||||
object_delay_samples: int = 1473,
|
|
||||||
tail_seconds: float = 5.0,
|
|
||||||
output_gain: float = 1.0,
|
|
||||||
chunk_frames: int = 64,
|
|
||||||
bed_channels=None,
|
|
||||||
interpolation: str = "lf_logmag_phase",
|
|
||||||
reference_distance_m: float | None = None,
|
|
||||||
sofa_room: bool = False,
|
|
||||||
room_preset: str | None = None) -> "SofaBinauralRenderer":
|
|
||||||
from sofa_direct_backend import DirectSofaBackend
|
|
||||||
if cache_policy not in (None, "none", "memory") or cache_dir is not None:
|
|
||||||
raise ValueError("SOFA renders full HRIRs; use compile_hrtf.py to export a .jochrtf")
|
|
||||||
if projection_ridge != DEFAULT_PROJECTION_RIDGE or sh_ridge != DEFAULT_SH_RIDGE:
|
|
||||||
raise ValueError("full HRIR rendering does not use projection/SH ridge parameters")
|
|
||||||
source = resolve_sofa_hrtf(sofa)
|
|
||||||
backend = DirectSofaBackend(
|
|
||||||
source,
|
|
||||||
source_count=SOURCE_CHANNELS,
|
|
||||||
output_gain=output_gain,
|
|
||||||
mode=mode,
|
|
||||||
shell_radius_m=shell_radius_m,
|
|
||||||
method=interpolation,
|
|
||||||
reference_distance_m=reference_distance_m, sofa_room=sofa_room, room_preset=room_preset)
|
|
||||||
return cls(
|
|
||||||
backend,
|
|
||||||
mode=mode,
|
|
||||||
object_delay_samples=object_delay_samples,
|
|
||||||
tail_seconds=tail_seconds,
|
|
||||||
chunk_frames=chunk_frames,
|
|
||||||
bed_channels=bed_channels)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_compiled_cache(
|
def from_compiled_cache(
|
||||||
cls, cache: str | Path, *,
|
cls, cache: str | Path, *,
|
||||||
@@ -152,16 +101,15 @@ class SofaBinauralRenderer:
|
|||||||
output_gain: float = 1.0,
|
output_gain: float = 1.0,
|
||||||
chunk_frames: int = 64,
|
chunk_frames: int = 64,
|
||||||
bed_channels=None,
|
bed_channels=None,
|
||||||
reference_distance_m: float | None = None,
|
room: bool = False) -> "JochrtfBinauralRenderer":
|
||||||
sofa_room: bool = False,
|
from jochrtf_backend import JochrtfBackend
|
||||||
room_preset: str | None = None) -> "SofaBinauralRenderer":
|
|
||||||
from sofa_direct_backend import DirectSofaBackend
|
|
||||||
source = resolve_compiled_hrtf_cache(cache)
|
source = resolve_compiled_hrtf_cache(cache)
|
||||||
backend = DirectSofaBackend(
|
backend = JochrtfBackend.from_compiled_cache(
|
||||||
source,
|
source,
|
||||||
source_count=SOURCE_CHANNELS,
|
source_count=SOURCE_CHANNELS,
|
||||||
compiled=True, output_gain=output_gain, mode=mode,
|
default_profile=mode or "mid",
|
||||||
reference_distance_m=reference_distance_m, sofa_room=sofa_room, room_preset=room_preset)
|
output_gain=output_gain,
|
||||||
|
room=room)
|
||||||
return cls(
|
return cls(
|
||||||
backend,
|
backend,
|
||||||
mode=mode,
|
mode=mode,
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
"""Explicit offline export of a SOFA source to the compact cache format."""
|
|
||||||
import argparse
|
|
||||||
from pathlib import Path
|
|
||||||
from sofa_canonical import load_simple_free_field_hrir
|
|
||||||
from sofa_hrtf_field import compile_sofa_hrtf
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv=None):
|
|
||||||
parser = argparse.ArgumentParser(description=__doc__)
|
|
||||||
parser.add_argument("sofa", type=Path)
|
|
||||||
parser.add_argument("output", type=Path)
|
|
||||||
parser.add_argument("--radius-m", type=float)
|
|
||||||
args = parser.parse_args(argv)
|
|
||||||
if args.output.suffix.lower() != ".jochrtf":
|
|
||||||
parser.error("output must use .jochrtf")
|
|
||||||
canonical = load_simple_free_field_hrir(args.sofa, target_sample_rate_hz=48000)
|
|
||||||
if args.radius_m is None and len(canonical.shells_m) != 1:
|
|
||||||
parser.error("multi-radius input requires --radius-m")
|
|
||||||
radius = float(canonical.shells_m[0]) if args.radius_m is None else args.radius_m
|
|
||||||
field = compile_sofa_hrtf(canonical, shell_radius_m=radius, cache_policy="none")
|
|
||||||
field.save(args.output)
|
|
||||||
print(args.output.resolve())
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
"""Generate a float64 FIR realization of the existing project room FDN."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
from dataclasses import asdict, replace
|
|
||||||
import hashlib
|
|
||||||
import json
|
|
||||||
from pathlib import Path
|
|
||||||
import time
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
from public_room import LateFdnConfig, SharedUnitaryFdn, ShoeboxRoomConfig
|
|
||||||
|
|
||||||
|
|
||||||
SCHEMA = "sofa-project-room-v1"
|
|
||||||
DEFAULT_PATH = Path(__file__).resolve().parents[1] / "data" / "sofa-room.npz"
|
|
||||||
ROOM_PATHS = {"legacy": DEFAULT_PATH, "compact": DEFAULT_PATH.with_name("sofa-room-compact.npz")}
|
|
||||||
|
|
||||||
|
|
||||||
def preset_config(preset):
|
|
||||||
early, late = ShoeboxRoomConfig(), LateFdnConfig()
|
|
||||||
provenance = {"preset": preset, "size_time_scale": 1.}
|
|
||||||
if preset == "compact":
|
|
||||||
dimensions = np.array([8.5, 11.3, 5.2])
|
|
||||||
original = np.array(early.dimensions_m)
|
|
||||||
|
|
||||||
def volume_over_area(d):
|
|
||||||
return float(np.prod(d) / (2 * (d[0] * d[1] + d[0] * d[2] + d[1] * d[2])))
|
|
||||||
|
|
||||||
scale = volume_over_area(dimensions) / volume_over_area(original)
|
|
||||||
early = replace(early, dimensions_m=tuple(dimensions), listener_position_m=tuple(dimensions / 2))
|
|
||||||
late = replace(late, rt60_seconds=late.rt60_seconds * scale,
|
|
||||||
delay_seconds=tuple(v * scale for v in late.delay_seconds),
|
|
||||||
allpass_seconds=tuple(v * scale for v in late.allpass_seconds))
|
|
||||||
provenance.update(size_time_scale=scale,
|
|
||||||
geometry_reference="Zahorik 2009, Table 1 model 9, L=11.3 W=8.5 H=5.2 m",
|
|
||||||
geometry_reference_url="https://pmc.ncbi.nlm.nih.gov/articles/PMC2730711/",
|
|
||||||
scaling="V/S ratio; same effective average wall absorption; original FDN algorithm",
|
|
||||||
reproduction_scope="geometry only; not a reproduction of the paper's BRIR model")
|
|
||||||
elif preset != "legacy":
|
|
||||||
raise ValueError("room preset must be compact or legacy")
|
|
||||||
return early, late, provenance
|
|
||||||
|
|
||||||
|
|
||||||
def generate(path=None, *, preset="legacy"):
|
|
||||||
started = time.perf_counter()
|
|
||||||
early, config, provenance = preset_config(preset)
|
|
||||||
fdn = SharedUnitaryFdn(config)
|
|
||||||
length = int(np.ceil(2 * config.rt60_seconds * config.sample_rate_hz))
|
|
||||||
probe_length = int(np.ceil(config.rt60_seconds * config.sample_rate_hz))
|
|
||||||
impulse = np.zeros(length + probe_length)
|
|
||||||
impulse[0] = 1.
|
|
||||||
response = fdn.process(impulse)
|
|
||||||
kernel = np.ascontiguousarray(response[:length].T, dtype="<f8")
|
|
||||||
discarded = float(np.sum(response[length:] ** 2) / np.sum(kernel ** 2))
|
|
||||||
if discarded > 1e-12:
|
|
||||||
raise ValueError(f"FDN FIR tail exceeds the -120 dB energy budget: {discarded}")
|
|
||||||
metadata = dict(schema=SCHEMA, sample_rate_hz=config.sample_rate_hz,
|
|
||||||
fdn_config=asdict(config), early_config=asdict(early), **provenance,
|
|
||||||
algorithm=fdn.info(), impulse_samples=length,
|
|
||||||
discarded_probe_samples=probe_length, discarded_energy_ratio=discarded,
|
|
||||||
kernel_sha256=hashlib.sha256(kernel.tobytes()).hexdigest(),
|
|
||||||
source_algorithm_sha256=hashlib.sha256(
|
|
||||||
Path(__file__).with_name("public_room.py").read_bytes()).hexdigest(),
|
|
||||||
generation_seconds=time.perf_counter() - started)
|
|
||||||
path = Path(ROOM_PATHS[preset] if path is None else path).expanduser().resolve()
|
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
np.savez_compressed(path, schema=np.array(SCHEMA), sample_rate_hz=np.int64(48000),
|
|
||||||
late_fir=kernel, metadata_json=np.array(json.dumps(metadata, sort_keys=True)))
|
|
||||||
print(json.dumps(dict(path=str(path), taps=length, bytes=path.stat().st_size,
|
|
||||||
preset=preset, dimensions_m=early.dimensions_m, rt60_parameter_s=config.rt60_seconds,
|
|
||||||
discarded_energy_ratio=discarded,
|
|
||||||
generation_seconds=metadata["generation_seconds"]), indent=2))
|
|
||||||
return metadata
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument("output", nargs="?", type=Path)
|
|
||||||
parser.add_argument("--preset", choices=("all", "legacy", "compact"), default="all")
|
|
||||||
args = parser.parse_args()
|
|
||||||
if args.preset == "all":
|
|
||||||
if args.output is not None:
|
|
||||||
parser.error("a custom output requires --preset legacy or compact")
|
|
||||||
for name in ROOM_PATHS:
|
|
||||||
generate(preset=name)
|
|
||||||
else:
|
|
||||||
generate(args.output, preset=args.preset)
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
"""Stateful direct and reflected paths for compact HRTF fields."""
|
|
||||||
from __future__ import annotations
|
|
||||||
from dataclasses import dataclass
|
|
||||||
import numpy as np
|
|
||||||
from public_filterbank import HYBRID_BANDS
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class HybridPath:
|
|
||||||
label: str
|
|
||||||
delay_slots: np.ndarray # whole-QMF delay per ear, [2]
|
|
||||||
transfer: np.ndarray # [ear,77], includes gain and residual delay
|
|
||||||
|
|
||||||
def __post_init__(self):
|
|
||||||
slots = np.asarray(self.delay_slots)
|
|
||||||
if slots.shape == ():
|
|
||||||
slots = np.repeat(slots, 2)
|
|
||||||
if slots.shape != (2,) or slots.dtype.kind not in "iu":
|
|
||||||
raise ValueError("hybrid path delay_slots must contain two integers")
|
|
||||||
slots = np.asarray(slots, dtype=np.int64)
|
|
||||||
transfer = np.asarray(self.transfer, dtype=np.complex128)
|
|
||||||
if transfer.shape != (2, HYBRID_BANDS) or not np.isfinite(transfer).all():
|
|
||||||
raise ValueError("hybrid path transfer must have finite shape [2,77]")
|
|
||||||
if np.any(slots < 0):
|
|
||||||
raise ValueError("hybrid path delay_slots must be non-negative")
|
|
||||||
slots.setflags(write=False)
|
|
||||||
transfer.setflags(write=False)
|
|
||||||
object.__setattr__(self, "delay_slots", slots)
|
|
||||||
object.__setattr__(self, "transfer", transfer)
|
|
||||||
|
|
||||||
|
|
||||||
class HybridObjectPathRenderer:
|
|
||||||
"""Per-object hybrid histories and path transitions."""
|
|
||||||
|
|
||||||
def __init__(self, source_count: int, *, history_slots: int = 256,
|
|
||||||
transition_slots: int = 8):
|
|
||||||
self.source_count = int(source_count)
|
|
||||||
self.history_slots = int(history_slots)
|
|
||||||
self.transition_slots = int(transition_slots)
|
|
||||||
if min(self.source_count, self.history_slots) <= 0 or self.transition_slots < 0:
|
|
||||||
raise ValueError("invalid hybrid path renderer dimensions")
|
|
||||||
self.history = np.zeros(
|
|
||||||
(self.source_count, self.history_slots, HYBRID_BANDS), dtype=np.complex128)
|
|
||||||
self.position = 0
|
|
||||||
self.current: list[tuple[HybridPath, ...]] = [tuple() for _ in range(self.source_count)]
|
|
||||||
self.target: list[tuple[HybridPath, ...] | None] = [None] * self.source_count
|
|
||||||
self.fade_position = np.zeros(self.source_count, dtype=np.int32)
|
|
||||||
self.fade_total = np.zeros(self.source_count, dtype=np.int32)
|
|
||||||
self.processed_slots = 0
|
|
||||||
|
|
||||||
def reset(self) -> None:
|
|
||||||
self.history.fill(0.0)
|
|
||||||
self.position = 0
|
|
||||||
self.target = [None] * self.source_count
|
|
||||||
self.fade_position.fill(0)
|
|
||||||
self.fade_total.fill(0)
|
|
||||||
self.processed_slots = 0
|
|
||||||
|
|
||||||
def set_paths(self, source: int, paths, *, fade_slots: int | None = None) -> None:
|
|
||||||
source = int(source)
|
|
||||||
if not 0 <= source < self.source_count:
|
|
||||||
raise IndexError(source)
|
|
||||||
values = tuple(paths)
|
|
||||||
for path in values:
|
|
||||||
if np.any(path.delay_slots >= self.history_slots):
|
|
||||||
raise ValueError(
|
|
||||||
f"path {path.label!r} needs {path.delay_slots.tolist()} slots, "
|
|
||||||
f"history capacity is {self.history_slots}")
|
|
||||||
fade = self.transition_slots if fade_slots is None else int(fade_slots)
|
|
||||||
if fade < 0:
|
|
||||||
raise ValueError("path fade must be non-negative")
|
|
||||||
if self.target[source] is not None:
|
|
||||||
self.current[source] = self.target[source]
|
|
||||||
self.target[source] = None
|
|
||||||
if not self.processed_slots or fade == 0:
|
|
||||||
self.current[source] = values
|
|
||||||
self.target[source] = None
|
|
||||||
self.fade_position[source] = 0
|
|
||||||
self.fade_total[source] = 0
|
|
||||||
else:
|
|
||||||
self.target[source] = values
|
|
||||||
self.fade_position[source] = 0
|
|
||||||
self.fade_total[source] = fade
|
|
||||||
|
|
||||||
def _render_paths(self, source: int, paths: tuple[HybridPath, ...]) -> np.ndarray:
|
|
||||||
result = np.zeros((2, HYBRID_BANDS), dtype=np.complex128)
|
|
||||||
for path in paths:
|
|
||||||
indices = (self.position - path.delay_slots) % self.history_slots
|
|
||||||
delayed = self.history[source, indices, :]
|
|
||||||
result += delayed * path.transfer
|
|
||||||
return result
|
|
||||||
|
|
||||||
def process(self, hybrid) -> np.ndarray:
|
|
||||||
values = np.asarray(hybrid, dtype=np.complex128)
|
|
||||||
if values.ndim != 3 or values.shape[1:] != (self.source_count, HYBRID_BANDS):
|
|
||||||
raise ValueError(
|
|
||||||
f"hybrid input must have shape [slots,{self.source_count},77]")
|
|
||||||
if not np.isfinite(values).all():
|
|
||||||
raise ValueError("hybrid input contains non-finite values")
|
|
||||||
output = np.zeros((len(values), 2, HYBRID_BANDS), dtype=np.complex128)
|
|
||||||
for slot in range(len(values)):
|
|
||||||
self.history[:, self.position, :] = values[slot]
|
|
||||||
for source in range(self.source_count - 1, -1, -1):
|
|
||||||
current = self._render_paths(source, self.current[source])
|
|
||||||
target_paths = self.target[source]
|
|
||||||
if target_paths is None:
|
|
||||||
output[slot] += current
|
|
||||||
continue
|
|
||||||
target = self._render_paths(source, target_paths)
|
|
||||||
self.fade_position[source] += 1
|
|
||||||
amount = min(
|
|
||||||
1.0, self.fade_position[source] / float(self.fade_total[source]))
|
|
||||||
output[slot] += current * (1.0 - amount) + target * amount
|
|
||||||
if self.fade_position[source] >= self.fade_total[source]:
|
|
||||||
self.current[source] = target_paths
|
|
||||||
self.target[source] = None
|
|
||||||
self.fade_position[source] = 0
|
|
||||||
self.fade_total[source] = 0
|
|
||||||
self.position = (self.position + 1) % self.history_slots
|
|
||||||
self.processed_slots += 1
|
|
||||||
return output
|
|
||||||
@@ -0,0 +1,515 @@
|
|||||||
|
"""Stateful .jochrtf binaural renderer.
|
||||||
|
|
||||||
|
The runtime topology mirrors the existing multi-object binaural path:
|
||||||
|
64-QMF -> 77 hybrid -> per-object directional transfer -> stereo synthesis.
|
||||||
|
The HRTF parameter source is a SOFA-derived fifth-order field. Early
|
||||||
|
reflections and the late room use project-owned behavior.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
import math
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from public_filterbank import (
|
||||||
|
ANALYSIS_SYNTHESIS_LATENCY_SAMPLES,
|
||||||
|
HYBRID_BANDS,
|
||||||
|
QMF_HOP,
|
||||||
|
PublicAnalysis77,
|
||||||
|
PublicSynthesis77,
|
||||||
|
table_info as filterbank_table_info,
|
||||||
|
)
|
||||||
|
from public_room import (
|
||||||
|
LateFdnConfig,
|
||||||
|
SharedUnitaryFdn,
|
||||||
|
ShoeboxRoomConfig,
|
||||||
|
first_order_image_sources,
|
||||||
|
)
|
||||||
|
from reference_distance import DistanceState, ReferenceDistanceProfileV1
|
||||||
|
from sofa_hrtf_field import SofaHrtfField
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class HybridPath:
|
||||||
|
label: str
|
||||||
|
delay_slots: np.ndarray # whole-QMF delay per ear, [2]
|
||||||
|
transfer: np.ndarray # [ear,77], includes residual delay and HRTF delay
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
slots = np.asarray(self.delay_slots)
|
||||||
|
if slots.shape == ():
|
||||||
|
slots = np.repeat(slots, 2)
|
||||||
|
if slots.shape != (2,) or slots.dtype.kind not in "iu":
|
||||||
|
raise ValueError("hybrid path delay_slots must contain two integers")
|
||||||
|
slots = np.asarray(slots, dtype=np.int64)
|
||||||
|
transfer = np.asarray(self.transfer, dtype=np.complex128)
|
||||||
|
if transfer.shape != (2, HYBRID_BANDS) or not np.isfinite(transfer).all():
|
||||||
|
raise ValueError("hybrid path transfer must have finite shape [2,77]")
|
||||||
|
if np.any(slots < 0):
|
||||||
|
raise ValueError("hybrid path delay_slots must be non-negative")
|
||||||
|
slots.setflags(write=False)
|
||||||
|
transfer.setflags(write=False)
|
||||||
|
object.__setattr__(self, "delay_slots", slots)
|
||||||
|
object.__setattr__(self, "transfer", transfer)
|
||||||
|
|
||||||
|
|
||||||
|
class HybridObjectPathRenderer:
|
||||||
|
"""Per-object hybrid histories for direct and image-source paths."""
|
||||||
|
|
||||||
|
def __init__(self, source_count: int, *, history_slots: int = 256,
|
||||||
|
transition_slots: int = 8):
|
||||||
|
self.source_count = int(source_count)
|
||||||
|
self.history_slots = int(history_slots)
|
||||||
|
self.transition_slots = int(transition_slots)
|
||||||
|
if min(self.source_count, self.history_slots) <= 0 or self.transition_slots < 0:
|
||||||
|
raise ValueError("invalid hybrid path renderer dimensions")
|
||||||
|
self.history = np.zeros(
|
||||||
|
(self.source_count, self.history_slots, HYBRID_BANDS), dtype=np.complex128)
|
||||||
|
self.position = 0
|
||||||
|
self.current: list[tuple[HybridPath, ...]] = [tuple() for _ in range(self.source_count)]
|
||||||
|
self.target: list[tuple[HybridPath, ...] | None] = [None] * self.source_count
|
||||||
|
self.fade_position = np.zeros(self.source_count, dtype=np.int32)
|
||||||
|
self.fade_total = np.zeros(self.source_count, dtype=np.int32)
|
||||||
|
self.processed_slots = 0
|
||||||
|
|
||||||
|
def reset(self) -> None:
|
||||||
|
self.history.fill(0.0)
|
||||||
|
self.position = 0
|
||||||
|
self.target = [None] * self.source_count
|
||||||
|
self.fade_position.fill(0)
|
||||||
|
self.fade_total.fill(0)
|
||||||
|
self.processed_slots = 0
|
||||||
|
|
||||||
|
def set_paths(self, source: int, paths, *, fade_slots: int | None = None) -> None:
|
||||||
|
source = int(source)
|
||||||
|
if not 0 <= source < self.source_count:
|
||||||
|
raise IndexError(source)
|
||||||
|
values = tuple(paths)
|
||||||
|
for path in values:
|
||||||
|
if np.any(path.delay_slots >= self.history_slots):
|
||||||
|
raise ValueError(
|
||||||
|
f"path {path.label!r} needs {path.delay_slots.tolist()} slots, "
|
||||||
|
f"history capacity is {self.history_slots}")
|
||||||
|
fade = self.transition_slots if fade_slots is None else int(fade_slots)
|
||||||
|
if fade < 0:
|
||||||
|
raise ValueError("path fade must be non-negative")
|
||||||
|
if self.target[source] is not None:
|
||||||
|
# Normal 512-sample updates complete an 8-slot transition exactly.
|
||||||
|
# If a caller updates faster, use the previous target as the new
|
||||||
|
# stable side rather than resetting signal history.
|
||||||
|
self.current[source] = self.target[source]
|
||||||
|
self.target[source] = None
|
||||||
|
if not self.processed_slots or fade == 0:
|
||||||
|
self.current[source] = values
|
||||||
|
self.target[source] = None
|
||||||
|
self.fade_position[source] = 0
|
||||||
|
self.fade_total[source] = 0
|
||||||
|
else:
|
||||||
|
self.target[source] = values
|
||||||
|
self.fade_position[source] = 0
|
||||||
|
self.fade_total[source] = fade
|
||||||
|
|
||||||
|
def _render_paths(self, source: int, paths: tuple[HybridPath, ...]) -> np.ndarray:
|
||||||
|
result = np.zeros((2, HYBRID_BANDS), dtype=np.complex128)
|
||||||
|
for path in paths:
|
||||||
|
indices = (self.position - path.delay_slots) % self.history_slots
|
||||||
|
delayed = self.history[source, indices, :]
|
||||||
|
result += delayed * path.transfer
|
||||||
|
return result
|
||||||
|
|
||||||
|
def process(self, hybrid) -> np.ndarray:
|
||||||
|
values = np.asarray(hybrid, dtype=np.complex128)
|
||||||
|
if values.ndim != 3 or values.shape[1:] != (self.source_count, HYBRID_BANDS):
|
||||||
|
raise ValueError(
|
||||||
|
f"hybrid input must have shape [slots,{self.source_count},77]")
|
||||||
|
if not np.isfinite(values).all():
|
||||||
|
raise ValueError("hybrid input contains non-finite values")
|
||||||
|
output = np.zeros((len(values), 2, HYBRID_BANDS), dtype=np.complex128)
|
||||||
|
for slot in range(len(values)):
|
||||||
|
self.history[:, self.position, :] = values[slot]
|
||||||
|
for source in range(self.source_count - 1, -1, -1):
|
||||||
|
current = self._render_paths(source, self.current[source])
|
||||||
|
target_paths = self.target[source]
|
||||||
|
if target_paths is None:
|
||||||
|
output[slot] += current
|
||||||
|
continue
|
||||||
|
target = self._render_paths(source, target_paths)
|
||||||
|
self.fade_position[source] += 1
|
||||||
|
amount = min(
|
||||||
|
1.0, self.fade_position[source] / float(self.fade_total[source]))
|
||||||
|
output[slot] += current * (1.0 - amount) + target * amount
|
||||||
|
if self.fade_position[source] >= self.fade_total[source]:
|
||||||
|
self.current[source] = target_paths
|
||||||
|
self.target[source] = None
|
||||||
|
self.fade_position[source] = 0
|
||||||
|
self.fade_total[source] = 0
|
||||||
|
self.position = (self.position + 1) % self.history_slots
|
||||||
|
self.processed_slots += 1
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
class _StereoDelay:
|
||||||
|
def __init__(self, delay_samples: int):
|
||||||
|
self.delay_samples = int(delay_samples)
|
||||||
|
if self.delay_samples < 0:
|
||||||
|
raise ValueError("delay must be non-negative")
|
||||||
|
self.state = np.zeros((self.delay_samples, 2), dtype=np.float64)
|
||||||
|
|
||||||
|
def reset(self) -> None:
|
||||||
|
self.state.fill(0.0)
|
||||||
|
|
||||||
|
def process(self, values) -> np.ndarray:
|
||||||
|
source = np.asarray(values, dtype=np.float64)
|
||||||
|
if source.ndim != 2 or source.shape[1] != 2:
|
||||||
|
raise ValueError("stereo delay input must have shape [samples,2]")
|
||||||
|
if self.delay_samples == 0:
|
||||||
|
return source.copy()
|
||||||
|
joined = np.concatenate((self.state, source), axis=0)
|
||||||
|
output = joined[:len(source)].copy()
|
||||||
|
self.state = joined[len(source):len(source) + self.delay_samples].copy()
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
class JochrtfBackend:
|
||||||
|
"""77-band/SH renderer for a compiled .jochrtf field, with the project room."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
field: SofaHrtfField,
|
||||||
|
*,
|
||||||
|
source_count: int = 16,
|
||||||
|
sample_rate_hz: float = 48000.0,
|
||||||
|
default_profile: str = "mid",
|
||||||
|
room: bool = True,
|
||||||
|
room_config: ShoeboxRoomConfig = ShoeboxRoomConfig(),
|
||||||
|
fdn_config: LateFdnConfig | None = None,
|
||||||
|
transition_slots: int = 8,
|
||||||
|
history_slots: int = 256,
|
||||||
|
output_gain: float = 1.0):
|
||||||
|
self.source_count = int(source_count)
|
||||||
|
self.sample_rate_hz = float(sample_rate_hz)
|
||||||
|
self.default_profile = ReferenceDistanceProfileV1.validate_profile(default_profile)
|
||||||
|
self.room_enabled = bool(room)
|
||||||
|
self.enable_early_reflections = self.room_enabled
|
||||||
|
self.enable_late_room = self.room_enabled
|
||||||
|
self.room_config = room_config
|
||||||
|
self.room_config.validate()
|
||||||
|
self.output_gain = float(output_gain)
|
||||||
|
if (self.source_count <= 0 or not math.isfinite(self.sample_rate_hz)
|
||||||
|
or self.sample_rate_hz <= 0.0):
|
||||||
|
raise ValueError("source_count and sample rate must be positive")
|
||||||
|
if not math.isfinite(self.output_gain):
|
||||||
|
raise ValueError("output gain must be finite")
|
||||||
|
if abs(self.sample_rate_hz - 48000.0) > 1.0e-9:
|
||||||
|
raise ValueError("the public binaural runtime requires 48 kHz")
|
||||||
|
|
||||||
|
if not isinstance(field, SofaHrtfField):
|
||||||
|
raise TypeError(
|
||||||
|
"field must be SofaHrtfField; use from_compiled_cache() for a file")
|
||||||
|
self.field = field
|
||||||
|
self.hrtf_input_kind = "field"
|
||||||
|
self.hrtf_input_path: str | None = None
|
||||||
|
self.cache_policy: str | None = None
|
||||||
|
if abs(self.field.sample_rate_hz - self.sample_rate_hz) > 1.0e-9:
|
||||||
|
raise ValueError("HRTF field sample rate does not match the renderer")
|
||||||
|
|
||||||
|
self.early_history_slots = int(history_slots)
|
||||||
|
if self.early_history_slots <= 0:
|
||||||
|
raise ValueError("history_slots must be positive")
|
||||||
|
maximum_hrtf_delay = float(np.max(self.field.delay_bounds[:, 1], initial=0.0))
|
||||||
|
self.maximum_hrtf_delay_samples = maximum_hrtf_delay
|
||||||
|
self.hrtf_history_slots = int(math.ceil(maximum_hrtf_delay / QMF_HOP))
|
||||||
|
self.analysis = PublicAnalysis77(self.source_count)
|
||||||
|
self.paths = HybridObjectPathRenderer(
|
||||||
|
self.source_count,
|
||||||
|
history_slots=self.early_history_slots + self.hrtf_history_slots,
|
||||||
|
transition_slots=transition_slots)
|
||||||
|
self.synthesis = PublicSynthesis77(2)
|
||||||
|
actual_fdn_config = fdn_config or LateFdnConfig(sample_rate_hz=self.sample_rate_hz)
|
||||||
|
if abs(actual_fdn_config.sample_rate_hz - self.sample_rate_hz) > 1.0e-9:
|
||||||
|
raise ValueError("FDN sample rate does not match the renderer")
|
||||||
|
self.fdn = SharedUnitaryFdn(actual_fdn_config)
|
||||||
|
self.late_delay = _StereoDelay(ANALYSIS_SYNTHESIS_LATENCY_SAMPLES)
|
||||||
|
|
||||||
|
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.distance_state: list[DistanceState | None] = [None] * self.source_count
|
||||||
|
self.late_current = np.zeros(self.source_count, dtype=np.float64)
|
||||||
|
self.late_start = np.zeros(self.source_count, dtype=np.float64)
|
||||||
|
self.late_target = np.zeros(self.source_count, dtype=np.float64)
|
||||||
|
self.late_fade_position = np.zeros(self.source_count, dtype=np.int64)
|
||||||
|
self.late_fade_total = np.zeros(self.source_count, dtype=np.int64)
|
||||||
|
self.maximum_early_delay_samples = 0.0
|
||||||
|
self.latency_to_discard = ANALYSIS_SYNTHESIS_LATENCY_SAMPLES
|
||||||
|
self.processed_input_samples = 0
|
||||||
|
self.output_samples = 0
|
||||||
|
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)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_compiled_cache(
|
||||||
|
cls, cache: str | Path, **renderer_options
|
||||||
|
) -> "JochrtfBackend":
|
||||||
|
"""Load an explicitly selected validated JOC compiled HRTF cache."""
|
||||||
|
path = Path(cache).expanduser().resolve()
|
||||||
|
field = SofaHrtfField.load(path)
|
||||||
|
result = cls(field, **renderer_options)
|
||||||
|
result.hrtf_input_kind = "compiled_cache"
|
||||||
|
result.hrtf_input_path = str(path)
|
||||||
|
result.cache_policy = None
|
||||||
|
return result
|
||||||
|
|
||||||
|
def _make_path(self, label: str, direction_adm, path_distance_m: float,
|
||||||
|
extra_delay_samples: float, amplitude: float) -> HybridPath:
|
||||||
|
del path_distance_m
|
||||||
|
evaluation = self.field.evaluate_adm(direction_adm)
|
||||||
|
extra_delay = float(extra_delay_samples)
|
||||||
|
if not math.isfinite(extra_delay) or extra_delay < 0.0:
|
||||||
|
raise ValueError("path delay must be finite and non-negative")
|
||||||
|
early_delay_slots = int(math.floor(extra_delay / QMF_HOP))
|
||||||
|
if early_delay_slots >= self.early_history_slots:
|
||||||
|
raise ValueError(
|
||||||
|
f"path {label!r} needs {early_delay_slots} early-delay slots, "
|
||||||
|
f"early history capacity is {self.early_history_slots}")
|
||||||
|
total_delay = np.asarray(evaluation.delay_samples, dtype=np.float64) + extra_delay
|
||||||
|
delay_slots = np.floor(total_delay / QMF_HOP).astype(np.int64)
|
||||||
|
residual = total_delay - delay_slots * QMF_HOP
|
||||||
|
propagation_phase = np.exp(
|
||||||
|
-2j * np.pi * self.field.band_center_frequencies_hz[None, :]
|
||||||
|
* residual[:, None]
|
||||||
|
/ self.sample_rate_hz)
|
||||||
|
transfer = np.asarray(
|
||||||
|
evaluation.aligned_gains * propagation_phase * float(amplitude),
|
||||||
|
dtype=np.complex128)
|
||||||
|
return HybridPath(label, delay_slots, transfer)
|
||||||
|
|
||||||
|
def _ordinary_paths(self, state: DistanceState, gain: float) -> tuple[HybridPath, ...]:
|
||||||
|
# Object PCM is programme-normalized. Physical distance controls room
|
||||||
|
# geometry, while the project profile supplies the direct presentation
|
||||||
|
# coefficient instead of applying a second free-field 1/r attenuation.
|
||||||
|
direct_amplitude = gain * ReferenceDistanceProfileV1.direct_level_gain(state)
|
||||||
|
room_gain = ReferenceDistanceProfileV1.room_calibration_gain(state)
|
||||||
|
paths = [self._make_path(
|
||||||
|
"direct", state.direction_adm, state.physical_distance_m, 0.0,
|
||||||
|
direct_amplitude)]
|
||||||
|
if self.enable_early_reflections:
|
||||||
|
reflections = first_order_image_sources(
|
||||||
|
state.direction_adm, state.physical_distance_m,
|
||||||
|
self.sample_rate_hz, self.room_config)
|
||||||
|
for reflection in reflections:
|
||||||
|
amplitude = (
|
||||||
|
gain * room_gain * reflection.reflection_gain
|
||||||
|
* ReferenceDistanceProfileV1.inverse_distance_gain(
|
||||||
|
self.field.measurement_radius_m,
|
||||||
|
reflection.path_distance_m))
|
||||||
|
paths.append(self._make_path(
|
||||||
|
f"early:{reflection.wall}", reflection.direction_adm,
|
||||||
|
reflection.path_distance_m, reflection.extra_delay_samples,
|
||||||
|
amplitude))
|
||||||
|
self.maximum_early_delay_samples = max(
|
||||||
|
self.maximum_early_delay_samples,
|
||||||
|
reflection.extra_delay_samples)
|
||||||
|
return tuple(paths)
|
||||||
|
|
||||||
|
def _lfe_paths(self, gain: float) -> tuple[HybridPath, ...]:
|
||||||
|
frequency = self.field.band_center_frequencies_hz
|
||||||
|
lowpass = np.ones(HYBRID_BANDS, dtype=np.float64)
|
||||||
|
lowpass[frequency >= 180.0] = 0.0
|
||||||
|
transition = (frequency > 120.0) & (frequency < 180.0)
|
||||||
|
amount = (frequency[transition] - 120.0) / 60.0
|
||||||
|
lowpass[transition] = np.cos(0.5 * np.pi * amount) ** 2
|
||||||
|
transfer = np.repeat(
|
||||||
|
(gain * lowpass / math.sqrt(2.0))[None, :], 2, axis=0
|
||||||
|
).astype(np.complex128)
|
||||||
|
return (HybridPath(
|
||||||
|
"public_lfe_lowpass", np.zeros(2, dtype=np.int64), transfer),)
|
||||||
|
|
||||||
|
def _set_late_target(self, source: int, value: float, fade: bool) -> None:
|
||||||
|
value = float(value)
|
||||||
|
fade_samples = (self.paths.transition_slots * QMF_HOP
|
||||||
|
if fade and self.processed_input_samples else 0)
|
||||||
|
if fade_samples == 0:
|
||||||
|
self.late_current[source] = value
|
||||||
|
self.late_start[source] = value
|
||||||
|
self.late_target[source] = value
|
||||||
|
self.late_fade_position[source] = 0
|
||||||
|
self.late_fade_total[source] = 0
|
||||||
|
else:
|
||||||
|
self.late_start[source] = self.late_current[source]
|
||||||
|
self.late_target[source] = value
|
||||||
|
self.late_fade_position[source] = 0
|
||||||
|
self.late_fade_total[source] = fade_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)
|
||||||
|
gain = float(gain)
|
||||||
|
if not math.isfinite(gain):
|
||||||
|
raise ValueError("source gain must be finite")
|
||||||
|
effective_gain = gain if enabled else 0.0
|
||||||
|
name = self.default_profile if profile is None else profile
|
||||||
|
state = ReferenceDistanceProfileV1.map_adm_position(position_adm, name)
|
||||||
|
path_set = (self._lfe_paths(effective_gain) if special_lfe
|
||||||
|
else self._ordinary_paths(state, effective_gain))
|
||||||
|
self.paths.set_paths(
|
||||||
|
source, path_set,
|
||||||
|
fade_slots=(self.paths.transition_slots if fade else 0))
|
||||||
|
late_send = (0.0 if special_lfe or not self.enable_late_room or not enabled
|
||||||
|
else effective_gain
|
||||||
|
* ReferenceDistanceProfileV1.room_calibration_gain(state)
|
||||||
|
* ReferenceDistanceProfileV1.late_send(state))
|
||||||
|
self._set_late_target(source, late_send, fade)
|
||||||
|
self.positions[source] = np.asarray(position_adm, dtype=np.float64)
|
||||||
|
self.profiles[source] = state.profile
|
||||||
|
self.user_gain[source] = gain
|
||||||
|
self.special_lfe[source] = bool(special_lfe)
|
||||||
|
self.distance_state[source] = state
|
||||||
|
self.parameter_updates += 1
|
||||||
|
|
||||||
|
def _late_send_envelope(self, sample_count: int) -> np.ndarray:
|
||||||
|
envelope = np.empty((sample_count, self.source_count), dtype=np.float64)
|
||||||
|
for source in range(self.source_count):
|
||||||
|
total = int(self.late_fade_total[source])
|
||||||
|
if total == 0:
|
||||||
|
envelope[:, source] = self.late_current[source]
|
||||||
|
continue
|
||||||
|
start_position = int(self.late_fade_position[source])
|
||||||
|
position = start_position + np.arange(1, sample_count + 1)
|
||||||
|
amount = np.clip(position / float(total), 0.0, 1.0)
|
||||||
|
envelope[:, source] = (
|
||||||
|
self.late_start[source] * (1.0 - amount)
|
||||||
|
+ self.late_target[source] * amount)
|
||||||
|
new_position = start_position + sample_count
|
||||||
|
if new_position >= total:
|
||||||
|
self.late_current[source] = self.late_target[source]
|
||||||
|
self.late_start[source] = self.late_target[source]
|
||||||
|
self.late_fade_position[source] = 0
|
||||||
|
self.late_fade_total[source] = 0
|
||||||
|
else:
|
||||||
|
self.late_current[source] = float(envelope[-1, source])
|
||||||
|
self.late_fade_position[source] = new_position
|
||||||
|
return envelope
|
||||||
|
|
||||||
|
def _process(self, sources) -> np.ndarray:
|
||||||
|
values = np.asarray(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:
|
||||||
|
raise ValueError("SOFA backend input must be divisible by 64 samples")
|
||||||
|
if not np.isfinite(values).all():
|
||||||
|
raise ValueError("SOFA backend input contains non-finite values")
|
||||||
|
hybrid = self.analysis.process(values)
|
||||||
|
direct_and_early = self.paths.process(hybrid)
|
||||||
|
direct_pcm = self.synthesis.process(direct_and_early)
|
||||||
|
if self.enable_late_room:
|
||||||
|
sends = self._late_send_envelope(len(values))
|
||||||
|
mono = np.sum(values * sends, axis=1, dtype=np.float64)
|
||||||
|
late_pcm = self.late_delay.process(self.fdn.process(mono))
|
||||||
|
else:
|
||||||
|
# Still advance any pending send fade deterministically.
|
||||||
|
self._late_send_envelope(len(values))
|
||||||
|
late_pcm = np.zeros_like(direct_pcm)
|
||||||
|
mixed = np.asarray((direct_pcm + late_pcm) * self.output_gain, dtype=np.float64)
|
||||||
|
skip = min(self.latency_to_discard, len(mixed))
|
||||||
|
self.latency_to_discard -= skip
|
||||||
|
self.processed_input_samples += len(values)
|
||||||
|
output = mixed[skip:]
|
||||||
|
self.output_samples += len(output)
|
||||||
|
return output
|
||||||
|
|
||||||
|
def process(self, sources) -> np.ndarray:
|
||||||
|
if self.finished:
|
||||||
|
raise RuntimeError("SOFA renderer is finished")
|
||||||
|
return self._process(sources)
|
||||||
|
|
||||||
|
def finish(self, *, tail_seconds: float | None = None) -> np.ndarray:
|
||||||
|
if self.finished:
|
||||||
|
return np.zeros((0, 2), 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")
|
||||||
|
requested = (self.fdn.tail_samples if tail_seconds is None
|
||||||
|
else int(math.ceil(float(tail_seconds) * self.sample_rate_hz)))
|
||||||
|
drain = max(
|
||||||
|
requested if self.enable_late_room else 0,
|
||||||
|
int(math.ceil(
|
||||||
|
self.maximum_hrtf_delay_samples
|
||||||
|
+ self.maximum_early_delay_samples)) + 2048,
|
||||||
|
) + ANALYSIS_SYNTHESIS_LATENCY_SAMPLES
|
||||||
|
drain = int(math.ceil(drain / QMF_HOP) * QMF_HOP)
|
||||||
|
output = self._process(np.zeros((drain, self.source_count), dtype=np.float64))
|
||||||
|
self.finished = True
|
||||||
|
return output
|
||||||
|
|
||||||
|
def finish_output_capacity(self, tail_seconds: float | None = None) -> int:
|
||||||
|
"""Return a conservative bound for one future :meth:`finish` output."""
|
||||||
|
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) * self.sample_rate_hz)))
|
||||||
|
hrtf_bound = self.hrtf_history_slots * QMF_HOP
|
||||||
|
early_bound = hrtf_bound + 2048
|
||||||
|
if self.enable_early_reflections:
|
||||||
|
early_bound += self.early_history_slots * QMF_HOP
|
||||||
|
drain = max(requested if self.enable_late_room else 0, early_bound)
|
||||||
|
drain += ANALYSIS_SYNTHESIS_LATENCY_SAMPLES
|
||||||
|
return int(math.ceil(drain / QMF_HOP) * QMF_HOP)
|
||||||
|
|
||||||
|
def reset(self) -> None:
|
||||||
|
self.analysis.reset()
|
||||||
|
self.paths.reset()
|
||||||
|
self.synthesis.reset()
|
||||||
|
self.fdn.reset()
|
||||||
|
self.late_delay.reset()
|
||||||
|
self.latency_to_discard = ANALYSIS_SYNTHESIS_LATENCY_SAMPLES
|
||||||
|
self.processed_input_samples = 0
|
||||||
|
self.output_samples = 0
|
||||||
|
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": "SofaBinauralBackend",
|
||||||
|
"source_count": self.source_count,
|
||||||
|
"sample_rate_hz": self.sample_rate_hz,
|
||||||
|
"precision": "float64/complex128",
|
||||||
|
"signal_path": (
|
||||||
|
"public 64-QMF -> public 77-hybrid -> SOFA order-5 real-SH "
|
||||||
|
"direct/early -> public 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": ANALYSIS_SYNTHESIS_LATENCY_SAMPLES,
|
||||||
|
"enable_early_reflections": self.enable_early_reflections,
|
||||||
|
"enable_late_room": self.enable_late_room,
|
||||||
|
"early_history_slots": self.early_history_slots,
|
||||||
|
"hrtf_history_slots": self.hrtf_history_slots,
|
||||||
|
"maximum_hrtf_delay_samples": self.maximum_hrtf_delay_samples,
|
||||||
|
"maximum_early_delay_samples": self.maximum_early_delay_samples,
|
||||||
|
"parameter_updates": self.parameter_updates,
|
||||||
|
"processed_input_samples_including_flush": self.processed_input_samples,
|
||||||
|
"output_samples_before_trim": self.output_samples,
|
||||||
|
"distance": ReferenceDistanceProfileV1.info(),
|
||||||
|
"filterbank": filterbank_table_info(),
|
||||||
|
"field": self.field.info(),
|
||||||
|
"late_room": self.fdn.info(),
|
||||||
|
}
|
||||||
@@ -1,217 +0,0 @@
|
|||||||
"""Uniform partitioned convolution with float64/complex128 state."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import math
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
|
|
||||||
class UniformPartitionedConvolver:
|
|
||||||
"""Overlap-add FIR convolution with shared input history.
|
|
||||||
|
|
||||||
Filter updates crossfade outputs with independent old/new overlap tails.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, filters, *, block_size: int = 128,
|
|
||||||
max_filter_taps: int | None = None):
|
|
||||||
values = np.asarray(filters, dtype=np.float64)
|
|
||||||
if values.ndim == 1:
|
|
||||||
values = values[None, :]
|
|
||||||
if values.ndim != 2 or values.shape[0] <= 0 or values.shape[1] <= 0:
|
|
||||||
raise ValueError("filters must have shape [outputs,taps]")
|
|
||||||
if not np.isfinite(values).all():
|
|
||||||
raise ValueError("filters must be finite")
|
|
||||||
self.block_size = int(block_size)
|
|
||||||
if self.block_size <= 0:
|
|
||||||
raise ValueError("block_size must be positive")
|
|
||||||
self.fft_size = 2 * self.block_size
|
|
||||||
self.frequency_bins = self.block_size + 1
|
|
||||||
self.outputs = int(values.shape[0])
|
|
||||||
self.max_filter_taps = int(max_filter_taps or values.shape[1])
|
|
||||||
if self.max_filter_taps < values.shape[1]:
|
|
||||||
raise ValueError("max_filter_taps is shorter than the initial filter")
|
|
||||||
self.partition_count = int(math.ceil(self.max_filter_taps / self.block_size))
|
|
||||||
self.input_history = np.zeros(
|
|
||||||
(self.partition_count, self.frequency_bins), dtype=np.complex128)
|
|
||||||
self.write_index = -1
|
|
||||||
self.processed_blocks = 0
|
|
||||||
self.current_spectra = self._partition(values)
|
|
||||||
self.current_overlap = np.zeros(
|
|
||||||
(self.outputs, self.block_size), dtype=np.float64)
|
|
||||||
self.target_spectra: np.ndarray | None = None
|
|
||||||
self.target_overlap: np.ndarray | None = None
|
|
||||||
self.fade_samples = 0
|
|
||||||
self.fade_position = 0
|
|
||||||
|
|
||||||
@property
|
|
||||||
def transition_active(self) -> bool:
|
|
||||||
return self.target_spectra is not None
|
|
||||||
|
|
||||||
def _partition(self, filters) -> np.ndarray:
|
|
||||||
values = np.asarray(filters, dtype=np.float64)
|
|
||||||
if values.ndim == 1:
|
|
||||||
values = values[None, :]
|
|
||||||
if values.shape[0] != self.outputs or values.shape[1] > self.max_filter_taps:
|
|
||||||
raise ValueError(
|
|
||||||
f"filter update must have shape [{self.outputs}, <= {self.max_filter_taps}]")
|
|
||||||
if not np.isfinite(values).all():
|
|
||||||
raise ValueError("filter update contains non-finite values")
|
|
||||||
padded = np.zeros(
|
|
||||||
(self.outputs, self.partition_count, self.fft_size), dtype=np.float64)
|
|
||||||
for partition in range(self.partition_count):
|
|
||||||
start = partition * self.block_size
|
|
||||||
stop = min(start + self.block_size, values.shape[1])
|
|
||||||
if stop > start:
|
|
||||||
padded[:, partition, :stop - start] = values[:, start:stop]
|
|
||||||
return np.asarray(np.fft.rfft(padded, axis=-1), dtype=np.complex128)
|
|
||||||
|
|
||||||
def _ordered_input_history(self) -> np.ndarray:
|
|
||||||
if self.write_index < 0:
|
|
||||||
return np.zeros_like(self.input_history)
|
|
||||||
indices = (self.write_index - np.arange(self.partition_count)) % self.partition_count
|
|
||||||
return self.input_history[indices]
|
|
||||||
|
|
||||||
def _convolution_block(self, spectra: np.ndarray,
|
|
||||||
overlap: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
|
|
||||||
ordered = self._ordered_input_history()
|
|
||||||
frequency = np.einsum(
|
|
||||||
"pf,opf->of", ordered, spectra, dtype=np.complex128, optimize=False)
|
|
||||||
time = np.asarray(np.fft.irfft(frequency, self.fft_size, axis=-1),
|
|
||||||
dtype=np.float64)
|
|
||||||
output = time[:, :self.block_size] + overlap
|
|
||||||
return output, time[:, self.block_size:].copy()
|
|
||||||
|
|
||||||
def _overlap_for_previous_block(self, spectra: np.ndarray) -> np.ndarray:
|
|
||||||
if self.processed_blocks == 0:
|
|
||||||
return np.zeros((self.outputs, self.block_size), dtype=np.float64)
|
|
||||||
ordered = self._ordered_input_history()
|
|
||||||
frequency = np.einsum(
|
|
||||||
"pf,opf->of", ordered, spectra, dtype=np.complex128, optimize=False)
|
|
||||||
time = np.asarray(np.fft.irfft(frequency, self.fft_size, axis=-1),
|
|
||||||
dtype=np.float64)
|
|
||||||
return time[:, self.block_size:].copy()
|
|
||||||
|
|
||||||
def set_filter(self, filters, *, fade_blocks: int = 0) -> None:
|
|
||||||
if self.transition_active:
|
|
||||||
raise RuntimeError("finish the current filter crossfade before another update")
|
|
||||||
target = self._partition(filters)
|
|
||||||
fade_blocks = int(fade_blocks)
|
|
||||||
if fade_blocks < 0:
|
|
||||||
raise ValueError("fade_blocks must be non-negative")
|
|
||||||
if fade_blocks == 0:
|
|
||||||
self.current_spectra = target
|
|
||||||
self.current_overlap = self._overlap_for_previous_block(target)
|
|
||||||
return
|
|
||||||
self.target_spectra = target
|
|
||||||
self.target_overlap = self._overlap_for_previous_block(target)
|
|
||||||
self.fade_samples = fade_blocks * self.block_size
|
|
||||||
self.fade_position = 0
|
|
||||||
|
|
||||||
def process_block(self, samples) -> np.ndarray:
|
|
||||||
values = np.asarray(samples, dtype=np.float64)
|
|
||||||
if values.shape != (self.block_size,) or not np.isfinite(values).all():
|
|
||||||
raise ValueError(f"samples must have finite shape ({self.block_size},)")
|
|
||||||
padded = np.zeros(self.fft_size, dtype=np.float64)
|
|
||||||
padded[:self.block_size] = values
|
|
||||||
spectrum = np.asarray(np.fft.rfft(padded), dtype=np.complex128)
|
|
||||||
self.write_index = (self.write_index + 1) % self.partition_count
|
|
||||||
self.input_history[self.write_index] = spectrum
|
|
||||||
|
|
||||||
current, next_current_overlap = self._convolution_block(
|
|
||||||
self.current_spectra, self.current_overlap)
|
|
||||||
if not self.transition_active:
|
|
||||||
self.current_overlap = next_current_overlap
|
|
||||||
self.processed_blocks += 1
|
|
||||||
return current.T.copy()
|
|
||||||
|
|
||||||
target, next_target_overlap = self._convolution_block(
|
|
||||||
self.target_spectra, self.target_overlap)
|
|
||||||
sample_indices = self.fade_position + np.arange(
|
|
||||||
1, self.block_size + 1, dtype=np.float64)
|
|
||||||
amount = np.clip(sample_indices / float(self.fade_samples), 0.0, 1.0)
|
|
||||||
output = current * (1.0 - amount[None, :]) + target * amount[None, :]
|
|
||||||
self.fade_position += self.block_size
|
|
||||||
self.current_overlap = next_current_overlap
|
|
||||||
self.target_overlap = next_target_overlap
|
|
||||||
if self.fade_position >= self.fade_samples:
|
|
||||||
self.current_spectra = self.target_spectra
|
|
||||||
self.current_overlap = self.target_overlap
|
|
||||||
self.target_spectra = None
|
|
||||||
self.target_overlap = None
|
|
||||||
self.fade_samples = 0
|
|
||||||
self.fade_position = 0
|
|
||||||
self.processed_blocks += 1
|
|
||||||
return output.T.copy()
|
|
||||||
|
|
||||||
def reset(self) -> None:
|
|
||||||
self.input_history.fill(0.0)
|
|
||||||
self.current_overlap.fill(0.0)
|
|
||||||
if self.target_overlap is not None:
|
|
||||||
self.target_overlap.fill(0.0)
|
|
||||||
self.write_index = -1
|
|
||||||
self.processed_blocks = 0
|
|
||||||
self.target_spectra = None
|
|
||||||
self.target_overlap = None
|
|
||||||
self.fade_samples = 0
|
|
||||||
self.fade_position = 0
|
|
||||||
|
|
||||||
|
|
||||||
class MultiSourcePartitionedConvolver:
|
|
||||||
"""Sum independent per-source convolution outputs."""
|
|
||||||
|
|
||||||
def __init__(self, source_count: int, *, block_size: int,
|
|
||||||
max_filter_taps: int, output_channels: int = 2):
|
|
||||||
self.source_count = int(source_count)
|
|
||||||
self.block_size = int(block_size)
|
|
||||||
self.output_channels = int(output_channels)
|
|
||||||
if min(self.source_count, self.block_size, self.output_channels,
|
|
||||||
int(max_filter_taps)) <= 0:
|
|
||||||
raise ValueError("source_count, block_size, outputs and taps must be positive")
|
|
||||||
zero = np.zeros((self.output_channels, int(max_filter_taps)), dtype=np.float64)
|
|
||||||
self.convolvers = [
|
|
||||||
UniformPartitionedConvolver(
|
|
||||||
zero, block_size=self.block_size, max_filter_taps=max_filter_taps)
|
|
||||||
for _ in range(self.source_count)
|
|
||||||
]
|
|
||||||
|
|
||||||
def set_filter(self, source: int, filters, *, fade_blocks: int = 0) -> None:
|
|
||||||
source = int(source)
|
|
||||||
if not 0 <= source < self.source_count:
|
|
||||||
raise IndexError(source)
|
|
||||||
self.convolvers[source].set_filter(filters, fade_blocks=fade_blocks)
|
|
||||||
|
|
||||||
def process_block(self, sources) -> np.ndarray:
|
|
||||||
values = np.asarray(sources, dtype=np.float64)
|
|
||||||
if values.shape != (self.block_size, self.source_count):
|
|
||||||
raise ValueError(
|
|
||||||
f"sources must have shape ({self.block_size},{self.source_count})")
|
|
||||||
if not np.isfinite(values).all():
|
|
||||||
raise ValueError("sources contain non-finite values")
|
|
||||||
output = np.zeros((self.block_size, self.output_channels), dtype=np.float64)
|
|
||||||
for source in range(self.source_count):
|
|
||||||
output += self.convolvers[source].process_block(values[:, source])
|
|
||||||
return output
|
|
||||||
|
|
||||||
def process(self, sources) -> np.ndarray:
|
|
||||||
values = np.asarray(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) % self.block_size:
|
|
||||||
raise ValueError("partitioned input length must be divisible by block_size")
|
|
||||||
blocks = [
|
|
||||||
self.process_block(values[start:start + self.block_size])
|
|
||||||
for start in range(0, len(values), self.block_size)
|
|
||||||
]
|
|
||||||
return (np.concatenate(blocks, axis=0) if blocks
|
|
||||||
else np.zeros((0, self.output_channels), dtype=np.float64))
|
|
||||||
|
|
||||||
def flush(self, blocks: int | None = None) -> np.ndarray:
|
|
||||||
count = (max(conv.partition_count for conv in self.convolvers) + 2
|
|
||||||
if blocks is None else int(blocks))
|
|
||||||
if count < 0:
|
|
||||||
raise ValueError("flush block count must be non-negative")
|
|
||||||
zero = np.zeros((self.block_size, self.source_count), dtype=np.float64)
|
|
||||||
return self.process(np.tile(zero, (count, 1)))
|
|
||||||
|
|
||||||
def reset(self) -> None:
|
|
||||||
for convolver in self.convolvers:
|
|
||||||
convolver.reset()
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
"""Listener-centred point-source coordinates for direct HRTF rendering."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from dataclasses import dataclass
|
|
||||||
import math
|
|
||||||
import numpy as np
|
|
||||||
from reference_distance import ReferenceDistanceProfileV1
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class PointSourcePosition:
|
|
||||||
direction: np.ndarray
|
|
||||||
distance_m: float
|
|
||||||
normalized_radius: float
|
|
||||||
|
|
||||||
|
|
||||||
class PointSourceReference:
|
|
||||||
def __init__(self, reference_distance_m=None, *, mode=None):
|
|
||||||
self.mode = ReferenceDistanceProfileV1.validate_profile("mid" if mode is None else mode)
|
|
||||||
self.reference_distance_m = float(ReferenceDistanceProfileV1.DISTANCE_M[self.mode]
|
|
||||||
if reference_distance_m is None else reference_distance_m)
|
|
||||||
if not math.isfinite(self.reference_distance_m) or self.reference_distance_m <= 0:
|
|
||||||
raise ValueError("source reference distance must be finite and positive")
|
|
||||||
|
|
||||||
def map_position(self, position, *, previous_direction=None):
|
|
||||||
values = np.asarray(position, dtype=np.float64)
|
|
||||||
if values.shape != (3,) or not np.isfinite(values).all():
|
|
||||||
raise ValueError("source position must contain three finite values")
|
|
||||||
if np.max(np.abs(values)) > 50.1 + 1e-8:
|
|
||||||
raise ValueError("source position exceeds the finite metadata domain")
|
|
||||||
radius = float(np.linalg.norm(values))
|
|
||||||
direction = values[[1, 0, 2]] * [1., -1., 1.]
|
|
||||||
if radius > 1e-12:
|
|
||||||
direction /= radius
|
|
||||||
elif previous_direction is None:
|
|
||||||
direction = np.array([1., 0., 0.])
|
|
||||||
else:
|
|
||||||
direction = np.asarray(previous_direction, dtype=np.float64).copy()
|
|
||||||
return PointSourcePosition(direction, radius * self.reference_distance_m, radius)
|
|
||||||
|
|
||||||
def info(self):
|
|
||||||
return dict(reference_variant="direct-point-source-v1",
|
|
||||||
reference_distance_m=self.reference_distance_m,
|
|
||||||
mode=self.mode, profile_distances_m=dict(ReferenceDistanceProfileV1.DISTANCE_M),
|
|
||||||
distance_semantics="nominal-object-coordinate-scale",
|
|
||||||
distance_attenuation="none; preserve authored programme gain",
|
|
||||||
radial_adaptation="none; retain selected SOFA measurement shell",
|
|
||||||
coincident_source_direction="previous direction, otherwise front",
|
|
||||||
additional_absolute_propagation_delay=False)
|
|
||||||
@@ -1,637 +0,0 @@
|
|||||||
"""Strict SimpleFreeFieldHRIR to canonical HRTF import.
|
|
||||||
|
|
||||||
The canonical representation keeps ``Data.IR`` and ``Data.Delay`` separate.
|
|
||||||
No importer operation silently bakes the SOFA delay into the stored FIRs. A
|
|
||||||
caller must explicitly request :meth:`CanonicalHrtf.materialized_measurement`
|
|
||||||
when a time-domain FIR with ``Data.Delay`` applied exactly once is required.
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from contextlib import contextmanager
|
|
||||||
from dataclasses import dataclass, replace
|
|
||||||
from fractions import Fraction
|
|
||||||
import hashlib
|
|
||||||
import math
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
import h5py
|
|
||||||
import numpy as np
|
|
||||||
from scipy import signal
|
|
||||||
from scipy.fft import next_fast_len
|
|
||||||
|
|
||||||
|
|
||||||
_SUPPORTED_VERSIONS = {"0.4", "1.0", "1.1"}
|
|
||||||
_FREE_FIELD_ROOM_TYPES = {"free field", "free-field", "anechoic", "hemi-anechoic"}
|
|
||||||
_LENGTH_UNITS = {
|
|
||||||
"m": 1.0,
|
|
||||||
"metre": 1.0,
|
|
||||||
"metres": 1.0,
|
|
||||||
"meter": 1.0,
|
|
||||||
"meters": 1.0,
|
|
||||||
"cm": 1.0e-2,
|
|
||||||
"centimetre": 1.0e-2,
|
|
||||||
"centimetres": 1.0e-2,
|
|
||||||
"centimeter": 1.0e-2,
|
|
||||||
"centimeters": 1.0e-2,
|
|
||||||
"mm": 1.0e-3,
|
|
||||||
"millimetre": 1.0e-3,
|
|
||||||
"millimetres": 1.0e-3,
|
|
||||||
"millimeter": 1.0e-3,
|
|
||||||
"millimeters": 1.0e-3,
|
|
||||||
}
|
|
||||||
_ANGLE_UNITS = {
|
|
||||||
"degree": np.deg2rad,
|
|
||||||
"degrees": np.deg2rad,
|
|
||||||
"radian": lambda value: np.asarray(value, dtype=np.float64),
|
|
||||||
"radians": lambda value: np.asarray(value, dtype=np.float64),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class SofaImportError(ValueError):
|
|
||||||
"""The file is outside the deliberately narrow public SOFA contract."""
|
|
||||||
|
|
||||||
|
|
||||||
def _text(value: Any) -> str:
|
|
||||||
if isinstance(value, np.ndarray) and value.shape == ():
|
|
||||||
value = value.item()
|
|
||||||
if isinstance(value, (bytes, np.bytes_)):
|
|
||||||
return value.decode("utf-8", "strict")
|
|
||||||
return str(value)
|
|
||||||
|
|
||||||
|
|
||||||
def _sha256_stream(stream) -> str:
|
|
||||||
digest = hashlib.sha256()
|
|
||||||
stream.seek(0)
|
|
||||||
for block in iter(lambda: stream.read(4 << 20), b""):
|
|
||||||
digest.update(block)
|
|
||||||
stream.seek(0)
|
|
||||||
return digest.hexdigest().upper()
|
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
|
||||||
def _stable_hdf5_source(path: Path):
|
|
||||||
"""Read arrays and content identity from one stable open-file snapshot."""
|
|
||||||
with path.open("rb") as stream:
|
|
||||||
before = _sha256_stream(stream)
|
|
||||||
with h5py.File(stream, "r") as file:
|
|
||||||
yield file, before
|
|
||||||
after = _sha256_stream(stream)
|
|
||||||
if after != before:
|
|
||||||
raise SofaImportError("SOFA file changed while it was being imported")
|
|
||||||
|
|
||||||
|
|
||||||
def _tokens(units: str) -> list[str]:
|
|
||||||
return [token.strip().lower() for token in units.split(",") if token.strip()]
|
|
||||||
|
|
||||||
|
|
||||||
def _coordinate_attributes(dataset: h5py.Dataset, *, inherit=None) -> tuple[str, str]:
|
|
||||||
source = dataset.attrs
|
|
||||||
if "Type" not in source or "Units" not in source:
|
|
||||||
if inherit is None or "Type" not in inherit.attrs or "Units" not in inherit.attrs:
|
|
||||||
raise SofaImportError(f"{dataset.name} must declare Type and Units")
|
|
||||||
source = inherit.attrs
|
|
||||||
return _text(source["Type"]).strip().lower(), _text(source["Units"]).strip()
|
|
||||||
|
|
||||||
|
|
||||||
def coordinates_to_cartesian_m(values, coordinate_type: str, units: str,
|
|
||||||
*, variable: str) -> np.ndarray:
|
|
||||||
"""Convert a SOFA coordinate array to Cartesian metres without reshaping it."""
|
|
||||||
data = np.asarray(values, dtype=np.float64)
|
|
||||||
if data.shape[-1] != 3 or not np.isfinite(data).all():
|
|
||||||
raise SofaImportError(f"{variable} must contain finite C=3 coordinates")
|
|
||||||
kind = coordinate_type.strip().lower()
|
|
||||||
unit_tokens = _tokens(units)
|
|
||||||
if kind == "cartesian":
|
|
||||||
if len(unit_tokens) == 1:
|
|
||||||
factors = [_LENGTH_UNITS.get(unit_tokens[0])] * 3
|
|
||||||
elif len(unit_tokens) == 3:
|
|
||||||
factors = [_LENGTH_UNITS.get(token) for token in unit_tokens]
|
|
||||||
else:
|
|
||||||
factors = []
|
|
||||||
if len(factors) != 3 or any(value is None for value in factors):
|
|
||||||
raise SofaImportError(f"unsupported Cartesian units for {variable}: {units!r}")
|
|
||||||
return data * np.asarray(factors, dtype=np.float64)
|
|
||||||
if kind != "spherical" or len(unit_tokens) != 3:
|
|
||||||
raise SofaImportError(
|
|
||||||
f"unsupported coordinates for {variable}: Type={coordinate_type!r}, Units={units!r}")
|
|
||||||
if unit_tokens[0] not in _ANGLE_UNITS or unit_tokens[1] not in _ANGLE_UNITS:
|
|
||||||
raise SofaImportError(f"unsupported spherical angle units for {variable}: {units!r}")
|
|
||||||
radius_factor = _LENGTH_UNITS.get(unit_tokens[2])
|
|
||||||
if radius_factor is None:
|
|
||||||
raise SofaImportError(f"unsupported spherical radius unit for {variable}: {units!r}")
|
|
||||||
azimuth = _ANGLE_UNITS[unit_tokens[0]](data[..., 0])
|
|
||||||
elevation = _ANGLE_UNITS[unit_tokens[1]](data[..., 1])
|
|
||||||
radius = data[..., 2] * radius_factor
|
|
||||||
if np.any(radius < 0.0):
|
|
||||||
raise SofaImportError(f"{variable} contains a negative spherical radius")
|
|
||||||
horizontal = np.cos(elevation)
|
|
||||||
return np.stack(
|
|
||||||
(radius * horizontal * np.cos(azimuth),
|
|
||||||
radius * horizontal * np.sin(azimuth),
|
|
||||||
radius * np.sin(elevation)),
|
|
||||||
axis=-1,
|
|
||||||
).astype(np.float64, copy=False)
|
|
||||||
|
|
||||||
|
|
||||||
def _rows(file: h5py.File, name: str, measurements: int, *, inherit=None) -> np.ndarray:
|
|
||||||
if name not in file:
|
|
||||||
raise SofaImportError(f"missing required SOFA variable {name}")
|
|
||||||
dataset = file[name]
|
|
||||||
kind, units = _coordinate_attributes(dataset, inherit=inherit)
|
|
||||||
result = coordinates_to_cartesian_m(dataset[...], kind, units, variable=name)
|
|
||||||
if result.shape not in ((1, 3), (measurements, 3)):
|
|
||||||
raise SofaImportError(
|
|
||||||
f"{name} must have shape [I,C] or [M,C], got {result.shape}")
|
|
||||||
return np.broadcast_to(result, (measurements, 3)).astype(np.float64, copy=True)
|
|
||||||
|
|
||||||
|
|
||||||
def _receiver_rows(file: h5py.File, measurements: int) -> np.ndarray:
|
|
||||||
if "ReceiverPosition" not in file:
|
|
||||||
raise SofaImportError("missing required SOFA variable ReceiverPosition")
|
|
||||||
dataset = file["ReceiverPosition"]
|
|
||||||
raw = np.asarray(dataset[...], dtype=np.float64)
|
|
||||||
if raw.shape not in ((2, 3, 1), (2, 3, measurements)):
|
|
||||||
raise SofaImportError(
|
|
||||||
"ReceiverPosition must have shape [R=2,C=3,I=1 or M]")
|
|
||||||
values = np.moveaxis(raw, 1, -1) # [R,I/M,C]
|
|
||||||
kind, units = _coordinate_attributes(dataset)
|
|
||||||
cartesian = coordinates_to_cartesian_m(
|
|
||||||
values, kind, units, variable="ReceiverPosition")
|
|
||||||
cartesian = np.moveaxis(cartesian, 0, 1) # [I/M,R,C]
|
|
||||||
return np.broadcast_to(cartesian, (measurements, 2, 3)).astype(
|
|
||||||
np.float64, copy=True)
|
|
||||||
|
|
||||||
|
|
||||||
def _emitter_is_origin(file: h5py.File, measurements: int) -> None:
|
|
||||||
if "EmitterPosition" not in file:
|
|
||||||
raise SofaImportError("missing required SOFA variable EmitterPosition")
|
|
||||||
dataset = file["EmitterPosition"]
|
|
||||||
raw = np.asarray(dataset[...], dtype=np.float64)
|
|
||||||
if raw.shape not in ((1, 3, 1), (1, 3, measurements)):
|
|
||||||
raise SofaImportError("SimpleFreeFieldHRIR v1 requires E=1 EmitterPosition[E,C,I/M]")
|
|
||||||
values = np.moveaxis(raw, 1, -1)
|
|
||||||
kind, units = _coordinate_attributes(dataset)
|
|
||||||
cartesian = coordinates_to_cartesian_m(
|
|
||||||
values, kind, units, variable="EmitterPosition")
|
|
||||||
if np.max(np.abs(cartesian), initial=0.0) > 1.0e-9:
|
|
||||||
raise SofaImportError("non-zero EmitterPosition needs a separate source-pose adapter")
|
|
||||||
|
|
||||||
|
|
||||||
def _sampling_rate(file: h5py.File) -> float:
|
|
||||||
if "Data.SamplingRate" not in file:
|
|
||||||
raise SofaImportError("missing Data.SamplingRate")
|
|
||||||
dataset = file["Data.SamplingRate"]
|
|
||||||
values = np.asarray(dataset[...], dtype=np.float64).reshape(-1)
|
|
||||||
if values.size != 1 or not math.isfinite(float(values[0])) or values[0] <= 0.0:
|
|
||||||
raise SofaImportError("Data.SamplingRate must contain one positive finite value")
|
|
||||||
units = _text(dataset.attrs.get("Units", "")).strip().lower()
|
|
||||||
if units not in {"hertz", "hz"}:
|
|
||||||
raise SofaImportError(f"Data.SamplingRate Units must be hertz, got {units!r}")
|
|
||||||
return float(values[0])
|
|
||||||
|
|
||||||
|
|
||||||
def _processing_label(file: h5py.File) -> str:
|
|
||||||
parts = []
|
|
||||||
for key in ("DatabaseName", "Title", "ListenerShortName", "Comment"):
|
|
||||||
value = _text(file.attrs.get(key, "")).strip()
|
|
||||||
if value and value not in parts:
|
|
||||||
parts.append(value)
|
|
||||||
return " | ".join(parts)
|
|
||||||
|
|
||||||
|
|
||||||
def _read_delay(file: h5py.File, measurements: int) -> np.ndarray:
|
|
||||||
if "Data.Delay" not in file:
|
|
||||||
raise SofaImportError("missing Data.Delay")
|
|
||||||
delay = np.asarray(file["Data.Delay"][...], dtype=np.float64)
|
|
||||||
if delay.shape not in ((1, 2), (measurements, 2)) or not np.isfinite(delay).all():
|
|
||||||
raise SofaImportError("Data.Delay must have finite shape [I=1,R=2] or [M,R=2]")
|
|
||||||
delay = np.broadcast_to(delay, (measurements, 2)).astype(np.float64, copy=True)
|
|
||||||
if np.min(delay, initial=0.0) < -1.0e-9:
|
|
||||||
raise SofaImportError("negative Data.Delay is outside the supported causal contract")
|
|
||||||
delay[delay < 0.0] = 0.0
|
|
||||||
return delay
|
|
||||||
|
|
||||||
|
|
||||||
def _readonly(array, dtype) -> np.ndarray:
|
|
||||||
result = np.asarray(array, dtype=dtype)
|
|
||||||
result.setflags(write=False)
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class CanonicalHrtf:
|
|
||||||
source_path: str
|
|
||||||
source_sha256: str
|
|
||||||
convention: str
|
|
||||||
convention_version: str
|
|
||||||
sofa_version: str
|
|
||||||
source_sample_rate_hz: float
|
|
||||||
sample_rate_hz: float
|
|
||||||
source_position_cartesian_m: np.ndarray # listener-local [M,3]
|
|
||||||
listener_view: np.ndarray # world, normalized [M,3]
|
|
||||||
listener_up: np.ndarray # world, orthonormal [M,3]
|
|
||||||
receiver_position_cartesian_m: np.ndarray # listener-local, L/R [M,2,3]
|
|
||||||
left_receiver_index: int
|
|
||||||
right_receiver_index: int
|
|
||||||
hrir: np.ndarray # canonical L/R [M,2,N]
|
|
||||||
delay_samples: np.ndarray # canonical L/R [M,2], not applied
|
|
||||||
measurement_radius_m: np.ndarray # [M]
|
|
||||||
processing_label: str
|
|
||||||
resampling_label: str = "none"
|
|
||||||
|
|
||||||
def __post_init__(self):
|
|
||||||
object.__setattr__(self, "source_position_cartesian_m", _readonly(
|
|
||||||
self.source_position_cartesian_m, np.float64))
|
|
||||||
object.__setattr__(self, "listener_view", _readonly(self.listener_view, np.float64))
|
|
||||||
object.__setattr__(self, "listener_up", _readonly(self.listener_up, np.float64))
|
|
||||||
object.__setattr__(self, "receiver_position_cartesian_m", _readonly(
|
|
||||||
self.receiver_position_cartesian_m, np.float64))
|
|
||||||
object.__setattr__(self, "hrir", _readonly(self.hrir, np.float64))
|
|
||||||
object.__setattr__(self, "delay_samples", _readonly(self.delay_samples, np.float64))
|
|
||||||
object.__setattr__(self, "measurement_radius_m", _readonly(
|
|
||||||
self.measurement_radius_m, np.float64))
|
|
||||||
|
|
||||||
@property
|
|
||||||
def measurements(self) -> int:
|
|
||||||
return int(self.hrir.shape[0])
|
|
||||||
|
|
||||||
@property
|
|
||||||
def taps(self) -> int:
|
|
||||||
return int(self.hrir.shape[2])
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unit_directions(self) -> np.ndarray:
|
|
||||||
return self.source_position_cartesian_m / self.measurement_radius_m[:, None]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def shells_m(self) -> np.ndarray:
|
|
||||||
return np.unique(np.round(self.measurement_radius_m, 9))
|
|
||||||
|
|
||||||
def shell_indices(self, radius_m: float) -> np.ndarray:
|
|
||||||
shell = float(self.shells_m[np.argmin(np.abs(self.shells_m - float(radius_m)))])
|
|
||||||
return np.flatnonzero(np.isclose(
|
|
||||||
self.measurement_radius_m, shell, atol=5.0e-7, rtol=0.0))
|
|
||||||
|
|
||||||
def nearest_index(self, direction_sofa, radius_m: float = 1.0) -> tuple[int, float]:
|
|
||||||
direction = np.asarray(direction_sofa, dtype=np.float64)
|
|
||||||
if direction.shape != (3,) or not np.isfinite(direction).all():
|
|
||||||
raise ValueError("direction must contain three finite SOFA Cartesian values")
|
|
||||||
norm = float(np.linalg.norm(direction))
|
|
||||||
if norm <= 1.0e-15:
|
|
||||||
raise ValueError("direction must be non-zero")
|
|
||||||
direction = direction / norm
|
|
||||||
indices = self.shell_indices(radius_m)
|
|
||||||
dots = self.unit_directions[indices] @ direction
|
|
||||||
local = int(np.argmax(dots))
|
|
||||||
error = math.degrees(math.acos(float(np.clip(dots[local], -1.0, 1.0))))
|
|
||||||
return int(indices[local]), float(error)
|
|
||||||
|
|
||||||
def resampled(self, target_sample_rate_hz: float) -> "CanonicalHrtf":
|
|
||||||
target = float(target_sample_rate_hz)
|
|
||||||
if not math.isfinite(target) or target <= 0.0:
|
|
||||||
raise ValueError("target sample rate must be positive and finite")
|
|
||||||
if abs(target - self.sample_rate_hz) <= 1.0e-9:
|
|
||||||
return self
|
|
||||||
ratio = target / self.sample_rate_hz
|
|
||||||
fraction = Fraction(ratio).limit_denominator(100000)
|
|
||||||
if abs(float(fraction) - ratio) > 1.0e-10:
|
|
||||||
raise ValueError("sample-rate ratio cannot be represented safely")
|
|
||||||
converted = signal.resample_poly(
|
|
||||||
np.asarray(self.hrir, dtype=np.float64), fraction.numerator,
|
|
||||||
fraction.denominator, axis=-1, window=("kaiser", 8.6), padtype="constant")
|
|
||||||
converted = np.asarray(converted, dtype=np.float64)
|
|
||||||
return replace(
|
|
||||||
self,
|
|
||||||
sample_rate_hz=target,
|
|
||||||
hrir=converted,
|
|
||||||
delay_samples=np.asarray(self.delay_samples * ratio, dtype=np.float64),
|
|
||||||
resampling_label=(
|
|
||||||
f"scipy.signal.resample_poly {self.sample_rate_hz:g}->{target:g} Hz "
|
|
||||||
f"({fraction.numerator}/{fraction.denominator}, Kaiser beta=8.6)"),
|
|
||||||
)
|
|
||||||
|
|
||||||
def materialized_measurement(self, index: int, *, fractional_half_length: int = 48
|
|
||||||
) -> np.ndarray:
|
|
||||||
"""Return [L/R,taps] with SOFA Data.Delay applied exactly once."""
|
|
||||||
index = int(index)
|
|
||||||
if not 0 <= index < self.measurements:
|
|
||||||
raise IndexError(index)
|
|
||||||
ears = []
|
|
||||||
for ear in range(2):
|
|
||||||
ears.append(apply_fractional_delay(
|
|
||||||
self.hrir[index, ear], float(self.delay_samples[index, ear]),
|
|
||||||
half_length=fractional_half_length))
|
|
||||||
length = max(map(len, ears))
|
|
||||||
result = np.zeros((2, length), dtype=np.float64)
|
|
||||||
for ear, value in enumerate(ears):
|
|
||||||
result[ear, :len(value)] = value
|
|
||||||
return result
|
|
||||||
|
|
||||||
def info(self) -> dict:
|
|
||||||
return {
|
|
||||||
"source_path": self.source_path,
|
|
||||||
"source_sha256": self.source_sha256,
|
|
||||||
"convention": self.convention,
|
|
||||||
"convention_version": self.convention_version,
|
|
||||||
"sofa_version": self.sofa_version,
|
|
||||||
"source_sample_rate_hz": self.source_sample_rate_hz,
|
|
||||||
"sample_rate_hz": self.sample_rate_hz,
|
|
||||||
"measurements": self.measurements,
|
|
||||||
"taps": self.taps,
|
|
||||||
"shells_m": [float(value) for value in self.shells_m],
|
|
||||||
"source_receiver_order": [self.left_receiver_index, self.right_receiver_index],
|
|
||||||
"canonical_ear_order": ["left", "right"],
|
|
||||||
"data_delay_samples_min": float(np.min(self.delay_samples)),
|
|
||||||
"data_delay_samples_max": float(np.max(self.delay_samples)),
|
|
||||||
"data_delay_applied": False,
|
|
||||||
"processing_label": self.processing_label,
|
|
||||||
"resampling": self.resampling_label,
|
|
||||||
"precision": "float64",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def load_simple_free_field_hrir(path, *, target_sample_rate_hz: float | None = None
|
|
||||||
) -> CanonicalHrtf:
|
|
||||||
"""Strictly import the supported SimpleFreeFieldHRIR subset."""
|
|
||||||
source = Path(path).expanduser().resolve()
|
|
||||||
if not source.is_file():
|
|
||||||
raise FileNotFoundError(source)
|
|
||||||
with _stable_hdf5_source(source) as (file, source_sha256):
|
|
||||||
if _text(file.attrs.get("Conventions", "")) != "SOFA":
|
|
||||||
raise SofaImportError("Conventions must be SOFA")
|
|
||||||
convention = _text(file.attrs.get("SOFAConventions", ""))
|
|
||||||
if convention != "SimpleFreeFieldHRIR":
|
|
||||||
raise SofaImportError(
|
|
||||||
f"unsupported SOFAConventions={convention!r}; convert explicitly first")
|
|
||||||
convention_version = _text(file.attrs.get("SOFAConventionsVersion", ""))
|
|
||||||
if convention_version not in _SUPPORTED_VERSIONS:
|
|
||||||
raise SofaImportError(
|
|
||||||
f"unsupported SimpleFreeFieldHRIR version {convention_version!r}; "
|
|
||||||
f"supported={sorted(_SUPPORTED_VERSIONS)}")
|
|
||||||
if _text(file.attrs.get("DataType", "")) != "FIR":
|
|
||||||
raise SofaImportError("DataType must be FIR")
|
|
||||||
room_type = _text(file.attrs.get("RoomType", "")).strip().lower()
|
|
||||||
if room_type not in _FREE_FIELD_ROOM_TYPES:
|
|
||||||
raise SofaImportError(f"RoomType must explicitly be free-field, got {room_type!r}")
|
|
||||||
if "Data.IR" not in file:
|
|
||||||
raise SofaImportError("missing Data.IR")
|
|
||||||
hrir_source = np.asarray(file["Data.IR"][...], dtype=np.float64)
|
|
||||||
if hrir_source.ndim != 3 or hrir_source.shape[1] != 2 or min(hrir_source.shape) <= 0:
|
|
||||||
raise SofaImportError("Data.IR must have shape [M,R=2,N]")
|
|
||||||
if not np.isfinite(hrir_source).all():
|
|
||||||
raise SofaImportError("Data.IR contains non-finite values")
|
|
||||||
measurements = int(hrir_source.shape[0])
|
|
||||||
sample_rate = _sampling_rate(file)
|
|
||||||
delay_source = _read_delay(file, measurements)
|
|
||||||
_emitter_is_origin(file, measurements)
|
|
||||||
|
|
||||||
listener_position = _rows(file, "ListenerPosition", measurements)
|
|
||||||
listener_view = _rows(file, "ListenerView", measurements)
|
|
||||||
listener_up_raw = _rows(
|
|
||||||
file, "ListenerUp", measurements,
|
|
||||||
inherit=file["ListenerView"] if "ListenerView" in file else None)
|
|
||||||
forward_norm = np.linalg.norm(listener_view, axis=1)
|
|
||||||
if np.any(forward_norm <= 1.0e-12):
|
|
||||||
raise SofaImportError("ListenerView must be non-zero")
|
|
||||||
forward = listener_view / forward_norm[:, None]
|
|
||||||
left = np.cross(listener_up_raw, forward)
|
|
||||||
left_norm = np.linalg.norm(left, axis=1)
|
|
||||||
if np.any(left_norm <= 1.0e-12):
|
|
||||||
raise SofaImportError("ListenerUp must not be parallel to ListenerView")
|
|
||||||
left /= left_norm[:, None]
|
|
||||||
up = np.cross(forward, left)
|
|
||||||
|
|
||||||
if "SourcePosition" not in file:
|
|
||||||
raise SofaImportError("missing required SOFA variable SourcePosition")
|
|
||||||
source_dataset = file["SourcePosition"]
|
|
||||||
source_type, source_units = _coordinate_attributes(source_dataset)
|
|
||||||
source_world = coordinates_to_cartesian_m(
|
|
||||||
source_dataset[...], source_type, source_units, variable="SourcePosition")
|
|
||||||
if source_world.shape != (measurements, 3):
|
|
||||||
raise SofaImportError("SourcePosition must have shape [M,C=3]")
|
|
||||||
relative = source_world - listener_position
|
|
||||||
source_local = np.stack(
|
|
||||||
(np.sum(relative * forward, axis=1),
|
|
||||||
np.sum(relative * left, axis=1),
|
|
||||||
np.sum(relative * up, axis=1)), axis=1)
|
|
||||||
radii = np.linalg.norm(source_local, axis=1)
|
|
||||||
if np.any(radii <= 1.0e-8) or not np.isfinite(radii).all():
|
|
||||||
raise SofaImportError("every source measurement must have a positive radius")
|
|
||||||
|
|
||||||
receiver = _receiver_rows(file, measurements)
|
|
||||||
lateral_difference = receiver[:, 0, 1] - receiver[:, 1, 1]
|
|
||||||
if np.all(lateral_difference > 1.0e-5):
|
|
||||||
left_index, right_index = 0, 1
|
|
||||||
elif np.all(lateral_difference < -1.0e-5):
|
|
||||||
left_index, right_index = 1, 0
|
|
||||||
else:
|
|
||||||
raise SofaImportError(
|
|
||||||
"ReceiverPosition does not identify one consistently-left and one "
|
|
||||||
"consistently-right receiver")
|
|
||||||
ear_order = [left_index, right_index]
|
|
||||||
hrir = hrir_source[:, ear_order, :]
|
|
||||||
delay = delay_source[:, ear_order]
|
|
||||||
receiver = receiver[:, ear_order, :]
|
|
||||||
processing_label = _processing_label(file)
|
|
||||||
sofa_version = _text(file.attrs.get("Version", ""))
|
|
||||||
|
|
||||||
canonical = CanonicalHrtf(
|
|
||||||
source_path=str(source),
|
|
||||||
source_sha256=source_sha256,
|
|
||||||
convention=convention,
|
|
||||||
convention_version=convention_version,
|
|
||||||
sofa_version=sofa_version,
|
|
||||||
source_sample_rate_hz=sample_rate,
|
|
||||||
sample_rate_hz=sample_rate,
|
|
||||||
source_position_cartesian_m=source_local,
|
|
||||||
listener_view=forward,
|
|
||||||
listener_up=up,
|
|
||||||
receiver_position_cartesian_m=receiver,
|
|
||||||
left_receiver_index=left_index,
|
|
||||||
right_receiver_index=right_index,
|
|
||||||
hrir=hrir,
|
|
||||||
delay_samples=delay,
|
|
||||||
measurement_radius_m=radii,
|
|
||||||
processing_label=processing_label,
|
|
||||||
)
|
|
||||||
return (canonical if target_sample_rate_hz is None
|
|
||||||
else canonical.resampled(target_sample_rate_hz))
|
|
||||||
|
|
||||||
|
|
||||||
def apply_fractional_delay(values, delay_samples: float, *, half_length: int = 48
|
|
||||||
) -> np.ndarray:
|
|
||||||
"""Apply one causal non-negative delay to a real FIR using windowed sinc."""
|
|
||||||
source = np.asarray(values, dtype=np.float64)
|
|
||||||
delay = float(delay_samples)
|
|
||||||
if source.ndim != 1 or not np.isfinite(source).all():
|
|
||||||
raise ValueError("fractional delay input must be a finite real vector")
|
|
||||||
if not math.isfinite(delay) or delay < -1.0e-12:
|
|
||||||
raise ValueError("fractional delay must be finite and non-negative")
|
|
||||||
if delay < 1.0e-12:
|
|
||||||
return source.copy()
|
|
||||||
integer = int(math.floor(delay))
|
|
||||||
fraction = delay - integer
|
|
||||||
if fraction < 1.0e-12:
|
|
||||||
return np.pad(source, (integer, 0)).astype(np.float64, copy=False)
|
|
||||||
half = int(half_length)
|
|
||||||
if half < 8:
|
|
||||||
raise ValueError("fractional delay half_length must be at least 8")
|
|
||||||
index = np.arange(-half, half + 1, dtype=np.float64)
|
|
||||||
kernel = np.sinc(index - fraction) * np.kaiser(2 * half + 1, 8.6)
|
|
||||||
kernel /= np.sum(kernel, dtype=np.float64)
|
|
||||||
full = signal.fftconvolve(source, kernel, mode="full")
|
|
||||||
causal = np.asarray(full[half:], dtype=np.float64)
|
|
||||||
return np.pad(causal, (integer, 0)).astype(np.float64, copy=False)
|
|
||||||
|
|
||||||
|
|
||||||
def shift_signal_fft(values, shift_samples: float) -> np.ndarray:
|
|
||||||
"""Band-limited linear shift; positive is delay and negative is advance."""
|
|
||||||
source = np.asarray(values, dtype=np.float64)
|
|
||||||
shift = float(shift_samples)
|
|
||||||
if source.ndim != 1 or not np.isfinite(source).all() or not math.isfinite(shift):
|
|
||||||
raise ValueError("shift input and amount must be finite")
|
|
||||||
if abs(shift) < 1.0e-12:
|
|
||||||
return source.copy()
|
|
||||||
guard = max(128, int(math.ceil(abs(shift))) + 64)
|
|
||||||
needed = len(source) + 2 * guard
|
|
||||||
fft_size = next_fast_len(needed)
|
|
||||||
padded = np.zeros(fft_size, dtype=np.float64)
|
|
||||||
padded[guard:guard + len(source)] = source
|
|
||||||
bins = np.arange(fft_size // 2 + 1, dtype=np.float64)
|
|
||||||
spectrum = np.fft.rfft(padded)
|
|
||||||
spectrum *= np.exp(-2j * np.pi * bins * shift / fft_size)
|
|
||||||
shifted = np.fft.irfft(spectrum, fft_size)
|
|
||||||
return np.asarray(shifted[guard:guard + len(source)], dtype=np.float64)
|
|
||||||
|
|
||||||
|
|
||||||
def estimate_interaural_delay_samples(left, right, sample_rate_hz: float,
|
|
||||||
*, low_hz: float = 200.0,
|
|
||||||
high_hz: float = 1500.0) -> float:
|
|
||||||
"""Estimate L-minus-R delay by low-frequency circular phase coherence.
|
|
||||||
|
|
||||||
A coarse-to-fine delay search avoids the phase-unwrapping branch failures
|
|
||||||
that ordinary straight-line regression can exhibit for strongly filtered
|
|
||||||
Far responses.
|
|
||||||
"""
|
|
||||||
left = np.asarray(left, dtype=np.float64)
|
|
||||||
right = np.asarray(right, dtype=np.float64)
|
|
||||||
if left.shape != right.shape or left.ndim != 1:
|
|
||||||
raise ValueError("ITD inputs must be equal-length vectors")
|
|
||||||
fft_size = next_fast_len(max(4096, 4 * len(left)))
|
|
||||||
left_spectrum = np.fft.rfft(left, fft_size)
|
|
||||||
right_spectrum = np.fft.rfft(right, fft_size)
|
|
||||||
frequency = np.fft.rfftfreq(fft_size, 1.0 / float(sample_rate_hz))
|
|
||||||
selected = (frequency >= low_hz) & (frequency <= high_hz)
|
|
||||||
if np.count_nonzero(selected) < 8:
|
|
||||||
return 0.0
|
|
||||||
cross = left_spectrum[selected] * np.conj(right_spectrum[selected])
|
|
||||||
magnitude = np.abs(cross)
|
|
||||||
maximum = float(np.max(magnitude, initial=0.0))
|
|
||||||
if maximum <= 1.0e-20:
|
|
||||||
return 0.0
|
|
||||||
weighted_unit = cross / np.maximum(magnitude, 1.0e-30)
|
|
||||||
weight = np.sqrt(magnitude / maximum)
|
|
||||||
weighted_unit *= weight
|
|
||||||
omega = 2.0 * np.pi * frequency[selected] / float(sample_rate_hz)
|
|
||||||
limit = 0.0012 * float(sample_rate_hz)
|
|
||||||
|
|
||||||
def best(candidates: np.ndarray) -> float:
|
|
||||||
steering = np.exp(1j * omega[:, None] * candidates[None, :])
|
|
||||||
score = np.abs(weighted_unit @ steering)
|
|
||||||
return float(candidates[int(np.argmax(score))])
|
|
||||||
|
|
||||||
coarse = np.arange(-limit, limit + 0.25, 0.5, dtype=np.float64)
|
|
||||||
estimate = best(coarse)
|
|
||||||
fine = np.arange(estimate - 0.6, estimate + 0.6001, 0.02, dtype=np.float64)
|
|
||||||
return float(np.clip(best(fine), -limit, limit))
|
|
||||||
|
|
||||||
def _subsample_peak(values: np.ndarray) -> float:
|
|
||||||
magnitude = np.abs(np.asarray(values, dtype=np.float64))
|
|
||||||
index = int(np.argmax(magnitude))
|
|
||||||
if index == 0 or index + 1 >= len(magnitude):
|
|
||||||
return float(index)
|
|
||||||
y0, y1, y2 = (float(magnitude[index - 1]), float(magnitude[index]),
|
|
||||||
float(magnitude[index + 1]))
|
|
||||||
denominator = y0 - 2.0 * y1 + y2
|
|
||||||
correction = 0.0 if abs(denominator) < 1.0e-30 else 0.5 * (y0 - y2) / denominator
|
|
||||||
return float(index + np.clip(correction, -0.5, 0.5))
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class TimeAlignedHrtf:
|
|
||||||
canonical: CanonicalHrtf
|
|
||||||
aligned_hrir: np.ndarray
|
|
||||||
runtime_delay_samples: np.ndarray
|
|
||||||
embedded_delay_removed_samples: np.ndarray
|
|
||||||
delay_source: str
|
|
||||||
|
|
||||||
def __post_init__(self):
|
|
||||||
object.__setattr__(self, "aligned_hrir", _readonly(self.aligned_hrir, np.float64))
|
|
||||||
object.__setattr__(self, "runtime_delay_samples", _readonly(
|
|
||||||
self.runtime_delay_samples, np.float64))
|
|
||||||
object.__setattr__(self, "embedded_delay_removed_samples", _readonly(
|
|
||||||
self.embedded_delay_removed_samples, np.float64))
|
|
||||||
|
|
||||||
|
|
||||||
def time_align_hrtf(canonical: CanonicalHrtf) -> TimeAlignedHrtf:
|
|
||||||
"""Separate one delay representation before directional interpolation.
|
|
||||||
|
|
||||||
Trusted non-zero ``Data.Delay`` is external to ``Data.IR`` and is therefore
|
|
||||||
retained without de-rotating the FIR. When ``Data.Delay`` is identically
|
|
||||||
zero, ordinary measured HRIRs with a positive onset use their per-ear main
|
|
||||||
peaks. A zero-origin effective FIR is already expressed at one common
|
|
||||||
time origin; its interaural phase is therefore retained in ``Data.IR``.
|
|
||||||
|
|
||||||
These representations are mutually exclusive. Runtime rendering must
|
|
||||||
restore exactly the delay separated here and must not add any second ear
|
|
||||||
delay or phase-group delay.
|
|
||||||
"""
|
|
||||||
hrir = np.asarray(canonical.hrir, dtype=np.float64)
|
|
||||||
if np.max(np.abs(canonical.delay_samples), initial=0.0) > 1.0e-12:
|
|
||||||
return TimeAlignedHrtf(
|
|
||||||
canonical=canonical,
|
|
||||||
aligned_hrir=hrir.copy(),
|
|
||||||
runtime_delay_samples=np.asarray(canonical.delay_samples, dtype=np.float64),
|
|
||||||
embedded_delay_removed_samples=np.zeros_like(canonical.delay_samples),
|
|
||||||
delay_source="Data.Delay (external; applied once at render time)",
|
|
||||||
)
|
|
||||||
|
|
||||||
measurements = canonical.measurements
|
|
||||||
runtime = np.zeros((measurements, 2), dtype=np.float64)
|
|
||||||
removed = np.zeros_like(runtime)
|
|
||||||
used_peak = 0
|
|
||||||
retained_embedded_phase = 0
|
|
||||||
for measurement in range(measurements):
|
|
||||||
peaks = np.asarray([
|
|
||||||
_subsample_peak(hrir[measurement, 0]),
|
|
||||||
_subsample_peak(hrir[measurement, 1]),
|
|
||||||
], dtype=np.float64)
|
|
||||||
if float(np.max(peaks)) > 2.0:
|
|
||||||
delays = peaks
|
|
||||||
used_peak += 1
|
|
||||||
else:
|
|
||||||
# An effective response can have both ear FIRs beginning at sample
|
|
||||||
# zero while still carrying the correct ITD in complex phase. Do
|
|
||||||
# not invent an external delay which SOFA did not author.
|
|
||||||
delays = np.zeros(2, dtype=np.float64)
|
|
||||||
retained_embedded_phase += 1
|
|
||||||
runtime[measurement] = delays
|
|
||||||
removed[measurement] = delays
|
|
||||||
|
|
||||||
aligned = np.empty_like(hrir)
|
|
||||||
for measurement in range(measurements):
|
|
||||||
for ear in range(2):
|
|
||||||
aligned[measurement, ear] = shift_signal_fft(
|
|
||||||
hrir[measurement, ear], -float(removed[measurement, ear]))
|
|
||||||
source = (
|
|
||||||
f"embedded Data.IR arrival separation: peak={used_peak}, "
|
|
||||||
f"zero-origin embedded phase retained={retained_embedded_phase}; "
|
|
||||||
"positive onset restored once at render time")
|
|
||||||
return TimeAlignedHrtf(
|
|
||||||
canonical=canonical,
|
|
||||||
aligned_hrir=aligned,
|
|
||||||
runtime_delay_samples=runtime,
|
|
||||||
embedded_delay_removed_samples=removed,
|
|
||||||
delay_source=source,
|
|
||||||
)
|
|
||||||
@@ -1,254 +0,0 @@
|
|||||||
"""Direct rendering of full SOFA HRIRs and explicitly selected compact fields."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from collections import deque
|
|
||||||
import math
|
|
||||||
from pathlib import Path
|
|
||||||
import warnings
|
|
||||||
import numpy as np
|
|
||||||
from scipy import signal
|
|
||||||
|
|
||||||
from partitioned_convolution import MultiSourcePartitionedConvolver
|
|
||||||
from point_source import PointSourceReference
|
|
||||||
from sofa_hrir import FullHrirField
|
|
||||||
|
|
||||||
|
|
||||||
class _CompiledField:
|
|
||||||
def __init__(self, path):
|
|
||||||
from sofa_hrtf_field import SofaHrtfField
|
|
||||||
self.field = SofaHrtfField.load(path)
|
|
||||||
self.fft_size = 2048
|
|
||||||
self.measurement_radius_m = self.field.measurement_radius_m
|
|
||||||
i = np.arange(128)
|
|
||||||
z = 1 - 2 * (i + .5) / 128
|
|
||||||
phi = i * np.pi * (3 - math.sqrt(5))
|
|
||||||
directions = np.column_stack((np.sqrt(1 - z * z) * np.cos(phi),
|
|
||||||
np.sqrt(1 - z * z) * np.sin(phi), z))
|
|
||||||
self.recording_origin_samples = int(round(float(np.median([
|
|
||||||
self.field.evaluate_sofa(q).delay_samples.mean() for q in directions]))))
|
|
||||||
|
|
||||||
def info(self):
|
|
||||||
return dict(source_sha256=self.field.source_sha256, method="compiled-band-field",
|
|
||||||
measurement_radius_m=self.measurement_radius_m,
|
|
||||||
cache_key=self.field.cache_key, format_version=self.field.format_version,
|
|
||||||
recording_origin_samples=self.recording_origin_samples,
|
|
||||||
radial_adaptation="none")
|
|
||||||
|
|
||||||
|
|
||||||
class DirectSofaBackend:
|
|
||||||
fixed_reference = True
|
|
||||||
default_profile = "mid"
|
|
||||||
dsp_backend = "python-full-hrir"
|
|
||||||
block_samples = 128
|
|
||||||
|
|
||||||
def __init__(self, source, *, compiled=False, source_count=16, output_gain=1.,
|
|
||||||
method="lf_logmag_phase", shell_radius_m=None, mode=None,
|
|
||||||
reference_distance_m=None, sofa_room=False, room_preset=None):
|
|
||||||
if source_count <= 0 or not math.isfinite(output_gain):
|
|
||||||
raise ValueError("invalid source count or output gain")
|
|
||||||
self.source_count, self.output_gain = int(source_count), float(output_gain)
|
|
||||||
self.reference = PointSourceReference(reference_distance_m, mode=mode)
|
|
||||||
self.default_profile = self.reference.mode
|
|
||||||
self.compiled = bool(compiled)
|
|
||||||
self.field = (_CompiledField(source) if compiled else
|
|
||||||
FullHrirField(source, method=method, shell_radius_m=shell_radius_m))
|
|
||||||
self.hrtf_input_kind = "compiled_cache" if compiled else "sofa"
|
|
||||||
self.response_kind = "hrir" if compiled else getattr(self.field, "response_kind", "hrir")
|
|
||||||
if self.response_kind == "brir":
|
|
||||||
message = "输入为包含房间响应的BRIR,将直接渲染文件响应。"
|
|
||||||
if sofa_room:
|
|
||||||
message += "已忽略--sofa-room。"
|
|
||||||
warnings.warn(message, UserWarning)
|
|
||||||
sofa_room = False
|
|
||||||
self.hrtf_input_path = str(Path(source).expanduser().resolve())
|
|
||||||
self._last = [None] * self.source_count
|
|
||||||
self.distance_states = [None] * self.source_count
|
|
||||||
self.processed_input_samples = self.output_samples = self.parameter_updates = 0
|
|
||||||
self.finished = False
|
|
||||||
self._filter_updates = deque()
|
|
||||||
self.origin = self.field.recording_origin_samples
|
|
||||||
if compiled:
|
|
||||||
from hybrid_paths import HybridObjectPathRenderer
|
|
||||||
from rosella_bank import RosellaBank
|
|
||||||
self.dsp_backend = "python-compiled-hrtf"
|
|
||||||
self.bank = RosellaBank(1, source_channels=self.source_count, output_channels=2)
|
|
||||||
maximum = int(math.ceil(float(np.max(self.field.field.delay_bounds)) / 64))
|
|
||||||
self.paths = HybridObjectPathRenderer(self.source_count, history_slots=maximum + 8,
|
|
||||||
transition_slots=2)
|
|
||||||
self.latency_samples = self.bank.latency_samples
|
|
||||||
from rosella_direct import special_lfe_direct
|
|
||||||
self.lfe_transfer = special_lfe_direct().gains
|
|
||||||
else:
|
|
||||||
self.latency_samples = self.field.common_latency_samples
|
|
||||||
self.convolver = MultiSourcePartitionedConvolver(
|
|
||||||
self.source_count, block_size=128, max_filter_taps=self.field.fft_size)
|
|
||||||
self.lfe_filter = np.zeros((2, self.field.fft_size), dtype=np.float64)
|
|
||||||
start = self.latency_samples + max(self.origin, 0) - 64
|
|
||||||
if start < 0 or start + 129 > self.field.fft_size:
|
|
||||||
raise ValueError("LFE latency exceeds filter budget")
|
|
||||||
self.lfe_filter[:, start:start + 129] = signal.firwin(129, 150., fs=48000) / math.sqrt(2)
|
|
||||||
self._latency_remaining = self.latency_samples
|
|
||||||
self.room = None
|
|
||||||
self.filter_taps = self.field.fft_size
|
|
||||||
if sofa_room:
|
|
||||||
from sofa_room import SofaRoom
|
|
||||||
self.room = SofaRoom(self.source_count, self.reference,
|
|
||||||
implementation_latency=self.latency_samples, recording_origin=self.origin,
|
|
||||||
preset=room_preset)
|
|
||||||
if self.compiled:
|
|
||||||
maximum += int(math.ceil(self.room.early_delay_bound_samples / 64))
|
|
||||||
self.paths = HybridObjectPathRenderer(self.source_count, history_slots=maximum + 8,
|
|
||||||
transition_slots=2)
|
|
||||||
else:
|
|
||||||
self.filter_taps = self.room.filter_budget(self.field)
|
|
||||||
self.convolver = MultiSourcePartitionedConvolver(
|
|
||||||
self.source_count, block_size=128, max_filter_taps=self.filter_taps)
|
|
||||||
|
|
||||||
def set_source(self, source, position_adm, *, profile=None, gain=1., enabled=True,
|
|
||||||
special_lfe=False, fade=True):
|
|
||||||
if self.finished:
|
|
||||||
raise RuntimeError("renderer is finished")
|
|
||||||
source = int(source)
|
|
||||||
if not 0 <= source < self.source_count or not math.isfinite(gain):
|
|
||||||
raise ValueError("invalid source or gain")
|
|
||||||
if profile not in (None, self.default_profile):
|
|
||||||
raise ValueError("source profile must match the selected reference mode")
|
|
||||||
position = np.asarray(position_adm, dtype=np.float64)
|
|
||||||
if position.shape != (3,) or not np.isfinite(position).all():
|
|
||||||
raise ValueError("source position must be finite")
|
|
||||||
gain = float(gain) if enabled else 0.
|
|
||||||
key = (tuple(position), gain, bool(special_lfe))
|
|
||||||
if key == self._last[source]:
|
|
||||||
return
|
|
||||||
first = self.processed_input_samples == 0
|
|
||||||
if special_lfe:
|
|
||||||
if self.room is not None:
|
|
||||||
self.room.set_source(source, None, gain, first=first, special_lfe=True)
|
|
||||||
if self.compiled:
|
|
||||||
from hybrid_paths import HybridPath
|
|
||||||
f = self.field.field.band_center_frequencies_hz
|
|
||||||
transfer = self.lfe_transfer * gain * np.exp(-2j * np.pi * (self.origin % 64) * f / 48000)
|
|
||||||
path = HybridPath("lfe", np.full(2, self.origin // 64, dtype=np.int64), transfer)
|
|
||||||
self.paths.set_paths(source, (path,), fade_slots=0 if first else 2)
|
|
||||||
else:
|
|
||||||
self._set_filter(source, self.lfe_filter * gain, first=first, fade=fade)
|
|
||||||
else:
|
|
||||||
previous = self.distance_states[source]
|
|
||||||
state = self.reference.map_position(
|
|
||||||
position, previous_direction=None if previous is None else previous.direction)
|
|
||||||
if self.compiled:
|
|
||||||
from hybrid_paths import HybridPath
|
|
||||||
evaluation = self.field.field.evaluate_sofa(state.direction)
|
|
||||||
slots = np.floor(evaluation.delay_samples / 64).astype(np.int64)
|
|
||||||
residual = evaluation.delay_samples - slots * 64
|
|
||||||
f = self.field.field.band_center_frequencies_hz
|
|
||||||
transfer = evaluation.aligned_gains * np.exp(-2j * np.pi * residual[:, None] * f / 48000)
|
|
||||||
direct_gain = 1. if self.room is None else self.room.direct_gain(state)
|
|
||||||
paths = [HybridPath("direct", slots, transfer * gain * direct_gain)]
|
|
||||||
if self.room is not None:
|
|
||||||
for reflection in self.room.paths(state):
|
|
||||||
q = reflection.direction_adm[[1, 0, 2]] * [1, -1, 1]
|
|
||||||
e = self.field.field.evaluate_sofa(q)
|
|
||||||
total = e.delay_samples + reflection.extra_delay_samples
|
|
||||||
early_slots = np.floor(total / 64).astype(np.int64)
|
|
||||||
residual = total - early_slots * 64
|
|
||||||
h = e.aligned_gains * np.exp(-2j * np.pi * residual[:, None] * f / 48000)
|
|
||||||
h *= gain * self.room.early_gain(reflection, self.field.measurement_radius_m)
|
|
||||||
paths.append(HybridPath("early:" + reflection.wall, early_slots, h))
|
|
||||||
self.paths.set_paths(source, paths, fade_slots=0 if first or not fade else 2)
|
|
||||||
else:
|
|
||||||
if self.room is None:
|
|
||||||
spectrum = self.field.spectrum(state.direction)
|
|
||||||
hrir = np.roll(np.fft.irfft(spectrum, self.field.fft_size),
|
|
||||||
self.field.common_latency_samples, axis=-1)
|
|
||||||
else:
|
|
||||||
hrir = self.room.full_hrir(self.field, state)
|
|
||||||
self._set_filter(source, hrir * gain, first=first, fade=fade)
|
|
||||||
self.distance_states[source] = state
|
|
||||||
if self.room is not None:
|
|
||||||
self.room.set_source(source, state, gain, first=first)
|
|
||||||
self._last[source] = key
|
|
||||||
self.parameter_updates += 1
|
|
||||||
|
|
||||||
def _set_filter(self, source, hrir, *, first, fade):
|
|
||||||
if first:
|
|
||||||
self.convolver.set_filter(source, hrir, fade_blocks=0)
|
|
||||||
else:
|
|
||||||
# Keep parameter changes on the source timeline after removing the common FIR shift.
|
|
||||||
due = self.processed_input_samples + self.field.common_latency_samples
|
|
||||||
update = (due, source, hrir, int(bool(fade)))
|
|
||||||
for index in range(len(self._filter_updates) - 1, -1, -1):
|
|
||||||
item = self._filter_updates[index]
|
|
||||||
if item[0] != due:
|
|
||||||
break
|
|
||||||
if item[1] == source:
|
|
||||||
self._filter_updates[index] = update
|
|
||||||
return
|
|
||||||
self._filter_updates.append(update)
|
|
||||||
|
|
||||||
def process(self, source):
|
|
||||||
if self.finished:
|
|
||||||
raise RuntimeError("renderer is finished")
|
|
||||||
values = np.asarray(source, dtype=np.float64)
|
|
||||||
if (values.ndim != 2 or values.shape[1] != self.source_count or len(values) % 128
|
|
||||||
or not np.isfinite(values).all()):
|
|
||||||
raise ValueError("input must contain finite complete 128-sample source blocks")
|
|
||||||
outputs = []
|
|
||||||
for start in range(0, len(values), 128):
|
|
||||||
block = values[start:start + 128]
|
|
||||||
if self.compiled:
|
|
||||||
hops = block.reshape(2, 64, self.source_count).transpose(0, 2, 1)
|
|
||||||
hybrid = self.bank.hybrid_analysis.process_chunk(self.bank.qmf_analysis.process_chunk(hops))
|
|
||||||
direct = self.paths.process(hybrid)
|
|
||||||
time = self.bank.qmf_synthesis.process_chunk(self.bank.hybrid_synthesis.process_chunk(direct))
|
|
||||||
output = time.transpose(0, 2, 1).reshape(-1, 2)
|
|
||||||
else:
|
|
||||||
while self._filter_updates and self._filter_updates[0][0] <= self.processed_input_samples:
|
|
||||||
_, source, hrir, fade_blocks = self._filter_updates.popleft()
|
|
||||||
self.convolver.set_filter(source, hrir, fade_blocks=fade_blocks)
|
|
||||||
output = self.convolver.process(block)
|
|
||||||
self.processed_input_samples += 128
|
|
||||||
if self.room is not None:
|
|
||||||
output = self.room.process(block, output)
|
|
||||||
if len(output) == 0:
|
|
||||||
continue
|
|
||||||
skip = min(len(output), self._latency_remaining)
|
|
||||||
self._latency_remaining -= skip
|
|
||||||
outputs.append(output[skip:] * self.output_gain)
|
|
||||||
result = np.concatenate(outputs) if outputs else np.empty((0, 2))
|
|
||||||
self.output_samples += len(result)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def finish_output_capacity(self, tail_seconds):
|
|
||||||
tail = float(tail_seconds)
|
|
||||||
if not math.isfinite(tail) or tail < 0:
|
|
||||||
raise ValueError("tail must be finite and nonnegative")
|
|
||||||
return int(math.ceil((tail * 48000 + self.latency_samples + self.filter_taps) / 128) * 128)
|
|
||||||
|
|
||||||
def finish(self, *, tail_seconds=5.):
|
|
||||||
if self.finished:
|
|
||||||
return np.empty((0, 2))
|
|
||||||
count = self.finish_output_capacity(tail_seconds)
|
|
||||||
expected = max(0, self.processed_input_samples + count - self.latency_samples - self.output_samples)
|
|
||||||
if self.room is not None:
|
|
||||||
pending = len(self.room.pending_direct) * 128
|
|
||||||
count = int(math.ceil((count + pending) / self.room.batch_samples) * self.room.batch_samples) - pending
|
|
||||||
output = self.process(np.zeros((count, self.source_count)))
|
|
||||||
self.output_samples -= max(0, len(output) - expected)
|
|
||||||
self.finished = True
|
|
||||||
return output[:expected]
|
|
||||||
|
|
||||||
def info(self):
|
|
||||||
return dict(hrtf_input_kind=self.hrtf_input_kind, hrtf_input_path=self.hrtf_input_path,
|
|
||||||
cache_policy=None, field=self.field.info(), reference=self.reference.info(),
|
|
||||||
level_calibration=1., latency_compensated_samples=self.latency_samples,
|
|
||||||
processing_batch_samples=128 if self.room is None else self.room.batch_samples,
|
|
||||||
recording_origin_compensated_samples=0,
|
|
||||||
recording_origin_preserved_samples=self.origin,
|
|
||||||
parameter_alignment_delay_samples=0 if self.compiled else self.field.common_latency_samples,
|
|
||||||
current_reference_distances_m=[s.distance_m for s in self.distance_states if s is not None],
|
|
||||||
parameter_updates=self.parameter_updates,
|
|
||||||
direct_representation="77-band" if self.compiled else "full-HRIR",
|
|
||||||
radial_model="none", room_processing=self.room is not None,
|
|
||||||
room={"enabled": False} if self.room is None else self.room.info(),
|
|
||||||
response_kind=self.response_kind, precision="float64/complex128")
|
|
||||||
@@ -1,161 +0,0 @@
|
|||||||
"""Local spherical interpolation of full complex HRIR spectra."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from collections import OrderedDict
|
|
||||||
import math
|
|
||||||
import numpy as np
|
|
||||||
from scipy.interpolate import PchipInterpolator
|
|
||||||
from scipy.spatial import ConvexHull, cKDTree
|
|
||||||
|
|
||||||
from sofa_canonical import CanonicalHrtf, load_simple_free_field_hrir
|
|
||||||
|
|
||||||
|
|
||||||
class FullHrirField:
|
|
||||||
response_kind = "hrir"
|
|
||||||
|
|
||||||
def __init__(self, source, *, method="lf_logmag_phase", shell_radius_m=None,
|
|
||||||
fft_size=None):
|
|
||||||
if method not in ("lf_logmag_phase", "lf_mca_zero"):
|
|
||||||
raise ValueError("unsupported full HRIR interpolation method")
|
|
||||||
self.canonical = (source if isinstance(source, CanonicalHrtf) else
|
|
||||||
load_simple_free_field_hrir(source, target_sample_rate_hz=48000))
|
|
||||||
c = self.canonical
|
|
||||||
if c.sample_rate_hz != 48000:
|
|
||||||
raise ValueError("full HRIR renderer requires 48000 Hz")
|
|
||||||
shells = c.shells_m
|
|
||||||
if shell_radius_m is not None and (not math.isfinite(shell_radius_m) or shell_radius_m <= 0):
|
|
||||||
raise ValueError("shell radius must be finite and positive")
|
|
||||||
if shell_radius_m is None and len(shells) != 1:
|
|
||||||
raise ValueError("multi-radius SOFA requires an explicit --hrtf-radius-m")
|
|
||||||
selected = c.shell_indices(shells[0] if shell_radius_m is None else shell_radius_m)
|
|
||||||
if np.any(np.sum(c.hrir[selected]**2, axis=-1) <= 0):
|
|
||||||
raise ValueError("each selected HRIR ear must contain signal energy")
|
|
||||||
self.measurement_radius_m = float(np.median(c.measurement_radius_m[selected]))
|
|
||||||
support = c.taps + int(math.ceil(float(np.max(c.delay_samples[selected])))) + 1
|
|
||||||
if fft_size is None:
|
|
||||||
fft_size = max(2048, 1 << (2*support-1).bit_length())
|
|
||||||
if fft_size > 16384:
|
|
||||||
raise ValueError("HRIR and Data.Delay exceed the automatic FFT budget")
|
|
||||||
self.method, self.fft_size = method, int(fft_size)
|
|
||||||
if self.fft_size < 64 or self.fft_size % 2 or c.taps + np.max(c.delay_samples[selected]) >= self.fft_size//2:
|
|
||||||
raise ValueError("HRIR and Data.Delay exceed the selected FFT time budget")
|
|
||||||
receiver = c.receiver_position_cartesian_m[selected]
|
|
||||||
if not np.allclose(receiver, receiver[0], atol=1e-9, rtol=0):
|
|
||||||
raise ValueError("moving receiver geometry is unsupported")
|
|
||||||
self.receivers = receiver[0].copy()
|
|
||||||
unit = c.unit_directions[selected]
|
|
||||||
_, first, inverse = np.unique(np.round(unit, 10), axis=0, return_index=True, return_inverse=True)
|
|
||||||
for group in np.flatnonzero(np.bincount(inverse) > 1):
|
|
||||||
members = selected[inverse == group]
|
|
||||||
if not (np.allclose(c.hrir[members], c.hrir[members[0]], atol=1e-10, rtol=1e-8)
|
|
||||||
and np.allclose(c.delay_samples[members], c.delay_samples[members[0]], atol=1e-10, rtol=0)):
|
|
||||||
raise ValueError("coincident SOFA measurements disagree")
|
|
||||||
selected = selected[np.sort(first)]
|
|
||||||
self.directions = c.unit_directions[selected].copy()
|
|
||||||
self.tree = cKDTree(self.directions)
|
|
||||||
hull = ConvexHull(self.directions)
|
|
||||||
if np.any(hull.equations[:, 3] >= -1e-9):
|
|
||||||
raise ValueError("SOFA must enclose the listener; extrapolation is unsupported")
|
|
||||||
self.faces = hull.simplices
|
|
||||||
vertices = self.directions[self.faces]
|
|
||||||
dots = np.einsum("fij,fkj->fik", vertices, vertices)
|
|
||||||
self.max_triangle_edge_degrees = math.degrees(math.acos(float(np.clip(dots.min(), -1, 1))))
|
|
||||||
matrix = self.directions[self.faces].transpose(0, 2, 1)
|
|
||||||
if np.max(np.linalg.cond(matrix)) > 1e8:
|
|
||||||
raise ValueError("ill-conditioned spherical interpolation mesh")
|
|
||||||
self.inverse = np.linalg.inv(matrix)
|
|
||||||
self.frequencies = np.fft.rfftfreq(self.fft_size, 1/c.sample_rate_hz)
|
|
||||||
self.omega = 2*np.pi*self.frequencies/c.sample_rate_hz
|
|
||||||
raw = np.fft.rfft(c.hrir[selected], self.fft_size)
|
|
||||||
fit = (self.frequencies >= 200) & (self.frequencies <= 1500)
|
|
||||||
x = self.omega[fit]-self.omega[fit].mean()
|
|
||||||
phase = np.unwrap(np.angle(raw), axis=-1)[..., fit]
|
|
||||||
self.delays = -np.sum(x*phase, axis=-1)/np.sum(x*x) + c.delay_samples[selected]
|
|
||||||
residual_phase = phase+(self.delays-c.delay_samples[selected])[..., None]*self.omega[fit]
|
|
||||||
residual_phase -= residual_phase.mean(axis=-1, keepdims=True)
|
|
||||||
self.phase_fit_rms_rad = np.sqrt(np.mean(residual_phase**2, axis=-1))
|
|
||||||
self.recording_origin_samples = int(round(float(np.median(self.delays.mean(axis=1)))))
|
|
||||||
self.common_latency_samples = self.fft_size//2
|
|
||||||
physical = raw*np.exp(-1j*self.omega*c.delay_samples[selected, :, None])
|
|
||||||
self.floor = max(float(np.max(np.abs(physical)))*1e-6, 1e-15)
|
|
||||||
self.aligned = physical*np.exp(1j*self.omega*self.delays[..., None])
|
|
||||||
self.log_magnitude = None
|
|
||||||
self.auditory = None
|
|
||||||
if method == "lf_logmag_phase":
|
|
||||||
self.log_magnitude = np.log(np.maximum(np.abs(physical), self.floor))
|
|
||||||
else:
|
|
||||||
erb = lambda f: 21.4*np.log10(1+.00437*f)
|
|
||||||
self.centres = (10**(np.linspace(erb(50), erb(20000), 41)/21.4)-1)/.00437
|
|
||||||
bw = 1.019*24.7*(1+.00437*self.centres)
|
|
||||||
self.bank = (1+((self.frequencies[None, :]-self.centres[:, None])/bw[:, None])**2)**-4
|
|
||||||
self.bank[:, 1:-1] *= 2
|
|
||||||
self.bank /= self.bank.sum(axis=1, keepdims=True)
|
|
||||||
self.auditory = self._auditory_db(physical)
|
|
||||||
self._cache = OrderedDict()
|
|
||||||
|
|
||||||
def _auditory_db(self, spectrum):
|
|
||||||
return 10*np.log10(np.maximum(np.abs(spectrum)**2 @ self.bank.T, 1e-24))
|
|
||||||
|
|
||||||
def weights(self, direction):
|
|
||||||
q = np.asarray(direction, dtype=np.float64)
|
|
||||||
if q.shape != (3,) or not np.isfinite(q).all() or np.linalg.norm(q) <= 1e-12:
|
|
||||||
raise ValueError("query direction must be a finite nonzero vector")
|
|
||||||
q = q/np.linalg.norm(q)
|
|
||||||
distance, nearest = self.tree.query(q)
|
|
||||||
if distance < 1e-10:
|
|
||||||
return np.array([nearest]), np.array([1.]), 0.
|
|
||||||
g = self.inverse @ q
|
|
||||||
valid = np.flatnonzero(np.min(g, axis=1) >= -1e-10)
|
|
||||||
if not len(valid):
|
|
||||||
raise ValueError("no containing spherical triangle")
|
|
||||||
face = int(valid[0])
|
|
||||||
weights = np.maximum(g[face], 0)
|
|
||||||
weights /= weights.sum()
|
|
||||||
ids = self.faces[face]
|
|
||||||
vertices = self.directions[ids]
|
|
||||||
edge = math.degrees(math.acos(float(np.clip(np.min(vertices @ vertices.T), -1, 1))))
|
|
||||||
return ids, weights, edge
|
|
||||||
|
|
||||||
def spectrum(self, direction):
|
|
||||||
q = np.asarray(direction, dtype=np.float64)
|
|
||||||
if q.shape != (3,) or not np.isfinite(q).all() or np.linalg.norm(q) <= 1e-12:
|
|
||||||
raise ValueError("query direction must be a finite nonzero vector")
|
|
||||||
q = q/np.linalg.norm(q)
|
|
||||||
key = tuple(q)
|
|
||||||
if key in self._cache:
|
|
||||||
self._cache.move_to_end(key)
|
|
||||||
return self._cache[key].copy()
|
|
||||||
ids, weights, _ = self.weights(q)
|
|
||||||
delay = weights @ self.delays[ids]
|
|
||||||
spectrum = np.einsum("i,ief->ef", weights, self.aligned[ids])
|
|
||||||
spectrum *= np.exp(-1j*self.omega*delay[:, None])
|
|
||||||
if len(ids) > 1:
|
|
||||||
if self.method == "lf_logmag_phase":
|
|
||||||
magnitude = np.exp(np.einsum("i,ief->ef", weights, self.log_magnitude[ids]))
|
|
||||||
spectrum = magnitude*spectrum/np.maximum(np.abs(spectrum), self.floor)
|
|
||||||
else:
|
|
||||||
target = np.einsum("i,ieb->eb", weights, self.auditory[ids])
|
|
||||||
difference = target-self._auditory_db(spectrum)
|
|
||||||
correction = PchipInterpolator(self.centres, difference, axis=-1)(
|
|
||||||
np.clip(self.frequencies, self.centres[0], self.centres[-1]))
|
|
||||||
spectrum *= 10**(np.clip(correction, -12, 12)/20)
|
|
||||||
self._cache[key] = spectrum.copy()
|
|
||||||
if len(self._cache) > 128:
|
|
||||||
self._cache.popitem(last=False)
|
|
||||||
return spectrum
|
|
||||||
|
|
||||||
def info(self):
|
|
||||||
arrays = [self.aligned, self.log_magnitude, self.auditory, self.delays,
|
|
||||||
self.inverse, self.directions, self.faces]
|
|
||||||
return dict(source_sha256=self.canonical.source_sha256,
|
|
||||||
sample_rate_hz=self.canonical.sample_rate_hz,
|
|
||||||
convention=self.canonical.convention,
|
|
||||||
measurement_radius_m=self.measurement_radius_m,
|
|
||||||
method=self.method, fft_size=self.fft_size,
|
|
||||||
retained_filter_taps=self.fft_size,
|
|
||||||
recording_origin_samples=self.recording_origin_samples,
|
|
||||||
interpolation_storage_bytes=sum(x.nbytes for x in arrays if x is not None),
|
|
||||||
max_triangle_edge_degrees=self.max_triangle_edge_degrees,
|
|
||||||
phase_fit_rms_rad_p95=float(np.percentile(self.phase_fit_rms_rad, 95)),
|
|
||||||
ear_model="measured-SOFA-receivers",
|
|
||||||
receiver_positions_m=self.receivers.tolist())
|
|
||||||
@@ -29,11 +29,6 @@ from public_filterbank import (
|
|||||||
hybrid_band_center_frequencies_hz,
|
hybrid_band_center_frequencies_hz,
|
||||||
project_hrir_to_hybrid_gains,
|
project_hrir_to_hybrid_gains,
|
||||||
)
|
)
|
||||||
from sofa_canonical import (
|
|
||||||
CanonicalHrtf,
|
|
||||||
load_simple_free_field_hrir,
|
|
||||||
time_align_hrtf,
|
|
||||||
)
|
|
||||||
from spherical_harmonics import (
|
from spherical_harmonics import (
|
||||||
evaluate_real_spherical_harmonics,
|
evaluate_real_spherical_harmonics,
|
||||||
fit_real_spherical_harmonics,
|
fit_real_spherical_harmonics,
|
||||||
@@ -118,22 +113,6 @@ def sofa_to_adm_direction(position) -> np.ndarray:
|
|||||||
return result / length
|
return result / length
|
||||||
|
|
||||||
|
|
||||||
def _group_coincident(directions, gains, delays):
|
|
||||||
unit = np.asarray(directions, dtype=np.float64)
|
|
||||||
keys = np.round(unit, 10)
|
|
||||||
_, first, inverse = np.unique(keys, axis=0, return_index=True, return_inverse=True)
|
|
||||||
grouped_directions = unit[first]
|
|
||||||
grouped_gains = np.zeros((len(first),) + gains.shape[1:], dtype=np.complex128)
|
|
||||||
grouped_delays = np.zeros((len(first), 2), dtype=np.float64)
|
|
||||||
counts = np.bincount(inverse).astype(np.float64)
|
|
||||||
for measurement, group in enumerate(inverse):
|
|
||||||
grouped_gains[group] += gains[measurement]
|
|
||||||
grouped_delays[group] += delays[measurement]
|
|
||||||
grouped_gains /= counts[:, None, None]
|
|
||||||
grouped_delays /= counts[:, None]
|
|
||||||
return grouped_directions, grouped_gains, grouped_delays
|
|
||||||
|
|
||||||
|
|
||||||
def _cache_key_payload(*, source_sha256: str, sample_rate_hz: float,
|
def _cache_key_payload(*, source_sha256: str, sample_rate_hz: float,
|
||||||
shell_radius_m: float, order: int,
|
shell_radius_m: float, order: int,
|
||||||
projection_ridge: float, sh_ridge: float) -> dict:
|
projection_ridge: float, sh_ridge: float) -> dict:
|
||||||
@@ -556,109 +535,6 @@ class SofaHrtfField:
|
|||||||
object.__setattr__(self, "fit_report", json.loads(
|
object.__setattr__(self, "fit_report", json.loads(
|
||||||
_json_bytes(dict(self.fit_report)).decode("utf-8")))
|
_json_bytes(dict(self.fit_report)).decode("utf-8")))
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def fit(cls, canonical: CanonicalHrtf, *, shell_radius_m: float | None = None,
|
|
||||||
order: int = DEFAULT_ORDER, ridge: float = DEFAULT_SH_RIDGE,
|
|
||||||
projection_ridge: float = DEFAULT_PROJECTION_RIDGE) -> "SofaHrtfField":
|
|
||||||
order = _strict_integer(order, "field order")
|
|
||||||
if order != DEFAULT_ORDER:
|
|
||||||
raise ValueError("the runtime HRTF field is fixed at fifth order")
|
|
||||||
if abs(float(canonical.sample_rate_hz) - TARGET_SAMPLE_RATE_HZ) > 1.0e-9:
|
|
||||||
raise ValueError("SOFA HRTF fields must be compiled at 48 kHz")
|
|
||||||
aligned = time_align_hrtf(canonical)
|
|
||||||
shell_target = 1.0 if shell_radius_m is None else float(shell_radius_m)
|
|
||||||
if not math.isfinite(shell_target) or shell_target <= 0.0:
|
|
||||||
raise ValueError("shell_radius_m must be positive and finite")
|
|
||||||
indices = canonical.shell_indices(shell_target)
|
|
||||||
if len(indices) < (order + 1) ** 2:
|
|
||||||
raise ValueError(
|
|
||||||
f"fifth-order SH needs at least 36 measurements on one shell, got {len(indices)}")
|
|
||||||
shell_radius = float(np.mean(canonical.measurement_radius_m[indices]))
|
|
||||||
directions = canonical.unit_directions[indices]
|
|
||||||
runtime_delay = np.asarray(aligned.runtime_delay_samples[indices], dtype=np.float64)
|
|
||||||
centers = hybrid_band_center_frequencies_hz(canonical.sample_rate_hz)
|
|
||||||
gains, projection_report = project_hrir_to_hybrid_gains(
|
|
||||||
np.asarray(canonical.hrir[indices], dtype=np.float64),
|
|
||||||
embedded_delay_samples=np.asarray(
|
|
||||||
aligned.embedded_delay_removed_samples[indices], dtype=np.float64),
|
|
||||||
sample_rate_hz=canonical.sample_rate_hz,
|
|
||||||
ridge=projection_ridge)
|
|
||||||
|
|
||||||
directions, gains, runtime_delay = _group_coincident(
|
|
||||||
directions, gains, runtime_delay)
|
|
||||||
if len(directions) < (order + 1) ** 2:
|
|
||||||
raise ValueError("coincident-direction merging left fewer than 36 directions")
|
|
||||||
weights = spherical_voronoi_weights(directions)
|
|
||||||
coefficients = fit_real_spherical_harmonics(
|
|
||||||
directions, gains, order=order, ridge=ridge, weights=weights)
|
|
||||||
delay_coefficients = fit_real_spherical_harmonics(
|
|
||||||
directions, runtime_delay, order=order, ridge=ridge, weights=weights)
|
|
||||||
reconstructed_aligned = evaluate_real_spherical_harmonics(
|
|
||||||
coefficients, directions, order=order)
|
|
||||||
delay_reconstructed = evaluate_real_spherical_harmonics(
|
|
||||||
delay_coefficients, directions, order=order)
|
|
||||||
reference_phase = np.exp(
|
|
||||||
-2j * np.pi * runtime_delay[..., None]
|
|
||||||
* centers[None, None, :] / canonical.sample_rate_hz)
|
|
||||||
reconstructed_phase = np.exp(
|
|
||||||
-2j * np.pi * delay_reconstructed[..., None]
|
|
||||||
* centers[None, None, :] / canonical.sample_rate_hz)
|
|
||||||
reference_transfer = np.asarray(gains * reference_phase, dtype=np.complex128)
|
|
||||||
reconstructed_transfer = np.asarray(
|
|
||||||
reconstructed_aligned * reconstructed_phase, dtype=np.complex128)
|
|
||||||
magnitude_reference = np.maximum(np.abs(reference_transfer), 1.0e-12)
|
|
||||||
relative = np.abs(reconstructed_transfer - reference_transfer) / magnitude_reference
|
|
||||||
magnitude_db_error = np.abs(
|
|
||||||
20.0 * np.log10(np.maximum(np.abs(reconstructed_transfer), 1.0e-12))
|
|
||||||
- 20.0 * np.log10(magnitude_reference))
|
|
||||||
delay_error = delay_reconstructed - runtime_delay
|
|
||||||
report = {
|
|
||||||
"format": "SOFA FIR -> public 64-QMF/77-hybrid -> ACN/N3D real SH",
|
|
||||||
"order": order,
|
|
||||||
"terms": (order + 1) ** 2,
|
|
||||||
"input_measurements": int(len(indices)),
|
|
||||||
"unique_directions": int(len(directions)),
|
|
||||||
"shell_radius_m": shell_radius,
|
|
||||||
"spherical_harmonic_ridge": float(ridge),
|
|
||||||
"projection": projection_report,
|
|
||||||
"phase_policy_version": PHASE_POLICY_VERSION,
|
|
||||||
"complex_relative_error_median": float(np.median(relative)),
|
|
||||||
"complex_relative_error_p95": float(np.percentile(relative, 95.0)),
|
|
||||||
"magnitude_error_db_median": float(np.median(magnitude_db_error)),
|
|
||||||
"magnitude_error_db_p95": float(np.percentile(magnitude_db_error, 95.0)),
|
|
||||||
"delay_error_samples_rms": float(np.sqrt(np.mean(delay_error * delay_error))),
|
|
||||||
"delay_error_samples_max": float(np.max(np.abs(delay_error))),
|
|
||||||
"delay_source": aligned.delay_source,
|
|
||||||
"precision": "float64/complex128",
|
|
||||||
}
|
|
||||||
delay_bounds = np.stack(
|
|
||||||
(np.min(runtime_delay, axis=0), np.max(runtime_delay, axis=0)), axis=1)
|
|
||||||
cache_key = compiled_hrtf_cache_key(
|
|
||||||
source_sha256=canonical.source_sha256,
|
|
||||||
sample_rate_hz=canonical.sample_rate_hz,
|
|
||||||
shell_radius_m=shell_radius,
|
|
||||||
order=order,
|
|
||||||
projection_ridge=float(projection_ridge),
|
|
||||||
sh_ridge=float(ridge),
|
|
||||||
)
|
|
||||||
display_name = Path(canonical.source_path).name if canonical.source_path else None
|
|
||||||
return cls(
|
|
||||||
source_sha256=canonical.source_sha256,
|
|
||||||
source_display_name=display_name,
|
|
||||||
sample_rate_hz=canonical.sample_rate_hz,
|
|
||||||
measurement_radius_m=shell_radius,
|
|
||||||
order=order,
|
|
||||||
projection_ridge=float(projection_ridge),
|
|
||||||
spherical_harmonic_ridge=float(ridge),
|
|
||||||
band_center_frequencies_hz=centers,
|
|
||||||
coefficients=coefficients,
|
|
||||||
delay_coefficients=delay_coefficients,
|
|
||||||
delay_bounds=delay_bounds,
|
|
||||||
delay_source=aligned.delay_source,
|
|
||||||
fit_report=report,
|
|
||||||
cache_key=cache_key,
|
|
||||||
)
|
|
||||||
|
|
||||||
def evaluate_sofa(self, direction_sofa) -> FieldEvaluation:
|
def evaluate_sofa(self, direction_sofa) -> FieldEvaluation:
|
||||||
direction = np.asarray(direction_sofa, dtype=np.float64)
|
direction = np.asarray(direction_sofa, dtype=np.float64)
|
||||||
if direction.shape != (3,) or not np.isfinite(direction).all():
|
if direction.shape != (3,) or not np.isfinite(direction).all():
|
||||||
@@ -928,93 +804,3 @@ class SofaHrtfField:
|
|||||||
"fit_report": self.fit_report,
|
"fit_report": self.fit_report,
|
||||||
"precision": "float64/complex128",
|
"precision": "float64/complex128",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _cache_file_name(display_name: str | None, cache_key: str) -> str:
|
|
||||||
stem = Path(display_name).stem if display_name else "hrtf"
|
|
||||||
safe = re.sub(r"[^A-Za-z0-9._-]+", "_", stem).strip("._") or "hrtf"
|
|
||||||
return f"{safe}.{cache_key[:20]}.jochrtf"
|
|
||||||
|
|
||||||
|
|
||||||
def compile_sofa_hrtf(
|
|
||||||
sofa: str | Path | CanonicalHrtf, *,
|
|
||||||
target_sample_rate_hz: float = TARGET_SAMPLE_RATE_HZ,
|
|
||||||
shell_radius_m: float = 1.0,
|
|
||||||
order: int = DEFAULT_ORDER,
|
|
||||||
projection_ridge: float = DEFAULT_PROJECTION_RIDGE,
|
|
||||||
sh_ridge: float = DEFAULT_SH_RIDGE,
|
|
||||||
cache_policy: str = "memory",
|
|
||||||
cache_dir: str | Path | None = None) -> SofaHrtfField:
|
|
||||||
"""Compile SOFA in memory, optionally using a validated disposable cache."""
|
|
||||||
policy = str(cache_policy).strip().lower()
|
|
||||||
if policy not in {"none", "memory", "disk"}:
|
|
||||||
raise ValueError("cache_policy must be none, memory, or disk")
|
|
||||||
target_rate = float(target_sample_rate_hz)
|
|
||||||
if (not math.isfinite(target_rate)
|
|
||||||
or abs(target_rate - TARGET_SAMPLE_RATE_HZ) > 1.0e-9):
|
|
||||||
raise ValueError("the public binaural runtime currently requires 48 kHz")
|
|
||||||
radius = float(shell_radius_m)
|
|
||||||
projection_ridge = float(projection_ridge)
|
|
||||||
sh_ridge = float(sh_ridge)
|
|
||||||
if not math.isfinite(radius) or radius <= 0.0:
|
|
||||||
raise ValueError("shell_radius_m must be positive and finite")
|
|
||||||
if (not math.isfinite(projection_ridge) or projection_ridge < 0.0
|
|
||||||
or not math.isfinite(sh_ridge) or sh_ridge < 0.0):
|
|
||||||
raise ValueError("compiler ridge values must be finite and non-negative")
|
|
||||||
order = _strict_integer(order, "compiler order")
|
|
||||||
if order != DEFAULT_ORDER:
|
|
||||||
raise ValueError("the runtime HRTF field is fixed at fifth order")
|
|
||||||
if isinstance(sofa, CanonicalHrtf):
|
|
||||||
canonical = (sofa if abs(sofa.sample_rate_hz - target_rate) <= 1.0e-9
|
|
||||||
else sofa.resampled(target_rate))
|
|
||||||
else:
|
|
||||||
canonical = load_simple_free_field_hrir(
|
|
||||||
sofa, target_sample_rate_hz=target_rate)
|
|
||||||
selected = canonical.shell_indices(radius)
|
|
||||||
actual_radius = float(np.mean(canonical.measurement_radius_m[selected]))
|
|
||||||
key = compiled_hrtf_cache_key(
|
|
||||||
source_sha256=canonical.source_sha256,
|
|
||||||
sample_rate_hz=target_rate,
|
|
||||||
shell_radius_m=actual_radius,
|
|
||||||
order=order,
|
|
||||||
projection_ridge=projection_ridge,
|
|
||||||
sh_ridge=sh_ridge,
|
|
||||||
)
|
|
||||||
cached = None
|
|
||||||
if policy in {"memory", "disk"}:
|
|
||||||
cached = _memory_cache_get(key)
|
|
||||||
if policy == "memory" and cached is not None:
|
|
||||||
return cached
|
|
||||||
target: Path | None = None
|
|
||||||
if policy == "disk":
|
|
||||||
directory = (DEFAULT_HRTF_CACHE_DIR if cache_dir is None
|
|
||||||
else Path(cache_dir).expanduser().resolve())
|
|
||||||
directory.mkdir(parents=True, exist_ok=True)
|
|
||||||
display_name = Path(canonical.source_path).name if canonical.source_path else None
|
|
||||||
target = directory / _cache_file_name(display_name, key)
|
|
||||||
if target.is_file():
|
|
||||||
try:
|
|
||||||
field = SofaHrtfField.load(
|
|
||||||
target,
|
|
||||||
expected_source_sha256=canonical.source_sha256,
|
|
||||||
expected_cache_key=key)
|
|
||||||
_memory_cache_put(key, field)
|
|
||||||
return field
|
|
||||||
except (HrtfCacheError, OSError):
|
|
||||||
pass
|
|
||||||
if cached is not None:
|
|
||||||
cached.save(target)
|
|
||||||
return cached
|
|
||||||
field = SofaHrtfField.fit(
|
|
||||||
canonical,
|
|
||||||
shell_radius_m=actual_radius,
|
|
||||||
order=order,
|
|
||||||
ridge=sh_ridge,
|
|
||||||
projection_ridge=projection_ridge)
|
|
||||||
if field.cache_key != key:
|
|
||||||
raise RuntimeError("internal compiled HRTF cache-key mismatch")
|
|
||||||
if target is not None:
|
|
||||||
field.save(target)
|
|
||||||
if policy in {"memory", "disk"}:
|
|
||||||
_memory_cache_put(key, field)
|
|
||||||
return field
|
|
||||||
@@ -1,123 +0,0 @@
|
|||||||
"""Full-resolution coupling to the existing project-owned early/late room."""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from dataclasses import asdict
|
|
||||||
import hashlib
|
|
||||||
import json
|
|
||||||
import math
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
from scipy import fft
|
|
||||||
|
|
||||||
from generate_sofa_room import SCHEMA, ROOM_PATHS
|
|
||||||
from partitioned_convolution import UniformPartitionedConvolver
|
|
||||||
from public_room import ShoeboxRoomConfig, first_order_image_sources
|
|
||||||
from reference_distance import DistanceState, ReferenceDistanceProfileV1
|
|
||||||
|
|
||||||
|
|
||||||
class SofaRoom:
|
|
||||||
batch_samples = 1536
|
|
||||||
|
|
||||||
def __init__(self, source_count, reference, *, implementation_latency, recording_origin,
|
|
||||||
path=None, preset=None):
|
|
||||||
self.reference = reference
|
|
||||||
self.preset = ("legacy" if reference.mode == "far" else "compact") if preset is None else str(preset)
|
|
||||||
if self.preset not in ROOM_PATHS:
|
|
||||||
raise ValueError("room preset must be compact or legacy")
|
|
||||||
self.path = Path(ROOM_PATHS[self.preset] if path is None else path)
|
|
||||||
with np.load(self.path, allow_pickle=False) as data:
|
|
||||||
if str(data["schema"]) != SCHEMA or int(data["sample_rate_hz"]) != 48000:
|
|
||||||
raise ValueError("unsupported SOFA room asset schema or sample rate")
|
|
||||||
kernel = np.asarray(data["late_fir"], dtype="<f8")
|
|
||||||
self.metadata = json.loads(str(data["metadata_json"]))
|
|
||||||
if (kernel.ndim != 2 or kernel.shape[0] != 2 or kernel.shape[1] < 1
|
|
||||||
or not np.isfinite(kernel).all()
|
|
||||||
or hashlib.sha256(kernel.tobytes()).hexdigest() != self.metadata["kernel_sha256"]):
|
|
||||||
raise ValueError("invalid SOFA room FIR or checksum")
|
|
||||||
self.config = ShoeboxRoomConfig(**self.metadata["early_config"])
|
|
||||||
self.config.validate()
|
|
||||||
self.alignment_samples = int(implementation_latency) + max(0, int(recording_origin))
|
|
||||||
kernel = np.pad(kernel, ((0, 0), (self.alignment_samples, 0)))
|
|
||||||
self.convolver = UniformPartitionedConvolver(kernel, block_size=self.batch_samples)
|
|
||||||
self.current = np.zeros(int(source_count))
|
|
||||||
self.target = self.current.copy()
|
|
||||||
self.pending_mono, self.pending_direct = [], []
|
|
||||||
self.maximum_early_delay_samples = 0.
|
|
||||||
|
|
||||||
def distance_state(self, state):
|
|
||||||
return DistanceState(
|
|
||||||
self.reference.mode, state.normalized_radius, self.reference.reference_distance_m,
|
|
||||||
max(ReferenceDistanceProfileV1.MINIMUM_DISTANCE_M, state.distance_m),
|
|
||||||
state.direction[[1, 0, 2]] * [-1, 1, 1])
|
|
||||||
|
|
||||||
def paths(self, state):
|
|
||||||
distance = self.distance_state(state)
|
|
||||||
try:
|
|
||||||
paths = first_order_image_sources(distance.direction_adm, distance.physical_distance_m,
|
|
||||||
48000, self.config)
|
|
||||||
except ValueError as error:
|
|
||||||
raise ValueError("--sofa-room预设房间无法表示当前声源位置;关闭该开关可保留直接声") from error
|
|
||||||
self.maximum_early_delay_samples = max(
|
|
||||||
self.maximum_early_delay_samples, max(p.extra_delay_samples for p in paths))
|
|
||||||
return paths
|
|
||||||
|
|
||||||
def direct_gain(self, state):
|
|
||||||
return ReferenceDistanceProfileV1.direct_level_gain(self.distance_state(state))
|
|
||||||
|
|
||||||
def early_gain(self, path, measurement_radius_m):
|
|
||||||
return (ReferenceDistanceProfileV1.PUBLIC_ROOM_CALIBRATION_GAIN * path.reflection_gain
|
|
||||||
* measurement_radius_m / path.path_distance_m)
|
|
||||||
|
|
||||||
def set_source(self, source, state, gain, *, first=False, special_lfe=False):
|
|
||||||
value = 0. if special_lfe else (float(gain) * ReferenceDistanceProfileV1.PUBLIC_ROOM_CALIBRATION_GAIN
|
|
||||||
* ReferenceDistanceProfileV1.late_send(self.distance_state(state)))
|
|
||||||
self.target[source] = value
|
|
||||||
if first:
|
|
||||||
self.current[source] = value
|
|
||||||
|
|
||||||
def filter_budget(self, field):
|
|
||||||
needed = field.fft_size + int(math.ceil(self.early_delay_bound_samples)) + 64
|
|
||||||
return 1 << (needed - 1).bit_length()
|
|
||||||
|
|
||||||
@property
|
|
||||||
def early_delay_bound_samples(self):
|
|
||||||
dimensions = np.asarray(self.config.dimensions_m)
|
|
||||||
listener = np.asarray(self.config.listener_position_m)
|
|
||||||
return 2 * float(np.max(np.maximum(listener, dimensions - listener))) * 48000 / self.config.speed_of_sound_m_s
|
|
||||||
|
|
||||||
def full_hrir(self, field, state):
|
|
||||||
length = self.filter_budget(field)
|
|
||||||
omega = 2 * math.pi * fft.rfftfreq(length)
|
|
||||||
direct = np.roll(np.fft.irfft(field.spectrum(state.direction), field.fft_size),
|
|
||||||
field.common_latency_samples, axis=-1)
|
|
||||||
spectrum = fft.rfft(direct, length) * self.direct_gain(state)
|
|
||||||
for path in self.paths(state):
|
|
||||||
q = path.direction_adm[[1, 0, 2]] * [1, -1, 1]
|
|
||||||
hrir = np.roll(np.fft.irfft(field.spectrum(q), field.fft_size),
|
|
||||||
field.common_latency_samples, axis=-1)
|
|
||||||
spectrum += (fft.rfft(hrir, length) * np.exp(-1j * omega * path.extra_delay_samples)
|
|
||||||
* self.early_gain(path, field.measurement_radius_m))
|
|
||||||
return fft.irfft(spectrum, length, axis=-1)
|
|
||||||
|
|
||||||
def process(self, source_block, direct_block):
|
|
||||||
amount = np.arange(1, 129, dtype=np.float64)[:, None] / 128
|
|
||||||
sends = self.current + (self.target - self.current) * amount
|
|
||||||
self.pending_mono.append(np.einsum("ts,ts->t", source_block, sends))
|
|
||||||
self.pending_direct.append(direct_block)
|
|
||||||
self.current[:] = self.target
|
|
||||||
if len(self.pending_mono) * 128 < self.batch_samples:
|
|
||||||
return np.empty((0, 2))
|
|
||||||
result = np.concatenate(self.pending_direct) + self.convolver.process_block(np.concatenate(self.pending_mono))
|
|
||||||
self.pending_mono.clear()
|
|
||||||
self.pending_direct.clear()
|
|
||||||
return result
|
|
||||||
|
|
||||||
def info(self):
|
|
||||||
return dict(enabled=True, schema=SCHEMA, preset=self.preset, asset=str(self.path.resolve()),
|
|
||||||
late_fir=self.metadata, early_config=asdict(self.config),
|
|
||||||
maximum_early_delay_samples=self.maximum_early_delay_samples,
|
|
||||||
late_alignment_samples=self.alignment_samples,
|
|
||||||
distance_coupling=ReferenceDistanceProfileV1.info(),
|
|
||||||
high_precision_adaptation="full-HRIR early paths with fractional delay; PCM float64 late FIR",
|
|
||||||
normalization="original project room coupling; not a measured BRIR")
|
|
||||||
Reference in new issue
Block a user