Render SOFA directly with full HRIR interpolation

SOFA inputs now use delay-aligned local interpolation and partitioned
convolution instead of compiling a compressed directional field.

- preserve full FIR support, complex phase and explicit delay handling
- add log-magnitude/phase and local magnitude-corrected interpolation
- support Near/Mid/Far with a shared calibration anchor
- retain .jochrtf as an explicit lightweight input and offline export
- keep personalized headphone rendering unchanged
- update the CLI, reports and bilingual documentation

Checked: 21 automated tests, 8 bit-exact personalized regressions,
and full-song D2 renders in Near/Mid/Far.
This commit is contained in:
TheM14 committed 2026-11-01 00:57:49 +08:00
1 parent 0e98e91762
commit faf8a921c2
13 files changed
+1140 -591

No files matched your search

+137
View File
@@ -0,0 +1,137 @@
"""Configured reference environment, geometry and scalar level calibration."""
from __future__ import annotations
from dataclasses import dataclass
import hashlib
import json
import math
import os
from pathlib import Path
import numpy as np
from rosella_bank import RosellaBank
from rosella_direct import _profile_geometry, direct_and_room_send
from rosella_model import load_personalized_headphone
from rosella_room import RosellaRoomFir
DEFAULT_REFERENCE_CONFIG = Path(__file__).resolve().parents[1]/"local"/"binaural_reference.json"
REFERENCE_MODES = {"near": 1, "mid": 3, "far": 2}
@dataclass(frozen=True)
class ReferencePosition:
direction: np.ndarray
distance_m: float
ear_distance_m: float
centre_weight: float
direct_gain: float
room_send: float
class ReferenceEnvironment:
def __init__(self, config=None, *, mode=None):
self.config_path = Path(config or os.environ.get("BINAURAL_REFERENCE_CONFIG") or
DEFAULT_REFERENCE_CONFIG).expanduser().resolve()
with self.config_path.open(encoding="utf-8") as stream:
spec = json.load(stream)
self.configuration_sha256 = hashlib.sha256(
json.dumps(spec, sort_keys=True, separators=(",", ":")).encode("utf-8")).hexdigest()
if spec.get("schema") != "binaural-reference-v1":
raise ValueError("unsupported reference configuration")
path = Path(spec["model_path"]).expanduser()
if not path.is_absolute():
path = self.config_path.parent/path
self.model_sha256 = hashlib.sha256(path.read_bytes()).hexdigest()
if self.model_sha256.lower() != str(spec["model_sha256"]).lower():
raise ValueError("reference data hash mismatch")
self.model = load_personalized_headphone(path)
self.calibration_profile_index = int(spec["profile_index"])
self.profile_id = str(spec["profile_id"])
if self.calibration_profile_index not in (2, 3) or self.model.sample_rate != 48000:
raise ValueError("unsupported reference profile")
self.mode = ({v: k for k, v in REFERENCE_MODES.items()}[self.calibration_profile_index]
if mode is None else str(mode).lower())
if self.mode not in REFERENCE_MODES:
raise ValueError("reference mode must be near, mid, or far")
self.profile_index = REFERENCE_MODES[self.mode]
if int(self.model.header_integer_fields[0]) != 0:
raise ValueError("reference-v1 requires a Cartesian mapping")
self.profile = self.model.profiles[self.profile_index]
self.compiled_receivers = spec.get("compiled_receiver_positions_m")
self.room_slots = int(spec.get("room_impulse_slots", 4096))
self.calibration_directions = int(spec.get("level_calibration_directions", 128))
if not 128 <= self.room_slots <= 16384 or not 16 <= self.calibration_directions <= 2048:
raise ValueError("reference configuration exceeds supported bounds")
self.h0, self.coupling = map(float, self.model.header_float_scalars[:2])
if self.h0 <= 0 or self.coupling <= 0:
raise ValueError("invalid reference energy coupling")
self.minimum_distance_m = float(self.profile.minimum_normalized_radius)*float(self.profile.distance_scale_m)
self.reference_distance_m = self.map_position((0., 1., 0.)).distance_m
def map_position(self, position, *, profile_index=None):
position = np.asarray(position, dtype=np.float64)
if position.shape != (3,) or not np.isfinite(position).all():
raise ValueError("reference position must contain three finite values")
extension = max(1., float(np.max(np.abs(position))))
if extension > 50.1+1e-9:
raise ValueError("position exceeds the finite metadata domain")
index = self.profile_index if profile_index is None else int(profile_index)
profile, direction, radius, _, _ = _profile_geometry(self.model, position/extension, index)
distance = float(profile.distance_scale_m)*radius*extension
minimum = float(profile.minimum_normalized_radius)*float(profile.distance_scale_m)
regularized = max(distance, minimum)
alpha = distance/regularized
effective = self.h0*distance
if index == REFERENCE_MODES["near"]:
a, send = 1., 0.
else:
a = 1/math.sqrt(1+self.coupling*effective*effective)
send = effective*a
return ReferencePosition(np.asarray(direction), distance, regularized, alpha, a, send)
def create_room(self):
return RosellaRoomFir(self.model, impulse_slots=self.room_slots)
@staticmethod
def create_bank(source_channels):
return RosellaBank(1, source_channels=source_channels, output_channels=2)
def calibrate(self, field, spectrum_at):
from public_filterbank import hybrid_gain_synthesis_dictionary
from rosella_direct import _FIELD_CACHE
# Discard stale cache entries when object ids are reused.
_FIELD_CACHE.pop(id(self.model), None)
count = self.calibration_directions
i = np.arange(count, dtype=np.float64)
z = 1-2*(i+.5)/count
phi = i*np.pi*(3-math.sqrt(5))
unit = np.column_stack((np.sqrt(1-z*z)*np.cos(phi), np.sqrt(1-z*z)*np.sin(phi), z))
dictionary = hybrid_gain_synthesis_dictionary(field.fft_size)
band = (field.frequencies >= 100) & (field.frequencies <= 16000)
weights = 1/field.frequencies[band]
reference_energy = 0.
new_energy = 0.
for direction in unit:
adm = (-direction[1], direction[0], direction[2])
state = self.map_position(adm, profile_index=self.calibration_profile_index)
parameters = direct_and_room_send(self.model, adm, self.calibration_profile_index)
vector = np.empty((2, 154), dtype=np.float64)
vector[:, 0::2], vector[:, 1::2] = parameters.gains.real, parameters.gains.imag
reference = np.fft.rfft(vector @ dictionary.T, field.fft_size)
target = spectrum_at(state)*state.direct_gain
reference_energy += float(np.sum(np.abs(reference[:, band])**2*weights))
new_energy += float(np.sum(np.abs(target[:, band])**2*weights))
if min(reference_energy, new_energy) <= 1e-24 or not math.isfinite(new_energy):
raise ValueError("reference level calibration has no finite signal energy")
return math.sqrt(reference_energy/new_energy)
def info(self):
return dict(profile_id=self.profile_id, model_sha256=self.model_sha256,
configuration_sha256=self.configuration_sha256,
mode=self.mode, selected_profile_index=self.profile_index,
calibration_profile_index=self.calibration_profile_index,
distance_semantics="reference-reproduction-metres", reference_distance_m=self.reference_distance_m,
regularized_distance_m=self.minimum_distance_m,
filterbank=1, room_impulse_slots=self.room_slots,
calibration_directions=self.calibration_directions)
+36 -30
View File
@@ -1,4 +1,4 @@
"""JOC frame adapter for the public SOFA binaural backend."""
"""JOC frame adapter for independent SOFA and compact HRTF rendering."""
from __future__ import annotations
import math
@@ -8,7 +8,6 @@ import numpy as np
from binaural_metadata import OamdPositionTimeline
from public_filterbank import ANALYSIS_SYNTHESIS_LATENCY_SAMPLES
from sofa_binaural_backend import SofaBinauralBackend
from sofa_hrtf_field import (
DEFAULT_HRTF_CACHE_DIR,
DEFAULT_PROJECTION_RIDGE,
@@ -50,14 +49,13 @@ def resolve_compiled_hrtf_cache(path: str | Path) -> Path:
class SofaBinauralRenderer:
"""Render interleaved LFE plus fifteen JOC objects to stereo.
The adapter owns frame buffering and sample-timed OAMD updates. The
backend owns the 64-QMF/77-hybrid state, the 961-sample latency policy,
per-object direct/early state, and the shared late room.
The adapter owns frame buffering and sample-timed OAMD updates; the
selected backend owns direct filtering, reference state and latency.
"""
def __init__(
self, backend, *,
mode: str = "mid",
mode: str | None = None,
object_delay_samples: int = 1473,
tail_seconds: float = 5.0,
chunk_frames: int = 64,
@@ -72,6 +70,7 @@ class SofaBinauralRenderer:
f"missing: {', '.join(missing)}")
if backend.source_count != SOURCE_CHANNELS:
raise ValueError(f"JOC binaural backend must have {SOURCE_CHANNELS} sources")
mode = backend.default_profile if mode is None else mode
if backend.default_profile != str(mode).lower():
raise ValueError("backend default profile does not match renderer mode")
if int(object_delay_samples) < 0:
@@ -90,7 +89,9 @@ class SofaBinauralRenderer:
self.dsp_backend = getattr(backend, "dsp_backend", "python-sofa")
from bed_layout import bed_object_positions
self.timeline = OamdPositionTimeline(
from sofa_metadata import SofaPositionTimeline
timeline_type = SofaPositionTimeline if getattr(backend, "fixed_reference", False) else OamdPositionTimeline
self.timeline = timeline_type(
15, bed_positions=bed_object_positions(bed_channels or ()))
self._input_buffer = np.empty(
@@ -105,28 +106,32 @@ class SofaBinauralRenderer:
@classmethod
def from_sofa(
cls, sofa: str | Path, *,
mode: str = "mid",
cache_policy: str = "memory",
cache_dir: str | Path | None = DEFAULT_HRTF_CACHE_DIR,
shell_radius_m: float = 1.0,
mode: str | None = None,
cache_policy: str | None = None,
cache_dir: str | Path | None = None,
shell_radius_m: float | None = None,
projection_ridge: float = DEFAULT_PROJECTION_RIDGE,
sh_ridge: float = DEFAULT_SH_RIDGE,
object_delay_samples: int = 1473,
tail_seconds: float = 5.0,
output_gain: float = 1.0,
chunk_frames: int = 64,
bed_channels=None) -> "SofaBinauralRenderer":
bed_channels=None, reference_config=None,
interpolation: str = "lf_logmag_phase") -> "SofaBinauralRenderer":
from sofa_reference_backend import ReferenceSofaBackend
if cache_policy not in (None, "none", "memory") or cache_dir is not None:
raise ValueError("SOFA renders full HRIRs; use compile_hrtf.py to export a .jochrtf")
if projection_ridge != DEFAULT_PROJECTION_RIDGE or sh_ridge != DEFAULT_SH_RIDGE:
raise ValueError("full HRIR rendering does not use projection/SH ridge parameters")
source = resolve_sofa_hrtf(sofa)
backend = SofaBinauralBackend.from_sofa(
backend = ReferenceSofaBackend(
source,
source_count=SOURCE_CHANNELS,
default_profile=mode,
output_gain=output_gain,
cache_policy=cache_policy,
cache_dir=cache_dir,
reference_config=reference_config,
mode=mode,
shell_radius_m=shell_radius_m,
projection_ridge=projection_ridge,
sh_ridge=sh_ridge)
method=interpolation)
return cls(
backend,
mode=mode,
@@ -138,18 +143,18 @@ class SofaBinauralRenderer:
@classmethod
def from_compiled_cache(
cls, cache: str | Path, *,
mode: str = "mid",
mode: str | None = None,
object_delay_samples: int = 1473,
tail_seconds: float = 5.0,
output_gain: float = 1.0,
chunk_frames: int = 64,
bed_channels=None) -> "SofaBinauralRenderer":
bed_channels=None, reference_config=None) -> "SofaBinauralRenderer":
from sofa_reference_backend import ReferenceSofaBackend
source = resolve_compiled_hrtf_cache(cache)
backend = SofaBinauralBackend.from_compiled_cache(
backend = ReferenceSofaBackend(
source,
source_count=SOURCE_CHANNELS,
default_profile=mode,
output_gain=output_gain)
compiled=True, reference_config=reference_config, output_gain=output_gain, mode=mode)
return cls(
backend,
mode=mode,
@@ -222,15 +227,16 @@ class SofaBinauralRenderer:
values = np.asarray(source, dtype=np.float64)
if values.ndim != 2 or values.shape[1] != SOURCE_CHANNELS:
raise ValueError(f"expected [samples,{SOURCE_CHANNELS}], got {values.shape}")
if len(values) % BINAURAL_BLOCK_SAMPLES:
raise ValueError("binaural input must be divisible by 512 samples")
block_samples = int(getattr(self.backend, "block_samples", BINAURAL_BLOCK_SAMPLES))
if len(values) % block_samples:
raise ValueError("binaural input must contain complete processing blocks")
outputs = []
block_base = self.processed_input_samples
for start in range(0, len(values), BINAURAL_BLOCK_SAMPLES):
for start in range(0, len(values), block_samples):
sample = block_base + start
self._set_block_parameters(sample)
outputs.append(self.backend.process(
values[start:start + BINAURAL_BLOCK_SAMPLES]))
values[start:start + block_samples]))
self.processed_input_samples += len(values)
nonempty = [value for value in outputs if len(value)]
if not nonempty:
@@ -240,7 +246,7 @@ class SofaBinauralRenderer:
return output
def finish(self) -> np.ndarray:
"""Process pending source samples and drain early/late room state once."""
"""Process pending source samples and drain filter/reference state once."""
if self.finished:
return np.empty((0, OUTPUT_CHANNELS), dtype=np.float64)
outputs: list[np.ndarray] = []
@@ -264,10 +270,10 @@ class SofaBinauralRenderer:
def backend_info(self) -> dict:
info = self.backend.info()
info.update({
"adapter": "JOC 1536-frame / 512-sample metadata",
"adapter": f"JOC 1536-frame / {getattr(self.backend, 'block_samples', BINAURAL_BLOCK_SAMPLES)}-sample metadata",
"dsp_backend": self.dsp_backend,
"mode": self.mode,
"latency_compensated_samples": BINAURAL_LATENCY_SAMPLES,
"latency_compensated_samples": getattr(self.backend, "latency_samples", BINAURAL_LATENCY_SAMPLES),
"object_delay_samples": self.object_delay_samples,
"tail_seconds": self.tail_seconds,
"metadata_payloads": self.timeline.payload_count,
+26
View File
@@ -0,0 +1,26 @@
"""Explicit offline export of a SOFA source to the compact cache format."""
import argparse
from pathlib import Path
from sofa_canonical import load_simple_free_field_hrir
from sofa_hrtf_field import compile_sofa_hrtf
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("sofa", type=Path)
parser.add_argument("output", type=Path)
parser.add_argument("--radius-m", type=float)
args = parser.parse_args(argv)
if args.output.suffix.lower() != ".jochrtf":
parser.error("output must use .jochrtf")
canonical = load_simple_free_field_hrir(args.sofa, target_sample_rate_hz=48000)
if args.radius_m is None and len(canonical.shells_m) != 1:
parser.error("multi-radius input requires --radius-m")
radius = float(canonical.shells_m[0]) if args.radius_m is None else args.radius_m
field = compile_sofa_hrtf(canonical, shell_radius_m=radius, cache_policy="none")
field.save(args.output)
print(args.output.resolve())
if __name__ == "__main__":
main()
+25 -15
View File
@@ -215,7 +215,7 @@ def _parse_program_assignment(reader, raw_payload):
return program
def _parse_object_info_block(reader, object_index, in_bed_or_isf, raw_payload):
def _parse_object_info_block(reader, object_index, in_bed_or_isf, raw_payload, retain_spatial=False):
"""5.5.9 ``object_info_block(0)``:一个对象的属性更新。"""
info = {
"object": object_index,
@@ -263,9 +263,16 @@ def _parse_object_info_block(reader, object_index, in_bed_or_isf, raw_payload):
z_sign = reader.read(1)
z = reader.read(4)
info["position"] = (x, y, z if z_sign else -z)
if reader.read(1): # b_object_distance_specified
if not reader.read(1): # b_object_at_infinity
reader.read(4) # distance_factor_idx
outside = bool(reader.read(1))
infinite = False
distance_index = None
if outside:
infinite = bool(reader.read(1))
if not infinite:
distance_index = reader.read(4)
if retain_spatial:
info.update(distance_specified=outside, at_infinity=infinite,
distance_factor_idx=distance_index)
if zone_present:
info["zone_constraints_idx"] = reader.read(3)
info["enable_elevation"] = bool(reader.read(1))
@@ -277,9 +284,12 @@ def _parse_object_info_block(reader, object_index, in_bed_or_isf, raw_payload):
elif size_idx == 2:
reader.read(15)
if screen_present:
if reader.read(1): # b_object_use_screen_ref
reader.read(3) # screen_factor_bits
reader.read(2) # depth_factor_idx
screen_ref = bool(reader.read(1))
screen_bits = reader.read(3) if screen_ref else None
depth_index = reader.read(2) if screen_ref else None
if retain_spatial:
info.update(use_screen_ref=screen_ref, screen_factor_bits=screen_bits,
depth_factor_idx=depth_index)
info["snap"] = bool(reader.read(1))
if reader.read(1): # b_additional_table_data_exists
info["additional_table_bytes"] = reader.read(4) + 1
@@ -287,7 +297,7 @@ def _parse_object_info_block(reader, object_index, in_bed_or_isf, raw_payload):
return info
def _parse_object_element(reader, element, raw_payload):
def _parse_object_element(reader, element, raw_payload, retain_spatial=False):
"""5.5.5 ``object_element()``:时间信息 + 每个对象一条 object_info_block。"""
object_count = element["object_count"]
try:
@@ -327,7 +337,7 @@ def _parse_object_element(reader, element, raw_payload):
reader.read(5)
objects = [
_parse_object_info_block(
reader, index, index < element["bed_isf_objects"], raw_payload)
reader, index, index < element["bed_isf_objects"], raw_payload, retain_spatial)
for index in range(object_count)
]
except UnsupportedVariantError:
@@ -348,7 +358,7 @@ def _parse_object_element(reader, element, raw_payload):
}
def _parse_elements(reader, alternate_present, object_count, program, raw_payload):
def _parse_elements(reader, alternate_present, object_count, program, raw_payload, retain_spatial=False):
"""5.5.4 ``oa_element_md()`` 目录;对象 element 按结构解析,其余按声明长度跳过。"""
try:
element_count = reader.read(4)
@@ -425,7 +435,7 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa
"payload": bytes_descriptor(raw_payload),
})
object_element = _parse_object_element(
reader, element, raw_payload)
reader, element, raw_payload, retain_spatial)
element["parsed_end_bit"] = reader.position
element["object_element"] = object_element
# 个别编码器声明的 oa_element_size 比实际内容短;以结构解析结果为准。
@@ -510,7 +520,7 @@ def program_bed_channels(bits_one):
return program_bed_summary(bits_one)["channels"]
def frame_update(bits_one):
def frame_update(bits_one, *, retain_spatial=False):
"""单帧 OAMD → 位置字段增量及其 sample offset/ramp duration。"""
bits, raw_payload = _payload_bits(bits_one)
try:
@@ -532,7 +542,7 @@ def frame_update(bits_one):
program = _parse_program_assignment(reader, raw_payload)
alternate_present = bool(reader.read(1))
object_element, elements = _parse_elements(
reader, alternate_present, object_count, program, raw_payload)
reader, alternate_present, object_count, program, raw_payload, retain_spatial)
except UnsupportedVariantError:
raise
except ValueError as exc:
@@ -584,8 +594,8 @@ def frame_update(bits_one):
x, y, z = position
out[(slot, "q1")] = q_of(x, N_Q12) if 0 <= x <= N_Q12 else None
out[(slot, "q2")] = q_of(y, N_Q12) if 0 <= y <= N_Q12 else None
# 5.6.1.1.10/5.6.1.1.11:pos3D_Z 带符号;当前 16 槽模型只表达非负高度。
out[(slot, "q3")] = q_of(z, N_Q3) if 0 <= z <= N_Q3 else (
# Preserve legacy nonnegative height unless signed spatial output is requested.
out[(slot, "q3")] = q_of(z, N_Q3) if (0 <= z <= N_Q3 or retain_spatial and -N_Q3 <= z < 0) else (
0 if z < 0 else None)
size_mismatch = [
+217
View File
@@ -0,0 +1,217 @@
"""Uniform partitioned convolution with float64/complex128 state."""
from __future__ import annotations
import math
import numpy as np
class UniformPartitionedConvolver:
"""Overlap-add FIR convolution with shared input history.
Filter updates crossfade outputs with independent old/new overlap tails.
"""
def __init__(self, filters, *, block_size: int = 128,
max_filter_taps: int | None = None):
values = np.asarray(filters, dtype=np.float64)
if values.ndim == 1:
values = values[None, :]
if values.ndim != 2 or values.shape[0] <= 0 or values.shape[1] <= 0:
raise ValueError("filters must have shape [outputs,taps]")
if not np.isfinite(values).all():
raise ValueError("filters must be finite")
self.block_size = int(block_size)
if self.block_size <= 0:
raise ValueError("block_size must be positive")
self.fft_size = 2 * self.block_size
self.frequency_bins = self.block_size + 1
self.outputs = int(values.shape[0])
self.max_filter_taps = int(max_filter_taps or values.shape[1])
if self.max_filter_taps < values.shape[1]:
raise ValueError("max_filter_taps is shorter than the initial filter")
self.partition_count = int(math.ceil(self.max_filter_taps / self.block_size))
self.input_history = np.zeros(
(self.partition_count, self.frequency_bins), dtype=np.complex128)
self.write_index = -1
self.processed_blocks = 0
self.current_spectra = self._partition(values)
self.current_overlap = np.zeros(
(self.outputs, self.block_size), dtype=np.float64)
self.target_spectra: np.ndarray | None = None
self.target_overlap: np.ndarray | None = None
self.fade_samples = 0
self.fade_position = 0
@property
def transition_active(self) -> bool:
return self.target_spectra is not None
def _partition(self, filters) -> np.ndarray:
values = np.asarray(filters, dtype=np.float64)
if values.ndim == 1:
values = values[None, :]
if values.shape[0] != self.outputs or values.shape[1] > self.max_filter_taps:
raise ValueError(
f"filter update must have shape [{self.outputs}, <= {self.max_filter_taps}]")
if not np.isfinite(values).all():
raise ValueError("filter update contains non-finite values")
padded = np.zeros(
(self.outputs, self.partition_count, self.fft_size), dtype=np.float64)
for partition in range(self.partition_count):
start = partition * self.block_size
stop = min(start + self.block_size, values.shape[1])
if stop > start:
padded[:, partition, :stop - start] = values[:, start:stop]
return np.asarray(np.fft.rfft(padded, axis=-1), dtype=np.complex128)
def _ordered_input_history(self) -> np.ndarray:
if self.write_index < 0:
return np.zeros_like(self.input_history)
indices = (self.write_index - np.arange(self.partition_count)) % self.partition_count
return self.input_history[indices]
def _convolution_block(self, spectra: np.ndarray,
overlap: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
ordered = self._ordered_input_history()
frequency = np.einsum(
"pf,opf->of", ordered, spectra, dtype=np.complex128, optimize=False)
time = np.asarray(np.fft.irfft(frequency, self.fft_size, axis=-1),
dtype=np.float64)
output = time[:, :self.block_size] + overlap
return output, time[:, self.block_size:].copy()
def _overlap_for_previous_block(self, spectra: np.ndarray) -> np.ndarray:
if self.processed_blocks == 0:
return np.zeros((self.outputs, self.block_size), dtype=np.float64)
ordered = self._ordered_input_history()
frequency = np.einsum(
"pf,opf->of", ordered, spectra, dtype=np.complex128, optimize=False)
time = np.asarray(np.fft.irfft(frequency, self.fft_size, axis=-1),
dtype=np.float64)
return time[:, self.block_size:].copy()
def set_filter(self, filters, *, fade_blocks: int = 0) -> None:
if self.transition_active:
raise RuntimeError("finish the current filter crossfade before another update")
target = self._partition(filters)
fade_blocks = int(fade_blocks)
if fade_blocks < 0:
raise ValueError("fade_blocks must be non-negative")
if fade_blocks == 0:
self.current_spectra = target
self.current_overlap = self._overlap_for_previous_block(target)
return
self.target_spectra = target
self.target_overlap = self._overlap_for_previous_block(target)
self.fade_samples = fade_blocks * self.block_size
self.fade_position = 0
def process_block(self, samples) -> np.ndarray:
values = np.asarray(samples, dtype=np.float64)
if values.shape != (self.block_size,) or not np.isfinite(values).all():
raise ValueError(f"samples must have finite shape ({self.block_size},)")
padded = np.zeros(self.fft_size, dtype=np.float64)
padded[:self.block_size] = values
spectrum = np.asarray(np.fft.rfft(padded), dtype=np.complex128)
self.write_index = (self.write_index + 1) % self.partition_count
self.input_history[self.write_index] = spectrum
current, next_current_overlap = self._convolution_block(
self.current_spectra, self.current_overlap)
if not self.transition_active:
self.current_overlap = next_current_overlap
self.processed_blocks += 1
return current.T.copy()
target, next_target_overlap = self._convolution_block(
self.target_spectra, self.target_overlap)
sample_indices = self.fade_position + np.arange(
1, self.block_size + 1, dtype=np.float64)
amount = np.clip(sample_indices / float(self.fade_samples), 0.0, 1.0)
output = current * (1.0 - amount[None, :]) + target * amount[None, :]
self.fade_position += self.block_size
self.current_overlap = next_current_overlap
self.target_overlap = next_target_overlap
if self.fade_position >= self.fade_samples:
self.current_spectra = self.target_spectra
self.current_overlap = self.target_overlap
self.target_spectra = None
self.target_overlap = None
self.fade_samples = 0
self.fade_position = 0
self.processed_blocks += 1
return output.T.copy()
def reset(self) -> None:
self.input_history.fill(0.0)
self.current_overlap.fill(0.0)
if self.target_overlap is not None:
self.target_overlap.fill(0.0)
self.write_index = -1
self.processed_blocks = 0
self.target_spectra = None
self.target_overlap = None
self.fade_samples = 0
self.fade_position = 0
class MultiSourcePartitionedConvolver:
"""Sum independent per-source convolution outputs."""
def __init__(self, source_count: int, *, block_size: int,
max_filter_taps: int, output_channels: int = 2):
self.source_count = int(source_count)
self.block_size = int(block_size)
self.output_channels = int(output_channels)
if min(self.source_count, self.block_size, self.output_channels,
int(max_filter_taps)) <= 0:
raise ValueError("source_count, block_size, outputs and taps must be positive")
zero = np.zeros((self.output_channels, int(max_filter_taps)), dtype=np.float64)
self.convolvers = [
UniformPartitionedConvolver(
zero, block_size=self.block_size, max_filter_taps=max_filter_taps)
for _ in range(self.source_count)
]
def set_filter(self, source: int, filters, *, fade_blocks: int = 0) -> None:
source = int(source)
if not 0 <= source < self.source_count:
raise IndexError(source)
self.convolvers[source].set_filter(filters, fade_blocks=fade_blocks)
def process_block(self, sources) -> np.ndarray:
values = np.asarray(sources, dtype=np.float64)
if values.shape != (self.block_size, self.source_count):
raise ValueError(
f"sources must have shape ({self.block_size},{self.source_count})")
if not np.isfinite(values).all():
raise ValueError("sources contain non-finite values")
output = np.zeros((self.block_size, self.output_channels), dtype=np.float64)
for source in range(self.source_count):
output += self.convolvers[source].process_block(values[:, source])
return output
def process(self, sources) -> np.ndarray:
values = np.asarray(sources, dtype=np.float64)
if values.ndim != 2 or values.shape[1] != self.source_count:
raise ValueError(f"sources must have shape [samples,{self.source_count}]")
if len(values) % self.block_size:
raise ValueError("partitioned input length must be divisible by block_size")
blocks = [
self.process_block(values[start:start + self.block_size])
for start in range(0, len(values), self.block_size)
]
return (np.concatenate(blocks, axis=0) if blocks
else np.zeros((0, self.output_channels), dtype=np.float64))
def flush(self, blocks: int | None = None) -> np.ndarray:
count = (max(conv.partition_count for conv in self.convolvers) + 2
if blocks is None else int(blocks))
if count < 0:
raise ValueError("flush block count must be non-negative")
zero = np.zeros((self.block_size, self.source_count), dtype=np.float64)
return self.process(np.tile(zero, (count, 1)))
def reset(self) -> None:
for convolver in self.convolvers:
convolver.reset()
+220
View File
@@ -0,0 +1,220 @@
"""Local spherical interpolation of full complex HRIR spectra."""
from __future__ import annotations
from collections import OrderedDict
import math
import numpy as np
from scipy.interpolate import PchipInterpolator
from scipy.spatial import ConvexHull, SphericalVoronoi, cKDTree
from sofa_canonical import CanonicalHrtf, load_simple_free_field_hrir
class FullHrirField:
def __init__(self, source, *, method="lf_logmag_phase", shell_radius_m=None,
fft_size=None):
if method not in ("lf_logmag_phase", "lf_mca_zero"):
raise ValueError("unsupported full HRIR interpolation method")
self.canonical = (source if isinstance(source, CanonicalHrtf) else
load_simple_free_field_hrir(source, target_sample_rate_hz=48000))
c = self.canonical
if c.sample_rate_hz != 48000:
raise ValueError("full HRIR renderer requires 48000 Hz")
shells = c.shells_m
if shell_radius_m is not None and (not math.isfinite(shell_radius_m) or shell_radius_m <= 0):
raise ValueError("shell radius must be finite and positive")
if shell_radius_m is None and len(shells) != 1:
raise ValueError("multi-radius SOFA requires an explicit --hrtf-radius-m")
selected = c.shell_indices(shells[0] if shell_radius_m is None else shell_radius_m)
if np.any(np.sum(c.hrir[selected]**2, axis=-1) <= 0):
raise ValueError("each selected HRIR ear must contain signal energy")
self.measurement_radius_m = float(np.median(c.measurement_radius_m[selected]))
support = c.taps + int(math.ceil(float(np.max(c.delay_samples[selected])))) + 1
if fft_size is None:
fft_size = max(2048, 1 << (2*support-1).bit_length())
if fft_size > 16384:
raise ValueError("HRIR and Data.Delay exceed the automatic FFT budget")
self.method, self.fft_size = method, int(fft_size)
if self.fft_size < 64 or self.fft_size % 2 or c.taps + np.max(c.delay_samples[selected]) >= self.fft_size//2:
raise ValueError("HRIR and Data.Delay exceed the selected FFT time budget")
receiver = c.receiver_position_cartesian_m[selected]
if not np.allclose(receiver, receiver[0], atol=1e-9, rtol=0):
raise ValueError("moving receiver geometry is unsupported")
self.receivers = receiver[0].copy()
if np.linalg.norm(self.receivers.mean(axis=0)) > 1e-6:
raise ValueError("radial model requires ears centred on the listener")
self.head_radius_m = float(np.linalg.norm(self.receivers[0]))
if not (0.04 < self.head_radius_m < .15) or not np.isclose(
np.linalg.norm(self.receivers[1]), self.head_radius_m, atol=1e-6):
raise ValueError("unsupported receiver geometry for the radial model")
self.ear_directions = self.receivers/self.head_radius_m
unit = c.unit_directions[selected]
_, first, inverse = np.unique(np.round(unit, 10), axis=0, return_index=True, return_inverse=True)
for group in np.flatnonzero(np.bincount(inverse) > 1):
members = selected[inverse == group]
if not (np.allclose(c.hrir[members], c.hrir[members[0]], atol=1e-10, rtol=1e-8)
and np.allclose(c.delay_samples[members], c.delay_samples[members[0]], atol=1e-10, rtol=0)):
raise ValueError("coincident SOFA measurements disagree")
selected = selected[np.sort(first)]
self.directions = c.unit_directions[selected].copy()
self.tree = cKDTree(self.directions)
hull = ConvexHull(self.directions)
if np.any(hull.equations[:, 3] >= -1e-9):
raise ValueError("SOFA must enclose the listener; extrapolation is unsupported")
self.faces = hull.simplices
vertices = self.directions[self.faces]
dots = np.einsum("fij,fkj->fik", vertices, vertices)
self.max_triangle_edge_degrees = math.degrees(math.acos(float(np.clip(dots.min(), -1, 1))))
matrix = self.directions[self.faces].transpose(0, 2, 1)
if np.max(np.linalg.cond(matrix)) > 1e8:
raise ValueError("ill-conditioned spherical interpolation mesh")
self.inverse = np.linalg.inv(matrix)
self.frequencies = np.fft.rfftfreq(self.fft_size, 1/c.sample_rate_hz)
self.omega = 2*np.pi*self.frequencies/c.sample_rate_hz
raw = np.fft.rfft(c.hrir[selected], self.fft_size)
fit = (self.frequencies >= 200) & (self.frequencies <= 1500)
x = self.omega[fit]-self.omega[fit].mean()
phase = np.unwrap(np.angle(raw), axis=-1)[..., fit]
self.delays = -np.sum(x*phase, axis=-1)/np.sum(x*x) + c.delay_samples[selected]
residual_phase = phase+(self.delays-c.delay_samples[selected])[..., None]*self.omega[fit]
residual_phase -= residual_phase.mean(axis=-1, keepdims=True)
self.phase_fit_rms_rad = np.sqrt(np.mean(residual_phase**2, axis=-1))
self.recording_origin_samples = int(round(float(np.median(self.delays.mean(axis=1)))))
self.common_latency_samples = self.fft_size//2
physical = raw*np.exp(-1j*self.omega*c.delay_samples[selected, :, None])
self.floor = max(float(np.max(np.abs(physical)))*1e-6, 1e-15)
areas = SphericalVoronoi(self.directions).calculate_areas()
areas /= areas.sum()
energy = np.einsum("m,mef->f", areas, np.abs(physical)**2)/2
self.centre_spectrum = np.sqrt(energy)*np.exp(-1j*self.omega*self.recording_origin_samples)
self.aligned = physical*np.exp(1j*self.omega*self.delays[..., None])
self.log_magnitude = None
self.auditory = None
if method == "lf_logmag_phase":
self.log_magnitude = np.log(np.maximum(np.abs(physical), self.floor))
else:
erb = lambda f: 21.4*np.log10(1+.00437*f)
self.centres = (10**(np.linspace(erb(50), erb(20000), 41)/21.4)-1)/.00437
bw = 1.019*24.7*(1+.00437*self.centres)
self.bank = (1+((self.frequencies[None, :]-self.centres[:, None])/bw[:, None])**2)**-4
self.bank[:, 1:-1] *= 2
self.bank /= self.bank.sum(axis=1, keepdims=True)
self.auditory = self._auditory_db(physical)
self._cache = OrderedDict()
def _auditory_db(self, spectrum):
return 10*np.log10(np.maximum(np.abs(spectrum)**2 @ self.bank.T, 1e-24))
def weights(self, direction):
q = np.asarray(direction, dtype=np.float64)
if q.shape != (3,) or not np.isfinite(q).all() or np.linalg.norm(q) <= 1e-12:
raise ValueError("query direction must be a finite nonzero vector")
q = q/np.linalg.norm(q)
distance, nearest = self.tree.query(q)
if distance < 1e-10:
return np.array([nearest]), np.array([1.]), 0.
g = self.inverse @ q
valid = np.flatnonzero(np.min(g, axis=1) >= -1e-10)
if not len(valid):
raise ValueError("no containing spherical triangle")
face = int(valid[0])
weights = np.maximum(g[face], 0)
weights /= weights.sum()
ids = self.faces[face]
vertices = self.directions[ids]
edge = math.degrees(math.acos(float(np.clip(np.min(vertices @ vertices.T), -1, 1))))
return ids, weights, edge
def spectrum(self, direction):
q = np.asarray(direction, dtype=np.float64)
if q.shape != (3,) or not np.isfinite(q).all() or np.linalg.norm(q) <= 1e-12:
raise ValueError("query direction must be a finite nonzero vector")
q = q/np.linalg.norm(q)
key = tuple(q)
if key in self._cache:
self._cache.move_to_end(key)
return self._cache[key].copy()
ids, weights, _ = self.weights(q)
delay = weights @ self.delays[ids]
spectrum = np.einsum("i,ief->ef", weights, self.aligned[ids])
spectrum *= np.exp(-1j*self.omega*delay[:, None])
if len(ids) > 1:
if self.method == "lf_logmag_phase":
magnitude = np.exp(np.einsum("i,ief->ef", weights, self.log_magnitude[ids]))
spectrum = magnitude*spectrum/np.maximum(np.abs(spectrum), self.floor)
else:
target = np.einsum("i,ieb->eb", weights, self.auditory[ids])
difference = target-self._auditory_db(spectrum)
correction = PchipInterpolator(self.centres, difference, axis=-1)(
np.clip(self.frequencies, self.centres[0], self.centres[-1]))
spectrum *= 10**(np.clip(correction, -12, 12)/20)
self._cache[key] = spectrum.copy()
if len(self._cache) > 128:
self._cache.popitem(last=False)
return spectrum
def info(self):
arrays = [self.aligned, self.log_magnitude, self.auditory, self.delays,
self.inverse, self.directions, self.faces]
return dict(source_sha256=self.canonical.source_sha256,
sample_rate_hz=self.canonical.sample_rate_hz,
convention=self.canonical.convention,
measurement_radius_m=self.measurement_radius_m,
method=self.method, fft_size=self.fft_size,
retained_filter_taps=self.fft_size,
recording_origin_samples=self.recording_origin_samples,
interpolation_storage_bytes=sum(x.nbytes for x in arrays if x is not None),
max_triangle_edge_degrees=self.max_triangle_edge_degrees,
phase_fit_rms_rad_p95=float(np.percentile(self.phase_fit_rms_rad, 95)),
ear_model="complex-rigid-sphere-distance-ratio",
radial_model_validation="analytic-identity-and-series-checks; approximate-human-head")
def sphere_transfer(frequencies, radius, head_radius, cosines, *, sound_speed=343.3,
tolerance=1e-10):
"""Point-source pressure / free-field centre pressure, FFT sign convention.
Evaluate the rigid-sphere series using adjacent Hankel ratios, avoiding
overflow of high-order Neumann functions at low frequency.
"""
f = np.asarray(frequencies, dtype=np.float64)
r, a = float(radius), float(head_radius)
mu = np.asarray(cosines, dtype=np.float64)
if f.ndim != 1 or not np.isfinite(f).all() or np.any(f < 0) or not np.isfinite(mu).all():
raise ValueError("invalid sphere frequency or incidence")
if not (math.isfinite(r) and math.isfinite(a) and r > a > 0):
raise ValueError("sphere source must be outside the head")
x = 2*np.pi*f*a/sound_speed
z = x*(r/a)
order = max(int(math.ceil(float(x.max(initial=0))))+32,
int(math.ceil(math.log(tolerance)/math.log(a/r)))+16)
if order > 4096:
raise ValueError("source too close to the sphere surface")
mu = np.clip(mu, -1., 1.)
p_before, p = np.ones_like(mu), mu.copy()
result = np.ones(mu.shape+(len(f),), dtype=np.complex128)/(1+1j*x)
ratio_a = x/(1+1j*x)
ratio_r = z/(1+1j*z)
product = np.ones_like(x, dtype=np.complex128)
for n in range(1, order+1):
quotient = np.divide(ratio_a, ratio_r, out=np.full_like(ratio_a, a/r), where=z != 0)
product *= quotient
result += (2*n+1)*p[..., None]*product/(n+1-x*ratio_a)
p_before, p = p, ((2*n+1)*mu*p-n*p_before)/(n+1)
ratio_a = x/((2*n+1)-x*ratio_a)
ratio_r = z/((2*n+1)-z*ratio_r)
result *= np.exp(1j*x)
if not np.isfinite(result).all():
raise ValueError("sphere series failed to converge")
return result
def radial_ratio(field, direction, radius):
if abs(float(radius)-field.measurement_radius_m) < 1e-10:
return np.ones((2, len(field.frequencies)), dtype=np.complex128)
mu = field.ear_directions @ np.asarray(direction, dtype=np.float64)
source = sphere_transfer(field.frequencies, field.measurement_radius_m, field.head_radius_m, mu)
target = sphere_transfer(field.frequencies, radius, field.head_radius_m, mu)
if np.min(np.abs(source)) < 1e-10:
raise ValueError("singular radial reference response")
return target/source
+53
View File
@@ -0,0 +1,53 @@
"""SOFA spatial metadata adaptation on a sample-timed trajectory."""
from __future__ import annotations
import numpy as np
from binaural_metadata import OamdPositionTimeline
from oamd_bits import frame_update
from variant_error import UnsupportedVariantError
DISTANCE_FACTORS = (1.1, 1.3, 1.6, 2., 2.5, 3.2, 4., 5., 6.3, 7.9, 10., 12.6,
15.8, 20., 25.1, 50.1)
class SofaPositionTimeline(OamdPositionTimeline):
def __init__(self, object_count=15, bed_positions=None):
super().__init__(object_count, bed_positions)
self.distance_factors = np.ones(16, dtype=np.float64)
def _targets(self):
targets = super()._targets()
for index, target in enumerate(targets, 1):
if index in self.bed_positions or self.distance_factors[index] == 1:
continue
value = np.asarray(target, dtype=np.float64)
boundary = float(np.max(np.abs(value)))
if boundary <= 1e-12:
raise UnsupportedVariantError("sofa", "undefined_outside_direction",
"Outside object has no ray from the listener")
targets[index-1] = tuple(value/boundary*self.distance_factors[index])
return targets
def submit_update(self, update, **timing):
if update.get("program", {}).get("num_isf_objects", 0):
raise UnsupportedVariantError("sofa", "isf_not_supported", "ISF spatial mapping is unsupported")
for info in update.get("objects", ()):
slot = int(info["object"])
if info.get("at_infinity"):
raise UnsupportedVariantError("sofa", "infinite_distance",
"Infinite-distance rendering is unsupported")
if info.get("use_screen_ref"):
raise UnsupportedVariantError("sofa", "screen_mapping_required",
"Screen-anchored objects require a calibrated mapping")
if "distance_specified" in info:
index = info.get("distance_factor_idx")
if info["distance_specified"] and (index is None or not 0 <= index < 16):
raise ValueError("invalid outside distance index")
self.distance_factors[slot] = DISTANCE_FACTORS[index] if info["distance_specified"] else 1.
return super().submit_update(update, **timing)
def submit_payload(self, payload, **timing):
update = frame_update(payload, retain_spatial=True)
self.submit_update(update, **timing)
return update
+270
View File
@@ -0,0 +1,270 @@
"""Direct HRIR rendering with a configured reference environment."""
from __future__ import annotations
from collections import OrderedDict
import math
from pathlib import Path
from types import SimpleNamespace
import numpy as np
from scipy import signal
from binaural_reference import ReferenceEnvironment
from partitioned_convolution import MultiSourcePartitionedConvolver
from sofa_hrir import FullHrirField, radial_ratio
from sofa_hrtf_field import SofaHrtfField
from sofa_binaural_backend import HybridPath, HybridObjectPathRenderer, _StereoDelay
class _CompiledField:
def __init__(self, path, receivers):
if receivers is None:
raise ValueError("compiled input requires configured receiver positions")
self.field = SofaHrtfField.load(path)
self.fft_size = 2048
self.frequencies = np.fft.rfftfreq(self.fft_size, 1/48000)
self.measurement_radius_m = self.field.measurement_radius_m
receivers = np.asarray(receivers, dtype=np.float64)
if receivers.shape != (2, 3) or not np.isfinite(receivers).all():
raise ValueError("invalid compiled receiver positions")
self.head_radius_m = float(np.linalg.norm(receivers[0]))
if not .04 < self.head_radius_m < .15 or not np.allclose(receivers[0], -receivers[1], atol=1e-6):
raise ValueError("unsupported compiled receiver geometry")
self.ear_directions = receivers/self.head_radius_m
self.band_geometry = SimpleNamespace(frequencies=self.field.band_center_frequencies_hz,
measurement_radius_m=self.measurement_radius_m, head_radius_m=self.head_radius_m,
ear_directions=self.ear_directions)
i = np.arange(128)
z = 1-2*(i+.5)/128
phi = i*np.pi*(3-math.sqrt(5))
directions = np.column_stack((np.sqrt(1-z*z)*np.cos(phi), np.sqrt(1-z*z)*np.sin(phi), z))
evaluations = [self.field.evaluate_sofa(q) for q in directions]
self.recording_origin_samples = int(round(float(np.median([e.delay_samples.mean() for e in evaluations]))))
self.centre_band = np.sqrt(np.mean([np.abs(e.transfer_gains)**2 for e in evaluations], axis=(0, 1)))
from public_filterbank import hybrid_gain_synthesis_dictionary
self.dictionary = hybrid_gain_synthesis_dictionary(self.fft_size)
def bands(self, state):
e = self.field.evaluate_sofa(state.direction)
ratio = radial_ratio(self.band_geometry, state.direction, state.ear_distance_m)
return e, ratio
def spectrum_at(self, state):
e, ratio = self.bands(state)
f = self.field.band_center_frequencies_hz
centre = self.centre_band*np.exp(-2j*np.pi*f*self.recording_origin_samples/48000)
gains = state.centre_weight*e.transfer_gains*ratio+(1-state.centre_weight)*centre
values = np.empty((2, 154))
values[:, 0::2], values[:, 1::2] = gains.real, gains.imag
return np.fft.rfft(values @ self.dictionary.T, self.fft_size)
def info(self):
return dict(source_sha256=self.field.source_sha256, method="compiled-band-field",
measurement_radius_m=self.measurement_radius_m,
receiver_geometry_source="reference-configuration",
cache_key=self.field.cache_key, format_version=self.field.format_version,
recording_origin_samples=self.recording_origin_samples,
radial_model_validation="analytic-identity-and-series-checks; approximate-human-head")
class ReferenceSofaBackend:
fixed_reference = True
default_profile = "mid"
dsp_backend = "python-full-hrir"
block_samples = 128
environment_batch_samples = 1536
def __init__(self, source, *, reference_config=None, compiled=False, source_count=16,
output_gain=1., method="lf_logmag_phase", shell_radius_m=None, mode=None):
if source_count <= 0 or not math.isfinite(output_gain):
raise ValueError("invalid source count or output gain")
self.source_count, self.output_gain = int(source_count), float(output_gain)
self.environment = ReferenceEnvironment(reference_config, mode=mode)
self.default_profile = self.environment.mode
self.compiled = bool(compiled)
self.field = (_CompiledField(source, self.environment.compiled_receivers) if compiled else
FullHrirField(source, method=method, shell_radius_m=shell_radius_m))
self.hrtf_input_kind = "compiled_cache" if compiled else "sofa"
self.hrtf_input_path = str(Path(source).expanduser().resolve())
self.cache_policy = None
if self.environment.minimum_distance_m <= self.field.head_radius_m:
raise ValueError("reference near-centre regularization lies inside the head")
self._spectra = OrderedDict()
self.level_calibration = self.environment.calibrate(self.field, self.spectrum_at)
self.bank = self.environment.create_bank(self.source_count)
self.room = self.environment.create_room()
self._last = [None]*self.source_count
self.distance_states = [None]*self.source_count
self._sends = np.zeros(self.source_count, dtype=np.float64)
self._send_targets = self._sends.copy()
self.processed_input_samples = self.output_samples = self.parameter_updates = 0
self.finished = False
self._pending_room = []
self._pending_direct = []
self.origin = self.field.recording_origin_samples
if self.origin < 0:
raise ValueError("negative recording origin requires an explicit time-base adapter")
self.implementation_latency = (self.bank.latency_samples if compiled else self.field.common_latency_samples)
self.latency_samples = max(self.implementation_latency, self.bank.latency_samples)+max(self.origin, 0)
self._latency_remaining = self.latency_samples
self.room_delay = _StereoDelay(self.latency_samples-self.bank.latency_samples)
if compiled:
self.dsp_backend = "python-compiled-hrtf"
maximum = int(math.ceil(float(np.max(self.field.field.delay_bounds))/64))
self.paths = HybridObjectPathRenderer(self.source_count, history_slots=maximum+8, transition_slots=2)
self.direct_delay = _StereoDelay(max(0, self.latency_samples-self.bank.latency_samples-self.origin))
self.lfe_transfer = self._compiled_lfe()
self.direct_bank = self.environment.create_bank(self.source_count)
else:
self.convolver = MultiSourcePartitionedConvolver(self.source_count, block_size=self.block_samples,
max_filter_taps=self.field.fft_size)
self.direct_delay = _StereoDelay(max(0, self.latency_samples-self.implementation_latency-max(self.origin, 0)))
lowpass = signal.firwin(129, 150., fs=48000)
h = np.zeros((2, self.field.fft_size), dtype=np.float64)
start = self.latency_samples-64
if start < 0 or start+len(lowpass) > self.field.fft_size:
raise ValueError("LFE latency exceeds filter budget")
h[:, start:start+len(lowpass)] = lowpass/math.sqrt(2)
self.lfe_filter = h
def spectrum_at(self, state):
if self.compiled:
return self.field.spectrum_at(state)
key = (tuple(state.direction), state.ear_distance_m, state.centre_weight)
if key in self._spectra:
self._spectra.move_to_end(key)
return self._spectra[key].copy()
value = self.field.spectrum(state.direction)*radial_ratio(self.field, state.direction, state.ear_distance_m)
value = value*state.centre_weight+(1-state.centre_weight)*self.field.centre_spectrum
self._spectra[key] = value.copy()
if len(self._spectra) > 128:
self._spectra.popitem(last=False)
return value
def _compiled_lfe(self):
from rosella_direct import special_lfe_direct
return special_lfe_direct().gains
def set_source(self, source, position_adm, *, profile=None, gain=1., enabled=True,
special_lfe=False, fade=True):
if self.finished:
raise RuntimeError("renderer is finished")
source = int(source)
if not 0 <= source < self.source_count or not math.isfinite(gain):
raise ValueError("invalid source or gain")
if profile not in (None, self.default_profile):
raise ValueError("source profile must match the selected reference mode")
position = np.asarray(position_adm, dtype=np.float64)
if position.shape != (3,) or not np.isfinite(position).all():
raise ValueError("source position must be finite")
gain = float(gain) if enabled else 0.
key = (tuple(position), gain, bool(special_lfe))
if key == self._last[source]:
return
self._last[source] = key
first = self.processed_input_samples == 0
if special_lfe:
self._send_targets[source] = 0.
if self.compiled:
f = self.field.field.band_center_frequencies_hz
transfer = self.lfe_transfer*gain*np.exp(-2j*np.pi*(self.origin % 64)*f/48000)
path = HybridPath("lfe", np.full(2, self.origin//64, dtype=np.int64), transfer)
self.paths.set_paths(source, (path,), fade_slots=0 if first else 2)
else:
self.convolver.set_filter(source, self.lfe_filter*gain, fade_blocks=0 if first else 1)
else:
state = self.environment.map_position(position)
self.distance_states[source] = state
self._send_targets[source] = state.room_send*gain
if self.compiled:
evaluation, ratio = self.field.bands(state)
slots = np.floor(evaluation.delay_samples/64).astype(np.int64)
residual = evaluation.delay_samples-slots*64
f = self.field.field.band_center_frequencies_hz
transfer = evaluation.aligned_gains*np.exp(-2j*np.pi*residual[:, None]*f/48000)*ratio
transfer *= state.centre_weight*state.direct_gain*self.level_calibration*gain
paths = [HybridPath("direct", slots, transfer)]
if state.centre_weight < 1:
delay = max(0, self.origin)
centre = self.field.centre_band*np.exp(-2j*np.pi*(delay % 64)*f/48000)
centre = np.tile(centre, (2, 1))*(1-state.centre_weight)*state.direct_gain*self.level_calibration*gain
paths.append(HybridPath("centre", np.full(2, delay//64, dtype=np.int64), centre))
self.paths.set_paths(source, paths, fade_slots=0 if first or not fade else 2)
else:
spectrum = self.spectrum_at(state)*state.direct_gain*self.level_calibration*gain
hrir = np.roll(np.fft.irfft(spectrum, self.field.fft_size), self.field.common_latency_samples, axis=-1)
self.convolver.set_filter(source, hrir, fade_blocks=0 if first or not fade else 1)
if first:
self._sends[source] = self._send_targets[source]
self.parameter_updates += 1
def process(self, source):
if self.finished:
raise RuntimeError("renderer is finished")
values = np.asarray(source, dtype=np.float64)
if values.ndim != 2 or values.shape[1] != self.source_count or len(values) % self.block_samples or not np.isfinite(values).all():
raise ValueError("input must contain finite complete 128-sample source blocks")
outputs = []
for start in range(0, len(values), self.block_samples):
block = values[start:start+self.block_samples]
hops = block.reshape(2, 64, self.source_count).transpose(0, 2, 1)
hybrid = self.bank.hybrid_analysis.process_chunk(self.bank.qmf_analysis.process_chunk(hops))
amount = np.array([.5, 1.])[:, None]
sends = self._sends+(self._send_targets-self._sends)*amount
room_input = np.einsum("tsf,ts->tf", hybrid, sends)
if self.compiled:
self._pending_direct.append(self.paths.process(hybrid))
else:
self._pending_direct.append(self.convolver.process(block))
self._pending_room.append(room_input)
self._sends[:] = self._send_targets
self.processed_input_samples += self.block_samples
if len(self._pending_room)*self.block_samples < self.environment_batch_samples:
continue
room = self.room.process_chunk(np.concatenate(self._pending_room))
time = self.bank.qmf_synthesis.process_chunk(self.bank.hybrid_synthesis.process_chunk(room))
wet = time.transpose(0, 2, 1).reshape(-1, 2)
direct = np.concatenate(self._pending_direct)
if self.compiled:
direct = self.direct_bank.qmf_synthesis.process_chunk(
self.direct_bank.hybrid_synthesis.process_chunk(direct))
direct = direct.transpose(0, 2, 1).reshape(-1, 2)
output = self.direct_delay.process(direct)+self.room_delay.process(wet)
self._pending_direct.clear()
self._pending_room.clear()
skip = min(len(output), self._latency_remaining)
self._latency_remaining -= skip
outputs.append(output[skip:]*self.output_gain)
output = np.concatenate(outputs) if outputs else np.empty((0, 2))
self.output_samples += len(output)
return output
def finish_output_capacity(self, tail_seconds):
tail = float(tail_seconds)
if not math.isfinite(tail) or tail < 0:
raise ValueError("tail must be finite and nonnegative")
return int(math.ceil((tail*48000+self.latency_samples+self.field.fft_size)/128)*128)
def finish(self, *, tail_seconds=5.):
if self.finished:
return np.empty((0, 2))
count = self.finish_output_capacity(tail_seconds)
pending = len(self._pending_room)*self.block_samples
padded = int(math.ceil((count+pending)/self.environment_batch_samples)*self.environment_batch_samples)-pending
expected = max(0, self.processed_input_samples+count-self.latency_samples-self.output_samples)
output = self.process(np.zeros((padded, self.source_count)))
self.output_samples -= max(0, len(output)-expected)
output = output[:expected]
self.finished = True
return output
def info(self):
distances = [s.distance_m for s in self.distance_states if s is not None]
return dict(hrtf_input_kind=self.hrtf_input_kind, hrtf_input_path=self.hrtf_input_path,
cache_policy=None, field=self.field.info(), reference=self.environment.info(),
level_calibration=self.level_calibration,
latency_compensated_samples=self.latency_samples,
processing_batch_samples=self.environment_batch_samples,
recording_origin_compensated_samples=self.origin,
current_reference_distances_m=distances,
parameter_updates=self.parameter_updates, direct_representation=("77-band" if self.compiled else "full-HRIR"),
radial_model="approximate-rigid-sphere-ratio", precision="float64/complex128")