Make SOFA room processing optional and self-contained
SOFA and explicit .jochrtf inputs now render direct sound by default. --sofa-room enables the project image-source/FDN room, with the compact preset for Near/Mid and the original large room for Far. - preserve native HRIR timing and apply Data.Delay once - remove external reference configuration, radial head synthesis and the artificial centre response - ship both generated float64 room assets and their generator - retain .jochrtf as an explicit lightweight input and offline export - keep personalized headphone processing unchanged and update bilingual documentation Checked: independent direct/reflection convolution, FDN FIR/recursive agreement, fractional delay and metadata timing, chunk invariance, 8 bit-exact personalized regressions, CLI smoke tests, and full-song D1/D2 renders of two tracks.
This commit is contained in:
1 parent
26e84569c2
commit
3ea285e407
19 files changed
+861
-1134
No files matched your search
+17
-9
@@ -87,8 +87,9 @@ 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
|
||||
```
|
||||
|
||||
Write binaural stereo directly. SOFA and compact inputs select Near/Mid/Far in an external
|
||||
reference environment, default Mid; personalized rendering retains its existing three modes.
|
||||
Write binaural stereo directly. SOFA and compact inputs render direct HRTFs by default;
|
||||
`--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
|
||||
@@ -105,10 +106,12 @@ python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrt
|
||||
|
||||
# Common options
|
||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa `
|
||||
--sofa-interpolation lf_mca_zero --reference-config C:\config\reference.json
|
||||
--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 --binaural-mode far
|
||||
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
|
||||
@@ -127,10 +130,15 @@ other locations. Compact input is always explicit, with no automatic export or f
|
||||
remain available. High-precision rendering reads SOFA directly.
|
||||
|
||||
The personalized default remains `HRTF/binaural.personalized_headphone`.
|
||||
SOFA and compact routes also require an external reference configuration, read
|
||||
from local `local/binaural_reference.json`, `--reference-config`, or
|
||||
`BINAURAL_REFERENCE_CONFIG`. Environment data is not supplied with the source;
|
||||
see the configuration contract in [Binaural rendering](docs/binaural.en.md).
|
||||
SOFA and compact routes retain Near/Mid/Far point-source scales of approximately
|
||||
1, 2.193 and 6.402 m, default Mid. Direct-only rendering does not alter the measured
|
||||
radius, HRIR, programme gain or flight time. `--sofa-room` enables the original project
|
||||
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.
|
||||
See [Binaural rendering](docs/binaural.en.md).
|
||||
Because the cache contains transformed
|
||||
HRTF data, its use and redistribution remain subject to the source dataset's
|
||||
terms. See [Binaural Rendering](docs/binaural.en.md) and
|
||||
@@ -315,7 +323,7 @@ The main documented stages are:
|
||||
- equal-power panning over target-layout regions;
|
||||
- layout-dependent position compensation and sample-wise gain ramps;
|
||||
- float32 and PCM24 output quantization;
|
||||
- SOFA canonical import, 64-QMF/77-hybrid projection, `36×2×77` fifth-order fields, exactly-once delay/phase, project early/late room behavior, and special LFE.
|
||||
- SOFA canonical import, full-HRIR directional interpolation, explicit 64-QMF/77-hybrid compact export, `36×2×77` fifth-order fields, exactly-once delay/phase, and special LFE.
|
||||
|
||||
See the [mathematical notes](docs/math.en.md) for the equations used by the decoding and rendering process.
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ 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
|
||||
```
|
||||
|
||||
直接输出双耳渲染 WAV。SOFA与压缩输入使用外部参考环境的 Near/Mid/Far,默认 Mid;个性化路线保持原三档行为。HRTF 输入支持三种来源:
|
||||
直接输出双耳渲染 WAV。SOFA与压缩输入默认仅渲染所选HRTF的直达声,可用`--sofa-room`启用项目自有房间;不做球头径向合成。个性化路线保持原Near/Mid/Far及房间处理。HRTF 输入支持三种来源:
|
||||
|
||||
```powershell
|
||||
# 1) 完整SOFA HRIR(默认 HRTF/binaural.sofa,可显式指定)
|
||||
@@ -101,10 +101,12 @@ python main.py input.m4a --binaural --compiled-hrtf-cache C:\HRTF\subject.jochrt
|
||||
|
||||
# 常用选项
|
||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa `
|
||||
--sofa-interpolation lf_mca_zero --reference-config C:\config\reference.json
|
||||
--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 --binaural-mode far
|
||||
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
|
||||
@@ -119,9 +121,9 @@ python main.py input.m4a --binaural --binaural-output output.binaural.wav
|
||||
本项目自行实现(`src/rosella_model.py`),不调用杜比软件。
|
||||
- `.jochrtf` 是从SOFA显式离线导出的轻量方向场,原格式及读写器保留;高精度路线直接读取SOFA。
|
||||
|
||||
个性化默认模型仍为 `HRTF/binaural.personalized_headphone`。SOFA与压缩路线还需要外部参考环境配置,
|
||||
默认读取本地 `local/binaural_reference.json`,或用 `--reference-config` / `BINAURAL_REFERENCE_CONFIG` 指定。
|
||||
具体环境数据不随源码提供,配置格式见[双耳渲染](docs/binaural.md)。cache 含有源 HRTF 的变换数据,使用与再分发
|
||||
个性化默认模型仍为 `HRTF/binaural.personalized_headphone`。SOFA与压缩路线沿用Near约1 m、Mid约2.193 m、Far约6.402 m的点声源参考尺度,默认Mid。
|
||||
默认不改变测量半径、HRIR、节目增益或传播延迟;开启`--sofa-room`后,使用原项目镜像早反射及FDN房间耦合。Near/Mid默认采用紧凑房间,Far默认采用原大房间;`--sofa-room-preset compact|legacy`可显式选择。两套晚期FIR均随代码提供。
|
||||
当前支持自由场HRIR SOFA,BRIR尚未接入;后续BRIR会直接使用文件内房间响应并忽略额外房间开关。细节见[双耳渲染](docs/binaural.md)。cache 含有源 HRTF 的变换数据,使用与再分发
|
||||
仍受源数据许可约束;格式边界、计算公式、状态、时间轴及发布注意事项见
|
||||
[双耳渲染](docs/binaural.md) 和 [第三方通知](THIRD_PARTY_NOTICES.md)。
|
||||
|
||||
@@ -273,7 +275,7 @@ JustOneCacophony/
|
||||
- 基于目标布局 region 的等功率声像;
|
||||
- 布局位置补偿与逐样本增益斜坡;
|
||||
- float32 与 PCM24 输出量化;
|
||||
- SOFA canonical importer、64-QMF/77-hybrid 投影、`36×2×77` 五阶方向 field、exactly-once delay/phase、项目 early/late room 与 special LFE。
|
||||
- SOFA canonical importer、完整HRIR方向插值、显式64-QMF/77-hybrid压缩导出、`36×2×77`五阶方向field、exactly-once delay/phase与special LFE。
|
||||
|
||||
解码与渲染过程使用的公式见[数学说明](docs/math.md)。
|
||||
|
||||
|
||||
+22
-2
@@ -4,6 +4,26 @@
|
||||
|
||||
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:
|
||||
|
||||
```text
|
||||
@@ -21,8 +41,8 @@ joc_huff_code_7ch_pos_index_sparse int64[6,2]
|
||||
|
||||
## Binaural rendering tables
|
||||
|
||||
`rosella_kernels.npz` contains the fixed 64-QMF/77-hybrid tables used by the
|
||||
public SOFA binaural path:
|
||||
`rosella_kernels.npz` contains the fixed 64-QMF/77-hybrid tables used by
|
||||
`.jochrtf` rendering and personalized bank 1. Full SOFA rendering uses PCM convolution:
|
||||
|
||||
```text
|
||||
format_version little-endian int32[1]
|
||||
|
||||
+17
-1
@@ -4,6 +4,22 @@
|
||||
|
||||
本目录保存 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 核心解码表:
|
||||
|
||||
```text
|
||||
@@ -21,7 +37,7 @@ joc_huff_code_7ch_pos_index_sparse int64[6,2]
|
||||
|
||||
## 双耳渲染表
|
||||
|
||||
`rosella_kernels.npz` 保存公开 SOFA 双耳路径使用的 64-QMF/77-hybrid 固定表:
|
||||
`rosella_kernels.npz` 保存`.jochrtf`压缩渲染和个性化bank 1使用的64-QMF/77-hybrid固定表;完整SOFA路线直接在PCM域卷积:
|
||||
|
||||
```text
|
||||
format_version little-endian int32[1]
|
||||
|
||||
Binary file not shown.
Binary file not shown.
+61
-36
@@ -1,67 +1,92 @@
|
||||
# Binaural rendering
|
||||
|
||||
[中文](binaural.md) · [Back to README](../README.en.md)
|
||||
[中文](binaural.md) · [README](../README.en.md)
|
||||
|
||||
## Routes
|
||||
## Independent input routes
|
||||
|
||||
- SOFA: full complex local HRIR interpolation, adaptive-length filters and 128-sample partitioned convolution.
|
||||
- `.jochrtf`: the existing compact 77-band/fifth-order field and delay representation.
|
||||
- `.personalized_headphone`: the existing DSP, filterbank selection and Near/Mid/Far behavior.
|
||||
| Input | Rendering | Distance and environment |
|
||||
|---|---|---|
|
||||
| SOFA | Full complex directional HRIR interpolation, adaptive FIR length, 128-sample partitioned convolution | Direct by default; optional project room; no synthetic radial head filtering |
|
||||
| `.jochrtf` | Existing 77-band, fifth-order field and delay representation | Direct by default; same optional room |
|
||||
| `.personalized_headphone` | Existing DSP, filterbank and model parameters | Existing Near/Mid/Far and room processing unchanged |
|
||||
|
||||
SOFA and compact inputs use the same externally configured reference geometry, distance gains and environment state. SOFA never compiles or silently falls back to the compact format. The old compact loader/writer and compiler remain available.
|
||||
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.
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a --binaural
|
||||
python main.py input.m4a --binaural --sofa-hrtf subject.sofa --reference-config reference.json
|
||||
python main.py input.m4a --binaural --sofa-hrtf subject.sofa --sofa-interpolation lf_mca_zero
|
||||
python main.py input.m4a --binaural --sofa-hrtf subject.sofa --binaural-mode near
|
||||
python main.py input.m4a --binaural --sofa-hrtf subject.sofa --binaural-mode far
|
||||
python src\compile_hrtf.py subject.sofa subject.jochrtf
|
||||
python main.py input.m4a --binaural --compiled-hrtf-cache subject.jochrtf
|
||||
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
|
||||
```
|
||||
|
||||
The default SOFA is `HRTF/binaural.sofa` under the project directory, independently of the current working directory. Use `--sofa-hrtf PATH` for other locations. A single measured shell is selected automatically; multi-shell data requires `--hrtf-radius-m`. The personalized default path is unchanged. All three binaural routes accept `--binaural-mode near|mid|far`, defaulting to mid in the CLI; off remains an ADM-only DBMD hint.
|
||||
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`.
|
||||
|
||||
## Reference configuration
|
||||
SOFA and compact inputs retain the Near/Mid/Far point-source scales, default Mid. Personalized presets and ADM DBMD hints retain their existing behavior.
|
||||
|
||||
Resolution order: `--reference-config`, `BINAURAL_REFERENCE_CONFIG`, then local `local/binaural_reference.json`. Missing data, a hash mismatch, or an unsupported mapping is an error. Environment data and its specific identity are not supplied with the source. Example:
|
||||
## Point-source scale
|
||||
|
||||
```json
|
||||
{"schema":"binaural-reference-v1","profile_id":"reference-v1",
|
||||
"model_path":"C:/path/to/reference-data.json","model_sha256":"<64 hex digits>",
|
||||
"profile_index":3,"room_impulse_slots":4096,"level_calibration_directions":128,
|
||||
"compiled_receiver_positions_m":[[0,0.09,0],[0,-0.09,0]]}
|
||||
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)`:
|
||||
|
||||
```text
|
||||
q_SOFA = (y, -x, z) / ||p||
|
||||
d_nominal = reference_distance_m * ||p||
|
||||
H_render(ear, f) = H_selected_shell(ear, f, q_SOFA)
|
||||
```
|
||||
|
||||
The external data must be accepted by the existing parameter loader. Relative paths resolve against the configuration file. `profile_index` fixes the shared level-calibration anchor; `--binaural-mode` selects the rendering profile without changing that anchor. Compact files do not store receiver positions; they require explicit configured geometry. Full SOFA uses its own ear positions. A measured radius does not set the scene scale or room size.
|
||||
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.
|
||||
|
||||
Reference mapping uses the selected profile's quantization, axes, bounds and centre regularization. Outside positions extend the listener-to-boundary ray by the coded finite factor and are never clamped back inside. Near uses direct gain 1 and environment send 0. Mid/Far use `a=1/sqrt(1+c*(h0*r)^2)` and `b=h0*r*a`. Send gain is not final reverberant pressure. All modes share the same interpolation, radial adaptation, convolution and timing. There is no additional 1/r attenuation or absolute flight-time delay.
|
||||
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.
|
||||
|
||||
## Full HRIR model
|
||||
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.
|
||||
|
||||
The strict importer validates SimpleFreeFieldHRIR 0.4/1.0/1.1, FIR, units, listener orientation, ear order, zero emitter offset, finite data and free-field declarations. Resampling scales Data.Delay in samples. The radial adapter requires fixed symmetric receiver positions centred on the listener.
|
||||
## Optional project room
|
||||
|
||||
Map ADM right/front/up to SOFA front/left/up with `(y,-x,z)`. A 3D convex hull supplies local spherical triangles. Ray/planar barycentric weights are nonnegative and sum to one; exact measured nodes preserve the full complex response. Large triangles over coverage holes still have limited support.
|
||||
`--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.
|
||||
|
||||
Per-ear delay comes from phase regression at 200–1500 Hz. Interpolate aligned complex residuals and restore weighted delay, including Data.Delay once. The default `lf_logmag_phase` combines interpolated log magnitude with aligned complex phase. `lf_mca_zero` is an independent local MCA approximation with 41 continuous Gammatone power bands, PCHIP and a ±12 dB gain bound; it is not a bitwise reproduction of a third-party implementation.
|
||||
Near/Mid default to `compact`; Far defaults to `legacy` to retain its unit-coordinate
|
||||
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.
|
||||
|
||||
Radial adaptation multiplies by the complex normalized rigid-sphere ratio `H_sphere(r)/H_sphere(r_measured)`, exactly one at the measured radius. Ear geometry determines sphere radius. Near the centre, the configured regularization and an equal-ear, zero-ITD, spherical-area-weighted energy response give a continuous blend. This is an approximate human-head radial model; angular interpolation scores do not validate arbitrary distances.
|
||||
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.
|
||||
|
||||
Calibration computes one global scalar per input at the configured anchor from equal-area directions and total binaural energy with logarithmic frequency weighting at 100–16000 Hz. All three modes share that scalar; no per-mode/ear/direction/frequency normalization is applied.
|
||||
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.
|
||||
|
||||
The FFT size is at least 2048, automatically increased to a power of two that fits the complete HRIR and Data.Delay, up to 16384. All resulting FIR taps are retained, with a declared common shift of half the FFT size. Rendering rebases one global recording origin, the rounded median of mean-ear fitted delays; the environment branch receives matching delay before common compensation. Original spectrum queries retain the dataset time reference. The shift is independent of target source distance.
|
||||
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.
|
||||
|
||||
Per-object convolution retains input history and independent old/new overlap during a 128-sample output crossfade. Environment computation batches 1536 samples without changing the 128-sample parameter timeline; extra computation padding is trimmed when draining. Source PCM, delays and accumulation use float64; spectra use complex128, at 48 kHz.
|
||||
## Full HRIR and time reference
|
||||
|
||||
## Scheduling and limits
|
||||
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.
|
||||
|
||||
Each `[1536,16]` frame contains LFE followed by 15 already reconstructed objects/bed slots. Metadata retains the existing 1473-sample content offset and 32-sample alignment; new parameters update every 128 samples. A SOFA-only opt-in preserves negative elevation, outside distance and anchor fields. Legacy parsing and the personalized timeline retain their default behavior.
|
||||
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.
|
||||
|
||||
Multi-position blocks, ISF, explicit infinity and screen anchors requiring calibration return structured unsupported errors. Finite outside updates participate in timeline interpolation. `finish()` drains state only once; output gain is applied once without an implicit limiter.
|
||||
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.
|
||||
|
||||
## Compact files
|
||||
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.
|
||||
|
||||
Version-1 `.jochrtf` files retain their pickle-free ZIP schema, band centres, complex128 `[36,2,77]` coefficients, float64 `[36,2]` delay coefficients, `[2,2]` bounds, metadata, hash validation and atomic writer. Their inherent directional compression error remains. See [filterbank data](../data/README.en.md) for the public filterbank sources.
|
||||
`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.
|
||||
|
||||
Mathematical sources: [Duda and Martens 1998](https://www.ece.ucdavis.edu/cipic/wp-content/uploads/sites/12/2015/04/cipic_JASA_Nov_1998.pdf), [MCA paper](https://doi.org/10.1109/TASLP.2023.3313908), [ETSI TS 103 420](https://www.etsi.org/deliver/etsi_ts/103400_103499/103420/01.02.01_60/ts_103420v010201p.pdf). HRTF algorithms are independently implemented; external data retain their own licence terms.
|
||||
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
|
||||
|
||||
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.
|
||||
|
||||
## 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.
|
||||
|
||||
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.
|
||||
+46
-38
@@ -4,74 +4,82 @@
|
||||
|
||||
## 三条独立路线
|
||||
|
||||
| 输入 | 直接声 | 距离与环境 |
|
||||
| 输入 | 渲染 | 距离与环境 |
|
||||
|---|---|---|
|
||||
| SOFA | 完整复数HRIR局部插值、自适应长度FIR、128样本分区卷积 | 外部参考配置的Near/Mid/Far |
|
||||
| `.jochrtf` | 保留原77带/五阶方向场及延迟表示 | 与SOFA相同的三档参考合同 |
|
||||
| `.personalized_headphone` | 原DSP及滤波器组 | 原Near/Mid/Far及环境处理 |
|
||||
| SOFA | 完整复数HRIR方向插值、自适应长度FIR、128样本分区卷积 | 默认纯直达声;`--sofa-room`可选项目房间;不做径向人头合成 |
|
||||
| `.jochrtf` | 原77带/五阶方向场及延迟表示 | 同样默认纯直达声,可选相同房间 |
|
||||
| `.personalized_headphone` | 原DSP、滤波器组及模型参数 | 原Near/Mid/Far及房间处理保持不变 |
|
||||
|
||||
SOFA不会调用编译器、生成压缩缓存或自动回退。原压缩格式、校验器和离线编译能力保留。旧 `sofa_binaural_backend.py` 作为已有底层实现仍在,CLI工厂使用新的 `ReferenceSofaBackend`。
|
||||
SOFA不会调用编译器、生成压缩缓存或自动回退。`.jochrtf`格式、校验器和离线编译能力保留。SOFA与压缩输入通过`DirectSofaBackend`接入对象时间线。
|
||||
|
||||
```powershell
|
||||
python main.py input.m4a --binaural
|
||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --reference-config C:\config\reference.json
|
||||
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 --binaural-mode near
|
||||
python main.py input.m4a --binaural --sofa-hrtf C:\HRTF\subject.sofa --binaural-mode far
|
||||
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
|
||||
```
|
||||
|
||||
默认SOFA为项目目录下的 `HRTF/binaural.sofa`,路径不依赖当前工作目录;其他位置用 `--sofa-hrtf PATH` 显式指定。单球壳自动选择,多球壳须用 `--hrtf-radius-m` 指定最近的测量球壳。个性化默认路径不变。三条双耳路线都支持 `--binaural-mode near|mid|far`,CLI默认mid;off仅适用于ADM DBMD提示。
|
||||
默认SOFA为项目目录下的`HRTF/binaural.sofa`,不依赖当前工作目录;其他位置用`--sofa-hrtf PATH`指定。单球壳自动选择,多球壳须用`--hrtf-radius-m`选择一个测量球壳,当前不做径向插值。默认个性化路径仍为`HRTF/binaural.personalized_headphone`。
|
||||
|
||||
## 外部参考配置
|
||||
SOFA与压缩输入沿用Near/Mid/Far的点声源参考尺度,默认Mid。个性化的Near/Mid/Far和ADM输出的DBMD提示保持原行为。
|
||||
|
||||
`--reference-config` > `BINAURAL_REFERENCE_CONFIG` > 本地 `local/binaural_reference.json`。缺失、SHA-256不符或不支持的映射明确报错。环境数据与具体身份说明不随源码提供。配置示例:
|
||||
## 点声源参考尺度
|
||||
|
||||
```json
|
||||
{"schema":"binaural-reference-v1","profile_id":"reference-v1",
|
||||
"model_path":"C:/path/to/reference-data.json","model_sha256":"<64 hex digits>",
|
||||
"profile_index":3,"room_impulse_slots":4096,"level_calibration_directions":128,
|
||||
"compiled_receiver_positions_m":[[0,0.09,0],[0,-0.09,0]]}
|
||||
```
|
||||
|
||||
`model_path` 指向受现有参数加载器支持的外部数据,支持相对配置文件的路径;`profile_index` 固定公共电平校准锚点,选择渲染档位使用 `--binaural-mode`,不改校准锚点。压缩格式没有耳位字段,因此压缩输入必须显式配置耳位,且报告其来源。完整SOFA使用文件自身耳位。不同SOFA共用环境尺度,测量半径不定义场景距离或房间大小。
|
||||
|
||||
参考映射执行所选档位的量化、轴缩放、边界与近中心正则规则。有限盒外位置沿边界射线延伸,保留ETSI距离因子,不能再次裁回坐标盒。Near共同直接声系数为1、房间发送为0;Mid/Far为:
|
||||
Near/Mid/Far的参考距离分别为1.00000465/2.19327927/6.40177584 m,可用`--source-reference-distance-m`覆盖所选档位。对于ADM右/前/上坐标`p=(x,y,z)`:
|
||||
|
||||
```text
|
||||
r_eff = h0 * reference_distance_m
|
||||
a = 1 / sqrt(1 + c * r_eff^2)
|
||||
b = r_eff * a
|
||||
q_SOFA = (y, -x, z) / ||p||
|
||||
d_nominal = reference_distance_m * ||p||
|
||||
H_render(ear, f) = H_selected_shell(ear, f, q_SOFA)
|
||||
```
|
||||
|
||||
房间发送乘对象信号后进入独立环境状态。完整SOFA直接声与环境分支分别处理,再对齐实现延迟并求和;没有额外的1/r或绝对飞行时间。输出增益只应用一次,不隐式限幅。
|
||||
参考距离给对象坐标规定标称米制尺度,不会把测量半径为1.2 m的HRIR合成为2.193 m真值,也不会更改SOFA测量元数据。默认直达声模式不额外应用1/r衰减、球头距离比值、人工中心响应、房间发送、direct/room能量耦合或目标距离传播延迟;节目增益和SOFA响应刻度保持不变。开启房间后采用原项目距离耦合,用户指定输出增益仍只应用一次。
|
||||
|
||||
声源与听者重合时方向没有定义,沿用该对象最后一个有效方向;没有历史时取正前方。该规则不合成零ITD响应。有限盒外坐标保留元数据距离因子,不裁回单位盒。
|
||||
|
||||
SOFA输入不需要个性化模型或本机配置;关闭房间时也不读取房间资产。报告记录输入路径、SHA-256、测量半径、插值方法与房间开关。
|
||||
|
||||
## 可选项目房间
|
||||
|
||||
`--sofa-room`默认关闭,复用原`.jochrtf`路线的`public_room.py`和`reference_distance.py`算法,提供原始与紧凑两套参数:六个一阶镜像反射,以及四路正交Hadamard反馈、两级全通扩散、低通阻尼的共享FDN。它是项目自有听感预设,目标是提供接近理想听音环境、以直达声为主的房间感,不是测量BRIR或强制标准房间。它不依赖Rosella房间表。
|
||||
|
||||
启用后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偏移、自由场、单位和有限数值。重采样时Data.Delay的采样点数同比例变化。当前距离模型要求固定、对称并以听者中心为原点的耳位;不支持的几何明确拒绝。
|
||||
导入器校验`SimpleFreeFieldHRIR` 0.4/1.0/1.1、FIR、两个可由位置识别左右的耳、零Emitter偏移、自由场、单位和有限数值。采样率统一为48 kHz;重采样时`Data.Delay`的采样点数同比例变化。耳位须在测量之间固定;方向插值不再要求用对称耳位拟合球头。
|
||||
|
||||
SOFA为前/左/上,ADM为右/前/上,变换为 `(y,-x,z)`。单位方向三维凸包构建局部三角网格,查询使用射线与平面三角形的非负、和为1的重心权重;不做经纬平面插值或外推。精确节点返回完整复数响应。跨越大测量缺口的面仍是低支持区域,网格包围原点不能证明覆盖精度。
|
||||
当前没有实现`SimpleFreeFieldHRIR`以外的BRIR或其他SOFA Convention。不支持的文件明确拒绝。后端已按响应类型预留BRIR提示及忽略`--sofa-room`的规则;当前验证的是该策略接口,不代表已实现BRIR读取。
|
||||
|
||||
FFT最少2048点,按完整HRIR与Data.Delay自动扩展为满足时间预算的2次幂,自动长度上限16384点;不裁短输入。每耳在200–1500 Hz作线性相位回归,对齐后插值完整剩余复数响应,再恢复加权延迟。Data.Delay恰好施加一次,不能从耳位再加一次ITD。默认 `lf_logmag_phase` 对数幅度与对齐复数插值相位结合;可选 `lf_mca_zero` 使用41个连续Gammatone功率近似带、PCHIP和±12 dB修正限幅。这是独立MCA近似,并非第三方项目逐位复现。
|
||||
测量单位方向的三维凸包构成局部三角网格,查询使用射线与面三角形的非负、和为1的重心权重。不使用经纬平面插值,不外推未覆盖半球。精确节点返回测量复数响应。
|
||||
|
||||
距离补充采用归一化刚性球传递函数 `H_sphere(r)/H_sphere(r_measurement)` 的复数比值。测量半径处严格为1;不叠加第二套耳间延迟。球半径由对称耳位推导。中心使用参考正则半径与零ITD的球面面积加权能量平均响应平滑混合。该模型是人头径向变化的近似,不能把角度插值实验当作任意距离准确性证明。
|
||||
低频对齐延迟在200–1500 Hz拟合。默认`lf_logmag_phase`对对数幅度插值,原相位通过时间对齐的复数残差及插值延迟恢复;备选`lf_mca_zero`使用听觉带幅度校正。两种方法都保留所选FFT长度的完整FIR,不替换为最小相位。
|
||||
|
||||
直接声电平按固定等面积方向及100–16000 Hz对数频率权重,与校准锚点的参考直接声匹配总双耳能量;每份SOFA只得到一个全局系数,三档共用该系数,不逐档、逐方向、逐耳或逐频率归一化。三档共用相同的HRIR插值、距离补充、卷积和时间补偿流程,只切换参考参数与其直接声/发送规则。
|
||||
`Data.Delay`在频域只叠加一次。公共移位为FFT长度的一半,用于保存插值前振铃;输出只补偿卷积/滤波器组的实现延迟。文件提供的共同录音延迟、左右耳相对延迟和频率相关相位关系均保留,不再减去拟合录音原点,也不根据目标距离增加飞行时间。真实序列的DC/Nyquist频点按实数端点处理。
|
||||
|
||||
保留选定FFT长度的全部FIR采样,共同移位FFT长度的一半以存储前振铃。渲染时使用全方向双耳低频延迟平均值的中位数、取整为单一录音时间原点,给环境分支补齐该原点与实现延迟后统一补偿;报告此偏移。查询得到的原始复数HRIR不改时间基准。这个全局时间参考变换不根据目标距离增加传播时间。
|
||||
每对象拥有独立卷积历史。更新时旧/新输出在128样本内线性交叉淡化,两个滤波器均使用完整输入历史。滤波器更新同样延后公共FIR实现移位,避免移除实现延迟后将元数据更新提前。完整HRIR路线直接在PCM域卷积;QMF/hybrid仅用于显式压缩输入。内部精度为float64/complex128,不做隐式限幅。
|
||||
|
||||
每对象独立卷积历史,更新时旧/新输出线性交叉淡化128样本,保持输入历史及独立overlap;环境发送平滑更新。环境计算按1536样本批处理,不改变128样本参数时间线,排空时裁掉额外计算填充。采样率固定48 kHz,内部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样本更新参数。
|
||||
|
||||
SOFA专用解析开关保留负高度、边界外因子及锚定字段;旧解析器默认行为与个性化时间线不变。有限盒外更新进入时间插值;当前多位置块、ISF、显式无穷远和需要屏幕校准的锚定明确返回结构化不支持错误。`finish()`只排空一次,并保留配置的尾声预算。
|
||||
LFE继续使用独立低通处理,不发送至可选房间。SOFA专用解析保留负高度、边界外因子及锚定字段;个性化解析与时间线不变。当前多位置块、ISF、显式无穷远以及需要屏幕校准的锚定明确报错。`finish()`仅排空一次,`--binaural-tail-seconds`规定FIR/滤波器组及可选房间尾声的排空预算。
|
||||
|
||||
## 轻量格式
|
||||
|
||||
`.jochrtf` v1仍为无pickle的NumPy ZIP,保存JSON元数据、77个频带中心、`complex128[36,2,77]`方向系数、`float64[36,2]`延迟系数和`float64[2,2]`边界。原格式、cache key、哈希校验、原子写入及源数据许可关系不变。它保留方向压缩的固有误差;与完整SOFA的距离语义一致不代表频响精度相同。滤波器组公开来源见 [data说明](../data/README.md)。
|
||||
`.jochrtf` v1仍为无pickle的NumPy ZIP,保存JSON元数据、77个频带中心、`complex128[36,2,77]`方向系数、`float64[36,2]`延迟系数及`float64[2,2]`边界。原格式、cache key、哈希校验、原子写入及源数据许可关系不变。压缩输入保留自身的方向与频率表示误差,不等同于完整HRIR精度。
|
||||
|
||||
数学依据:[Duda与Martens 1998](https://www.ece.ucdavis.edu/cipic/wp-content/uploads/sites/12/2015/04/cipic_JASA_Nov_1998.pdf),[MCA原论文](https://doi.org/10.1109/TASLP.2023.3313908),[ETSI TS 103 420](https://www.etsi.org/deliver/etsi_ts/103400_103499/103420/01.02.01_60/ts_103420v010201p.pdf)。算法独立实现,不复制第三方HRTF源码;数据许可仍随外部数据适用。
|
||||
滤波器组来源见[data说明](../data/README.md)。方向插值和MCA的参考文献见[研究实现依据](https://doi.org/10.1109/TASLP.2023.3313908);算法独立实现,外部测量数据仍适用其自身许可。
|
||||
@@ -529,7 +529,7 @@ def build_parser():
|
||||
help="扬声器渲染 metadata 相对帧偏移,默认 1473 samples")
|
||||
parser.add_argument("--binaural-mode", choices=("off", "near", "mid", "far"),
|
||||
default=None,
|
||||
help="双耳渲染模式或ADM DBMD提示,默认mid;支持near/mid/far。"
|
||||
help="双耳参考距离档位或ADM DBMD提示,默认mid;支持near/mid/far。"
|
||||
"off仅用于ADM BWF关闭DBMD双耳提示")
|
||||
hrtf_input = parser.add_mutually_exclusive_group()
|
||||
hrtf_input.add_argument(
|
||||
@@ -556,12 +556,16 @@ def build_parser():
|
||||
parser.add_argument(
|
||||
"--hrtf-radius-m", type=float, default=None,
|
||||
help="选择最近的SOFA测量球壳;单球壳默认自动选择,多球壳必须指定")
|
||||
parser.add_argument("--reference-config", type=Path,
|
||||
help="SOFA/.jochrtf的外部参考环境配置")
|
||||
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,
|
||||
help="双耳 room/filterbank flush 上限,默认 5 秒")
|
||||
help="双耳 FIR/filterbank 排空上限,默认 5 秒")
|
||||
parser.add_argument("--binaural-tail-threshold", type=float, default=1.0e-8,
|
||||
help="双耳尾声裁切阈值,默认 1e-8;主体至少保留原时长")
|
||||
parser.add_argument("--binaural-chunk-frames", type=int, default=64,
|
||||
@@ -636,8 +640,10 @@ def main(argv=None):
|
||||
args.hrtf_cache_policy is not None,
|
||||
args.hrtf_cache_dir is not None,
|
||||
args.hrtf_radius_m is not None,
|
||||
args.reference_config 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:
|
||||
raise ValueError("SOFA/HRTF 选项仅与 --binaural 一起使用")
|
||||
@@ -678,12 +684,22 @@ def main(argv=None):
|
||||
raise ValueError("eac3-target-level 必须在 -31..0 之间")
|
||||
binaural_hrtf_input = resolve_binaural_hrtf_input(
|
||||
args, required=binaural_mode and not args.metadata_only)
|
||||
if args.source_reference_distance_m is not None:
|
||||
if (not math.isfinite(args.source_reference_distance_m)
|
||||
or args.source_reference_distance_m <= 0):
|
||||
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.reference_config is not None or args.sofa_interpolation is not None):
|
||||
raise ValueError("参考环境/HRIR插值选项仅用于SOFA路径")
|
||||
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
|
||||
and binaural_hrtf_input["kind"] != "rosella"):
|
||||
raise ValueError(
|
||||
@@ -814,8 +830,12 @@ def main(argv=None):
|
||||
"output_gain": gain_float64,
|
||||
"chunk_frames": args.binaural_chunk_frames,
|
||||
"bed_channels": getattr(index, "bed_channels", ()),
|
||||
"reference_config": args.reference_config,
|
||||
}
|
||||
if args.source_reference_distance_m is not None:
|
||||
common_options["reference_distance_m"] = args.source_reference_distance_m
|
||||
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",
|
||||
@@ -849,6 +869,9 @@ def main(argv=None):
|
||||
f"backend={binaural_decoder.dsp_backend} "
|
||||
f"precision=float64/complex128 "
|
||||
f"hrtf={hrtf_source['kind']}:{hrtf_source['path']}", flush=True)
|
||||
if hrtf_source["kind"] != "rosella":
|
||||
room = binaural_decoder.backend.room
|
||||
print(f"[binaural] room={'off' if room is None else room.preset}", flush=True)
|
||||
if hrtf_source["kind"] == "rosella":
|
||||
print(
|
||||
f"[binaural] filterbank={FILTERBANK_NAMES[binaural_decoder.filterbank]}"
|
||||
|
||||
@@ -1,137 +0,0 @@
|
||||
"""Configured reference environment, geometry and scalar level calibration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
|
||||
from rosella_bank import RosellaBank
|
||||
from rosella_direct import _profile_geometry, direct_and_room_send
|
||||
from rosella_model import load_personalized_headphone
|
||||
from rosella_room import RosellaRoomFir
|
||||
|
||||
|
||||
DEFAULT_REFERENCE_CONFIG = Path(__file__).resolve().parents[1]/"local"/"binaural_reference.json"
|
||||
REFERENCE_MODES = {"near": 1, "mid": 3, "far": 2}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ReferencePosition:
|
||||
direction: np.ndarray
|
||||
distance_m: float
|
||||
ear_distance_m: float
|
||||
centre_weight: float
|
||||
direct_gain: float
|
||||
room_send: float
|
||||
|
||||
|
||||
class ReferenceEnvironment:
|
||||
def __init__(self, config=None, *, mode=None):
|
||||
self.config_path = Path(config or os.environ.get("BINAURAL_REFERENCE_CONFIG") or
|
||||
DEFAULT_REFERENCE_CONFIG).expanduser().resolve()
|
||||
with self.config_path.open(encoding="utf-8") as stream:
|
||||
spec = json.load(stream)
|
||||
self.configuration_sha256 = hashlib.sha256(
|
||||
json.dumps(spec, sort_keys=True, separators=(",", ":")).encode("utf-8")).hexdigest()
|
||||
if spec.get("schema") != "binaural-reference-v1":
|
||||
raise ValueError("unsupported reference configuration")
|
||||
path = Path(spec["model_path"]).expanduser()
|
||||
if not path.is_absolute():
|
||||
path = self.config_path.parent/path
|
||||
self.model_sha256 = hashlib.sha256(path.read_bytes()).hexdigest()
|
||||
if self.model_sha256.lower() != str(spec["model_sha256"]).lower():
|
||||
raise ValueError("reference data hash mismatch")
|
||||
self.model = load_personalized_headphone(path)
|
||||
self.calibration_profile_index = int(spec["profile_index"])
|
||||
self.profile_id = str(spec["profile_id"])
|
||||
if self.calibration_profile_index not in (2, 3) or self.model.sample_rate != 48000:
|
||||
raise ValueError("unsupported reference profile")
|
||||
self.mode = ({v: k for k, v in REFERENCE_MODES.items()}[self.calibration_profile_index]
|
||||
if mode is None else str(mode).lower())
|
||||
if self.mode not in REFERENCE_MODES:
|
||||
raise ValueError("reference mode must be near, mid, or far")
|
||||
self.profile_index = REFERENCE_MODES[self.mode]
|
||||
if int(self.model.header_integer_fields[0]) != 0:
|
||||
raise ValueError("reference-v1 requires a Cartesian mapping")
|
||||
self.profile = self.model.profiles[self.profile_index]
|
||||
self.compiled_receivers = spec.get("compiled_receiver_positions_m")
|
||||
self.room_slots = int(spec.get("room_impulse_slots", 4096))
|
||||
self.calibration_directions = int(spec.get("level_calibration_directions", 128))
|
||||
if not 128 <= self.room_slots <= 16384 or not 16 <= self.calibration_directions <= 2048:
|
||||
raise ValueError("reference configuration exceeds supported bounds")
|
||||
self.h0, self.coupling = map(float, self.model.header_float_scalars[:2])
|
||||
if self.h0 <= 0 or self.coupling <= 0:
|
||||
raise ValueError("invalid reference energy coupling")
|
||||
self.minimum_distance_m = float(self.profile.minimum_normalized_radius)*float(self.profile.distance_scale_m)
|
||||
self.reference_distance_m = self.map_position((0., 1., 0.)).distance_m
|
||||
|
||||
def map_position(self, position, *, profile_index=None):
|
||||
position = np.asarray(position, dtype=np.float64)
|
||||
if position.shape != (3,) or not np.isfinite(position).all():
|
||||
raise ValueError("reference position must contain three finite values")
|
||||
extension = max(1., float(np.max(np.abs(position))))
|
||||
if extension > 50.1+1e-9:
|
||||
raise ValueError("position exceeds the finite metadata domain")
|
||||
index = self.profile_index if profile_index is None else int(profile_index)
|
||||
profile, direction, radius, _, _ = _profile_geometry(self.model, position/extension, index)
|
||||
distance = float(profile.distance_scale_m)*radius*extension
|
||||
minimum = float(profile.minimum_normalized_radius)*float(profile.distance_scale_m)
|
||||
regularized = max(distance, minimum)
|
||||
alpha = distance/regularized
|
||||
effective = self.h0*distance
|
||||
if index == REFERENCE_MODES["near"]:
|
||||
a, send = 1., 0.
|
||||
else:
|
||||
a = 1/math.sqrt(1+self.coupling*effective*effective)
|
||||
send = effective*a
|
||||
return ReferencePosition(np.asarray(direction), distance, regularized, alpha, a, send)
|
||||
|
||||
def create_room(self):
|
||||
return RosellaRoomFir(self.model, impulse_slots=self.room_slots)
|
||||
|
||||
@staticmethod
|
||||
def create_bank(source_channels):
|
||||
return RosellaBank(1, source_channels=source_channels, output_channels=2)
|
||||
|
||||
def calibrate(self, field, spectrum_at):
|
||||
from public_filterbank import hybrid_gain_synthesis_dictionary
|
||||
from rosella_direct import _FIELD_CACHE
|
||||
# Discard stale cache entries when object ids are reused.
|
||||
_FIELD_CACHE.pop(id(self.model), None)
|
||||
count = self.calibration_directions
|
||||
i = np.arange(count, dtype=np.float64)
|
||||
z = 1-2*(i+.5)/count
|
||||
phi = i*np.pi*(3-math.sqrt(5))
|
||||
unit = np.column_stack((np.sqrt(1-z*z)*np.cos(phi), np.sqrt(1-z*z)*np.sin(phi), z))
|
||||
dictionary = hybrid_gain_synthesis_dictionary(field.fft_size)
|
||||
band = (field.frequencies >= 100) & (field.frequencies <= 16000)
|
||||
weights = 1/field.frequencies[band]
|
||||
reference_energy = 0.
|
||||
new_energy = 0.
|
||||
for direction in unit:
|
||||
adm = (-direction[1], direction[0], direction[2])
|
||||
state = self.map_position(adm, profile_index=self.calibration_profile_index)
|
||||
parameters = direct_and_room_send(self.model, adm, self.calibration_profile_index)
|
||||
vector = np.empty((2, 154), dtype=np.float64)
|
||||
vector[:, 0::2], vector[:, 1::2] = parameters.gains.real, parameters.gains.imag
|
||||
reference = np.fft.rfft(vector @ dictionary.T, field.fft_size)
|
||||
target = spectrum_at(state)*state.direct_gain
|
||||
reference_energy += float(np.sum(np.abs(reference[:, band])**2*weights))
|
||||
new_energy += float(np.sum(np.abs(target[:, band])**2*weights))
|
||||
if min(reference_energy, new_energy) <= 1e-24 or not math.isfinite(new_energy):
|
||||
raise ValueError("reference level calibration has no finite signal energy")
|
||||
return math.sqrt(reference_energy/new_energy)
|
||||
|
||||
def info(self):
|
||||
return dict(profile_id=self.profile_id, model_sha256=self.model_sha256,
|
||||
configuration_sha256=self.configuration_sha256,
|
||||
mode=self.mode, selected_profile_index=self.profile_index,
|
||||
calibration_profile_index=self.calibration_profile_index,
|
||||
distance_semantics="reference-reproduction-metres", reference_distance_m=self.reference_distance_m,
|
||||
regularized_distance_m=self.minimum_distance_m,
|
||||
filterbank=1, room_impulse_slots=self.room_slots,
|
||||
calibration_directions=self.calibration_directions)
|
||||
+17
-10
@@ -116,22 +116,25 @@ class SofaBinauralRenderer:
|
||||
tail_seconds: float = 5.0,
|
||||
output_gain: float = 1.0,
|
||||
chunk_frames: int = 64,
|
||||
bed_channels=None, reference_config=None,
|
||||
interpolation: str = "lf_logmag_phase") -> "SofaBinauralRenderer":
|
||||
from sofa_reference_backend import ReferenceSofaBackend
|
||||
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 = ReferenceSofaBackend(
|
||||
backend = DirectSofaBackend(
|
||||
source,
|
||||
source_count=SOURCE_CHANNELS,
|
||||
output_gain=output_gain,
|
||||
reference_config=reference_config,
|
||||
mode=mode,
|
||||
shell_radius_m=shell_radius_m,
|
||||
method=interpolation)
|
||||
method=interpolation,
|
||||
reference_distance_m=reference_distance_m, sofa_room=sofa_room, room_preset=room_preset)
|
||||
return cls(
|
||||
backend,
|
||||
mode=mode,
|
||||
@@ -148,13 +151,17 @@ class SofaBinauralRenderer:
|
||||
tail_seconds: float = 5.0,
|
||||
output_gain: float = 1.0,
|
||||
chunk_frames: int = 64,
|
||||
bed_channels=None, reference_config=None) -> "SofaBinauralRenderer":
|
||||
from sofa_reference_backend import ReferenceSofaBackend
|
||||
bed_channels=None,
|
||||
reference_distance_m: float | None = None,
|
||||
sofa_room: bool = False,
|
||||
room_preset: str | None = None) -> "SofaBinauralRenderer":
|
||||
from sofa_direct_backend import DirectSofaBackend
|
||||
source = resolve_compiled_hrtf_cache(cache)
|
||||
backend = ReferenceSofaBackend(
|
||||
backend = DirectSofaBackend(
|
||||
source,
|
||||
source_count=SOURCE_CHANNELS,
|
||||
compiled=True, reference_config=reference_config, output_gain=output_gain, mode=mode)
|
||||
compiled=True, output_gain=output_gain, mode=mode,
|
||||
reference_distance_m=reference_distance_m, sofa_room=sofa_room, room_preset=room_preset)
|
||||
return cls(
|
||||
backend,
|
||||
mode=mode,
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
"""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)
|
||||
@@ -0,0 +1,121 @@
|
||||
"""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,49 @@
|
||||
"""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,552 +0,0 @@
|
||||
"""Stateful public SOFA 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_canonical import CanonicalHrtf
|
||||
from sofa_hrtf_field import (
|
||||
DEFAULT_ORDER,
|
||||
DEFAULT_PROJECTION_RIDGE,
|
||||
DEFAULT_SH_RIDGE,
|
||||
SofaHrtfField,
|
||||
compile_sofa_hrtf,
|
||||
)
|
||||
|
||||
|
||||
@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 SofaBinauralBackend:
|
||||
"""SOFA-derived public 77-band/SH renderer with public room processing."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
field: SofaHrtfField,
|
||||
*,
|
||||
source_count: int = 16,
|
||||
sample_rate_hz: float = 48000.0,
|
||||
default_profile: str = "mid",
|
||||
enable_early_reflections: bool = True,
|
||||
enable_late_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.enable_early_reflections = bool(enable_early_reflections)
|
||||
self.enable_late_room = bool(enable_late_room)
|
||||
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_sofa() or "
|
||||
"from_compiled_cache() for file inputs")
|
||||
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_sofa(
|
||||
cls, sofa: str | Path | CanonicalHrtf, *,
|
||||
cache_policy: str = "memory",
|
||||
cache_dir: str | Path | None = None,
|
||||
shell_radius_m: float = 1.0,
|
||||
order: int = DEFAULT_ORDER,
|
||||
projection_ridge: float = DEFAULT_PROJECTION_RIDGE,
|
||||
sh_ridge: float = DEFAULT_SH_RIDGE,
|
||||
**renderer_options) -> "SofaBinauralBackend":
|
||||
"""Compile a SOFA source once and construct the runtime renderer."""
|
||||
field = compile_sofa_hrtf(
|
||||
sofa,
|
||||
target_sample_rate_hz=float(
|
||||
renderer_options.get("sample_rate_hz", 48000.0)),
|
||||
shell_radius_m=shell_radius_m,
|
||||
order=order,
|
||||
projection_ridge=projection_ridge,
|
||||
sh_ridge=sh_ridge,
|
||||
cache_policy=cache_policy,
|
||||
cache_dir=cache_dir)
|
||||
result = cls(field, **renderer_options)
|
||||
result.hrtf_input_kind = "sofa"
|
||||
result.hrtf_input_path = (
|
||||
str(Path(sofa).expanduser().resolve())
|
||||
if not isinstance(sofa, CanonicalHrtf) else sofa.source_path)
|
||||
result.cache_policy = str(cache_policy).lower()
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def from_compiled_cache(
|
||||
cls, cache: str | Path, **renderer_options
|
||||
) -> "SofaBinauralBackend":
|
||||
"""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(),
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
"""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")
|
||||
+5
-64
@@ -5,12 +5,14 @@ from collections import OrderedDict
|
||||
import math
|
||||
import numpy as np
|
||||
from scipy.interpolate import PchipInterpolator
|
||||
from scipy.spatial import ConvexHull, SphericalVoronoi, cKDTree
|
||||
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"):
|
||||
@@ -41,13 +43,6 @@ class FullHrirField:
|
||||
if not np.allclose(receiver, receiver[0], atol=1e-9, rtol=0):
|
||||
raise ValueError("moving receiver geometry is unsupported")
|
||||
self.receivers = receiver[0].copy()
|
||||
if np.linalg.norm(self.receivers.mean(axis=0)) > 1e-6:
|
||||
raise ValueError("radial model requires ears centred on the listener")
|
||||
self.head_radius_m = float(np.linalg.norm(self.receivers[0]))
|
||||
if not (0.04 < self.head_radius_m < .15) or not np.isclose(
|
||||
np.linalg.norm(self.receivers[1]), self.head_radius_m, atol=1e-6):
|
||||
raise ValueError("unsupported receiver geometry for the radial model")
|
||||
self.ear_directions = self.receivers/self.head_radius_m
|
||||
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):
|
||||
@@ -83,10 +78,6 @@ class FullHrirField:
|
||||
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)
|
||||
areas = SphericalVoronoi(self.directions).calculate_areas()
|
||||
areas /= areas.sum()
|
||||
energy = np.einsum("m,mef->f", areas, np.abs(physical)**2)/2
|
||||
self.centre_spectrum = np.sqrt(energy)*np.exp(-1j*self.omega*self.recording_origin_samples)
|
||||
self.aligned = physical*np.exp(1j*self.omega*self.delays[..., None])
|
||||
self.log_magnitude = None
|
||||
self.auditory = None
|
||||
@@ -166,55 +157,5 @@ class FullHrirField:
|
||||
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="complex-rigid-sphere-distance-ratio",
|
||||
radial_model_validation="analytic-identity-and-series-checks; approximate-human-head")
|
||||
|
||||
|
||||
def sphere_transfer(frequencies, radius, head_radius, cosines, *, sound_speed=343.3,
|
||||
tolerance=1e-10):
|
||||
"""Point-source pressure / free-field centre pressure, FFT sign convention.
|
||||
|
||||
Evaluate the rigid-sphere series using adjacent Hankel ratios, avoiding
|
||||
overflow of high-order Neumann functions at low frequency.
|
||||
"""
|
||||
f = np.asarray(frequencies, dtype=np.float64)
|
||||
r, a = float(radius), float(head_radius)
|
||||
mu = np.asarray(cosines, dtype=np.float64)
|
||||
if f.ndim != 1 or not np.isfinite(f).all() or np.any(f < 0) or not np.isfinite(mu).all():
|
||||
raise ValueError("invalid sphere frequency or incidence")
|
||||
if not (math.isfinite(r) and math.isfinite(a) and r > a > 0):
|
||||
raise ValueError("sphere source must be outside the head")
|
||||
x = 2*np.pi*f*a/sound_speed
|
||||
z = x*(r/a)
|
||||
order = max(int(math.ceil(float(x.max(initial=0))))+32,
|
||||
int(math.ceil(math.log(tolerance)/math.log(a/r)))+16)
|
||||
if order > 4096:
|
||||
raise ValueError("source too close to the sphere surface")
|
||||
mu = np.clip(mu, -1., 1.)
|
||||
p_before, p = np.ones_like(mu), mu.copy()
|
||||
result = np.ones(mu.shape+(len(f),), dtype=np.complex128)/(1+1j*x)
|
||||
ratio_a = x/(1+1j*x)
|
||||
ratio_r = z/(1+1j*z)
|
||||
product = np.ones_like(x, dtype=np.complex128)
|
||||
for n in range(1, order+1):
|
||||
quotient = np.divide(ratio_a, ratio_r, out=np.full_like(ratio_a, a/r), where=z != 0)
|
||||
product *= quotient
|
||||
result += (2*n+1)*p[..., None]*product/(n+1-x*ratio_a)
|
||||
p_before, p = p, ((2*n+1)*mu*p-n*p_before)/(n+1)
|
||||
ratio_a = x/((2*n+1)-x*ratio_a)
|
||||
ratio_r = z/((2*n+1)-z*ratio_r)
|
||||
result *= np.exp(1j*x)
|
||||
if not np.isfinite(result).all():
|
||||
raise ValueError("sphere series failed to converge")
|
||||
return result
|
||||
|
||||
|
||||
def radial_ratio(field, direction, radius):
|
||||
if abs(float(radius)-field.measurement_radius_m) < 1e-10:
|
||||
return np.ones((2, len(field.frequencies)), dtype=np.complex128)
|
||||
mu = field.ear_directions @ np.asarray(direction, dtype=np.float64)
|
||||
source = sphere_transfer(field.frequencies, field.measurement_radius_m, field.head_radius_m, mu)
|
||||
target = sphere_transfer(field.frequencies, radius, field.head_radius_m, mu)
|
||||
if np.min(np.abs(source)) < 1e-10:
|
||||
raise ValueError("singular radial reference response")
|
||||
return target/source
|
||||
ear_model="measured-SOFA-receivers",
|
||||
receiver_positions_m=self.receivers.tolist())
|
||||
@@ -1,270 +0,0 @@
|
||||
"""Direct HRIR rendering with a configured reference environment."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections import OrderedDict
|
||||
import math
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
import numpy as np
|
||||
from scipy import signal
|
||||
|
||||
from binaural_reference import ReferenceEnvironment
|
||||
from partitioned_convolution import MultiSourcePartitionedConvolver
|
||||
from sofa_hrir import FullHrirField, radial_ratio
|
||||
from sofa_hrtf_field import SofaHrtfField
|
||||
from sofa_binaural_backend import HybridPath, HybridObjectPathRenderer, _StereoDelay
|
||||
|
||||
|
||||
class _CompiledField:
|
||||
def __init__(self, path, receivers):
|
||||
if receivers is None:
|
||||
raise ValueError("compiled input requires configured receiver positions")
|
||||
self.field = SofaHrtfField.load(path)
|
||||
self.fft_size = 2048
|
||||
self.frequencies = np.fft.rfftfreq(self.fft_size, 1/48000)
|
||||
self.measurement_radius_m = self.field.measurement_radius_m
|
||||
receivers = np.asarray(receivers, dtype=np.float64)
|
||||
if receivers.shape != (2, 3) or not np.isfinite(receivers).all():
|
||||
raise ValueError("invalid compiled receiver positions")
|
||||
self.head_radius_m = float(np.linalg.norm(receivers[0]))
|
||||
if not .04 < self.head_radius_m < .15 or not np.allclose(receivers[0], -receivers[1], atol=1e-6):
|
||||
raise ValueError("unsupported compiled receiver geometry")
|
||||
self.ear_directions = receivers/self.head_radius_m
|
||||
self.band_geometry = SimpleNamespace(frequencies=self.field.band_center_frequencies_hz,
|
||||
measurement_radius_m=self.measurement_radius_m, head_radius_m=self.head_radius_m,
|
||||
ear_directions=self.ear_directions)
|
||||
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))
|
||||
evaluations = [self.field.evaluate_sofa(q) for q in directions]
|
||||
self.recording_origin_samples = int(round(float(np.median([e.delay_samples.mean() for e in evaluations]))))
|
||||
self.centre_band = np.sqrt(np.mean([np.abs(e.transfer_gains)**2 for e in evaluations], axis=(0, 1)))
|
||||
from public_filterbank import hybrid_gain_synthesis_dictionary
|
||||
self.dictionary = hybrid_gain_synthesis_dictionary(self.fft_size)
|
||||
|
||||
def bands(self, state):
|
||||
e = self.field.evaluate_sofa(state.direction)
|
||||
ratio = radial_ratio(self.band_geometry, state.direction, state.ear_distance_m)
|
||||
return e, ratio
|
||||
|
||||
def spectrum_at(self, state):
|
||||
e, ratio = self.bands(state)
|
||||
f = self.field.band_center_frequencies_hz
|
||||
centre = self.centre_band*np.exp(-2j*np.pi*f*self.recording_origin_samples/48000)
|
||||
gains = state.centre_weight*e.transfer_gains*ratio+(1-state.centre_weight)*centre
|
||||
values = np.empty((2, 154))
|
||||
values[:, 0::2], values[:, 1::2] = gains.real, gains.imag
|
||||
return np.fft.rfft(values @ self.dictionary.T, self.fft_size)
|
||||
|
||||
def info(self):
|
||||
return dict(source_sha256=self.field.source_sha256, method="compiled-band-field",
|
||||
measurement_radius_m=self.measurement_radius_m,
|
||||
receiver_geometry_source="reference-configuration",
|
||||
cache_key=self.field.cache_key, format_version=self.field.format_version,
|
||||
recording_origin_samples=self.recording_origin_samples,
|
||||
radial_model_validation="analytic-identity-and-series-checks; approximate-human-head")
|
||||
|
||||
|
||||
class ReferenceSofaBackend:
|
||||
fixed_reference = True
|
||||
default_profile = "mid"
|
||||
dsp_backend = "python-full-hrir"
|
||||
block_samples = 128
|
||||
environment_batch_samples = 1536
|
||||
|
||||
def __init__(self, source, *, reference_config=None, compiled=False, source_count=16,
|
||||
output_gain=1., method="lf_logmag_phase", shell_radius_m=None, mode=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.environment = ReferenceEnvironment(reference_config, mode=mode)
|
||||
self.default_profile = self.environment.mode
|
||||
self.compiled = bool(compiled)
|
||||
self.field = (_CompiledField(source, self.environment.compiled_receivers) if compiled else
|
||||
FullHrirField(source, method=method, shell_radius_m=shell_radius_m))
|
||||
self.hrtf_input_kind = "compiled_cache" if compiled else "sofa"
|
||||
self.hrtf_input_path = str(Path(source).expanduser().resolve())
|
||||
self.cache_policy = None
|
||||
if self.environment.minimum_distance_m <= self.field.head_radius_m:
|
||||
raise ValueError("reference near-centre regularization lies inside the head")
|
||||
self._spectra = OrderedDict()
|
||||
self.level_calibration = self.environment.calibrate(self.field, self.spectrum_at)
|
||||
self.bank = self.environment.create_bank(self.source_count)
|
||||
self.room = self.environment.create_room()
|
||||
self._last = [None]*self.source_count
|
||||
self.distance_states = [None]*self.source_count
|
||||
self._sends = np.zeros(self.source_count, dtype=np.float64)
|
||||
self._send_targets = self._sends.copy()
|
||||
self.processed_input_samples = self.output_samples = self.parameter_updates = 0
|
||||
self.finished = False
|
||||
self._pending_room = []
|
||||
self._pending_direct = []
|
||||
self.origin = self.field.recording_origin_samples
|
||||
if self.origin < 0:
|
||||
raise ValueError("negative recording origin requires an explicit time-base adapter")
|
||||
self.implementation_latency = (self.bank.latency_samples if compiled else self.field.common_latency_samples)
|
||||
self.latency_samples = max(self.implementation_latency, self.bank.latency_samples)+max(self.origin, 0)
|
||||
self._latency_remaining = self.latency_samples
|
||||
self.room_delay = _StereoDelay(self.latency_samples-self.bank.latency_samples)
|
||||
if compiled:
|
||||
self.dsp_backend = "python-compiled-hrtf"
|
||||
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.direct_delay = _StereoDelay(max(0, self.latency_samples-self.bank.latency_samples-self.origin))
|
||||
self.lfe_transfer = self._compiled_lfe()
|
||||
self.direct_bank = self.environment.create_bank(self.source_count)
|
||||
else:
|
||||
self.convolver = MultiSourcePartitionedConvolver(self.source_count, block_size=self.block_samples,
|
||||
max_filter_taps=self.field.fft_size)
|
||||
self.direct_delay = _StereoDelay(max(0, self.latency_samples-self.implementation_latency-max(self.origin, 0)))
|
||||
lowpass = signal.firwin(129, 150., fs=48000)
|
||||
h = np.zeros((2, self.field.fft_size), dtype=np.float64)
|
||||
start = self.latency_samples-64
|
||||
if start < 0 or start+len(lowpass) > self.field.fft_size:
|
||||
raise ValueError("LFE latency exceeds filter budget")
|
||||
h[:, start:start+len(lowpass)] = lowpass/math.sqrt(2)
|
||||
self.lfe_filter = h
|
||||
|
||||
def spectrum_at(self, state):
|
||||
if self.compiled:
|
||||
return self.field.spectrum_at(state)
|
||||
key = (tuple(state.direction), state.ear_distance_m, state.centre_weight)
|
||||
if key in self._spectra:
|
||||
self._spectra.move_to_end(key)
|
||||
return self._spectra[key].copy()
|
||||
value = self.field.spectrum(state.direction)*radial_ratio(self.field, state.direction, state.ear_distance_m)
|
||||
value = value*state.centre_weight+(1-state.centre_weight)*self.field.centre_spectrum
|
||||
self._spectra[key] = value.copy()
|
||||
if len(self._spectra) > 128:
|
||||
self._spectra.popitem(last=False)
|
||||
return value
|
||||
|
||||
def _compiled_lfe(self):
|
||||
from rosella_direct import special_lfe_direct
|
||||
return special_lfe_direct().gains
|
||||
|
||||
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
|
||||
self._last[source] = key
|
||||
first = self.processed_input_samples == 0
|
||||
if special_lfe:
|
||||
self._send_targets[source] = 0.
|
||||
if self.compiled:
|
||||
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.convolver.set_filter(source, self.lfe_filter*gain, fade_blocks=0 if first else 1)
|
||||
else:
|
||||
state = self.environment.map_position(position)
|
||||
self.distance_states[source] = state
|
||||
self._send_targets[source] = state.room_send*gain
|
||||
if self.compiled:
|
||||
evaluation, ratio = self.field.bands(state)
|
||||
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)*ratio
|
||||
transfer *= state.centre_weight*state.direct_gain*self.level_calibration*gain
|
||||
paths = [HybridPath("direct", slots, transfer)]
|
||||
if state.centre_weight < 1:
|
||||
delay = max(0, self.origin)
|
||||
centre = self.field.centre_band*np.exp(-2j*np.pi*(delay % 64)*f/48000)
|
||||
centre = np.tile(centre, (2, 1))*(1-state.centre_weight)*state.direct_gain*self.level_calibration*gain
|
||||
paths.append(HybridPath("centre", np.full(2, delay//64, dtype=np.int64), centre))
|
||||
self.paths.set_paths(source, paths, fade_slots=0 if first or not fade else 2)
|
||||
else:
|
||||
spectrum = self.spectrum_at(state)*state.direct_gain*self.level_calibration*gain
|
||||
hrir = np.roll(np.fft.irfft(spectrum, self.field.fft_size), self.field.common_latency_samples, axis=-1)
|
||||
self.convolver.set_filter(source, hrir, fade_blocks=0 if first or not fade else 1)
|
||||
if first:
|
||||
self._sends[source] = self._send_targets[source]
|
||||
self.parameter_updates += 1
|
||||
|
||||
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) % self.block_samples 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), self.block_samples):
|
||||
block = values[start:start+self.block_samples]
|
||||
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))
|
||||
amount = np.array([.5, 1.])[:, None]
|
||||
sends = self._sends+(self._send_targets-self._sends)*amount
|
||||
room_input = np.einsum("tsf,ts->tf", hybrid, sends)
|
||||
if self.compiled:
|
||||
self._pending_direct.append(self.paths.process(hybrid))
|
||||
else:
|
||||
self._pending_direct.append(self.convolver.process(block))
|
||||
self._pending_room.append(room_input)
|
||||
self._sends[:] = self._send_targets
|
||||
self.processed_input_samples += self.block_samples
|
||||
if len(self._pending_room)*self.block_samples < self.environment_batch_samples:
|
||||
continue
|
||||
room = self.room.process_chunk(np.concatenate(self._pending_room))
|
||||
time = self.bank.qmf_synthesis.process_chunk(self.bank.hybrid_synthesis.process_chunk(room))
|
||||
wet = time.transpose(0, 2, 1).reshape(-1, 2)
|
||||
direct = np.concatenate(self._pending_direct)
|
||||
if self.compiled:
|
||||
direct = self.direct_bank.qmf_synthesis.process_chunk(
|
||||
self.direct_bank.hybrid_synthesis.process_chunk(direct))
|
||||
direct = direct.transpose(0, 2, 1).reshape(-1, 2)
|
||||
output = self.direct_delay.process(direct)+self.room_delay.process(wet)
|
||||
self._pending_direct.clear()
|
||||
self._pending_room.clear()
|
||||
skip = min(len(output), self._latency_remaining)
|
||||
self._latency_remaining -= skip
|
||||
outputs.append(output[skip:]*self.output_gain)
|
||||
output = np.concatenate(outputs) if outputs else np.empty((0, 2))
|
||||
self.output_samples += len(output)
|
||||
return output
|
||||
|
||||
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.field.fft_size)/128)*128)
|
||||
|
||||
def finish(self, *, tail_seconds=5.):
|
||||
if self.finished:
|
||||
return np.empty((0, 2))
|
||||
count = self.finish_output_capacity(tail_seconds)
|
||||
pending = len(self._pending_room)*self.block_samples
|
||||
padded = int(math.ceil((count+pending)/self.environment_batch_samples)*self.environment_batch_samples)-pending
|
||||
expected = max(0, self.processed_input_samples+count-self.latency_samples-self.output_samples)
|
||||
output = self.process(np.zeros((padded, self.source_count)))
|
||||
self.output_samples -= max(0, len(output)-expected)
|
||||
output = output[:expected]
|
||||
self.finished = True
|
||||
return output
|
||||
|
||||
def info(self):
|
||||
distances = [s.distance_m for s in self.distance_states if s is not None]
|
||||
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.environment.info(),
|
||||
level_calibration=self.level_calibration,
|
||||
latency_compensated_samples=self.latency_samples,
|
||||
processing_batch_samples=self.environment_batch_samples,
|
||||
recording_origin_compensated_samples=self.origin,
|
||||
current_reference_distances_m=distances,
|
||||
parameter_updates=self.parameter_updates, direct_representation=("77-band" if self.compiled else "full-HRIR"),
|
||||
radial_model="approximate-rigid-sphere-ratio", precision="float64/complex128")
|
||||
@@ -0,0 +1,123 @@
|
||||
"""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