Keep only the Rosella and .jochrtf binaural paths
The kernel renders two inputs now: the Rosella .personalized_headphone model and an already compiled .jochrtf field. The SOFA reader, its compile cache and the field fit leave the kernel, and the direct SOFA runtime built on them goes with them: the point-source distance reference, the shoebox image-source model, the generated FDN tables and the asset archives are all gone. Fitting a SOFA into a .jochrtf belongs to the offline toolchain. The compiled-field runtime is the one that predates that work: 64-QMF analysis, 77 hybrid bands, per-object path delays and the project room (image sources plus the shared unitary FDN) computed in code. It keeps its behaviour and takes the .jochrtf name throughout: sofa_binaural_renderer.cpp -> jochrtf_renderer.cpp, binaural_runtime.* -> jochrtf_runtime.*, SofaBinauralRuntime -> JochrtfRuntime, ejoc_sofa_binaural_* -> ejoc_jochrtf_*. The room becomes a switch. --jochrtf-room on|off (default on) drives the enable_early_reflections and enable_late_room flags of the existing room configuration; off renders the field's direct binaural sound only. joc_task_config and joc_stream_config drop hrtf_sofa_path, hrtf_cache_dir, hrtf_cache_policy and hrtf_radius_m, and gain jochrtf_room. The CLI no longer accepts --sofa-hrtf, --hrtf-cache-policy, --hrtf-cache-dir or --hrtf-radius-m; without an explicit input the renderer uses HRTF/binaural.personalized_headphone, and a .jochrtf has to be named through --compiled-hrtf-cache. The SOFA-only unit tests go with the reader. Rosella parsing, filterbank, room and timing are untouched. Both .jochrtf modes reproduce the Python reference sample for sample, and ctest passes.
This commit is contained in:
1 parent
29f4f83c29
commit
20f2e77664
46 files changed
+1180
-8377
No files matched your search
@@ -17,17 +17,8 @@
|
||||
#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"
|
||||
#include "hrtf/sofa_cache.h"
|
||||
#include "hrtf/rosella_model.h"
|
||||
#include "hrtf/rosella_renderer.h"
|
||||
#include "hrtf/sofa_field.h"
|
||||
#include "hrtf/spherical_mesh.h"
|
||||
#include "foundation/mini_json.h"
|
||||
#include "foundation/sha256.h"
|
||||
#include "io/inflate.h"
|
||||
@@ -37,7 +28,6 @@
|
||||
#include "io/process.h"
|
||||
#include "io/wav_writer.h"
|
||||
#include "io/zip_reader.h"
|
||||
#include "joc_bitstream/bed_layout_map.h"
|
||||
#include "oamd/oamd_parser.h"
|
||||
#include "stream/stream.h"
|
||||
#include "task/task.h"
|
||||
@@ -301,178 +291,6 @@ void test_oamd_and_adm_helpers() {
|
||||
// DBMD segment 10: the nine configuration trims must default to automatic (bit 0
|
||||
// of each trim's first byte), and every other field of the block must keep the
|
||||
// layout the writer has always produced.
|
||||
// Spherical interpolation mesh: a 200-point Fibonacci sphere exercises the hull,
|
||||
// the barycentric weights and the Voronoi areas against values captured from the
|
||||
// reference implementation.
|
||||
void test_spherical_mesh() {
|
||||
constexpr int kPoints = 200;
|
||||
const double pi = 3.14159265358979323846;
|
||||
std::vector<double> directions;
|
||||
directions.reserve(static_cast<std::size_t>(kPoints) * 3u);
|
||||
for (int index = 0; index < kPoints; ++index) {
|
||||
const double z = 1.0 - 2.0 * (static_cast<double>(index) + 0.5) / kPoints;
|
||||
const double phi = static_cast<double>(index) * pi * (3.0 - std::sqrt(5.0));
|
||||
const double radius = std::sqrt(std::max(0.0, 1.0 - z * z));
|
||||
directions.push_back(radius * std::cos(phi));
|
||||
directions.push_back(radius * std::sin(phi));
|
||||
directions.push_back(z);
|
||||
}
|
||||
joc::hrtf::SphericalMesh mesh;
|
||||
joc::Status status = mesh.build(directions, kPoints);
|
||||
CHECK(status.ok());
|
||||
CHECK(mesh.vertex_count() == 200u);
|
||||
CHECK(mesh.face_count() == 396u);
|
||||
|
||||
const double query[3] = {0.30304576336566319, 0.5050762722761053, 0.80812203564176865};
|
||||
const int expected_ids[3] = {16, 11, 24};
|
||||
const double expected_weights[3] = {0.18552244832037465, 0.23319383531074916,
|
||||
0.5812837163688762};
|
||||
std::vector<int> ids;
|
||||
std::vector<double> weights;
|
||||
status = mesh.weights(query, &ids, &weights);
|
||||
CHECK(status.ok());
|
||||
CHECK(ids.size() == 3u);
|
||||
double difference = 0.0;
|
||||
for (std::size_t index = 0; index < ids.size(); ++index) {
|
||||
double expected = 0.0;
|
||||
for (int corner = 0; corner < 3; ++corner) {
|
||||
if (expected_ids[corner] == ids[index]) {
|
||||
expected = expected_weights[corner];
|
||||
}
|
||||
}
|
||||
difference += std::fabs(weights[index] - expected);
|
||||
}
|
||||
CHECK(difference < 1e-9);
|
||||
CHECK(std::fabs(weights[0] + weights[1] + weights[2] - 1.0) < 1e-12);
|
||||
|
||||
std::vector<double> areas;
|
||||
mesh.voronoi_areas(&areas);
|
||||
CHECK(areas.size() == 200u);
|
||||
double smallest = areas[0];
|
||||
double largest = areas[0];
|
||||
double total = 0.0;
|
||||
for (const double area : areas) {
|
||||
smallest = std::min(smallest, area);
|
||||
largest = std::max(largest, area);
|
||||
total += area;
|
||||
}
|
||||
CHECK(std::fabs(total - 1.0) < 1e-12);
|
||||
CHECK(std::fabs(largest / smallest - 1.1197648114672005) < 1e-9);
|
||||
}
|
||||
|
||||
// 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<double, 3> 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<double, 3> 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<double, 3> 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<double, 3> 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<double, 3> origin = {0.0, 0.0, 0.0};
|
||||
const std::array<double, 3> 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<double, 3> direction = {0.0, 1.0, 0.0};
|
||||
std::vector<joc::binaural::EarlyReflection> 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<joc::binaural::EarlyReflection> 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() {
|
||||
std::string dbmd;
|
||||
CHECK(joc::adm::build_dbmd(25, joc::adm::BinauralMode::Mid, &dbmd).ok());
|
||||
@@ -714,261 +532,10 @@ constexpr double kPi = 3.14159265358979323846;
|
||||
// `taps` decaying taps per ear. The values are closed-form placeholders; what
|
||||
// the canonicaliser and the spherical-harmonic fit actually read is the
|
||||
// geometry and the shape.
|
||||
joc::hrtf::SofaHrir synthetic_sofa(std::uint32_t measurements, std::uint32_t taps) {
|
||||
joc::hrtf::SofaHrir sofa;
|
||||
sofa.sample_rate = 48000.0;
|
||||
sofa.sampling_rate_units = "hertz";
|
||||
sofa.ir_count = measurements;
|
||||
sofa.ir_length = taps;
|
||||
sofa.conventions = "SOFA";
|
||||
sofa.sofa_conventions = "SimpleFreeFieldHRIR";
|
||||
sofa.convention_version = "1.0";
|
||||
sofa.version = "1.0";
|
||||
sofa.data_type = "FIR";
|
||||
sofa.room_type = "free field";
|
||||
sofa.receiver_position[0] = 0.0;
|
||||
sofa.receiver_position[1] = 0.09;
|
||||
sofa.receiver_position[2] = 0.0;
|
||||
sofa.receiver_position[3] = 0.0;
|
||||
sofa.receiver_position[4] = -0.09;
|
||||
sofa.receiver_position[5] = 0.0;
|
||||
sofa.source_position_coordinates.type = "spherical";
|
||||
sofa.source_position_coordinates.units = "degree, degree, metre";
|
||||
sofa.emitter_position_coordinates.type = "cartesian";
|
||||
sofa.emitter_position_coordinates.units = "metre";
|
||||
sofa.listener_position_coordinates.type = "cartesian";
|
||||
sofa.listener_position_coordinates.units = "metre";
|
||||
sofa.listener_view_coordinates.type = "cartesian";
|
||||
sofa.listener_view_coordinates.units = "metre";
|
||||
sofa.receiver_position_coordinates.type = "cartesian";
|
||||
sofa.receiver_position_coordinates.units = "metre";
|
||||
sofa.source_sha256 = std::string(64u, '0');
|
||||
sofa.source_position.assign(static_cast<std::size_t>(measurements) * 3u, 0.0);
|
||||
sofa.ir.assign(static_cast<std::size_t>(measurements) * 2u * taps, 0.0);
|
||||
for (std::uint32_t index = 0; index < measurements; ++index) {
|
||||
// Fibonacci sphere: an even spread over the whole sphere, every point at
|
||||
// one metre, so the set is a single shell.
|
||||
const double unit = (static_cast<double>(index) + 0.5) / static_cast<double>(measurements);
|
||||
const double elevation = std::asin(2.0 * unit - 1.0);
|
||||
const double azimuth =
|
||||
2.0 * kPi * std::fmod(static_cast<double>(index) * 0.6180339887498949, 1.0);
|
||||
sofa.source_position[index * 3u + 0u] = azimuth * 180.0 / kPi;
|
||||
sofa.source_position[index * 3u + 1u] = elevation * 180.0 / kPi;
|
||||
sofa.source_position[index * 3u + 2u] = 1.0;
|
||||
for (std::uint32_t ear = 0; ear < 2u; ++ear) {
|
||||
for (std::uint32_t tap = 0; tap < taps; ++tap) {
|
||||
const double decay =
|
||||
std::exp(-4.0 * static_cast<double>(tap) / static_cast<double>(taps));
|
||||
const double phase = 0.2 * static_cast<double>(tap) +
|
||||
0.05 * static_cast<double>(index) +
|
||||
1.5 * static_cast<double>(ear) + elevation;
|
||||
sofa.ir[(static_cast<std::size_t>(index) * 2u + ear) * taps + tap] =
|
||||
decay * std::sin(phase);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sofa;
|
||||
}
|
||||
|
||||
void test_sofa_canonicalize() {
|
||||
const joc::hrtf::SofaHrir sofa = synthetic_sofa(64u, 64u);
|
||||
|
||||
joc::hrtf::CanonicalHrtf canonical;
|
||||
const joc::Status status = joc::hrtf::canonicalize_sofa(sofa, &canonical);
|
||||
CHECK(status.ok());
|
||||
if (!status.ok()) {
|
||||
std::printf(" canonicalize_sofa failed: %s\n", status.message().c_str());
|
||||
return;
|
||||
}
|
||||
CHECK(canonical.measurements == 64u && canonical.taps == 64u);
|
||||
CHECK(canonical.sample_rate_hz == 48000.0);
|
||||
CHECK(canonical.hrir.size() == 64u * 2u * 64u);
|
||||
CHECK(canonical.delay_samples.size() == 64u * 2u);
|
||||
CHECK(canonical.unit_directions.size() == 64u * 3u);
|
||||
CHECK(canonical.source_position_cartesian_m.size() == 64u * 3u);
|
||||
CHECK(canonical.left_receiver_index != canonical.right_receiver_index);
|
||||
CHECK(canonical.left_receiver_index >= 0 && canonical.left_receiver_index < 2);
|
||||
CHECK(canonical.right_receiver_index >= 0 && canonical.right_receiver_index < 2);
|
||||
bool unit_length = canonical.unit_directions.size() == 64u * 3u;
|
||||
bool one_shell = canonical.measurement_radius_m.size() == 64u;
|
||||
for (std::size_t index = 0u; index < 64u; ++index) {
|
||||
const double x = canonical.unit_directions[index * 3u + 0u];
|
||||
const double y = canonical.unit_directions[index * 3u + 1u];
|
||||
const double z = canonical.unit_directions[index * 3u + 2u];
|
||||
unit_length = unit_length && std::abs(x * x + y * y + z * z - 1.0) < 1.0e-9;
|
||||
one_shell = one_shell && std::abs(canonical.measurement_radius_m[index] - 1.0) < 1.0e-9;
|
||||
}
|
||||
CHECK(unit_length);
|
||||
CHECK(one_shell);
|
||||
|
||||
// The shell the compile step selects, and the radius it reports for it.
|
||||
double actual_radius = 0.0;
|
||||
const std::vector<std::size_t> shell =
|
||||
joc::hrtf::canonical_shell_indices(canonical, 0.5, &actual_radius);
|
||||
CHECK(shell.size() == 64u);
|
||||
CHECK(std::abs(actual_radius - 1.0) < 1.0e-9);
|
||||
|
||||
// The import is strict: each of these is a rejection, never a silent repair.
|
||||
joc::hrtf::CanonicalHrtf rejected;
|
||||
joc::hrtf::SofaHrir broken = sofa;
|
||||
broken.conventions = "not-sofa";
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.sofa_conventions = "SimpleFreeFieldHRTF";
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.convention_version = "9.9";
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.data_type = "FLOAT";
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.room_type = "";
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.sampling_rate_units = "furlongs";
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.sample_rate = 0.0;
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.ir[0] = std::numeric_limits<double>::quiet_NaN();
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.delay[1] = -1.0;
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.emitter_position[0] = 0.5;
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.listener_view[0] = broken.listener_view[1] = broken.listener_view[2] = 0.0;
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.listener_up[0] = 1.0; // parallel to ListenerView
|
||||
broken.listener_up[1] = 0.0;
|
||||
broken.listener_up[2] = 0.0;
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.source_position[2] = -1.0; // a negative spherical radius
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
broken = sofa;
|
||||
broken.ir.resize(7u);
|
||||
CHECK(!joc::hrtf::canonicalize_sofa(broken, &rejected).ok());
|
||||
}
|
||||
|
||||
void test_sofa_compile_and_cache() {
|
||||
const joc::hrtf::SofaHrir sofa = synthetic_sofa(64u, 64u);
|
||||
const joc::hrtf::CompileOptions options = joc::hrtf::CompileOptions();
|
||||
joc::hrtf::Field field;
|
||||
|
||||
// The runtime field is fixed at fifth order and at 48 kHz.
|
||||
joc::hrtf::CompileOptions wrong_order = options;
|
||||
wrong_order.order = 3;
|
||||
CHECK(!joc::hrtf::compile_sofa_field(sofa, wrong_order, &field).ok());
|
||||
joc::hrtf::SofaHrir wrong_rate = sofa;
|
||||
wrong_rate.sample_rate = 44100.0;
|
||||
CHECK(!joc::hrtf::compile_sofa_field(wrong_rate, options, &field).ok());
|
||||
|
||||
const joc::Status status = joc::hrtf::compile_sofa_field(sofa, options, &field);
|
||||
CHECK(status.ok());
|
||||
if (!status.ok()) {
|
||||
std::printf(" compile_sofa_field failed: %s\n", status.message().c_str());
|
||||
return;
|
||||
}
|
||||
CHECK(field.order == 5);
|
||||
CHECK(field.measurement_radius_m == 1.0);
|
||||
CHECK(field.band_centers_hz.size() == 77u);
|
||||
CHECK(field.coefficients.size() == 36u * 2u * 77u * 2u);
|
||||
CHECK(field.delay_coefficients.size() == 36u * 2u);
|
||||
CHECK(field.delay_bounds.size() == 4u);
|
||||
CHECK(!field.cache_key.empty() && !field.payload_sha256.empty());
|
||||
CHECK(!field.metadata_json.empty() && !field.delay_source.empty());
|
||||
bool finite = true;
|
||||
for (const double value : field.coefficients) {
|
||||
finite = finite && std::isfinite(value);
|
||||
}
|
||||
for (const double value : field.delay_coefficients) {
|
||||
finite = finite && std::isfinite(value);
|
||||
}
|
||||
CHECK(finite);
|
||||
// The payload digest is a pure function of the compiled arrays.
|
||||
CHECK(joc::hrtf::field_payload_sha256(field) == field.payload_sha256);
|
||||
|
||||
// The cache round-trips through the shipped writer and loader.
|
||||
const std::string cache_path = joc::fs_utf8::temp_directory() + "/joc_test_cache.jochrtf";
|
||||
CHECK(joc::hrtf::write_jochrtf(field, cache_path).ok());
|
||||
joc::hrtf::Field reloaded;
|
||||
const joc::Status loaded = joc::hrtf::load_jochrtf(cache_path, &reloaded);
|
||||
CHECK(loaded.ok());
|
||||
if (loaded.ok()) {
|
||||
CHECK(reloaded.cache_key == field.cache_key);
|
||||
CHECK(reloaded.source_sha256 == field.source_sha256);
|
||||
CHECK(reloaded.order == field.order);
|
||||
CHECK(reloaded.measurement_radius_m == field.measurement_radius_m);
|
||||
CHECK(reloaded.band_centers_hz == field.band_centers_hz);
|
||||
CHECK(reloaded.coefficients == field.coefficients);
|
||||
CHECK(reloaded.delay_coefficients == field.delay_coefficients);
|
||||
CHECK(reloaded.delay_bounds == field.delay_bounds);
|
||||
CHECK(reloaded.payload_sha256 == field.payload_sha256);
|
||||
}
|
||||
// A cache is trusted only after its key and payload verify; a damaged one is
|
||||
// rejected instead of being used.
|
||||
CHECK(joc::hrtf::validate_jochrtf(cache_path, field.source_sha256, field.cache_key, nullptr)
|
||||
.ok());
|
||||
CHECK(!joc::hrtf::validate_jochrtf(cache_path, field.source_sha256, "00", nullptr).ok());
|
||||
CHECK(joc::fs_utf8::remove(cache_path) == 0);
|
||||
|
||||
const std::string poisoned = joc::fs_utf8::temp_directory() + "/joc_test_poisoned.jochrtf";
|
||||
CHECK(joc::io::write_zip(poisoned, {}, nullptr));
|
||||
CHECK(!joc::hrtf::validate_jochrtf(poisoned, field.source_sha256, field.cache_key, nullptr)
|
||||
.ok());
|
||||
CHECK(!joc::hrtf::load_jochrtf(poisoned, &reloaded).ok());
|
||||
CHECK(joc::fs_utf8::remove(poisoned) == 0);
|
||||
}
|
||||
|
||||
void test_sofa_reader_errors() {
|
||||
// No SOFA file is needed to check that the reader answers correctly: a path
|
||||
// that does not exist and a file that is not HDF5 are distinct, reported
|
||||
// errors rather than a partially filled structure.
|
||||
joc::hrtf::SofaHrir sofa;
|
||||
const std::string missing = joc::fs_utf8::temp_directory() + "/joc_absent.sofa";
|
||||
const joc::Status absent = joc::hrtf::load_sofa(missing, &sofa);
|
||||
CHECK(!absent.ok());
|
||||
CHECK(!absent.message().empty());
|
||||
CHECK(!joc::hrtf::load_sofa(std::string(), &sofa).ok());
|
||||
CHECK(!joc::hrtf::load_sofa(missing, nullptr).ok());
|
||||
|
||||
const std::string garbage = joc::fs_utf8::temp_directory() + "/joc_garbage.sofa";
|
||||
{
|
||||
std::ofstream stream = joc::fs_utf8::open_output(garbage);
|
||||
stream << "this is not an HDF5 container";
|
||||
}
|
||||
const joc::Status malformed = joc::hrtf::load_sofa(garbage, &sofa);
|
||||
CHECK(!malformed.ok());
|
||||
CHECK(!malformed.message().empty());
|
||||
CHECK(joc::fs_utf8::remove(garbage) == 0);
|
||||
}
|
||||
|
||||
void test_hrtf_cache_policy() {
|
||||
// The policy parser and the miss path of the cache layer, neither of which
|
||||
// needs a SOFA file.
|
||||
joc::hrtf::CachePolicy policy = joc::hrtf::CachePolicy::None;
|
||||
CHECK(joc::hrtf::parse_cache_policy("none", &policy).ok() &&
|
||||
policy == joc::hrtf::CachePolicy::None);
|
||||
CHECK(joc::hrtf::parse_cache_policy("memory", &policy).ok() &&
|
||||
policy == joc::hrtf::CachePolicy::Memory);
|
||||
CHECK(joc::hrtf::parse_cache_policy("disk", &policy).ok() &&
|
||||
policy == joc::hrtf::CachePolicy::Disk);
|
||||
CHECK(!joc::hrtf::parse_cache_policy("sometimes", &policy).ok());
|
||||
|
||||
joc::hrtf::SofaFieldRequest request;
|
||||
request.sofa_path = joc::fs_utf8::temp_directory() + "/joc_absent.sofa";
|
||||
request.policy = joc::hrtf::CachePolicy::Disk;
|
||||
request.cache_dir = joc::fs_utf8::temp_directory() + "/joc_absent_cache";
|
||||
joc::hrtf::Field field;
|
||||
std::string written = "sentinel";
|
||||
CHECK(!joc::hrtf::load_or_compile_sofa_field(request, &field, &written).ok());
|
||||
}
|
||||
|
||||
void test_rosella_model_errors() {
|
||||
// A real model is proprietary user data and is not part of this repository.
|
||||
@@ -1312,186 +879,6 @@ void test_clipgain_reset_clears_the_previous_gain() {
|
||||
sizeof(float) * after_reset.size()) == 0);
|
||||
}
|
||||
|
||||
// CBA bed:每个声道的方位来自它的标签(ETSI TS 103 420 V1.2.1 table B.10),
|
||||
// 声道顺序来自 table 12/13,因此 bed 声道不会随输出布局移动。
|
||||
void test_bed_layout_map() {
|
||||
using joc::joc::BedChannel;
|
||||
using joc::joc::BedLayout;
|
||||
|
||||
double xyz[3] = {0.0, 0.0, 0.0};
|
||||
CHECK(joc::joc::bed_channel_adm_position(BedChannel::kLeft, xyz));
|
||||
CHECK(xyz[0] == -1.0 && xyz[1] == 1.0 && xyz[2] == 0.0);
|
||||
CHECK(joc::joc::bed_channel_adm_position(BedChannel::kCenter, xyz));
|
||||
CHECK(xyz[0] == 0.0 && xyz[1] == 1.0 && xyz[2] == 0.0);
|
||||
CHECK(joc::joc::bed_channel_adm_position(BedChannel::kTopFrontLeft, xyz));
|
||||
CHECK(xyz[0] == -1.0 && xyz[1] == 1.0 && xyz[2] == 1.0);
|
||||
CHECK(joc::joc::bed_channel_adm_position(BedChannel::kTopFrontRight, xyz));
|
||||
CHECK(xyz[0] == 1.0 && xyz[1] == 1.0 && xyz[2] == 1.0);
|
||||
CHECK(joc::joc::bed_channel_adm_position(BedChannel::kTopSideLeft, xyz));
|
||||
CHECK(xyz[0] == -1.0 && xyz[1] == 0.0 && xyz[2] == 1.0);
|
||||
CHECK(joc::joc::bed_channel_adm_position(BedChannel::kTopBackLeft, xyz));
|
||||
CHECK(xyz[0] == -1.0 && xyz[1] == -1.0 && xyz[2] == 1.0);
|
||||
CHECK(joc::joc::bed_channel_adm_position(BedChannel::kTopBackRight, xyz));
|
||||
CHECK(xyz[0] == 1.0 && xyz[1] == -1.0 && xyz[2] == 1.0);
|
||||
CHECK(joc::joc::bed_channel_adm_position(BedChannel::kLeftWide, xyz));
|
||||
CHECK(xyz[0] == -1.0 && xyz[1] == 21.0 / 31.0 && xyz[2] == 0.0);
|
||||
CHECK(joc::joc::bed_channel_adm_position(BedChannel::kRightWide, xyz));
|
||||
CHECK(xyz[0] == 1.0 && xyz[1] == 21.0 / 31.0 && xyz[2] == 0.0);
|
||||
CHECK(joc::joc::bed_channel_adm_position(BedChannel::kLfe, xyz));
|
||||
CHECK(xyz[0] == -1.0 && xyz[1] == 1.0 && xyz[2] == -1.0);
|
||||
CHECK(joc::joc::bed_channel_adm_position(BedChannel::kLfe2, xyz));
|
||||
CHECK(xyz[0] == 1.0 && xyz[1] == 1.0 && xyz[2] == -1.0);
|
||||
|
||||
std::uint16_t q15[3] = {0, 0, 0};
|
||||
CHECK(joc::joc::bed_channel_q15(BedChannel::kLeft, q15));
|
||||
CHECK(q15[0] == 0 && q15[1] == 0 && q15[2] == 0);
|
||||
CHECK(joc::joc::bed_channel_q15(BedChannel::kTopFrontLeft, q15));
|
||||
CHECK(q15[0] == 0 && q15[1] == 0 && q15[2] == 32767);
|
||||
CHECK(joc::joc::bed_channel_q15(BedChannel::kLeftWide, q15));
|
||||
CHECK(q15[0] == 0 && q15[1] == 5285 && q15[2] == 0);
|
||||
CHECK(joc::joc::bed_channel_q15(BedChannel::kTopBackLeft, q15));
|
||||
CHECK(q15[0] == 0 && q15[1] == 32767 && q15[2] == 32767);
|
||||
|
||||
// table 12:0x0F 是 5.1,0x1FF 是 9.1.6(不含 LFE2)。
|
||||
std::vector<joc::oamd::BedAssignment> standard(1);
|
||||
standard[0].standard = true;
|
||||
standard[0].mask = 0x0Fu;
|
||||
const BedLayout five_one = joc::joc::resolve_bed_layout(standard, true);
|
||||
CHECK(five_one.present && five_one.bed_only && five_one.complete);
|
||||
CHECK(std::string(five_one.name) == "5.1");
|
||||
CHECK(five_one.channels.size() == 5);
|
||||
CHECK(five_one.channels[0] == BedChannel::kLeft);
|
||||
CHECK(five_one.channels[1] == BedChannel::kRight);
|
||||
CHECK(five_one.channels[2] == BedChannel::kCenter);
|
||||
CHECK(five_one.channels[3] == BedChannel::kLeftSurround);
|
||||
CHECK(five_one.channels[4] == BedChannel::kRightSurround);
|
||||
|
||||
standard[0].mask = 0x1FFu;
|
||||
const BedLayout nine_six = joc::joc::resolve_bed_layout(standard, true);
|
||||
CHECK(std::string(nine_six.name) == "9.1.6");
|
||||
CHECK(nine_six.channels.size() == 15);
|
||||
CHECK(nine_six.channels[0] == BedChannel::kLeft);
|
||||
CHECK(nine_six.channels[2] == BedChannel::kCenter);
|
||||
CHECK(nine_six.channels[3] == BedChannel::kLeftSurround);
|
||||
CHECK(nine_six.channels[7] == BedChannel::kTopFrontLeft);
|
||||
CHECK(nine_six.channels[9] == BedChannel::kTopSideLeft);
|
||||
CHECK(nine_six.channels[11] == BedChannel::kTopBackLeft);
|
||||
CHECK(nine_six.channels[13] == BedChannel::kLeftWide);
|
||||
CHECK(nine_six.channels[14] == BedChannel::kRightWide);
|
||||
// LFE 留在声明掩码里,但不占 JOC 对象槽。
|
||||
CHECK(joc::joc::bed_layout_has(nine_six, BedChannel::kLfe));
|
||||
bool lfe_slot = false;
|
||||
for (const BedChannel channel : nine_six.channels) {
|
||||
lfe_slot = lfe_slot || channel == BedChannel::kLfe ||
|
||||
channel == BedChannel::kLfe2;
|
||||
}
|
||||
CHECK(!lfe_slot);
|
||||
CHECK(joc::joc::bed_layout_has(nine_six, BedChannel::kRightWide));
|
||||
|
||||
// table 13:17 bit 掩码的 channel order 与 table 12 相同。
|
||||
std::vector<joc::oamd::BedAssignment> nonstandard(1);
|
||||
nonstandard[0].standard = false;
|
||||
nonstandard[0].mask = 0x3Fu;
|
||||
const BedLayout ns_five = joc::joc::resolve_bed_layout(nonstandard, true);
|
||||
CHECK(ns_five.channels.size() == 5);
|
||||
CHECK(ns_five.channels[0] == BedChannel::kLeft);
|
||||
CHECK(ns_five.channels[4] == BedChannel::kRightSurround);
|
||||
nonstandard[0].mask = 0xFFFFu;
|
||||
const BedLayout ns_nine = joc::joc::resolve_bed_layout(nonstandard, true);
|
||||
CHECK(ns_nine.channels.size() == 15);
|
||||
CHECK(ns_nine.channels[0] == BedChannel::kLeft);
|
||||
CHECK(ns_nine.channels[13] == BedChannel::kLeftWide);
|
||||
CHECK(ns_nine.channels[14] == BedChannel::kRightWide);
|
||||
}
|
||||
|
||||
// Partitioned convolution: overlap-add with a shared input history and a filter
|
||||
// crossfade. The expected values come from the reference
|
||||
// partitioned_convolution.py running the same filters and blocks.
|
||||
void test_partitioned_convolution() {
|
||||
constexpr std::size_t kBlock = 128;
|
||||
constexpr std::size_t kTaps = 300;
|
||||
constexpr std::size_t kMaxTaps = 512;
|
||||
constexpr std::size_t kOutputs = 2;
|
||||
|
||||
auto make_filter = [](double step, double decay, bool cosine, double offset) {
|
||||
std::vector<double> values(kOutputs * kTaps);
|
||||
for (std::size_t output = 0; output < kOutputs; ++output) {
|
||||
for (std::size_t tap = 0; tap < kTaps; ++tap) {
|
||||
const double angle = step * static_cast<double>(tap + 1) *
|
||||
(static_cast<double>(output) + offset);
|
||||
const double trig = cosine ? std::cos(angle) : std::sin(angle);
|
||||
values[output * kTaps + tap] = trig * std::exp(-static_cast<double>(tap) / decay);
|
||||
}
|
||||
}
|
||||
return values;
|
||||
};
|
||||
|
||||
joc::hrtf::UniformPartitionedConvolver convolver(kOutputs, kMaxTaps, kBlock);
|
||||
const std::vector<double> first = make_filter(0.017, 100.0, false, 1.0);
|
||||
CHECK(convolver.set_filter(first.data(), kTaps, 0).ok());
|
||||
CHECK(!convolver.transition_active());
|
||||
|
||||
std::vector<double> block(kBlock);
|
||||
std::vector<double> out(kBlock * kOutputs);
|
||||
std::vector<double> rendered;
|
||||
for (std::size_t index = 0; index < 8; ++index) {
|
||||
for (std::size_t sample = 0; sample < kBlock; ++sample) {
|
||||
const double n = static_cast<double>(index * kBlock + sample);
|
||||
block[sample] = std::sin(0.01 * n) + 0.5 * std::cos(0.003 * n);
|
||||
}
|
||||
CHECK(convolver.process_block(block.data(), out.data()).ok());
|
||||
rendered.insert(rendered.end(), out.begin(), out.end());
|
||||
}
|
||||
CHECK(rendered.size() == 8 * kBlock * kOutputs);
|
||||
CHECK(std::fabs(rendered[0] - 0.0084995905892490953) < 1e-9);
|
||||
CHECK(std::fabs(rendered[1] - 0.016996724855971479) < 1e-9);
|
||||
CHECK(std::fabs(rendered[2] - 0.025497145937656285) < 1e-9);
|
||||
CHECK(std::fabs(rendered[63] - 8.1367456949245849) < 1e-9);
|
||||
CHECK(std::fabs(rendered[64] - 4.6183248807335495) < 1e-9);
|
||||
CHECK(std::fabs(rendered[65] - 8.5945567424849969) < 1e-9);
|
||||
CHECK(std::fabs(rendered[127] - 24.744288415490566) < 1e-8);
|
||||
CHECK(std::fabs(rendered[128] - 16.21718594261781) < 1e-8);
|
||||
CHECK(std::fabs(rendered[129] - 25.258064041591062) < 1e-8);
|
||||
CHECK(std::fabs(rendered[255] - 42.867139711879446) < 1e-8);
|
||||
CHECK(std::fabs(rendered[256] - 47.087759632531053) < 1e-8);
|
||||
CHECK(std::fabs(rendered[511] - 30.307479702394744) < 1e-8);
|
||||
CHECK(std::fabs(rendered[512] - 64.6869976138059) < 1e-8);
|
||||
CHECK(std::fabs(rendered[700] - 26.847289638199864) < 1e-8);
|
||||
CHECK(std::fabs(rendered[701] - 2.9756928363101984) < 1e-8);
|
||||
CHECK(std::fabs(rendered[1023] - -25.703654152948225) < 1e-8);
|
||||
CHECK(std::fabs(rendered[1024] - -43.300148318757053) < 1e-8);
|
||||
|
||||
// A two-block crossfade keeps both filters' overlap tails alive.
|
||||
const std::vector<double> second = make_filter(0.011, 140.0, true, 2.0);
|
||||
CHECK(convolver.set_filter(second.data(), kTaps, 2).ok());
|
||||
CHECK(convolver.transition_active());
|
||||
std::vector<double> faded;
|
||||
for (std::size_t index = 0; index < 4; ++index) {
|
||||
for (std::size_t sample = 0; sample < kBlock; ++sample) {
|
||||
const double n = 1000.0 + static_cast<double>(index * kBlock + sample);
|
||||
block[sample] = std::sin(0.02 * n) + 0.3;
|
||||
}
|
||||
CHECK(convolver.process_block(block.data(), out.data()).ok());
|
||||
faded.insert(faded.end(), out.begin(), out.end());
|
||||
}
|
||||
CHECK(!convolver.transition_active());
|
||||
CHECK(std::fabs(faded[0] - -31.254215789646686) < 1e-8);
|
||||
CHECK(std::fabs(faded[1] - -30.520784088408671) < 1e-8);
|
||||
CHECK(std::fabs(faded[2] - -31.64727283056877) < 1e-8);
|
||||
CHECK(std::fabs(faded[127] - 45.044850086774559) < 1e-8);
|
||||
CHECK(std::fabs(faded[128] - 16.242938783563034) < 1e-8);
|
||||
CHECK(std::fabs(faded[129] - 45.566945939182197) < 1e-8);
|
||||
CHECK(std::fabs(faded[255] - -11.640284877460564) < 1e-8);
|
||||
CHECK(std::fabs(faded[256] - 20.660738168974696) < 1e-8);
|
||||
CHECK(std::fabs(faded[383] - -24.027948887675439) < 1e-8);
|
||||
CHECK(std::fabs(faded[384] - -41.675076381317034) < 1e-8);
|
||||
CHECK(std::fabs(faded[511] - 34.95547294171444) < 1e-8);
|
||||
CHECK(std::fabs(faded[512] - -4.9486250951335764) < 1e-8);
|
||||
|
||||
convolver.reset();
|
||||
CHECK(!convolver.transition_active());
|
||||
}
|
||||
|
||||
int main() {
|
||||
test_sha256();
|
||||
test_crc32_and_inflate();
|
||||
@@ -1503,12 +890,6 @@ int main() {
|
||||
test_task_result_json();
|
||||
test_npy();
|
||||
test_oamd_and_adm_helpers();
|
||||
test_bed_layout_map();
|
||||
test_partitioned_convolution();
|
||||
test_spherical_mesh();
|
||||
test_reference_distance();
|
||||
test_point_source();
|
||||
test_shoebox_room();
|
||||
test_dbmd_auto_trim();
|
||||
test_int24_packing();
|
||||
test_utf8_paths();
|
||||
@@ -1516,10 +897,6 @@ int main() {
|
||||
test_find_executable();
|
||||
test_builtin_kernel_tables();
|
||||
test_npy_and_zip_writers();
|
||||
test_sofa_canonicalize();
|
||||
test_sofa_compile_and_cache();
|
||||
test_sofa_reader_errors();
|
||||
test_hrtf_cache_policy();
|
||||
test_rosella_model_errors();
|
||||
test_rosella_filterbank_config();
|
||||
test_stream_render_ahead();
|
||||
|
||||
Reference in new issue
Block a user