Files
JustOneCacophony/src/oamd/oamd_math.h
T
TheM14 0bb82e4115
ci / ubuntu-latest (push) Failing after 19s
ci / macos-latest (push) Canceled after 0s
ci / windows-latest (push) Canceled after 0s
ci / release (push) Canceled after 0s
Port the OAMD metadata rendering and the frame-level JOC core
The JOC bitstream reports a per-frame parse status instead of treating every
anomaly as an exception: an unsupported downmix configuration, a clamped
object count, an unknown parameter-band count, bad offset_ts values and
trailing bytes each get their own status. The raw 5-bit offset_ts values are
kept as coded and validated to be strictly increasing per object, and the
clipgain uses 2^(x-4) * (1 + y/32).

The JOC core gains a frame-level state machine. It keeps the per-object
previous matrix and its presence marker, the seq counter, the 8-plane
passthrough history, the LFE clipgain ramp start and the previous actual
core-channel count. Normal, hold, hard-cut and passthrough frames are
distinguished: a frame without a previous state interpolates per the
data-point rules, an object that disappears keeps publishing a zero matrix,
a frame without metadata passes the 8 core planes through to slots 0..7 with
the 1217-sample delay, and a declared core-channel-count change hard-cuts and
clears the LFE ring while the clipgain ramp state is kept. The core LFE
applies the full clipgain as a sample-wise ramp before its delay.

The OAMD parser exposes the full frame state: every object's state in every
position block, the programme assignment and the trim element, with multiple
blocks and up to 30 declared objects accepted. The speaker renderer applies
this state: per-block gain/inactive, zone/elevation, the size kernel, snap,
divergence, element 5 refinement, the priority size budget, ISF basis
matrices and bed channel distribution, scheduled on the 32-sample metadata
grid with block alignment and gain ramps. The size kernel, ISF basis
assembly, bed routing and compensation are computed in oamd_math from the
speaker layout geometry; bed labels and reference positions derive from the
layout tables. The six ISF configurations and twelve layout descriptors are
compiled in as static tables (oamd/isf_tables.h); the JSON loader and the
configure-time embedding are gone. The layout tables gain 6.0, and the layout
lookup, bed map and trim probe follow.

The binaural position timeline consumes the full frame state as well: every
position block is scheduled at its own offset and ramp, and its track list
grows with the declared object count, so multi-block and over-16-object
programmes are rendered instead of rejected. The JOC binaural input stays one
LFE plus fifteen object sources; the extra tracks are followed but not
panned.

The file task and the stream parse the full state for speaker and binaural,
while the ADM export keeps the 15-track single-block view. The public headers
carry the new status values, the frame-level entry point and the raw offset_ts
description; the internal ABI versions go to 6 (joc_core), 2 (frame params and
EMDF info) and 4 (eac3joc).

ctest covers the frame-level core (clipgain formula and LFE ramp, offset_ts
validation, frame statuses, the 8-plane passthrough, the seq state machine and
the no-previous-state interpolation), the OAMD parser state, the compiled-in
ISF tables and the binaural timeline. The READMEs and the mathematical notes
describe the 6.0 layout, the frame state machine, the metadata application and
the ISF lookup tables.
2026-11-08 14:04:21 +08:00

102 lines
4.0 KiB
C++

// Port of src/oamd_math.py: float32 equations for the metadata application path.
//
// The routines implement the fixed float32 equations for the size tables, ISF
// basis assembly, bed channel routing and the position-dependent compensation.
// The size tables are generated from the region geometry with grouped equal-power
// panning, an S448 weight and a power sum; every operation is evaluated in
// float32 in a fixed order so the coefficients are reproducible.
//
// The caller passes the parameter mappings from oamd_params; this module does
// not read files itself.
#pragma once
#include <array>
#include <cstddef>
#include <cstdint>
#include <vector>
#include "oamd/oamd_params.h"
#include "speaker/speaker_layout_lookup.h"
#include "speaker/speaker_layouts.h"
namespace joc::oamd {
// The specified 2**x approximation used by the compensation kernels.
float exp2_approx(float value);
// The specified x**power approximation; power is a float32.
float pow_approx(float value, float power);
// Stable float32 normalisation with the tiny-norm floor.
std::vector<float> norm(const std::vector<float>& vector);
// One boundary weight of the size kernel.
float boundary(int axis, int edge, float position, float t);
// Maps a scalar size in [0, 1] to t on the size curve.
float size_curve_value(double size);
// Size kernel response in region-speaker order (the region point order).
std::vector<float> size_kernel(const ::ejoc::speaker_tables::RegionGeometry& region,
const float position[3], float t, bool height);
// Size-kernel target vector in layout channel order.
std::vector<float> size_targets(const MetadataParams& meta,
const ::ejoc::speaker_tables::LayoutGeometry& layout,
int region_index, const double position[3],
double size, bool height);
// One assembled ISF basis matrix (row_count x layout output_count, row major).
struct IsfBasis {
int rows = 0;
int columns = 0;
std::vector<float> values;
float at(int row, int column) const {
return values[static_cast<std::size_t>(row) * static_cast<std::size_t>(columns) +
static_cast<std::size_t>(column)];
}
};
// Assembles one ISF basis matrix from the compressed base tables.
IsfBasis assemble_isf(const MetadataParams& meta, const char* layout_key, int isf_idx);
// Returns the per-basis ISF compensation gain.
float isf_compensation(const MetadataParams& meta, const char* layout_key, int isf_idx,
int row, bool disabled);
// Per-label bed redistribution weight, in the reference-position index space
// used by the layout descriptors.
float bed_weight(int label, std::uint32_t mask, int target_label);
// The five height-candidate flags for one input bed label.
std::array<int, 5> bed_candidates(int label, std::uint32_t mask,
const std::vector<int>& output_labels);
// Route and weights for one input bed label. route == -1 means the returned
// weights are the target; otherwise route is the reference position used to
// synthesise a point source.
struct BedDistribution {
int route = -1;
std::vector<float> weights;
};
// Derives the bed routing of one input bed label from the target layout.
BedDistribution bed_distribution(const ::joc::speaker::LayoutInfo& layout, int label,
std::uint32_t mask, bool distribute);
// Returns the Q15 reference position used by the bed fallback path.
void bed_reference_position(int index, int q15[3]);
// Returns the bed path compensation gain for one input bed label. ``mode`` is
// the trim mode (0 automatic, 1 disabled, 2 explicit) and ``levels`` holds the
// surround, height and centre levels in dB.
float bed_compensation(const ::joc::speaker::LayoutInfo& layout, int label, int mode,
bool bypass, const double levels[3]);
// Automatic attenuation of a target layout in dB.
float bed_attenuation_db(const ::joc::speaker::LayoutInfo& layout);
} // namespace joc::oamd