"""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 ") if __name__ == "__main__": for name, fn in list(globals().items()): if name.startswith("test_"): fn() print("ok", name)