Files
JustOneCacophony/src/hrtf/partitioned_convolution.h
T
TheM14 fd842db26f
ci / ubuntu-latest (push) Failing after 11s
ci / macos-latest (push) Canceled after 0s
ci / windows-latest (push) Canceled after 0s
ci / release (push) Canceled after 0s
Render SOFA HRIRs directly and place bed channels by their labels
Two changes that belong to the same reproduction model: a bed channel has
one position, defined by its label, and a SOFA set keeps the impulse
responses it was measured with.

CBA bed channels: the position comes from the channel's label in ETSI
TS 103 420 Table B.10, not from the layout the decoder is asked to render
to. The per-layout anchor table displaced bed channels onto the nearest
target speaker, which silently moved content whenever the target layout
changed.

- Drop the anchor table and the geometry lookup; bed_channel_q15 is the
  only source of bed positions, so stream and task no longer need a
  layout to ask for one
- Keep the 17-bit non-standard bed mask in channel order, as Table 13 /
  TS 103 190-2 Table 65 transmit it
- Exclude LFE channels from the object list while keeping them in the
  coverage mask
- Cover the Table B.10 coordinates, the q15 encoding, the 10-bit and
  17-bit masks and the LFE split in tests

SOFA rendering: compiling a SOFA set into a fifth-order directional field
threw away the measured impulse response - the SH projection, the
64-QMF/77-hybrid round trip and the synthetic room replaced it. The full
IRs are now kept and rendered as such, with the reproduction defined by a
configuration that names a personalized model rather than by the renderer
itself.

- Add SphericalMesh: incremental convex hull over the measurement
  directions, barycentric weights inside the containing triangle and
  spherical Voronoi areas for the centre spectrum
- Add FullHrirField: rfft of the selected shell, 200-1500 Hz phase fit for
  the delay, delay-aligned spectra, log-magnitude/phase or MCA
  interpolation, rigid-sphere radial ratio, recording origin and the
  common FFT/2 latency
- Add Uniform/MultiSourcePartitionedConvolver: block 128, numpy FFT
  conventions, zero-fade filter rebuild from the blocks in flight
- Move the Rosella reference arithmetic (profile geometry, direction
  basis, direct gains and room send, table-A room) out of the renderer
  into rosella_reference so both renderers share it
- Add ReferenceEnvironment and SofaReferenceRuntime: JSON configuration
  with model hash and distance-profile mapping, level calibration over
  equal-area directions, the reference room, 1536-sample batching and a
  single latency removal
- Keep .jochrtf an explicit input and the SOFA compile step behind
  --hrtf-cache-policy/--hrtf-cache-dir; add --reference-config and
  --sofa-interpolation, and make --hrtf-radius-m optional for a single
  shell
- Cover the mesh weights and Voronoi areas, the sphere series and the
  partitioned convolver with reference values in tests
- Document both rules in the READMEs and the binaural notes

Rosella renders stay byte-identical (d5877e89...) and so do renders of
the legacy compiled path (6f0a75d2...). Direct SOFA renders match the
Python reference to 1e-4 in level; the residual comes from the
barycentric solve (analytic inverse here, LAPACK there) on near-degenerate
triangles.
2026-11-01 02:54:55 +08:00

119 lines
4.8 KiB
C++

#pragma once
#include <complex>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <vector>
#include "foundation/fft.h"
#include "foundation/status.h"
// Uniform partitioned convolution, ported from the reference
// partitioned_convolution.py. One input history is shared by the outputs of a
// source; a filter replacement can crossfade the output over a number of blocks
// while both the old and the new filter keep their own overlap tail.
namespace joc::hrtf {
class UniformPartitionedConvolver {
public:
UniformPartitionedConvolver() = default;
UniformPartitionedConvolver(std::size_t outputs, std::size_t max_filter_taps,
std::size_t block_size);
void configure(std::size_t outputs, std::size_t max_filter_taps, std::size_t block_size);
std::size_t outputs() const { return outputs_; }
std::size_t block_size() const { return block_size_; }
std::size_t fft_size() const { return fft_size_; }
std::size_t max_filter_taps() const { return max_filter_taps_; }
bool transition_active() const { return target_active_; }
// `filters` is [outputs][taps] row-major and `taps` <= max_filter_taps. A zero
// fade applies the filter immediately and rebuilds the overlap tail from the
// blocks already processed, so an impulse in flight is not dropped.
Status set_filter(const double* filters, std::size_t taps, std::size_t fade_blocks);
// `block` is block_size doubles; `out` is block_size*outputs, sample-major.
Status process_block(const double* block, double* out);
void reset();
private:
void partition(const double* filters, std::size_t taps,
std::vector<std::complex<double>>* out);
void convolution_block(const std::vector<std::complex<double>>& spectra,
const std::vector<double>& overlap, double* out,
double* next_overlap);
void overlap_for_previous_block(const std::vector<std::complex<double>>& spectra,
double* overlap);
void order_history();
void real_spectrum(const double* time, std::vector<std::complex<double>>* out) const;
void real_signal(const std::vector<std::complex<double>>& spectrum, double* out) const;
std::size_t outputs_ = 0;
std::size_t max_filter_taps_ = 0;
std::size_t block_size_ = 0;
std::size_t fft_size_ = 0;
std::size_t bins_ = 0;
std::size_t partitions_ = 0;
std::unique_ptr<dsp::FftPlan> forward_plan_;
std::unique_ptr<dsp::FftPlan> inverse_plan_;
mutable std::vector<std::complex<double>> fft_buffer_;
std::vector<std::complex<double>> history_; // [partitions][bins]
std::vector<std::complex<double>> spectra_; // [outputs][partitions][bins]
std::vector<std::complex<double>> target_spectra_;
std::vector<double> overlap_; // [outputs][block]
std::vector<double> target_overlap_; // [outputs][block]
std::vector<std::complex<double>> work_; // [bins] scratch
std::vector<double> time_work_; // [fft_size] scratch
std::vector<std::complex<double>> ordered_; // [partitions][bins] scratch
std::ptrdiff_t write_index_ = -1;
std::uint64_t processed_blocks_ = 0;
bool target_active_ = false;
std::int64_t fade_samples_ = 0;
std::int64_t fade_position_ = 0;
};
class MultiSourcePartitionedConvolver {
public:
MultiSourcePartitionedConvolver() = default;
MultiSourcePartitionedConvolver(std::size_t source_count, std::size_t block_size,
std::size_t max_filter_taps,
std::size_t output_channels = 2);
void configure(std::size_t source_count, std::size_t block_size,
std::size_t max_filter_taps, std::size_t output_channels = 2);
std::size_t source_count() const { return convolvers_.size(); }
std::size_t block_size() const { return block_size_; }
std::size_t output_channels() const { return output_channels_; }
std::size_t max_filter_taps() const { return max_filter_taps_; }
const UniformPartitionedConvolver& convolver(std::size_t source) const {
return convolvers_[source];
}
Status set_filter(std::size_t source, const double* filters, std::size_t taps,
std::size_t fade_blocks);
// `sources` is block_size*source_count (sample-major); `out` is
// block_size*output_channels and is overwritten.
Status process_block(const double* sources, double* out);
void reset();
private:
std::vector<UniformPartitionedConvolver> convolvers_;
std::size_t block_size_ = 0;
std::size_t max_filter_taps_ = 0;
std::size_t output_channels_ = 2;
std::vector<double> source_block_;
std::vector<double> partial_;
};
} // namespace joc::hrtf