diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e33f10..00e39a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,9 +92,13 @@ set(JOC_REUSED_SOURCES set(JOC_INTERNAL_SOURCES src/adm/adm_metadata.cpp src/adm/adm_tracks.cpp - src/binaural/binaural_reference.cpp src/binaural/binaural_runtime.cpp - src/binaural/sofa_reference_runtime.cpp + src/binaural/direct_sofa_runtime.cpp + src/binaural/point_source.cpp + src/binaural/reference_distance.cpp + src/binaural/room_tables.cpp + src/binaural/shoebox_room.cpp + src/binaural/sofa_room.cpp src/binaural/sofa_binaural_renderer.cpp src/eac3_transport/eac3_reader.cpp src/emdf/emdf_parser.cpp diff --git a/docs/binaural.en.md b/docs/binaural.en.md index 0817396..c4acc54 100644 --- a/docs/binaural.en.md +++ b/docs/binaural.en.md @@ -26,44 +26,73 @@ SOFA full HRIR [M,2,N] real-SH field renderer; passing `--hrtf-cache-policy` / `--hrtf-cache-dir` selects the legacy "SOFA -> `.jochrtf`" compile path. -## Reference environment +## Point-source reference scale -Direct SOFA rendering needs a reference-environment configuration -(`--reference-config`, default `/local/binaural_reference.json`). It names -the Rosella `.personalized_headphone` model together with its SHA-256, the -`profile_index` used as the level anchor (2 = far, 3 = mid), the room impulse -slots (4096 by default) and the number of level-calibration directions (128). - -Every SOFA direction is first mapped into the reference reproduction through the -model's distance profile: +Near/Mid/Far use reference distances of 1.00000465 / 2.19327927 / 6.40177584 m; +`--source-reference-distance-m` overrides the selected mode. For an ADM +right/front/up position `p = (x, y, z)`: ```text -ADM position -> int16 metadata grid -> distance profile - -> unit direction + distance scale - -> direct gain a = 1/sqrt(1+coupling*(h0*d)^2) (1 for near) - -> room send = h0*d*a (0 for near) - -> near-centre regularization: ear distance = max(d, minimum_distance) +q_SOFA = (y, -x, z) / ||p|| +d_nominal = reference_distance_m * ||p|| +H_render = H_selected_shell(q_SOFA) ``` -`near|mid|far` (`--binaural-mode`) selects one of the model's three distance -profiles; level calibration always uses the configured `profile_index` as its -anchor, so all three modes share one level. +The reference distance only gives object coordinates a nominal metric scale. It +does not turn a 1.2 m measurement radius into a 2.193 m ground truth and does not +rewrite the SOFA metadata. The default direct mode adds no 1/r attenuation, no +head-sphere distance ratio, no synthetic centre response, no room send and no +target-distance propagation delay; programme gain and the SOFA response scale +are preserved. -The direct path is the interpolated, delay-aligned full HRIR; the room send only -feeds the reference room model (the LFE channel never does). Their latencies come -from the larger of the field (FFT/2 plus the recording origin) and the bank -(961), and are removed once before the output. +A coincident source has no defined direction, so the object keeps its last valid +direction, or front when there is no history. Finite positions outside the unit +box keep their metadata distance factor and are not clamped. + +## Optional project room + +`--sofa-room` is off by default and reuses the project's own room algorithms: +six first-order image reflections plus a shared FDN tail with a four-channel +orthogonal Hadamard feedback matrix, two all-pass diffusers and low-pass damping. +It is a listening preset of this project, not a measured BRIR or an external +standard room, and it does not use the Rosella room tables. + +When enabled, Near/Mid default to the `compact` room and Far to the `legacy` +room; `--sofa-room-preset compact|legacy` selects explicitly. The compact +geometry follows +[Zahorik 2009, Table 1 model 9](https://pmc.ncbi.nlm.nih.gov/articles/PMC2730711/), +keeping the original image/FDN algorithm while scaling late delay, diffusion +time and RT60 by the `V/S` ratio; the parameters and their source are recorded +in the asset metadata. Only the geometry is borrowed, not that paper's BRIR +algorithm. + +The full-HRIR route queries an HRIR for each of the six arrival directions and +applies the fractional delay as a phase ramp on the full frequency grid; the +extra flight time of a reflection is `(L_path - L_direct)/c`, preserving the +original `Data.Delay`. Direct sound, reflections and the late send use the +project's original gain formulas. + +The late FDN is rendered at 48 kHz with a unit impulse into a two-channel float64 +FIR cut at 2*RT60: 37,383 taps (RT60 about 0.3894 s) for the compact room and +81,600 taps (RT60 0.85 s) for the legacy one. The FIR is produced at startup by +the four-line Hadamard feedback loop, the two all-pass diffusers and the low-pass +damping in `src/binaural/room_tables.cpp`; no audio asset ships with the binary. +At runtime the same LTI system is applied by PCM-domain FFT convolution in +1536-sample batches, the early reflections still use the selected SOFA, and the +LFE channel never enters the room. These states are neither generated nor kept +when the room is off, and batch padding is trimmed on flush. ## Inputs -A SOFA input renders directly from its full HRIRs and only needs the SOFA file and -the reference-environment configuration: +A SOFA input renders directly from its full HRIRs and only needs the SOFA file: ```powershell joc_cli input.m4a --binaural ` --sofa-hrtf HRTF\subject.sofa ` - --reference-config local\binaural_reference.json ` --binaural-mode mid --sofa-interpolation lf_logmag_phase + +joc_cli input.m4a --binaural --sofa-hrtf HRTF\subject.sofa --sofa-room +joc_cli input.m4a --binaural --sofa-hrtf HRTF\subject.sofa --sofa-room --sofa-room-preset legacy ``` A `.jochrtf` input (and `.personalized_headphone`) also needs the shared filterbank @@ -84,7 +113,8 @@ magnitude correction). The library exposes the same fields: `joc_task_config` for a file task, `joc_stream_config` for streaming, where `hrtf_sofa_path` (or `hrtf_path`), -`reference_config_path`, `sofa_interpolation`, `hrtf_compile`, `kernels_path`, +`sofa_interpolation`, `hrtf_radius_m`, `source_reference_distance_m`, `sofa_room`, +`sofa_room_preset`, `sofa_room_asset_dir`, `hrtf_compile`, `kernels_path`, `binaural_mode` and `binaural_tail_seconds` configure the binaural path. The `.jochrtf` file is an **input**, not a product of this library: compiling it from diff --git a/docs/binaural.md b/docs/binaural.md index 8e8a5d1..da25fa1 100644 --- a/docs/binaural.md +++ b/docs/binaural.md @@ -6,55 +6,77 @@ JustOneCacophony 的双耳后端支持三种 HRTF 来源:`SimpleFreeFieldHRIR` 杜比官方软件个性化扫描导出的 Rosella `.personalized_headphone`(JSON 解析由本项目 自行实现,不调用杜比软件),以及 `.jochrtf`(JOC compiled HRTF cache)。 -SOFA 默认保留完整 HRIR,按“参考环境”直连渲染,不再编译成方向场: +SOFA 默认保留完整 HRIR,按点声源参考直连渲染,不编译成方向场: ```text SOFA 完整 HRIR [M,2,N] -> CanonicalHrtf(48 kHz / 单 radius shell) -> 延迟对齐的复数频谱(200-1500 Hz 相位拟合,含 Data.Delay) -> 球面三角形重心插值(lf_logmag_phase 或 lf_mca_zero) - -> 刚性球径向修正(按参考环境距离) + -> 点声源参考尺度(方向 + 档位参考距离,无径向修正) -> 逐对象 partitioned convolution(block 128,FFT = max(2048, ≥2×support)) - -> 参考环境的递归房间模型 + -> 可选项目房间(六路一阶镜像 + 共享 FDN 晚期 FIR) -> stereo float64(统一扣除 latency) ``` `.jochrtf` 仍是显式输入(`--compiled-hrtf-cache`),走球谐方向场渲染;显式给出 `--hrtf-cache-policy` / `--hrtf-cache-dir` 会回到旧的“SOFA → `.jochrtf`”编译路径。 -## 参考环境 +## 点声源参考尺度 -SOFA 直连渲染需要一个参考环境配置(`--reference-config`,默认 -`/local/binaural_reference.json`)。配置给出 Rosella `.personalized_headphone` -模型的路径与 SHA-256、`profile_index`(2=far、3=mid,作为 level 校准锚点)、 -`room_impulse_slots`(默认 4096)与 `level_calibration_directions`(默认 128)。 - -渲染时每个 SOFA 方向先经该模型的 distance profile 映射成参考环境中的位置: +Near/Mid/Far 的参考距离分别为 1.00000465 / 2.19327927 / 6.40177584 m,可用 +`--source-reference-distance-m` 覆盖所选档位。对于 ADM 右/前/上坐标 `p=(x,y,z)`: ```text -ADM position -> int16 metadata grid -> distance profile - -> direction(单位向量)+ 距离 scale - -> direct gain a = 1/sqrt(1+coupling·(h0·d)^2)(near 恒为 1) - -> room send = h0·d·a(near 恒为 0) - -> 近中心正则化:ear distance 取 max(d, minimum_distance) +q_SOFA = (y, -x, z) / ||p|| +d_nominal = reference_distance_m * ||p|| +H_render = H_selected_shell(q_SOFA) ``` -`near|mid|far` 选择该模型的三个 distance profile 之一(`--binaural-mode`),level 校准 -始终用配置里的 `profile_index` 作为锚点,因此三种模式共享同一个 level。 +参考距离只给对象坐标规定标称米制尺度,不会把测量半径为 1.2 m 的 HRIR 合成成 2.193 m +的真值,也不改动 SOFA 测量元数据。默认直达声模式不额外应用 1/r 衰减、球头距离比值、 +人工中心响应、房间发送或目标距离传播延迟;节目增益与 SOFA 响应刻度保持不变。 -指向同一声道的直达声是延迟对齐后的完整 HRIR 插值结果,房间 send 只进参考环境的 -房间模型(LFE 声道不进房间)。两者的 latency 由 field(FFT/2 与 recording origin) -和 bank(961)中的较大者决定,输出前统一扣除。 +声源与听者重合时方向没有定义,沿用该对象最后一个有效方向;没有历史时取正前方。 +有限盒外坐标保留元数据距离因子,不裁回单位盒。 + +## 可选项目房间 + +`--sofa-room` 默认关闭,复用项目原有的房间算法:六个一阶镜像反射,以及四路正交 +Hadamard 反馈、两级全通扩散、低通阻尼的共享 FDN 晚期。它是项目自有的听感预设, +不是测量 BRIR 或外部标准房间,也不依赖 Rosella 房间表。 + +启用后 Near/Mid 默认使用 `compact` 紧凑房间,Far 默认使用 `legacy` 原大房间; +`--sofa-room-preset compact|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`。直接声、反射与晚期发送使用项目原有的增益公式。 + +晚期 FDN 在 48 kHz 下用单位脉冲生成双声道 float64 FIR,长度截到 2×RT60:紧凑版 +37,383 点、RT60 参数约 0.3894 s,原版 81,600 点、RT60 参数 0.85 s。FIR 由 +`src/binaural/room_tables.cpp` 里的四路 Hadamard 反馈、两级全通扩散与低通阻尼在启动时 +现场渲染,不读取任何随包发布的音频资产。运行时按 1536 样本批次在 PCM 域做 FFT 卷积, +早期反射仍使用所选 SOFA;LFE 声道不进房间。房间关闭时既不生成也不保留这些状态, +排空时裁去批处理填充。 + +指向同一声道的直达声是延迟对齐后的完整 HRIR 插值结果;文件任务的 latency 由 field +(FFT/2 与 recording origin)决定,输出前统一扣除。 ## 输入接口 -SOFA 输入直接按完整 HRIR 渲染,只需要 SOFA 与参考环境配置: +SOFA 输入直接按完整 HRIR 渲染,只需要 SOFA 本身: ```powershell joc_cli input.m4a --binaural ` --sofa-hrtf HRTF\subject.sofa ` - --reference-config local\binaural_reference.json ` --binaural-mode mid --sofa-interpolation lf_logmag_phase + +joc_cli input.m4a --binaural --sofa-hrtf HRTF\subject.sofa --sofa-room +joc_cli input.m4a --binaural --sofa-hrtf HRTF\subject.sofa --sofa-room --sofa-room-preset legacy ``` `.jochrtf` 输入(以及 `.personalized_headphone`)需要共享滤波器组表,两者都是只读输入: @@ -71,7 +93,8 @@ joc_cli input.m4a --binaural ` `lf_logmag_phase`(默认,对数幅度 + 相位)或 `lf_mca_zero`(听觉带幅度修正)。 库接口使用同一组字段:文件任务用 `joc_task_config`,流式用 `joc_stream_config`, -其中 `hrtf_sofa_path`(或 `hrtf_path`)、`reference_config_path`、`sofa_interpolation`、 +其中 `hrtf_sofa_path`(或 `hrtf_path`)、`sofa_interpolation`、`hrtf_radius_m`、 +`source_reference_distance_m`、`sofa_room`、`sofa_room_preset`、`sofa_room_asset_dir`、 `hrtf_compile`、`kernels_path`、`binaural_mode`、`binaural_tail_seconds` 决定双耳通路。 `.jochrtf` 是**输入**而不是本库的产物:从 SOFA 或测量数据编译该缓存属于工具链的职责, diff --git a/include/joc_core.h b/include/joc_core.h index ee28f69..e9407b8 100644 --- a/include/joc_core.h +++ b/include/joc_core.h @@ -39,7 +39,7 @@ #define JOC_ABI_VERSION 5u #define JOC_FRAME_PARAMS_VERSION 1u #define JOC_EMDF_INFO_VERSION 1u -#define JOC_TASK_CONFIG_VERSION 4u +#define JOC_TASK_CONFIG_VERSION 5u #define JOC_TASK_RESULT_VERSION 2u #define JOC_EVENT_VERSION 1u @@ -440,9 +440,11 @@ typedef struct joc_task_config { uint32_t hrtf_cache_policy; /* joc_hrtf_cache_policy */ uint32_t rosella_filterbank; /* joc_rosella_filterbank; 0 = PUBLIC */ double hrtf_radius_m; /* SOFA shell radius; <= 0 = automatic */ - /* Reference environment for the direct SOFA path (config version 3). */ - const char* reference_config_path; /* NULL = local/binaural_reference.json */ + /* Direct SOFA rendering (config version 5). */ uint32_t sofa_interpolation; /* joc_sofa_interpolation */ + double source_reference_distance_m; /* > 0 overrides the mode's reference distance */ + uint32_t sofa_room; /* non-zero enables the project room */ + const char* sofa_room_preset; /* "compact" or "legacy"; NULL = by mode */ /* Nonzero keeps the legacy SOFA compile step (hrtf_cache_policy/hrtf_cache_dir * decide where the .jochrtf lands); zero renders the SOFA directly. */ uint32_t hrtf_compile; diff --git a/include/joc_stream.h b/include/joc_stream.h index b942f4f..27c02a5 100644 --- a/include/joc_stream.h +++ b/include/joc_stream.h @@ -74,6 +74,10 @@ typedef struct joc_stream_config { const char* hrtf_cache_dir; /* disk cache directory for SOFA compilation */ uint32_t hrtf_cache_policy; /* joc_hrtf_cache_policy: 0 none, 1 memory, 2 disk */ double hrtf_radius_m; /* SOFA measurement-radius shell, default 1.0 */ + /* Direct SOFA rendering: the same fields joc_task_config carries. */ + double source_reference_distance_m; /* > 0 overrides the mode's reference distance */ + uint32_t sofa_room; /* non-zero enables the project room */ + const char* sofa_room_preset; /* "compact" or "legacy"; NULL = by mode */ } joc_stream_config; typedef struct joc_stream_buffer { diff --git a/src/api/joc_stream_api.cpp b/src/api/joc_stream_api.cpp index bca9fa7..43e125c 100644 --- a/src/api/joc_stream_api.cpp +++ b/src/api/joc_stream_api.cpp @@ -50,6 +50,9 @@ joc::stream::Config to_config(const joc_stream_config& config) { if (config.rosella_filterbank != 0u) { out.rosella_filterbank = config.rosella_filterbank; } + out.source_reference_distance_m = config.source_reference_distance_m; + out.sofa_room = config.sofa_room != 0u; + if (config.sofa_room_preset != nullptr) { out.sofa_room_preset = config.sofa_room_preset; } return out; } diff --git a/src/binaural/binaural_reference.cpp b/src/binaural/binaural_reference.cpp deleted file mode 100644 index f617eda..0000000 --- a/src/binaural/binaural_reference.cpp +++ /dev/null @@ -1,393 +0,0 @@ - -#include "binaural/binaural_reference.h" - -#include -#include -#include -#include -#include -#include - -#include "foundation/fft.h" -#include "foundation/fs_utf8.h" -#include "foundation/mini_json.h" -#include "foundation/sha256.h" -#include "hrtf/sofa_field.h" - -namespace joc::binaural { -namespace { - -constexpr double kPi = 3.141592653589793238462643383279502884; -constexpr double kMaximumExtension = 50.1; - -Status reference_error(const char* message) { - return Status::fail(JOC_ERR_INVALID_CONFIG, joc::stage::kRender, message); -} - -bool read_text_file(const std::string& path, std::string* out) { - std::ifstream stream(fs_utf8::to_path(path), std::ios::binary); - if (!stream) { - return false; - } - std::ostringstream buffer; - buffer << stream.rdbuf(); - *out = buffer.str(); - return true; -} - -bool read_binary_file(const std::string& path, std::string* out) { - return read_text_file(path, out); -} - -std::string directory_of(const std::string& path) { - const std::size_t slash = path.find_last_of("/\\"); - return slash == std::string::npos ? std::string() : path.substr(0, slash + 1); -} - -std::string join_path(const std::string& directory, const std::string& name) { - if (directory.empty()) { - return name; - } - const char last = directory.back(); - return (last == '/' || last == '\\' ? directory : directory + "/") + name; -} - -std::vector parse_number_list(const std::string& raw) { - std::vector values; - std::size_t index = 0; - while (index < raw.size()) { - if (raw[index] == '-' || raw[index] == '+' || raw[index] == '.' || - (raw[index] >= '0' && raw[index] <= '9')) { - std::size_t end = index; - while (end < raw.size() && - (raw[end] == '-' || raw[end] == '+' || raw[end] == '.' || - raw[end] == 'e' || raw[end] == 'E' || - (raw[end] >= '0' && raw[end] <= '9'))) { - ++end; - } - values.push_back(std::strtod(raw.substr(index, end - index).c_str(), nullptr)); - index = end; - continue; - } - ++index; - } - return values; -} - -} // namespace - -std::string ReferenceEnvironment::default_reference_config_path() { - return std::string("local/binaural_reference.json"); -} - -Status ReferenceEnvironment::load(const std::string& config_path, int mode_index, - bool mode_specified) { - config_path_ = config_path.empty() ? default_reference_config_path() : config_path; - std::string text; - if (!read_text_file(config_path_, &text)) { - return Status::fail(JOC_ERR_HRTF_NOT_FOUND, joc::stage::kRender, - "reference configuration not found: " + config_path_); - } - configuration_sha256_ = joc::crypto::sha256_hex(text.data(), text.size()); - - std::vector members; - std::string error; - if (!json::parse_object(text, &members, &error)) { - return reference_error("reference configuration is not a JSON object"); - } - std::string schema; - const json::Member* schema_member = json::find(members, "schema"); - if (schema_member == nullptr || !json::as_string(*schema_member, &schema) || - schema != "binaural-reference-v1") { - return reference_error("unsupported reference configuration schema"); - } - std::string model_path; - std::string expected_sha; - const json::Member* model_member = json::find(members, "model_path"); - const json::Member* sha_member = json::find(members, "model_sha256"); - if (model_member == nullptr || sha_member == nullptr || - !json::as_string(*model_member, &model_path) || - !json::as_string(*sha_member, &expected_sha)) { - return reference_error("reference configuration needs model_path and model_sha256"); - } - long long profile_index = 0; - const json::Member* profile_member = json::find(members, "profile_index"); - if (profile_member == nullptr || !json::as_integer(*profile_member, &profile_index)) { - return reference_error("reference configuration needs profile_index"); - } - calibration_profile_index_ = static_cast(profile_index); - if (calibration_profile_index_ != kProfileFar && - calibration_profile_index_ != kProfileMid) { - return reference_error("unsupported reference profile"); - } - const json::Member* profile_id_member = json::find(members, "profile_id"); - if (profile_id_member != nullptr) { - json::as_string(*profile_id_member, &profile_id_); - } - long long slots = 4096; - if (const json::Member* member = json::find(members, "room_impulse_slots")) { - json::as_integer(*member, &slots); - } - long long directions = 128; - if (const json::Member* member = json::find(members, "level_calibration_directions")) { - json::as_integer(*member, &directions); - } - if (slots < 128 || slots > 16384 || directions < 16 || directions > 2048) { - return reference_error("reference configuration exceeds supported bounds"); - } - room_impulse_slots_ = static_cast(slots); - calibration_directions_ = static_cast(directions); - - // The model path is relative to the configuration file unless absolute. - std::string resolved = model_path; - if (!(model_path.size() > 1 && (model_path[1] == ':' || model_path[0] == '/' || - model_path[0] == '\\'))) { - resolved = join_path(directory_of(config_path_), model_path); - } - std::string model_bytes; - if (!read_binary_file(resolved, &model_bytes)) { - return Status::fail(JOC_ERR_HRTF_NOT_FOUND, joc::stage::kRender, - "reference model not found: " + resolved); - } - model_sha256_ = joc::crypto::sha256_hex(model_bytes.data(), model_bytes.size()); - std::string lower_expected = expected_sha; - std::transform(lower_expected.begin(), lower_expected.end(), lower_expected.begin(), - [](unsigned char value) { return static_cast(std::tolower(value)); }); - std::string lower_actual = model_sha256_; - std::transform(lower_actual.begin(), lower_actual.end(), lower_actual.begin(), - [](unsigned char value) { return static_cast(std::tolower(value)); }); - if (lower_actual != lower_expected) { - return reference_error("reference data hash mismatch"); - } - const Status status = hrtf::load_personalized_headphone(resolved, &model_); - if (!status.ok()) { - return status; - } - if (model_.sample_rate != 48000) { - return reference_error("unsupported reference profile"); - } - // Cartesian reference configurations only. - if (model_.header_integer_fields[0] != 0) { - return reference_error("reference-v1 requires a Cartesian mapping"); - } - - mode_name_ = mode_specified - ? (mode_index == kProfileNear ? "near" - : (mode_index == kProfileFar ? "far" : "mid")) - : (calibration_profile_index_ == kProfileFar ? "far" : "mid"); - profile_index_ = mode_specified ? mode_index - : (calibration_profile_index_ == kProfileFar ? kProfileFar - : kProfileMid); - if (profile_index_ != kProfileNear && profile_index_ != kProfileFar && - profile_index_ != kProfileMid) { - return reference_error("reference mode must be near, mid, or far"); - } - - header_constant_ = model_.header_float_scalars[0]; - coupling_ = model_.header_float_scalars[1]; - if (!(header_constant_ > 0.0) || !(coupling_ > 0.0)) { - return reference_error("invalid reference energy coupling"); - } - const hrtf::RosellaDistanceProfile& profile = - model_.profiles[calibration_profile_index_]; - minimum_distance_m_ = static_cast(profile.minimum_normalized_radius) * - static_cast(profile.distance_scale_m); - const double origin[3] = {0.0, 1.0, 0.0}; - ReferencePosition position; - const Status mapped = map_position(origin, &position); - if (!mapped.ok()) { - return mapped; - } - reference_distance_m_ = position.distance_m; - - if (const json::Member* member = json::find(members, "compiled_receiver_positions_m")) { - compiled_receivers_m_ = parse_number_list(member->raw); - if (compiled_receivers_m_.size() != 6u) { - compiled_receivers_m_.clear(); - } - } - return Status::success(); -} - -Status ReferenceEnvironment::map_position(const double position[3], - ReferencePosition* out) const { - return map_position(position, profile_index_, out); -} - -Status ReferenceEnvironment::map_position(const double position[3], int profile_index, - ReferencePosition* out) const { - if (out == nullptr) { - return reference_error("reference position output is null"); - } - for (int axis = 0; axis < 3; ++axis) { - if (!std::isfinite(position[axis])) { - return reference_error("reference position must contain three finite values"); - } - } - double maximum = 0.0; - for (int axis = 0; axis < 3; ++axis) { - maximum = std::max(maximum, std::fabs(position[axis])); - } - const double extension = std::max(1.0, maximum); - if (extension > kMaximumExtension + 1e-9) { - return reference_error("position exceeds the finite metadata domain"); - } - const double scaled[3] = {position[0] / extension, position[1] / extension, - position[2] / extension}; - const int profile = profile_index < 0 ? profile_index_ : profile_index; - hrtf::ProfileGeometry geometry; - Status status = hrtf::profile_geometry(model_, scaled, profile, &geometry); - if (!status.ok()) { - return status; - } - const hrtf::RosellaDistanceProfile& distance_profile = model_.profiles[profile]; - const double scale = static_cast(distance_profile.distance_scale_m); - const double distance = scale * geometry.radius * extension; - const double minimum = - static_cast(distance_profile.minimum_normalized_radius) * scale; - const double regularized = std::max(distance, minimum); - const double alpha = regularized > 0.0 ? distance / regularized : 0.0; - const double effective = header_constant_ * distance; - double gain = 1.0; - double send = 0.0; - if (profile != kProfileNear) { - gain = 1.0 / std::sqrt(1.0 + coupling_ * effective * effective); - send = effective * gain; - } - for (int axis = 0; axis < 3; ++axis) { - out->direction[axis] = geometry.direction[axis]; - } - out->distance_m = distance; - out->ear_distance_m = regularized; - out->centre_weight = alpha; - out->direct_gain = gain; - out->room_send = send; - return Status::success(); -} - -Status ReferenceEnvironment::create_room(std::unique_ptr* out) const { - if (out == nullptr) { - return reference_error("room output is null"); - } - std::unique_ptr room = std::make_unique(); - Status status = room->configure(model_, room_impulse_slots_); - if (!status.ok()) { - return status; - } - *out = std::move(room); - return Status::success(); -} - -std::unique_ptr ReferenceEnvironment::create_analysis_bank( - std::size_t channels) const { - return hrtf::RosellaBank::create(hrtf::RosellaBankKind::Public, channels); -} - -std::unique_ptr ReferenceEnvironment::create_synthesis_bank() const { - return hrtf::RosellaBank::create(hrtf::RosellaBankKind::Public, 2u); -} - -Status ReferenceEnvironment::calibrate(std::size_t fft_size, - const std::vector& frequencies, - const SpectrumAt& spectrum_at, double* level) const { - if (level == nullptr || frequencies.empty()) { - return reference_error("calibration needs a spectrum callback and frequencies"); - } - const std::vector dictionary = - hrtf::hybrid_gain_synthesis_dictionary_for_check(fft_size); - if (dictionary.size() != fft_size * 154u) { - return reference_error("hybrid gain dictionary has the wrong shape"); - } - const std::vector field_left = hrtf::rosella_logical_field(model_.field_left_padded); - const std::vector field_right = hrtf::rosella_logical_field(model_.field_right_padded); - - // Fibonacci sphere: equal-area calibration directions. - const int count = calibration_directions_; - std::vector band_bins; - std::vector weights; - for (std::size_t bin = 0; bin < frequencies.size(); ++bin) { - if (frequencies[bin] >= 100.0 && frequencies[bin] <= 16000.0) { - band_bins.push_back(bin); - weights.push_back(1.0 / frequencies[bin]); - } - } - if (band_bins.empty()) { - return reference_error("calibration band is empty"); - } - const std::size_t bins = frequencies.size(); - double reference_energy = 0.0; - double new_energy = 0.0; - std::vector> reference(2 * bins); - std::vector> target; - std::vector parameters(2 * 154); - for (int index = 0; index < count; ++index) { - const double z = 1.0 - 2.0 * (static_cast(index) + 0.5) / count; - const double phi = static_cast(index) * kPi * (3.0 - std::sqrt(5.0)); - const double radial = std::sqrt(std::max(0.0, 1.0 - z * z)); - const double direction[3] = {-radial * std::sin(phi), radial * std::cos(phi), z}; - const double adm[3] = {-direction[1], direction[0], direction[2]}; - ReferencePosition state; - Status status = map_position(adm, calibration_profile_index_, &state); - if (!status.ok()) { - return status; - } - hrtf::DirectResult result; - status = hrtf::direct_and_room_send(model_, field_left, field_right, adm, - calibration_profile_index_, &result); - if (!status.ok()) { - return status; - } - for (int ear = 0; ear < 2; ++ear) { - for (int band = 0; band < 77; ++band) { - parameters[static_cast(ear * 154 + 2 * band)] = - result.gains[ear][band].real(); - parameters[static_cast(ear * 154 + 2 * band + 1)] = - result.gains[ear][band].imag(); - } - } - for (int ear = 0; ear < 2; ++ear) { - std::vector time(fft_size, 0.0); - for (std::size_t tap = 0; tap < fft_size; ++tap) { - double sum = 0.0; - for (int parameter = 0; parameter < 154; ++parameter) { - sum += dictionary[tap * 154u + static_cast(parameter)] * - parameters[static_cast(ear * 154 + parameter)]; - } - time[tap] = sum; - } - std::vector> buffer(fft_size); - for (std::size_t tap = 0; tap < fft_size; ++tap) { - buffer[tap] = std::complex(time[tap], 0.0); - } - dsp::fft_radix2(&buffer, false); - for (std::size_t bin = 0; bin < bins; ++bin) { - reference[static_cast(ear) * bins + bin] = buffer[bin]; - } - } - status = spectrum_at(state, &target); - if (!status.ok()) { - return status; - } - if (target.size() != 2 * bins) { - return reference_error("calibration spectrum has the wrong shape"); - } - for (std::size_t index_band = 0; index_band < band_bins.size(); ++index_band) { - const std::size_t bin = band_bins[index_band]; - const double weight = weights[index_band]; - for (int ear = 0; ear < 2; ++ear) { - const std::size_t offset = static_cast(ear) * bins + bin; - reference_energy += std::norm(reference[offset]) * weight; - new_energy += - std::norm(target[offset] * state.direct_gain) * weight; - } - } - } - if (!(reference_energy > 1e-24) || !(new_energy > 1e-24) || - !std::isfinite(new_energy)) { - return reference_error("reference level calibration has no finite signal energy"); - } - *level = std::sqrt(reference_energy / new_energy); - return Status::success(); -} - -} // namespace joc::binaural diff --git a/src/binaural/binaural_reference.h b/src/binaural/binaural_reference.h deleted file mode 100644 index e52607e..0000000 --- a/src/binaural/binaural_reference.h +++ /dev/null @@ -1,97 +0,0 @@ - -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -#include "foundation/status.h" -#include "hrtf/full_hrir_field.h" -#include "hrtf/rosella_bank.h" -#include "hrtf/rosella_model.h" -#include "hrtf/rosella_reference.h" - -// Configured reference environment for the SOFA renderer, ported from the -// reference binaural_reference.py: the JSON configuration names the personalized -// model that defines the reference reproduction (room, bank and level anchor), -// and every SOFA direction is mapped through that model's distance profile. -namespace joc::binaural { - -// Profile indices, as in the reference rosella_direct module. -inline constexpr int kProfileNear = 1; -inline constexpr int kProfileFar = 2; -inline constexpr int kProfileMid = 3; - -struct ReferencePosition { - double direction[3] = {1.0, 0.0, 0.0}; - double distance_m = 0.0; // physical distance implied by the metadata - double ear_distance_m = 0.0; // distance floored at the near-centre minimum - double centre_weight = 1.0; // direction vs centre blend - double direct_gain = 1.0; - double room_send = 0.0; -}; - -class ReferenceEnvironment { -public: - // Loads `config_path` (JSON) and the model it names, verifying its SHA-256. - // An empty path selects `default_reference_config_path()`. - Status load(const std::string& config_path, int mode_index, bool mode_specified); - - int profile_index() const { return profile_index_; } - int calibration_profile_index() const { return calibration_profile_index_; } - int minimum_profile_index() const { return calibration_profile_index_; } - const std::string& mode_name() const { return mode_name_; } - const hrtf::RosellaModel& model() const { return model_; } - const std::string& profile_id() const { return profile_id_; } - const std::string& model_sha256() const { return model_sha256_; } - const std::string& configuration_sha256() const { return configuration_sha256_; } - double reference_distance_m() const { return reference_distance_m_; } - double minimum_distance_m() const { return minimum_distance_m_; } - int room_impulse_slots() const { return room_impulse_slots_; } - int calibration_directions() const { return calibration_directions_; } - const std::vector& compiled_receivers_m() const { return compiled_receivers_m_; } - - Status map_position(const double position[3], ReferencePosition* out) const; - // `profile_index` selects the distance profile; negative keeps the mode's. - Status map_position(const double position[3], int profile_index, - ReferencePosition* out) const; - - Status create_room(std::unique_ptr* out) const; - - // The public bank is the reference bank; the analysis side carries the source - // channels, the synthesis side the two ears. - std::unique_ptr create_analysis_bank(std::size_t channels) const; - std::unique_ptr create_synthesis_bank() const; - - // Raises the SOFA field to the reference level: one scalar over the equal-area - // calibration directions, shared by all three modes. - using SpectrumAt = std::function>*)>; - Status calibrate(std::size_t fft_size, const std::vector& frequencies, - const SpectrumAt& spectrum_at, double* level) const; - - static std::string default_reference_config_path(); - -private: - hrtf::RosellaModel model_; - std::string config_path_; - std::string profile_id_; - std::string model_sha256_; - std::string configuration_sha256_; - std::string mode_name_; - int calibration_profile_index_ = kProfileMid; - int profile_index_ = kProfileMid; - int room_impulse_slots_ = 4096; - int calibration_directions_ = 128; - double header_constant_ = 1.0; // header_float_scalars[0] - double coupling_ = 0.0; // header_float_scalars[1] - double minimum_distance_m_ = 0.0; - double reference_distance_m_ = 0.0; - std::vector compiled_receivers_m_; // [2][3], when the config supplies them -}; - -} // namespace joc::binaural diff --git a/src/binaural/direct_sofa_runtime.cpp b/src/binaural/direct_sofa_runtime.cpp new file mode 100644 index 0000000..851c871 --- /dev/null +++ b/src/binaural/direct_sofa_runtime.cpp @@ -0,0 +1,432 @@ +#include "binaural/direct_sofa_runtime.h" + +#include +#include +#include + +#include "foundation/fft.h" +#include "hrtf/sofa.h" +#include "hrtf/sofa_field.h" + +namespace joc::binaural { +namespace { + +constexpr double kPi = 3.141592653589793238462643383279502884; + +Status runtime_error(const char* message) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, joc::stage::kRender, message); +} + +Status render_fail(const char* message) { + return Status::fail(JOC_ERR_HRTF_FORMAT, joc::stage::kDsp, message); +} + +// One ear of a measured HRIR: inverse real transform, then undo the common delay. +void ear_impulse(const std::vector>& spectrum, std::size_t ear, + std::size_t bins, std::size_t fft_size, std::vector* out) { + std::vector> buffer(fft_size, std::complex(0.0, 0.0)); + for (std::size_t bin = 0; bin < bins; ++bin) { + buffer[bin] = spectrum[ear * bins + bin]; + } + for (std::size_t bin = 1; bin + 1 < bins; ++bin) { + buffer[fft_size - bin] = std::conj(spectrum[ear * bins + bin]); + } + dsp::fft_radix2(&buffer, true); + out->resize(fft_size); + for (std::size_t sample = 0; sample < fft_size; ++sample) { + (*out)[(sample + fft_size / 2u) % fft_size] = buffer[sample].real(); + } +} + +// scipy.signal.firwin(129, 150, fs=48000): windowed-sinc, unit DC gain. +std::vector lowpass_150(double sample_rate) { + constexpr int kTaps = 129; + const double cutoff = 150.0 / (sample_rate * 0.5); + std::vector taps(kTaps, 0.0); + double total = 0.0; + for (int index = 0; index < kTaps; ++index) { + const double position = static_cast(index) - 0.5 * (kTaps - 1); + double value = 0.0; + if (std::fabs(position) < 1e-12) { + value = cutoff; + } else { + value = cutoff * std::sin(kPi * cutoff * position) / (kPi * position); + } + const double window = 0.54 - 0.46 * std::cos(2.0 * kPi * index / (kTaps - 1)); + taps[static_cast(index)] = value * window; + total += value * window; + } + for (double& value : taps) { + value /= total; + } + return taps; +} + +} // namespace + +DirectSofaRuntime::DirectSofaRuntime() = default; +DirectSofaRuntime::~DirectSofaRuntime() = default; + +Status DirectSofaRuntime::open(const std::string& sofa_path, const DirectSofaOptions& options) { + output_gain_ = options.output_gain; + Status status = reference_.configure(options.mode, options.has_reference_distance, + options.reference_distance_m); + if (!status.ok()) { + return status; + } + hrtf::SofaHrir sofa; + status = hrtf::load_sofa(sofa_path, &sofa); + if (!status.ok()) { + return status; + } + hrtf::CanonicalHrtf canonical; + status = hrtf::canonicalize_sofa(sofa, &canonical); + if (!status.ok()) { + return status; + } + hrtf::FullHrirOptions field_options; + field_options.method = options.interpolation; + field_options.has_shell_radius = options.has_shell_radius; + field_options.shell_radius_m = options.shell_radius_m; + status = field_.build(canonical, field_options); + if (!status.ok()) { + return status; + } + fft_size_ = field_.fft_size(); + bins_ = field_.frequency_bins(); + origin_samples_ = field_.recording_origin_samples(); + if (origin_samples_ < 0) { + return runtime_error("negative recording origin requires an explicit time base"); + } + latency_samples_ = field_.common_latency_samples(); + filter_taps_ = fft_size_; + + if (options.sofa_room) { + const std::string preset = options.room_preset.empty() + ? (reference_.mode() == "far" ? "legacy" : "compact") + : options.room_preset; + if (preset != "compact" && preset != "legacy") { + return runtime_error("room preset must be compact or legacy"); + } + status = room_.configure(static_cast(kDirectSofaSources), reference_, + latency_samples_, origin_samples_, preset); + if (!status.ok()) { + return status; + } + filter_taps_ = room_.filter_budget(field_); + } + convolver_.configure(static_cast(kDirectSofaSources), kDirectSofaBlock, + filter_taps_, 2u); + status = build_lfe_filter(); + if (!status.ok()) { + return status; + } + hop_block_.assign(kDirectSofaBlock * kDirectSofaSources, 0.0); + staging_.assign(static_cast(kDirectSofaFrame) * kDirectSofaSources, 0.0); + direct_.assign(kDirectSofaBlock * 2u, 0.0); + distance_states_.assign(kDirectSofaSources, PointSourcePosition{}); + has_state_.assign(kDirectSofaSources, 0); + std::memset(last_key_, 0, sizeof(last_key_)); + pending_filters_.clear(); + return reset(); +} + +Status DirectSofaRuntime::build_lfe_filter() { + const std::vector lowpass = lowpass_150(48000.0); + lfe_filter_.assign(2u * fft_size_, 0.0); + const int start = latency_samples_ + std::max(origin_samples_, 0) - 64; + if (start < 0 || static_cast(start) + lowpass.size() > fft_size_) { + return render_fail("LFE latency exceeds the filter budget"); + } + const double scale = 1.0 / std::sqrt(2.0); + for (std::size_t ear = 0; ear < 2u; ++ear) { + for (std::size_t tap = 0; tap < lowpass.size(); ++tap) { + lfe_filter_[ear * fft_size_ + static_cast(start) + tap] = + lowpass[tap] * scale; + } + } + return Status::success(); +} + +Status DirectSofaRuntime::reset() { + convolver_.reset(); + room_.reset(); + timeline_.reset(); + output_.clear(); + pending_filters_.clear(); + std::fill(has_state_.begin(), has_state_.end(), 0); + std::memset(last_key_, 0, sizeof(last_key_)); + input_samples_ = 0; + processed_input_samples_ = 0; + parameter_updates_ = 0; + output_samples_ = 0; + latency_remaining_ = latency_samples_; + return Status::success(); +} + +void DirectSofaRuntime::schedule_filter(std::size_t source, + const std::vector& filters, std::size_t taps, + bool first, bool fade) { + if (first) { + convolver_.set_filter(source, filters.data(), taps, 0u); + return; + } + const std::int64_t due = + processed_input_samples_ + static_cast(field_.common_latency_samples()); + for (std::size_t index = pending_filters_.size(); index-- > 0;) { + if (pending_filters_[index].due != due) { + break; + } + if (pending_filters_[index].source == source) { + pending_filters_[index].filters = filters; + pending_filters_[index].taps = taps; + pending_filters_[index].fade_blocks = fade ? 1u : 0u; + return; + } + } + PendingFilter pending; + pending.due = due; + pending.source = source; + pending.filters = filters; + pending.taps = taps; + pending.fade_blocks = fade ? 1u : 0u; + pending_filters_.push_back(std::move(pending)); +} + +void DirectSofaRuntime::apply_due_filters() { + while (!pending_filters_.empty() && + pending_filters_.front().due <= processed_input_samples_) { + PendingFilter pending = std::move(pending_filters_.front()); + pending_filters_.pop_front(); + convolver_.set_filter(pending.source, pending.filters.data(), pending.taps, + pending.fade_blocks); + } +} + +Status DirectSofaRuntime::set_source(int source, const double position[3], double gain, + bool special_lfe, bool fade) { + if (source < 0 || source >= kDirectSofaSources) { + return runtime_error("source index is out of range"); + } + if (!std::isfinite(gain)) { + return runtime_error("source gain must be finite"); + } + const std::size_t slot = static_cast(source); + double key[5] = {position[0], position[1], position[2], gain, special_lfe ? 1.0 : 0.0}; + bool unchanged = has_state_[slot] != 0; + for (int axis = 0; axis < 5 && unchanged; ++axis) { + unchanged = last_key_[slot][axis] == key[axis]; + } + if (unchanged) { + return Status::success(); + } + for (int axis = 0; axis < 5; ++axis) { + last_key_[slot][axis] = key[axis]; + } + const bool first = processed_input_samples_ == 0; + if (special_lfe) { + if (room_.active()) { + const Status room_status = room_.set_source(slot, nullptr, gain, first, true); + if (!room_status.ok()) { + return room_status; + } + } + std::vector filters = lfe_filter_; + for (double& value : filters) { + value *= gain; + } + schedule_filter(slot, filters, fft_size_, first, fade); + } else { + const std::array xyz = {position[0], position[1], position[2]}; + const std::array* previous = + has_state_[slot] != 0 ? &distance_states_[slot].direction : nullptr; + PointSourcePosition state; + Status status = reference_.map_position(xyz, previous, &state); + if (!status.ok()) { + return status; + } + std::vector filters; + std::size_t taps = 0; + if (room_.active()) { + status = room_.full_hrir(field_, state, filter_taps_, &filters); + if (!status.ok()) { + return status; + } + taps = filter_taps_; + } else { + std::vector> spectrum; + status = field_.spectrum(state.direction.data(), &spectrum); + if (!status.ok()) { + return status; + } + filters.assign(2u * fft_size_, 0.0); + std::vector impulse; + for (std::size_t ear = 0; ear < 2u; ++ear) { + ear_impulse(spectrum, ear, bins_, fft_size_, &impulse); + std::copy(impulse.begin(), impulse.end(), + filters.begin() + static_cast(ear * fft_size_)); + } + taps = fft_size_; + } + for (double& value : filters) { + value *= gain; + } + schedule_filter(slot, filters, taps, first, fade); + distance_states_[slot] = state; + if (room_.active()) { + status = room_.set_source(slot, &state, gain, first, false); + if (!status.ok()) { + return status; + } + } + } + has_state_[slot] = 1; + ++parameter_updates_; + return Status::success(); +} + +Status DirectSofaRuntime::process_block() { + apply_due_filters(); + const Status status = convolver_.process_block(hop_block_.data(), direct_.data()); + if (!status.ok()) { + return status; + } + processed_input_samples_ += static_cast(kDirectSofaBlock); + if (room_.active()) { + bool produced = false; + const Status room_status = room_.process(hop_block_.data(), kDirectSofaSources, + direct_.data(), &room_out_, &produced); + if (!room_status.ok()) { + return room_status; + } + if (produced) { + emit(room_out_.data(), room_out_.size() / 2u); + } + return Status::success(); + } + emit(direct_.data(), kDirectSofaBlock); + return Status::success(); +} + +void DirectSofaRuntime::emit(const double* interleaved, std::size_t samples) { + const std::size_t skip = static_cast( + std::min(static_cast(samples), latency_remaining_)); + latency_remaining_ -= static_cast(skip); + for (std::size_t sample = skip; sample < samples; ++sample) { + output_.push_back(interleaved[sample * 2u] * output_gain_); + output_.push_back(interleaved[sample * 2u + 1u] * output_gain_); + } + output_samples_ += samples - skip; +} + +Status DirectSofaRuntime::submit_frame(const float* objects16_planar, + const oamd::OamdUpdate* update, + std::int64_t /*frame_index*/, + std::int64_t outer_sample_offset, + std::int64_t object_delay_samples) { + if (objects16_planar == nullptr) { + return runtime_error("frame buffer is null"); + } + for (std::size_t channel = 0; channel < kDirectSofaSources; ++channel) { + for (std::size_t sample = 0; sample < static_cast(kDirectSofaFrame); + ++sample) { + staging_[sample * kDirectSofaSources + channel] = + static_cast(objects16_planar[channel * kDirectSofaFrame + sample]); + } + } + if (update != nullptr) { + const Status status = timeline_.submit_update(*update, input_samples_, + outer_sample_offset, + object_delay_samples, + processed_input_samples_); + if (!status.ok()) { + return status; + } + } + input_samples_ += kDirectSofaFrame; + double positions[timeline::kTimelineObjects][3] = {}; + for (std::size_t block = 0; block < 12u; ++block) { + Status status = timeline_.positions_at(processed_input_samples_, positions); + if (!status.ok()) { + return status; + } + status = set_source(0, kDirectSofaLfePosition, 1.0, true, true); + if (!status.ok()) { + return status; + } + for (int object = 0; object < timeline::kTimelineObjects; ++object) { + status = set_source(object + 1, positions[object], 1.0, false, true); + if (!status.ok()) { + return status; + } + } + for (std::size_t sample = 0; sample < kDirectSofaBlock; ++sample) { + for (std::size_t channel = 0; channel < kDirectSofaSources; ++channel) { + hop_block_[sample * kDirectSofaSources + channel] = + staging_[(block * kDirectSofaBlock + sample) * kDirectSofaSources + channel]; + } + } + status = process_block(); + if (!status.ok()) { + return status; + } + } + return Status::success(); +} + +std::uint32_t DirectSofaRuntime::finish_capacity(double tail_seconds) const { + const double samples = tail_seconds * 48000.0 + static_cast(latency_samples_) + + static_cast(filter_taps_); + const double blocks = std::ceil(samples / 128.0); + return static_cast(std::max(0.0, blocks * 128.0)); +} + +Status DirectSofaRuntime::finish(std::uint32_t flush_samples, std::vector* out) { + if (out == nullptr) { + return runtime_error("finish output is null"); + } + const std::size_t before = output_.size(); + std::size_t count = flush_samples; + const std::int64_t expected = std::max( + 0, processed_input_samples_ + static_cast(count) - latency_samples_ - + static_cast(output_samples_)); + if (room_.active()) { + const std::size_t pending = room_.pending_samples(); + const double batches = std::ceil(static_cast(count + pending) / + static_cast(kSofaRoomBatchSamples)); + count = static_cast( + std::max(0.0, batches * static_cast(kSofaRoomBatchSamples) - + static_cast(pending))); + } + std::fill(hop_block_.begin(), hop_block_.end(), 0.0); + double positions[timeline::kTimelineObjects][3] = {}; + for (std::size_t block = 0; block < count / kDirectSofaBlock; ++block) { + Status status = timeline_.positions_at(processed_input_samples_, positions); + if (!status.ok()) { + return status; + } + status = set_source(0, kDirectSofaLfePosition, 1.0, true, true); + if (!status.ok()) { + return status; + } + for (int object = 0; object < timeline::kTimelineObjects; ++object) { + status = set_source(object + 1, positions[object], 1.0, false, true); + if (!status.ok()) { + return status; + } + } + status = process_block(); + if (!status.ok()) { + return status; + } + } + std::vector tail(output_.begin() + static_cast(before), output_.end()); + if (static_cast(tail.size() / 2u) > expected) { + tail.resize(static_cast(expected) * 2u); + } + output_.resize(before + tail.size()); + out->swap(tail); + return Status::success(); +} + +} // namespace joc::binaural diff --git a/src/binaural/direct_sofa_runtime.h b/src/binaural/direct_sofa_runtime.h new file mode 100644 index 0000000..d49580b --- /dev/null +++ b/src/binaural/direct_sofa_runtime.h @@ -0,0 +1,133 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "binaural/point_source.h" +#include "binaural/sofa_room.h" +#include "foundation/status.h" +#include "hrtf/full_hrir_field.h" +#include "hrtf/partitioned_convolution.h" +#include "oamd/oamd_parser.h" +#include "timeline/position_timeline.h" + +// Direct SOFA renderer: full HRIR per source, point-source coordinates, and the +// optional project-owned room. Ported from the reference src/sofa_direct_backend.py; +// there is no distance-dependent HRTF adaptation and no external reference model. +namespace joc::binaural { + +inline constexpr int kDirectSofaSources = 16; +inline constexpr std::size_t kDirectSofaBlock = 128; +inline constexpr int kDirectSofaFrame = 1536; +inline constexpr double kDirectSofaLfePosition[3] = {0.0, 1.0, 0.0}; + +struct DirectSofaOptions { + std::string mode; // near/mid/far; empty selects mid + bool has_reference_distance = false; + double reference_distance_m = 0.0; + hrtf::HrirInterpolation interpolation = hrtf::HrirInterpolation::kLogMagnitudePhase; + bool has_shell_radius = false; + double shell_radius_m = 0.0; + double output_gain = 1.0; + // Optional project room; `room_preset` is compact/legacy (empty selects + // legacy for far and compact otherwise). + bool sofa_room = false; + std::string room_preset; + std::int64_t object_delay_samples = 1473; + double tail_seconds = 5.0; +}; + +class DirectSofaRuntime { +public: + DirectSofaRuntime(); + ~DirectSofaRuntime(); + DirectSofaRuntime(const DirectSofaRuntime&) = delete; + DirectSofaRuntime& operator=(const DirectSofaRuntime&) = delete; + + Status open(const std::string& sofa_path, const DirectSofaOptions& options); + + // objects16_planar is channel-major: channel * 1536 + sample. + Status submit_frame(const float* objects16_planar, const oamd::OamdUpdate* update, + std::int64_t frame_index, std::int64_t outer_sample_offset, + std::int64_t object_delay_samples); + + Status finish(std::uint32_t flush_samples, std::vector* out); + std::uint32_t finish_capacity(double tail_seconds) const; + Status reset(); + + const std::vector& output() const { return output_; } + void take_output(std::vector* out) { + out->swap(output_); + output_.clear(); + } + + void set_fixed_position(int object_index, const double position[3]) { + timeline_.set_fixed_position(object_index, position); + } + + const timeline::OamdPositionTimeline& timeline() const { return timeline_; } + std::uint64_t input_samples() const { return input_samples_; } + std::uint64_t processed_input_samples() const { return processed_input_samples_; } + std::uint64_t parameter_updates() const { return parameter_updates_; } + int latency_samples() const { return latency_samples_; } + int recording_origin_samples() const { return origin_samples_; } + std::size_t filter_taps() const { return filter_taps_; } + bool room_active() const { return room_.active(); } + const std::string& room_preset() const { return room_.preset(); } + const hrtf::FullHrirField& field() const { return field_; } + const PointSourceReference& reference() const { return reference_; } + +private: + struct PendingFilter { + std::int64_t due = 0; + std::size_t source = 0; + std::vector filters; + std::size_t taps = 0; + std::uint32_t fade_blocks = 0; + }; + + Status set_source(int source, const double position[3], double gain, bool special_lfe, + bool fade); + void schedule_filter(std::size_t source, const std::vector& filters, + std::size_t taps, bool first, bool fade); + void apply_due_filters(); + Status process_block(); + void emit(const double* interleaved, std::size_t samples); + Status build_lfe_filter(); + + PointSourceReference reference_; + hrtf::FullHrirField field_; + SofaRoom room_; + hrtf::MultiSourcePartitionedConvolver convolver_; + + std::vector lfe_filter_; // [2][fft_size] + std::deque pending_filters_; + + std::vector distance_states_; + std::vector has_state_; + double last_key_[kDirectSofaSources][5] = {}; + + std::vector hop_block_; // [128][sources] + std::vector staging_; // [1536][sources] + std::vector direct_; // [128][2] + std::vector room_out_; // [1536][2] when the room produced a batch + std::vector output_; + + timeline::OamdPositionTimeline timeline_; + std::int64_t input_samples_ = 0; + std::int64_t processed_input_samples_ = 0; + std::uint64_t parameter_updates_ = 0; + std::uint64_t output_samples_ = 0; + int origin_samples_ = 0; + int latency_samples_ = 0; + std::size_t filter_taps_ = 0; + std::size_t fft_size_ = 0; + std::size_t bins_ = 0; + std::int64_t latency_remaining_ = 0; + double output_gain_ = 1.0; +}; + +} // namespace joc::binaural diff --git a/src/binaural/point_source.cpp b/src/binaural/point_source.cpp new file mode 100644 index 0000000..46461ee --- /dev/null +++ b/src/binaural/point_source.cpp @@ -0,0 +1,66 @@ +#include "binaural/point_source.h" + +#include +#include + +#include "binaural/reference_distance.h" + +namespace joc::binaural { +namespace { + +Status argument_error(const char* message) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, joc::stage::kRender, message); +} + +} // namespace + +Status PointSourceReference::configure(const std::string& mode, bool has_reference_distance, + double reference_distance_m) { + Status status = normalize_mode_name(mode.empty() ? std::string("mid") : mode, &mode_); + if (!status.ok()) { + return status; + } + if (has_reference_distance) { + if (!std::isfinite(reference_distance_m) || reference_distance_m <= 0.0) { + return argument_error("source reference distance must be finite and positive"); + } + reference_distance_m_ = reference_distance_m; + return Status::success(); + } + return source_reference_distance(mode_, &reference_distance_m_); +} + +Status PointSourceReference::map_position(const std::array& position, + const std::array* previous_direction, + PointSourcePosition* out) const { + if (out == nullptr) { + return argument_error("point source output is null"); + } + for (const double value : position) { + if (!std::isfinite(value)) { + return argument_error("source position must contain three finite values"); + } + if (std::fabs(value) > 50.1 + 1e-8) { + return argument_error("source position exceeds the finite metadata domain"); + } + } + const double radius = std::sqrt(position[0] * position[0] + position[1] * position[1] + + position[2] * position[2]); + // ADM (+x right, +y front, +z up) to the listener frame (+x front, +y left). + std::array direction = {position[1], -position[0], position[2]}; + if (radius > 1e-12) { + for (double& value : direction) { + value /= radius; + } + } else if (previous_direction == nullptr) { + direction = {1.0, 0.0, 0.0}; + } else { + direction = *previous_direction; + } + out->direction = direction; + out->distance_m = radius * reference_distance_m_; + out->normalized_radius = radius; + return Status::success(); +} + +} // namespace joc::binaural diff --git a/src/binaural/point_source.h b/src/binaural/point_source.h new file mode 100644 index 0000000..e0c4326 --- /dev/null +++ b/src/binaural/point_source.h @@ -0,0 +1,41 @@ +#pragma once + +#include +#include + +#include "foundation/status.h" + +// Listener-centred point-source coordinates for direct HRTF rendering, ported +// from the reference src/point_source.py: the ADM position selects a direction +// in the listener frame and a distance scaled by the mode's reference distance. +// There is no distance-dependent HRTF adaptation; the measured SOFA response is +// kept as it was measured. +namespace joc::binaural { + +struct PointSourcePosition { + std::array direction = {1.0, 0.0, 0.0}; // listener frame, unit + double distance_m = 0.0; + double normalized_radius = 0.0; +}; + +class PointSourceReference { +public: + // `mode` is near/mid/far (empty selects mid). `has_reference_distance` + // overrides the mode's reference distance, as the reference CLI option does. + Status configure(const std::string& mode, bool has_reference_distance, + double reference_distance_m); + + const std::string& mode() const { return mode_; } + double reference_distance_m() const { return reference_distance_m_; } + + // `previous_direction` resolves a coincident position (nullptr keeps front). + Status map_position(const std::array& position, + const std::array* previous_direction, + PointSourcePosition* out) const; + +private: + std::string mode_ = "mid"; + double reference_distance_m_ = 0.0; +}; + +} // namespace joc::binaural diff --git a/src/binaural/reference_distance.cpp b/src/binaural/reference_distance.cpp new file mode 100644 index 0000000..bd08d5b --- /dev/null +++ b/src/binaural/reference_distance.cpp @@ -0,0 +1,131 @@ +#include "binaural/reference_distance.h" + +#include +#include + +namespace joc::binaural { +namespace { + +using ArgumentError = joc::Status; + +Status argument_error(const char* message) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, joc::stage::kRender, message); +} + +struct ModePreset { + const char* name; + double reference_distance_m; + double late_send; +}; + +constexpr ModePreset kModes[3] = { + {"near", 1.00000465, 0.06}, + {"mid", 2.19327927, 0.16}, + {"far", 6.40177584, 0.28}, +}; + +const ModePreset* find_mode(const std::string& mode) { + for (const ModePreset& preset : kModes) { + if (mode == preset.name) { + return &preset; + } + } + return nullptr; +} + +} // namespace + +Status normalize_mode_name(const std::string& mode, std::string* out) { + if (out == nullptr) { + return argument_error("mode output is null"); + } + std::string lowered; + lowered.reserve(mode.size()); + for (const char value : mode) { + if (value == ' ' || value == '\t') { + continue; + } + lowered.push_back(static_cast(std::tolower(static_cast(value)))); + } + if (find_mode(lowered) == nullptr) { + return argument_error("distance profile must be near, mid, or far"); + } + *out = lowered; + return Status::success(); +} + +Status source_reference_distance(const std::string& mode, double* out) { + std::string normalized; + Status status = normalize_mode_name(mode, &normalized); + if (!status.ok()) { + return status; + } + if (out == nullptr) { + return argument_error("reference distance output is null"); + } + *out = find_mode(normalized)->reference_distance_m; + return Status::success(); +} + +Status map_adm_position(const std::array& position, const std::string& mode, + DistanceState* out) { + if (out == nullptr) { + return argument_error("distance state output is null"); + } + for (const double value : position) { + if (!std::isfinite(value)) { + return argument_error("ADM position must contain three finite Cartesian values"); + } + } + std::string normalized; + Status status = normalize_mode_name(mode, &normalized); + if (!status.ok()) { + return status; + } + const double radius = std::sqrt(position[0] * position[0] + position[1] * position[1] + + position[2] * position[2]); + std::array direction = {0.0, 1.0, 0.0}; + if (radius > 1.0e-15) { + direction[0] = position[0] / radius; + direction[1] = position[1] / radius; + direction[2] = position[2] / radius; + } + const double reference = find_mode(normalized)->reference_distance_m; + out->profile = normalized; + out->normalized_radius = radius; + out->reference_distance_m = reference; + out->physical_distance_m = std::max(kMinimumSourceDistanceM, radius * reference); + out->direction_adm = direction; + return Status::success(); +} + +double direct_level_gain(const DistanceState& state) { + if (state.profile == "near") { + return 1.0; + } + const double distance = state.physical_distance_m; + return 1.0 / std::sqrt(1.0 + kRoomEnergyCouplingPerM2 * distance * distance); +} + +double late_send(const DistanceState& state) { + const ModePreset* preset = find_mode(state.profile); + const double base = preset == nullptr ? 0.16 : preset->late_send; + const double radial = std::sqrt(std::max(state.normalized_radius, 0.0)); + return base * std::min(std::max(radial, 0.25), 1.5); +} + +Status inverse_distance_gain(double measurement_radius_m, double path_distance_m, double* out) { + if (!std::isfinite(measurement_radius_m) || !std::isfinite(path_distance_m)) { + return argument_error("measurement and path distances must be finite"); + } + if (measurement_radius_m <= 0.0 || path_distance_m <= 0.0) { + return argument_error("measurement and path distances must be positive"); + } + if (out == nullptr) { + return argument_error("gain output is null"); + } + *out = measurement_radius_m / path_distance_m; + return Status::success(); +} + +} // namespace joc::binaural diff --git a/src/binaural/reference_distance.h b/src/binaural/reference_distance.h new file mode 100644 index 0000000..e82db0b --- /dev/null +++ b/src/binaural/reference_distance.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include + +#include "foundation/status.h" + +// Project-owned distance policy for the SOFA renderer, ported from the +// reference src/reference_distance.py. These are presentation presets of this +// project, not a claim about an external standard: the reference distances are +// the nominal object-coordinate scales, the direct level is an equal-power dry +// coefficient (never a raw 1/r attenuation of authored programme gain), and the +// late send is a fixed per-mode share scaled by the object radius. +namespace joc::binaural { + +inline constexpr double kMinimumSourceDistanceM = 0.10; +inline constexpr double kRoomEnergyCouplingPerM2 = 0.01318359375; +inline constexpr double kPublicRoomCalibrationGain = 1.4; + +// Reference distance of one mode, in metres. +Status source_reference_distance(const std::string& mode, double* out); +// Validates a mode name ("near", "mid" or "far") and stores its canonical spelling. +Status normalize_mode_name(const std::string& mode, std::string* out); + +struct DistanceState { + std::string profile = "mid"; + double normalized_radius = 0.0; + double reference_distance_m = 0.0; + double physical_distance_m = 0.0; + std::array direction_adm = {0.0, 1.0, 0.0}; +}; + +// Resolves an ADM position on one reference distance profile. +Status map_adm_position(const std::array& position, const std::string& mode, + DistanceState* out); + +// Equal-power dry coefficient: unity for near, 1/sqrt(1+coupling*d^2) otherwise. +double direct_level_gain(const DistanceState& state); +// Per-mode late send, scaled by the square root of the normalized radius. +double late_send(const DistanceState& state); +// measurement radius / path distance, the image-source level of one reflection. +Status inverse_distance_gain(double measurement_radius_m, double path_distance_m, double* out); + +} // namespace joc::binaural diff --git a/src/binaural/room_tables.cpp b/src/binaural/room_tables.cpp new file mode 100644 index 0000000..b647cfc --- /dev/null +++ b/src/binaural/room_tables.cpp @@ -0,0 +1,267 @@ +#include "binaural/room_tables.h" + +#include +#include +#include + +namespace joc::binaural { +namespace { + +Status room_error(const char* message) { + return Status::fail(JOC_ERR_HRTF_FORMAT, joc::stage::kRender, message); +} + +bool is_prime(int value) { + if (value < 2) { + return false; + } + if (value % 2 == 0) { + return value == 2; + } + for (int divisor = 3; divisor * divisor <= value; divisor += 2) { + if (value % divisor == 0) { + return false; + } + } + return true; +} + +int next_prime(int value) { + int candidate = std::max(2, value); + while (!is_prime(candidate)) { + ++candidate; + } + return candidate; +} + +// One Schroeder all-pass section; the sign convention follows the reference. +class SchroederAllpass { +public: + void configure(int delay_samples, double gain) { + delay_ = std::max(delay_samples, 1); + gain_ = gain; + buffer_.assign(static_cast(delay_), 0.0); + position_ = 0; + } + + void reset() { + std::fill(buffer_.begin(), buffer_.end(), 0.0); + position_ = 0; + } + + void process(const double* input, double* output, std::size_t count) { + for (std::size_t index = 0; index < count; ++index) { + const double value = input[index]; + const double delayed = buffer_[static_cast(position_)]; + const double result = delayed - gain_ * value; + buffer_[static_cast(position_)] = value + gain_ * result; + position_ = (position_ + 1) % delay_; + output[index] = result; + } + } + +private: + int delay_ = 1; + double gain_ = 0.0; + std::vector buffer_; + int position_ = 0; +}; + +constexpr double kHadamard4[4][4] = { + {0.5, 0.5, 0.5, 0.5}, + {0.5, -0.5, 0.5, -0.5}, + {0.5, 0.5, -0.5, -0.5}, + {0.5, -0.5, -0.5, 0.5}, +}; + +constexpr double kInputVector[4] = {0.5, -0.5, 0.5, 0.5}; +constexpr double kOutputMatrix[2][4] = { + {0.5, 0.5, -0.5, -0.5}, + {0.5, -0.5, 0.5, -0.5}, +}; + +struct Fdn { + std::array, 4> buffers; + std::array positions = {}; + std::array delays = {}; + std::array feedback_gain = {}; + std::array damping_state = {}; + SchroederAllpass diffusers[2]; + double damping = 0.0; + double output_gain = 0.0; + + void reset() { + for (std::vector& buffer : buffers) { + std::fill(buffer.begin(), buffer.end(), 0.0); + } + positions.fill(0); + damping_state.fill(0.0); + for (SchroederAllpass& diffuser : diffusers) { + diffuser.reset(); + } + } + + void process(const double* input, double* left, double* right, std::size_t count) { + std::vector diffused(input, input + count); + std::vector stage(count); + for (SchroederAllpass& diffuser : diffusers) { + diffuser.process(diffused.data(), stage.data(), count); + diffused.swap(stage); + } + for (std::size_t sample = 0; sample < count; ++sample) { + std::array delayed = {}; + for (int line = 0; line < 4; ++line) { + delayed[static_cast(line)] = + buffers[static_cast(line)] + [static_cast(positions[static_cast(line)])]; + } + for (int line = 0; line < 4; ++line) { + const std::size_t index = static_cast(line); + damping_state[index] = + damping * damping_state[index] + (1.0 - damping) * delayed[index]; + } + left[sample] = output_gain * (kOutputMatrix[0][0] * damping_state[0] + + kOutputMatrix[0][1] * damping_state[1] + + kOutputMatrix[0][2] * damping_state[2] + + kOutputMatrix[0][3] * damping_state[3]); + right[sample] = output_gain * (kOutputMatrix[1][0] * damping_state[0] + + kOutputMatrix[1][1] * damping_state[1] + + kOutputMatrix[1][2] * damping_state[2] + + kOutputMatrix[1][3] * damping_state[3]); + std::array feedback = {}; + for (int row = 0; row < 4; ++row) { + double sum = 0.0; + for (int column = 0; column < 4; ++column) { + sum += kHadamard4[row][column] * + (damping_state[static_cast(column)] * + feedback_gain[static_cast(column)]); + } + feedback[static_cast(row)] = sum; + } + for (int line = 0; line < 4; ++line) { + const std::size_t index = static_cast(line); + const double write = + kInputVector[index] * diffused[sample] + feedback[index]; + buffers[index][static_cast(positions[index])] = write; + positions[index] = (positions[index] + 1) % delays[index]; + } + } + } +}; + +double volume_over_area(const std::array& dimensions) { + const double volume = dimensions[0] * dimensions[1] * dimensions[2]; + const double area = 2.0 * (dimensions[0] * dimensions[1] + + dimensions[0] * dimensions[2] + + dimensions[1] * dimensions[2]); + return volume / area; +} + +struct FdnConfig { + double sample_rate_hz = 48000.0; + double rt60_seconds = 0.85; + double damping = 0.32; + double output_gain = 0.22; + std::array delay_seconds = {0.0297, 0.0371, 0.0411, 0.0437}; + std::array allpass_seconds = {0.0023, 0.0067}; + std::array allpass_gain = {0.63, 0.51}; +}; + +} // namespace + +Status render_late_fir(double sample_rate_hz, double rt60_seconds, double damping, + double output_gain, const std::array& delay_seconds, + const std::array& allpass_seconds, + const std::array& allpass_gain, std::size_t impulse_samples, + std::vector* left, std::vector* right) { + if (left == nullptr || right == nullptr || impulse_samples == 0u) { + return room_error("late FDN output is invalid"); + } + if (!(sample_rate_hz > 0.0) || !(rt60_seconds > 0.0) || !std::isfinite(damping) || + damping < 0.0 || damping >= 1.0 || !std::isfinite(output_gain)) { + return room_error("invalid FDN sample rate, RT60, damping or output gain"); + } + Fdn fdn; + fdn.damping = damping; + fdn.output_gain = output_gain; + for (int line = 0; line < 4; ++line) { + const double seconds = delay_seconds[static_cast(line)]; + if (!(seconds > 0.0)) { + return room_error("FDN requires four positive delay times"); + } + const int delay = next_prime( + static_cast(std::lround(seconds * sample_rate_hz))); + fdn.delays[static_cast(line)] = delay; + fdn.feedback_gain[static_cast(line)] = + std::pow(10.0, -3.0 * static_cast(delay) / (rt60_seconds * sample_rate_hz)); + fdn.buffers[static_cast(line)].assign(static_cast(delay), 0.0); + } + for (int stage = 0; stage < 2; ++stage) { + const double seconds = allpass_seconds[static_cast(stage)]; + const double gain = allpass_gain[static_cast(stage)]; + if (!(seconds > 0.0) || !(std::fabs(gain) < 1.0)) { + return room_error("FDN requires two positive all-pass delays and gains"); + } + fdn.diffusers[stage].configure( + next_prime(static_cast(std::lround(seconds * sample_rate_hz))), gain); + } + // The probe is the part beyond the FIR that the reference discards. + const std::size_t probe = + static_cast(std::ceil(rt60_seconds * sample_rate_hz)); + std::vector impulse(impulse_samples + probe, 0.0); + impulse[0] = 1.0; + left->assign(impulse.size(), 0.0); + right->assign(impulse.size(), 0.0); + fdn.process(impulse.data(), left->data(), right->data(), impulse.size()); + left->resize(impulse_samples); + right->resize(impulse_samples); + return Status::success(); +} + +Status build_room_preset(const std::string& preset, RoomPreset* out) { + if (out == nullptr) { + return room_error("room preset output is null"); + } + FdnConfig config; + ShoeboxRoomConfig early; + double scale = 1.0; + if (preset == "compact") { + const std::array dimensions = {8.5, 11.3, 5.2}; + early.dimensions_m = dimensions; + early.listener_position_m = {dimensions[0] * 0.5, dimensions[1] * 0.5, + dimensions[2] * 0.5}; + scale = volume_over_area(dimensions) / + volume_over_area(std::array{18.0, 18.0, 14.0}); + config.rt60_seconds *= scale; + for (double& value : config.delay_seconds) { + value *= scale; + } + for (double& value : config.allpass_seconds) { + value *= scale; + } + } else if (preset != "legacy") { + return room_error("room preset must be compact or legacy"); + } + const Status validated = early.validate(); + if (!validated.ok()) { + return validated; + } + const std::size_t taps = static_cast( + std::ceil(2.0 * config.rt60_seconds * config.sample_rate_hz)); + std::vector left; + std::vector right; + const Status rendered = render_late_fir( + config.sample_rate_hz, config.rt60_seconds, config.damping, config.output_gain, + config.delay_seconds, config.allpass_seconds, config.allpass_gain, taps, &left, &right); + if (!rendered.ok()) { + return rendered; + } + out->early = early; + out->late_fir[0] = std::move(left); + out->late_fir[1] = std::move(right); + out->rt60_seconds = config.rt60_seconds; + out->size_time_scale = scale; + return Status::success(); +} + +} // namespace joc::binaural diff --git a/src/binaural/room_tables.h b/src/binaural/room_tables.h new file mode 100644 index 0000000..754d097 --- /dev/null +++ b/src/binaural/room_tables.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include +#include + +#include "binaural/shoebox_room.h" +#include "foundation/status.h" + +// The project's own room, generated in code: a shared unitary FDN (four +// orthogonal Hadamard feedback lines, two Schroeder all-pass diffusers and +// low-pass damping) rendered to a two-channel float64 FIR, plus the shoebox +// geometry its first-order images are computed from. Ported from the reference +// src/public_room.py; there is no measured BRIR and no external model behind it. +namespace joc::binaural { + +struct RoomPreset { + ShoeboxRoomConfig early; + std::vector late_fir[2]; // [2][taps] + double rt60_seconds = 0.0; + double size_time_scale = 1.0; +}; + +// "legacy" is the original project room; "compact" keeps the same algorithm with +// the Zahorik-table geometry and the late time constants scaled by V/S. +Status build_room_preset(const std::string& preset, RoomPreset* out); + +// One FDN realization: the response to a unit impulse, cut to 2*RT60. +Status render_late_fir(double sample_rate_hz, double rt60_seconds, double damping, + double output_gain, const std::array& delay_seconds, + const std::array& allpass_seconds, + const std::array& allpass_gain, + std::size_t impulse_samples, std::vector* left, + std::vector* right); + +} // namespace joc::binaural diff --git a/src/binaural/shoebox_room.cpp b/src/binaural/shoebox_room.cpp new file mode 100644 index 0000000..52b3cac --- /dev/null +++ b/src/binaural/shoebox_room.cpp @@ -0,0 +1,125 @@ +#include "binaural/shoebox_room.h" + +#include +#include + +namespace joc::binaural { +namespace { + +Status argument_error(const char* message) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, joc::stage::kRender, message); +} + +const char* const kWallNames[6] = {"left", "right", "back", "front", "floor", "ceiling"}; + +} // namespace + +Status ShoeboxRoomConfig::validate() const { + for (const double value : dimensions_m) { + if (!std::isfinite(value) || value <= 0.0) { + return argument_error("room dimensions must be three positive finite values"); + } + } + for (int axis = 0; axis < 3; ++axis) { + const double value = listener_position_m[static_cast(axis)]; + if (!std::isfinite(value) || value <= 0.0 || + value >= dimensions_m[static_cast(axis)]) { + return argument_error("listener must be strictly inside the shoebox"); + } + } + for (const double gain : wall_reflection_gain) { + if (!std::isfinite(gain) || std::fabs(gain) >= 1.0) { + return argument_error("six finite wall gains must have magnitude below one"); + } + } + if (!std::isfinite(speed_of_sound_m_s) || speed_of_sound_m_s <= 0.0) { + return argument_error("speed of sound must be positive"); + } + return Status::success(); +} + +Status first_order_image_sources(const std::array& direction_adm, + double source_distance_m, double sample_rate_hz, + const ShoeboxRoomConfig& config, + std::vector* out) { + if (out == nullptr) { + return argument_error("reflection output is null"); + } + const Status validated = config.validate(); + if (!validated.ok()) { + return validated; + } + for (const double value : direction_adm) { + if (!std::isfinite(value)) { + return argument_error( + "reflection direction must contain three finite ADM values"); + } + } + const double norm = std::sqrt(direction_adm[0] * direction_adm[0] + + direction_adm[1] * direction_adm[1] + + direction_adm[2] * direction_adm[2]); + const std::array direction = norm <= 1.0e-15 + ? std::array{0.0, 1.0, 0.0} + : std::array{ + direction_adm[0] / norm, + direction_adm[1] / norm, + direction_adm[2] / norm}; + if (!std::isfinite(source_distance_m) || source_distance_m <= 0.0 || + !std::isfinite(sample_rate_hz) || sample_rate_hz <= 0.0) { + return argument_error("source distance and sample rate must be positive"); + } + std::array source = {}; + for (int axis = 0; axis < 3; ++axis) { + source[static_cast(axis)] = + config.listener_position_m[static_cast(axis)] + + direction[static_cast(axis)] * source_distance_m; + const double value = source[static_cast(axis)]; + if (value <= 0.0 || value >= config.dimensions_m[static_cast(axis)]) { + return argument_error( + "source lies outside the configured public shoebox; enlarge the room"); + } + } + std::array, 6> images = {}; + for (int axis = 0; axis < 3; ++axis) { + std::array low = source; + low[static_cast(axis)] = -source[static_cast(axis)]; + std::array high = source; + high[static_cast(axis)] = + 2.0 * config.dimensions_m[static_cast(axis)] - + source[static_cast(axis)]; + images[static_cast(axis * 2)] = low; + images[static_cast(axis * 2 + 1)] = high; + } + out->clear(); + out->reserve(6); + for (int index = 0; index < 6; ++index) { + const std::array& image = images[static_cast(index)]; + std::array vector = {}; + for (int axis = 0; axis < 3; ++axis) { + vector[static_cast(axis)] = + image[static_cast(axis)] - + config.listener_position_m[static_cast(axis)]; + } + const double path_distance = std::sqrt(vector[0] * vector[0] + vector[1] * vector[1] + + vector[2] * vector[2]); + if (!(path_distance > 0.0)) { + return argument_error("image source coincides with the listener"); + } + EarlyReflection reflection; + reflection.wall = kWallNames[index]; + for (int axis = 0; axis < 3; ++axis) { + reflection.direction_adm[static_cast(axis)] = + vector[static_cast(axis)] / path_distance; + } + reflection.path_distance_m = path_distance; + const double extra_metres = std::max(path_distance - source_distance_m, 0.0); + reflection.extra_delay_samples = extra_metres * sample_rate_hz / config.speed_of_sound_m_s; + const double air = std::exp(-0.002 * extra_metres); + reflection.reflection_gain = + config.wall_reflection_gain[static_cast(index)] * air; + out->push_back(reflection); + } + return Status::success(); +} + +} // namespace joc::binaural diff --git a/src/binaural/shoebox_room.h b/src/binaural/shoebox_room.h new file mode 100644 index 0000000..0314d7a --- /dev/null +++ b/src/binaural/shoebox_room.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include + +#include "foundation/status.h" + +// Project-owned shoebox geometry and first-order image sources, ported from the +// reference src/public_room.py. The room is this project's own construction, not +// a measured BRIR and not an external model: one rectangular room, one listener +// position, six wall gains and a speed of sound. +namespace joc::binaural { + +struct ShoeboxRoomConfig { + std::array dimensions_m = {18.0, 18.0, 14.0}; + std::array listener_position_m = {9.0, 9.0, 7.0}; + std::array wall_reflection_gain = {0.62, 0.60, 0.58, 0.61, 0.52, 0.56}; + double speed_of_sound_m_s = 343.3; + + Status validate() const; +}; + +struct EarlyReflection { + std::string wall; + std::array direction_adm = {0.0, 1.0, 0.0}; + double path_distance_m = 0.0; + double extra_delay_samples = 0.0; + double reflection_gain = 0.0; +}; + +// Six first-order image-source paths for one object at `direction_adm` and +// `source_distance_m` metres from the listener. +Status first_order_image_sources(const std::array& direction_adm, + double source_distance_m, double sample_rate_hz, + const ShoeboxRoomConfig& config, + std::vector* out); + +} // namespace joc::binaural diff --git a/src/binaural/sofa_reference_runtime.cpp b/src/binaural/sofa_reference_runtime.cpp deleted file mode 100644 index 9b3e849..0000000 --- a/src/binaural/sofa_reference_runtime.cpp +++ /dev/null @@ -1,571 +0,0 @@ - -#include "binaural/sofa_reference_runtime.h" - -#include -#include -#include -#include - -#include "foundation/fft.h" -#include "hrtf/sofa.h" -#include "hrtf/sofa_field.h" - -namespace joc::binaural { -namespace { - -constexpr double kPi = 3.141592653589793238462643383279502884; - -Status runtime_error(const char* message) { - return Status::fail(JOC_ERR_INVALID_ARGUMENT, joc::stage::kRender, message); -} - -Status render_fail(const char* message) { - return Status::fail(JOC_ERR_HRTF_FORMAT, joc::stage::kDsp, message); -} - -// Inverse real transform: Hermitian half -> n real samples (numpy irfft). -void irfft_to_real(const std::vector>& half, std::size_t count, - std::vector* out) { - std::vector> buffer(count); - const std::size_t bins = count / 2 + 1; - for (std::size_t bin = 0; bin < bins; ++bin) { - buffer[bin] = half[bin]; - } - for (std::size_t bin = 1; bin + 1 < bins; ++bin) { - buffer[count - bin] = std::conj(half[bin]); - } - dsp::fft_radix2(&buffer, true); - out->resize(count); - for (std::size_t index = 0; index < count; ++index) { - (*out)[index] = buffer[index].real(); - } -} - -// numpy.roll along the sample axis. -void roll_forward(const std::vector& source, std::size_t shift, - std::vector* out) { - const std::size_t count = source.size(); - std::vector copy(source); - out->resize(count); - if (count == 0) { - return; - } - const std::size_t offset = shift % count; - for (std::size_t index = 0; index < count; ++index) { - (*out)[index] = copy[(index + count - offset) % count]; - } -} - -// scipy.signal.firwin(129, 150, fs=48000): windowed-sinc, unit DC gain. -std::vector lowpass_150(double sample_rate) { - constexpr int kTaps = 129; - const double cutoff = 150.0 / (sample_rate * 0.5); - std::vector taps(kTaps, 0.0); - double total = 0.0; - for (int index = 0; index < kTaps; ++index) { - const double position = static_cast(index) - 0.5 * (kTaps - 1); - double value = 0.0; - if (std::fabs(position) < 1e-12) { - value = cutoff; - } else { - value = cutoff * std::sin(kPi * cutoff * position) / (kPi * position); - } - const double window = 0.54 - 0.46 * std::cos(2.0 * kPi * index / (kTaps - 1)); - taps[static_cast(index)] = value * window; - total += value * window; - } - for (double& value : taps) { - value /= total; - } - return taps; -} - -void stereo_delay(const std::vector& input, std::size_t delay, std::vector* state, - std::vector* out) { - const std::size_t samples = input.size() / 2; - out->assign(input.size(), 0.0); - const std::size_t capacity = delay * 2; - std::vector joined(capacity + input.size(), 0.0); - for (std::size_t index = 0; index < capacity; ++index) { - joined[index] = (*state)[index]; - } - std::copy(input.begin(), input.end(), joined.begin() + static_cast(capacity)); - for (std::size_t index = 0; index < input.size(); ++index) { - (*out)[index] = joined[index]; - } - state->assign(capacity, 0.0); - for (std::size_t index = 0; index < capacity; ++index) { - (*state)[index] = joined[input.size() + index]; - } -} - -} // namespace - -SofaReferenceRuntime::SofaReferenceRuntime() = default; -SofaReferenceRuntime::~SofaReferenceRuntime() = default; - -void SofaReferenceRuntime::rebuild_latency() { - latency_samples_ = std::max(implementation_latency_, bank_latency_) + - std::max(origin_samples_, 0); - room_delay_samples_ = latency_samples_ - bank_latency_; - direct_delay_samples_ = std::max( - 0, latency_samples_ - implementation_latency_ - std::max(origin_samples_, 0)); - latency_remaining_ = latency_samples_; - room_delay_state_.assign(static_cast(room_delay_samples_) * 2, 0.0); - direct_delay_state_.assign(static_cast(direct_delay_samples_) * 2, 0.0); -} - -Status SofaReferenceRuntime::open(const std::string& sofa_path, - const SofaReferenceOptions& options) { - output_gain_ = options.output_gain; - Status status = environment_.load(options.reference_config, options.mode_index, - options.mode_specified); - if (!status.ok()) { - return status; - } - hrtf::SofaHrir sofa; - status = hrtf::load_sofa(sofa_path, &sofa); - if (!status.ok()) { - return status; - } - hrtf::CanonicalHrtf canonical; - status = hrtf::canonicalize_sofa(sofa, &canonical); - if (!status.ok()) { - return status; - } - hrtf::FullHrirOptions field_options; - field_options.method = options.interpolation; - field_options.has_shell_radius = options.has_shell_radius; - field_options.shell_radius_m = options.shell_radius_m; - status = field_.build(canonical, field_options); - if (!status.ok()) { - return status; - } - fft_size_ = field_.fft_size(); - bins_ = field_.frequency_bins(); - frequencies_ = field_.frequencies(); - implementation_latency_ = field_.common_latency_samples(); - origin_samples_ = field_.recording_origin_samples(); - if (origin_samples_ < 0) { - return runtime_error("negative recording origin requires an explicit time base"); - } - bank_latency_ = static_cast(hrtf::rosella_bank_latency(hrtf::RosellaBankKind::Public)); - if (environment_.minimum_distance_m() <= field_.head_radius_m()) { - return runtime_error("reference near-centre regularization lies inside the head"); - } - rebuild_latency(); - - const auto callback = [this](const ReferencePosition& state, - std::vector>* out) { - return spectrum_at(state, out); - }; - status = environment_.calibrate(fft_size_, frequencies_, callback, &level_calibration_); - if (!status.ok()) { - return status; - } - analysis_bank_ = environment_.create_analysis_bank(kSofaReferenceSources); - synthesis_bank_ = environment_.create_synthesis_bank(); - status = environment_.create_room(&room_); - if (!status.ok()) { - return status; - } - convolver_.configure(kSofaReferenceSources, kSofaReferenceBlock, fft_size_, 2); - hop_block_.assign(kSofaReferenceBlock * kSofaReferenceSources, 0.0); - staging_.assign(kSofaReferenceBatch * kSofaReferenceSources, 0.0); - direct_.assign(kSofaReferenceBlock * 2, 0.0); - qmf_.assign(2 * kSofaReferenceSources * 64, std::complex(0.0, 0.0)); - hybrid_.assign(2 * kSofaReferenceSources * 77, std::complex(0.0, 0.0)); - room_in_.assign(2 * 77, std::complex(0.0, 0.0)); - room_out_.assign(2 * 2 * 77, std::complex(0.0, 0.0)); - synth_.assign(2 * 2 * 64, std::complex(0.0, 0.0)); - synth_time_.assign(2 * 2 * 64, 0.0); - wet_.assign(kSofaReferenceBlock * 2, 0.0); - sends_.assign(kSofaReferenceSources, 0.0); - send_targets_.assign(kSofaReferenceSources, 0.0); - distance_states_.assign(kSofaReferenceSources, ReferencePosition{}); - has_state_.assign(kSofaReferenceSources, 0); - last_position_.assign(kSofaReferenceSources * 3, 0.0); - std::memset(last_key_, 0, sizeof(last_key_)); - for (double& value : last_gain_) { - value = std::numeric_limits::quiet_NaN(); - } - status = build_lfe_filter(); - if (!status.ok()) { - return status; - } - return reset(); -} - -Status SofaReferenceRuntime::build_lfe_filter() { - const std::vector lowpass = lowpass_150(48000.0); - lfe_filter_.assign(2 * fft_size_, 0.0); - const int start = latency_samples_ - 64; - if (start < 0 || static_cast(start) + lowpass.size() > fft_size_) { - return runtime_error("LFE latency exceeds the filter budget"); - } - const double scale = 1.0 / std::sqrt(2.0); - for (int ear = 0; ear < 2; ++ear) { - for (std::size_t tap = 0; tap < lowpass.size(); ++tap) { - lfe_filter_[static_cast(ear) * fft_size_ + - static_cast(start) + tap] = lowpass[tap] * scale; - } - } - return Status::success(); -} - -Status SofaReferenceRuntime::reset() { - for (hrtf::RosellaBank* bank : {analysis_bank_.get(), synthesis_bank_.get()}) { - if (bank != nullptr) { - bank->reset(); - } - } - if (room_) { - room_->reset(); - } - convolver_.reset(); - timeline_.reset(); - output_.clear(); - std::fill(sends_.begin(), sends_.end(), 0.0); - std::fill(send_targets_.begin(), send_targets_.end(), 0.0); - std::fill(has_state_.begin(), has_state_.end(), 0); - for (double& value : last_gain_) { - value = std::numeric_limits::quiet_NaN(); - } - spectrum_cache_.clear(); - pending_room_.clear(); - pending_direct_.clear(); - pending_slots_ = 0; - pending_direct_samples_ = 0; - input_samples_ = 0; - processed_input_samples_ = 0; - parameter_updates_ = 0; - latency_remaining_ = latency_samples_; - std::fill(room_delay_state_.begin(), room_delay_state_.end(), 0.0); - std::fill(direct_delay_state_.begin(), direct_delay_state_.end(), 0.0); - return Status::success(); -} - -Status SofaReferenceRuntime::spectrum_at(const ReferencePosition& state, - std::vector>* out) { - double key[5] = {state.direction[0], state.direction[1], state.direction[2], - state.ear_distance_m, state.centre_weight}; - for (const SpectrumEntry& entry : spectrum_cache_) { - bool match = true; - for (int axis = 0; axis < 5; ++axis) { - if (entry.key[axis] != key[axis]) { - match = false; - break; - } - } - if (match) { - *out = entry.spectrum; - return Status::success(); - } - } - std::vector> interpolated; - Status status = field_.spectrum(state.direction, &interpolated); - if (!status.ok()) { - return status; - } - std::vector> ratio; - status = field_.radial_ratio(state.direction, state.ear_distance_m, &ratio); - if (!status.ok()) { - return status; - } - out->assign(2 * bins_, std::complex(0.0, 0.0)); - for (int ear = 0; ear < 2; ++ear) { - for (std::size_t bin = 0; bin < bins_; ++bin) { - const std::size_t offset = static_cast(ear) * bins_ + bin; - const std::complex radial = - interpolated[offset] * ratio[offset] * state.centre_weight; - (*out)[offset] = - radial + (1.0 - state.centre_weight) * field_.centre_spectrum()[bin]; - } - } - SpectrumEntry entry; - for (int axis = 0; axis < 5; ++axis) { - entry.key[axis] = key[axis]; - } - entry.spectrum = *out; - if (spectrum_cache_.size() >= 128) { - spectrum_cache_.erase(spectrum_cache_.begin()); - } - spectrum_cache_.push_back(std::move(entry)); - return Status::success(); -} - -Status SofaReferenceRuntime::set_source(int source, const double position[3], double gain, - bool special_lfe, bool fade) { - if (source < 0 || source >= kSofaReferenceSources) { - return runtime_error("source index is out of range"); - } - if (!std::isfinite(gain)) { - return runtime_error("source gain must be finite"); - } - const std::size_t slot = static_cast(source); - double key[5] = {position[0], position[1], position[2], gain, - special_lfe ? 1.0 : 0.0}; - bool unchanged = has_state_[slot] != 0; - for (int axis = 0; axis < 5 && unchanged; ++axis) { - unchanged = last_key_[slot][axis] == key[axis]; - } - if (unchanged) { - return Status::success(); - } - for (int axis = 0; axis < 5; ++axis) { - last_key_[slot][axis] = key[axis]; - } - const bool first = processed_input_samples_ == 0; - std::vector filters(2 * fft_size_, 0.0); - if (special_lfe) { - send_targets_[slot] = 0.0; - std::copy(lfe_filter_.begin(), lfe_filter_.end(), filters.begin()); - for (double& value : filters) { - value *= gain; - } - } else { - ReferencePosition state; - Status status = environment_.map_position(position, &state); - if (!status.ok()) { - return status; - } - distance_states_[slot] = state; - send_targets_[slot] = state.room_send * gain; - std::vector> spectrum; - status = spectrum_at(state, &spectrum); - if (!status.ok()) { - return status; - } - const double scale = state.direct_gain * level_calibration_ * gain; - for (std::complex& value : spectrum) { - value *= scale; - } - // One ear at a time: the field returns both ears side by side. - std::vector> ear_spectrum(bins_); - for (int ear = 0; ear < 2; ++ear) { - std::copy(spectrum.begin() + static_cast(ear * bins_), - spectrum.begin() + static_cast((ear + 1) * bins_), - ear_spectrum.begin()); - std::vector impulse; - irfft_to_real(ear_spectrum, fft_size_, &impulse); - roll_forward(impulse, fft_size_ / 2, &impulse); - std::copy(impulse.begin(), impulse.end(), - filters.begin() + static_cast(ear * fft_size_)); - } - } - const std::size_t fade_blocks = (first || !fade) ? 0u : 1u; - const Status status = convolver_.set_filter(static_cast(source), - filters.data(), fft_size_, fade_blocks); - if (!status.ok()) { - return status; - } - has_state_[slot] = 1; - if (first) { - sends_[slot] = send_targets_[slot]; - } - ++parameter_updates_; - return Status::success(); -} - -Status SofaReferenceRuntime::process_block() { - // One 128-sample block is two 64-sample hops of the 16 source channels. - std::vector hops(2 * kSofaReferenceSources * 64, 0.0); - for (std::size_t slot = 0; slot < 2; ++slot) { - for (std::size_t channel = 0; channel < kSofaReferenceSources; ++channel) { - for (std::size_t tap = 0; tap < 64; ++tap) { - hops[(slot * kSofaReferenceSources + channel) * 64 + tap] = - hop_block_[(slot * 64 + tap) * kSofaReferenceSources + channel]; - } - } - } - analysis_bank_->analyze_qmf(hops, 2, &qmf_); - analysis_bank_->analyze_hybrid(qmf_, 2, &hybrid_); - - const double amount[2] = {0.5, 1.0}; - std::fill(room_in_.begin(), room_in_.end(), std::complex(0.0, 0.0)); - for (std::size_t slot = 0; slot < 2; ++slot) { - for (std::size_t source = 0; source < kSofaReferenceSources; ++source) { - const double ramp = sends_[source] + - (send_targets_[source] - sends_[source]) * amount[slot]; - const std::complex* hybrid = - hybrid_.data() + (slot * kSofaReferenceSources + source) * 77; - for (std::size_t band = 0; band < 77; ++band) { - room_in_[slot * 77 + band] += hybrid[band] * ramp; - } - } - } - sends_ = send_targets_; - - const Status status = convolver_.process_block(hop_block_.data(), direct_.data()); - if (!status.ok()) { - return status; - } - pending_room_.insert(pending_room_.end(), room_in_.begin(), room_in_.end()); - pending_direct_.insert(pending_direct_.end(), direct_.begin(), direct_.end()); - pending_slots_ += 2; - pending_direct_samples_ += kSofaReferenceBlock; - processed_input_samples_ += kSofaReferenceBlock; - if (pending_slots_ * 64 < static_cast(kSofaReferenceBatch)) { - return Status::success(); - } - return flush_batch(nullptr); -} - -Status SofaReferenceRuntime::flush_batch(std::vector* /*out*/) { - const std::size_t slots = pending_slots_; - Status status = room_->process_chunk(pending_room_, slots, &room_out_); - if (!status.ok()) { - return status; - } - synthesis_bank_->synthesize_hybrid(room_out_, slots, &synth_); - synth_time_.assign(slots * 2 * 64, 0.0); - synthesis_bank_->synthesize_qmf(synth_, slots, &synth_time_); - const std::size_t samples = slots * 64; - wet_.assign(samples * 2, 0.0); - for (std::size_t sample = 0; sample < samples; ++sample) { - const std::size_t slot = sample / 64; - const std::size_t tap = sample % 64; - for (std::size_t ear = 0; ear < 2; ++ear) { - wet_[sample * 2 + ear] = synth_time_[(slot * 2 + ear) * 64 + tap]; - } - } - std::vector wet_delayed; - std::vector direct_delayed; - stereo_delay(wet_, static_cast(room_delay_samples_), &room_delay_state_, - &wet_delayed); - stereo_delay(pending_direct_, static_cast(direct_delay_samples_), - &direct_delay_state_, &direct_delayed); - std::vector combined(samples * 2, 0.0); - for (std::size_t index = 0; index < combined.size(); ++index) { - combined[index] = wet_delayed[index] + direct_delayed[index]; - } - const std::size_t skip = static_cast( - std::min(static_cast(samples), latency_remaining_)); - latency_remaining_ -= static_cast(skip); - for (std::size_t sample = skip; sample < samples; ++sample) { - for (std::size_t ear = 0; ear < 2; ++ear) { - output_.push_back(combined[sample * 2 + ear] * output_gain_); - } - } - pending_room_.clear(); - pending_direct_.clear(); - pending_slots_ = 0; - pending_direct_samples_ = 0; - return Status::success(); -} - -Status SofaReferenceRuntime::submit_frame(const float* objects16_planar, - const oamd::OamdUpdate* update, - std::int64_t /*frame_index*/, - std::int64_t outer_sample_offset, - std::int64_t object_delay_samples) { - if (objects16_planar == nullptr) { - return runtime_error("frame buffer is null"); - } - for (std::size_t channel = 0; channel < kSofaReferenceSources; ++channel) { - for (std::size_t sample = 0; sample < static_cast(kSofaReferenceBatch); - ++sample) { - staging_[sample * kSofaReferenceSources + channel] = - static_cast(objects16_planar[channel * kSofaReferenceBatch + sample]); - } - } - if (update != nullptr) { - const Status status = timeline_.submit_update(*update, input_samples_, - outer_sample_offset, - object_delay_samples, - processed_input_samples_); - if (!status.ok()) { - return status; - } - } - input_samples_ += kSofaReferenceBatch; - for (std::size_t block = 0; block < 12; ++block) { - double positions[timeline::kTimelineObjects][3] = {}; - Status status = timeline_.positions_at(processed_input_samples_, positions); - if (!status.ok()) { - return status; - } - status = set_source(0, kSofaReferenceLfePosition, 1.0, true, true); - if (!status.ok()) { - return status; - } - for (int object = 0; object < timeline::kTimelineObjects; ++object) { - status = set_source(object + 1, positions[object], 1.0, false, true); - if (!status.ok()) { - return status; - } - } - for (std::size_t sample = 0; sample < static_cast(kSofaReferenceBlock); - ++sample) { - for (std::size_t channel = 0; channel < kSofaReferenceSources; ++channel) { - hop_block_[sample * kSofaReferenceSources + channel] = - staging_[static_cast(block * kSofaReferenceBlock + sample) * - kSofaReferenceSources + channel]; - } - } - status = process_block(); - if (!status.ok()) { - return status; - } - } - return Status::success(); -} - -std::uint32_t SofaReferenceRuntime::finish_capacity(double tail_seconds) const { - const double tail = tail_seconds; - const double samples = tail * 48000.0 + static_cast(latency_samples_) + - static_cast(fft_size_); - const double blocks = std::ceil(samples / 128.0); - return static_cast(std::max(0.0, blocks * 128.0)); -} - -Status SofaReferenceRuntime::finish(std::uint32_t flush_samples, std::vector* out) { - if (out == nullptr) { - return runtime_error("finish output is null"); - } - std::vector tail; - const std::size_t before = output_.size(); - const std::size_t output_samples_before = before; - const std::size_t pending = pending_slots_ * 64; - const std::size_t count = flush_samples; - const double padded_blocks = - std::ceil((static_cast(count + pending)) / - static_cast(kSofaReferenceBatch)); - const std::size_t padded = static_cast( - std::max(0.0, padded_blocks * kSofaReferenceBatch - static_cast(pending))); - const std::int64_t expected = - std::max(0, processed_input_samples_ + static_cast(count) - - latency_samples_ - - static_cast(output_samples_before)); - std::fill(hop_block_.begin(), hop_block_.end(), 0.0); - for (std::size_t block = 0; block < padded / kSofaReferenceBlock; ++block) { - double positions[timeline::kTimelineObjects][3] = {}; - Status status = timeline_.positions_at(processed_input_samples_, positions); - if (!status.ok()) { - return status; - } - status = set_source(0, kSofaReferenceLfePosition, 1.0, true, true); - if (!status.ok()) { - return status; - } - for (int object = 0; object < timeline::kTimelineObjects; ++object) { - status = set_source(object + 1, positions[object], 1.0, false, true); - if (!status.ok()) { - return status; - } - } - status = process_block(); - if (!status.ok()) { - return status; - } - } - tail.assign(output_.begin() + static_cast(before), output_.end()); - if (static_cast(tail.size()) > expected) { - tail.resize(static_cast(expected)); - } - output_.resize(before + tail.size() / 2 * 2); - out->swap(tail); - return Status::success(); -} - -} // namespace joc::binaural diff --git a/src/binaural/sofa_reference_runtime.h b/src/binaural/sofa_reference_runtime.h deleted file mode 100644 index 626b5f0..0000000 --- a/src/binaural/sofa_reference_runtime.h +++ /dev/null @@ -1,155 +0,0 @@ - -#pragma once - -#include -#include -#include -#include -#include - -#include "binaural/binaural_reference.h" -#include "foundation/status.h" -#include "hrtf/full_hrir_field.h" -#include "hrtf/partitioned_convolution.h" -#include "hrtf/rosella_bank.h" -#include "hrtf/rosella_reference.h" -#include "oamd/oamd_parser.h" -#include "timeline/position_timeline.h" - -// SOFA binaural renderer with a configured reference environment, ported from -// the reference sofa_reference_backend.py: the direct sound is the interpolated -// full HRIR (delay-aligned, partitioned convolution), the room send goes through -// the reference model's room, and the two are summed after their own latency -// compensation. -namespace joc::binaural { - -inline constexpr int kSofaReferenceSources = 16; -inline constexpr int kSofaReferenceBlock = 128; -inline constexpr int kSofaReferenceBatch = 1536; -inline constexpr double kSofaReferenceLfePosition[3] = {0.0, 1.0, 0.0}; - -struct SofaReferenceOptions { - int mode_index = kProfileMid; - bool mode_specified = false; - std::string reference_config; // empty selects the default configuration - hrtf::HrirInterpolation interpolation = hrtf::HrirInterpolation::kLogMagnitudePhase; - bool has_shell_radius = false; - double shell_radius_m = 0.0; - double output_gain = 1.0; - std::int64_t object_delay_samples = 1473; - double tail_seconds = 5.0; -}; - -class SofaReferenceRuntime { -public: - SofaReferenceRuntime(); - ~SofaReferenceRuntime(); - SofaReferenceRuntime(const SofaReferenceRuntime&) = delete; - SofaReferenceRuntime& operator=(const SofaReferenceRuntime&) = delete; - - Status open(const std::string& sofa_path, const SofaReferenceOptions& options); - - // objects16_planar is channel-major: channel * 1536 + sample. - Status submit_frame(const float* objects16_planar, const oamd::OamdUpdate* update, - std::int64_t frame_index, std::int64_t outer_sample_offset, - std::int64_t object_delay_samples); - - Status finish(std::uint32_t flush_samples, std::vector* out); - std::uint32_t finish_capacity(double tail_seconds) const; - Status reset(); - - const std::vector& output() const { return output_; } - void take_output(std::vector* out) { - out->swap(output_); - output_.clear(); - } - - std::uint64_t input_samples() const { return input_samples_; } - std::uint64_t processed_input_samples() const { return processed_input_samples_; } - std::uint64_t parameter_updates() const { return parameter_updates_; } - const timeline::OamdPositionTimeline& timeline() const { return timeline_; } - - void set_fixed_position(int object_index, const double position[3]) { - timeline_.set_fixed_position(object_index, position); - } - - // Reporting surface (mirrors the reference backend's info()). - int latency_samples() const { return latency_samples_; } - int recording_origin_samples() const { return origin_samples_; } - double level_calibration() const { return level_calibration_; } - const ReferenceEnvironment& environment() const { return environment_; } - const hrtf::FullHrirField& field() const { return field_; } - bool fixed_reference() const { return true; } - -private: - Status process_block(); - Status flush_batch(std::vector* out); - Status set_source(int source, const double position[3], double gain, bool special_lfe, - bool fade); - Status spectrum_at(const ReferencePosition& state, - std::vector>* out); - Status build_lfe_filter(); - void rebuild_latency(); - - ReferenceEnvironment environment_; - hrtf::FullHrirField field_; - std::unique_ptr analysis_bank_; - std::unique_ptr synthesis_bank_; - std::unique_ptr room_; - hrtf::MultiSourcePartitionedConvolver convolver_; - - std::vector lfe_filter_; // [2][fft_size] - std::vector sends_; // [sources] - std::vector send_targets_; - std::vector distance_states_; - std::vector has_state_; - double last_gain_[kSofaReferenceSources] = {}; - std::vector last_position_; // [sources][3] - double last_key_[kSofaReferenceSources][5] = {}; - - std::vector> pending_room_; // [slots][77] - std::vector pending_direct_; // [samples][2] - std::size_t pending_slots_ = 0; - std::size_t pending_direct_samples_ = 0; - - std::vector hop_block_; // [block][sources] - std::vector staging_; // [frame][sources] - std::vector output_; - std::vector> qmf_; // [slots][sources][64] - std::vector> hybrid_; // [slots][sources][77] - std::vector> room_in_; // [bins][77] - std::vector> room_out_; // [bins][2][77] - std::vector> synth_; // [bins][2][64] - std::vector synth_time_; // [bins][2][64] - std::vector wet_; // [samples][2] - std::vector direct_; // [samples][2] - - // Reference-position spectrum cache (the reference keeps 128 entries). - struct SpectrumEntry { - double key[5] = {}; - std::vector> spectrum; - }; - std::vector spectrum_cache_; - - timeline::OamdPositionTimeline timeline_; - std::int64_t input_samples_ = 0; - std::int64_t processed_input_samples_ = 0; - std::uint64_t parameter_updates_ = 0; - int origin_samples_ = 0; - int latency_samples_ = 0; - int implementation_latency_ = 0; - int bank_latency_ = 0; - int room_delay_samples_ = 0; - int direct_delay_samples_ = 0; - std::int64_t latency_remaining_ = 0; - double level_calibration_ = 1.0; - double output_gain_ = 1.0; - std::size_t fft_size_ = 0; - std::size_t bins_ = 0; - std::vector frequencies_; - std::vector room_delay_state_; - std::vector direct_delay_state_; - bool empty_block_ = true; -}; - -} // namespace joc::binaural diff --git a/src/binaural/sofa_room.cpp b/src/binaural/sofa_room.cpp new file mode 100644 index 0000000..e87a8fa --- /dev/null +++ b/src/binaural/sofa_room.cpp @@ -0,0 +1,292 @@ +#include "binaural/sofa_room.h" + +#include +#include + +#include "foundation/fft.h" + +namespace joc::binaural { +namespace { + +constexpr double kPi = 3.141592653589793238462643383279502884; + +Status argument_error(const char* message) { + return Status::fail(JOC_ERR_INVALID_ARGUMENT, joc::stage::kRender, message); +} + +// Inverse real transform of one half spectrum on a power-of-two grid. +void irfft_row(const std::vector>& half, std::size_t fft_size, + std::vector* out, std::size_t out_offset, std::size_t out_stride) { + std::vector> buffer(fft_size, std::complex(0.0, 0.0)); + const std::size_t bins = half.size(); + for (std::size_t bin = 0; bin < bins; ++bin) { + buffer[bin] = half[bin]; + } + for (std::size_t bin = 1; bin + 1 < bins; ++bin) { + buffer[fft_size - bin] = std::conj(half[bin]); + } + dsp::fft_radix2(&buffer, true); + for (std::size_t sample = 0; sample < fft_size; ++sample) { + (*out)[out_offset + (sample % out_stride)] = buffer[sample].real(); + } +} + +// Real block (zero padded to `fft_size`) to its first `fft_size/2+1` bins. +void rfft_row(const std::vector& time, std::size_t fft_size, + std::vector>* out) { + std::vector> buffer(fft_size, std::complex(0.0, 0.0)); + for (std::size_t sample = 0; sample < fft_size && sample < time.size(); ++sample) { + buffer[sample] = std::complex(time[sample], 0.0); + } + dsp::fft_radix2(&buffer, false); + out->assign(buffer.begin(), buffer.begin() + static_cast(fft_size / 2u + 1u)); +} + +} // namespace + +Status SofaRoom::configure(std::size_t source_count, const PointSourceReference& reference, + int implementation_latency, int recording_origin, + const std::string& preset) { + reference_ = reference; + RoomPreset generated; + const Status status = build_room_preset(preset, &generated); + if (!status.ok()) { + return status; + } + preset_ = preset; + config_ = generated.early; + rt60_seconds_ = generated.rt60_seconds; + alignment_samples_ = static_cast(implementation_latency) + + static_cast(std::max(recording_origin, 0)); + const std::size_t taps = generated.late_fir[0].size(); + const std::size_t aligned = static_cast(alignment_samples_) + taps; + std::vector kernel(2u * aligned, 0.0); + for (std::size_t row = 0; row < 2u; ++row) { + std::copy(generated.late_fir[row].begin(), generated.late_fir[row].end(), + kernel.begin() + static_cast( + row * aligned + static_cast(alignment_samples_))); + } + convolver_.configure(2u, aligned, kSofaRoomBatchSamples); + const Status filtered = convolver_.set_filter(kernel.data(), aligned, 0u); + if (!filtered.ok()) { + return filtered; + } + current_.assign(source_count, 0.0); + target_.assign(source_count, 0.0); + pending_mono_.clear(); + pending_direct_.clear(); + pending_blocks_ = 0; + maximum_early_delay_samples_ = 0.0; + valid_ = true; + return Status::success(); +} + +void SofaRoom::distance_state(const PointSourcePosition& state, DistanceState* out) const { + out->profile = reference_.mode(); + out->normalized_radius = state.normalized_radius; + out->reference_distance_m = reference_.reference_distance_m(); + out->physical_distance_m = std::max(kMinimumSourceDistanceM, state.distance_m); + // Listener frame (+x front, +y left) back to ADM (+x right, +y front). + out->direction_adm = {-state.direction[1], state.direction[0], state.direction[2]}; +} + +void SofaRoom::reset() { + if (!valid_) { + return; + } + std::fill(current_.begin(), current_.end(), 0.0); + std::fill(target_.begin(), target_.end(), 0.0); + pending_mono_.clear(); + pending_direct_.clear(); + pending_blocks_ = 0; + maximum_early_delay_samples_ = 0.0; + convolver_.reset(); +} + +Status SofaRoom::set_source(std::size_t source, const PointSourcePosition* state, double gain, + bool first, bool special_lfe) { + if (source >= current_.size()) { + return argument_error("room source index is out of range"); + } + double value = 0.0; + if (!special_lfe && state != nullptr) { + DistanceState distance; + distance_state(*state, &distance); + value = gain * kPublicRoomCalibrationGain * late_send(distance); + } + target_[source] = value; + if (first) { + current_[source] = value; + } + return Status::success(); +} + +Status SofaRoom::radians_paths(const PointSourcePosition& state, + std::vector* paths) { + DistanceState distance; + distance_state(state, &distance); + const Status status = first_order_image_sources(distance.direction_adm, + distance.physical_distance_m, 48000.0, + config_, paths); + if (!status.ok()) { + return status; + } + for (const EarlyReflection& path : *paths) { + maximum_early_delay_samples_ = + std::max(maximum_early_delay_samples_, path.extra_delay_samples); + } + return Status::success(); +} + +double SofaRoom::early_delay_bound_samples() const { + double extent = 0.0; + for (int axis = 0; axis < 3; ++axis) { + const double listener = config_.listener_position_m[static_cast(axis)]; + const double dimension = config_.dimensions_m[static_cast(axis)]; + extent = std::max(extent, std::max(listener, dimension - listener)); + } + return 2.0 * extent * 48000.0 / config_.speed_of_sound_m_s; +} + +std::size_t SofaRoom::filter_budget(const hrtf::FullHrirField& field) const { + const double needed = static_cast(field.fft_size()) + + std::ceil(early_delay_bound_samples()) + 64.0; + std::size_t length = 1; + while (static_cast(length) < needed) { + length <<= 1u; + } + return length; +} + +double SofaRoom::direct_gain(const PointSourcePosition& state) const { + DistanceState distance; + distance_state(state, &distance); + return direct_level_gain(distance); +} + +double SofaRoom::early_gain(const EarlyReflection& reflection, double measurement_radius_m) const { + return kPublicRoomCalibrationGain * reflection.reflection_gain * measurement_radius_m / + reflection.path_distance_m; +} + +Status SofaRoom::full_hrir(hrtf::FullHrirField& field, const PointSourcePosition& state, + std::size_t output_taps, std::vector* hrir) { + if (hrir == nullptr || output_taps < field.fft_size() || output_taps % 2u != 0) { + return argument_error("room HRIR output is invalid"); + } + const std::size_t fft_size = field.fft_size(); + const std::size_t field_bins = field.frequency_bins(); + const std::size_t bins = output_taps / 2u + 1u; + std::vector omega(bins); + for (std::size_t bin = 0; bin < bins; ++bin) { + omega[bin] = 2.0 * kPi * static_cast(bin) / static_cast(output_taps); + } + std::vector> spectrum(2u * bins, std::complex(0.0, 0.0)); + std::vector impulse(fft_size); + std::vector padded(output_taps, 0.0); + std::vector> half(field_bins); + std::vector> bins_out; + + const auto accumulate = [&](const std::array& direction, double extra_delay, + double gain) -> Status { + std::vector> value; + const Status status = field.spectrum(direction.data(), &value); + if (!status.ok()) { + return status; + } + for (int ear = 0; ear < 2; ++ear) { + for (std::size_t bin = 0; bin < field_bins; ++bin) { + half[bin] = value[static_cast(ear) * field_bins + bin]; + } + irfft_row(half, fft_size, &impulse, 0u, fft_size); + std::fill(padded.begin(), padded.end(), 0.0); + for (std::size_t sample = 0; sample < fft_size; ++sample) { + padded[(sample + fft_size / 2u) % fft_size] = impulse[sample]; + } + rfft_row(padded, output_taps, &bins_out); + for (std::size_t bin = 0; bin < bins; ++bin) { + const std::complex rotation = + extra_delay == 0.0 + ? std::complex(1.0, 0.0) + : std::exp(std::complex(0.0, -omega[bin] * extra_delay)); + spectrum[static_cast(ear) * bins + bin] += + bins_out[bin] * rotation * gain; + } + } + return Status::success(); + }; + + Status status = accumulate(state.direction, 0.0, direct_gain(state)); + if (!status.ok()) { + return status; + } + std::vector paths; + status = radians_paths(state, &paths); + if (!status.ok()) { + return status; + } + const double radius = field.measurement_radius_m(); + for (const EarlyReflection& path : paths) { + // ADM (+x right, +y front) back to the listener frame (+x front, +y left). + const std::array direction = {path.direction_adm[1], -path.direction_adm[0], + path.direction_adm[2]}; + status = accumulate(direction, path.extra_delay_samples, early_gain(path, radius)); + if (!status.ok()) { + return status; + } + } + + hrir->assign(2u * output_taps, 0.0); + std::vector> row(bins); + for (int ear = 0; ear < 2; ++ear) { + for (std::size_t bin = 0; bin < bins; ++bin) { + row[bin] = spectrum[static_cast(ear) * bins + bin]; + } + irfft_row(row, output_taps, hrir, static_cast(ear) * output_taps, + output_taps); + } + return Status::success(); +} + +Status SofaRoom::process(const double* block, std::size_t sources, const double* direct, + std::vector* out, bool* produced) { + if (!valid_ || block == nullptr || direct == nullptr || out == nullptr || + produced == nullptr) { + return argument_error("room process arguments are invalid"); + } + *produced = false; + const std::size_t offset = pending_mono_.size(); + pending_mono_.resize(offset + 128u, 0.0); + for (std::size_t sample = 0; sample < 128u; ++sample) { + const double amount = static_cast(sample + 1u) / 128.0; + double mono = 0.0; + for (std::size_t source = 0; source < sources; ++source) { + const double send = + current_[source] + (target_[source] - current_[source]) * amount; + mono += block[sample * sources + source] * send; + } + pending_mono_[offset + sample] = mono; + } + pending_direct_.insert(pending_direct_.end(), direct, direct + 256u); + current_ = target_; + ++pending_blocks_; + if (pending_blocks_ * 128u < kSofaRoomBatchSamples) { + return Status::success(); + } + std::vector late(kSofaRoomBatchSamples * 2u, 0.0); + const Status status = convolver_.process_block(pending_mono_.data(), late.data()); + if (!status.ok()) { + return status; + } + out->assign(pending_direct_.size(), 0.0); + for (std::size_t index = 0; index < pending_direct_.size(); ++index) { + (*out)[index] = pending_direct_[index] + late[index]; + } + pending_mono_.clear(); + pending_direct_.clear(); + pending_blocks_ = 0; + *produced = true; + return Status::success(); +} + +} // namespace joc::binaural diff --git a/src/binaural/sofa_room.h b/src/binaural/sofa_room.h new file mode 100644 index 0000000..0484b0a --- /dev/null +++ b/src/binaural/sofa_room.h @@ -0,0 +1,77 @@ +#pragma once + +#include +#include +#include +#include + +#include "binaural/point_source.h" +#include "binaural/reference_distance.h" +#include "binaural/room_tables.h" +#include "binaural/shoebox_room.h" +#include "foundation/status.h" +#include "hrtf/full_hrir_field.h" +#include "hrtf/partitioned_convolution.h" + +// Optional coupling of the SOFA direct path to this project's own room: six +// first-order image sources rendered through the same HRIR field, plus the late +// FDN tail. Ported from the reference src/sofa_room.py; the room is generated by +// this project's own model (see room_tables.h), not a measured BRIR. +namespace joc::binaural { + +inline constexpr std::size_t kSofaRoomBatchSamples = 1536; + +class SofaRoom { +public: + Status configure(std::size_t source_count, const PointSourceReference& reference, + int implementation_latency, int recording_origin, + const std::string& preset); + + bool active() const { return valid_; } + const std::string& preset() const { return preset_; } + double rt60_seconds() const { return rt60_seconds_; } + + // Sends for one source; `state` is null for the LFE channel. + Status set_source(std::size_t source, const PointSourcePosition* state, double gain, + bool first, bool special_lfe); + + Status radians_paths(const PointSourcePosition& state, + std::vector* paths); + double direct_gain(const PointSourcePosition& state) const; + double early_gain(const EarlyReflection& reflection, double measurement_radius_m) const; + const ShoeboxRoomConfig& early_config() const { return config_; } + double early_delay_bound_samples() const; + double maximum_early_delay_samples() const { return maximum_early_delay_samples_; } + + // HRIR of the direct sound plus the six first-order reflections. + Status full_hrir(hrtf::FullHrirField& field, const PointSourcePosition& state, + std::size_t output_taps, std::vector* hrir); + std::size_t filter_budget(const hrtf::FullHrirField& field) const; + + // One 128-sample block: returns false while the 1536-sample batch is filling. + Status process(const double* block, std::size_t sources, const double* direct, + std::vector* out, bool* produced); + + std::size_t pending_samples() const { return pending_blocks_ * 128u; } + // Clears the send state and the pending batch but keeps the loaded room. + void reset(); + +private: + void distance_state(const PointSourcePosition& state, DistanceState* out) const; + + bool valid_ = false; + std::string preset_; + PointSourceReference reference_; + ShoeboxRoomConfig config_; + double rt60_seconds_ = 0.0; + double alignment_samples_ = 0.0; + hrtf::UniformPartitionedConvolver convolver_; + std::vector current_; + std::vector target_; + std::vector pending_mono_; + std::vector pending_direct_; + std::size_t pending_blocks_ = 0; + double maximum_early_delay_samples_ = 0.0; +}; + +} // namespace joc::binaural diff --git a/src/cli/joc_cli.cpp b/src/cli/joc_cli.cpp index f3a8035..0fc9cee 100644 --- a/src/cli/joc_cli.cpp +++ b/src/cli/joc_cli.cpp @@ -49,8 +49,10 @@ struct Options { std::string hrtf_cache_policy; std::string hrtf_cache_dir; double hrtf_radius_m = 0.0; // 0 selects the single measurement shell - std::string reference_config; std::string sofa_interpolation; + double source_reference_distance_m = 0.0; + bool sofa_room = false; + std::string sofa_room_preset; double binaural_tail_seconds = 5.0; double binaural_tail_threshold = 1.0e-8; int binaural_chunk_frames = 64; @@ -125,6 +127,9 @@ void print_usage() { " --rosella-filterbank N Rosella 模型滤波器组:1=13 taps/6 slots(默认),\n" " 2=11 taps/3 slots;仅作用于个性化模型\n" " --compiled-hrtf-cache PATH 直接读取 .jochrtf(高级用法,跳过 SOFA 编译)\n" + " --source-reference-distance-m R 覆盖档位的点声源参考距离(米)\n" + " --sofa-room 可选:为 SOFA 启用项目自有房间,默认关闭\n" + " --sofa-room-preset P 房间预设 compact|legacy;默认 near/mid 用 compact、far 用 legacy\n" " --hrtf-cache-policy P SOFA 编译缓存策略 none|memory|disk,默认 memory\n" " --hrtf-cache-dir DIR disk cache 目录,默认 /output/hrtf-cache\n" " --hrtf-radius-m R 选择最近的 SOFA measurement-radius shell,默认 1.0 m\n" @@ -252,7 +257,9 @@ void parse_args(const std::vector& arguments, Options* options) { else if (arg == "--hrtf-cache-policy") { options->hrtf_cache_policy = require_value(arguments, &index); } else if (arg == "--hrtf-cache-dir") { options->hrtf_cache_dir = require_value(arguments, &index); } else if (arg == "--hrtf-radius-m") { options->hrtf_radius_m = to_double(require_value(arguments, &index), "--hrtf-radius-m"); } - else if (arg == "--reference-config") { options->reference_config = require_value(arguments, &index); } + else if (arg == "--source-reference-distance-m") { options->source_reference_distance_m = to_double(require_value(arguments, &index), "--source-reference-distance-m"); } + else if (arg == "--sofa-room") { options->sofa_room = true; } + else if (arg == "--sofa-room-preset") { options->sofa_room_preset = require_value(arguments, &index); } else if (arg == "--sofa-interpolation") { options->sofa_interpolation = require_value(arguments, &index); } else if (arg == "--binaural-tail-seconds") { options->binaural_tail_seconds = to_double(require_value(arguments, &index), "--binaural-tail-seconds"); } else if (arg == "--binaural-tail-threshold") { options->binaural_tail_threshold = to_double(require_value(arguments, &index), "--binaural-tail-threshold"); } @@ -358,7 +365,6 @@ struct HrtfInput { std::string compiled_path; // or read this .jochrtf directly std::string cache_dir; // disk policy directory std::string personalized_path; // Rosella .personalized_headphone - std::string reference_config; // SOFA reference environment std::string interpolation; // SOFA local interpolation bool disk = false; bool compile = false; @@ -465,11 +471,6 @@ HrtfInput resolve_hrtf_input(const Options& options, const std::string& project_ } input.sofa_path = sofa; input.disk = effective_policy == "disk"; - input.reference_config = - options.reference_config.empty() - ? fs_utf8::from_path(fs_utf8::to_path(project_directory) / "local" / - "binaural_reference.json") - : options.reference_config; input.interpolation = options.sofa_interpolation; input.cache_dir = options.hrtf_cache_dir.empty() ? default_cache_dir : options.hrtf_cache_dir; if (!fs_utf8::exists(input.sofa_path)) { @@ -582,6 +583,10 @@ int main(int argc, char** argv) { check_choice(options.sofa_interpolation, "--sofa-interpolation", {"lf_logmag_phase", "lf_mca_zero"}); } + if (!options.sofa_room_preset.empty()) { + check_choice(options.sofa_room_preset, "--sofa-room-preset", + {"compact", "legacy"}); + } check_choice(options.backend, "--backend", {"auto", "native", "python"}); check_choice(options.print_metadata, "--print-metadata", {"none", "summary", "frames"}); check_choice(options.metadata_backend, "--metadata-backend", {"auto", "emdf", "sidecar"}); @@ -736,7 +741,6 @@ int main(int argc, char** argv) { std::string hrtf_cache_dir; std::string personalized_path; std::string kernels_path; - std::string hrtf_reference_config; std::string hrtf_interpolation; bool hrtf_compile = false; if (binaural_mode) { @@ -745,7 +749,6 @@ int main(int argc, char** argv) { hrtf_path = input.compiled_path; hrtf_sofa_path = input.sofa_path; hrtf_cache_dir = input.cache_dir; - hrtf_reference_config = input.reference_config; hrtf_interpolation = input.interpolation; hrtf_compile = input.compile; personalized_path = input.personalized_path; @@ -769,8 +772,10 @@ int main(int argc, char** argv) { : options.hrtf_cache_policy == "none" ? JOC_HRTF_CACHE_NONE : JOC_HRTF_CACHE_MEMORY; config.hrtf_radius_m = options.hrtf_radius_m; - config.reference_config_path = - hrtf_reference_config.empty() ? nullptr : hrtf_reference_config.c_str(); + config.source_reference_distance_m = options.source_reference_distance_m; + config.sofa_room = options.sofa_room ? 1u : 0u; + config.sofa_room_preset = + options.sofa_room_preset.empty() ? nullptr : options.sofa_room_preset.c_str(); config.sofa_interpolation = hrtf_interpolation == "lf_mca_zero" ? JOC_SOFA_INTERPOLATION_MCA_ZERO : JOC_SOFA_INTERPOLATION_LOGMAG_PHASE; diff --git a/src/hrtf/full_hrir_field.cpp b/src/hrtf/full_hrir_field.cpp index fd3421c..c9162ca 100644 --- a/src/hrtf/full_hrir_field.cpp +++ b/src/hrtf/full_hrir_field.cpp @@ -144,91 +144,6 @@ private: } // namespace -Status sphere_transfer(const std::vector& frequencies, double radius, double head_radius, - const std::vector& cosines, - std::vector>* out) { - if (out == nullptr || !(radius > head_radius) || head_radius <= 0.0) { - return argument_error("sphere transfer needs a source outside the head"); - } - const std::size_t bands = frequencies.size(); - out->assign(cosines.size() * bands, std::complex(0.0, 0.0)); - std::vector> x(bands); - std::vector> z(bands); - std::vector> ratio_a(bands); - std::vector> ratio_r(bands); - std::vector> product(bands, std::complex(1.0, 0.0)); - double maximum_x = 0.0; - for (std::size_t band = 0; band < bands; ++band) { - const double wave = 2.0 * kPi * frequencies[band] * head_radius / kSoundSpeed; - maximum_x = std::max(maximum_x, wave); - x[band] = std::complex(wave, 0.0); - z[band] = std::complex(wave * radius / head_radius, 0.0); - ratio_a[band] = x[band] / (1.0 + std::complex(0.0, 1.0) * x[band]); - ratio_r[band] = z[band] / (1.0 + std::complex(0.0, 1.0) * z[band]); - } - const double geometric = std::ceil(std::log(kScanTolerance) / std::log(head_radius / radius)) + 16.0; - const int order = static_cast(std::max(std::ceil(maximum_x) + 32.0, geometric)); - if (order > 4096) { - return argument_error("source too close to the sphere surface"); - } - std::vector legendre_previous(cosines.size(), 1.0); - std::vector legendre(cosines.size(), 0.0); - std::vector mu(cosines.size()); - for (std::size_t row = 0; row < cosines.size(); ++row) { - mu[row] = std::clamp(cosines[row], -1.0, 1.0); - legendre[row] = mu[row]; - for (std::size_t band = 0; band < bands; ++band) { - const std::complex value = 1.0 / (1.0 + std::complex(0.0, 1.0) * x[band]); - (*out)[row * bands + band] = value; - } - } - std::vector> term(bands); - for (int degree = 1; degree <= order; ++degree) { - for (std::size_t band = 0; band < bands; ++band) { - const std::complex quotient = - std::abs(z[band]) != 0.0 ? ratio_a[band] / ratio_r[band] - : std::complex(head_radius / radius, 0.0); - product[band] *= quotient; - term[band] = product[band] / - ((static_cast(degree) + 1.0) - - x[band] * ratio_a[band]); - } - for (std::size_t row = 0; row < cosines.size(); ++row) { - const double factor = (2.0 * static_cast(degree) + 1.0) * legendre[row]; - for (std::size_t band = 0; band < bands; ++band) { - (*out)[row * bands + band] += factor * term[band]; - } - } - // Legendre recurrence P_{n+1} = ((2n+1) mu P_n - n P_{n-1}) / (n+1). - for (std::size_t row = 0; row < cosines.size(); ++row) { - const double next = - ((2.0 * static_cast(degree) + 1.0) * mu[row] * legendre[row] - - static_cast(degree) * legendre_previous[row]) / - (static_cast(degree) + 1.0); - legendre_previous[row] = legendre[row]; - legendre[row] = next; - } - for (std::size_t band = 0; band < bands; ++band) { - const double denominator_a = - (2.0 * static_cast(degree) + 1.0); - ratio_a[band] = x[band] / (denominator_a - x[band] * ratio_a[band]); - ratio_r[band] = z[band] / (denominator_a - z[band] * ratio_r[band]); - } - } - for (std::size_t band = 0; band < bands; ++band) { - const std::complex phase = std::exp(std::complex(0.0, 1.0) * x[band]); - for (std::size_t row = 0; row < cosines.size(); ++row) { - (*out)[row * bands + band] *= phase; - } - } - for (const std::complex& value : *out) { - if (!std::isfinite(value.real()) || !std::isfinite(value.imag())) { - return field_error("sphere series failed to converge"); - } - } - return Status::success(); -} - Status FullHrirField::build(const CanonicalHrtf& canonical, const FullHrirOptions& options) { method_ = options.method; if (std::fabs(canonical.sample_rate_hz - 48000.0) > 1e-9) { @@ -307,30 +222,6 @@ Status FullHrirField::build(const CanonicalHrtf& canonical, const FullHrirOption fft_size_ = fft_size; bins_ = fft_size / 2 + 1; - // Receiver geometry: a fixed pair, centred on the listener, one radius. - if (canonical.receiver_position_cartesian_m.size() < 6u) { - return field_error("ReceiverPosition is missing"); - } - const double* left = canonical.receiver_position_cartesian_m.data(); - const double* right = left + 3; - const double mean[3] = {(left[0] + right[0]) * 0.5, (left[1] + right[1]) * 0.5, - (left[2] + right[2]) * 0.5}; - if (std::sqrt(mean[0] * mean[0] + mean[1] * mean[1] + mean[2] * mean[2]) > 1e-6) { - return field_error("radial model requires ears centred on the listener"); - } - head_radius_m_ = std::sqrt(left[0] * left[0] + left[1] * left[1] + left[2] * left[2]); - const double right_radius = std::sqrt(right[0] * right[0] + right[1] * right[1] + - right[2] * right[2]); - if (!(head_radius_m_ > 0.04 && head_radius_m_ < 0.15) || - std::fabs(right_radius - head_radius_m_) > 1e-6) { - return field_error("unsupported receiver geometry for the radial model"); - } - ear_directions_.assign(6, 0.0); - for (int axis = 0; axis < 3; ++axis) { - ear_directions_[static_cast(axis)] = left[axis] / head_radius_m_; - ear_directions_[3u + static_cast(axis)] = right[axis] / head_radius_m_; - } - // Frequencies and phase-fit grid. frequencies_.resize(bins_); omega_.resize(bins_); @@ -531,28 +422,6 @@ Status FullHrirField::build(const CanonicalHrtf& canonical, const FullHrirOption } } - // Centre spectrum from the spherical Voronoi weights. - std::vector areas; - mesh_.voronoi_areas(&areas); - std::vector energy(bins_, 0.0); - for (std::size_t row = 0; row < rows; ++row) { - const double weight = row < areas.size() ? areas[row] : 0.0; - for (int ear = 0; ear < 2; ++ear) { - const std::size_t base = (row * 2u + static_cast(ear)) * bins_; - for (std::size_t bin = 0; bin < bins_; ++bin) { - energy[bin] += weight * std::norm(physical[base + bin]); - } - } - } - centre_spectrum_.assign(bins_, std::complex(0.0, 0.0)); - for (std::size_t bin = 0; bin < bins_; ++bin) { - const double magnitude = std::sqrt(energy[bin] * 0.5); - centre_spectrum_[bin] = magnitude * std::exp(std::complex( - 0.0, -omega_[bin] * - static_cast( - recording_origin_samples_))); - } - // Aligned spectra and the magnitude representation used by the interpolation. aligned_.assign(rows * 2 * bins_, std::complex(0.0, 0.0)); log_magnitude_.assign(rows * 2 * bins_, 0.0); @@ -624,48 +493,6 @@ Status FullHrirField::build(const CanonicalHrtf& canonical, const FullHrirOption return Status::success(); } -Status FullHrirField::radial_ratio(const double direction[3], double radius, - std::vector>* out) const { - if (out == nullptr) { - return argument_error("radial ratio output is null"); - } - if (std::fabs(radius - measurement_radius_m_) < 1e-10) { - out->assign(2 * bins_, std::complex(1.0, 0.0)); - return Status::success(); - } - if (!(radius > 0.0) || !std::isfinite(radius)) { - return argument_error("radial ratio needs a positive finite radius"); - } - std::vector cosines(2, 0.0); - for (int ear = 0; ear < 2; ++ear) { - cosines[static_cast(ear)] = - ear_directions_[static_cast(ear) * 3u] * direction[0] + - ear_directions_[static_cast(ear) * 3u + 1u] * direction[1] + - ear_directions_[static_cast(ear) * 3u + 2u] * direction[2]; - } - std::vector> source; - std::vector> target; - Status status = sphere_transfer(frequencies_, measurement_radius_m_, head_radius_m_, cosines, - &source); - if (!status.ok()) { - return status; - } - status = sphere_transfer(frequencies_, radius, head_radius_m_, cosines, &target); - if (!status.ok()) { - return status; - } - for (const std::complex& value : source) { - if (std::abs(value) < 1e-10) { - return field_error("singular radial reference response"); - } - } - out->resize(source.size()); - for (std::size_t index = 0; index < source.size(); ++index) { - (*out)[index] = target[index] / source[index]; - } - return Status::success(); -} - Status FullHrirField::spectrum(const double direction[3], std::vector>* out) { if (out == nullptr) { diff --git a/src/hrtf/full_hrir_field.h b/src/hrtf/full_hrir_field.h index 9c1e0af..508dbdf 100644 --- a/src/hrtf/full_hrir_field.h +++ b/src/hrtf/full_hrir_field.h @@ -18,13 +18,6 @@ namespace joc::hrtf { struct CanonicalHrtf; -// Point-source pressure at `radius` over the free-field pressure at the sphere -// centre (rigid sphere, FFT sign convention) for the cosines in `cosines`. -// Output is [cosines][frequencies] complex, row-major. -Status sphere_transfer(const std::vector& frequencies, double radius, double head_radius, - const std::vector& cosines, - std::vector>* out); - enum class HrirInterpolation : std::uint32_t { kLogMagnitudePhase = 0, // lf_logmag_phase: geometric mean magnitude, interpolated phase kMcaZero = 1, // lf_mca_zero: auditory-band magnitude correction @@ -47,8 +40,6 @@ public: const std::vector& frequencies() const { return frequencies_; } const std::vector& omega() const { return omega_; } double measurement_radius_m() const { return measurement_radius_m_; } - double head_radius_m() const { return head_radius_m_; } - const std::vector& ear_directions() const { return ear_directions_; } // [2][3] std::size_t direction_count() const { return mesh_.vertex_count(); } std::size_t triangle_count() const { return mesh_.face_count(); } int recording_origin_samples() const { return recording_origin_samples_; } @@ -61,13 +52,6 @@ public: // Interpolated two-ear spectrum at a direction (`out` is [2][bins] complex). Status spectrum(const double direction[3], std::vector>* out); - // Radial (distance) correction for a source at `radius` metres. - Status radial_ratio(const double direction[3], double radius, - std::vector>* out) const; - - // Centre (omnidirectional) spectrum used when a position is inside the head. - const std::vector>& centre_spectrum() const { return centre_spectrum_; } - private: Status build_interpolation(const CanonicalHrtf& canonical, const FullHrirOptions& options); @@ -84,10 +68,7 @@ private: std::vector auditory_; // [M][2][41] (MCA method only) std::vector centres_hz_; // [41] (MCA method only) std::vector bank_; // [41][bins] (MCA method only) - std::vector> centre_spectrum_; // [bins] double measurement_radius_m_ = 0.0; - double head_radius_m_ = 0.0; - std::vector ear_directions_; // [2][3] double floor_ = 1.0e-15; int recording_origin_samples_ = 0; double phase_fit_rms_rad_p95_ = 0.0; diff --git a/src/stream/stream.cpp b/src/stream/stream.cpp index 3dd1adc..ed18f67 100644 --- a/src/stream/stream.cpp +++ b/src/stream/stream.cpp @@ -7,6 +7,7 @@ #include "adm/adm_metadata.h" #include "foundation/status.h" #include "hrtf/jochrtf.h" +#include "binaural/direct_sofa_runtime.h" #include "hrtf/rosella_model.h" #include "hrtf/sofa_cache.h" #include "joc_bitstream/bed_layout_map.h" @@ -64,6 +65,9 @@ void Stream::reset_state() { if (binaural_ready_) { binaural_.reset(); } + if (sofa_direct_ready_) { + sofa_direct_.reset(); + } if (rosella_ready_) { rosella_.reset(); } @@ -144,25 +148,30 @@ Status Stream::create(const Config& config) { hrtf::Kernels kernels; Status status = Status::success(); if (!config_.hrtf_sofa_path.empty()) { - // The .jochrtf is an internal cache: the SOFA is the user-facing input. - hrtf::SofaFieldRequest request; - request.sofa_path = config_.hrtf_sofa_path; - request.options.shell_radius_m = config_.hrtf_radius_m; - request.cache_dir = config_.hrtf_cache_dir; - switch (config_.hrtf_cache_policy) { - case JOC_HRTF_CACHE_NONE: request.policy = hrtf::CachePolicy::None; break; - case JOC_HRTF_CACHE_DISK: request.policy = hrtf::CachePolicy::Disk; break; - default: request.policy = hrtf::CachePolicy::Memory; break; + // A SOFA renders from its full HRIRs; the .jochrtf stays the + // compiled input of hrtf_path. + binaural::DirectSofaOptions options; + options.mode = config_.binaural_mode == JOC_BINAURAL_NEAR + ? "near" + : (config_.binaural_mode == JOC_BINAURAL_FAR ? "far" : "mid"); + options.has_reference_distance = config_.source_reference_distance_m > 0.0; + options.reference_distance_m = config_.source_reference_distance_m; + options.has_shell_radius = config_.hrtf_radius_m > 0.0; + options.shell_radius_m = config_.hrtf_radius_m; + options.sofa_room = config_.sofa_room != 0u; + options.room_preset = config_.sofa_room_preset; + options.object_delay_samples = config_.object_delay_samples; + options.tail_seconds = config_.tail_seconds; + status = sofa_direct_.open(config_.hrtf_sofa_path, options); + if (status.ok()) { + sofa_direct_ready_ = true; + output_channels_ = 2; + return Status::success(); } - std::string cache_path; - status = hrtf::load_or_compile_sofa_field(request, &field, &cache_path); - if (!status.ok()) { - return Status::fail(status.code(), stage::kRender, - "binaural setup failed: " + status.message()); - } - } else { - status = hrtf::load_jochrtf(config_.hrtf_path, &field); + return Status::fail(status.code(), stage::kRender, + "binaural setup failed: " + status.message()); } + status = hrtf::load_jochrtf(config_.hrtf_path, &field); if (status.ok()) { status = config_.kernels_path.empty() ? (kernels = hrtf::builtin_kernels(), Status::success()) @@ -213,6 +222,9 @@ void Stream::apply_bed_positions(const FrameMetadata& entry) { if (binaural_ready_) { binaural_.set_fixed_position(static_cast(slot) + 1, xyz); } + if (sofa_direct_ready_) { + sofa_direct_.set_fixed_position(static_cast(slot) + 1, xyz); + } if (rosella_ready_) { rosella_.set_fixed_position(static_cast(slot) + 1, xyz); } @@ -611,6 +623,27 @@ Status Stream::render_objects16(const std::vector& objects16) { if (rosella_ready_) { return render_rosella_objects16(objects16); } + if (sofa_direct_ready_) { + const Status submitted = + sofa_direct_.submit_frame(objects16.data(), + pending_metadata_.has_update + ? &pending_metadata_.update + : nullptr, + static_cast(info_.frames_out), + pending_metadata_.outer_offset, + static_cast(config_.object_delay_samples)); + if (!submitted.ok()) { + return submitted; + } + sofa_direct_.take_output(&produced_); + output_.reserve(output_.size() + produced_.size()); + for (const double value : produced_) { + output_.push_back(static_cast(value)); + } + info_.frames_out++; + info_.samples_out += produced_.size() / 2u; + return Status::success(); + } const Status submitted = binaural_.submit_frame(objects16.data(), @@ -725,6 +758,19 @@ Status Stream::flush() { if (!objects.ok()) { return objects; } + if (sofa_direct_ready_) { + std::vector tail; + const Status drained = + sofa_direct_.finish(sofa_direct_.finish_capacity(config_.tail_seconds), &tail); + if (!drained.ok()) { + return drained; + } + output_.reserve(output_.size() + tail.size()); + for (const double value : tail) { + output_.push_back(static_cast(value)); + } + info_.samples_out += tail.size() / 2u; + } if (binaural_ready_) { std::vector tail; const Status drained = diff --git a/src/stream/stream.h b/src/stream/stream.h index 80030b3..88942aa 100644 --- a/src/stream/stream.h +++ b/src/stream/stream.h @@ -7,6 +7,7 @@ #include #include "binaural/binaural_runtime.h" +#include "binaural/direct_sofa_runtime.h" #include "eac3_transport/eac3_reader.h" #include "foundation/status.h" #include "hrtf/rosella_renderer.h" @@ -37,6 +38,10 @@ struct Config { std::string hrtf_cache_dir; std::uint32_t hrtf_cache_policy = JOC_HRTF_CACHE_MEMORY; double hrtf_radius_m = 1.0; + // Direct SOFA rendering: the same fields joc_task_config carries. + double source_reference_distance_m = 0.0; + std::uint32_t sofa_room = 0; + std::string sofa_room_preset; // Filterbank of the Rosella path; the other binaural inputs ignore it. std::uint32_t rosella_filterbank = JOC_ROSELLA_FILTERBANK_PUBLIC; }; @@ -171,6 +176,7 @@ private: ejoc_renderer_handle rebuilder_ = nullptr; speaker::SpeakerStep speaker_; binaural::SofaBinauralRuntime binaural_; + binaural::DirectSofaRuntime sofa_direct_; hrtf::RosellaRuntime rosella_; std::vector rosella_pending_; std::size_t rosella_read_offset_ = 0; @@ -178,6 +184,7 @@ private: bool speaker_enabled_ = false; bool binaural_enabled_ = false; bool binaural_ready_ = false; + bool sofa_direct_ready_ = false; bool bed_positions_ready_ = false; bool rosella_ready_ = false; float gain_ = 1.0f; diff --git a/src/task/task.cpp b/src/task/task.cpp index fd23b91..9b565e9 100644 --- a/src/task/task.cpp +++ b/src/task/task.cpp @@ -29,7 +29,7 @@ #include "adm/adm_metadata.h" #include "adm/adm_tracks.h" #include "binaural/binaural_runtime.h" -#include "binaural/sofa_reference_runtime.h" +#include "binaural/direct_sofa_runtime.h" #include "eac3_transport/eac3_reader.h" #include "emdf/emdf_parser.h" #include "foundation/mini_json.h" @@ -96,8 +96,10 @@ struct Settings { std::string hrtf_path; std::string kernels_path; std::string hrtf_sofa_path; - std::string reference_config_path; std::uint32_t sofa_interpolation = JOC_SOFA_INTERPOLATION_LOGMAG_PHASE; + double source_reference_distance_m = 0.0; + bool sofa_room = false; + std::string sofa_room_preset; bool hrtf_compile = false; std::string personalized_headphone_path; std::string hrtf_cache_dir; @@ -149,10 +151,10 @@ Settings normalise(const joc_task_config& raw) { if (raw.speaker_layout_name != nullptr) { cfg.layout = raw.speaker_layout_name; } if (raw.hrtf_path != nullptr) { cfg.hrtf_path = raw.hrtf_path; } if (raw.hrtf_sofa_path != nullptr) { cfg.hrtf_sofa_path = raw.hrtf_sofa_path; } - if (raw.reference_config_path != nullptr) { - cfg.reference_config_path = raw.reference_config_path; - } cfg.sofa_interpolation = raw.sofa_interpolation; + cfg.source_reference_distance_m = raw.source_reference_distance_m; + cfg.sofa_room = raw.sofa_room != 0u; + if (raw.sofa_room_preset != nullptr) { cfg.sofa_room_preset = raw.sofa_room_preset; } cfg.hrtf_compile = raw.hrtf_compile != 0u; if (raw.personalized_headphone_path != nullptr) { cfg.personalized_headphone_path = raw.personalized_headphone_path; @@ -1364,7 +1366,7 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu } ::joc::binaural::SofaBinauralRuntime binaural; - ::joc::binaural::SofaReferenceRuntime binaural_reference; + ::joc::binaural::DirectSofaRuntime sofa_direct; bool use_reference = false; ::joc::hrtf::RosellaRuntime rosella; const bool binaural_enabled = cfg.operation == JOC_OP_BINAURAL; @@ -1412,32 +1414,35 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu if (!cfg.hrtf_sofa_path.empty() && !cfg.hrtf_compile) { // The SOFA keeps its full HRIRs: the direct path renders through the // reference environment, and a .jochrtf stays an explicit input below. - ::joc::binaural::SofaReferenceOptions options; - options.mode_specified = cfg.binaural_mode != JOC_BINAURAL_MID; - options.mode_index = cfg.binaural_mode == JOC_BINAURAL_NEAR - ? ::joc::binaural::kProfileNear - : (cfg.binaural_mode == JOC_BINAURAL_FAR - ? ::joc::binaural::kProfileFar - : ::joc::binaural::kProfileMid); - options.reference_config = cfg.reference_config_path; + ::joc::binaural::DirectSofaOptions options; + options.mode = cfg.binaural_mode == JOC_BINAURAL_NEAR + ? "near" + : (cfg.binaural_mode == JOC_BINAURAL_FAR ? "far" : "mid"); + options.has_reference_distance = cfg.source_reference_distance_m > 0.0; + options.reference_distance_m = cfg.source_reference_distance_m; options.interpolation = cfg.sofa_interpolation == JOC_SOFA_INTERPOLATION_MCA_ZERO ? ::joc::hrtf::HrirInterpolation::kMcaZero : ::joc::hrtf::HrirInterpolation::kLogMagnitudePhase; options.has_shell_radius = cfg.hrtf_radius_m > 0.0; options.shell_radius_m = cfg.hrtf_radius_m; + options.sofa_room = cfg.sofa_room; + options.room_preset = cfg.sofa_room_preset; options.object_delay_samples = cfg.object_delay_samples; options.tail_seconds = cfg.tail_seconds; - status = binaural_reference.open(cfg.hrtf_sofa_path, options); + status = sofa_direct.open(cfg.hrtf_sofa_path, options); use_reference = status.ok(); if (status.ok()) { bus.emit(JOC_EV_HRTF_LOADED, JOC_STAGE_RENDER, JOC_LOG_INFO, fmt("SOFA full-HRIR field: %zu bins, %zu directions, %zu triangles, " - "latency %d samples", - binaural_reference.field().fft_size(), - binaural_reference.field().direction_count(), - binaural_reference.field().triangle_count(), - binaural_reference.latency_samples())); + "latency %d samples, room %s", + sofa_direct.field().fft_size(), + sofa_direct.field().direction_count(), + sofa_direct.field().triangle_count(), + sofa_direct.latency_samples(), + sofa_direct.room_active() + ? sofa_direct.room_preset().c_str() + : "off")); } } else { if (!cfg.hrtf_sofa_path.empty()) { @@ -1486,7 +1491,7 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu } bus.emit(JOC_EV_RENDERER_INITIALIZED, JOC_STAGE_RENDER, JOC_LOG_INFO, use_reference - ? "binaural runtime ready (full-HRIR SOFA reference path)" + ? "binaural runtime ready (full-HRIR SOFA direct path)" : "binaural runtime ready (the first 961 samples are the kernel latency)"); } } @@ -1845,7 +1850,7 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu xyz[2]); if (binaural_enabled) { if (use_reference) { - binaural_reference.set_fixed_position( + sofa_direct.set_fixed_position( static_cast(slot) + 1, xyz); } else { binaural.set_fixed_position( @@ -2035,7 +2040,7 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu static_cast(frame.index), frame.outer_offset, static_cast(cfg.object_delay_samples)); } else if (use_reference) { - submitted = binaural_reference.submit_frame( + submitted = sofa_direct.submit_frame( frame.objects16.data(), frame.has_update ? &frame.update : nullptr, static_cast(frame.index), frame.outer_offset, @@ -2089,7 +2094,7 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu if (rosella_enabled) { rosella.take_output(&pending.dsp); } else if (use_reference) { - binaural_reference.take_output(&pending.dsp); + sofa_direct.take_output(&pending.dsp); } else { binaural.take_output(&pending.dsp); } @@ -2132,13 +2137,13 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu !binaural_enabled ? 0ull : (use_reference - ? binaural_reference.timeline().payload_count() + ? sofa_direct.timeline().payload_count() : binaural.timeline().payload_count())), static_cast( !binaural_enabled ? 0ull : (use_reference - ? binaural_reference.timeline().transition_count() + ? sofa_direct.timeline().transition_count() : binaural.timeline().transition_count())))); } } @@ -2250,8 +2255,8 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu drained_tail = rosella.finish(rosella.finish_capacity(cfg.tail_seconds), &tail); } else if (use_reference) { - drained_tail = binaural_reference.finish( - binaural_reference.finish_capacity(cfg.tail_seconds), &tail); + drained_tail = sofa_direct.finish( + sofa_direct.finish_capacity(cfg.tail_seconds), &tail); } else { drained_tail = binaural.finish(binaural.finish_capacity(cfg.tail_seconds), &tail); @@ -2365,12 +2370,12 @@ Status run(const joc_task_config& raw, const joc_event_sink* sink, joc_task_resu out->oamd_payloads = !binaural_enabled ? out->oamd_payloads - : (use_reference ? binaural_reference.timeline().payload_count() + : (use_reference ? sofa_direct.timeline().payload_count() : binaural.timeline().payload_count()); out->oamd_transitions = !binaural_enabled ? out->oamd_transitions - : (use_reference ? binaural_reference.timeline().transition_count() + : (use_reference ? sofa_direct.timeline().transition_count() : binaural.timeline().transition_count()); out->t_decode_bed = t_decode; out->t_render = t_render; diff --git a/tests/test_core.cpp b/tests/test_core.cpp index 3cba9f4..93592a9 100644 --- a/tests/test_core.cpp +++ b/tests/test_core.cpp @@ -17,6 +17,9 @@ #include "foundation/fs_utf8.h" #include "foundation/geometry.h" #include "hrtf/jochrtf.h" +#include "binaural/point_source.h" +#include "binaural/reference_distance.h" +#include "binaural/shoebox_room.h" #include "hrtf/full_hrir_field.h" #include "hrtf/partitioned_convolution.h" #include "hrtf/sofa.h" @@ -357,33 +360,117 @@ void test_spherical_mesh() { CHECK(std::fabs(largest / smallest - 1.1197648114672005) < 1e-9); } -// Rigid-sphere radial model: values captured from the reference implementation. -void test_sphere_transfer() { - const std::vector frequencies = {100.0, 1000.0, 10000.0}; - const std::vector cosines = {1.0, -1.0, 0.0}; - std::vector> transfer; - const joc::Status status = - joc::hrtf::sphere_transfer(frequencies, 1.0, 0.0875, cosines, &transfer); - CHECK(status.ok()); - CHECK(transfer.size() == 9u); - const double expected[9][2] = { - {1.1168983535509718, 0.2665696486002471}, - {-0.510805763797141, 1.6128537943222743}, - {-2.028923793661157, -0.7781755079388825}, - {0.8548955324773564, -0.21699922414442047}, - {-0.8363832921619323, -0.5012251963562975}, - {0.03287720702899299, -0.7655728018349265}, - {0.9881471826293796, -0.01028038096534477}, - {1.0740047354266093, 0.11688911754407418}, - {0.9394042077254096, -0.7927884777210633}, - }; - for (std::size_t index = 0; index < 9u; ++index) { - const double real = expected[index][0]; - const double imaginary = expected[index][1]; - CHECK(std::fabs(transfer[index].real() - real) < 1e-9 * (1.0 + std::fabs(real))); - CHECK(std::fabs(transfer[index].imag() - imaginary) < - 1e-9 * (1.0 + std::fabs(imaginary))); +// Point-source reference: the ADM position selects a listener-frame direction and +// a distance scaled by the mode's reference distance. +void test_reference_distance() { + double near = 0.0; + double mid = 0.0; + double far = 0.0; + CHECK(joc::binaural::source_reference_distance("near", &near).ok()); + CHECK(joc::binaural::source_reference_distance("mid", &mid).ok()); + CHECK(joc::binaural::source_reference_distance("far", &far).ok()); + CHECK(std::fabs(near - 1.00000465) < 1e-12); + CHECK(std::fabs(mid - 2.19327927) < 1e-12); + CHECK(std::fabs(far - 6.40177584) < 1e-12); + CHECK(!joc::binaural::source_reference_distance("wide", &near).ok()); + + joc::binaural::DistanceState state; + state.profile = "near"; + state.physical_distance_m = 2.0; + state.normalized_radius = 0.5; + CHECK(joc::binaural::direct_level_gain(state) == 1.0); + CHECK(std::fabs(joc::binaural::late_send(state) - 0.042426406871192854) < 1e-15); + state.profile = "mid"; + CHECK(std::fabs(joc::binaural::direct_level_gain(state) - 0.97463184619707621) < 1e-15); + CHECK(std::fabs(joc::binaural::late_send(state) - 0.11313708498984762) < 1e-15); + state.profile = "far"; + CHECK(std::fabs(joc::binaural::late_send(state) - 0.19798989873223333) < 1e-15); + + const std::array front = {0.0, 1.0, 0.0}; + CHECK(joc::binaural::map_adm_position(front, "mid", &state).ok()); + CHECK(std::fabs(state.physical_distance_m - 2.19327927) < 1e-12); + CHECK(std::fabs(state.direction_adm[1] - 1.0) < 1e-15); + const std::array origin = {0.0, 0.0, 0.0}; + CHECK(joc::binaural::map_adm_position(origin, "mid", &state).ok()); + CHECK(std::fabs(state.physical_distance_m - joc::binaural::kMinimumSourceDistanceM) < 1e-15); + + double gain = 0.0; + CHECK(joc::binaural::inverse_distance_gain(1.2, 2.4, &gain).ok()); + CHECK(std::fabs(gain - 0.5) < 1e-15); + CHECK(!joc::binaural::inverse_distance_gain(1.2, 0.0, &gain).ok()); +} + +void test_point_source() { + joc::binaural::PointSourceReference reference; + CHECK(reference.configure("mid", false, 0.0).ok()); + CHECK(reference.mode() == "mid"); + CHECK(std::fabs(reference.reference_distance_m() - 2.19327927) < 1e-12); + + const std::array front = {0.0, 1.0, 0.0}; + joc::binaural::PointSourcePosition position; + CHECK(reference.map_position(front, nullptr, &position).ok()); + CHECK(std::fabs(position.direction[0] - 1.0) < 1e-15); + CHECK(std::fabs(position.direction[1]) < 1e-15); + CHECK(std::fabs(position.distance_m - 2.1932792700000001) < 1e-12); + CHECK(std::fabs(position.normalized_radius - 1.0) < 1e-15); + + const std::array side = {-0.8, 0.2, 0.6}; + CHECK(reference.map_position(side, nullptr, &position).ok()); + CHECK(std::fabs(position.direction[0] - 0.19611613513818402) < 1e-12); + CHECK(std::fabs(position.direction[1] - 0.7844645405527361) < 1e-12); + CHECK(std::fabs(position.distance_m - 2.2367147592977079) < 1e-12); + + const std::array origin = {0.0, 0.0, 0.0}; + const std::array previous = {0.0, -1.0, 0.0}; + CHECK(reference.map_position(origin, &previous, &position).ok()); + CHECK(std::fabs(position.direction[1] + 1.0) < 1e-15); + CHECK(reference.map_position(origin, nullptr, &position).ok()); + CHECK(std::fabs(position.direction[0] - 1.0) < 1e-15); + + joc::binaural::PointSourceReference overridden; + CHECK(overridden.configure("far", true, 3.5).ok()); + CHECK(overridden.mode() == "far"); + CHECK(std::fabs(overridden.reference_distance_m() - 3.5) < 1e-15); + CHECK(!overridden.configure("far", true, 0.0).ok()); +} + +// Six first-order image sources of the project shoebox, against the reference. +void test_shoebox_room() { + const joc::binaural::ShoeboxRoomConfig config; + CHECK(config.validate().ok()); + joc::binaural::ShoeboxRoomConfig invalid; + invalid.listener_position_m = {0.0, 9.0, 7.0}; + CHECK(!invalid.validate().ok()); + + const std::array direction = {0.0, 1.0, 0.0}; + std::vector paths; + CHECK(joc::binaural::first_order_image_sources(direction, 2.0, 48000.0, config, &paths).ok()); + CHECK(paths.size() == 6u); + const char* const walls[6] = {"left", "right", "back", "front", "floor", "ceiling"}; + const double distances[6] = {18.110770276275, 18.110770276275, 20.0, 16.0, + 14.142135623731, 14.142135623731}; + const double delays[6] = {2252.598232627999, 2252.598232627999, 2516.749198951355, + 1957.471599184387, 1697.706116921310, 1697.706116921310}; + const double gains[6] = {0.600341066264, 0.580975225417, 0.559491370220, 0.593156903749, + 0.507524274007, 0.546564602777}; + for (std::size_t index = 0; index < 6u; ++index) { + CHECK(paths[index].wall == walls[index]); + CHECK(std::fabs(paths[index].path_distance_m - distances[index]) < 1e-9); + CHECK(std::fabs(paths[index].extra_delay_samples - delays[index]) < 1e-6); + CHECK(std::fabs(paths[index].reflection_gain - gains[index]) < 1e-9); } + CHECK(std::fabs(paths[0].direction_adm[0] + 0.993883734674) < 1e-9); + CHECK(std::fabs(paths[1].direction_adm[0] - 0.993883734674) < 1e-9); + CHECK(std::fabs(paths[2].direction_adm[1] + 1.0) < 1e-12); + + // A source outside the shoebox is rejected instead of producing a path. + std::vector outside; + CHECK(!joc::binaural::first_order_image_sources(direction, 12.0, 48000.0, config, &outside).ok()); + + joc::binaural::ShoeboxRoomConfig compact; + compact.dimensions_m = {8.5, 11.3, 5.2}; + compact.listener_position_m = {4.25, 5.65, 2.6}; + CHECK(compact.validate().ok()); } void test_dbmd_auto_trim() { @@ -1419,7 +1506,9 @@ int main() { test_bed_layout_map(); test_partitioned_convolution(); test_spherical_mesh(); - test_sphere_transfer(); + test_reference_distance(); + test_point_source(); + test_shoebox_room(); test_dbmd_auto_trim(); test_int24_packing(); test_utf8_paths();