Add JOC downmix configurations 0..4, cross-layer checks, and drop the native core

This commit is contained in:
TheM14 committed 2026-10-16 19:46:24 +08:00
1 parent 428772eb87
commit a1853f2dc5
37 files changed
+926 -6420

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
"""JOC 核心通道的输入序、床声明与 ADM 落位。
这里只有"位置/身份"层:按通道码把 E-AC-3 解出的 PCM 摆进 JOC 输入序,并把额外
那一对落到 ADM 骨架的槽位。JOC 的 DSP 不读取这些信息(规格 §10.1),它只用
``(N, sigma)``。
"""
from emdf import expand_chanmap
# JOC 核心通道序列的前 5 位(规格 §2):L R C Ls Rs。
JOC_CORE_POSITIONS = ("L", "R", "C", "Ls", "Rs")
# 额外那一对的身份由 k 决定(规格 §2):k=1 是后环绕,k∈{2,4} 是前上方。
EXTRA_PAIR_BY_CONFIG = {1: ("Lrs", "Rrs"), 2: ("Vhl", "Vhr"), 4: ("Vhl", "Vhr")}
# 额外那一对在 10 槽 ADM 骨架里的槽位(规格 §10.2 的 RC_Lrs/RC_Rrs 与 RC_Lts/RC_Rts)。
# ADM 现契约是"除 LFE 槽外全部静音"(床通道不作为输出通道),因此这些槽位只被记录,
# 不承载音频;DSP 也不读它们。
BED_EXTRA_SLOTS_BY_CONFIG = {1: (6, 7), 2: (8, 9), 4: (8, 9)}
# 位置 → ffmpeg 声道名与其声道位。ffmpeg 的帧内声道顺序就是声道位的升序,
# 因此这张表同时给出"位置集合 → 帧内下标"的换算。
_FFMPEG_POSITIONS = {
"L": ("FL", 0x1),
"R": ("FR", 0x2),
"C": ("FC", 0x4),
"LFE": ("LFE", 0x8),
"Lrs": ("BL", 0x10),
"Rrs": ("BR", 0x20),
"Lc": ("FLC", 0x40),
"Rc": ("FRC", 0x80),
"Cs": ("BC", 0x100),
"Ls": ("SL", 0x200),
"Rs": ("SR", 0x400),
"Vhl": ("TFL", 0x1000),
"Vhc": ("TFC", 0x2000),
"Vhr": ("TFR", 0x4000),
"Lts": ("TBL", 0x8000),
"Rts": ("TBR", 0x20000),
}
def bed_order(locations):
"""把位置集合按 ffmpeg 的声道位顺序排好,返回 (位置, ffmpeg 名) 列表。
未知位置返回 None,由调用方决定报哪种变体。
"""
unique = []
for position in locations:
if position not in unique:
unique.append(position)
if any(position not in _FFMPEG_POSITIONS for position in unique):
return None
return [(position, _FFMPEG_POSITIONS[position][0])
for position in sorted(unique, key=lambda p: _FFMPEG_POSITIONS[p][1])]
def extra_pair(core_locations, chanmap):
"""依赖子流的 chanmap 里超出核心声道集合的那一对位置。
chanmap 会把核心已有的声道一并列出(例如 5.1 核心 + 前上方对会写
``Ls/Rs/Lrs/Rrs``),因此额外对是"chanmap 位置 − 核心位置"。
"""
core = set(core_locations)
return tuple(position for position in expand_chanmap(chanmap) if position not in core)
def joc_input_indices(locations, pair):
"""返回 JOC 输入序 (L, R, C, Ls, Rs, 额外对…) 在床 PCM 里的下标。"""
if pair is None:
pair = ()
order = bed_order(locations)
if order is None:
return None
index_of = {position: index for index, (position, _name) in enumerate(order)}
required = list(JOC_CORE_POSITIONS) + list(pair)
if any(position not in index_of for position in required):
return None
if len(required) != len(set(required)):
return None
return [index_of[position] for position in required]
-214
View File
@@ -1,214 +0,0 @@
"""ctypes bridge for the native float64 binaural DSP."""
from __future__ import annotations
import ctypes
from pathlib import Path
import numpy as np
from native_renderer import ABI_VERSION, find_native_library
from rosella_filterbank import DEFAULT_KERNEL_DATA, load_kernel_tables
from rosella_model import RosellaModel
BLOCK_SAMPLES = 512
INPUT_CHANNELS = 16
OUTPUT_CHANNELS = 2
HYBRID_BANDS = 77
class NativeBinauralDsp:
def __init__(self, model: RosellaModel, *, library_path=None,
kernel_data: str | Path = DEFAULT_KERNEL_DATA):
self.library_path = find_native_library(library_path)
self._lib = ctypes.CDLL(str(self.library_path))
self._bind()
version = int(self._lib.ejoc_abi_version())
if version != ABI_VERSION:
raise RuntimeError(
f"native ABI mismatch: expected {ABI_VERSION}, got {version}")
self._handle = self._lib.ejoc_binaural_renderer_create()
if not self._handle:
raise RuntimeError("native binaural renderer creation failed")
try:
self._configure_kernels(kernel_data)
self._configure_room(model)
except Exception:
self.close()
raise
def _bind(self):
void_p = ctypes.c_void_p
f64_p = ctypes.POINTER(ctypes.c_double)
i16_p = ctypes.POINTER(ctypes.c_int16)
u32_p = ctypes.POINTER(ctypes.c_uint32)
self._lib.ejoc_abi_version.argtypes = []
self._lib.ejoc_abi_version.restype = ctypes.c_uint32
self._lib.ejoc_binaural_renderer_create.argtypes = []
self._lib.ejoc_binaural_renderer_create.restype = void_p
self._lib.ejoc_binaural_renderer_destroy.argtypes = [void_p]
self._lib.ejoc_binaural_renderer_destroy.restype = None
self._lib.ejoc_binaural_renderer_reset.argtypes = [void_p]
self._lib.ejoc_binaural_renderer_reset.restype = ctypes.c_int
self._lib.ejoc_binaural_renderer_last_error.argtypes = [void_p]
self._lib.ejoc_binaural_renderer_last_error.restype = ctypes.c_char_p
self._lib.ejoc_binaural_renderer_configure_kernels.argtypes = [
void_p, f64_p, f64_p, i16_p, f64_p, ctypes.c_uint32, f64_p, f64_p]
self._lib.ejoc_binaural_renderer_configure_kernels.restype = ctypes.c_int
self._lib.ejoc_binaural_renderer_configure_room.argtypes = [
void_p, ctypes.c_uint32, ctypes.c_uint32, u32_p, f64_p,
u32_p, f64_p, ctypes.c_uint32, f64_p, f64_p, f64_p,
ctypes.c_uint32, u32_p, f64_p, f64_p]
self._lib.ejoc_binaural_renderer_configure_room.restype = ctypes.c_int
self._lib.ejoc_binaural_renderer_process.argtypes = [
void_p, f64_p, f64_p, f64_p, ctypes.c_double, f64_p]
self._lib.ejoc_binaural_renderer_process.restype = ctypes.c_int
def _raise(self, operation, status):
message = self._lib.ejoc_binaural_renderer_last_error(self._handle)
detail = (message or b"").decode("utf-8", "replace")
raise RuntimeError(
f"native binaural renderer {operation} failed ({status}): {detail}")
@staticmethod
def _f64_pointer(values):
return values.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
def _configure_kernels(self, kernel_data):
tables = load_kernel_tables(kernel_data)
qmf_analysis = np.ascontiguousarray(
tables["qmf_analysis_coefficients"], dtype=np.float64)
hybrid_low = np.ascontiguousarray(
tables["hybrid_analysis_low_kernel"], dtype=np.float64)
hybrid_indices = np.ascontiguousarray(
tables["hybrid_synthesis_indices"], dtype=np.int16)
hybrid_values = np.ascontiguousarray(
tables["hybrid_synthesis_values"], dtype=np.float64)
qmf_basis = np.ascontiguousarray(
tables["qmf_synthesis_basis"], dtype=np.float64)
qmf_taps = np.ascontiguousarray(
tables["qmf_synthesis_taps"], dtype=np.float64)
status = self._lib.ejoc_binaural_renderer_configure_kernels(
self._handle,
self._f64_pointer(qmf_analysis),
self._f64_pointer(hybrid_low),
hybrid_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int16)),
self._f64_pointer(hybrid_values),
len(hybrid_values),
self._f64_pointer(qmf_basis),
self._f64_pointer(qmf_taps),
)
if status:
self._raise("configure_kernels", status)
def _configure_room(self, model: RosellaModel):
if float(model.table_a_scalar) >= 0.5:
raise NotImplementedError("alternate table-A room mode")
bands = min(64, model.table_a_dimension)
allpass_delays = np.ascontiguousarray(
model.table_a_option_ids, dtype=np.uint32)
allpass_gains = np.ascontiguousarray(
model.table_a_option_values, dtype=np.float64)
fdn_delays = np.ascontiguousarray(
model.table_a_four_integers, dtype=np.uint32)
fdn_matrix = np.ascontiguousarray(
np.asarray(model.table_a_vector16, dtype=np.float64).reshape(
4, 4, order="F"))
filter8 = np.asarray(
model.table_a_filter_8x64_padded, dtype=np.float64).reshape(20, 4, 2, 4)
filter4 = np.asarray(
model.table_a_filter_4x64_padded, dtype=np.float64).reshape(20, 4, 4)
filter16 = np.asarray(
model.table_a_filter_16x64_padded, dtype=np.float64).reshape(20, 4, 4, 4)
feedback = np.empty((64, 4, 2), dtype=np.float64)
output_taps = np.empty((64, 4), dtype=np.float64)
output_matrix = np.empty((2, 64, 4, 2), dtype=np.float64)
for band in range(64):
group, lane = divmod(band, 4)
feedback[band, :, 0] = filter8[group, :, 0, lane]
feedback[band, :, 1] = filter8[group, :, 1, lane]
output_taps[band] = filter4[group, :, lane]
output_matrix[0, band, :, 0] = filter16[group, :, 0, lane]
output_matrix[0, band, :, 1] = filter16[group, :, 1, lane]
output_matrix[1, band, :, 0] = filter16[group, :, 2, lane]
output_matrix[1, band, :, 1] = filter16[group, :, 3, lane]
extra_count = int(model.table_a_extra)
extra_delays = np.ascontiguousarray(
model.table_a_extra_indices, dtype=np.uint32)
extra_fields = np.empty((extra_count, 64, 2), dtype=np.float64)
extra_source = np.asarray(
model.table_a_extra_fields_padded, dtype=np.float64).reshape(
extra_count, 20, 2, 4)
for extra in range(extra_count):
for band in range(64):
group, lane = divmod(band, 4)
extra_fields[extra, band] = extra_source[extra, group, :, lane]
extra_matrices = np.empty((extra_count, 4, 4), dtype=np.float64)
for extra in range(extra_count):
extra_matrices[extra] = np.asarray(
model.table_a_extra_vectors[extra], dtype=np.float64).reshape(
4, 4, order="F")
null_u32 = ctypes.POINTER(ctypes.c_uint32)()
null_f64 = ctypes.POINTER(ctypes.c_double)()
status = self._lib.ejoc_binaural_renderer_configure_room(
self._handle,
bands,
len(allpass_delays),
allpass_delays.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)),
self._f64_pointer(allpass_gains),
fdn_delays.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)),
self._f64_pointer(fdn_matrix),
int(model.table_a_integer),
self._f64_pointer(feedback),
self._f64_pointer(output_taps),
self._f64_pointer(output_matrix),
extra_count,
(extra_delays.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32))
if extra_count else null_u32),
self._f64_pointer(extra_fields) if extra_count else null_f64,
self._f64_pointer(extra_matrices) if extra_count else null_f64,
)
if status:
self._raise("configure_room", status)
def reset(self):
if not self._handle:
raise RuntimeError("native binaural renderer is closed")
status = self._lib.ejoc_binaural_renderer_reset(self._handle)
if status:
self._raise("reset", status)
def process_block(self, pcm16, gains, room_sends, output_gain=1.0):
if not self._handle:
raise RuntimeError("native binaural renderer is closed")
source = np.ascontiguousarray(pcm16, dtype=np.float64)
gain_values = np.asarray(gains)
sends = np.ascontiguousarray(room_sends, dtype=np.float64)
if source.shape != (BLOCK_SAMPLES, INPUT_CHANNELS):
raise ValueError(f"pcm16 block must be (512,16), got {source.shape}")
if gain_values.shape != (INPUT_CHANNELS, OUTPUT_CHANNELS, HYBRID_BANDS):
raise ValueError(f"gains must be (16,2,77), got {gain_values.shape}")
direct = np.ascontiguousarray(
gain_values, dtype=np.complex128).view(np.float64)
if sends.shape != (INPUT_CHANNELS,):
raise ValueError(f"room_sends must be (16,), got {sends.shape}")
output = np.empty((BLOCK_SAMPLES, OUTPUT_CHANNELS), dtype=np.float64)
status = self._lib.ejoc_binaural_renderer_process(
self._handle,
self._f64_pointer(source),
self._f64_pointer(direct),
self._f64_pointer(sends),
float(output_gain),
self._f64_pointer(output),
)
if status:
self._raise("process", status)
return output
def close(self):
handle = getattr(self, "_handle", None)
if handle:
self._lib.ejoc_binaural_renderer_destroy(handle)
self._handle = None
+193 -13
View File
@@ -73,6 +73,9 @@ class EmdfContainer:
raw: bytes
payloads: dict
sample_offsets: dict
# payload_id -> group_id(未声明 groupid 的载荷为 None)。规格 §8.2/Table 56:
# 同一帧的 OAMD 与 JOC 载荷属于同一 group,凭此把一对载荷关联起来。
group_ids: dict = None
def _parse_at(data, start_bit):
@@ -99,6 +102,7 @@ def _parse_at(data, start_bit):
payloads = {}
sample_offsets = {}
group_ids = {}
terminated = False
while reader.position + 5 <= body_end:
payload_id = reader.read(5)
@@ -115,7 +119,9 @@ def _parse_at(data, start_bit):
if reader.read(1):
variable_bits(reader, 11) # duration
if reader.read(1):
variable_bits(reader, 2) # group id
group_ids[payload_id] = variable_bits(reader, 2) # group id
else:
group_ids[payload_id] = None
if reader.read(1):
reader.skip(8) # codec data
@@ -140,7 +146,7 @@ def _parse_at(data, start_bit):
total_bytes = 4 + length
raw_reader = BitReader(data, start_bit, start_bit + total_bytes * 8)
raw = raw_reader.read_bytes(total_bytes)
return EmdfContainer(start_bit, raw, payloads, sample_offsets)
return EmdfContainer(start_bit, raw, payloads, sample_offsets, group_ids)
def _marker_offsets(data):
@@ -162,13 +168,18 @@ def _marker_offsets(data):
return sorted(offsets)
def find_joc_emdf(frame):
def find_joc_emdf(frame, usable=None, diagnostics=None, frame_number=None):
"""返回同步帧中唯一、顶层连续且包含 ID11/ID14 的 JOC EMDF 容器。
EMDF payload 是不透明字节串,其中可能自然出现另一个 ``0x5838``。若从这个
内嵌 marker 开始的后续随机位恰好也能通过容器语法探测,它仍不是一个独立的
transport 容器。因此,候选的起点一旦落在较早 JOC 容器的声明范围内,就只把
它记作内嵌伪候选,不参与“多个容器”的判定。
一个同步帧里可能出现多个顶层容器(同一帧可能同时带两套 ID11/ID14)。
``usable`` 给出“这个容器能否作为本帧元数据”的分值(0 = 不是,越大越可信):
只有唯一最高分时用它,其余记入 ``diagnostics``;没有候选或最高分并列仍按变体
报错,不猜。
"""
matches = []
offsets = _marker_offsets(frame)
@@ -216,6 +227,22 @@ def find_joc_emdf(frame):
})
if len(top_level_matches) != 1:
starts = [item.start_bit for item in top_level_matches]
if usable is not None:
scored = [(usable(item), item) for item in top_level_matches]
best = max(score for score, _item in scored)
winners = [item for score, item in scored if score == best]
if best > 0 and len(winners) == 1:
if diagnostics is not None:
diagnostics.append({
"stage": "emdf",
"variant": "extra_container_ignored",
"frame": frame_number,
"message": "同步帧内有多个含 ID11/ID14 的 EMDF 容器,只有一个能作为本帧元数据",
"details": {"joc_container_start_bits": starts,
"used_start_bit": winners[0].start_bit,
"pair_score": best},
})
return winners[0]
raise UnsupportedVariantError(
"emdf_transport", "multiple_joc_containers",
"同步帧中存在多个可用 JOC EMDF,当前无法自动选择",
@@ -235,31 +262,184 @@ def parse_container(data):
return container
def iter_eac3_frames(data):
"""按 E-AC-3 ``frmsiz`` 遍历同步帧,拒绝静默重同步。"""
# AC-3 同步帧长度表(16-bit word),索引为 fscod(0=48 kHz、1=44.1 kHz、2=32 kHz)
# 再取 frmsizecod;与 A/52 的比特率表等价。
_AC3_FRAME_WORDS = (
(64, 64, 80, 80, 96, 96, 112, 112, 128, 128, 160, 160, 192, 192, 224, 224,
256, 256, 320, 320, 384, 384, 448, 448, 512, 512, 640, 640, 768, 768,
896, 896, 1024, 1024, 1152, 1152, 1280, 1280),
(69, 70, 87, 88, 104, 105, 121, 122, 139, 140, 174, 175, 208, 209, 243, 244,
278, 278, 348, 348, 417, 418, 487, 487, 557, 557, 696, 696, 835, 835,
975, 975, 1114, 1114, 1253, 1253, 1393, 1393),
(96, 96, 120, 120, 144, 144, 168, 168, 192, 192, 240, 240, 288, 288, 336, 336,
384, 384, 480, 480, 576, 576, 672, 672, 768, 768, 960, 960, 1152, 1152,
1344, 1344, 1536, 1536, 1728, 1728, 1920, 1920),
)
# E-AC-3 chanmap(TS 102 366 Annex E Table E2.5):字段的最高位对应 bit 0。
CHANMAP_LOCATIONS = (
"L", "C", "R", "Ls", "Rs", "Lc/Rc", "Lrs/Rrs", "Cs", "Ts", "Lsd/Rsd",
"Lw/Rw", "Vhl/Vhr", "Vhc", "Lts/Rts", "LFE2", "LFE",
)
# 单个 bit 展开为两个相邻声道的成对位置;展开顺序为先左后右。
CHANMAP_PAIR_BITS = frozenset((5, 6, 9, 10, 11, 13))
# A/52 acmod 对应的全频段声道位置(不含 LFE,按解码顺序)。
ACMOD_LOCATIONS = {
0: ("C",), # 1+1 双单声道
1: ("C",),
2: ("L", "R"),
3: ("L", "C", "R"),
4: ("L", "R", "Cs"),
5: ("L", "C", "R", "Cs"),
6: ("L", "R", "Ls", "Rs"),
7: ("L", "C", "R", "Ls", "Rs"),
}
def expand_chanmap(chanmap):
"""按 Table E2.5 把 16 bit chanmap 展开为有序声道位置列表。"""
locations = []
for bit in range(16):
if not (int(chanmap) >> (15 - bit)) & 1:
continue
locations.extend(CHANMAP_LOCATIONS[bit].split("/"))
return locations
@dataclass(frozen=True)
class SyncFrame:
"""一个同步帧及其传输层(L2)字段。"""
offset: int
size: int
codec: str # "ac3" 或 "eac3"
bsid: int
strmtyp: int # AC-3 没有子流类型,固定记为 0
substreamid: int
acmod: int
lfeon: int
chanmap: object # 依赖子流的 16 bit chanmap;未声明为 None
@property
def key(self):
"""同一音频帧内唯一的子流标识。"""
return (self.codec, self.strmtyp, self.substreamid)
def _read_bits(data, start, count):
value = 0
for index in range(start, start + count):
value = (value << 1) | ((data[index >> 3] >> (7 - (index & 7))) & 1)
return value
def sync_frame_at(data, offset):
"""解析 offset 处同步帧的传输层头。
区分 AC-3 与 E-AC-3 的依据是 bsid(bit 40 起 5 bit):bsid <= 8 为 AC-3,
其帧长由 fscod/frmsizecod 的比特率表给出;bsid >= 16 为 E-AC-3,帧长由
frmsiz 给出。依赖子流(strmtyp=1)在这里顺带解析出 chanmap。
"""
if offset + 6 > len(data) or data[offset] != 0x0B or data[offset + 1] != 0x77:
return None
base = offset * 8
bsid = _read_bits(data, base + 40, 5)
if bsid <= 8:
fscod = _read_bits(data, base + 32, 2)
frmsizecod = _read_bits(data, base + 34, 6)
if fscod > 2 or frmsizecod >= 38:
return None
size = _AC3_FRAME_WORDS[fscod][frmsizecod] * 2
acmod = _read_bits(data, base + 48, 3)
bit = 51
if acmod & 0x1 and acmod != 0x1:
bit += 2 # cmixlev
if acmod & 0x4:
bit += 2 # surmixlev
if acmod == 0x2:
bit += 2 # dsurmod
lfeon = _read_bits(data, base + bit, 1)
return SyncFrame(offset, size, "ac3", bsid, 0, 0, acmod, lfeon, None)
strmtyp = _read_bits(data, base + 16, 2)
substreamid = _read_bits(data, base + 18, 3)
frmsiz = _read_bits(data, base + 21, 11)
# fscod==3 时的 fscod2 同样是 2 bit,因此 acmod 恒在 bit 36。
acmod = _read_bits(data, base + 36, 3)
lfeon = _read_bits(data, base + 39, 1)
bit = 45 # bsid 之后
bit += 5 # dialnorm
compre = _read_bits(data, base + bit, 1)
bit += 1
if compre:
bit += 8 # compr
if acmod == 0:
bit += 5 # dialnorm2
compr2 = _read_bits(data, base + bit, 1)
bit += 1
if compr2:
bit += 8
chanmap = None
if strmtyp == 1:
if _read_bits(data, base + bit, 1):
chanmap = _read_bits(data, base + bit + 1, 16)
return SyncFrame(offset, (frmsiz + 1) * 2, "eac3", bsid, strmtyp, substreamid,
acmod, lfeon, chanmap)
def iter_syncframes(data):
"""按同步字长度遍历输入中的 AC-3 与 E-AC-3 同步帧。"""
pos = 0
while pos < len(data):
if pos + 4 > len(data) or data[pos:pos + 2] != b"\x0b\x77":
frame = sync_frame_at(data, pos)
if frame is None:
raise UnsupportedVariantError(
"eac3_transport", "syncframe_header",
"E-AC-3 同步帧头无效或出现了未处理的子流排列",
"同步帧头无效或出现了未处理的子流排列",
details={
"byte_offset": pos,
"remaining_bytes": len(data) - pos,
"next_16_bytes_hex": data[pos:pos + 16].hex(),
})
size = ((((data[pos + 2] & 7) << 8) | data[pos + 3]) + 1) * 2
if pos + size > len(data):
if pos + frame.size > len(data):
raise UnsupportedVariantError(
"eac3_transport", "truncated_syncframe",
"E-AC-3 末帧长度超过输入剩余数据",
"末帧长度超过输入剩余数据",
details={
"byte_offset": pos,
"declared_frame_bytes": size,
"codec": frame.codec,
"declared_frame_bytes": frame.size,
"remaining_bytes": len(data) - pos,
})
yield data[pos:pos + size]
pos += size
yield frame
pos += frame.size
def audio_frame_groups(frames):
"""把同步帧序列按音频帧分组。
一个音频帧内每个 (编码, strmtyp, substreamid) 组合只出现一次,因此再次遇到
已出现过的组合即表示下一个音频帧开始。独立子流缺席(只有依赖子流)或核心是
AC-3 时同样成立。
"""
group = []
keys = set()
for frame in frames:
if frame.key in keys:
yield group
group = []
keys = set()
keys.add(frame.key)
group.append(frame)
if group:
yield group
def iter_eac3_frames(data):
"""兼容入口:只产出同步帧字节。"""
for frame in iter_syncframes(data):
yield data[frame.offset:frame.offset + frame.size]
def extract_index(eac3_path, output_dir, max_frames=None):
+28 -11
View File
@@ -35,8 +35,10 @@ def _load_huff_tables():
H = _load_huff_tables()
JOC_NUM_CHANNELS = {0: 5, 1: 7, 2: 7, 3: 5, 4: 7} # Table 33
# band-0 的 21-tap DC 补偿只在 downmix 配置 3/4 下启用。
DC_FILTER_DMX_CONFIGS = (3, 4)
# σ(k) = 1 当且仅当 k∈{3,4}:同一个开关同时门控 Ls/Rs 的 -j 旋转与 band-0 FIR。
SIGMA_DMX_CONFIGS = (3, 4)
# band-0 的 21-tap DC 补偿只在 σ(k)=1 的 downmix 配置下启用。
DC_FILTER_DMX_CONFIGS = SIGMA_DMX_CONFIGS
JOC_NUM_BANDS = {0: 1, 1: 3, 2: 5, 3: 7, 4: 9, 5: 12, 6: 15, 7: 23} # Table 35
JOC_NUM_QUANT = {0: 96, 1: 192} # Table 51
# dense 的量化零点就是 nquant/2;sparse 的递推起点比它高 2 个量化步。
@@ -77,18 +79,33 @@ def get_huff_code(mode, typ, nch):
else "joc_huff_code_fine_generic"] # MTX
def parse_joc_header(payload):
"""只读 joc_header 的 3+6+3 bit,不做 Huffman 解码。
跨层一致性校验(规格 §11.2)只需要这些字段,因此不必为校验再走一遍完整解析。
"""
br = BR(payload)
out = {
"dmx_config_idx": br.bits(3),
"num_objects_bits": br.bits(6),
"ext_config_idx": br.bits(3),
}
out["n_channels"] = JOC_NUM_CHANNELS.get(out["dmx_config_idx"])
if out["n_channels"] is None:
raise ValueError(f"未知的 JOC downmix 配置 {out['dmx_config_idx']}")
# 声明对象数超过 15 时截断到 15;截断本身另行记入 diagnostics。
declared = out["num_objects_bits"] + 1
out["objects_clamped"] = declared > 15
out["n_objects"] = min(declared, 15)
return out
def parse_joc(payload):
"""解析 id14 载荷(joc() 位流)。返回字段 dict + 解析后剩余位数。"""
br = BR(payload)
out = {}
out["dmx_config_idx"] = br.bits(3)
out["num_objects_bits"] = br.bits(6)
out["ext_config_idx"] = br.bits(3)
n_objects = out["num_objects_bits"] + 1
n_channels = JOC_NUM_CHANNELS.get(out["dmx_config_idx"])
if n_channels is None:
raise ValueError(f"未知的 JOC downmix 配置 {out['dmx_config_idx']}")
out["n_objects"], out["n_channels"] = n_objects, n_channels
out = parse_joc_header(payload)
n_objects, n_channels = out["n_objects"], out["n_channels"]
br.p = 12
out["clipgain_x_bits"] = br.bits(3)
out["clipgain_y_bits"] = br.bits(5)
out["seq_count_bits"] = br.bits(10)
+5 -4
View File
@@ -130,11 +130,12 @@ _SURROUND_DC_B = np.array([
_SURROUND_DC_C = _SURROUND_DC_B + 1j * _SURROUND_DC_A
def surround_post_frame(x, delay, dc_hist, apply_dc_filter=True):
def surround_post_frame(x, delay, dc_hist, apply_dc_filter=True, rotate=True):
"""处理 Ls/Rs 的 10 槽延迟、-j 旋转和 band-0 FIR。
``apply_dc_filter=False`` 时跳过 band-0 的 21-tap DC 补偿,只做延迟与
-j 旋转;延迟线与 DC 历史仍照常推进,便于逐帧切换。
``rotate`` 与 ``apply_dc_filter`` 由同一个开关 σ(k) 门控(规格 §4):
σ(k)=0(k∈{0,1,2})时 c=3,4 退化为普通延迟,既不旋转也不做 FIR。
延迟线与 DC 历史无论开关如何都照常推进,便于逐帧切换。
"""
src = np.asarray(x, dtype=np.complex128)
qdelay = np.asarray(delay, dtype=np.complex128).copy()
@@ -145,7 +146,7 @@ def surround_post_frame(x, delay, dc_hist, apply_dc_filter=True):
for group in range(0, 24, 4):
current = src[:, :, group:group + 4].transpose(0, 2, 1)
queued = np.concatenate((qdelay, current), axis=1)
block = -1j * queued[:, :4, :]
block = -1j * queued[:, :4, :] if rotate else queued[:, :4, :].copy()
qdelay = queued[:, 4:, :]
dc_buf = np.concatenate((hist, current[:, :, 0]), axis=1)
if apply_dc_filter:
+278 -26
View File
@@ -6,26 +6,218 @@ 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 evo_unpack import unpack_evolution
from emdf import EmdfError, find_joc_emdf, iter_eac3_frames, parse_container
from joc_decode import parse_joc
from oamd_bits import JocFieldState, frame_update_values
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 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)。
§8.2:OAMD 与 JOC 载荷放在同一个 EMDF 容器里;Table 56:两者的 groupid
必须相同。同一个同步帧里可能出现多个含 ID11/ID14 的顶层容器,其中不构成一对的那个
(ID14 声明的对象数超出其字节所能容纳的范围、ID11 未声明 groupid)既不是一对
合法载荷,也不可能是本帧的 JOC 参数。
2 = 两个载荷 groupid 相同且 ID14 完整解析;1 = ID14 完整解析;0 = 其余。
"""
payload = container.payloads.get(14)
if payload is None or 11 not in container.payloads:
return 0
parses = True
try:
parse_joc(payload)
except (IndexError, ValueError):
parses = False
group_ids = getattr(container, "group_ids", None) or {}
first = group_ids.get(11)
second = group_ids.get(14)
associated = first is not None and second is not None and first == second
if associated and parses:
return 2
return 1 if parses else 0
def _transport_record(group):
"""从一个音频帧的同步帧组提取规格 §11.2 需要的 L2 声明。"""
core_locations = ()
core_lfe = False
chanmap = None
for sync in group:
if sync.codec == "ac3" or sync.strmtyp == 0:
core_locations = ACMOD_LOCATIONS.get(sync.acmod, ())
core_lfe = bool(sync.lfeon)
elif sync.chanmap is not None:
chanmap = sync.chanmap
return {
"core_locations": tuple(core_locations),
"core_lfe": core_lfe,
"chanmap": chanmap,
"syncframes": [
{
"offset": sync.offset,
"size": sync.size,
"codec": sync.codec,
"strmtyp": sync.strmtyp,
"substreamid": sync.substreamid,
"acmod": sync.acmod,
"lfeon": sync.lfeon,
}
for sync in 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 check_cross_layer(parsed, *, frame, transport=None, bed_channels=None,
diagnostics=None):
"""规格 §11.2 的跨层一致性校验。
校验 1/2(额外那对的身份)不一致时只记 diagnostics:通道身份不进 DSP
(§10.1),床映射按 chanmap 给出的通道码做,音频与 k 一致时逐样本相同。
校验 3(核心通道数)与 joc_ext_config_idx 会改变 DSP 输入,仍按变体报错。
"""
config_idx = parsed["dmx_config_idx"]
n_channels = parsed["n_channels"]
expected_pair = EXTRA_PAIR_BY_CONFIG.get(config_idx)
if parsed["ext_config_idx"] != 0:
raise UnsupportedVariantError(
"joc", "ext_config_idx_unsupported",
"joc_ext_config_idx 取到当前未映射的值",
frame=frame,
details={
"downmix_config": config_idx,
"extension_config_idx": parsed["ext_config_idx"],
"payload": bytes_descriptor(parsed.get("payload", b"")),
})
if diagnostics is not None and parsed.get("objects_clamped"):
diagnostics.append({
"stage": "joc",
"variant": "num_objects_clamped",
"frame": frame,
"message": "对象数超过 15,已截断",
"details": {"declared": parsed["num_objects_bits"] + 1,
"used": parsed["n_objects"]},
})
if transport is not None:
chanmap = transport["chanmap"]
core_locations = transport["core_locations"]
details = {
"config_idx": config_idx,
"n_channels": n_channels,
"syncframes": transport["syncframes"],
}
if n_channels == 7:
if not core_locations or chanmap is None:
raise UnsupportedVariantError(
"eac3", "core_extension_pair",
"7 通道配置需要独立子流与依赖子流 chanmap 同时在场",
frame=frame, details=details)
pair = extra_pair(core_locations, chanmap)
details["chanmap"] = chanmap
details["chanmap_locations"] = expand_chanmap(chanmap)
details["extra_pair"] = list(pair)
if pair != expected_pair:
if diagnostics is not None:
diagnostics.append({
"stage": "eac3",
"variant": "chanmap_cfg_mismatch",
"frame": frame,
"message": "依赖子流 chanmap 声明的额外对与 joc_dmx_config_idx 不一致,"
"按 chanmap 的通道码映射,音频不受影响",
"details": {**details, "expected_pair": list(expected_pair or ())},
})
elif chanmap is not None and extra_pair(core_locations, chanmap):
raise UnsupportedVariantError(
"eac3", "core_channel_count_mismatch",
"5 通道配置的依赖子流声明了额外通道",
frame=frame,
details={**details, "chanmap_locations": expand_chanmap(chanmap)})
# 校验 3:核心通道数(子流声明汇总,去掉 LFE)
declared = set(core_locations)
if chanmap is not None:
declared |= set(expand_chanmap(chanmap))
declared -= {"LFE", "LFE2"}
details["declared_full_band_channels"] = len(declared)
if len(declared) != n_channels:
raise UnsupportedVariantError(
"eac3", "core_channel_count_mismatch",
"子流声明的全频段通道数与 joc_dmx_config_idx 不一致",
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)):
if diagnostics is not None:
diagnostics.append({
"stage": "oamd",
"variant": "bed_mask_cfg_mismatch",
"frame": frame,
"message": "OAMD 标准 bed 掩码声明的额外对与 joc_dmx_config_idx 不一致,"
"只影响 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 ()),
},
})
class DirectPayloadIndex:
"""One-pass in-memory view of contiguous EMDF containers in an E-AC-3 file.
The optional cache directory keeps the existing ``frames.csv + emdf/``
contract, but normal processing reuses the containers already parsed during
the scan instead of reading and parsing thousands of small files again.
音频帧 = 同一 (编码, strmtyp, substreamid) 组合出现一次为止的同步帧组;
JOC 通常只在依赖子流里,核心子流(AC-3 或 E-AC-3 独立子流)没有 EMDF。
"""
def __init__(self, rows, subpayloads, sample_offsets, directory=None):
def __init__(self, rows, subpayloads, sample_offsets, directory=None,
transports=None, diagnostics=None, config_idx=None, n_channels=None):
self.rows = rows
self._subpayloads = subpayloads
self._sample_offsets = sample_offsets
self.directory = Path(directory) if directory is not None else None
self.transports = transports if transports is not None else []
self.diagnostics = diagnostics if diagnostics is not None else []
self.config_idx = config_idx
self.n_channels = n_channels
def transport(self, row):
return self.transports[int(row["frame"])] if self.transports else None
def subpayloads(self, row):
return self._subpayloads[int(row["frame"])]
def subpayload_sample_offset(self, row, payload_id):
offsets = self._sample_offsets[int(row["frame"])]
return 0 if offsets is None else int(offsets.get(payload_id, 0))
@classmethod
def from_eac3(cls, eac3_path, max_frames=None, cache_dir=None):
@@ -34,24 +226,68 @@ class DirectPayloadIndex:
if cache_dir is not None:
emdf_dir = cache_dir / "emdf"
emdf_dir.mkdir(parents=True, exist_ok=True)
data = Path(eac3_path).read_bytes()
rows = []
payloads = []
offsets = []
frames = iter_eac3_frames(Path(eac3_path).read_bytes())
for frame_number, frame in enumerate(frames):
transports = []
diagnostics = []
config_idx = None
n_channels = None
missing_joc_frames = 0
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:
break
try:
container = find_joc_emdf(frame)
except UnsupportedVariantError as exc:
exc.add_context(frame=frame_number, details={"syncframe_bytes": len(frame)})
raise
except EmdfError as exc:
raise UnsupportedVariantError(
"emdf_transport", "container_syntax",
"EMDF 容器语法无法解析",
frame=frame_number,
details={"syncframe_bytes": len(frame), "parser_error": str(exc)}) from exc
transport = _transport_record(group)
transports.append(transport)
container = None
for sync in group:
chunk = data[sync.offset:sync.offset + sync.size]
try:
container = find_joc_emdf(chunk, usable=_joc_pair_score,
diagnostics=diagnostics,
frame_number=frame_number)
except UnsupportedVariantError as exc:
if exc.variant != "no_contiguous_joc_container":
exc.add_context(frame=frame_number,
details={"syncframe_bytes": len(chunk)})
raise
continue
except EmdfError as exc:
raise UnsupportedVariantError(
"emdf_transport", "container_syntax",
"EMDF 容器语法无法解析",
frame=frame_number,
details={"syncframe_bytes": len(chunk),
"parser_error": str(exc)}) from exc
break
if container is None:
missing_joc_frames += 1
rows.append({
"frame": frame_number,
"emdf_hash": "",
"emdf_size": 0,
"emdf_start_bit": "",
"payload_ids": "",
"error": "no_joc",
})
payloads.append(None)
offsets.append(None)
continue
if 14 in container.payloads:
parsed = parse_joc_header(container.payloads[14])
parsed["payload"] = container.payloads[14]
check_cross_layer(parsed, frame=frame_number, transport=transport,
bed_channels=(
program_bed_channels(container.payloads[11])
if 11 in container.payloads else None),
diagnostics=diagnostics)
if config_idx is None:
config_idx = parsed["dmx_config_idx"]
n_channels = parsed["n_channels"]
digest = hashlib.sha256(container.raw).hexdigest()
if emdf_dir is not None:
target = emdf_dir / f"{digest}.bin"
@@ -71,19 +307,27 @@ class DirectPayloadIndex:
print(f"[metadata] {frame_number + 1} frames", flush=True)
if not rows:
raise EmdfError("E-AC-3 输入中没有可处理的同步帧")
if config_idx is None:
raise UnsupportedVariantError(
"emdf_transport", "no_contiguous_joc_container",
"输入中没有找到可连续解析且同时包含 ID11/ID14 的 EMDF 容器",
details={"audio_frames": len(rows), "syncframe_bytes": len(data)})
if missing_joc_frames:
diagnostics.append({
"stage": "eac3",
"variant": "joc_frame_missing",
"frame": None,
"message": "部分音频帧没有 JOC 元数据,这些帧的对象通道静音",
"details": {"frames": missing_joc_frames, "total": len(rows)},
})
if cache_dir is not None:
with (cache_dir / "frames.csv").open("w", encoding="utf-8", newline="") as fp:
fields = ("frame", "emdf_hash", "emdf_size", "emdf_start_bit", "payload_ids", "error")
writer = csv.DictWriter(fp, fieldnames=fields)
writer.writeheader()
writer.writerows(rows)
return cls(rows, payloads, offsets, cache_dir)
def subpayloads(self, row):
return self._subpayloads[int(row["frame"])]
def subpayload_sample_offset(self, row, payload_id):
return int(self._sample_offsets[int(row["frame"])].get(payload_id, 0))
return cls(rows, payloads, offsets, cache_dir, transports, diagnostics,
config_idx, n_channels)
def __len__(self):
return len(self.rows)
@@ -194,6 +438,10 @@ def inspect(index, limit=None, print_frames=False):
"元数据容器无法解析",
frame=frame_number,
details={"exception_type": type(exc).__name__, "parser_error": str(exc)}) from exc
if subs is None:
# 该音频帧没有任何 JOC EMDF(例如编码器未写首帧元数据):对象静音。
frame_records.append({"frame": frame_number, "no_joc": True})
continue
if 11 not in subs or 14 not in subs:
raise UnsupportedVariantError(
"emdf_payloads", "missing_required_payload",
@@ -246,10 +494,10 @@ def inspect(index, limit=None, print_frames=False):
"parser_error": str(exc),
"repair_hint": "检查 JOC header、对象数、参数带、Huffman 或扩展字段",
}) from exc
if parsed["n_channels"] != 5 or parsed["n_objects"] > 15:
if parsed["n_channels"] not in (5, 7):
raise UnsupportedVariantError(
"joc", "unsupported_configuration",
"JOC 核心通道数或对象数超出当前渲染器范围",
"JOC 核心通道数超出当前渲染器范围",
frame=frame_number,
details={
"downmix_config": parsed["dmx_config_idx"],
@@ -258,6 +506,10 @@ def inspect(index, limit=None, print_frames=False):
"extension_config": parsed["ext_config_idx"],
"payload": bytes_descriptor(subs[14]),
})
check_cross_layer(
parsed, frame=frame_number,
transport=(index.transport(row) if hasattr(index, "transport") else None),
bed_channels=program_bed_channels(subs[11]))
rec = _frame_record(frame_number, parsed, oamd)
configs[(parsed["dmx_config_idx"], parsed["ext_config_idx"],
parsed["n_channels"], parsed["n_objects"])] += 1
-293
View File
@@ -1,293 +0,0 @@
"""ctypes bridge for the dependency-free MSVC C++ JOC DSP core.
Metadata parsing intentionally stays in Python. One C call consumes a complete
1536-sample frame, so Python is not involved in the hot 24-timeslot x 15-object
DSP loops.
"""
from __future__ import annotations
import ctypes
import os
from pathlib import Path
import sys
import numpy as np
from evo_unpack import unpack_evolution
from joc_decode import DC_FILTER_DMX_CONFIGS, dequantize, diff_decode, parse_joc
FRAME_SAMPLES = 1536
MAX_OBJECTS = 15
MAX_DPOINTS = 2
CORE_CHANNELS = 5
MAX_BANDS = 23
ABI_VERSION = 1
class NativeBackendUnavailable(RuntimeError):
pass
def native_library_filename():
if sys.platform == "win32":
return "eac3joc_core.dll"
if sys.platform == "darwin":
return "libeac3joc_core.dylib"
if sys.platform.startswith("linux"):
return "libeac3joc_core.so"
raise NativeBackendUnavailable(f"unsupported native platform: {sys.platform}")
def _candidate_libraries():
override = os.environ.get("EAC3JOC_NATIVE_LIBRARY")
if override:
yield Path(override).expanduser()
root = Path(__file__).resolve().parent.parent
yield root / "lib" / native_library_filename()
def find_native_library(explicit=None):
if explicit is not None:
path = Path(explicit).expanduser().resolve()
if not path.is_file():
raise NativeBackendUnavailable(f"native library not found: {path}")
return path
checked = []
for item in _candidate_libraries():
path = item.resolve()
checked.append(str(path))
if path.is_file():
return path
raise NativeBackendUnavailable("native library not found; checked: " + "; ".join(checked))
def default_native_threads():
override = os.environ.get("EAC3JOC_NATIVE_THREADS")
if override is not None:
value = int(override)
if value < 1:
raise ValueError("EAC3JOC_NATIVE_THREADS must be at least 1")
return min(value, MAX_OBJECTS)
return 2 if (os.cpu_count() or 1) >= 4 else 1
def _load_library(path):
lib = ctypes.CDLL(str(path))
float_p = ctypes.POINTER(ctypes.c_float)
u8_p = ctypes.POINTER(ctypes.c_uint8)
double_p = ctypes.POINTER(ctypes.c_double)
lib.ejoc_abi_version.argtypes = []
lib.ejoc_abi_version.restype = ctypes.c_uint32
lib.ejoc_build_info.argtypes = []
lib.ejoc_build_info.restype = ctypes.c_char_p
lib.ejoc_renderer_create.argtypes = []
lib.ejoc_renderer_create.restype = ctypes.c_void_p
lib.ejoc_renderer_destroy.argtypes = [ctypes.c_void_p]
lib.ejoc_renderer_destroy.restype = None
lib.ejoc_renderer_reset.argtypes = [ctypes.c_void_p]
lib.ejoc_renderer_reset.restype = ctypes.c_int
lib.ejoc_renderer_set_threads.argtypes = [ctypes.c_void_p, ctypes.c_uint32]
lib.ejoc_renderer_set_threads.restype = ctypes.c_int
if hasattr(lib, "ejoc_renderer_set_dc_filter"):
lib.ejoc_renderer_set_dc_filter.argtypes = [ctypes.c_void_p, ctypes.c_uint32]
lib.ejoc_renderer_set_dc_filter.restype = ctypes.c_int
lib.ejoc_renderer_thread_count.argtypes = [ctypes.c_void_p]
lib.ejoc_renderer_thread_count.restype = ctypes.c_uint32
lib.ejoc_renderer_last_error.argtypes = [ctypes.c_void_p]
lib.ejoc_renderer_last_error.restype = ctypes.c_char_p
lib.ejoc_renderer_process.argtypes = [
ctypes.c_void_p,
float_p,
float_p,
ctypes.c_uint32,
u8_p,
u8_p,
u8_p,
u8_p,
double_p,
ctypes.c_double,
ctypes.c_float,
ctypes.c_float,
float_p,
]
lib.ejoc_renderer_process.restype = ctypes.c_int
abi = int(lib.ejoc_abi_version())
if abi != ABI_VERSION:
raise NativeBackendUnavailable(f"native ABI mismatch: library={abi}, Python={ABI_VERSION}")
return lib
class NativeJocRenderer:
"""Stateful whole-frame native DSP renderer with the Python renderer API shape."""
def __init__(self, output_scale=1.0, library_path=None, threads=None):
self.output_scale = np.float32(output_scale)
if not np.isfinite(self.output_scale):
raise ValueError("output_scale must be finite")
self.library_path = find_native_library(library_path)
self._lib = _load_library(self.library_path)
self._handle = self._lib.ejoc_renderer_create()
if not self._handle:
raise MemoryError("ejoc_renderer_create failed")
requested_threads = default_native_threads() if threads is None else int(threads)
if requested_threads < 1:
raise ValueError("threads must be at least 1")
result = self._lib.ejoc_renderer_set_threads(self._handle, requested_threads)
if result:
self._raise_native("set_threads", result)
self.threads = int(self._lib.ejoc_renderer_thread_count(self._handle))
self._n_bands = np.zeros(MAX_OBJECTS, dtype=np.uint8)
self._n_dpoints = np.zeros(MAX_OBJECTS, dtype=np.uint8)
self._slope_idx = np.zeros(MAX_OBJECTS, dtype=np.uint8)
self._offset_ts = np.zeros((MAX_OBJECTS, MAX_DPOINTS), dtype=np.uint8)
self._dq = np.zeros(
(MAX_OBJECTS, MAX_DPOINTS, CORE_CHANNELS, MAX_BANDS),
dtype=np.float64,
)
self._output = np.zeros((16, FRAME_SAMPLES), dtype=np.float32)
@property
def build_info(self):
value = self._lib.ejoc_build_info()
return value.decode("utf-8", "replace") if value else ""
@staticmethod
def decode_payload(payload_bytes):
subs, _ = unpack_evolution(payload_bytes, loose=True)
return NativeJocRenderer.decode_subpayloads(subs)
@staticmethod
def decode_subpayloads(subs):
if 14 not in subs:
raise ValueError("EMDF missing ID14/JOC")
out = parse_joc(subs[14])
mix_q = diff_decode(out)
mix_dq = dequantize(out, mix_q)
return out, mix_q, mix_dq
def reset(self):
self._require_open()
result = self._lib.ejoc_renderer_reset(self._handle)
if result:
self._raise_native("reset", result)
def close(self):
handle = getattr(self, "_handle", None)
if handle:
self._lib.ejoc_renderer_destroy(handle)
self._handle = None
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
self.close()
def __del__(self):
try:
self.close()
except Exception:
pass
def _require_open(self):
if not self._handle:
raise RuntimeError("native renderer is closed")
def _raise_native(self, operation, code):
raw = self._lib.ejoc_renderer_last_error(self._handle)
detail = raw.decode("utf-8", "replace") if raw else "unknown native error"
raise RuntimeError(f"native {operation} failed ({code}): {detail}")
def _pack_frame(self, out, mix_dq):
if out["n_channels"] != CORE_CHANNELS:
raise ValueError(f"native core requires 5 JOC channels, got {out['n_channels']}")
if out["n_objects"] > MAX_OBJECTS:
raise ValueError(f"native core supports at most 15 objects, got {out['n_objects']}")
self._n_bands.fill(0)
self._n_dpoints.fill(0)
self._slope_idx.fill(0)
self._offset_ts.fill(0)
mask = 0
for object_index, info in enumerate(out["objs"]):
if not info["present"]:
continue
bands = int(info["n_bands"])
points = int(info["n_dpoints"])
if bands > MAX_BANDS or points > MAX_DPOINTS:
raise ValueError(f"native descriptor out of range: bands={bands}, points={points}")
values = np.asarray(mix_dq[object_index], dtype=np.float64)
expected = (points, CORE_CHANNELS, bands)
if values.shape != expected:
raise ValueError(f"object {object_index} dq shape {values.shape}, expected {expected}")
mask |= 1 << object_index
self._n_bands[object_index] = bands
self._n_dpoints[object_index] = points
self._slope_idx[object_index] = int(info["slope_idx"])
offsets = info.get("offset_ts", ())
self._offset_ts[object_index, :len(offsets)] = offsets
self._dq[object_index, :points, :, :bands] = values
return mask
def _set_dc_filter(self, dmx_config_idx):
"""band-0 的 21-tap DC 补偿只在 downmix 配置 3/4 下启用。"""
enabled = dmx_config_idx in DC_FILTER_DMX_CONFIGS
setter = getattr(self._lib, "ejoc_renderer_set_dc_filter", None)
if setter is None:
if not enabled:
raise RuntimeError(
"native library has no ejoc_renderer_set_dc_filter; rebuild "
"eac3joc_core to disable the band-0 DC filter for "
f"dmx_config_idx={dmx_config_idx}")
return
result = setter(self._handle, 1 if enabled else 0)
if result != 0:
self._raise_native("set_dc_filter", result)
def render_frame(self, payload_bytes, bed5_pcm, lfe_pcm=None):
subs, _ = unpack_evolution(payload_bytes, loose=True)
return self.render_subpayloads(subs, bed5_pcm, lfe_pcm)
def render_subpayloads(self, subs, bed5_pcm, lfe_pcm=None):
self._require_open()
out, _, mix_dq = self.decode_subpayloads(subs)
object_mask = self._pack_frame(out, mix_dq)
self._set_dc_filter(out["dmx_config_idx"])
bed5 = np.ascontiguousarray(bed5_pcm, dtype=np.float32)
if bed5.shape != (CORE_CHANNELS, FRAME_SAMPLES):
raise ValueError(f"core PCM shape must be (5,1536), got {bed5.shape}")
if lfe_pcm is None:
lfe = None
lfe_ptr = ctypes.POINTER(ctypes.c_float)()
else:
lfe = np.ascontiguousarray(lfe_pcm, dtype=np.float32)
if lfe.shape != (FRAME_SAMPLES,):
raise ValueError(f"LFE shape must be (1536,), got {lfe.shape}")
lfe_ptr = lfe.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
result = self._lib.ejoc_renderer_process(
self._handle,
bed5.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
lfe_ptr,
object_mask,
self._n_bands.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)),
self._n_dpoints.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)),
self._slope_idx.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)),
self._offset_ts.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)),
self._dq.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
float(out["clipgain"]),
ctypes.c_float(0.0625),
ctypes.c_float(self.output_scale),
self._output.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
)
if result:
self._raise_native("process", result)
return self._output, None
def native_available(library_path=None):
try:
path = find_native_library(library_path)
lib = _load_library(path)
return True, str(path), (lib.ejoc_build_info() or b"").decode("utf-8", "replace")
except Exception as exc:
return False, None, str(exc)
+33
View File
@@ -458,6 +458,39 @@ 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 的声道标签列表。
跨层一致性校验(规格 §11.2 校验 2)只需要这一段;完整帧解析见
:func:`frame_update`,它还要继续解析全部对象 element。
"""
bits, raw_payload = _payload_bits(bits_one)
try:
version = bits[0] << 1 | bits[1]
except IndexError:
raise UnsupportedVariantError(
"oamd", "header_truncated",
"OAMD payload 不足以容纳 header",
details={"payload_bits": len(bits),
"payload": bytes_descriptor(raw_payload)}) from None
reader = _BitReader(bits, 2)
try:
if version == 3:
version += reader.read(3)
object_count_bits = reader.read(5)
if object_count_bits == 0x1F:
object_count_bits += reader.read(7)
program = _parse_program_assignment(reader, raw_payload)
except UnsupportedVariantError:
raise
except ValueError as exc:
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"]]
def frame_update(bits_one):
"""单帧 OAMD → 位置字段增量及其 sample offset/ramp duration。"""
bits, raw_payload = _payload_bits(bits_one)
+1 -1
View File
@@ -212,7 +212,7 @@ def build_adm_tracks(index, frames=None, rate=48000, frame_samples=1536,
for seq, row in enumerate(frames):
subs = index.subpayloads(row)
timing = None
if 11 in subs:
if subs is not None and 11 in subs:
timing = frame_update(subs[11])
state.apply(timing["values"])
+70 -32
View File
@@ -1,7 +1,7 @@
"""把 E-AC-3 核心 PCM 和 JOC 元数据重建为 LFE 加 15 路对象 PCM。
管线(每帧):
核心 5.1 PCM → QMF 分析 x → 填充器 z = Σ m·x(每对象)
核心 PCM(N=5 或 7 路全频段通道,按 k 查表)→ QMF 分析 x → 填充器 z = Σ m·x(每对象)
→ 对象时域组装(前步、64 点 FFT、旋转、合成窗)→ ×16 + clamp
→ 对象波形 × joc_clipgain
+ LFE 专用 1217-sample 环形延迟
@@ -13,7 +13,7 @@ output_scale:
"""
import numpy as np
from joc_decode import DC_FILTER_DMX_CONFIGS, parse_joc, diff_decode, dequantize
from joc_decode import SIGMA_DMX_CONFIGS, parse_joc, diff_decode, dequantize
from joc_qmf import (N, QMF5_WINDOW, qmf_analysis_frame,
surround_post_frame, interp_matrix)
from evo_unpack import unpack_evolution
@@ -29,10 +29,12 @@ class JocRenderer:
self.output_scale = np.float32(output_scale)
self._prev = {} # 每对象插值 prev(m 的帧末 sb 值)
self._synthesis_state = np.zeros((15, 640), dtype=np.float64)
# 分析 QMF:五路各自保留 9×64 FIFO;L/R/C 另有 10-timeslot 延迟。
self._analysis_fifo = np.zeros((5, 9, 64), dtype=np.float64)
self._analysis_delay = np.zeros((3, 10, 64), dtype=np.float32)
self._analysis_phase = np.float32(0.0625)
# 分析 QMF:最多七路,各保留 9×64 FIFO。L/R/C 与 N=7 时的第 6、7 路
# (额外那一对)在这里做 10 时隙延迟;Ls/Rs 的同一延迟在环绕通路内完成。
self._analysis_fifo = np.zeros((7, 9, 64), dtype=np.float64)
self._analysis_delay = np.zeros((5, 10, 64), dtype=np.float32)
# 哨兵:复位后(或整条流的第一帧)直接用新标量,不做帧内过渡。
self._analysis_phase = np.float32(-0.5)
self._surround_qmf_delay = np.zeros((2, 10, 64), dtype=np.complex128)
self._surround_dc_hist = np.zeros((2, 20), dtype=np.complex128)
# ch0/LFE 使用循环历史,读指针恒落后写指针 1217 个采样。
@@ -41,6 +43,13 @@ class JocRenderer:
self._last_x = None # 仅供逐槽诊断读取,不参与状态推进
self._rot = None # 每带旋转表
def held_analysis_scalar(self):
"""当前分析端标量;还没有过 JOC 帧时用结构常数 1/16。"""
value = self._analysis_phase
if value <= 0.0 and value != 0.0: # 哨兵
return np.float32(0.0625)
return value
@staticmethod
def decode_payload(payload_bytes):
"""id14 载荷 → (parse 结果, mix_q, mix_dq)。"""
@@ -60,36 +69,45 @@ class JocRenderer:
mix_dq = dequantize(out, mix_q)
return out, mix_q, mix_dq
def qmf_x(self, bed5, phase_new=0.0625, apply_dc_filter=True):
"""核心 5ch PCM → 对象矩阵使用的复数 QMF ``x``。
def qmf_x(self, bed_pcm, phase_new=0.0625, apply_dc_filter=True, rotate=True):
"""核心 N ch PCM → 对象矩阵使用的复数 QMF ``x``。
先以 float32 对当前帧应用 phase;phase 变化时仅前 256 个样本从旧值
线性过渡。缩放后的 L/R/C 延迟 10 槽,Ls/Rs 不延迟,再进入分析 QMF。
``apply_dc_filter`` 控制 Ls/Rs band-0 的 21-tap DC 补偿。
phase_new 是分析端标量:结构常数 1/16 × 本帧 joc_clipgain。先以 float32
对当前帧应用它;相比上一帧变化时仅前 256 个样本从旧值线性过渡。缩放后的
L/R/C 与 N=7 时的第 6、7 路延迟 10 槽,Ls/Rs 不延迟,再进入分析 QMF;
出口的 ×16 抵消 1/16,净效果是 ×joc_clipgain。``apply_dc_filter`` 与 ``rotate`` 是规格 §4 的同一个
σ(k) 开关,控制 Ls/Rs 的 band-0 21-tap DC 补偿与 -j 旋转。
"""
pcm = np.asarray(bed5, dtype=np.float32)
if pcm.shape != (5, 1536):
raise ValueError(f"核心 5ch 帧应为 (5,1536),实际 {pcm.shape}")
pcm = np.asarray(bed_pcm, dtype=np.float32)
n_channels = pcm.shape[0] if pcm.ndim == 2 else 0
if n_channels not in (5, 7) or pcm.shape != (n_channels, 1536):
raise ValueError(f"核心帧应为 (5,1536) 或 (7,1536),实际 {pcm.shape}")
new_phase = np.float32(phase_new)
old_phase = self._analysis_phase
if old_phase <= 0.0 and old_phase != 0.0: # 哨兵:首帧/复位后不做过过渡
old_phase = new_phase
gains = np.full(1536, new_phase, dtype=np.float32)
if old_phase != new_phase:
step = np.float32((new_phase - old_phase) / np.float32(256.0))
gains[:256] = old_phase + np.arange(256, dtype=np.float32) * step
scaled = np.multiply(pcm, gains[None, :], dtype=np.float32).reshape(5, 24, 64)
scaled = np.multiply(pcm, gains[None, :], dtype=np.float32).reshape(
n_channels, 24, 64)
# 走外部延迟线的通道:前级 L/R/C,以及 N=7 时额外那一对(索引 5、6)。
plain = [0, 1, 2] + ([5, 6] if n_channels == 7 else [])
blocks = np.empty_like(scaled)
for ch in range(3):
delayed = np.concatenate((self._analysis_delay[ch], scaled[ch]), axis=0)
for lane, ch in enumerate(plain):
delayed = np.concatenate((self._analysis_delay[lane], scaled[ch]), axis=0)
blocks[ch] = delayed[:24]
self._analysis_delay[ch] = delayed[24:]
blocks[3:] = scaled[3:]
self._analysis_delay[lane] = delayed[24:]
blocks[3:5] = scaled[3:5]
x, self._analysis_fifo = qmf_analysis_frame(self._analysis_fifo, blocks)
fifo = self._analysis_fifo[:n_channels]
x, self._analysis_fifo[:n_channels] = qmf_analysis_frame(fifo, blocks)
self._analysis_phase = new_phase
x[3:], self._surround_qmf_delay, self._surround_dc_hist = surround_post_frame(
x[3:], self._surround_qmf_delay, self._surround_dc_hist,
apply_dc_filter=apply_dc_filter)
x[3:5], self._surround_qmf_delay, self._surround_dc_hist = surround_post_frame(
x[3:5], self._surround_qmf_delay, self._surround_dc_hist,
apply_dc_filter=apply_dc_filter, rotate=rotate)
return x
def object_z(self, out, mix_dq, x):
@@ -234,29 +252,49 @@ class JocRenderer:
self._lfe_delay = delayed[-1217:].copy()
return out
def render_frame(self, payload_bytes, bed5_pcm, lfe_pcm=None):
"""payload_bytes = evolution 载荷(含 id14 JOC);bed5_pcm = 5×1536 核心 PCM。
def render_missing_frame(self, bed_pcm, lfe_pcm=None, config_idx=3):
"""没有 JOC 元数据的音频帧:对象通道静音,只保留 LFE 旁通。
QMF 分析与对象合成状态照常推进,否则后续帧的时间轴会整体少一格。
"""
sigma = config_idx in SIGMA_DMX_CONFIGS
# 本帧没有 JOC 元数据,分析端标量保持上一帧的值(槽位不被改写)。
x = self.qmf_x(bed_pcm, phase_new=self.held_analysis_scalar(),
apply_dc_filter=sigma, rotate=sigma)
self._last_x = x.copy()
z_all = {obj: np.zeros((x.shape[1], x.shape[2]), dtype=np.complex128)
for obj in range(15)}
self.dll_synth_objects(z_all)
pcm16 = np.zeros((16, 1536), dtype=np.float64)
if lfe_pcm is not None:
pcm16[0] = self.decode_lfe(lfe_pcm)
pcm16_f32 = np.asarray(pcm16, dtype=np.float32)
return np.multiply(pcm16_f32, self.output_scale, dtype=np.float32), z_all
def render_frame(self, payload_bytes, bed_pcm, lfe_pcm=None):
"""payload_bytes = evolution 载荷(含 id14 JOC);bed_pcm = N×1536 核心 PCM。
lfe_pcm = 1536 核心 LFE;提供时生成 ch0,省略时 ch0 静音
(保留给只验证对象/z 链的诊断脚本)。
返回 (pcm16, z_all):pcm16 = 16×1536(ch0 LFE + 15 对象)f32,
已用 FP32 系数乘 output_scale。"""
subs, _ = unpack_evolution(payload_bytes, loose=True)
return self.render_subpayloads(subs, bed5_pcm, lfe_pcm)
return self.render_subpayloads(subs, bed_pcm, lfe_pcm)
def render_subpayloads(self, subs, bed5_pcm, lfe_pcm=None):
def render_subpayloads(self, subs, bed_pcm, lfe_pcm=None):
"""以已拆出的 EMDF payload 字典渲染一帧,避免绑定 transport 容器。"""
out, mix_q, mix_dq = self.decode_subpayloads(subs)
x = self.qmf_x(
bed5_pcm,
apply_dc_filter=out["dmx_config_idx"] in DC_FILTER_DMX_CONFIGS)
# σ(k) 同时门控 -j 旋转与 band-0 FIR(规格 §4)。
sigma = out["dmx_config_idx"] in SIGMA_DMX_CONFIGS
x = self.qmf_x(bed_pcm, phase_new=0.0625 * out["clipgain"],
apply_dc_filter=sigma, rotate=sigma)
self._last_x = x.copy()
z_all = self.object_z(out, mix_dq, x)
# joc_clipgain 在对象逆 QMF 后应用,并与用户 output_scale 分离。
# joc_clipgain 已在分析端进入 qmf_x,与用户 output_scale 分离。
objs_pcm = self.dll_synth_objects(z_all)
pcm16 = np.zeros((16, 1536), dtype=np.float64)
pcm16[0] = self.decode_lfe(lfe_pcm) if lfe_pcm is not None else 0.0
for obj, p in objs_pcm.items():
pcm16[obj + 1] = p * out["clipgain"]
pcm16[obj + 1] = p
pcm16_f32 = np.asarray(pcm16, dtype=np.float32)
return np.multiply(pcm16_f32, self.output_scale, dtype=np.float32), z_all
+32 -73
View File
@@ -15,7 +15,6 @@ from pathlib import Path
import numpy as np
from binaural_metadata import OamdPositionTimeline
from binaural_native_renderer import NativeBinauralDsp
from rosella_core import RosellaRenderer
from rosella_direct import BINAURAL_PROFILE_NAMES
from rosella_filterbank import (
@@ -72,9 +71,7 @@ class RosellaBinauralRenderer:
tail_seconds: float = 5.0,
output_gain: float = 1.0,
chunk_frames: int = 64,
room_impulse_slots: int = 4096,
backend: str = "python",
native_library=None):
room_impulse_slots: int = 4096):
if mode not in BINAURAL_PROFILE_NAMES:
raise ValueError("binaural mode must be near, mid, or far")
if int(object_delay_samples) < 0:
@@ -85,8 +82,6 @@ class RosellaBinauralRenderer:
raise ValueError("chunk_frames must be positive")
if not math.isfinite(float(output_gain)):
raise ValueError("output_gain must be finite")
if backend not in ("auto", "native", "python"):
raise ValueError("backend must be auto, native, or python")
if isinstance(personalized_headphone, RosellaModel):
self.model = personalized_headphone
@@ -108,34 +103,14 @@ class RosellaBinauralRenderer:
self.chunk_frames = int(chunk_frames)
self.chunk_samples = self.chunk_frames * FRAME_SAMPLES
self.native_dsp = None
self.backend_fallback = None
if backend in ("auto", "native"):
try:
self.native_dsp = NativeBinauralDsp(
self.model, library_path=native_library,
kernel_data=self.kernel_data)
except (AttributeError, OSError, RuntimeError) as exc:
if backend == "native":
raise RuntimeError(f"native binaural backend unavailable: {exc}") from exc
self.backend_fallback = str(exc)
if self.native_dsp is not None:
self.dsp_backend = "native"
self.qmf_analysis = None
self.hybrid_analysis = None
self.hybrid_synthesis = None
self.qmf_synthesis = None
self.core = RosellaRenderer(
self.model, SOURCE_CHANNELS, create_room=False)
else:
self.dsp_backend = "python"
self.qmf_analysis = QmfAnalysis(SOURCE_CHANNELS, self.kernel_data)
self.hybrid_analysis = HybridAnalysis(SOURCE_CHANNELS, self.kernel_data)
self.core = RosellaRenderer(
self.model, SOURCE_CHANNELS,
room_impulse_slots=room_impulse_slots)
self.hybrid_synthesis = HybridSynthesis(OUTPUT_CHANNELS, self.kernel_data)
self.qmf_synthesis = QmfSynthesis(OUTPUT_CHANNELS, self.kernel_data)
self.dsp_backend = "python"
self.qmf_analysis = QmfAnalysis(SOURCE_CHANNELS, self.kernel_data)
self.hybrid_analysis = HybridAnalysis(SOURCE_CHANNELS, self.kernel_data)
self.core = RosellaRenderer(
self.model, SOURCE_CHANNELS,
room_impulse_slots=room_impulse_slots)
self.hybrid_synthesis = HybridSynthesis(OUTPUT_CHANNELS, self.kernel_data)
self.qmf_synthesis = QmfSynthesis(OUTPUT_CHANNELS, self.kernel_data)
self.timeline = OamdPositionTimeline(15)
self._input_buffer = np.empty(
@@ -210,40 +185,29 @@ class RosellaBinauralRenderer:
blocks = len(values) // ROSSELLA_BLOCK_SAMPLES
block_base = self.processed_input_samples
if self.native_dsp is not None:
stereo = np.empty((len(values), OUTPUT_CHANNELS), dtype=np.float64)
for block in range(blocks):
sample = block_base + block * ROSSELLA_BLOCK_SAMPLES
self._set_block_parameters(sample)
start = block * ROSSELLA_BLOCK_SAMPLES
stop = start + ROSSELLA_BLOCK_SAMPLES
stereo[start:stop] = self.native_dsp.process_block(
values[start:stop], self.core.gains, self.core.room_sends,
self.output_gain)
else:
hops = values.reshape(
blocks, ROSSELLA_BLOCK_SAMPLES // QMF_HOP_SAMPLES,
QMF_HOP_SAMPLES, SOURCE_CHANNELS,
).transpose(0, 1, 3, 2).reshape(
blocks * (ROSSELLA_BLOCK_SAMPLES // QMF_HOP_SAMPLES),
SOURCE_CHANNELS, QMF_HOP_SAMPLES)
hybrid = self.hybrid_analysis.process_chunk(
self.qmf_analysis.process_chunk(hops))
direct = np.empty((blocks * 8, OUTPUT_CHANNELS, 77), dtype=np.complex128)
room_send = np.empty((blocks * 8, 77), dtype=np.complex128)
for block in range(blocks):
sample = block_base + block * ROSSELLA_BLOCK_SAMPLES
self._set_block_parameters(sample)
start = block * 8
stop = start + 8
direct[start:stop], room_send[start:stop] = (
self.core.direct_and_send_static(hybrid[start:stop]))
rendered = direct + self.core.room.process_chunk(room_send)
time_bands = self.qmf_synthesis.process_chunk(
self.hybrid_synthesis.process_chunk(rendered))
stereo = time_bands.transpose(0, 2, 1).reshape(
blocks * ROSSELLA_BLOCK_SAMPLES, OUTPUT_CHANNELS)
stereo *= self.output_gain
hops = values.reshape(
blocks, ROSSELLA_BLOCK_SAMPLES // QMF_HOP_SAMPLES,
QMF_HOP_SAMPLES, SOURCE_CHANNELS,
).transpose(0, 1, 3, 2).reshape(
blocks * (ROSSELLA_BLOCK_SAMPLES // QMF_HOP_SAMPLES),
SOURCE_CHANNELS, QMF_HOP_SAMPLES)
hybrid = self.hybrid_analysis.process_chunk(
self.qmf_analysis.process_chunk(hops))
direct = np.empty((blocks * 8, OUTPUT_CHANNELS, 77), dtype=np.complex128)
room_send = np.empty((blocks * 8, 77), dtype=np.complex128)
for block in range(blocks):
sample = block_base + block * ROSSELLA_BLOCK_SAMPLES
self._set_block_parameters(sample)
start = block * 8
stop = start + 8
direct[start:stop], room_send[start:stop] = (
self.core.direct_and_send_static(hybrid[start:stop]))
rendered = direct + self.core.room.process_chunk(room_send)
time_bands = self.qmf_synthesis.process_chunk(
self.hybrid_synthesis.process_chunk(rendered))
stereo = time_bands.transpose(0, 2, 1).reshape(
blocks * ROSSELLA_BLOCK_SAMPLES, OUTPUT_CHANNELS)
stereo *= self.output_gain
skip = max(0, min(
len(stereo), ROSSELLA_LATENCY_SAMPLES - self.raw_output_samples))
@@ -278,8 +242,6 @@ class RosellaBinauralRenderer:
return np.concatenate(nonempty, axis=0)
def close(self):
if self.native_dsp is not None:
self.native_dsp.close()
self.finished = True
@property
@@ -287,9 +249,6 @@ class RosellaBinauralRenderer:
return {
"name": self.dsp_backend,
"precision": "float64/complex128",
"fallback_reason": self.backend_fallback,
"library": (str(self.native_dsp.library_path)
if self.native_dsp is not None else None),
"model": str(self.model_path.resolve()),
"model_coefficients": int(len(self.model.coefficients)),
"model_coefficient_sha256": self.model.coefficient_sha256,
-398
View File
@@ -1,398 +0,0 @@
"""Native float64 SOFA binaural DSP (ctypes bridge to eac3joc_core).
The C++ side mirrors the Python :class:`sofa_binaural_backend.SofaBinauralBackend`
mathematics: 64-QMF/77-hybrid analysis and synthesis, fifth-order ACN/N3D real
spherical-harmonic field evaluation, whole-QMF-slot per-object delay histories,
six image-source early reflections, the shared unitary FDN late room, the LFE
low-pass and the 961-sample latency policy. The compiled HRTF field, the
filterbank tables and the room constants are uploaded once; per 512-sample
block the adapter updates every source and streams PCM through the DLL.
"""
from __future__ import annotations
import ctypes
import math
from pathlib import Path
import numpy as np
from native_renderer import ABI_VERSION, find_native_library
from public_filterbank import DEFAULT_FILTERBANK_DATA, load_filterbank_tables
from public_room import LateFdnConfig, SharedUnitaryFdn, ShoeboxRoomConfig
from reference_distance import ReferenceDistanceProfileV1
from sofa_binaural_backend import SofaBinauralBackend
from sofa_hrtf_field import (
DEFAULT_HRTF_CACHE_DIR,
SofaHrtfField,
compile_sofa_hrtf,
)
BLOCK_SAMPLES = 512
INPUT_CHANNELS = 16
OUTPUT_CHANNELS = 2
QMF_HOP = 64
LATENCY_SAMPLES = 961
_PROFILE_INDEX = {"near": 0, "mid": 1, "far": 2}
def _room_numbers(fdn_config: LateFdnConfig) -> dict:
"""Derive the FDN delays/feedback with the same arithmetic as the Python room."""
fdn = SharedUnitaryFdn(fdn_config)
return {
"fdn_delays": np.asarray(fdn.delays, dtype=np.uint32),
"fdn_feedback": np.asarray(fdn.feedback_gain, dtype=np.float64),
"damping": fdn.damping,
"output_gain": fdn.output_gain,
"allpass_delays": np.asarray(
[diffuser.delay_samples for diffuser in fdn.diffusers], dtype=np.uint32),
"allpass_gains": np.asarray(fdn_config.allpass_gain, dtype=np.float64),
"tail_samples": fdn.tail_samples,
}
class NativeSofaBinauralDsp:
"""Duck-type compatible with SofaBinauralBackend for the JOC adapter."""
def __init__(
self,
field: SofaHrtfField,
*,
source_count: int = INPUT_CHANNELS,
default_profile: str = "mid",
enable_early_reflections: bool = True,
enable_late_room: bool = True,
room_config: ShoeboxRoomConfig = ShoeboxRoomConfig(),
fdn_config: LateFdnConfig | None = None,
library_path: str | Path | None = None):
if not isinstance(field, SofaHrtfField):
raise TypeError("field must be SofaHrtfField")
self.source_count = int(source_count)
self.default_profile = ReferenceDistanceProfileV1.validate_profile(default_profile)
self.enable_early_reflections = bool(enable_early_reflections)
self.enable_late_room = bool(enable_late_room)
self.field = field
if self.source_count != INPUT_CHANNELS:
raise ValueError(f"native SOFA backend requires {INPUT_CHANNELS} sources")
if abs(self.field.sample_rate_hz - 48000.0) > 1.0e-9:
raise ValueError("the native SOFA binaural runtime requires 48 kHz")
self.room_config = room_config
self.room_config.validate()
self.dsp_backend = "native-sofa"
self.hrtf_input_kind = "field"
self.hrtf_input_path = None
self.cache_policy = None
self.library_path = find_native_library(library_path)
self._lib = ctypes.CDLL(str(self.library_path))
self._bind()
version = int(self._lib.ejoc_abi_version())
if version != ABI_VERSION:
raise RuntimeError(
f"native ABI mismatch: expected {ABI_VERSION}, got {version}")
self._handle = self._lib.ejoc_sofa_binaural_create()
if not self._handle:
raise RuntimeError("native SOFA binaural renderer creation failed")
try:
self._configure_kernels()
self._configure_field()
self._configure_room(fdn_config)
except Exception:
self.close()
raise
self.positions = np.zeros((self.source_count, 3), dtype=np.float64)
self.positions[:, 1] = 1.0
self.profiles = [self.default_profile] * self.source_count
self.user_gain = np.ones(self.source_count, dtype=np.float64)
self.special_lfe = np.zeros(self.source_count, dtype=bool)
self.parameter_updates = 0
self.finished = False
for source in range(self.source_count):
self.set_source(
source, self.positions[source], profile=self.default_profile,
fade=False)
def _bind(self):
void_p = ctypes.c_void_p
f64_p = ctypes.POINTER(ctypes.c_double)
i16_p = ctypes.POINTER(ctypes.c_int16)
u32_p = ctypes.POINTER(ctypes.c_uint32)
self._lib.ejoc_abi_version.argtypes = []
self._lib.ejoc_abi_version.restype = ctypes.c_uint32
self._lib.ejoc_sofa_binaural_create.argtypes = []
self._lib.ejoc_sofa_binaural_create.restype = void_p
self._lib.ejoc_sofa_binaural_destroy.argtypes = [void_p]
self._lib.ejoc_sofa_binaural_destroy.restype = None
self._lib.ejoc_sofa_binaural_reset.argtypes = [void_p]
self._lib.ejoc_sofa_binaural_reset.restype = ctypes.c_int
self._lib.ejoc_sofa_binaural_last_error.argtypes = [void_p]
self._lib.ejoc_sofa_binaural_last_error.restype = ctypes.c_char_p
self._lib.ejoc_sofa_binaural_configure_kernels.argtypes = [
void_p, f64_p, f64_p, i16_p, f64_p, ctypes.c_uint32, f64_p, f64_p]
self._lib.ejoc_sofa_binaural_configure_kernels.restype = ctypes.c_int
self._lib.ejoc_sofa_binaural_configure_field.argtypes = [
void_p, f64_p, f64_p, f64_p, f64_p, ctypes.c_double]
self._lib.ejoc_sofa_binaural_configure_field.restype = ctypes.c_int
self._lib.ejoc_sofa_binaural_configure_room.argtypes = [
void_p, f64_p, f64_p, f64_p, ctypes.c_double, u32_p, f64_p,
ctypes.c_double, ctypes.c_double, u32_p, f64_p,
ctypes.c_uint32, ctypes.c_uint32]
self._lib.ejoc_sofa_binaural_configure_room.restype = ctypes.c_int
self._lib.ejoc_sofa_binaural_set_source.argtypes = [
void_p, ctypes.c_uint32, f64_p, ctypes.c_uint32, ctypes.c_double,
ctypes.c_uint32, ctypes.c_uint32, ctypes.c_uint32]
self._lib.ejoc_sofa_binaural_set_source.restype = ctypes.c_int
self._lib.ejoc_sofa_binaural_process.argtypes = [
void_p, f64_p, ctypes.c_uint32, ctypes.c_double, f64_p]
self._lib.ejoc_sofa_binaural_process.restype = ctypes.c_int
self._lib.ejoc_sofa_binaural_finish.argtypes = [
void_p, ctypes.c_uint32, f64_p, ctypes.c_uint32]
self._lib.ejoc_sofa_binaural_finish.restype = ctypes.c_int
def _raise(self, operation, status):
message = self._lib.ejoc_sofa_binaural_last_error(self._handle)
detail = (message or b"").decode("utf-8", "replace")
raise RuntimeError(
f"native SOFA binaural renderer {operation} failed ({status}): {detail}")
@staticmethod
def _f64_pointer(values):
return values.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
def _configure_kernels(self):
tables = load_filterbank_tables(DEFAULT_FILTERBANK_DATA)
qmf_analysis = np.ascontiguousarray(
tables["qmf_analysis_coefficients"], dtype=np.float64)
hybrid_low = np.ascontiguousarray(
tables["hybrid_analysis_low_kernel"], dtype=np.float64)
hybrid_indices = np.ascontiguousarray(
tables["hybrid_synthesis_indices"], dtype=np.int16)
hybrid_values = np.ascontiguousarray(
tables["hybrid_synthesis_values"], dtype=np.float64)
qmf_basis = np.ascontiguousarray(
tables["qmf_synthesis_basis"], dtype=np.float64)
qmf_taps = np.ascontiguousarray(
tables["qmf_synthesis_taps"], dtype=np.float64)
status = self._lib.ejoc_sofa_binaural_configure_kernels(
self._handle,
self._f64_pointer(qmf_analysis),
self._f64_pointer(hybrid_low),
hybrid_indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int16)),
self._f64_pointer(hybrid_values),
len(hybrid_indices),
self._f64_pointer(qmf_basis),
self._f64_pointer(qmf_taps))
if status:
self._raise("configure_kernels", status)
self._keepalive = (qmf_analysis, hybrid_low, hybrid_indices,
hybrid_values, qmf_basis, qmf_taps)
def _configure_field(self):
coefficients = np.ascontiguousarray(
self.field.coefficients, dtype=np.complex128).view(np.float64)
delay_coefficients = np.ascontiguousarray(
self.field.delay_coefficients, dtype=np.float64)
delay_bounds = np.ascontiguousarray(
self.field.delay_bounds, dtype=np.float64)
centers = np.ascontiguousarray(
self.field.band_center_frequencies_hz, dtype=np.float64)
status = self._lib.ejoc_sofa_binaural_configure_field(
self._handle,
self._f64_pointer(coefficients),
self._f64_pointer(delay_coefficients),
self._f64_pointer(delay_bounds),
self._f64_pointer(centers),
float(self.field.measurement_radius_m))
if status:
self._raise("configure_field", status)
def _configure_room(self, fdn_config: LateFdnConfig | None):
actual = fdn_config or LateFdnConfig(sample_rate_hz=48000.0)
numbers = _room_numbers(actual)
dims = np.asarray(self.room_config.dimensions_m, dtype=np.float64)
listener = np.asarray(self.room_config.listener_position_m, dtype=np.float64)
walls = np.asarray(self.room_config.wall_reflection_gain, dtype=np.float64)
status = self._lib.ejoc_sofa_binaural_configure_room(
self._handle,
self._f64_pointer(dims),
self._f64_pointer(listener),
self._f64_pointer(walls),
float(self.room_config.speed_of_sound_m_s),
numbers["fdn_delays"].ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)),
self._f64_pointer(numbers["fdn_feedback"]),
float(numbers["damping"]),
float(numbers["output_gain"]),
numbers["allpass_delays"].ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)),
self._f64_pointer(numbers["allpass_gains"]),
1 if self.enable_early_reflections else 0,
1 if self.enable_late_room else 0)
if status:
self._raise("configure_room", status)
self._fdn = SharedUnitaryFdn(actual)
self._fdn_tail_samples = numbers["tail_samples"]
def set_source(self, source: int, position_adm, *, profile: str | None = None,
gain: float = 1.0, enabled: bool = True,
special_lfe: bool = False, fade: bool = True) -> None:
if self.finished:
raise RuntimeError("SOFA renderer is finished")
source = int(source)
if not 0 <= source < self.source_count:
raise IndexError(source)
name = self.default_profile if profile is None else profile
position = np.asarray(position_adm, dtype=np.float64)
if position.shape != (3,):
raise ValueError("ADM position must contain three Cartesian values")
status = self._lib.ejoc_sofa_binaural_set_source(
self._handle, source,
self._f64_pointer(np.ascontiguousarray(position)),
_PROFILE_INDEX[ReferenceDistanceProfileV1.validate_profile(name)],
float(gain), 1 if enabled else 0, 1 if special_lfe else 0,
1 if fade else 0)
if status:
self._raise("set_source", status)
self.positions[source] = position
self.profiles[source] = name
self.user_gain[source] = float(gain)
self.special_lfe[source] = bool(special_lfe)
self.parameter_updates += 1
def process(self, sources) -> np.ndarray:
if self.finished:
raise RuntimeError("SOFA renderer is finished")
values = np.ascontiguousarray(sources, dtype=np.float64)
if values.ndim != 2 or values.shape[1] != self.source_count:
raise ValueError(f"sources must have shape [samples,{self.source_count}]")
if len(values) % QMF_HOP or len(values) > BLOCK_SAMPLES:
raise ValueError("native SOFA backend input must be a 64-aligned block")
output = np.empty((len(values), OUTPUT_CHANNELS), dtype=np.float64)
count = self._lib.ejoc_sofa_binaural_process(
self._handle, self._f64_pointer(values), len(values), 1.0,
self._f64_pointer(output))
if count < 0:
self._raise("process", count)
return output[:count]
def finish(self, *, tail_seconds: float | None = None) -> np.ndarray:
if self.finished:
return np.zeros((0, OUTPUT_CHANNELS), dtype=np.float64)
if tail_seconds is not None and (
not math.isfinite(float(tail_seconds)) or float(tail_seconds) < 0.0):
raise ValueError("tail_seconds must be finite and non-negative")
flush = self.finish_output_capacity(tail_seconds)
pieces = []
remaining = flush
while remaining > 0:
chunk = min(BLOCK_SAMPLES, remaining)
output = np.empty((chunk, OUTPUT_CHANNELS), dtype=np.float64)
count = self._lib.ejoc_sofa_binaural_finish(
self._handle, chunk, self._f64_pointer(output), chunk)
if count < 0:
self._raise("finish", count)
pieces.append(output[:count])
remaining -= chunk
self.finished = True
nonempty = [piece for piece in pieces if len(piece)]
if not nonempty:
return np.zeros((0, OUTPUT_CHANNELS), dtype=np.float64)
return np.concatenate(nonempty, axis=0)
def finish_output_capacity(self, tail_seconds: float | None = None) -> int:
if tail_seconds is not None and (
not math.isfinite(float(tail_seconds)) or float(tail_seconds) < 0.0):
raise ValueError("tail_seconds must be finite and non-negative")
requested = (self._fdn_tail_samples if tail_seconds is None
else int(math.ceil(float(tail_seconds) * 48000.0)))
maximum_hrtf = float(np.max(self.field.delay_bounds[:, 1], initial=0.0))
hrtf_slots = int(math.ceil(maximum_hrtf / QMF_HOP))
hrtf_bound = hrtf_slots * QMF_HOP
early_bound = hrtf_bound + 2048
if self.enable_early_reflections:
early_bound += 256 * QMF_HOP
drain = max(requested if self.enable_late_room else 0, early_bound)
drain += LATENCY_SAMPLES
return int(math.ceil(drain / QMF_HOP) * QMF_HOP)
def reset(self) -> None:
if self._lib.ejoc_sofa_binaural_reset(self._handle):
self._raise("reset", -1)
self.finished = False
for source in range(self.source_count):
self.set_source(
source, self.positions[source], profile=self.profiles[source],
gain=float(self.user_gain[source]),
special_lfe=bool(self.special_lfe[source]), fade=False)
def info(self) -> dict:
return {
"name": "NativeSofaBinauralDsp",
"source_count": self.source_count,
"sample_rate_hz": 48000.0,
"precision": "float64/complex128",
"signal_path": (
"native 64-QMF -> native 77-hybrid -> SOFA order-5 real-SH "
"direct/early -> native synthesis + shared unitary FDN"),
"hrtf_input_kind": self.hrtf_input_kind,
"hrtf_input_path": self.hrtf_input_path,
"cache_policy": self.cache_policy,
"latency_compensated_samples": LATENCY_SAMPLES,
"enable_early_reflections": self.enable_early_reflections,
"enable_late_room": self.enable_late_room,
"early_history_slots": 256,
"hrtf_history_slots": int(math.ceil(
float(np.max(self.field.delay_bounds[:, 1], initial=0.0)) / QMF_HOP)),
"maximum_hrtf_delay_samples": float(
np.max(self.field.delay_bounds[:, 1], initial=0.0)),
"parameter_updates": self.parameter_updates,
"distance": ReferenceDistanceProfileV1.info(),
"field": self.field.info(),
"library_path": str(self.library_path),
"native_backend": True,
}
def close(self) -> None:
handle = getattr(self, "_handle", None)
if handle:
self._lib.ejoc_sofa_binaural_destroy(handle)
self._handle = None
self.finished = True
def create_native_sofa_renderer(
sofa, *, mode="mid", cache_policy="memory", cache_dir=None,
shell_radius_m=1.0, object_delay_samples=1473, tail_seconds=5.0,
output_gain=1.0, chunk_frames=64):
"""Compile a SOFA source and build a JOC adapter over the native DSP."""
from binaural_renderer import SofaBinauralRenderer, resolve_sofa_hrtf
source = resolve_sofa_hrtf(sofa)
field = compile_sofa_hrtf(
source,
shell_radius_m=shell_radius_m,
cache_policy=cache_policy,
cache_dir=cache_dir)
backend = NativeSofaBinauralDsp(field, default_profile=mode)
backend.hrtf_input_kind = "sofa"
backend.hrtf_input_path = str(source)
backend.cache_policy = str(cache_policy).lower()
return SofaBinauralRenderer(
backend, mode=mode, object_delay_samples=object_delay_samples,
tail_seconds=tail_seconds, chunk_frames=chunk_frames)
def create_native_compiled_cache_renderer(
cache, *, mode="mid", object_delay_samples=1473, tail_seconds=5.0,
output_gain=1.0, chunk_frames=64):
"""Load a compiled cache and build a JOC adapter over the native DSP."""
from binaural_renderer import SofaBinauralRenderer, resolve_compiled_hrtf_cache
source = resolve_compiled_hrtf_cache(cache)
field = SofaHrtfField.load(source)
backend = NativeSofaBinauralDsp(field, default_profile=mode)
backend.hrtf_input_kind = "compiled_cache"
backend.hrtf_input_path = str(source)
backend.cache_policy = None
return SofaBinauralRenderer(
backend, mode=mode, object_delay_samples=object_delay_samples,
tail_seconds=tail_seconds, chunk_frames=chunk_frames)
+3 -22
View File
@@ -1,4 +1,4 @@
"""Unified auto/native/python entry point for float64 speaker rendering."""
"""Float64 speaker rendering entry point."""
from __future__ import annotations
import time
@@ -6,39 +6,21 @@ import time
import numpy as np
from speaker_layouts import SpeakerLayout, get_speaker_layout
from speaker_native_renderer import NativeSpeakerRenderer
from speaker_renderer import PythonSpeakerRenderer, payload_events_from_index, render_objects16
FRAME_SAMPLES = 1536
def create_speaker_renderer(layout: str | SpeakerLayout, *, backend="auto", native_library=None):
def create_speaker_renderer(layout: str | SpeakerLayout):
"""Create a stateful speaker renderer and return ``(renderer, info)``."""
if backend not in ("auto", "native", "python"):
raise ValueError(f"unknown speaker backend: {backend}")
target = get_speaker_layout(layout) if isinstance(layout, str) else layout
fallback_reason = None
if backend in ("auto", "native"):
try:
renderer = NativeSpeakerRenderer(target, native_library)
return renderer, {
"name": "native",
"layout": target.name,
"library": str(renderer.library_path),
"fallback_reason": None,
}
except (OSError, RuntimeError) as exc:
fallback_reason = str(exc)
return PythonSpeakerRenderer(target), {
"name": "python",
"layout": target.name,
"library": None,
"fallback_reason": fallback_reason,
}
def render_speaker_layout(objects16, index, layout: str | SpeakerLayout, *,
backend="auto", native_library=None,
metadata_offset=1473):
"""Render a complete indexed object stream and return ``(pcm64, info)``."""
target = get_speaker_layout(layout) if isinstance(layout, str) else layout
@@ -51,8 +33,7 @@ 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, backend=backend, native_library=native_library)
renderer, info = create_speaker_renderer(target)
output = np.empty((len(source), target.channel_count), dtype=np.float64)
started = time.perf_counter()
try:
-154
View File
@@ -1,154 +0,0 @@
"""ctypes bridge for the float64 native object-to-speaker renderer."""
from __future__ import annotations
import ctypes
from pathlib import Path
import numpy as np
from native_renderer import ABI_VERSION, find_native_library
from oamd_bits import JocFieldState, frame_update
from speaker_layouts import SpeakerLayout, get_speaker_layout
FRAME_SAMPLES = 1536
INPUT_CHANNELS = 16
OBJECTS = 15
COORDINATES = 3
class NativeSpeakerRenderer:
def __init__(self, layout: str | SpeakerLayout, library_path=None):
self.layout = get_speaker_layout(layout) if isinstance(layout, str) else layout
self.library_path = find_native_library(library_path)
self._lib = ctypes.CDLL(str(self.library_path))
self._bind()
version = int(self._lib.ejoc_abi_version())
if version != ABI_VERSION:
raise RuntimeError(f"native ABI mismatch: expected {ABI_VERSION}, got {version}")
channels = int(self._lib.ejoc_speaker_layout_channel_count(self.layout.speaker_bitfield))
if channels != self.layout.channel_count:
raise RuntimeError(
f"native layout channel count mismatch: expected {self.layout.channel_count}, got {channels}"
)
self._handle = self._lib.ejoc_speaker_renderer_create(self.layout.speaker_bitfield)
if not self._handle:
raise RuntimeError(f"native speaker renderer rejected mask 0x{self.layout.speaker_bitfield:X}")
self._state = JocFieldState()
def _bind(self):
void_p = ctypes.c_void_p
self._lib.ejoc_abi_version.argtypes = []
self._lib.ejoc_abi_version.restype = ctypes.c_uint32
self._lib.ejoc_speaker_layout_channel_count.argtypes = [ctypes.c_uint32]
self._lib.ejoc_speaker_layout_channel_count.restype = ctypes.c_uint32
self._lib.ejoc_speaker_renderer_create.argtypes = [ctypes.c_uint32]
self._lib.ejoc_speaker_renderer_create.restype = void_p
self._lib.ejoc_speaker_renderer_destroy.argtypes = [void_p]
self._lib.ejoc_speaker_renderer_destroy.restype = None
self._lib.ejoc_speaker_renderer_reset.argtypes = [void_p]
self._lib.ejoc_speaker_renderer_reset.restype = ctypes.c_int
self._lib.ejoc_speaker_renderer_last_error.argtypes = [void_p]
self._lib.ejoc_speaker_renderer_last_error.restype = ctypes.c_char_p
self._lib.ejoc_speaker_renderer_process.argtypes = [
void_p,
ctypes.POINTER(ctypes.c_float),
ctypes.c_uint32,
ctypes.c_uint32,
ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(ctypes.c_uint32),
ctypes.POINTER(ctypes.c_uint16),
ctypes.POINTER(ctypes.c_uint8),
ctypes.POINTER(ctypes.c_uint8),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_double),
]
self._lib.ejoc_speaker_renderer_process.restype = ctypes.c_int
def _raise(self, operation, status):
message = self._lib.ejoc_speaker_renderer_last_error(self._handle)
detail = (message or b"").decode("utf-8", "replace")
raise RuntimeError(f"native speaker renderer {operation} failed ({status}): {detail}")
def reset(self):
if not self._handle:
raise RuntimeError("native speaker renderer is closed")
status = self._lib.ejoc_speaker_renderer_reset(self._handle)
if status:
self._raise("reset", status)
self._state = JocFieldState()
def close(self):
if self._handle:
self._lib.ejoc_speaker_renderer_destroy(self._handle)
self._handle = None
def __enter__(self):
return self
def __exit__(self, exc_type, exc, tb):
self.close()
def __del__(self):
try:
self.close()
except Exception:
pass
def _metadata_arrays(self, events):
events = list(events)
count = len(events)
if not count:
return count, None, None, None
offsets = np.empty(count, dtype=np.uint32)
ramps = np.empty(count, dtype=np.uint32)
positions = np.empty((count, OBJECTS, COORDINATES), dtype=np.uint16)
for event_index, (outer_offset, payload) in enumerate(events):
update = frame_update(payload)
self._state.apply(update["values"])
offsets[event_index] = int(outer_offset) + int(update["block_offset_samples"])
ramps[event_index] = int(update["ramp_duration_samples"])
for object_index in range(1, OBJECTS + 1):
positions[event_index, object_index - 1] = (
self._state.q[(object_index, "q1")],
self._state.q[(object_index, "q2")],
self._state.q[(object_index, "q3")],
)
return count, offsets, ramps, positions
def process(self, objects16, events=()):
if not self._handle:
raise RuntimeError("native speaker renderer is closed")
source = np.ascontiguousarray(objects16, dtype=np.float32)
if source.ndim != 2 or source.shape[1] != INPUT_CHANNELS:
raise ValueError(f"objects16 must have shape [samples,16], got {source.shape}")
if len(source) % 32:
raise ValueError("sample count must be a multiple of 32")
count, offsets, ramps, positions = self._metadata_arrays(events)
output = np.empty((len(source), self.layout.channel_count), dtype=np.float64)
null_u32 = ctypes.POINTER(ctypes.c_uint32)()
null_u16 = ctypes.POINTER(ctypes.c_uint16)()
null_u8 = ctypes.POINTER(ctypes.c_uint8)()
null_f64 = ctypes.POINTER(ctypes.c_double)()
status = self._lib.ejoc_speaker_renderer_process(
self._handle,
source.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
len(source),
count,
offsets.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)) if count else null_u32,
ramps.ctypes.data_as(ctypes.POINTER(ctypes.c_uint32)) if count else null_u32,
positions.ctypes.data_as(ctypes.POINTER(ctypes.c_uint16)) if count else null_u16,
null_u8,
null_u8,
null_f64,
output.ctypes.data_as(ctypes.POINTER(ctypes.c_double)),
)
if status:
self._raise("process", status)
return output
def render_frame(self, objects16, payload=None, metadata_offset=1473):
source = np.asarray(objects16)
if source.shape != (FRAME_SAMPLES, INPUT_CHANNELS):
raise ValueError(f"frame must have shape (1536,16), got {source.shape}")
events = () if payload is None else ((int(metadata_offset), bytes(payload)),)
return self.process(source, events)
+1 -1
View File
@@ -190,7 +190,7 @@ def _all_object_targets(layout: SpeakerLayout, state: JocFieldState) -> np.ndarr
class PythonSpeakerRenderer:
"""Stateful float64 speaker renderer with the same frame API as native."""
"""Stateful float64 speaker renderer."""
def __init__(self, layout: str | SpeakerLayout, block_size: int = 32):
self.layout = get_speaker_layout(layout) if isinstance(layout, str) else layout