FIR DSP 408: own memory layout and FIR tab (filters, coefficient files, tap budget)

Layout and commands derived from the behaviour of its control software, tested
against the simulator only. No GEQ on this model; dynamic EQs are left untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
GivouandClaude Opus 5.5 committed 2026-11-02 17:11:46 +01:00
1 parent f7d5e22bb0
commit 5597fae3f3
13 files changed
+622 -35

No files matched your search

+34 -1
View File
@@ -85,6 +85,7 @@ class Device:
"detected_model": self.model_name or None,
"n_in": self.model.n_in if self.model else 0,
"n_out": self.model.n_out if self.model else 0,
"fir": bool(self.model and self.model.fir),
"presets": self.presets, "state": self.state}
def start(self):
@@ -138,6 +139,35 @@ class Device:
await asyncio.sleep(0.3)
await self.refresh()
def fir_taps_used(self, skip: tuple[str, int] | None = None) -> int:
"""FIR taps taken by all channels together (they share P.FIR_MAX_TOTAL)."""
total = 0
for kind, key in (("in", "inputs"), ("out", "outputs")):
for i, ch in enumerate(self.state[key]):
if (kind, i) != skip:
total += P.fir_channel_taps(ch)
return total
async def upload_fir(self, kind: str, idx: int, coeffs: list[float], name: str) -> int:
"""Send FIR coefficients to a channel. Returns how many frames were not answered."""
if self.status != "online" or not self.state or not self.model.fir:
raise RuntimeError("DSP is not connected")
await self.flush()
c = self.model.channel(kind, idx)
name = P.clean_name(name)[:8]
failed = 0
if await self._request(P.CMD_FIR_NAME, bytes([c]) + name.encode("ascii").ljust(8, b" ")) is None:
failed += 1
for payload in P.fir_coef_frames(c, coeffs):
if await self._request(P.CMD_FIR_COEF, payload) is None:
failed += 1
fir = self.state["inputs" if kind == "in" else "outputs"][idx]["fir"]
fir["name"] = name
if kind == "in":
fir["taps"] = len(coeffs)
self._emit_state()
return failed
def gain_cap(self, kind: str, idx: int, value: int) -> int:
"""Software limit of a channel's gain: never above the limit - unless the DSP itself is set higher,
then only downwards (it snaps back into the limit once it is below)."""
@@ -406,7 +436,10 @@ class Device:
self._codes = None
async def _load_config(self):
self.state = await self._read_config()
state = await self._read_config()
if self.model.fir and self.state: # where this model keeps the test signal in its memory is not known
state["test"] = self.state.get("test", state["test"])
self.state = state
async def _read_config(self) -> dict:
model = self.model
+18 -10
View File
@@ -49,16 +49,16 @@ def _peq(src, cur: list) -> list:
def _group(src, cur: dict, ranges: dict) -> dict:
src = src if isinstance(src, dict) else {}
return {k: _int(src.get(k), lo, hi, cur[k]) for k, (lo, hi) in ranges.items()}
return {k: _int(src.get(k), *ranges.get(k, (0, 0xFFFF)), v) for k, v in cur.items()}
def _common(s: dict, c: dict) -> dict:
name = s.get("name", c["name"])
return {"name": P.clean_name(name).strip(),
"hp": _filter(s.get("hp"), c["hp"]), "lp": _filter(s.get("lp"), c["lp"]),
return {"name": P.clean_name(name).strip(), **{k: _filter(s.get(k), c[k]) for k in ("hp", "lp") if k in c},
"gain": _int(s.get("gain"), 0, 400, c["gain"]), "polarity": bool(s.get("polarity", c["polarity"])),
"delay": _int(s.get("delay"), 0, 65280, c["delay"]), "link": _int(s.get("link"), 0, 0xFFFF, c["link"]),
"mute": bool(s.get("mute", c["mute"])), "peq_bypass": bool(s.get("peq_bypass", c.get("peq_bypass", False)))}
"mute": bool(s.get("mute", c["mute"])),
**({"peq_bypass": bool(s.get("peq_bypass", c.get("peq_bypass", False)))} if "peq_bypass" in c else {})}
def clean_state(model: P.Model, src: dict, base: dict) -> dict:
@@ -69,16 +69,24 @@ def clean_state(model: P.Model, src: dict, base: dict) -> dict:
for i, c in enumerate(st["inputs"]):
s = ins[i] if i < len(ins) and isinstance(ins[i], dict) else {}
geq = s.get("geq") if isinstance(s.get("geq"), list) else []
c.update(_common(s, c), peq=_peq(s.get("peq"), c["peq"]), gate=_group(s.get("gate"), c["gate"], _GATE),
geq_bypass=bool(s.get("geq_bypass", c.get("geq_bypass", False))),
c.update(_common(s, c), peq=_peq(s.get("peq"), c["peq"]), gate=_group(s.get("gate"), c["gate"], _GATE))
if model.fir:
c.update(comp=_group(s.get("comp"), c["comp"], _COMP))
c["fir"]["bypass"] = bool((s.get("fir") if isinstance(s.get("fir"), dict) else {}).get("bypass", c["fir"]["bypass"]))
continue
c.update(geq_bypass=bool(s.get("geq_bypass", c.get("geq_bypass", False))),
geq=[_int(geq[b], 0, 240, v) if b < len(geq) else v for b, v in enumerate(c["geq"])])
for i, c in enumerate(st["outputs"]):
s = outs[i] if i < len(outs) and isinstance(outs[i], dict) else {}
mix = s.get("mix") if isinstance(s.get("mix"), list) else []
c.update(_common(s, c), peq=_peq(s.get("peq"), c["peq"]),
comp=_group(s.get("comp"), c["comp"], _COMP), limit=_group(s.get("limit"), c["limit"], _LIMIT),
limit=_group(s.get("limit"), c["limit"], _LIMIT),
matrix=_int(s.get("matrix"), 0, (1 << model.n_in) - 1, c["matrix"]),
mix=[_int(mix[k], 0, 400, v) if k < len(mix) else v for k, v in enumerate(c["mix"])])
if model.fir:
c["fir"] = P.clean_fir(s.get("fir"), c["fir"])
else:
c.update(comp=_group(s.get("comp"), c["comp"], _COMP))
if isinstance(src.get("preset_name"), str):
st["preset_name"] = src["preset_name"][:14]
return st
@@ -132,11 +140,11 @@ def import_commands(model: P.Model, st: dict, current: dict) -> list[tuple[int,
cmds.append((P.CMD_LINK, bytes([model.channel(kind, i)]) + (1 << i).to_bytes(2, "little")))
for kind, key in kinds:
for i, ch in enumerate(st[key]):
params = ["name", "gain", "polarity", "delay", "hp", "lp", "peq_bypass"] + [f"peq.{b}" for b in range(len(ch["peq"]))]
params = ["name", "gain", "polarity", "delay"] + [k for k in ("hp", "lp", "peq_bypass") if k in ch] + [f"peq.{b}" for b in range(len(ch["peq"]))]
if kind == "in":
params += [f"geq.{b}" for b in range(len(ch["geq"]))] + ["gate", "geq_bypass"]
params += ["gate", "comp", "fir"] if model.fir else [f"geq.{b}" for b in range(len(ch["geq"]))] + ["gate", "geq_bypass"]
else:
params += ["comp", "limit", "matrix"] + [f"mix.{k}" for k in range(len(ch["mix"]))]
params += (["fir", "limit"] if model.fir else ["comp", "limit"]) + ["matrix"] + [f"mix.{k}" for k in range(len(ch["mix"]))]
for p in params + ["mute"]:
cmd = P.param_command(model, kind, i, ch, p)
if relink or cmd != P.param_command(model, kind, i, current[key][i], p):
+223 -5
View File
@@ -46,6 +46,11 @@ CMD_LIMIT = 0x3F
CMD_MIX = 0x41
CMD_GEQ = 0x48
CMD_LINK = 0x3B # [ch][u16 link mask], see link_masks()
# FIR DSP 408 only
CMD_FIR = 0x4B # [ch][type][window][u16 low Hz][u16 high Hz][u16 taps index] (outputs)
CMD_FIR_COEF = 0x4E # [ch][packet][u16 total][up to 12 floats, most significant byte first]
CMD_FIR_BYPASS = 0x4F # [ch][1 = bypassed] (inputs)
CMD_FIR_NAME = 0x5B # [ch][8 characters]
CHUNK_SIZE = 50
TEST_SOURCES = ("analog", "pink", "white", "sine")
@@ -209,15 +214,16 @@ class Model:
name: str
n_in: int
n_out: int
beta: bool = False # layout assumed from the other models, never seen on a real device; only selectable by hand
beta: bool = False # never seen on a real device; only selectable by hand
fir: bool = False # FIR DSP 408: own memory layout, see _parse_fir
@property
def in_size(self) -> int:
return 140
return 138 if self.fir else 140
@property
def out_size(self) -> int:
return 96 + 2 * self.n_in
return 108 if self.fir else 96 + 2 * self.n_in
@property
def out_base(self) -> int:
@@ -234,6 +240,8 @@ class Model:
@property
def size(self) -> int:
if self.fir:
return 0x625
return self.globals_base + 4 + 2 * self.n_in + 2 * self.n_out + 12
@property
@@ -249,7 +257,10 @@ class Model:
MODELS = {"204": Model("DSP 204", 2, 4), "408": Model("DSP 408", 4, 8),
"206": Model("DSP 206", 2, 6, beta=True), "408fir": Model("FIR DSP 408", 4, 8, beta=True)}
"206": Model("DSP 206", 2, 6, beta=True), "408fir": Model("FIR DSP 408", 4, 8, beta=True, fir=True)}
FIR_MAX_IN, FIR_MAX_OUT, FIR_MAX_TOTAL = 2048, 1024, 4096 # taps per input / output, all channels together
FIR_PACKET = 12 # coefficients per CMD_FIR_COEF frame
def detect_model(info: str) -> str | None:
@@ -289,6 +300,8 @@ def _peq(buf: bytes, off: int, n: int) -> list[dict]:
def parse_state(model: Model, buf: bytes) -> dict:
"""Decode the reassembled config dump into a JSON friendly dict (raw values)."""
if model.fir:
return _parse_fir(model, buf)
u16 = lambda off: struct.unpack_from("<H", buf, off)[0]
g = model.globals_base
in_mute, out_mute = u16(g), u16(g + 2)
@@ -339,8 +352,207 @@ def parse_state(model: Model, buf: bytes) -> dict:
return {"preset_name": _name(buf[2:16]), "inputs": inputs, "outputs": outputs, "test": test}
# ---------------------------------------------------------------- FIR DSP 408
#
# Layout: header 0x10, inputs of 138 bytes, outputs of 108, then the mute masks, one word per channel with
# the bypassed PEQ bands, 8 characters of FIR name per channel and 17 bytes of other settings.
# Inputs: name, gate, 3 dynamic EQs (not used here), 8 PEQ bands, compressor, FIR taps + bypass, gain ...
# Outputs: name, matrix, mix, HP/LP (with own bypass bytes), FIR, 9 PEQ bands, limiter, gain ...
def _fir_slope(index: int, bypass: int) -> int:
"""This model keeps slope and bypass apart, the state uses one value (0 = bypass) like the other models."""
return 0 if bypass else min(index + 1, 20)
def _fir_filter(buf: bytes, freq_off: int, slope_off: int) -> dict:
return {"freq": struct.unpack_from("<H", buf, freq_off)[0], "slope": _fir_slope(buf[slope_off], buf[slope_off + 2])}
def _parse_fir(model: Model, buf: bytes) -> dict:
u16 = lambda off: struct.unpack_from("<H", buf, off)[0]
g = model.globals_base
in_mute, out_mute = u16(g), u16(g + 2)
fir_name = lambda c: _name(buf[0x5B4 + 8 * c : 0x5BC + 8 * c])
def bands(off: int, n: int, mask: int) -> list[dict]:
out = _peq(buf, off, n)
for j, b in enumerate(out):
b["bypass"] = bool(mask >> j & 1)
return out
inputs = []
for i in range(model.n_in):
o = 0x10 + i * model.in_size
comp = dict(zip(("ratio", "attack", "release", "knee", "threshold", "eq_freq"), struct.unpack_from("<6H", buf, o + 112)))
inputs.append({
"name": _name(buf[o : o + 8]),
"gate": dict(zip(("attack", "release", "hold", "threshold"), struct.unpack_from("<4H", buf, o + 8))),
"peq": bands(o + 64, 8, u16(g + 4 + 2 * i)),
"comp": {**comp, "eq_type": buf[o + 124], "eq_bypass": buf[o + 125]},
"fir": {"taps": u16(o + 126), "bypass": bool(u16(o + 128)), "name": fir_name(i)},
"gain": u16(o + 130), "polarity": u16(o + 132) != 0, "delay": u16(o + 134), "link": u16(o + 136),
"mute": bool(in_mute >> i & 1),
})
outputs = []
for i in range(model.n_out):
o = model.out_base + i * model.out_size
typ, win, low, high, taps = struct.unpack_from("<BBHHH", buf, o + 28)
outputs.append({
"name": _name(buf[o : o + 8]),
"matrix": u16(o + 8),
"mix": list(struct.unpack_from(f"<{model.n_in}H", buf, o + 10)),
"hp": _fir_filter(buf, o + 20, o + 24),
"lp": _fir_filter(buf, o + 22, o + 25),
"fir": {"type": typ, "window": win, "low": low, "high": high, "taps": taps, "name": fir_name(model.channel("out", i))},
"peq": bands(o + 36, 9, u16(g + 12 + 2 * i)),
"limit": dict(zip(("ratio", "attack", "release", "knee", "threshold"), struct.unpack_from("<5H", buf, o + 90))),
"gain": u16(o + 100), "polarity": u16(o + 102) != 0, "delay": u16(o + 104), "link": u16(o + 106),
"mute": bool(out_mute >> i & 1),
})
return {"preset_name": _name(buf[2:16]), "inputs": inputs, "outputs": outputs, "test": {"source": 0, "freq": 0}}
def _build_fir(model: Model, state: dict) -> bytearray:
buf = bytearray(model.chunks * CHUNK_SIZE)
pad = lambda s, n: s.encode("latin1")[:n].ljust(n, b" ")
buf[0:2] = b"\xff\xff"
buf[2:16] = pad(state["preset_name"], 14)
g = model.globals_base
def put_bands(off: int, bands: list[dict]) -> int:
for j, b in enumerate(bands):
struct.pack_into("<HHBB", buf, off + j * 6, b["gain"], b["freq"], b["q"], b["type"])
return sum(int(b.get("bypass", False)) << j for j, b in enumerate(bands))
in_mute = out_mute = 0
for i, ch in enumerate(state["inputs"]):
o = 0x10 + i * model.in_size
buf[o : o + 8] = pad(ch["name"], 8)
gt, c = ch["gate"], ch["comp"]
struct.pack_into("<4H", buf, o + 8, gt["attack"], gt["release"], gt["hold"], gt["threshold"])
struct.pack_into("<H", buf, g + 4 + 2 * i, put_bands(o + 64, ch["peq"]))
struct.pack_into("<6HBB", buf, o + 112, c["ratio"], c["attack"], c["release"], c["knee"], c["threshold"],
c["eq_freq"], c["eq_type"], c["eq_bypass"])
struct.pack_into("<6H", buf, o + 126, ch["fir"]["taps"], int(ch["fir"]["bypass"]), ch["gain"],
int(ch["polarity"]), ch["delay"], ch.get("link", 1 << i))
buf[0x5B4 + 8 * i : 0x5BC + 8 * i] = pad(ch["fir"]["name"], 8)
in_mute |= int(ch["mute"]) << i
for i, ch in enumerate(state["outputs"]):
o = model.out_base + i * model.out_size
buf[o : o + 8] = pad(ch["name"], 8)
struct.pack_into("<H", buf, o + 8, ch["matrix"])
struct.pack_into(f"<{model.n_in}H", buf, o + 10, *ch["mix"])
hp, lp, f, lim = ch["hp"], ch["lp"], ch["fir"], ch["limit"]
struct.pack_into("<HHBBBB", buf, o + 20, hp["freq"], lp["freq"], max(hp["slope"] - 1, 0), max(lp["slope"] - 1, 0),
int(not hp["slope"]), int(not lp["slope"]))
struct.pack_into("<BBHHH", buf, o + 28, f["type"], f["window"], f["low"], f["high"], f["taps"])
struct.pack_into("<H", buf, g + 12 + 2 * i, put_bands(o + 36, ch["peq"]))
struct.pack_into("<5H", buf, o + 90, lim["ratio"], lim["attack"], lim["release"], lim["knee"], lim["threshold"])
struct.pack_into("<4H", buf, o + 100, ch["gain"], int(ch["polarity"]), ch["delay"], ch.get("link", 1 << i))
c = model.channel("out", i)
buf[0x5B4 + 8 * c : 0x5BC + 8 * c] = pad(f["name"], 8)
out_mute |= int(ch["mute"]) << i
struct.pack_into("<HH", buf, g, in_mute, out_mute)
return buf
def _default_fir(model: Model) -> dict:
peq = lambda n: [{"gain": 120, "freq": 50 + i * 25, "q": 32, "type": 0, "bypass": False} for i in range(n)]
return {
"preset_name": "Default",
"inputs": [{
"name": f"In{chr(65 + i)}", "gate": {"attack": 0, "release": 99, "hold": 99, "threshold": 0}, "peq": peq(8),
"comp": {"ratio": 0, "attack": 49, "release": 99, "knee": 0, "threshold": 120, "eq_freq": 150, "eq_type": 0, "eq_bypass": 1},
"fir": {"taps": 0, "bypass": True, "name": ""},
"gain": 280, "polarity": False, "delay": 0, "mute": False, "link": 1 << i,
} for i in range(model.n_in)],
"outputs": [{
"name": f"Out{i + 1}", "matrix": 1 << (i % model.n_in), "mix": [280] * model.n_in,
"hp": {"freq": 0, "slope": 0}, "lp": {"freq": 300, "slope": 0},
"fir": {"type": 0, "window": 3, "low": 150, "high": 20000, "taps": 8, "name": ""},
"peq": peq(9), "limit": {"ratio": 0, "attack": 49, "release": 99, "knee": 0, "threshold": 200},
"gain": 280, "polarity": False, "delay": 0, "mute": False, "link": 1 << i,
} for i in range(model.n_out)],
"test": {"source": 0, "freq": 0},
}
def fir_channel_taps(ch: dict) -> int:
"""How many of the shared FIR taps a channel takes."""
f = ch["fir"]
if "type" in f:
return 256 + 32 * f["taps"] if f["type"] else 0
return 0 if f["bypass"] else f["taps"]
def clean_fir(value, cur: dict) -> dict:
"""FIR settings of an output limited to valid values; anything missing stays as it is."""
value = value if isinstance(value, dict) else {}
def num(key, lo, hi):
try:
return min(hi, max(lo, int(value.get(key, cur[key]))))
except (TypeError, ValueError):
return cur[key]
return {"type": num("type", 0, 4), "window": num("window", 0, 10), "low": num("low", 20, 20000),
"high": num("high", 20, 20000), "taps": num("taps", 0, 24), "name": cur.get("name", "")}
def fir_coef_frames(channel: int, coeffs: list[float]) -> list[bytes]:
"""Payloads of the CMD_FIR_COEF frames for one channel."""
n = len(coeffs)
return [bytes([channel, p]) + struct.pack("<H", n)
+ b"".join(struct.pack(">f", c) for c in coeffs[p * FIR_PACKET : (p + 1) * FIR_PACKET])
for p in range(-(-n // FIR_PACKET))]
def _fir_command(c: int, kind: str, ch: dict, top: str, rest: list[str]) -> tuple[int, bytes] | None:
if top in ("hp", "lp"):
f = ch[top]
return (CMD_HIGH_PASS if top == "hp" else CMD_LOW_PASS), bytes([c]) + _w(f["freq"]) + bytes([max(f["slope"] - 1, 0), int(not f["slope"])])
if top == "limit" and kind == "out":
return CMD_LIMIT, bytes([c]) + b"".join(_w(ch["limit"][k]) for k in ("ratio", "attack", "release", "knee", "threshold"))
if top == "comp" and kind == "in":
g = ch["comp"]
return CMD_COMP, bytes([c]) + b"".join(_w(g[k]) for k in ("ratio", "attack", "release", "knee", "threshold", "eq_freq")) + bytes([g["eq_type"], g["eq_bypass"]])
if top == "fir" and kind == "out":
f = ch["fir"]
return CMD_FIR, bytes([c, f["type"], f["window"]]) + _w(f["low"]) + _w(f["high"]) + _w(f["taps"])
if top == "fir" and kind == "in":
return CMD_FIR_BYPASS, bytes([c, int(ch["fir"]["bypass"])])
return None
def _apply_fir(ch: dict, cmd: int, p: bytes) -> bool:
"""Set commands of this model (simulator). False: handled like on the other models."""
w = lambda o: struct.unpack_from("<H", p, o)[0]
if cmd in (CMD_HIGH_PASS, CMD_LOW_PASS):
ch["hp" if cmd == CMD_HIGH_PASS else "lp"] = {"freq": w(1), "slope": _fir_slope(p[3], p[4])}
elif cmd == CMD_LIMIT:
ch["limit"] = dict(zip(("ratio", "attack", "release", "knee", "threshold"), struct.unpack_from("<5H", p, 1)))
elif cmd == CMD_COMP:
ch["comp"] = {**dict(zip(("ratio", "attack", "release", "knee", "threshold", "eq_freq"), struct.unpack_from("<6H", p, 1))),
"eq_type": p[13], "eq_bypass": p[14]}
elif cmd == CMD_FIR:
ch["fir"].update(type=p[1], window=p[2], low=w(3), high=w(5), taps=w(7))
elif cmd == CMD_FIR_BYPASS:
ch["fir"]["bypass"] = bool(p[1])
elif cmd == CMD_FIR_NAME:
ch["fir"]["name"] = _name(p[1:9])
elif cmd == CMD_FIR_COEF:
if "bypass" in ch["fir"]:
ch["fir"]["taps"] = w(2)
else:
return False
return True
def build_state_buffer(model: Model, state: dict) -> bytearray:
"""Inverse of parse_state (used by the simulator)."""
if model.fir:
return _build_fir(model, state)
buf = bytearray(model.chunks * CHUNK_SIZE)
pad = lambda s, n: s.encode("latin1")[:n].ljust(n, b" ")
buf[0:2] = b"\xff\xff"
@@ -389,6 +601,8 @@ def build_state_buffer(model: Model, state: dict) -> bytearray:
def default_state(model: Model) -> dict:
if model.fir:
return _default_fir(model)
peq = lambda n: [{"gain": 120, "freq": 50 + i * 25, "q": 32, "type": 0, "bypass": False}
for i in range(n)]
return {
@@ -471,6 +685,8 @@ def param_command(model: Model, kind: str, idx: int, ch: dict, param: str) -> tu
"""
c = model.channel(kind, idx)
top, *rest = param.split(".")
if model.fir and (fir := _fir_command(c, kind, ch, top, rest)):
return fir
if top == "gain":
return CMD_GAIN, bytes([c]) + _w(ch["gain"])
if top == "mute":
@@ -541,7 +757,9 @@ def apply_command(model: Model, state: dict, cmd: int, p: bytes):
return
ch = chans[idx]
w = lambda o: struct.unpack_from("<H", p, o)[0]
if cmd == CMD_PEQ_BYPASS:
if model.fir and _apply_fir(ch, cmd, p):
pass
elif cmd == CMD_PEQ_BYPASS:
ch["peq_bypass"] = bool(p[1])
elif cmd == CMD_GEQ_BYPASS:
ch["geq_bypass"] = bool(p[1])