Implement OAMD metadata rendering and runtime table generation

- Add oamd_params/oamd_math for OAMD metadata state, ISF matrix assembly,
  runtime size-table generation and bed routing.
- Apply per-block gain/inactive, zone/elevation, size, snap, divergence,
  position refinement, priority budget, ISF and bed distribution in the
  speaker renderer.
- Compute gain, size, priority, position, refinement and delta values from
  formulas; generate the size spread kernel from speaker-layout geometry.
- Derive bed labels, routing, reference positions and automatic attenuation
  from speaker layouts.
- Correct the surround positions of 5.1, 5.1.2 and 5.1.4 and add the 6.0
  speaker layout.
- Update README, data README and math notes for the metadata path and
  runtime table generation.
This commit is contained in:
TheM14 committed 2026-11-08 14:01:07 +08:00
1 parent 17e52a531c
commit 42a05d7722
13 files changed
+2031 -229

No files matched your search

+135
View File
@@ -4,6 +4,9 @@
那一对落到 ADM 骨架的槽位。JOC 的 DSP 不读取这些信息(规格 §10.1),它只用
``(N, sigma)``。
"""
import math
from functools import lru_cache
from emdf import expand_chanmap
# JOC 核心通道序列的前 5 位(规格 §2):L R C Ls Rs。
@@ -107,6 +110,137 @@ BED_CHANNEL_ADM_POSITION = {
# LFE 不是空间声源:它的 PCM 走独立通路,不参与对象摆位。
BED_LFE_LABELS = frozenset(("RC_LFE", "RC_LFE2"))
# 参考位置网格:bed 标签/输出 slots 的最近邻匹配使用这一组固定的 Q15 点。
BED_REFERENCE_POSITIONS_Q15 = (
(0, 0, 0), (32767, 0, 0), (16384, 0, 0), (-1, -1, -1),
(0, 16384, 0), (32767, 16384, 0), (0, 32767, 0), (32767, 32767, 0),
(16384, 32767, 0), (5285, 0, 0), (27483, 0, 0), (0, 5285, 0),
(32767, 5285, 0), (7928, 0, 32767), (24840, 0, 32767),
(7928, 7928, 32767), (24840, 7928, 32767), (7928, 16384, 32767),
(24840, 16384, 32767), (7928, 24840, 32767), (24840, 24840, 32767),
(7928, 32767, 32767), (24840, 32767, 32767), (11099, 0, 0),
(21669, 0, 0), (0, 11099, 0), (32767, 11099, 0), (0, 21669, 0),
(32767, 21669, 0), (0, 27483, 0), (32767, 27483, 0), (5285, 32767, 0),
(27483, 32767, 0), (11099, 32767, 0), (21669, 32767, 0),
(16384, 0, 32767), (16384, 16384, 32767), (16384, 32767, 32767),
)
# 自动 trim 的每个布局基准衰减,单位 dB。
BED_LAYOUT_ATTENUATION_DB = {
"20": -4.5, "31": -4.5, "40": -3.0, "51": -3.0, "60": -1.5,
"71": -1.5, "512": -2.25, "514": -1.5, "712": -0.75,
"714": 0.0, "914": 0.0, "916": 0.0,
}
def _channel_positions(layout):
"""Channel index -> Q15 room position (LFE gets the special sentinel)."""
found = {}
for region in layout.regions:
for speaker, coordinate in zip(region.speaker_ids,
region.coordinates_q15):
found.setdefault(int(speaker), tuple(coordinate))
result = []
for index, channel in enumerate(layout.channels):
if channel == "LFE":
result.append((-1, -1, -1))
else:
result.append(found.get(index, (-1, -1, -1)))
return result
def _nearest_reference(coordinate):
"""Index of the closest entry in BED_REFERENCE_POSITIONS_Q15."""
if tuple(coordinate) == (-1, -1, -1):
return 3
position = (coordinate[0] / 32768.0, coordinate[1] / 32768.0,
coordinate[2] / 32768.0)
best = 3
best_distance = float("inf")
for index, candidate in enumerate(BED_REFERENCE_POSITIONS_Q15):
if candidate == (-1, -1, -1):
continue
distance = math.dist(position,
(candidate[0] / 32768.0,
candidate[1] / 32768.0,
candidate[2] / 32768.0))
if distance < best_distance:
best = index
best_distance = distance
return best
def _nearest_channel(desired, positions, tolerance=0.05):
"""Channel index nearest to desired (u, v, w), or None if too far."""
best = None
best_distance = float("inf")
for index, coordinate in enumerate(positions):
if coordinate == (-1, -1, -1):
continue
candidate = (coordinate[0] / 32768.0, coordinate[1] / 32768.0,
coordinate[2] / 32768.0)
distance = math.dist(desired, candidate)
if distance < best_distance:
best = index
best_distance = distance
return best if best_distance <= tolerance else None
@lru_cache(maxsize=32)
def bed_output_description(layout):
"""Derive the bed routing description from a speaker layout.
The description is keyed by the standard bed label order and contains the
output reference-position list, per-label slot routing and the automatic
trim attenuation for the layout.
"""
channels = tuple(layout.channels)
positions = _channel_positions(layout)
output_labels = [_nearest_reference(position) for position in positions]
label_to_slot = []
direct_output_labels = []
for label in (item for pair in BED_LAYOUT_ORDER
for item in pair.split("/")):
name = BED_CHANNEL_NAMES.get(label)
slot = None
if name in channels:
slot = channels.index(name)
elif label == "RC_LFE2" and "LFE" in channels:
slot = channels.index("LFE")
else:
desired = adm_xyz_to_render_coordinates(
*BED_CHANNEL_ADM_POSITION[label])
slot = _nearest_channel(desired, positions)
if slot is None:
label_to_slot.append(-1 if label in BED_LFE_LABELS else -2)
else:
label_to_slot.append(int(slot))
if label in BED_LFE_LABELS:
direct_output_labels.append(3)
else:
desired = adm_xyz_to_render_coordinates(
*BED_CHANNEL_ADM_POSITION[label])
best = None
best_distance = float("inf")
for index, candidate in enumerate(BED_REFERENCE_POSITIONS_Q15):
if candidate == (-1, -1, -1):
continue
point = (candidate[0] / 32768.0, candidate[1] / 32768.0,
candidate[2] / 32768.0)
distance = math.dist(desired, point)
if distance < best_distance:
best = index
best_distance = distance
direct_output_labels.append(int(best))
return {
"output_labels": output_labels,
"label_to_slot": label_to_slot,
"direct_output_labels": direct_output_labels,
"automatic_attenuation_db": BED_LAYOUT_ATTENUATION_DB.get(
layout.name, 0.0),
}
# 标准 bed 布局名:由声明声道的集合唯一确定(9.1.6 这类常用叫法即由此得来)。
BED_LAYOUT_NAMES = {
frozenset(("L", "R")): "2.0",
@@ -115,6 +249,7 @@ BED_LAYOUT_NAMES = {
frozenset(("L", "R", "Ls", "Rs")): "4.0",
frozenset(("L", "R", "C", "Ls", "Rs")): "5.0",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs")): "5.1",
frozenset(("L", "R", "Ls", "Rs", "Lrs", "Rrs")): "6.0",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs")): "7.1",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Lw", "Rw")): "9.1",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Ltm", "Rtm")): "5.1.2",
+501 -153
View File
@@ -5,6 +5,8 @@ ch1..15;bed/ISF/未激活对象没有位置字段,保持上一帧位置。el
驱动,未知 element 按边界跳过;个别编码器的 ``oa_element_size`` 比实际内容短时以
结构解析为准,差异记入 ``diagnostics``,不算变体错误。
"""
import math
import numpy as np
from variant_error import UnsupportedVariantError, bytes_descriptor
@@ -25,6 +27,18 @@ TRIM_LUT = (6.0, 3.0, 1.5, 0.75, -0.75, -1.5, -3.0, -4.5,
-6.0, -7.5, -9.0, -10.5, -12.0, -13.5, -15.0, -36.0)
# bal3d_y 的编码为 (code + 1) / 16,幅度落在 [1/16, 1],取不到 0。
BALANCE_STEP = 16
# 5.5.3/5.5.5 divergence 码表(公开实现固定值),Q15 量纲。
DIVERGENCE_TABLE = (
0, 0, 66, 117, 209, 331, 467, 659,
829, 1042, 1309, 1643, 2059, 2302, 2573, 2872,
3202, 3564, 3960, 4391, 4856, 5354, 5882, 6437,
7014, 7605, 8204, 8803, 9394, 9970, 10524, 11051,
11548, 12011, 12441, 12835, 13197, 13525, 13823, 14092,
14335, 14553, 14748, 14924, 15081, 15221, 15347, 15459,
15559, 15649, 15728, 15799, 15863, 15970, 16055, 16123,
16176, 16219, 16267, 16301, 16332, 16355, 16368, 16384,
)
DIVERGENCE_QUICK_INDEX = (26, 29, 32, 63)
# 5.6.0.11 Table 11b:ISF 类型 → 对象数。
ISF_OBJECT_COUNTS = (4, 8, 10, 14, 15, 30)
# 5.6.1.1.4 Table 12:10 bit 标准 bed 掩码。array position 9(RC_L/RC_R)是 channel
@@ -179,6 +193,8 @@ def _parse_program_assignment(reader, raw_payload):
"content_description": None,
"bed_assignments": [],
"num_bed_objects": 0,
"b_bed_chan_distribute": False,
"bed_mask": 0,
"isf_idx": None,
"num_isf_objects": 0,
"num_dynamic_objects": None,
@@ -187,6 +203,11 @@ def _parse_program_assignment(reader, raw_payload):
program["lfe_present"] = bool(reader.read(1))
# 5.6.4.8:对象顺序为 bed → ISF → dynamic,LFE 属于 bed,排在最前。
program["num_bed_objects"] = 1 if program["lfe_present"] else 0
if program["lfe_present"]:
program["bed_assignments"] = [{
"instance": 0, "lfe_only": True, "channels": ["RC_LFE"],
}]
program["bed_mask"] = 0x8
return program
mask = reader.read(4)
@@ -207,16 +228,19 @@ def _parse_program_assignment(reader, raw_payload):
continue
if reader.read(1): # b_standard_chan_assign
bed_mask = reader.read(10)
program["bed_assignments"].append({
"instance": instance, "lfe_only": False, "standard": True,
"mask": bed_mask, "channels": standard_bed_channels(bed_mask),
})
standard = True
channels = standard_bed_channels(bed_mask)
wide_mask = standard_bed_mask(bed_mask)
else:
bed_mask = reader.read(17)
program["bed_assignments"].append({
"instance": instance, "lfe_only": False, "standard": False,
"mask": bed_mask, "channels": nonstandard_bed_channels(bed_mask),
})
standard = False
channels = nonstandard_bed_channels(bed_mask)
wide_mask = bed_mask
program["bed_assignments"].append({
"instance": instance, "lfe_only": False, "standard": standard,
"mask": bed_mask, "channels": channels,
})
program["bed_mask"] |= wide_mask
program["num_bed_objects"] = sum(
len(assignment["channels"]) for assignment in program["bed_assignments"])
if mask & 0x2:
@@ -235,82 +259,264 @@ def _parse_program_assignment(reader, raw_payload):
return program
def _parse_object_info_block(reader, object_index, in_bed_or_isf, raw_payload, retain_spatial=False):
"""5.5.9 ``object_info_block(0)``:一个对象的属性更新。"""
# 17 bit 非标准 bed 标签在 wide mask 中的位顺序(与 NONSTD_BED_LABELS 相同)。
_STANDARD_BED_TO_WIDE = (0x3, 0x4, 0x8, 0x30, 0xC0,
0x300, 0xC00, 0x3000, 0xC000, 0x10000)
def standard_bed_mask(bed_mask):
"""5.6.1.1.4 的 10 bit 标准掩码 → 17 bit 非标准 bed 掩码。"""
wide = 0
for bit, expanded in enumerate(_STANDARD_BED_TO_WIDE):
if (int(bed_mask) >> bit) & 1:
wide |= expanded
return wide
def _q15_ratio(numerator, denominator):
"""Q15 of ``numerator / denominator``, rounded half up and capped."""
return min(32767, int(math.floor(32768.0 * numerator / denominator + 0.5)))
def _gain_q(gain_idx, gain_bits):
"""gain_idx/bits → 整数增益 Qg(单位增益 4096)。"""
if gain_idx == 0:
return 4096
if gain_idx == 1:
return 0
if gain_idx == 2:
bits = int(gain_bits)
db = 15 - bits if bits < 15 else 14 - bits
return int(math.floor(4096.0 * (10.0 ** (db / 20.0))))
raise ValueError("gain_idx=3 需要同块前一对象的状态")
def _size_q(mode, values):
"""size_idx 与 5 bit 数值 → (sx, sy, sz) 整数 Q15。"""
if mode == 1:
value = _q15_ratio(int(values), 31)
return (value, value, value)
if mode == 2:
return tuple(_q15_ratio(int(value), 31) for value in values)
return (0, 0, 0)
_RENDER_DEFAULTS = {
"qx": 16384, "qy": 16384, "qz": 0,
"coarse": (31, 31, 0),
"zone_idx": 0, "enable_elevation": True,
"size_mode": 0, "size_q": (0, 0, 0), "snap": 0,
"divergence_q": 0,
}
def _clamp_coarse(coarse):
x, y, z = coarse
return (min(max(int(x), 0), 62), min(max(int(y), 0), 62),
min(max(int(z), -15), 15))
def _coarse_to_q15(coarse):
x, y, z = coarse
qx = _q15_ratio(int(x), 62)
qy = _q15_ratio(int(y), 62)
magnitude = _q15_ratio(abs(int(z)), 15)
return qx, qy, (-magnitude if z < 0 else magnitude)
_POSITION_DELTA = (0, 1, 2, 3, -4, -3, -2, -1)
_REFINE_DELTA = (1, 2, -1, -2)
def _refine_xy(index):
"""Q15 code for one refined X/Y position index ``5*coarse + delta + 2``."""
return min(32767, max(0, int(math.floor(
(int(index) - 2) * 32768.0 / 310.0 + 0.5))))
def _refine_z(index):
"""Q15 code for one refined Z position index (signed)."""
return min(32767, max(-32767, int(math.floor(
(int(index) - 2) * 32768.0 / 75.0 + 0.5))))
def _copy_render_state(previous):
state = {key: previous[key] for key in _RENDER_DEFAULTS}
state["coarse"] = tuple(previous["coarse"])
state["size_q"] = tuple(previous["size_q"])
state["qx"], state["qy"], state["qz"] = previous["qx"], previous["qy"], previous["qz"]
state["divergence_q"] = int(previous["divergence_q"])
return state
def parse_object_info_block(reader, object_index, block_index, kind,
previous_block, raw_payload, retain_spatial=False,
object_states=None):
"""5.5.9 ``object_info_block``:展开一个对象的一个块。"""
info = {
"object": object_index,
"in_bed_or_isf": bool(in_bed_or_isf),
"block": block_index,
"kind": kind,
"in_bed_or_isf": kind != "dynamic",
"not_active": bool(reader.read(1)),
}
# blk == 0 且对象激活时 basic info 恒为 full update(status 0b01)。
basic_status = 0 if info["not_active"] else 1
info["basic_status"] = basic_status
if basic_status in (1, 3):
if basic_status == 1:
# 5.5.10:object_basic_info[] = {true, true};
# 5.6.4.12:array[1] = object_gain_idx,array[0] = b_default_object_priority。
gain_present = priority_present = True
else:
flags = reader.read(2)
gain_present = bool(flags & 1)
priority_present = bool(flags & 2)
if gain_present:
inactive = info["not_active"]
if inactive:
info["basic_status"] = 0
info["gain_q"] = 0
info["priority_q"] = 0
render = dict(_RENDER_DEFAULTS)
render["coarse"] = (31, 31, 0)
render["size_q"] = tuple(render["size_q"])
info.update({key: render[key] for key in render if key != "coarse"})
info["coarse"] = render["coarse"]
info["render_status"] = 0
info["qx"], info["qy"], info["qz"] = _coarse_to_q15(render["coarse"])
else:
basic_status = 1 if block_index == 0 else reader.read(2)
info["basic_status"] = basic_status
if basic_status == 0:
info["gain_q"] = 0
info["priority_q"] = 0
elif basic_status == 1:
gain_idx = reader.read(2)
info["gain_idx"] = gain_idx
if gain_idx == 2:
info["gain_bits"] = reader.read(6)
if priority_present:
gain_bits = reader.read(6) if gain_idx == 2 else None
if gain_bits is not None:
info["gain_bits"] = gain_bits
if gain_idx == 3:
if object_index == 0:
info["gain_q"] = 4096
elif object_states[object_index - 1][block_index]["not_active"]:
info["gain_q"] = 0
else:
info["gain_q"] = object_states[object_index - 1][block_index]["gain_q"]
else:
info["gain_q"] = _gain_q(gain_idx, gain_bits)
default_priority = bool(reader.read(1))
info["default_priority"] = default_priority
if not default_priority:
info["priority_bits"] = reader.read(5)
render_status = 0 if (info["not_active"] or info["in_bed_or_isf"]) else 1
info["render_status"] = render_status
if render_status in (1, 3):
if render_status == 1:
# 5.5.11:obj_render_info[] = {true, true, true, true}。
position_present = zone_present = size_present = screen_present = True
if default_priority:
info["priority_q"] = 32767
else:
bits = reader.read(5)
info["priority_bits"] = bits
info["priority_q"] = 1024 * bits
elif basic_status == 2:
info["gain_q"] = previous_block["gain_q"]
info["priority_q"] = previous_block["priority_q"]
else:
flags = reader.read(4)
position_present = bool(flags & 0x1)
zone_present = bool(flags & 0x2)
size_present = bool(flags & 0x4)
screen_present = bool(flags & 0x8)
if position_present:
# blk == 0 时 b_differential_position_specified 恒为 FALSE。
x = reader.read(6)
y = reader.read(6)
z_sign = reader.read(1)
z = reader.read(4)
info["position"] = (x, y, z if z_sign else -z)
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))
if size_present:
size_idx = reader.read(2)
info["object_size_idx"] = size_idx
if size_idx == 1:
reader.read(5)
elif size_idx == 2:
reader.read(15)
if screen_present:
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))
flags = reader.read(2)
info["basic_mask"] = flags
info["gain_q"] = previous_block["gain_q"]
info["priority_q"] = previous_block["priority_q"]
if flags & 1:
gain_idx = reader.read(2)
info["gain_idx"] = gain_idx
gain_bits = reader.read(6) if gain_idx == 2 else None
if gain_bits is not None:
info["gain_bits"] = gain_bits
if gain_idx == 3:
if object_index == 0:
info["gain_q"] = 4096
elif object_states[object_index - 1][block_index]["not_active"]:
info["gain_q"] = 0
else:
info["gain_q"] = object_states[object_index - 1][block_index]["gain_q"]
else:
info["gain_q"] = _gain_q(gain_idx, gain_bits)
if flags & 2:
default_priority = bool(reader.read(1))
info["default_priority"] = default_priority
if default_priority:
info["priority_q"] = 32767
else:
bits = reader.read(5)
info["priority_bits"] = bits
info["priority_q"] = 1024 * bits
render_status = 0 if kind in ("bed", "isf") else (
1 if block_index == 0 else reader.read(2))
info["render_status"] = render_status
if render_status in (1, 3):
if render_status == 1:
position_present = zone_present = size_present = screen_present = True
flags = 0xF
else:
flags = reader.read(4)
position_present = bool(flags & 0x1)
zone_present = bool(flags & 0x2)
size_present = bool(flags & 0x4)
screen_present = bool(flags & 0x8)
info["render_mask"] = flags
state = _copy_render_state(previous_block) if render_status == 3 \
else dict(_RENDER_DEFAULTS)
state["coarse"] = tuple(state["coarse"])
state["size_q"] = tuple(state["size_q"])
if position_present:
differential = (block_index > 0 and bool(reader.read(1)))
if differential:
dx, dy, dz = (_POSITION_DELTA[reader.read(3)]
for _axis in range(3))
coarse = (previous_block["coarse"][0] + dx,
previous_block["coarse"][1] + dy,
previous_block["coarse"][2] + dz)
else:
x = reader.read(6)
y = reader.read(6)
z_sign = reader.read(1)
z = reader.read(4)
coarse = (x, y, z if z_sign else -z)
state["coarse"] = _clamp_coarse(coarse)
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["distance_specified"] = outside
info["at_infinity"] = infinite
info["distance_factor_idx"] = distance_index
if zone_present:
state["zone_idx"] = reader.read(3)
state["enable_elevation"] = bool(reader.read(1))
if size_present:
size_idx = reader.read(2)
state["size_mode"] = size_idx
if size_idx == 1:
state["size_q"] = _size_q(1, reader.read(5))
elif size_idx == 2:
state["size_q"] = _size_q(2, (reader.read(5), reader.read(5),
reader.read(5)))
else:
state["size_q"] = (0, 0, 0)
if screen_present:
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["use_screen_ref"] = screen_ref
info["screen_factor_bits"] = screen_bits
info["depth_factor_idx"] = depth_index
state["snap"] = 1 if reader.read(1) else 0
info.update({key: state[key] for key in state if key != "coarse"})
info["coarse"] = state["coarse"]
info["qx"], info["qy"], info["qz"] = _coarse_to_q15(state["coarse"])
elif render_status == 2:
info.update({key: previous_block[key] for key in _RENDER_DEFAULTS
if key != "coarse"})
info["coarse"] = tuple(previous_block["coarse"])
info["size_q"] = tuple(previous_block["size_q"])
info["qx"], info["qy"], info["qz"] = (
previous_block["qx"], previous_block["qy"], previous_block["qz"])
info["divergence_q"] = previous_block["divergence_q"]
else:
info.update({key: _RENDER_DEFAULTS[key] for key in _RENDER_DEFAULTS
if key != "coarse"})
info["coarse"] = tuple(_RENDER_DEFAULTS["coarse"])
info["size_q"] = tuple(_RENDER_DEFAULTS["size_q"])
info["qx"], info["qy"], info["qz"] = _coarse_to_q15(info["coarse"])
if reader.read(1): # b_additional_table_data_exists
info["additional_table_bytes"] = reader.read(4) + 1
reader.skip(info["additional_table_bytes"] * 8)
@@ -318,7 +524,7 @@ def _parse_object_info_block(reader, object_index, in_bed_or_isf, raw_payload, r
def _parse_object_element(reader, element, raw_payload, retain_spatial=False):
"""5.5.5 ``object_element()``:时间信息 + 每个对象一条 object_info_block。"""
"""5.5.5 ``object_element()``:时间头 + 对象外/块内顺序的完整块状态。"""
object_count = element["object_count"]
try:
sample_offset_code = reader.read(2)
@@ -355,11 +561,20 @@ def _parse_object_element(reader, element, raw_payload, retain_spatial=False):
reserved_data_not_present = bool(reader.read(1))
if not reserved_data_not_present:
reader.read(5)
objects = [
_parse_object_info_block(
reader, index, index < element["bed_isf_objects"], raw_payload, retain_spatial)
for index in range(object_count)
]
kinds = element["object_kinds"]
object_blocks = []
for object_index in range(object_count):
kind = kinds[object_index]
states = []
previous = None
for block_index in range(block_count):
info = parse_object_info_block(
reader, object_index, block_index, kind, previous,
raw_payload, retain_spatial, object_blocks)
info["previous"] = previous
states.append(info)
previous = info
object_blocks.append(states)
except UnsupportedVariantError:
raise
except ValueError as exc:
@@ -374,10 +589,101 @@ def _parse_object_element(reader, element, raw_payload, retain_spatial=False):
"sample_offset": sample_offset,
"blocks": blocks,
"reserved_data_not_present": reserved_data_not_present,
"objects": objects,
"objects": object_blocks,
}
def _parse_extended_element(reader, element, object_element, raw_payload):
"""扩展对象元素:divergence 与高精度位置补充。"""
blocks = object_element["blocks"]
block_count = len(blocks)
objects = object_element["objects"]
object_count = len(objects)
if object_element is None:
raise UnsupportedVariantError(
"oamd", "extended_without_object_element",
"OAMD 扩展对象元素在默认主对象元素之前出现",
details={"element": _element_details([element])[0],
"payload": bytes_descriptor(raw_payload)})
try:
divergence_present = bool(reader.read(1))
if divergence_present:
table = DIVERGENCE_TABLE
quick = DIVERGENCE_QUICK_INDEX
for object_index in range(object_count):
for block_index in range(block_count):
state = objects[object_index][block_index]
if state["not_active"]:
state["divergence_q"] = 0
continue
if state["kind"] != "dynamic":
continue
present = bool(reader.read(1))
if not present:
state["divergence_q"] = 0
continue
mode = reader.read(2)
if mode == 0:
state["divergence_q"] = int(table[quick[reader.read(2)]])
elif mode == 1:
state["divergence_q"] = (
0 if block_index == 0
else objects[object_index][block_index - 1]["divergence_q"])
elif mode == 2:
state["divergence_q"] = int(table[reader.read(6)])
else:
reader.read(6)
state["divergence_q"] = 0
refinement_present = bool(reader.read(1))
if refinement_present:
for object_index in range(object_count):
for block_index in range(block_count):
state = objects[object_index][block_index]
if state["not_active"] or state["kind"] != "dynamic":
continue
present = bool(reader.read(1))
if not present:
continue
mask = reader.read(3)
if not mask:
continue
deltas = [0, 0, 0]
for axis in range(3):
if mask & (1 << axis):
deltas[axis] = _REFINE_DELTA[reader.read(2)]
qx, qy, qz = state["qx"], state["qy"], state["qz"]
def _sign(value):
return -1 if value < 0 else 1
def _coarse_xy(value):
return 31 + _sign(value - 16384) * (
(62 * abs(value - 16384) + 16384) >> 15)
def _coarse_z(value):
value = max(value, -32767)
return _sign(value) * ((15 * abs(value) + 16384) >> 15)
ix = _coarse_xy(qx)
iy = _coarse_xy(qy)
iz = _coarse_z(qz)
sign = _sign(iz)
qx = _refine_xy(5 * ix + 2 + deltas[0])
qy = _refine_xy(5 * iy + 2 + deltas[1])
qz = sign * _refine_z(sign * (5 * iz + deltas[2]) + 2)
state["qx"], state["qy"], state["qz"] = qx, qy, qz
except ValueError as exc:
raise _syntax_error(
"extended_element_syntax",
"OAMD 扩展对象元素字段越界或不完整",
raw_payload, exc,
element=_element_details([element])[0],
object_count=object_count,
) from exc
return {"divergence_present": divergence_present,
"refinement_present": refinement_present}
def _parse_trim_element(reader, object_count, raw_payload):
"""``trim_element``:全局 trim/warp 配置,每个 payload 出现一次。
@@ -490,8 +796,9 @@ def parse_trim(bits_one):
return None
def _parse_elements(reader, alternate_present, object_count, program, raw_payload, retain_spatial=False):
"""5.5.4 ``oa_element_md()`` 目录;对象 element 按结构解析,其余按声明长度跳过。"""
def _parse_elements(reader, alternate_present, object_count, program,
raw_payload, retain_spatial=False):
"""5.5.4 ``oa_element_md()`` 目录:先选替代变体,再解析内容。"""
try:
element_count = reader.read(4)
if element_count == 0xF:
@@ -507,8 +814,18 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa
details={"payload": bytes_descriptor(raw_payload)})
bed_isf_objects = program["num_bed_objects"] + program["num_isf_objects"]
kinds = []
for object_index in range(object_count):
if object_index < program["num_bed_objects"]:
kinds.append("bed")
elif object_index < bed_isf_objects:
kinds.append("isf")
else:
kinds.append("dynamic")
elements = []
object_element = None
extended = None
seen = set()
for ordinal in range(element_count):
header_start = reader.position
try:
@@ -534,16 +851,8 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa
"payload_bits": reader.limit,
"payload": bytes_descriptor(raw_payload),
})
try:
alternate_data_id = (reader.read(4)
if alternate_present else None)
discard_unknown = bool(reader.read(1))
except ValueError as exc:
raise _syntax_error(
"element_control_bounds", "OAMD element 太短,无法容纳控制字段",
raw_payload, exc,
element_ordinal=ordinal, element_id=element_id,
size_bytes=size_bytes) from exc
alternate_data_id = reader.read(4) if alternate_present else 0
discard_unknown = bool(reader.read(1))
element = {
"ordinal": ordinal,
"element_id": element_id,
@@ -557,30 +866,38 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa
"object_count": object_count,
"bed_isf_objects": bed_isf_objects,
}
if element_id == OBJECT_ELEMENT_ID:
if object_element is not None:
raise UnsupportedVariantError(
"oamd", "multiple_object_elements",
"OAMD 含多个 object element",
details={
"elements": _element_details(elements + [element]),
"payload": bytes_descriptor(raw_payload),
})
selected = (element_id in (OBJECT_ELEMENT_ID, TRIM_ELEMENT_ID, 5)
and alternate_data_id == 0 and element_id not in seen)
if not selected:
element["parsed_end_bit"] = region_end
reader.position = region_end
elif element_id == OBJECT_ELEMENT_ID:
element["object_kinds"] = kinds
object_element = _parse_object_element(
reader, element, raw_payload, retain_spatial)
element["parsed_end_bit"] = reader.position
element["object_element"] = object_element
# 个别编码器声明的 oa_element_size 比实际内容短;以结构解析结果为准。
seen.add(element_id)
reader.position = max(region_end, reader.position)
elif element_id == TRIM_ELEMENT_ID:
elif element_id == 5:
if object_element is None:
raise UnsupportedVariantError(
"oamd", "extended_before_object_element",
"OAMD 扩展对象元素出现在默认主对象元素之前",
details={"element": _element_details(elements + [element])[0],
"payload": bytes_descriptor(raw_payload)})
extended = _parse_extended_element(reader, element,
object_element, raw_payload)
element["parsed_end_bit"] = reader.position
element["extended_element"] = extended
seen.add(element_id)
reader.position = max(region_end, reader.position)
else:
element["trim_element"] = _parse_trim_element(
reader, object_count, raw_payload)
element["parsed_end_bit"] = reader.position
seen.add(element_id)
reader.position = max(region_end, reader.position)
else:
# extended / 未知 element:按声明长度整体跳过(5.5.4)。
element["parsed_end_bit"] = region_end
reader.position = region_end
elements.append(element)
if object_element is None:
@@ -605,6 +922,16 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa
return object_element, elements
def _element_kinds(program, object_count):
kinds = []
bed_end = program["num_bed_objects"]
isf_end = bed_end + program["num_isf_objects"]
for index in range(object_count):
kinds.append("bed" if index < bed_end
else ("isf" if index < isf_end else "dynamic"))
return kinds
def program_bed_summary(bits_one):
"""只走 OAMD 头与 program_assignment(),返回 bed 声明与内容类型。
@@ -645,6 +972,11 @@ def program_bed_summary(bits_one):
"num_bed_objects": program["num_bed_objects"],
"num_isf_objects": program["num_isf_objects"],
"num_dynamic_objects": program["num_dynamic_objects"],
"isf_idx": program["isf_idx"],
"dynamic_object_only": program["dynamic_object_only"],
"lfe_present": program["lfe_present"],
"b_bed_chan_distribute": program["b_bed_chan_distribute"],
"bed_mask": program["bed_mask"],
}
@@ -657,8 +989,8 @@ def program_bed_channels(bits_one):
return program_bed_summary(bits_one)["channels"]
def frame_update(bits_one, *, retain_spatial=False):
"""单帧 OAMD → 位置字段增量及其 sample offset/ramp duration。"""
def frame_state(bits_one, *, retain_spatial=False):
"""完整展开一帧 OAMD:每个对象每一块的属性、节目分区与扩展对象元素更新。"""
bits, raw_payload = _payload_bits(bits_one)
try:
version = bits[0] << 1 | bits[1]
@@ -679,14 +1011,14 @@ def frame_update(bits_one, *, retain_spatial=False):
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, retain_spatial)
reader, alternate_present, object_count, program, raw_payload,
retain_spatial)
except UnsupportedVariantError:
raise
except ValueError as exc:
raise _syntax_error(
"header_truncated", "OAMD header 字段越界或不完整",
raw_payload, exc, payload_bits=len(bits)) from exc
if version != 0:
raise UnsupportedVariantError(
"oamd", "oamd_version",
@@ -695,46 +1027,27 @@ def frame_update(bits_one, *, retain_spatial=False):
"version": version,
"payload": bytes_descriptor(raw_payload),
})
if object_count > MAX_SLOTS:
raise UnsupportedVariantError(
"oamd", "object_count",
"OAMD 对象数超过当前 16 槽对象模型",
details={
"object_count": object_count,
"max_slots": MAX_SLOTS,
"payload": bytes_descriptor(raw_payload),
})
blocks = object_element["blocks"]
if len(blocks) != 1:
raise UnsupportedVariantError(
"oamd", "multiple_position_blocks",
"OAMD 一帧含多个对象位置更新块,单次 frame_update 无法表达",
details={
"block_count": len(blocks),
"blocks": blocks,
"payload": bytes_descriptor(raw_payload),
"repair_hint": "按 ObjectInfoBlock 顺序逐块解析坐标,再生成分段 ADM ramp",
})
# 对外契约:始终给出 16 个槽;本帧没有覆盖到的槽保持上一帧位置。
out = {(slot, field): None for slot in range(MAX_SLOTS)
for field in ("q1", "q2", "q3")}
for info in object_element["objects"]:
slot = info["object"]
position = info.get("position")
if position is None:
# bed/ISF 对象与未激活对象没有位置字段:保持上一帧位置。
out[(slot, "q1")] = None
out[(slot, "q2")] = None
out[(slot, "q3")] = None
objects = object_element["objects"]
# 对外契约:16 槽位置视图(block 0),保留旧 frame_update 的 values 形状。
values = {(slot, field): None for slot in range(MAX_SLOTS)
for field in ("q1", "q2", "q3")}
for object_index, states in enumerate(objects):
if object_index >= MAX_SLOTS:
break
state = states[0]
if state["not_active"] or state.get("position") is None and \
state["render_status"] == 0 and state["kind"] != "dynamic":
# bed/ISF 与未激活对象没有位置字段:保持上一帧位置。
continue
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
# Clamp negative height to zero 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)
values[(object_index, "q1")] = state["qx"]
values[(object_index, "q2")] = state["qy"]
qz = state["qz"]
if not state["enable_elevation"]:
# 描述符构建在 enable_elevation=0 时令 z=0。
qz = 0
values[(object_index, "q3")] = (
qz if qz >= 0 or retain_spatial else 0)
size_mismatch = [
{
"element_ordinal": element["ordinal"],
@@ -746,11 +1059,18 @@ def frame_update(bits_one, *, retain_spatial=False):
for element in elements
if element["parsed_end_bit"] > element["body_end_bit"]
]
trim = next((element["trim_element"] for element in elements
if "trim_element" in element), None)
return {
"values": out,
"values": values,
"block_offset_samples": blocks[0]["block_offset_samples"],
"ramp_duration_samples": blocks[0]["ramp_duration_samples"],
"blocks": blocks,
"sample_offset": object_element["sample_offset"],
"object_count": object_count,
"kinds": [states[0]["kind"] for states in objects],
"objects": [dict(states[0]) for states in objects],
"object_blocks": objects,
"program": {
"dynamic_object_only": program["dynamic_object_only"],
"lfe_present": program["lfe_present"],
@@ -760,13 +1080,41 @@ def frame_update(bits_one, *, retain_spatial=False):
"num_dynamic_objects": program["num_dynamic_objects"],
"bed_channels": [list(assignment["channels"])
for assignment in program["bed_assignments"]],
"bed_mask": program["bed_mask"],
"b_bed_chan_distribute": program["b_bed_chan_distribute"],
"isf_idx": program["isf_idx"],
},
"trim": trim,
"elements": _element_details(elements),
"objects": object_element["objects"],
"size_mismatch": size_mismatch,
}
def frame_update(bits_one, *, retain_spatial=False):
"""单帧 OAMD → 位置字段增量及其 sample offset/ramp duration。"""
state = frame_state(bits_one, retain_spatial=retain_spatial)
if state["object_count"] > MAX_SLOTS:
raise UnsupportedVariantError(
"oamd", "object_count",
"OAMD 对象数超过当前 16 槽对象模型",
details={
"object_count": state["object_count"],
"max_slots": MAX_SLOTS,
})
if len(state["blocks"]) != 1:
# 旧接口(ADM/双耳/sofa 轨迹)只表达单块位置;多块由 frame_state 提供给
# 扬声器渲染路径。这里保持与旧实现一致的显式拒绝,避免静默只取首块。
raise UnsupportedVariantError(
"oamd", "multiple_position_blocks",
"OAMD 一帧含多个对象位置更新块,单次 frame_update 无法表达",
details={
"block_count": len(state["blocks"]),
"blocks": state["blocks"],
"repair_hint": "扬声器渲染路径改用 frame_state()",
})
return state
def frame_update_values(bits_one):
"""兼容接口:只返回 ``{(slot, field): q|None}``。"""
return frame_update(bits_one)["values"]
+699
View File
@@ -0,0 +1,699 @@
"""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 :mod:`oamd_params`; this module
does not read files itself.
"""
from __future__ import annotations
import math
from functools import lru_cache
import numpy as np
from bed_layout import BED_REFERENCE_POSITIONS_Q15, bed_output_description
from speaker_layouts import get_speaker_layout
F = np.float32
TINY = F(2 ** -126)
def exp2_approx(value):
"""Specified ``2**x`` approximation used by the compensation kernels."""
x = F(value)
n = math.floor(float(x))
f = F(x - F(n))
poly = F(F(F(F(f * F(0.6862793)) + F(1)) + F(F(f * f) * F(0.25482178)))
+ F(F(f * F(f * f)) * F(0.058898926)))
return F(math.ldexp(float(poly), n))
def pow_approx(x, power):
"""Specified ``x**power`` approximation; ``power`` is a float32."""
x = F(x)
if abs(x) < TINY:
return F(0)
fraction, exponent = math.frexp(float(x))
fraction = F(fraction)
logarithm = F(F(F(F(fraction * F(4.0793457)) - F(2.6931152))
- F(F(fraction * fraction) * F(1.3862305))) + F(exponent))
return exp2_approx(F(logarithm * F(power)))
def norm(vector):
"""Stable float32 normalization with the tiny-norm floor."""
vector = np.asarray(vector, dtype=np.float32)
total = F(0)
for value in vector:
total = F(total + F(value * value))
if abs(total) < TINY:
total = F(0)
denominator = max(F(math.sqrt(float(total))), F(1e-5))
result = (vector / denominator).astype(np.float32)
result[np.abs(result) < TINY] = 0
return result
def boundary(axis, edge, position, t):
"""One boundary weight of the size kernel."""
edge, position, t = F(edge), F(position), F(t)
delta = F(F(edge - position) * F(0.25))
if t < F(0.000025) or abs(delta) > t:
return F(0)
ratio = F(float(delta) / float(t))
ratio2 = F(ratio * ratio)
weight = exp2_approx(F(F(-F(ratio2 * ratio2) * F(0.6328125))
* F(26.575424)))
_, exponent = math.frexp(float(weight))
if exponent < -31 or exponent > 1:
weight = F(0)
if axis == 2:
weight = F(F(math.cos(float(F(F(edge * F(0.34147748)) * F(4)))))
* weight)
return weight
def _region_coordinates(region):
"""Normalize the region's Q15 coordinates to the 0..1 room grid."""
return (np.asarray(region.coordinates_q15, dtype=np.float32)
/ F(32768))
def _group_pan_x(coordinates, groups, position, output):
"""Region X panning: equal-power between x-adjacent speakers."""
position = F(position)
for group in groups:
first = group[0]
last = group[-1]
x_first = coordinates[first][0]
x_last = coordinates[last][0]
if position < x_first:
output[first] = F(1)
continue
if position > x_last:
output[last] = F(1)
continue
for left, right in zip(group, group[1:]):
x_left = coordinates[left][0]
x_right = coordinates[right][0]
if not (x_left < x_right and x_left <= position <= x_right):
continue
fraction = F(F(position - x_left) / F(x_right - x_left))
if fraction <= F(0.5):
angle = F(F(F(fraction * F(0.78539819)) * F(0.5)) * F(4))
output[left] = F(np.cos(np.float32(angle)))
output[right] = F(np.sin(np.float32(angle)))
else:
angle = F(F(F(F(1) - fraction) * F(0.78539819))
* F(0.5) * F(4))
output[left] = F(np.sin(np.float32(angle)))
output[right] = F(np.cos(np.float32(angle)))
break
def _group_pan_y(coordinates, groups, position, output):
"""Region Y panning: equal-power between y-adjacent speaker rows."""
if not groups:
return
position = F(position)
rows = [coordinates[group[0]][1] for group in groups]
if position <= rows[0]:
for index in groups[0]:
output[index] = F(1)
return
if position > rows[-1]:
for index in groups[-1]:
output[index] = F(1)
return
for row in range(len(groups) - 1):
y_low = rows[row]
y_high = rows[row + 1]
if not (y_low <= position <= y_high and y_low < y_high):
continue
fraction = F(F(position - y_low) / F(y_high - y_low))
if fraction <= F(0.5):
angle = F(F(F(fraction * F(0.78539819)) * F(0.5)) * F(4))
low_gain = F(np.cos(np.float32(angle)))
high_gain = F(np.sin(np.float32(angle)))
else:
angle = F(F(F(F(1) - fraction) * F(0.78539819))
* F(0.5) * F(4))
low_gain = F(np.sin(np.float32(angle)))
high_gain = F(np.cos(np.float32(angle)))
for index in groups[row]:
output[index] = low_gain
for index in groups[row + 1]:
output[index] = high_gain
return
def _layer_pan_z(coordinates, position):
"""Region Z panning: floor/height equal-power split."""
position = F(position)
if position <= F(0):
floor_gain = F(1)
height_gain = F(0)
elif position < F(0.99996948):
angle = F(F(position * F(0.39269909)) * F(4))
floor_gain = F(np.cos(np.float32(angle)))
height_gain = F(np.sin(np.float32(angle)))
else:
floor_gain = F(0)
height_gain = F(1)
output = np.zeros(len(coordinates), dtype=np.float32)
for index, coordinate in enumerate(coordinates):
output[index] = (height_gain if coordinate[2] >= F(0.5)
else floor_gain)
return output
def _pan_x_all(coordinates, floor_groups, height_groups, position, mode):
output = np.zeros(len(coordinates), dtype=np.float32)
_group_pan_x(coordinates, floor_groups, position, output)
if mode == 3 and height_groups:
extra = np.zeros(len(coordinates), dtype=np.float32)
_group_pan_x(coordinates, height_groups, position, extra)
output = F(output + extra)
return output
def _pan_y_all(coordinates, floor_groups, height_groups, position, mode):
output = np.zeros(len(coordinates), dtype=np.float32)
_group_pan_y(coordinates, floor_groups, position, output)
if mode == 3 and height_groups:
extra = np.zeros(len(coordinates), dtype=np.float32)
_group_pan_y(coordinates, height_groups, position, extra)
output = F(output + extra)
return output
def _boundary_rows(coordinates, floor_groups, height_groups, mode):
x0 = _pan_x_all(coordinates, floor_groups, height_groups, F(0), mode)
x1 = _pan_x_all(coordinates, floor_groups, height_groups, F(1), mode)
y0 = _pan_y_all(coordinates, floor_groups, height_groups, F(0), mode)
y1 = _pan_y_all(coordinates, floor_groups, height_groups, F(1), mode)
if mode == 3:
z1 = _layer_pan_z(coordinates, F(1))
else:
z1 = np.zeros(len(coordinates), dtype=np.float32)
return [x0, x1, y0, y1, z1]
def _size_weight(position, u, t, axis):
"""Per-virtual-point S448 weight used by the generated size tables."""
t = F(t)
offset = F(F(F(u - position)) * F(0.25))
if t < F(0.000025) or abs(float(offset)) > float(t):
return F(0)
ratio = F(offset / t)
squared = F(ratio * ratio)
power = F(F(-F(squared * squared)) * F(0.6328125))
weight = exp2_approx(F(power * F(26.575424)))
if axis == 2:
angle = F(F(F(u * F(0.34147748))) * F(4))
weight = F(F(np.cos(np.float32(angle))) * weight)
return weight
def _axis_table(pan_vectors, position_nodes, speaker_count, offset_start,
axis):
table = np.zeros((position_nodes, 20, speaker_count), dtype=np.float32)
for position_index in range(position_nodes):
position = F(F(position_index) / F(position_nodes - 1))
for size_index in range(20):
t = F(F(size_index) * F(0.036842104))
m = (F(6) if t <= F(0.125)
else F(F(6.869565) - F(t * F(6.956522))))
for u, pan in enumerate(pan_vectors):
weight = _size_weight(position, F(F(u) / F(len(pan_vectors) - 1)),
t, axis)
if weight == F(0):
continue
for speaker in range(speaker_count):
value = F(pan[offset_start + speaker] * weight)
table[position_index, size_index, speaker] = F(
table[position_index, size_index, speaker]
+ pow_approx(value, m))
return table
def _axis2_table(pan_vectors, speaker_index):
table = np.zeros((4, 20), dtype=np.float32)
if speaker_index >= len(pan_vectors[0]):
return table
for position_index in range(4):
position = F(F(position_index) / F(3))
for size_index in range(20):
t = F(F(size_index) * F(0.036842104))
m = (F(6) if t <= F(0.125)
else F(F(6.869565) - F(t * F(6.956522))))
total = F(0)
for u, pan in enumerate(pan_vectors):
weight = _size_weight(position, F(F(u) / F(7)), t, 2)
if weight == F(0):
continue
total = F(total + pow_approx(
F(pan[speaker_index] * weight), m))
table[position_index, size_index] = total
return table
@lru_cache(maxsize=256)
def _region_size_tables(region):
"""Generate the region size tables from the region geometry."""
coordinates = _region_coordinates(region)
floor_groups = tuple(region.axis0_groups)
height_groups = tuple(region.axis1_groups) if region.mode == 3 else ()
floor_count = sum(len(group) for group in floor_groups)
height_count = sum(len(group) for group in height_groups)
mode = region.mode
x_vectors = [_pan_x_all(coordinates, floor_groups, height_groups,
F(F(index) / F(19)), mode) for index in range(20)]
y_vectors = [_pan_y_all(coordinates, floor_groups, height_groups,
F(F(index) / F(19)), mode) for index in range(20)]
z_vectors = ([_layer_pan_z(coordinates, F(F(index) / F(7)))
for index in range(8)] if mode == 3
else [np.zeros(len(coordinates), dtype=np.float32)
for _ in range(8)])
x_floor = _axis_table(x_vectors, 35, floor_count, 0, 0)
x_height = _axis_table(x_vectors, 35, height_count, floor_count, 0)
y_floor = _axis_table(y_vectors, 35, floor_count, 0, 1)
y_height = _axis_table(y_vectors, 35, height_count, floor_count, 1)
if mode == 3:
z_floor = _axis2_table(z_vectors, 0)
z_height = _axis2_table(z_vectors, floor_count)
else:
z_floor = np.zeros(0, dtype=np.float32)
z_height = np.zeros(0, dtype=np.float32)
entry = {
"floor_count": floor_count,
"height_count": height_count,
"mode": mode,
"floor_axis_group_count": len(floor_groups),
"height_axis_group_count": len(height_groups),
"boundary_3d": _boundary_rows(coordinates, floor_groups,
height_groups, mode),
"boundary_2d": _boundary_rows(coordinates, floor_groups, (), 2),
"axis_tables": {
"0": {"position_nodes": 35, "size_nodes": 20,
"floor_stride": floor_count,
"height_stride": height_count,
"floor": x_floor.reshape(-1).copy(),
"height": x_height.reshape(-1).copy()},
"1": {"position_nodes": 35, "size_nodes": 20,
"floor_stride": floor_count,
"height_stride": height_count,
"floor": y_floor.reshape(-1).copy(),
"height": y_height.reshape(-1).copy()},
"2": {"position_nodes": 4, "size_nodes": 20,
"floor_stride": 1, "height_stride": 1,
"floor": z_floor.reshape(-1).copy(),
"height": z_height.reshape(-1).copy()},
},
}
return entry
def _bracket(position, axis):
position = F(position)
step = F(F(1) / F(3 if axis == 2 else 34))
last = 3 if axis == 2 else 34
low = math.floor(float(F(F(last) * position)))
high = min(low + 1, last)
if low > last:
low = high = last
alpha = (F(0) if low == high
else max(F(0), F(F(position - F(F(low) * step)) / step)))
return low, high, alpha
def _size_bracket(t):
t = F(t)
step = F(0.036842104)
low = math.floor(float(F(F(F(t * F(0.71428573)) * F(2)) * F(19))))
if low > 19:
return 19, 19, F(0)
high = min(low + 1, 19)
beta = (F(0) if low == high
else max(F(0), F(float(F(t - F(F(low) * step))) / float(step))))
return low, high, beta
def _axis_weights(entry, axis, position, t, height):
low, high, alpha = _bracket(position, axis)
low_t, high_t, beta = _size_bracket(t)
weights = np.asarray([
F(1) - max(beta, alpha),
F(1) - max(beta, F(1) - alpha),
F(1) - max(F(1) - beta, alpha),
F(1) - max(F(1) - beta, F(1) - alpha)], dtype=np.float32)
total = F(F(F(weights[1] + weights[0]) + weights[2]) + weights[3])
weights = np.asarray([F(float(value) / float(total)) for value in weights],
dtype=np.float32)
table = entry["axis_tables"][str(axis)]
result = []
for group, count in (("floor", entry["floor_count"]),
("height", entry["height_count"] if height else 0)):
if not count:
continue
stride = table[group + "_stride"]
values = np.asarray(table[group], dtype=np.float32).reshape(
table["position_nodes"], 20, stride)
value = F(F(F(weights[0] * values[low, low_t]
+ weights[1] * values[high, low_t])
+ weights[2] * values[low, high_t])
+ weights[3] * values[high, high_t])
result.extend([value[0]] * count if axis == 2 else value[:count])
return np.asarray(result, dtype=np.float32)
def size_kernel(region, position, t, height=True):
"""Size kernel response in region-speaker order."""
entry = _region_size_tables(region)
position = np.asarray(position, dtype=np.float32)
t = F(t)
floor_count = entry["floor_count"]
height_count = entry["height_count"] if height else 0
total = floor_count + height_count
if not total:
return np.zeros(0, dtype=np.float32)
dim = (entry["mode"] if height
else (1 if entry["floor_axis_group_count"] == 1 else 2))
m = (F(6) if t <= F(0.125)
else F(F(6.869565) - F(t * F(6.956522))))
inverse_m = F(F(1) / m)
axes = [_axis_weights(entry, axis, position[axis], t, height)
for axis in range(dim)]
key = "boundary_3d" if dim == 3 else "boundary_2d"
boundary_table = np.asarray(entry[key], dtype=np.float32)[
[4, 0, 1, 2, 3], :total].copy()
for row, (axis, edge) in enumerate([(2, 1), (0, 0), (0, 1),
(1, 0), (1, 1)]):
boundary_table[row] *= boundary(axis, edge, position[axis], t)
inside = axes[0].copy()
if dim > 1:
inside *= axes[1]
if dim > 2:
inside *= axes[2]
inside = norm(inside)
if dim == 1:
return np.asarray([pow_approx(value, inverse_m) for value in inside],
dtype=np.float32)
powered = np.asarray([[pow_approx(value, m) for value in row]
for row in boundary_table], dtype=np.float32)
x, y = axes[0], axes[1]
if dim == 2:
edge = F(F(F(powered[1] * y + powered[2] * y)
+ powered[3] * x) + powered[4] * x)
else:
z = axes[2]
edge = F(F(F(F(F(x * y) * powered[0]
+ F(y * z) * powered[1])
+ F(y * z) * powered[2])
+ F(x * z) * powered[3])
+ F(x * z) * powered[4])
edge = norm(edge)
distances = [position[0], F(1) - position[0],
position[1], F(1) - position[1]]
if dim == 3:
distances.append(F(1) - position[2])
distance = min(distances)
if t < F(0.000025) or (F(F(0.25) * distance) > t
and F(F(0.25) * distance) > F(0.05)):
mix = F(1)
else:
mix = F(F(float(distance) / float(F(t * F(4))))
* F(F(distance * F(5)) * F(distance * F(5))))
joined = F(F(F(mix * inside) + edge) * F(0.5)) * F(0.0625)
return np.asarray([pow_approx(value, inverse_m) for value in joined],
dtype=np.float32)
_SIZE_CURVE_NODES = ((0.0, 0.0), (0.2, 0.075), (0.5, 0.25),
(0.75, 0.45), (1.0, 0.7))
def size_curve_value(size):
"""Map a scalar size in ``[0, 1]`` to ``t`` on the size curve."""
points = [(F(point[0]), F(point[1])) for point in _SIZE_CURVE_NODES]
size = F(size)
if size <= points[0][0]:
return F(0)
if size >= points[-1][0]:
return F(1)
for (size_low, t_low), (size_high, t_high) in zip(points, points[1:]):
if size <= size_high:
span = F(size_high - size_low)
fraction = F(F(size - size_low) / span) if span else F(0)
return F(t_low + F(fraction * F(t_high - t_low)))
return F(1)
def size_targets(meta, layout, region_index, position, size, height=True):
"""Return a size-kernel target vector in ``layout`` channel order."""
region = layout.regions[int(region_index)]
result = np.zeros(layout.channel_count, dtype=np.float32)
if not len(region.speaker_ids):
return result
t = size_curve_value(size)
kernel = size_kernel(region, position, t, height=height)
for value, channel in zip(kernel, region.speaker_ids):
result[channel] = value
return result
def assemble_isf(meta, layout, isf_idx):
"""Assemble one ISF basis matrix from the compressed base tables."""
config = meta["isf"]["configs"][isf_idx]
geometry = meta["isf"]["layouts"][layout]
row_count = config["counts"][4]
matrix = np.asarray(config["matrix_values"], dtype=np.float32).reshape(
-1, 73)
result = np.zeros((row_count, geometry["output_count"]), dtype=np.float32)
cursor = 0
def add_group(name, selector):
nonlocal cursor
record = next((item for item in meta["isf"]["layout_groups"][name]
if item[0] == selector), None)
if record is None:
return
for column in range(record[1], record[2]):
mappings = [config["primary_row_map"]]
if meta["isf"]["paired_columns"][column]:
mappings.append(config["secondary_row_map"])
for mapping in mappings:
speaker = geometry["output_ids"][cursor]
result[:, speaker] += matrix[np.asarray(mapping), column]
cursor += 1
floor_a, floor_b, height, additional = geometry["selectors"]
add_group("floor_a", floor_a)
cursor -= 2
add_group("floor_b", floor_b)
cursor -= 2
add_group("height", height)
if additional:
mid, upper, lower, zenith, _ = config["counts"]
result[mid:mid + upper] = 0
result[mid + upper + lower:mid + upper + lower + zenith] = 0
add_group("additional", additional)
return result
def isf_matrix(meta, layout, isf_idx):
"""Return the assembled ISF basis matrix ``(Nisf, output_count)``."""
return assemble_isf(meta, layout, isf_idx)
def isf_compensation(meta, layout, isf_idx, row, disabled=False):
"""Return the per-basis ISF compensation gain."""
if disabled:
return F(1)
config = meta["isf"]["configs"][isf_idx]
geom = meta["isf"]["layouts"][layout]
first = config["counts"][0]
value = F(geom["compensation_value"])
if row < first:
distance = F(min(row, first - row))
lower = max(F(F(first) * F(0.16666667)), F(1))
width = F(F(first) * F(0.11111111))
if distance <= lower:
fraction = F(0)
elif distance > F(lower + width):
fraction = F(1)
else:
fraction = F(F(distance - lower) / width)
value = F(fraction * value)
return exp2_approx(F(value * F(0.1660964)))
DIRECT_BED_LABEL = {1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 7,
9: 13, 10: 14, 11: 17, 12: 18, 13: 21, 14: 22,
15: 11, 16: 12, 17: 3}
A_WEIGHT = F(0.5011872053146362)
B_WEIGHT = F(0.7079457640647888)
C_WEIGHT = F(0.3548133969306946)
def bed_weight(label, mask, target_label):
"""Return the per-label bed redistribution weight."""
value = F(1 if target_label == DIRECT_BED_LABEL.get(label, 3) else 0)
if 9 <= label <= 14:
rows = {9: {13: 1, 15: B_WEIGHT, 17: A_WEIGHT},
10: {14: 1, 16: B_WEIGHT, 18: A_WEIGHT},
11: {13: A_WEIGHT, 15: B_WEIGHT, 17: 1,
19: B_WEIGHT, 21: A_WEIGHT},
12: {14: A_WEIGHT, 16: B_WEIGHT, 18: 1,
20: B_WEIGHT, 22: A_WEIGHT},
13: {17: A_WEIGHT, 19: B_WEIGHT, 21: 1},
14: {18: A_WEIGHT, 20: B_WEIGHT, 22: 1}}
return F(rows[label].get(target_label, value))
if mask & 0xC0:
rows = {5: {4: 1, 25: B_WEIGHT, 27: B_WEIGHT},
6: {5: 1, 26: B_WEIGHT, 28: B_WEIGHT},
7: {6: 1, 8: C_WEIGHT, 29: B_WEIGHT, 31: B_WEIGHT,
33: A_WEIGHT},
8: {7: 1, 8: C_WEIGHT, 30: B_WEIGHT, 32: B_WEIGHT,
34: A_WEIGHT}}
elif mask & 0x30:
rows = {5: {4: 1, 6: F(0.5370317697525024),
8: F(0.2884031534194946), 25: B_WEIGHT,
27: F(0.8128305077552795),
29: F(0.6606934666633606),
31: F(0.43651583790779114), 33: C_WEIGHT},
6: {5: 1, 7: F(0.5370317697525024),
8: F(0.2884031534194946), 26: B_WEIGHT,
28: F(0.8128305077552795),
30: F(0.6606934666633606),
32: F(0.43651583790779114), 34: C_WEIGHT}}
else:
return value
if label == 5 and not mask & 0x4000 and target_label == 11:
return A_WEIGHT
if label == 6 and not mask & 0x8000 and target_label == 12:
return A_WEIGHT
return F(rows.get(label, {}).get(target_label, value))
def bed_candidates(label, mask, output_labels):
"""Return the five height-candidate flags for one input bed label."""
available = [int(value in output_labels)
for value in (13, 15, 17, 19, 21)]
a, b, m, d, e = available
remaining = available.copy()
bits = (8, 10, 12) if label in (9, 11, 13) else (9, 11, 13)
front, middle, rear = [(mask >> bit) & 1 for bit in bits]
if front:
if a:
remaining[0] = 0
elif b:
remaining[1] = 0
if middle and m:
remaining[2] = 0
if rear:
if e:
remaining[4] = 0
elif d:
remaining[3] = 0
special = (not m and sum(available) == 2 and not front and not rear
and middle and (a or b) and (d or e))
if label in (9, 10):
return [a, b, remaining[2], 0, 0] if (a or b or remaining[2]) \
else [0] * 5
if label in (11, 12):
return [0] * 5 if special else [remaining[0], remaining[1], m,
remaining[3], remaining[4]]
if label in (13, 14):
return [0, 0, remaining[2], d, e] if (d or e or remaining[2]) \
else [0] * 5
return [0] * 5
def bed_distribution(layout_key, label, mask, distribute):
"""Return ``(route, weights)`` for one input bed label.
``route == -1`` means the returned weights are the target; otherwise
``route`` is the reference position used to synthesize a point source.
The slot map is derived from the speaker layout, not stored separately.
"""
layout = bed_output_description(get_speaker_layout(layout_key))
labels = layout["output_labels"]
base = np.zeros(len(labels), dtype=np.float32)
slot = layout["label_to_slot"][label - 1]
fallback = layout["direct_output_labels"][label - 1]
height = 9 <= label <= 14
if slot == -2 and (not height or not distribute):
return fallback, base
if slot == -1:
return -1, base
if slot >= 0:
base[slot] = F(1)
if not distribute:
return -1, base
if height:
candidate = bed_candidates(label, mask, labels)
if not any(candidate):
return fallback, base
side_labels = (13, 15, 17, 19, 21) if label % 2 \
else (14, 16, 18, 20, 22)
allowed = {value for value, active in zip(side_labels, candidate)
if active}
weights = np.array([bed_weight(label, mask, value)
if value in allowed else F(0)
for value in labels], dtype=np.float32)
else:
weights = np.array([bed_weight(label, mask, value)
for value in labels], dtype=np.float32)
if all(value == F(0) or value == F(1) for value in weights):
return -1, base
count = np.count_nonzero(weights)
if count == 1:
weights[weights != 0] = F(1)
elif count > 1:
weights = norm(weights)
else:
return fallback, base
return -1, weights
def bed_reference_position(layout_key, index):
"""Return the Q15 reference position used by the bed fallback path."""
return BED_REFERENCE_POSITIONS_Q15[int(index)]
def bed_compensation(layout_key, label, mode=0, bypass=False,
levels=(0, 0, 0)):
"""Return the bed path compensation gain for one input bed label."""
if label in (4, 17) or bypass or mode == 1:
return F(1)
layout = bed_output_description(get_speaker_layout(layout_key))
source = layout["direct_output_labels"][label - 1]
position = BED_REFERENCE_POSITIONS_Q15[source]
x, y, z = [F(value / 32768) for value in position]
if mode == 0:
y_term = F(1) if y >= F(0.6) else (
F(0) if y <= 0 else F(float(y) / 0.6))
z_term = F(1) if z >= 1 else (
F(0) if z <= F(0.2) else F(float(F(z - F(0.2))) / 0.8))
amount = max(F(0), min(F(y_term + z_term), F(1)))
value_db = F(amount * F(layout["automatic_attenuation_db"]))
else:
surround, height, centre = map(F, levels)
surround_factor = F(surround if y >= F(0.5)
else F(F(y * surround) * F(2)))
centre_position = max(F(abs(z) - F(0.05)), F(y - F(0.05)),
F(abs(F(x - F(0.5))) - F(0.05)))
centre_factor = F(F(1) - min(max(F(10) * centre_position, F(0)),
F(1)))
value_db = F(F(F(surround_factor * F(F(1) - z))
+ F(z * height)) + F(centre_factor * centre))
return exp2_approx(F(value_db * F(0.1660964)))
+27
View File
@@ -0,0 +1,27 @@
"""Static lookup data used by the OAMD metadata application path.
``metadata_params.json``
ISF base coefficients and layout descriptors, applied as lookup tables.
gain, size, priority, position, delta and refinement values are computed
in code; the size tables are generated from the region geometry at runtime,
and bed routing is derived from the speaker layouts.
The data file is parsed once per process.
"""
from __future__ import annotations
import json
from functools import lru_cache
from pathlib import Path
_DATA_DIR = Path(__file__).resolve().parent.parent / "data" / "oamd"
@lru_cache(maxsize=1)
def metadata_params():
"""Return the parsed ``metadata_params.json`` mapping."""
with (_DATA_DIR / "metadata_params.json").open(
"r", encoding="utf-8") as handle:
return json.load(handle)
+58 -29
View File
@@ -128,11 +128,11 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis0_groups': ((0, 1), (2, 3)),
'axis1_groups': (),
'mode': 2},
{'coordinates_q15': ((0, 32767, 0), (32767, 32767, 0)),
'speaker_ids': (2, 3),
'axis0_groups': ((0, 1),),
{'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (0, 32767, 0), (32767, 32767, 0)),
'speaker_ids': (0, 1, 2, 3),
'axis0_groups': ((0, 1), (2, 3)),
'axis1_groups': (),
'mode': 1},
'mode': 2},
{'coordinates_q15': ((0, 0, 0), (32767, 0, 0)),
'speaker_ids': (0, 1),
'axis0_groups': ((0, 1),),
@@ -152,22 +152,22 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'speaker_bitfield': 15,
'channels': ('L', 'R', 'C', 'LFE', 'Ls', 'Rs'),
'internal_to_standard': (0, 1, 2, 3, 4, 5),
'regions': ({'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 32767, 0), (32767, 32767, 0)),
'regions': ({'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 16384, 0), (32767, 16384, 0)),
'speaker_ids': (0, 1, 2, 4, 5),
'axis0_groups': ((0, 2, 1), (3, 4)),
'axis1_groups': (),
'mode': 2},
{'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 32767, 0), (32767, 32767, 0)),
{'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 16384, 0), (32767, 16384, 0)),
'speaker_ids': (0, 1, 2, 4, 5),
'axis0_groups': ((0, 2, 1), (3, 4)),
'axis1_groups': (),
'mode': 2},
{'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 32767, 0), (32767, 32767, 0)),
{'coordinates_q15': ((0, 0, 0), (32767, 0, 0), (16384, 0, 0), (0, 16384, 0), (32767, 16384, 0)),
'speaker_ids': (0, 1, 2, 4, 5),
'axis0_groups': ((0, 2, 1), (3, 4)),
'axis1_groups': (),
'mode': 2},
{'coordinates_q15': ((16384, 0, 0), (0, 32767, 0), (32767, 32767, 0)),
{'coordinates_q15': ((16384, 0, 0), (0, 16384, 0), (32767, 16384, 0)),
'speaker_ids': (2, 4, 5),
'axis0_groups': ((0,), (1, 2)),
'axis1_groups': (),
@@ -177,7 +177,7 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis0_groups': ((0, 2, 1),),
'axis1_groups': (),
'mode': 1},
{'coordinates_q15': ((0, 32767, 0), (32767, 32767, 0)),
{'coordinates_q15': ((0, 16384, 0), (32767, 16384, 0)),
'speaker_ids': (4, 5),
'axis0_groups': ((0, 1),),
'axis1_groups': (),
@@ -239,8 +239,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'regions': ({'coordinates_q15': ((0, 0, 0),
(32767, 0, 0),
(16384, 0, 0),
(0, 32767, 0),
(32767, 32767, 0),
(0, 16384, 0),
(32767, 16384, 0),
(7928, 16384, 32767),
(24840, 16384, 32767)),
'speaker_ids': (0, 1, 2, 4, 5, 6, 7),
@@ -250,8 +250,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
{'coordinates_q15': ((0, 0, 0),
(32767, 0, 0),
(16384, 0, 0),
(0, 32767, 0),
(32767, 32767, 0),
(0, 16384, 0),
(32767, 16384, 0),
(7928, 16384, 32767),
(24840, 16384, 32767)),
'speaker_ids': (0, 1, 2, 4, 5, 6, 7),
@@ -261,8 +261,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
{'coordinates_q15': ((0, 0, 0),
(32767, 0, 0),
(16384, 0, 0),
(0, 32767, 0),
(32767, 32767, 0),
(0, 16384, 0),
(32767, 16384, 0),
(7928, 16384, 32767),
(24840, 16384, 32767)),
'speaker_ids': (0, 1, 2, 4, 5, 6, 7),
@@ -270,8 +270,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis1_groups': ((5, 6),),
'mode': 3},
{'coordinates_q15': ((16384, 0, 0),
(0, 32767, 0),
(32767, 32767, 0),
(0, 16384, 0),
(32767, 16384, 0),
(7928, 16384, 32767),
(24840, 16384, 32767)),
'speaker_ids': (2, 4, 5, 6, 7),
@@ -287,8 +287,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis0_groups': ((0, 2, 1),),
'axis1_groups': ((3, 4),),
'mode': 3},
{'coordinates_q15': ((0, 32767, 0),
(32767, 32767, 0),
{'coordinates_q15': ((0, 16384, 0),
(32767, 16384, 0),
(7928, 16384, 32767),
(24840, 16384, 32767)),
'speaker_ids': (4, 5, 6, 7),
@@ -307,8 +307,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'regions': ({'coordinates_q15': ((0, 0, 0),
(32767, 0, 0),
(16384, 0, 0),
(0, 32767, 0),
(32767, 32767, 0),
(0, 16384, 0),
(32767, 16384, 0),
(7928, 7928, 32767),
(24840, 7928, 32767),
(7928, 24840, 32767),
@@ -320,8 +320,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
{'coordinates_q15': ((0, 0, 0),
(32767, 0, 0),
(16384, 0, 0),
(0, 32767, 0),
(32767, 32767, 0),
(0, 16384, 0),
(32767, 16384, 0),
(7928, 7928, 32767),
(24840, 7928, 32767),
(7928, 24840, 32767),
@@ -333,8 +333,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
{'coordinates_q15': ((0, 0, 0),
(32767, 0, 0),
(16384, 0, 0),
(0, 32767, 0),
(32767, 32767, 0),
(0, 16384, 0),
(32767, 16384, 0),
(7928, 7928, 32767),
(24840, 7928, 32767),
(7928, 24840, 32767),
@@ -344,8 +344,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis1_groups': ((5, 6), (7, 8)),
'mode': 3},
{'coordinates_q15': ((16384, 0, 0),
(0, 32767, 0),
(32767, 32767, 0),
(0, 16384, 0),
(32767, 16384, 0),
(7928, 7928, 32767),
(24840, 7928, 32767),
(7928, 24840, 32767),
@@ -365,8 +365,8 @@ _RAW_LAYOUTS = {'20': {'out_ch_config': 0,
'axis0_groups': ((0, 2, 1),),
'axis1_groups': ((3, 4), (5, 6)),
'mode': 3},
{'coordinates_q15': ((0, 32767, 0),
(32767, 32767, 0),
{'coordinates_q15': ((0, 16384, 0),
(32767, 16384, 0),
(7928, 7928, 32767),
(24840, 7928, 32767),
(7928, 24840, 32767),
@@ -774,6 +774,34 @@ def _make_layout(name: str, item: dict) -> SpeakerLayout:
SPEAKER_LAYOUTS = {name: _make_layout(name, item) for name, item in _RAW_LAYOUTS.items()}
def _make_six_zero():
"""6.0: L/R, side pair and rear pair without centre or LFE."""
base = SPEAKER_LAYOUTS["71"]
channels = ("L", "R", "Ls", "Rs", "Lrs", "Rrs")
source_ids = (0, 1, 4, 5, 6, 7)
remap = {old: new for new, old in enumerate(source_ids)}
regions = []
for region in base.regions:
kept = [index for index, speaker in enumerate(region.speaker_ids)
if speaker in remap]
if not kept:
continue
local = {old: new for new, old in enumerate(kept)}
def groups(items):
return tuple(tuple(local[index] for index in group if index in local)
for group in items if any(index in local for index in group))
regions.append(RegionGeometry(
tuple(region.coordinates_q15[index] for index in kept),
tuple(remap[region.speaker_ids[index]] for index in kept),
groups(region.axis0_groups), groups(region.axis1_groups),
region.mode))
return SpeakerLayout("60", 255, 25, channels,
tuple(range(6)), tuple(regions))
SPEAKER_LAYOUTS["60"] = _make_six_zero()
SPEAKER_LAYOUT_ALIASES = {
"2.0": "20",
"3.1": "31",
@@ -786,6 +814,7 @@ SPEAKER_LAYOUT_ALIASES = {
"7.1.4": "714",
"9.1.4": "914",
"9.1.6": "916",
"6.0": "60",
}
SPEAKER_LAYOUT_CHOICES = tuple(SPEAKER_LAYOUT_ALIASES)
SPEAKER_LAYOUT_DISPLAY_NAMES = {value: key for key, value in SPEAKER_LAYOUT_ALIASES.items()}
+497 -39
View File
@@ -13,7 +13,10 @@ from typing import Iterable
import numpy as np
import oamd_math
from oamd_bits import JocFieldState, frame_update, parse_trim
from oamd_params import metadata_params
from variant_error import UnsupportedVariantError
from speaker_layouts import RegionGeometry, SpeakerLayout, get_speaker_layout
from trim_balance import (SpeakerRenderParameters, apply_balance,
explicit_position_gain, parameters_from_oamd)
@@ -21,6 +24,57 @@ from trim_balance import (SpeakerRenderParameters, apply_balance,
Q15_SCALE = 32768.0
Q15_MAX = 32767.0 / Q15_SCALE
GAIN_SNAP_THRESHOLD = 1.0e-4
ZONE_TO_REGION = (0, 1, 2, 3, 4, 5, 6, 0)
LFE_BED_LABELS = ("RC_LFE", "RC_LFE2")
BED_LABEL_INDEX = {label: index + 1 for index, label in
enumerate(("RC_L", "RC_R", "RC_C", "RC_LFE",
"RC_LS", "RC_RS", "RC_LB", "RC_RB",
"RC_TFL", "RC_TFR", "RC_TSL", "RC_TSR",
"RC_TBL", "RC_TBR", "RC_LW", "RC_RW",
"RC_LFE2"))}
def _stable_norm(vector):
"""Reference vector normalization: tiny-norm floor and unit clamp."""
values = np.asarray(vector, dtype=np.float64)
total = float(np.dot(values, values))
if total == 0.0:
return np.zeros_like(values)
result = values / max(math.sqrt(total), 1.0e-5)
return np.minimum(result, 1.0)
def _region_vector(layout, region_index, u, floor_v, top_v, w,
enable_height):
"""Uncompensated point-source vector in ``layout`` internal order."""
region = layout.regions[int(region_index)]
coordinates = _coordinates(region)
floor = _plane_gains(region, coordinates, region.axis0_groups, u,
floor_v, region.mode)
gains = floor
if region.mode == 3:
top = _plane_gains(region, coordinates, region.axis1_groups, u,
top_v, 3)
height = min(max(float(w) if enable_height else 0.0, 0.0),
Q15_MAX)
if height >= Q15_MAX:
gains = top
elif height > 0.0:
floor_weight, height_weight = equal_power_pair(height)
gains = floor * floor_weight + top * height_weight
vector = np.zeros(layout.channel_count, dtype=np.float64)
for value, speaker in zip(gains, region.speaker_ids):
vector[speaker] = value
return vector
def _fold_vector(calc, final, warp_mode, vector):
"""Apply the existing warp 2/3 surround fold to a calculation vector."""
if calc is final:
return vector
return (_surround_fold_matrix(calc.speaker_bitfield,
final.speaker_bitfield, warp_mode)
@ vector)
def bed_object_targets(bed_channels) -> dict[int, tuple[float, float, float]]:
@@ -205,6 +259,8 @@ def _surround_fold_layout(target: SpeakerLayout) -> SpeakerLayout:
expanded = {"51": "71", "512": "712", "514": "714"}
if target.name in expanded:
return get_speaker_layout(expanded[target.name])
if target.name == "60":
return target
if target.name != "40":
raise ValueError(f"unsupported surround fold layout: {target.name}")
# 4.0 expands to six full-band speakers without adding centre or LFE.
@@ -299,10 +355,18 @@ def ramp_block_count(duration_samples: int, block_size: int = 32,
class PythonSpeakerRenderer:
"""Stateful float64 speaker renderer."""
"""Stateful float64 speaker renderer.
Two input shapes are accepted. Payloads that expand to block-level object
state (``frame_update`` with ``object_blocks``) use the metadata path:
object gain/inactive, region/elevation, size/snap/divergence/refinement,
priority budget, ISF and bed distribution. Older payload dictionaries keep
the previous point-position path so existing callers and tests still work.
"""
def __init__(self, layout: str | SpeakerLayout, block_size: int = 32,
bed_channels=None, trim_meta=None, *, parameters=None):
bed_channels=None, trim_meta=None, *, parameters=None,
size_budget: int = 0):
self.layout = get_speaker_layout(layout) if isinstance(layout, str) else layout
self.block_size = int(block_size)
if self.block_size <= 0:
@@ -312,6 +376,11 @@ class PythonSpeakerRenderer:
raise ValueError("pass normalized parameters or OAMD trim_meta, not both")
self._parameters_override = parameters
self._initial_parameters = parameters or parameters_from_oamd(trim_meta, self.layout)
self._size_budget = int(size_budget)
if self._size_budget < 0:
raise ValueError("size_budget must be non-negative")
self._raw_trim_mode = (trim_meta or {}).get("global_trim_mode", 0)
self._meta_params = metadata_params()
self.reset()
def reset(self):
@@ -323,16 +392,341 @@ class PythonSpeakerRenderer:
self._step = np.zeros_like(self._current)
self._remaining = np.zeros((15, channels), dtype=np.int32)
self._window = np.arange(self.block_size, dtype=np.float64) / float(self.block_size)
self._meta_mode = False
self._meta_ready = False
self._n_meta = 0
self._meta_slots: list[int | None] = []
self._meta_labels: list[str | None] = []
self._mc = None
self._mt = None
self._ms = None
self._mr = None
def _apply_payload(self, payload):
update = frame_update(payload)
if self._parameters_override is None:
# -------------------------------------------------------------- metadata state
def _update_trim(self, payload, update=None):
trim = update.get("trim") if update is not None else None
if trim is None and update is None:
trim = parse_trim(payload)
if trim is not None:
self.parameters = parameters_from_oamd(trim, self.layout)
if trim is not None and self._parameters_override is None:
self.parameters = parameters_from_oamd(trim, self.layout)
if isinstance(trim, dict) and "global_trim_mode" in trim:
self._raw_trim_mode = trim["global_trim_mode"]
def _init_meta(self, update):
self._isf_cache = {}
program = update["program"]
kinds = update["kinds"]
count = int(update["object_count"])
labels = [label for group in program["bed_channels"] for label in group]
num_bed = int(program["num_bed_objects"])
if len(labels) != num_bed:
if program["dynamic_object_only"]:
labels = ["RC_LFE"] if program["lfe_present"] else []
if len(labels) != num_bed:
raise UnsupportedVariantError(
"speaker", "bed_declaration_mismatch",
"OAMD bed 声明数量与节目分区不一致",
details={"declared": len(labels), "num_bed_objects": num_bed})
slots = []
slot = 1
for object_index, kind in enumerate(kinds):
label = labels[object_index] if object_index < len(labels) else None
if kind == "bed" and label in LFE_BED_LABELS:
slots.append(0)
else:
if slot > 15:
raise UnsupportedVariantError(
"speaker", "object_slot_overflow",
"元数据对象数量超出 15 个 JOC 对象槽",
details={"object_count": count})
slots.append(slot)
slot += 1
if not program["dynamic_object_only"]:
declared = program["num_dynamic_objects"]
if (declared is not None
and num_bed + int(program["num_isf_objects"]) + int(declared)
!= count):
raise UnsupportedVariantError(
"speaker", "program_object_count_mismatch",
"OAMD 节目声明的对象分区与对象数不一致",
details={"object_count": count,
"num_bed_objects": num_bed,
"num_isf_objects": program["num_isf_objects"],
"num_dynamic_objects": declared})
if program["num_isf_objects"]:
isf_idx = program["isf_idx"]
config = (self._meta_params["isf"]["configs"][isf_idx]
if isf_idx is not None and isf_idx < 6 else None)
if config is None or config["counts"][4] != program["num_isf_objects"]:
raise UnsupportedVariantError(
"speaker", "isf_configuration_mismatch",
"ISF 配置数量与节目声明不一致",
details={"isf_idx": isf_idx,
"num_isf_objects": program["num_isf_objects"]})
if count != self._n_meta or slots != self._meta_slots:
channels = self.layout.channel_count
self._n_meta = count
self._meta_slots = slots
self._mc = np.zeros((count, channels), dtype=np.float64)
self._mt = np.zeros((count, channels), dtype=np.float64)
self._ms = np.zeros((count, channels), dtype=np.float64)
self._mr = np.zeros((count, channels), dtype=np.int32)
self._meta_labels = [labels[object_index]
if object_index < len(labels) else None
for object_index in range(count)]
self._meta_mode = True
def _block_targets(self, update, block_index):
count = int(update["object_count"])
channels = self.layout.channel_count
targets = np.zeros((count, channels), dtype=np.float64)
blocks = update["object_blocks"]
kinds = update["kinds"]
zero_size = set()
size_objects = []
for object_index in range(count):
state = blocks[object_index][block_index]
if (kinds[object_index] == "dynamic" and not state["not_active"]
and state["size_mode"] in (1, 2)
and not state["snap"] and state["divergence_q"] == 0):
size_objects.append(object_index)
if 0 < self._size_budget < len(size_objects):
ranked = sorted(
size_objects,
key=lambda index: (-((int(blocks[index][block_index]["priority_q"])
+ 512) >> 10), index))
zero_size = set(ranked[self._size_budget:])
for object_index in range(count):
state = blocks[object_index][block_index]
if state["not_active"]:
continue
spatial = self._object_spatial(update, object_index, block_index,
object_index in zero_size)
gain = float(state["gain_q"]) / 4096.0
targets[object_index] = spatial * gain
return targets
def _object_spatial(self, update, object_index, block_index, force_zero_size):
kind = update["kinds"][object_index]
if kind == "dynamic":
return self._dynamic_spatial(update, object_index, block_index,
force_zero_size)
if kind == "bed":
return self._bed_spatial(update, object_index)
if kind == "isf":
return self._isf_spatial(update, object_index)
return np.zeros(self.layout.channel_count, dtype=np.float64)
# -------------------------------------------------------------- dynamic path
def _dynamic_spatial(self, update, object_index, block_index,
force_zero_size):
state = update["object_blocks"][object_index][block_index]
final = self.layout
warp = self.parameters.warp_mode
calc = _surround_fold_layout(final) if warp in (2, 3) else final
region_index = ZONE_TO_REGION[int(state["zone_idx"]) & 7]
u = float(state["qx"]) / Q15_SCALE
v = float(state["qy"]) / Q15_SCALE
w = max(float(state["qz"]) / Q15_SCALE, 0.0)
enable_height = bool(state["enable_elevation"])
if not enable_height:
w = 0.0
bypass = self.parameters.bypasses(object_index)
floor_v = min(max(math.ldexp(v, floor_y_exponent(
calc.speaker_bitfield, warp != 0)), 0.0), 1.0)
top_v = min(max(v, 0.0), 1.0)
trim_v = v
if self.parameters.trim_mode == "explicit" and not bypass:
listener = self.parameters.listener_balance
overhead = self.parameters.overhead_balance
floor_v = apply_balance(floor_v, w, listener, overhead)
top_v = apply_balance(top_v, w, listener, overhead)
trim_v = apply_balance(v, w, listener, overhead)
region = calc.regions[region_index]
coordinates = _coordinates(region)
if state["snap"]:
point = _region_vector(calc, region_index, u, floor_v, top_v, w,
enable_height)
best = -1.0
chosen = 0
for index, speaker in enumerate(region.speaker_ids):
value = float(point[speaker])
if value > best:
best = value
chosen = index
speaker_u, speaker_v, speaker_w = coordinates[chosen]
scale = 1.0
distance = ((u - speaker_u) ** 2
+ (min(v, scale) - scale * speaker_v) ** 2
+ (w - speaker_w) ** 2)
if distance < 0.04:
vector = np.zeros(calc.channel_count, dtype=np.float64)
vector[region.speaker_ids[chosen]] = 1.0
comp = (float(speaker_u), float(speaker_v), float(speaker_w))
else:
vector = point
comp = (u, trim_v, w)
elif state["divergence_q"] > 0:
spread = min(float(state["divergence_q"]) / Q15_SCALE, u, 1.0 - u)
left = _region_vector(calc, region_index, u - spread, floor_v,
top_v, w, enable_height)
right = _region_vector(calc, region_index, u + spread, floor_v,
top_v, w, enable_height)
vector = _stable_norm(left + right)
comp = (u, trim_v, w)
elif state["size_mode"] in (1, 2) and not force_zero_size:
size_q = state["size_q"]
if size_q[0] == size_q[1] == size_q[2]:
q_size = int(size_q[0])
else:
q_size = (int(size_q[0]) + int(size_q[1]) + int(size_q[2])) // 3
size = q_size / Q15_SCALE
t = oamd_math.size_curve_value(size)
point = _region_vector(calc, region_index, u, floor_v, top_v, w,
enable_height)
if t < 0.000025:
vector = point
else:
query = (u, (1.0 - w) * floor_v + w * top_v, w)
spread = np.asarray(oamd_math.size_targets(
self._meta_params, calc, region_index, query, size,
height=enable_height), dtype=np.float64)
if t > 0.05:
vector = spread
else:
angle = 20.0 * t
if angle >= 0.99996948:
cosine, sine = 0.0, 1.0
else:
cosine = math.cos(angle * math.pi / 2.0)
sine = math.sin(angle * math.pi / 2.0)
vector = _stable_norm(0.5 * cosine * point
+ 0.5 * sine * spread)
comp = (u, trim_v, w)
else:
vector = _region_vector(calc, region_index, u, floor_v, top_v, w,
enable_height)
comp = (u, trim_v, w)
compensation = 1.0
if not bypass:
if self.parameters.trim_mode == "automatic":
compensation = position_gain(final.speaker_bitfield,
comp[1], comp[2])
elif self.parameters.trim_mode == "explicit":
compensation = explicit_position_gain(self.parameters,
comp[0], comp[1], comp[2])
return _fold_vector(calc, final, warp, vector) * compensation
# -------------------------------------------------------------- bed path
def _bed_spatial(self, update, object_index):
final = self.layout
warp = self.parameters.warp_mode
calc = _surround_fold_layout(final) if warp in (2, 3) else final
label = self._meta_labels[object_index]
bed_index = BED_LABEL_INDEX.get(label)
if bed_index is None:
return np.zeros(final.channel_count, dtype=np.float64)
program = update["program"]
route, weights = oamd_math.bed_distribution(
calc.name, bed_index, int(program["bed_mask"]),
bool(program["b_bed_chan_distribute"]))
bypass = self.parameters.bypasses(object_index)
mode = {"automatic": 0, "disabled": 1, "explicit": 2}[
self.parameters.trim_mode]
if route != -1:
position = oamd_math.bed_reference_position(calc.name, route)
qx, qy, qz = (int(value) for value in position)
if qz > 0:
qx = 32767 if qx > 16384 else 0
u, v = qx / Q15_SCALE, qy / Q15_SCALE
w = max(qz, 0) / Q15_SCALE
floor_v = min(max(math.ldexp(v, floor_y_exponent(
calc.speaker_bitfield, warp != 0)), 0.0), 1.0)
top_v = min(max(v, 0.0), 1.0)
vector = _region_vector(calc, 0, u, floor_v, top_v, w, True)
compensation = 1.0
if not bypass and self.parameters.trim_mode != "disabled":
if self.parameters.trim_mode == "automatic":
compensation = position_gain(final.speaker_bitfield, v, w)
else:
compensation = explicit_position_gain(
self.parameters, u, v, w)
else:
vector = np.asarray(weights, dtype=np.float64).copy()
levels = (self.parameters.surround_db,
self.parameters.height_db,
self.parameters.centre_db)
compensation = float(oamd_math.bed_compensation(
final.name, bed_index, mode, bypass, levels))
return _fold_vector(calc, final, warp, vector) * compensation
# -------------------------------------------------------------- ISF path
def _isf_spatial(self, update, object_index):
program = update["program"]
isf_idx = int(program["isf_idx"])
row = object_index - int(program["num_bed_objects"])
final = self.layout
warp = self.parameters.warp_mode
calc = _surround_fold_layout(final) if warp in (2, 3) else final
key = (calc.name, isf_idx)
matrix = self._isf_cache.get(key)
if matrix is None:
matrix = oamd_math.isf_matrix(self._meta_params, calc.name, isf_idx)
self._isf_cache[key] = matrix
vector = np.asarray(matrix[row], dtype=np.float64)
disabled = (int(self._raw_trim_mode) == 1
or self.parameters.bypasses(object_index))
compensation = float(oamd_math.isf_compensation(
self._meta_params, final.name, isf_idx, row, disabled))
return _fold_vector(calc, final, warp, vector) * compensation
# -------------------------------------------------------------- scheduling
def _apply_meta_targets(self, targets, ramp_samples):
channels = self.layout.channel_count
if self._mc is None or self._mc.shape != (len(targets), channels):
self._mc = np.zeros((len(targets), channels), dtype=np.float64)
self._mt = np.zeros_like(self._mc)
self._ms = np.zeros_like(self._mc)
self._mr = np.zeros((len(targets), channels), dtype=np.int32)
difference = np.asarray(targets, dtype=np.float64) - self._mc
significant = np.abs(difference) >= GAIN_SNAP_THRESHOLD
self._mt[...] = targets
self._mc[~significant] = self._mt[~significant]
self._ms[~significant] = 0.0
self._mr[~significant] = 0
blocks = ramp_block_count(int(ramp_samples), self.block_size)
if blocks:
self._ms[significant] = difference[significant] / float(blocks)
self._mr[significant] = blocks
else:
self._mc[significant] = self._mt[significant]
self._ms[significant] = 0.0
self._mr[significant] = 0
self._meta_ready = True
def _schedule_payload(self, update, event_sample):
if "object_blocks" not in update or not update["object_count"]:
return False
if update.get("trim") is not None:
self._update_trim(None, update)
self._init_meta(update)
for block_index, block in enumerate(update["blocks"]):
sample = (int(event_sample) + int(update["sample_offset"])
+ int(block["block_offset_samples"]))
aligned = align_metadata_sample(sample, self.block_size)
targets = self._block_targets(update, block_index)
self._pending.setdefault(aligned // self.block_size, []).append(
(targets, block["ramp_duration_samples"]))
return True
# -------------------------------------------------------------- legacy path
def _apply_legacy(self, update):
trim = parse_trim(self._current_payload)
if trim is not None and self._parameters_override is None:
self.parameters = parameters_from_oamd(trim, self.layout)
self._state.apply(update["values"])
new_target = _all_object_targets(self.layout, self._state, self.bed_targets,
self.parameters)
new_target = _all_object_targets(self.layout, self._state,
self.bed_targets, self.parameters)
blocks = ramp_block_count(update["ramp_duration_samples"], self.block_size)
difference = new_target - self._current
significant = np.abs(difference) >= GAIN_SNAP_THRESHOLD
@@ -348,6 +742,62 @@ class PythonSpeakerRenderer:
self._step[significant] = 0.0
self._remaining[significant] = 0
def _apply_payload(self, payload):
self._current_payload = payload
update = frame_update(payload)
if "object_blocks" in update and update["object_count"]:
self._update_trim(payload, update)
self._init_meta(update)
targets = self._block_targets(update, 0)
self._apply_meta_targets(targets,
update["blocks"][0]["ramp_duration_samples"])
else:
self._apply_legacy(update)
# -------------------------------------------------------------- process
def _render_meta_block(self, source, start, stop, out_block):
for object_index in range(self._n_meta):
slot = self._meta_slots[object_index]
if slot is None:
continue
active = self._mr[object_index] > 0
gains = np.broadcast_to(self._mt[object_index],
(self.block_size, self.layout.channel_count)).copy()
if np.any(active):
gains[:, active] = (
self._mc[object_index, active][None, :]
+ self._window[:, None] * self._ms[object_index, active][None, :]
)
out_block += source[start:stop, slot, None] * gains
if np.any(active):
self._mc[object_index, active] += self._ms[object_index, active]
self._mr[object_index, active] -= 1
finished = self._mr[object_index] == 0
self._mc[object_index, finished] = self._mt[object_index, finished]
def _render_legacy_block(self, source, start, stop, out_block):
channels = self.layout.channel_count
if "LFE" in self.layout.channels:
out_block[:, 3] = source[start:stop, 0].astype(np.float64, copy=False)
for object_index in range(15):
active = self._remaining[object_index] > 0
gains = np.broadcast_to(self._target[object_index],
(self.block_size, channels)).copy()
if np.any(active):
gains[:, active] = (
self._current[object_index, active][None, :]
+ self._window[:, None] * self._step[object_index, active][None, :]
)
out_block += (
source[start:stop, object_index + 1, None].astype(np.float64, copy=False)
* gains
)
if np.any(active):
self._current[object_index, active] += self._step[object_index, active]
self._remaining[object_index, active] -= 1
finished = self._remaining[object_index] == 0
self._current[object_index, finished] = self._target[object_index, finished]
def process(self, objects16: np.ndarray, events=(), *, standard_order: bool = True,
output: np.ndarray | None = None) -> np.ndarray:
source = np.asarray(objects16)
@@ -363,44 +813,48 @@ class PythonSpeakerRenderer:
raise ValueError((output.shape, output.dtype))
else:
output[...] = 0.0
if "LFE" in self.layout.channels:
output[:, 3] = source[:, 0].astype(np.float64, copy=False)
events_by_block: dict[int, list[bytes]] = defaultdict(list)
events_by_block: dict[int, list[tuple[int, bytes]]] = defaultdict(list)
for sample_offset, payload in events:
if not 0 <= int(sample_offset) <= total_samples:
raise ValueError(f"metadata offset outside process call: {sample_offset}")
block = align_metadata_sample(sample_offset, self.block_size) // self.block_size
events_by_block[block].append(bytes(payload))
events_by_block[block].append((align_metadata_sample(
sample_offset, self.block_size), bytes(payload)))
self._pending: dict[int, list] = {}
total_blocks = total_samples // self.block_size
for block in range(total_blocks):
for payload in events_by_block.get(block, ()):
self._apply_payload(payload)
for event_sample, payload in events_by_block.get(block, ()):
update = frame_update(payload)
if "object_blocks" in update and update["object_count"]:
self._schedule_payload(update, event_sample)
else:
self._current_payload = payload
self._apply_legacy(update)
for targets, ramp_samples in self._pending.pop(block, ()):
self._apply_meta_targets(targets, ramp_samples)
start = block * self.block_size
stop = start + self.block_size
out_block = output[start:stop]
for object_index in range(15):
active = self._remaining[object_index] > 0
gains = np.broadcast_to(self._target[object_index],
(self.block_size, channels)).copy()
if np.any(active):
gains[:, active] = (
self._current[object_index, active][None, :]
+ self._window[:, None] * self._step[object_index, active][None, :]
)
out_block += (
source[start:stop, object_index + 1, None].astype(np.float64, copy=False)
* gains
)
if np.any(active):
self._current[object_index, active] += self._step[object_index, active]
self._remaining[object_index, active] -= 1
finished = self._remaining[object_index] == 0
self._current[object_index, finished] = self._target[object_index, finished]
if self._meta_mode and self._meta_ready:
self._render_meta_block(source, start, stop, out_block)
else:
self._render_legacy_block(source, start, stop, out_block)
for payload in events_by_block.get(total_blocks, ()):
self._apply_payload(payload)
for event_sample, payload in events_by_block.get(total_blocks, ()):
update = frame_update(payload)
if "object_blocks" in update and update["object_count"]:
self._schedule_payload(update, event_sample)
else:
self._current_payload = payload
self._apply_legacy(update)
for targets, ramp_samples in self._pending.pop(total_blocks, ()):
self._apply_meta_targets(targets, ramp_samples)
for block_index in sorted(self._pending):
for targets, ramp_samples in self._pending[block_index]:
self._apply_meta_targets(targets, ramp_samples)
self._pending.clear()
if standard_order:
return output[:, list(self.layout.internal_to_standard)]
return output
@@ -416,11 +870,14 @@ class PythonSpeakerRenderer:
def render_objects16(objects16: np.ndarray, payload_events: Iterable[tuple[int, bytes]],
layout: str | SpeakerLayout, *, block_size: int = 32,
standard_order: bool = True, output: np.ndarray | None = None,
trim_meta=None, parameters=None) -> np.ndarray:
trim_meta=None, parameters=None, size_budget: int = 0) -> np.ndarray:
"""Render a complete interleaved LFE + 15 object stream."""
renderer = PythonSpeakerRenderer(layout, block_size=block_size,
trim_meta=trim_meta, parameters=parameters)
return renderer.process(objects16, payload_events, standard_order=standard_order, output=output)
trim_meta=trim_meta, parameters=parameters,
size_budget=size_budget)
return renderer.process(objects16, payload_events, standard_order=standard_order,
output=output)
def payload_events_from_index(index, frame_count: int, *, frame_samples: int = 1536,
payload_id: int = 11, outer_offset_samples: int = 1473):
@@ -429,6 +886,7 @@ def payload_events_from_index(index, frame_count: int, *, frame_samples: int = 1
if payload_id in subpayloads:
timing = frame_update(subpayloads[payload_id])
yield (
frame * frame_samples + outer_offset_samples + timing["block_offset_samples"],
frame * frame_samples + outer_offset_samples
+ int(timing["sample_offset"]),
subpayloads[payload_id],
)