"""Protocol self test: python tests/test_protocol.py""" import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from dsp import protocol as P # noqa: E402 def test_frames_match_known_captures(): # Known-good frames as sent to the devices (they also match the DSP 408 captures published by dsp-408-ui) assert P.build_frame(0x10) == bytes.fromhex("10 02 00 01 01 10 10 03 11") assert P.build_frame(0x40) == bytes.fromhex("10 02 00 01 01 40 10 03 41") assert P.build_frame(0x29, b"\x13") == bytes.fromhex("10 02 00 01 02 29 13 10 03 38") assert P.build_frame(0x35, b"\x0b\x01") == bytes.fromhex("10 02 00 01 03 35 0b 01 10 03 3c") def test_parser_handles_split_and_embedded_dle(): payload = bytes([5]) + b"\x10\x03\x10\x02" + bytes(46) frame = P.build_frame(P.CMD_CONFIG_CHUNK, payload, addr=0, src=1) parser = P.FrameParser() out = parser.feed(b"\x00garbage" + frame[:7]) + parser.feed(frame[7:] + P.build_frame(0x01, b"\x00")) assert [f.cmd for f in out] == [P.CMD_CONFIG_CHUNK, 0x01] assert out[0].payload == payload and out[0].checksum_ok def test_layout_matches_devices(): m204, m408 = P.MODELS["204"], P.MODELS["408"] assert (m204.size, m204.chunks) == (724, 15) assert (m408.size, m408.chunks) == (1448, 29) assert m204.out_base == 0x128 and m204.globals_base == 0x2B8 assert m408.out_base == 0x240 and m408.globals_base == 0x580 assert m204.channel("out", 0) == 2 and m408.channel("out", 0) == 4 def test_state_roundtrip(): for m in P.MODELS.values(): st = P.default_state(m) st["outputs"][-1]["comp"]["threshold"] = 123 st["inputs"][0]["geq"][30] = 7 st["inputs"][-1]["mute"] = True assert P.parse_state(m, bytes(P.build_state_buffer(m, st))) == st def test_scales(): assert P.gain_to_db(0) == -60 and P.gain_to_db(80) == -20 and P.gain_to_db(280) == 0 and P.gain_to_db(400) == 12 for raw in (0, 37, 80, 150, 280, 400): assert P.db_to_gain(P.gain_to_db(raw)) == raw assert round(P.freq_to_hz(0), 1) == 19.7 and round(P.freq_to_hz(300)) == 20159 assert P.hz_to_freq(1000) == 170 assert len(P.Q_TABLE) == 101 and len(P.SLOPES) == 21 lv = [0.5, 0.0, 1.0, 0.001] + [0.25] * 8 dec = P.decode_meters(P.encode_meters(lv)) assert all(abs(a - b) < 1e-3 * max(a, 1e-3) for a, b in zip(lv, dec)) def test_param_commands(): m = P.MODELS["204"] st = P.default_state(m) out = st["outputs"][1] P.apply_param(out, "peq.3.gain", 150) assert P.param_command(m, "out", 1, out, "peq.3.gain") == (0x33, bytes([3, 3, 150, 0, 125, 0, 32, 0, 0])) P.apply_param(out, "mix.1", 200) assert P.param_command(m, "out", 1, out, "mix.1") == (0x41, bytes([3, 1, 200, 0])) inp = st["inputs"][1] assert P.param_command(m, "in", 1, inp, "gate.hold") == (0x3E, bytes([1, 0, 0, 99, 0, 99, 0, 0, 0])) def test_links(): # masks as read from a real DSP 204: In A+B, Out 1+2 and Out 3+4 linked m = P.MODELS["204"] st = P.default_state(m) for ch, mask in zip(st["inputs"] + st["outputs"], [3, 0, 3, 0, 12, 0]): ch["link"] = mask st = P.parse_state(m, bytes(P.build_state_buffer(m, st))) assert P.link_groups(st["inputs"]) == [[0, 1]] assert P.link_groups(st["outputs"]) == [[0, 1], [2, 3]] assert P.link_groups(P.default_state(m)["outputs"]) == [[0], [1], [2], [3]] assert P.link_masks(4, [[0, 1], [2, 3]]) == [3, 0, 12, 0] assert P.link_masks(4, [[3, 1]]) == [1, 10, 4, 0] # the DSP applies a change on any member to the whole group, but not names P.apply_command(m, st, P.CMD_GAIN, bytes([5, 100, 0])) P.apply_command(m, st, P.CMD_CHANNEL_NAME, bytes([4]) + b"Sub ") assert st["outputs"][2]["gain"] == 100 and st["outputs"][3]["name"] != "Sub" def test_preset_import_commands(): from dsp import preset m = P.MODELS["204"] cur = P.default_state(m) assert preset.import_commands(m, cur, cur) == [] # identical: nothing is written tgt = P.default_state(m) tgt["outputs"][1]["gain"] = 300 assert preset.import_commands(m, tgt, cur) == [(0x34, bytes([3]) + (300).to_bytes(2, "little"))] tgt["outputs"][0]["link"], tgt["outputs"][1]["link"] = 3, 0 # new link: dissolve, set, relink cmds = preset.import_commands(m, tgt, cur) assert cmds[0][0] == P.CMD_LINK and (P.CMD_LINK, bytes([2, 3, 0])) in cmds # a file is read with linked channels made alike, like the DSP keeps them doc = preset.export_doc("204", "DSP204", tgt, bytes(P.build_state_buffer(m, tgt))[: m.size]) st = preset.read_file("204", m, cur, doc=doc) assert st["outputs"][1]["gain"] == st["outputs"][0]["gain"] == 280 assert st["outputs"][1]["matrix"] == tgt["outputs"][1]["matrix"] # routing stays per channel def test_limit_flags(): # status bytes after the 12 levels, captured on a DSP 204 while forcing the limiters assert P.decode_limit_flags(bytes(36) + bytes([0x00, 0x03]), 4) == [True, True, False, False] assert P.decode_limit_flags(bytes(36) + bytes([0x00, 0x0C]), 4) == [False, False, True, True] assert P.decode_limit_flags(bytes(36), 4) == [False] * 4 assert P.decode_limit_flags(P.encode_meters([0.0] * 12, 0x05), 4) == [True, False, True, False] def test_channel_names(): # the DSP keeps 8 ASCII characters; umlauts are spelled out, anything else is dropped assert P.clean_name("Bühne€ Links") == "Buehne L" m = P.MODELS["204"] assert P.param_command(m, "in", 0, {"name": "Süd"}, "name") == (0x3D, b"\x00Sued ") def test_device_lock(): # answer to 0x2C: [time lock][4 bytes time lock data][device lock] assert P.device_locked(bytes([0, 0, 0, 0, 0, 1])) assert not P.device_locked(bytes([0, 0, 0, 0, 0, 0])) assert not P.device_locked(bytes([0, 20])) # short answer: no lock information assert P.LOCK_CODE.fullmatch("A1BA") and not P.LOCK_CODE.fullmatch("a1ba") and not P.LOCK_CODE.fullmatch("A1B") assert P.build_frame(P.CMD_UNLOCK, b"\x00A1BA")[5:11] == bytes([0x2D, 0x00]) + b"A1BA" import sim dsp = sim.SimDSP("204", "A1BA") assert P.device_locked(dsp.handle(P.CMD_PRESET_COUNT, b"")[1]) assert dsp.handle(P.CMD_UNLOCK, b"\x00ZZZZ") == (P.CMD_UNLOCK, bytes([0, 0])) assert dsp.handle(P.CMD_UNLOCK, b"\x00A1BA") == (P.CMD_UNLOCK, bytes([0, 1])) assert not P.device_locked(dsp.handle(P.CMD_PRESET_COUNT, b"")[1]) dsp.handle(P.CMD_SET_LOCK_CODE, b"B2C3") assert dsp.lock_code == "B2C3" def test_test_signal(): # source/frequency follow the mute masks and one word per channel (editor: 0x2C8 on the 204, 0x59C on the 408) assert P.MODELS["204"].test_base == 0x2C8 and P.MODELS["408"].test_base == 0x59C for m in P.MODELS.values(): st = P.default_state(m) st["test"] = {"source": 3, "freq": 17} assert P.parse_state(m, bytes(P.build_state_buffer(m, st)))["test"] == {"source": 3, "freq": 17} st = P.default_state(P.MODELS["204"]) P.apply_command(P.MODELS["204"], st, P.CMD_TEST_SIGNAL, bytes([1, 5])) assert st["test"] == {"source": 1, "freq": 5} def test_master_bypass(): # masks after the test signal: [?][PEQ inputs][PEQ outputs][GEQ inputs] (editor: 0x2CE/0x2D0/0x2D2 on the 204) m = P.MODELS["204"] assert m.test_base + 6 == 0x2CE and m.test_base + 10 == 0x2D2 == m.size - 2 st = P.default_state(m) st["inputs"][1]["peq_bypass"] = st["inputs"][0]["geq_bypass"] = st["outputs"][3]["peq_bypass"] = True buf = P.build_state_buffer(m, st) assert buf[0x2CE:0x2D4] == bytes([2, 0, 8, 0, 1, 0]) back = P.parse_state(m, bytes(buf)) assert back["inputs"][1]["peq_bypass"] and back["inputs"][0]["geq_bypass"] and back["outputs"][3]["peq_bypass"] assert not back["inputs"][0]["peq_bypass"] and not back["outputs"][0]["peq_bypass"] assert P.param_command(m, "out", 3, back["outputs"][3], "peq_bypass") == (0x3C, bytes([5, 1])) assert P.param_command(m, "in", 0, back["inputs"][0], "geq_bypass") == (0x49, bytes([0, 1])) assert P.MODELS["408"].test_base + 6 == 0x5A2 def test_beta_models(): assert P.detect_model("DSP408 V010") == "408" and P.detect_model("DSP 204") == "204" for info in ("DSP206 V1", "FIR DSP408 V1", "FIRDSP408", "DSP408FIR"): # only selectable by hand assert P.detect_model(info) is None, info for key in ("206", "408fir"): m = P.MODELS[key] assert m.beta and not {P.CMD_GAIN, P.CMD_MUTE, P.CMD_LINK, P.CMD_PRESET_RECALL} & P.READ_COMMANDS st = P.default_state(m) st["outputs"][m.n_out - 1]["gain"] = 123 back = P.parse_state(m, bytes(P.build_state_buffer(m, st))) assert back["outputs"][m.n_out - 1]["gain"] == 123 and len(back["inputs"]) == m.n_in if __name__ == "__main__": for name, fn in list(globals().items()): if name.startswith("test_"): fn() print("ok", name)