Files
qlcplus/tests/test_padeditor.py
T
jimmyandClaude Sonnet 5.5 3992744a1d Light pad bank 2 from page 7, and add factory reset and a pad bank toggle to the editor.
Bank 2's lamp colours come from preset page 7, not page 3, so pad_colours.py
writes there and the editor labels it Bank 2. The factory reset loads the
image MidiSuite uploads for its own reset, and the bank toggle writes the
global-block byte MidiSuite uses to switch banks.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-21 20:34:34 +13:00

199 lines
6.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Preset image round-trip: factory knob and note-36 pad stay put."""
from __future__ import annotations
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from padeditor.preset import ( # noqa: E402
GAP_AT,
IMAGE_LEN,
KNOB_AT,
PAD_AT,
PAD_LEN,
PRESET_LEN,
changed_ranges,
pack,
scale,
unpack,
)
from padeditor.sysex import decode, encode # noqa: E402
FACTORY_KNOB = bytes.fromhex("0002001e007f")
def _image() -> bytearray:
raw = bytearray(IMAGE_LEN)
raw[KNOB_AT:KNOB_AT + len(FACTORY_KNOB)] = FACTORY_KNOB
raw[GAP_AT] = 0x5A
# Note 36 is pad record 32: channel 10, note, min and max velocity,
# RGB, then Led, as MidiSuite shows PAD1.
pad = PAD_AT + 32 * PAD_LEN
assert pad == 1044
raw[pad:pad + 8] = bytes((0x09, 36, 0, 0x7F, 0xF0, 0x00, 0x00, 0xFF))
raw[pad + 8] = 0xAB
# Last pad of preset 0 is one byte short of a full record.
last = PAD_AT + 127 * PAD_LEN
raw[last] = 0x09
raw[last + 1] = 67
raw[PRESET_LEN - 1] = 0x11
# A button with a sysex payload and a flag, matching the factory shape.
raw[0:23] = bytes.fromhex("020019007f06f07f7f0601f700000000000000000000ff")
return raw
def test_codec_vector_lives_with_the_editor():
inner = bytes.fromhex("00592209000005000000800100000178")
wire = bytes([0xF0, *encode(inner), 0xF7])
assert wire == bytes.fromhex("f000320949000040020000000018000000017001f7")
assert decode(wire[1:-1]) == inner
def test_factory_knob_and_note_36_pad():
raw = bytes(_image())
assert raw[KNOB_AT:KNOB_AT + 6] == FACTORY_KNOB
assert raw[1044:1046] == bytes((0x09, 36))
assert raw[1048:1051] == bytes.fromhex("f00000")
doc = unpack(raw)
knob = doc["presets"][0]["knobs"][0]
assert knob == {"type": 0, "speed": 2, "channel": 1, "number": 30, "left": 0, "right": 127}
pad = doc["presets"][0]["pads"][32]
assert pad["note"] == 36
assert pad["channel"] == 10
assert (pad["min"], pad["max"]) == (0, 127)
assert pad["rgb"] == [0xF0, 0x00, 0x00]
assert pad["led"] == 0xFF
assert doc["presets"][0]["gap"] == 0x5A
assert doc["presets"][0]["buttons"][0]["name"] == "Left"
assert doc["presets"][0]["buttons"][0]["sysex"] == "f07f7f0601f7"
assert pack(doc) == raw
def test_an_edit_dirties_only_that_byte():
raw = bytes(_image())
doc = unpack(raw)
doc["presets"][0]["knobs"][0]["number"] = 31
edited = pack(doc)
assert changed_ranges(raw, edited) == [(KNOB_AT + 3, b"\x1f")]
assert edited[GAP_AT] == 0x5A
assert edited[1048:1051] == raw[1048:1051]
at = KNOB_AT + 3
assert edited[:at] + edited[at + 1:] == raw[:at] + raw[at + 1:]
def test_a_colour_is_one_write_like_midisuite(monkeypatch):
from padeditor import device
from padeditor.preset import colour_offset
# MidiSuite set preset 5 record 21 green with one write to 0x3a46.
assert colour_offset(4, 21) == 0x3A46
writes = []
class FakeLink:
def __enter__(self):
return self
def __exit__(self, *exc):
pass
monkeypatch.setattr(device, "Link", FakeLink)
monkeypatch.setattr(device, "_write_checked", lambda link, address, data: writes.append((address, data)))
device.write_colour(4, 21, [0, 0xFF, 0])
assert writes == [(0x3A46, b"\x00\xff\x00")]
def test_global_block_matches_midisuite():
from padeditor.preset import pack_global, unpack_global
factory = bytes.fromhex("780032040000020001010000")
block = unpack_global(factory)
assert (block["tempo"], block["swing"], block["curve"], block["transpose"]) == (120, 50, 2, 0)
assert pack_global(block) == factory
assert unpack_global(bytes.fromhex("77003204000001f601010400"))["transpose"] == -10
block["transpose"] = -10
block["tempo"] = 119
assert pack_global(block) == bytes.fromhex("770032040000 02f6 01010000".replace(" ", ""))
def test_colour_scale_matches_the_pad_lamps():
assert scale(0) == 0
assert scale(255) == 0xF0
assert scale(127) == 0x78
def test_a_step_is_relative_and_a_ramp_is_absolute():
from padeditor.device import _classify
assert _classify([]) == "silent"
assert _classify([(0, 45, 1), (0, 45, 127)]) == "relative"
assert _classify([(0, 45, value) for value in range(10, 20)]) == "absolute"
def test_export_packs_the_image_without_opening_midi():
from padeditor.server import ApiError, Editor
raw = bytes(_image())
assert Editor().exported(unpack(raw)) == raw
try:
Editor().save({"presets": []})
except ApiError as exc:
assert "8" in str(exc)
else:
raise AssertionError("a short preset list should be refused before any MIDI")
def test_last_pad_keeps_its_short_tail():
raw = bytes(_image())
doc = unpack(raw)
tail = doc["presets"][0]["pads"][127]["tail"]
assert len(bytes.fromhex(tail)) == 17
assert pack(doc)[PRESET_LEN - 1] == 0x11
def test_factory_image_reads_as_midisuite_shows_it():
# Captured from MidiSuite's Reset with scripts/decode_usbmon.py.
raw = (ROOT / "tests" / "data" / "smc_pad_factory.bin").read_bytes()
doc = unpack(raw)
assert pack(doc) == raw
first = doc["presets"][0]
# PAD4, bottom right of the PAD1–PAD16 grid: Note 39, Channel 10, 0–127, Led 255.
pad4 = first["pads"][2 * 16 + 3]
assert (pad4["note"], pad4["channel"], pad4["min"], pad4["max"], pad4["led"]) == (39, 10, 0, 127, 255)
# The [<] button: CC Push, Channel 1, CC 25, 0–127, Led 255.
left = first["buttons"][0]
assert (left["type"], left["channel"], left["number"], left["min"], left["max"], left["led"]) == (2, 1, 25, 0, 127, 255)
# Knob 6: CC, Channel 1, CC 35, 0–127.
knob6 = first["knobs"][5]
assert (knob6["type"], knob6["channel"], knob6["number"], knob6["left"], knob6["right"]) == (0, 1, 35, 0, 127)
def test_factory_document_packs_back_to_the_midisuite_image():
from pathlib import Path
from padeditor.preset import pack
from padeditor.server import Editor
document = Editor().factory()
assert pack(document) == (Path(__file__).parents[1] / "padeditor" / "factory.bin").read_bytes()
assert document["global"]["raw"] == "780032040000020001010000"
def test_global_bank_byte_round_trips_and_is_checked():
import pytest
from padeditor.preset import PresetError, pack_global, unpack_global
raw = bytes.fromhex("780032040000020001010001")
block = unpack_global(raw)
assert block["bank"] == 1
assert pack_global(block) == raw
block["bank"] = 0
assert pack_global(block)[11] == 0
block["bank"] = 2
with pytest.raises(PresetError):
pack_global(block)