Read the OAMD bed layout and place bed objects at their fixed positions

This commit is contained in:
TheM14 committed 2026-10-30 03:59:52 +08:00
1 parent aa1b43e8fa
commit 5c4f876411
11 files changed
+373 -78

No files matched your search

+3
View File
@@ -13,6 +13,9 @@ This is research code, not a complete, standards-compliant, or production-grade
- Scan common contiguous EMDF containers in E-AC-3 sync frames.
- Parse ID14 dense / sparse JOC parameters, Huffman data, differential matrices, and `joc_clipgain`.
- Parse ID11 OAMD position updates and build object trajectories.
- Read the bed layout the OAMD declares (5.1, 7.1, 7.1.2, 7.1.4, 9.1.4, 9.1.6, ...): for
bed-only content the 15 object slots are the bed channels, rendered at that layout's
fixed bed positions.
- Reconstruct LFE plus 15 object channels through analysis QMF, parameter interpolation, the object matrix, and inverse QMF.
- Write a 25-channel ADM BWF: a 10-channel 7.1.2 bed (silent except for LFE) plus 15 objects.
- Render directly to `2.0`, `3.1`, `4.0`, `5.1`, `7.1`, `5.1.2`, `5.1.4`, `7.1.2`, `7.1.4`, `9.1.4`
+2
View File
@@ -13,6 +13,8 @@
- 扫描 E-AC-3 同步帧中的常见连续 EMDF 容器;
- 解析 ID14 dense / sparse JOC 参数、Huffman 数据、差分矩阵与 `joc_clipgain`;
- 解析 ID11 OAMD 位置更新并生成对象轨迹;
- 读取 OAMD 声明的 bed 布局(5.1、7.1、7.1.2、7.1.4、9.1.4、9.1.6 等):bed-only 内容的
15 路对象就是 bed 声道,按布局的固定方位渲染;
- 通过 analysis QMF、参数插值、对象矩阵和 inverse QMF 重建 LFE + 15 路对象 PCM;
- 输出 25 声道 ADM BWF:10 声道 7.1.2 bed(除 LFE 外静音)+ 15 个对象;
- 直接渲染 `2.0`、`3.1`、`4.0`、`5.1`、`7.1`、`5.1.2`、`5.1.4`、`7.1.2`、`7.1.4`、`9.1.4`、
+13 -4
View File
@@ -23,8 +23,8 @@ import numpy as np
import adm_assemble
import adm_atmos
from adm_validate import validate
from bed_layout import (BED_EXTRA_SLOTS_BY_CONFIG, bed_order, extra_pair,
joc_input_indices)
from bed_layout import (BED_CHANNEL_NAMES, BED_EXTRA_SLOTS_BY_CONFIG, bed_order,
extra_pair, joc_input_indices)
from emdf import expand_chanmap
from metadata import DirectPayloadIndex, PayloadIndex, write_summary
import oamd_tracks
@@ -738,11 +738,14 @@ def main(argv=None):
+ json.dumps(entry, ensure_ascii=False, separators=(",", ":")),
file=sys.stderr, flush=True)
core_channels = getattr(index, "n_channels", None)
bed_layout_info = getattr(index, "bed_layout", None) or {}
if core_channels is not None:
print(f"[joc] downmix_config_idx={index.config_idx} "
f"core_channels={core_channels} "
f"bed_channels={bed_channel_count(index)} "
f"extra_pair_slots={bed_layout_plan(index)['extra_pair_slots']}",
f"extra_pair_slots={bed_layout_plan(index)['extra_pair_slots']} "
f"bed_layout={bed_layout_info.get('name') or '-'} "
f"bed_object_channels={len(index.bed_channels)}",
flush=True)
bed_path = timed_call(
timings, "decode_core", decode_core,
@@ -765,7 +768,8 @@ def main(argv=None):
timings["create_binaural_renderer"] = 0.0
layout = get_speaker_layout(args.speaker_layout)
speaker_name = speaker_layout_display_name(layout)
speaker_decoder, speaker_backend_info = create_speaker_renderer(layout)
speaker_decoder, speaker_backend_info = create_speaker_renderer(
layout, bed_channels=getattr(index, "bed_channels", ()))
print(f"[speaker] layout={speaker_name} backend={speaker_backend_info['name']} "
f"channels={layout.channel_count}", flush=True)
spool = SpeakerPcmSpool(
@@ -806,6 +810,7 @@ def main(argv=None):
"tail_seconds": args.binaural_tail_seconds,
"output_gain": gain_float64,
"chunk_frames": args.binaural_chunk_frames,
"bed_channels": getattr(index, "bed_channels", ()),
}
if hrtf_source["kind"] == "sofa":
binaural_decoder = timed_call(
@@ -948,6 +953,10 @@ def main(argv=None):
"joc_downmix_config_idx": getattr(index, "config_idx", None),
"joc_core_channels": getattr(index, "n_channels", None),
"joc_extra_pair_slots": bed_layout_plan(index)["extra_pair_slots"],
"joc_bed_layout": (getattr(index, "bed_layout", None) or {}).get("name"),
"joc_bed_channels": [
BED_CHANNEL_NAMES.get(label, label)
for label in getattr(index, "bed_channels", ())],
"diagnostics": getattr(index, "diagnostics", None),
"metadata_cache": str(metadata_cache_dir) if metadata_cache_dir is not None else None,
"frames": frame_count,
+145
View File
@@ -64,6 +64,151 @@ def extra_pair(core_locations, chanmap):
return tuple(position for position in expand_chanmap(chanmap) if position not in core)
# OAMD §5.6.1.1.4 Table 12 的标准 bed 声道(LSB 在前,与 oamd_bits.BED_CHANNEL_LABELS 同序)。
# 一个 bed_assignment() 声明的声道就是按这个顺序展开的。
BED_LAYOUT_ORDER = (
"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",
)
# 标准 bed 声道 → 扬声器布局里的短名(与 speaker_layouts 的声道名同形,如 RC_LW → Lw)。
BED_CHANNEL_NAMES = {
"RC_L": "L", "RC_R": "R", "RC_C": "C", "RC_LFE": "LFE",
"RC_LS": "Ls", "RC_RS": "Rs", "RC_LB": "Lrs", "RC_RB": "Rrs",
"RC_TFL": "Ltf", "RC_TFR": "Rtf", "RC_TSL": "Ltm", "RC_TSR": "Rtm",
"RC_TBL": "Ltr", "RC_TBR": "Rtr", "RC_LW": "Lw", "RC_RW": "Rw",
}
# 每个 bed 声道的固定方位(ADM 笛卡尔坐标,x 向右、y 向前、z 向上)。
# bed 声道是固定位置,不是可移动对象;渲染时按这里的位置摆位,而不是沿用 OAMD 的
# 对象坐标(bed 对象在 OAMD 里没有位置字段)。
BED_CHANNEL_ADM_POSITION = {
"RC_L": (-1.0, 1.0, 0.0),
"RC_R": (1.0, 1.0, 0.0),
"RC_C": (0.0, 1.0, 0.0),
"RC_LS": (-1.0, 0.0, 0.0),
"RC_RS": (1.0, 0.0, 0.0),
"RC_LB": (-1.0, -1.0, 0.0),
"RC_RB": (1.0, -1.0, 0.0),
"RC_LW": (-1.0, 0.5, 0.0),
"RC_RW": (1.0, 0.5, 0.0),
"RC_TFL": (-0.5, 0.5, 1.0),
"RC_TFR": (0.5, 0.5, 1.0),
"RC_TSL": (-1.0, 0.0, 1.0),
"RC_TSR": (1.0, 0.0, 1.0),
"RC_TBL": (-0.5, -0.5, 1.0),
"RC_TBR": (0.5, -0.5, 1.0),
}
# 标准 bed 布局名:由声明声道的集合唯一确定(9.1.6 这类常用叫法即由此得来)。
BED_LAYOUT_NAMES = {
frozenset(("L", "R")): "2.0",
frozenset(("L", "R", "C")): "3.0",
frozenset(("L", "R", "C", "LFE")): "3.1",
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", "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",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Ltf", "Rtf", "Ltr", "Rtr")):
"5.1.4",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Ltm", "Rtm")): "7.1.2",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs",
"Ltf", "Rtf", "Ltr", "Rtr")): "7.1.4",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs",
"Ltf", "Rtf", "Ltm", "Rtm", "Ltr", "Rtr")): "7.1.6",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs",
"Lw", "Rw", "Ltm", "Rtm")): "9.1.2",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs",
"Lw", "Rw", "Ltf", "Rtf", "Ltr", "Rtr")): "9.1.4",
frozenset(("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Lw", "Rw",
"Ltf", "Rtf", "Ltm", "Rtm", "Ltr", "Rtr")): "9.1.6",
}
# ADM 7.1.2 床的槽位顺序:与 BED_EXTRA_SLOTS_BY_CONFIG 的 10 槽骨架一致。
ADM_BED_712 = ("L", "R", "C", "LFE", "Ls", "Rs", "Lrs", "Rrs", "Ltf", "Rtf")
def adm_xyz_to_q(x, y, z):
"""ADM 笛卡尔坐标 → OAMD/渲染器使用的 Q15 方位三元组。"""
q1 = int(round((float(x) + 1.0) * 0.5 * 32767.0))
q2 = int(round((1.0 - float(y)) * 0.5 * 32767.0))
q3 = int(round(float(z) * 32767.0))
clamp = lambda value: max(0, min(32767, value)) # noqa: E731
return clamp(q1), clamp(q2), clamp(q3)
def declared_bed_channels(assignments):
"""把 ``program_assignment()`` 的 bed 声明展开成有序的标准声道标签。
``assignments`` 是 ``bed_assignments`` 列表:元素可以是带 ``channels`` 的字典,
也可以直接是声道标签列表(``program_bed_channels()`` 的形式)。未识别标签保持
原样,由调用方决定是否当作变体处理。
"""
labels = []
for assignment in assignments or ():
if isinstance(assignment, dict):
channels = assignment.get("channels", ())
else:
channels = assignment
for label in channels:
if label not in labels:
labels.append(label)
return tuple(labels)
def bed_layout_name(labels):
"""按声明声道集合识别 bed 布局名;未知集合返回 None。"""
names = [BED_CHANNEL_NAMES.get(label, label) for label in labels]
return BED_LAYOUT_NAMES.get(frozenset(names))
def bed_layout(assignments):
"""声明的 bed 布局:``{"name", "labels", "channels"}``;没有 bed 声明时返回 None。
``channels`` 去掉 LFE/LFE2:JOC 的对象槽按这个顺序依次对应 15 路对象 PCM,
LFE 走核心声道的独立通路。
"""
labels = declared_bed_channels(assignments)
if not labels:
return None
channels = [label for label in labels if label not in ("RC_LFE", "RC_LFE2")]
return {
"name": bed_layout_name(labels),
"labels": list(labels),
"channels": channels,
}
def bed_object_positions(labels):
"""JOC 对象槽 → 固定 ADM 位置:(1..15) 对应未含 LFE 的 bed 声道。
bed 对象在 OAMD 里没有位置字段,位置由声明的 bed 布局决定。返回的字典只包含
能给出固定位置的标准声道;不是 bed 声道的对象不在其中,仍按 OAMD 坐标渲染。
"""
positions = {}
for index, label in enumerate(labels, 1):
if index > 15:
break
position = BED_CHANNEL_ADM_POSITION.get(label)
if position is not None:
positions[index] = position
return positions
def bed_object_names(labels):
"""JOC 对象槽 → 扬声器布局短名(用于把 bed 声道直接落到同名扬声器)。"""
names = {}
for index, label in enumerate(labels, 1):
if index > 15:
break
name = BED_CHANNEL_NAMES.get(label)
if name is not None and name != "LFE":
names[index] = name
return names
def joc_input_indices(locations, pair):
"""返回 JOC 输入序 (L, R, C, Ls, Rs, 额外对…) 在床 PCM 里的下标。"""
if pair is None:
+12 -6
View File
@@ -89,10 +89,12 @@ class _ObjectPositionTrack:
class OamdPositionTimeline:
"""Convert OAMD state updates into a sample-timed Cartesian trajectory."""
def __init__(self, object_count: int = 15):
def __init__(self, object_count: int = 15, bed_positions=None):
if object_count != 15:
raise ValueError("JOC OAMD currently requires 15 object slots")
self.object_count = int(object_count)
# bed 声道是固定通道:OAMD 不给位置,方位由声明的 bed 布局给出。
self.bed_positions = dict(bed_positions or {})
self.state = JocFieldState()
self.tracks = [_ObjectPositionTrack() for _ in range(self.object_count)]
self.initialized = False
@@ -104,14 +106,18 @@ class OamdPositionTimeline:
def _targets(self) -> list[tuple[float, float, float]]:
q = self.state.q
return [
q_to_adm_xyz(
targets = []
for object_index in range(1, self.object_count + 1):
fixed = self.bed_positions.get(object_index)
if fixed is not None:
targets.append((float(fixed[0]), float(fixed[1]), float(fixed[2])))
continue
targets.append(q_to_adm_xyz(
q[(object_index, "q1")],
q[(object_index, "q2")],
q[(object_index, "q3")],
)
for object_index in range(1, self.object_count + 1)
]
))
return targets
def submit_update(self, update: dict, *, frame_start_sample: int,
outer_sample_offset: int = 0,
+14 -6
View File
@@ -60,7 +60,8 @@ class SofaBinauralRenderer:
mode: str = "mid",
object_delay_samples: int = 1473,
tail_seconds: float = 5.0,
chunk_frames: int = 64):
chunk_frames: int = 64,
bed_channels=None):
required_interface = (
"source_count", "default_profile", "set_source", "process",
"finish", "finish_output_capacity", "info")
@@ -87,7 +88,10 @@ class SofaBinauralRenderer:
self.chunk_frames = int(chunk_frames)
self.chunk_samples = self.chunk_frames * FRAME_SAMPLES
self.dsp_backend = getattr(backend, "dsp_backend", "python-sofa")
self.timeline = OamdPositionTimeline(15)
from bed_layout import bed_object_positions
self.timeline = OamdPositionTimeline(
15, bed_positions=bed_object_positions(bed_channels or ()))
self._input_buffer = np.empty(
(self.chunk_samples, SOURCE_CHANNELS), dtype=np.float64)
@@ -110,7 +114,8 @@ class SofaBinauralRenderer:
object_delay_samples: int = 1473,
tail_seconds: float = 5.0,
output_gain: float = 1.0,
chunk_frames: int = 64) -> "SofaBinauralRenderer":
chunk_frames: int = 64,
bed_channels=None) -> "SofaBinauralRenderer":
source = resolve_sofa_hrtf(sofa)
backend = SofaBinauralBackend.from_sofa(
source,
@@ -127,7 +132,8 @@ class SofaBinauralRenderer:
mode=mode,
object_delay_samples=object_delay_samples,
tail_seconds=tail_seconds,
chunk_frames=chunk_frames)
chunk_frames=chunk_frames,
bed_channels=bed_channels)
@classmethod
def from_compiled_cache(
@@ -136,7 +142,8 @@ class SofaBinauralRenderer:
object_delay_samples: int = 1473,
tail_seconds: float = 5.0,
output_gain: float = 1.0,
chunk_frames: int = 64) -> "SofaBinauralRenderer":
chunk_frames: int = 64,
bed_channels=None) -> "SofaBinauralRenderer":
source = resolve_compiled_hrtf_cache(cache)
backend = SofaBinauralBackend.from_compiled_cache(
source,
@@ -148,7 +155,8 @@ class SofaBinauralRenderer:
mode=mode,
object_delay_samples=object_delay_samples,
tail_seconds=tail_seconds,
chunk_frames=chunk_frames)
chunk_frames=chunk_frames,
bed_channels=bed_channels)
@property
def finish_capacity_samples(self) -> int:
+59 -31
View File
@@ -6,23 +6,17 @@ import json
from pathlib import Path
from adm_atmos import q_to_adm_xyz
from bed_layout import EXTRA_PAIR_BY_CONFIG, extra_pair
from bed_layout import (BED_CHANNEL_NAMES, EXTRA_PAIR_BY_CONFIG, bed_layout,
declared_bed_channels, extra_pair)
from evo_unpack import unpack_evolution
from emdf import (ACMOD_LOCATIONS, EmdfError, audio_frame_groups, expand_chanmap,
find_joc_emdf, iter_syncframes, parse_container)
from joc_decode import parse_joc, parse_joc_header
from oamd_bits import JocFieldState, frame_update_values, program_bed_channels
from oamd_bits import (JocFieldState, frame_update_values, program_bed_channels,
program_bed_summary)
from variant_error import UnsupportedVariantError, bytes_descriptor
# 规格 §11.2 校验 2:OAMD 标准 bed 掩码(Table 12)里的额外对标签。
_BED_PAIR_GROUPS = (
(frozenset(("RC_LB", "RC_RB")), ("Lrs", "Rrs")),
(frozenset(("RC_TFL", "RC_TFR")), ("Vhl", "Vhr")),
(frozenset(("RC_TBL", "RC_TBR")), ("Lts", "Rts")),
)
def _joc_pair_score(container):
"""这个容器作为本帧 JOC 元数据的可信度(规格 §8.2 / Table 56)。
@@ -80,17 +74,19 @@ def _transport_record(group):
}
def _declared_bed_pairs(bed_channels):
"""标准 bed 掩码里声明的"额外那一对",按 Table 12 的标签判断。"""
labels = {name for instance in bed_channels for name in instance}
pairs = []
for members, pair in _BED_PAIR_GROUPS:
if members.issubset(labels):
pairs.append(pair)
return pairs
def _bed_covers_core(bed_channels, core_positions):
"""声明的 bed 布局是否覆盖核心子流携带的全频段声道。
OAMD 的 bed 掩码描述的是**内容**的 bed 布局,可以比 E-AC-3 核心实际编码的声道多
(例如 9.1.6 bed 只编 5.1 核心)。因此只有"核心声道在 bed 布局里找不到位置"才是
真正的不一致;多出来的 bed 声道是布局的一部分,不算冲突。
"""
names = {BED_CHANNEL_NAMES.get(label, label)
for instance in bed_channels for label in instance}
return all(position in names for position in core_positions)
def check_cross_layer(parsed, *, frame, transport=None, bed_channels=None,
def check_cross_layer(parsed, *, frame, transport=None, bed_channels=None, bed_layout_info=None,
diagnostics=None):
"""规格 §11.2 的跨层一致性校验。
@@ -168,21 +164,24 @@ def check_cross_layer(parsed, *, frame, transport=None, bed_channels=None,
frame=frame, details=details)
if bed_channels is not None:
declared_pairs = _declared_bed_pairs(bed_channels)
if declared_pairs and (expected_pair is None or
any(pair != expected_pair for pair in declared_pairs)):
core_positions = []
if transport is not None:
core_positions = list(transport["core_locations"])
if transport["chanmap"] is not None:
core_positions += expand_chanmap(transport["chanmap"])
if not _bed_covers_core(bed_channels, core_positions):
if diagnostics is not None:
diagnostics.append({
"stage": "oamd",
"variant": "bed_mask_cfg_mismatch",
"frame": frame,
"message": "OAMD 标准 bed 掩码声明的额外对与 joc_dmx_config_idx 不一致,"
"只影响 bed 槽位记录,音频不受影响",
"message": "OAMD bed 布局没有覆盖核心子流的全频段声道,只影响 bed 槽位记录,"
"音频不受影响",
"details": {
"config_idx": config_idx,
"bed_channels": [list(instance) for instance in bed_channels],
"declared_pairs": [list(pair) for pair in declared_pairs],
"expected_pair": list(expected_pair or ()),
"bed_layout": (bed_layout_info or {}).get("name"),
"core_positions": core_positions,
},
})
@@ -199,7 +198,8 @@ class DirectPayloadIndex:
"""
def __init__(self, rows, subpayloads, sample_offsets, directory=None,
transports=None, diagnostics=None, config_idx=None, n_channels=None):
transports=None, diagnostics=None, config_idx=None, n_channels=None,
bed_assignments=None, bed_only=False):
self.rows = rows
self._subpayloads = subpayloads
self._sample_offsets = sample_offsets
@@ -208,6 +208,22 @@ class DirectPayloadIndex:
self.diagnostics = diagnostics if diagnostics is not None else []
self.config_idx = config_idx
self.n_channels = n_channels
# OAMD bed 声明(内容声床布局):bed 对象没有位置字段,位置由布局决定。
self.bed_assignments = bed_assignments if bed_assignments is not None else []
self.bed_layout = bed_layout(self.bed_assignments)
self.bed_only = bool(bed_only)
@property
def bed_channels(self):
"""bed 声道标签(JOC 对象槽顺序);非 bed-only 内容返回空。
bed 声道是固定通道,只有整个 program 都由 bed 组成时 15 路 JOC 对象才逐槽
对应 bed 声道。含动态对象的内容仍按 OAMD 坐标渲染。
"""
if not self.bed_only:
return ()
layout = self.bed_layout or {}
return tuple(layout.get("channels") or ())
def transport(self, row):
return self.transports[int(row["frame"])] if self.transports else None
@@ -235,6 +251,9 @@ class DirectPayloadIndex:
config_idx = None
n_channels = None
missing_joc_frames = 0
bed_assignments = []
bed_only = False
bed_layout_info = None
groups = audio_frame_groups(iter_syncframes(data))
for frame_number, group in enumerate(groups):
if max_frames is not None and frame_number >= max_frames:
@@ -279,10 +298,15 @@ class DirectPayloadIndex:
if 14 in container.payloads:
parsed = parse_joc_header(container.payloads[14])
parsed["payload"] = container.payloads[14]
frame_bed = (program_bed_summary(container.payloads[11])
if 11 in container.payloads else None)
if frame_bed and not bed_assignments and frame_bed["channels"]:
bed_assignments = [list(instance) for instance in frame_bed["channels"]]
bed_only = bool(frame_bed["bed_only"])
bed_layout_info = bed_layout(bed_assignments)
check_cross_layer(parsed, frame=frame_number, transport=transport,
bed_channels=(
program_bed_channels(container.payloads[11])
if 11 in container.payloads else None),
bed_channels=(frame_bed["channels"] if frame_bed else None),
bed_layout_info=bed_layout_info,
diagnostics=diagnostics)
if config_idx is None:
config_idx = parsed["dmx_config_idx"]
@@ -327,7 +351,7 @@ class DirectPayloadIndex:
writer.writeheader()
writer.writerows(rows)
return cls(rows, payloads, offsets, cache_dir, transports, diagnostics,
config_idx, n_channels)
config_idx, n_channels, bed_assignments, bed_only)
def __len__(self):
return len(self.rows)
@@ -526,6 +550,10 @@ def inspect(index, limit=None, print_frames=False):
"frame_samples": 1536,
"sample_rate": 48000,
"duration_sec": len(rows) * 1536 / 48000,
"bed_layout": (getattr(index, "bed_layout", None) or {}).get("name"),
"bed_channels": [
BED_CHANNEL_NAMES.get(label, label)
for label in getattr(index, "bed_channels", ())],
"configurations": [
{"downmix_config": k[0], "extension_config": k[1], "core_channels": k[2],
"objects": k[3], "frames": count}
+25 -6
View File
@@ -458,11 +458,12 @@ def _parse_elements(reader, alternate_present, object_count, program, raw_payloa
return object_element, elements
def program_bed_channels(bits_one):
"""只走 OAMD 头与 program_assignment(),返回声明为 bed 的声道标签列表。
def program_bed_summary(bits_one):
"""只走 OAMD 头与 program_assignment(),返回 bed 声明与内容类型。
跨层一致性校验(规格 §11.2 校验 2)只需要这一段;完整帧解析见
:func:`frame_update`,它还要继续解析全部对象 element。
``channels`` 是按 bed_assignment() 展开的声道标签;``bed_only`` 表示这个 program
只由 bed 组成(没有 ISF、没有动态对象)——此时 15 路 JOC 对象就是 bed 声道,
位置由 bed 布局决定。跨层校验与完整帧解析之外的调用都只读这一段。
"""
bits, raw_payload = _payload_bits(bits_one)
try:
@@ -487,8 +488,26 @@ def program_bed_channels(bits_one):
raise _syntax_error(
"header_truncated", "OAMD header 字段越界或不完整",
raw_payload, exc, payload_bits=len(bits)) from exc
return [list(assignment["channels"])
for assignment in program["bed_assignments"]]
assignments = program["bed_assignments"]
content = program.get("content_description") or {}
return {
"channels": [list(assignment["channels"]) for assignment in assignments],
"assignments": assignments,
"bed_only": bool(content.get("bed")) and not content.get("isf")
and not content.get("dynamic"),
"num_bed_objects": program["num_bed_objects"],
"num_isf_objects": program["num_isf_objects"],
"num_dynamic_objects": program["num_dynamic_objects"],
}
def program_bed_channels(bits_one):
"""只走 OAMD 头与 program_assignment(),返回声明为 bed 的声道标签列表。
跨层一致性校验(规格 §11.2 校验 2)只需要这一段;完整帧解析见
:func:`frame_update`,它还要继续解析全部对象 element。
"""
return program_bed_summary(bits_one)["channels"]
def frame_update(bits_one):
+17 -7
View File
@@ -2,6 +2,7 @@
import math
from adm_atmos import q_to_adm_xyz
from bed_layout import bed_object_positions
from oamd_bits import JocFieldState, frame_update
from variant_error import UnsupportedVariantError
@@ -232,10 +233,19 @@ def build_adm_tracks(index, frames=None, rate=48000, frame_samples=1536,
previous[obj - 1] = xyz
total_samples = len(frames) * frame_samples
return [
(f"JOC_Object_{obj}",
_expand_events(events[obj - 1], total_samples, rate,
update_block_samples, object_delay_samples, obj,
trajectory_mode))
for obj in range(1, 16)
]
# bed 对象在 OAMD 里没有位置字段:位置由声明的 bed 布局决定,整片静止在该方位。
bed_positions = bed_object_positions(getattr(index, "bed_channels", ()))
tracks = []
for obj in range(1, 16):
fixed = bed_positions.get(obj)
if fixed is not None:
tracks.append((f"JOC_Object_{obj}",
[(0.0, fixed[0], fixed[1], fixed[2],
total_samples / float(rate), 0.0)]))
continue
tracks.append(
(f"JOC_Object_{obj}",
_expand_events(events[obj - 1], total_samples, rate,
update_block_samples, object_delay_samples, obj,
trajectory_mode)))
return tracks
+6 -3
View File
@@ -11,12 +11,14 @@ from speaker_renderer import PythonSpeakerRenderer, payload_events_from_index, r
FRAME_SAMPLES = 1536
def create_speaker_renderer(layout: str | SpeakerLayout):
def create_speaker_renderer(layout: str | SpeakerLayout, bed_channels=None):
"""Create a stateful speaker renderer and return ``(renderer, info)``."""
target = get_speaker_layout(layout) if isinstance(layout, str) else layout
return PythonSpeakerRenderer(target), {
renderer = PythonSpeakerRenderer(target, bed_channels=bed_channels)
return renderer, {
"name": "python",
"layout": target.name,
"bed_objects": len(getattr(renderer, "bed_targets", {}) or {}),
}
@@ -33,7 +35,8 @@ def render_speaker_layout(objects16, index, layout: str | SpeakerLayout, *,
if frames > len(index.rows):
raise ValueError(f"metadata has {len(index.rows)} frames, PCM needs {frames}")
renderer, info = create_speaker_renderer(target)
renderer, info = create_speaker_renderer(
target, bed_channels=getattr(index, "bed_channels", ()))
output = np.empty((len(source), target.channel_count), dtype=np.float64)
started = time.perf_counter()
try:
+77 -15
View File
@@ -19,6 +19,79 @@ Q15_SCALE = 32768.0
Q15_MAX = 32767.0 / Q15_SCALE
GAIN_SNAP_THRESHOLD = 1.0e-4
# 每个布局 region 0 控制点 → 该点上唯一响应的声道。床声道用这个锚点直接落到目标
# 布局的同名扬声器上(床声道是一路固定通道,不是需要声像的对象)。
_ANCHOR_CACHE: dict[str, dict[str, tuple[float, float, float]]] = {}
def layout_channel_anchors(layout: SpeakerLayout) -> dict[str, tuple[float, float, float]]:
"""返回 ``{声道名: (u, v, w)}``:把点对象放在这里时该声道增益为 1。
坐标取自布局自己的 region 0 控制点,因此锚点与布局的声像模型一致;目标布局没
有的声道不会出现在结果里。
"""
cached = _ANCHOR_CACHE.get(layout.name)
if cached is not None:
return cached
anchors: dict[str, tuple[float, float, float]] = {}
region = layout.regions[0]
for coordinates in region.coordinates_q15:
u, v, w = (coordinate / 32767.0 for coordinate in coordinates)
gains = render_point_gains(layout, u, v, w, standard_order=True)
best = int(np.argmax(gains))
dominant = float(gains[best])
if dominant < 0.99:
continue
others = np.delete(gains, best)
if others.size and float(np.max(np.abs(others))) > 1.0e-3:
continue
anchors.setdefault(layout.channels[best], (u, v, w))
_ANCHOR_CACHE[layout.name] = anchors
return anchors
def bed_object_anchors(layout: SpeakerLayout, bed_channels) -> dict[int, tuple[float, float, float]]:
"""JOC 对象槽 → 固定方位 ``(u, v, w)``;只包含 bed 声道。
目标布局有同名扬声器时用该扬声器的锚点(床声道直连同名声道);目标布局没有这
个声道时退回 bed 布局给出的标准方位,由声像模型摆位。
"""
from bed_layout import BED_CHANNEL_ADM_POSITION, BED_CHANNEL_NAMES
anchors = layout_channel_anchors(layout)
result: dict[int, tuple[float, float, float]] = {}
for object_index, label in enumerate(bed_channels or (), 1):
if object_index > 15:
break
name = BED_CHANNEL_NAMES.get(label)
if name is None or name == "LFE":
continue
if name in anchors:
result[object_index] = anchors[name]
continue
position = BED_CHANNEL_ADM_POSITION.get(label)
if position is not None:
x, y, z = position
result[object_index] = ((x + 1.0) * 0.5, (1.0 - y) * 0.5, z)
return result
def _all_object_targets(layout: SpeakerLayout, state: JocFieldState,
bed_targets=None) -> np.ndarray:
bed_targets = bed_targets or {}
result = np.zeros((15, layout.channel_count), dtype=np.float64)
for object_index in range(1, 16):
fixed = bed_targets.get(object_index)
if fixed is not None:
u, v, w = fixed
else:
u = state.q[(object_index, "q1")] / Q15_SCALE
v = state.q[(object_index, "q2")] / Q15_SCALE
w = state.q[(object_index, "q3")] / Q15_SCALE
result[object_index - 1] = render_point_gains(
layout, u, v, w, standard_order=False)
return result
def expand_speaker_bitfield(compact_mask: int, center_height: bool = False) -> int:
"""Expand the compact target-layout bitfield into individual speakers."""
@@ -176,27 +249,16 @@ def ramp_block_count(duration_samples: int, block_size: int = 32,
return (int(duration_samples) * int(rate_scale) + block_size // 2 - 1) // block_size
def _all_object_targets(layout: SpeakerLayout, state: JocFieldState) -> np.ndarray:
result = np.zeros((15, layout.channel_count), dtype=np.float64)
for object_index in range(1, 16):
result[object_index - 1] = render_point_gains(
layout,
state.q[(object_index, "q1")] / Q15_SCALE,
state.q[(object_index, "q2")] / Q15_SCALE,
state.q[(object_index, "q3")] / Q15_SCALE,
standard_order=False,
)
return result
class PythonSpeakerRenderer:
"""Stateful float64 speaker renderer."""
def __init__(self, layout: str | SpeakerLayout, block_size: int = 32):
def __init__(self, layout: str | SpeakerLayout, block_size: int = 32,
bed_channels=None):
self.layout = get_speaker_layout(layout) if isinstance(layout, str) else layout
self.block_size = int(block_size)
if self.block_size <= 0:
raise ValueError("block_size must be positive")
self.bed_targets = bed_object_anchors(self.layout, bed_channels)
self.reset()
def reset(self):
@@ -211,7 +273,7 @@ class PythonSpeakerRenderer:
def _apply_payload(self, payload):
update = frame_update(payload)
self._state.apply(update["values"])
new_target = _all_object_targets(self.layout, self._state)
new_target = _all_object_targets(self.layout, self._state, self.bed_targets)
blocks = ramp_block_count(update["ramp_duration_samples"], self.block_size)
difference = new_target - self._current
significant = np.abs(difference) >= GAIN_SNAP_THRESHOLD