Device lock (hardware lock of the DSP, independent of the app lock): - detect a locked DSP from the 0x2C answer during connect and wait for the code - unlock with 0x2D, optionally remember the code and unlock automatically - set a new code (0x2F) from the device settings - simulator: --lock CODE Demo mode: - switch in the settings starts a simulated DSP 204/408 inside the server - real DSPs are disconnected and greyed out meanwhile and resume afterwards - demo devices are never saved or exported; sim.py ships with the Pi image and EXEs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
122 lines
5.0 KiB
Python
122 lines
5.0 KiB
Python
"""Simulated t.racks DSP for testing without hardware.
|
|
|
|
Usage: python sim.py --model 204 --port 9761
|
|
python sim.py --model 408 --port 9762
|
|
python sim.py --model 204 --port 9761 --lock A1BA (asks for that lock code on every connection)
|
|
"""
|
|
|
|
import argparse
|
|
import asyncio
|
|
import copy
|
|
import logging
|
|
import math
|
|
import time
|
|
|
|
from dsp import protocol as P
|
|
|
|
log = logging.getLogger("sim")
|
|
|
|
|
|
class SimDSP:
|
|
def __init__(self, model_key: str, lock_code: str = ""):
|
|
self.key = model_key
|
|
self.lock_code = lock_code # hardware lock: "" = none
|
|
self.unlocked = not lock_code
|
|
self.model = P.MODELS[model_key]
|
|
self.state = P.default_state(self.model)
|
|
self.presets = [copy.deepcopy(self.state) if i == 0 else None for i in range(20)]
|
|
self.presets[0]["preset_name"] = "Default"
|
|
self.pending_name = ""
|
|
|
|
def preset_names(self) -> list[str]:
|
|
return [p["preset_name"] if p else "" for p in self.presets]
|
|
|
|
def handle(self, cmd: int, p: bytes) -> tuple[int, bytes]:
|
|
m = self.model
|
|
if cmd == P.CMD_DEVICE_INFO:
|
|
return cmd, f"DSP{self.key} V010 SIM".encode()
|
|
if cmd == P.CMD_PRESET_COUNT:
|
|
return cmd, bytes([0, 0, 0, 0, 0, 0 if self.unlocked else 1])
|
|
if cmd == P.CMD_UNLOCK:
|
|
self.unlocked = self.unlocked or p[1:5].decode("latin1") == self.lock_code
|
|
return cmd, bytes([0, 1 if self.unlocked else 0])
|
|
if cmd == P.CMD_SET_LOCK_CODE:
|
|
self.lock_code = p[:4].decode("latin1")
|
|
return P.CMD_ACK, b"\x00"
|
|
if not self.unlocked and cmd not in (P.CMD_HANDSHAKE, P.CMD_METER, P.CMD_LOGOUT):
|
|
return P.CMD_ACK, b"\x00" # locked: ignores everything until the code was entered
|
|
if cmd == P.CMD_PRESET_NAME:
|
|
return cmd, bytes([p[0]]) + self.preset_names()[p[0]].encode("latin1").ljust(14, b" ")
|
|
if cmd == P.CMD_CONFIG_REQUEST:
|
|
buf = P.build_state_buffer(m, self.state)
|
|
n = p[0]
|
|
return P.CMD_CONFIG_CHUNK, bytes([n]) + bytes(buf[n * P.CHUNK_SIZE:(n + 1) * P.CHUNK_SIZE])
|
|
if cmd == P.CMD_METER:
|
|
t = time.monotonic()
|
|
levels = [0.0] * 12
|
|
for i in range(m.n_in):
|
|
ch = self.state["inputs"][i]
|
|
lv = 0.0 if ch["mute"] else 0.25 * (1 + math.sin(t * (1.3 + i))) * 10 ** (P.gain_to_db(ch["gain"]) / 20)
|
|
levels[m.meter_index("in", i)] = lv
|
|
flags = 0
|
|
for o in range(m.n_out):
|
|
ch = self.state["outputs"][o]
|
|
src = sum(levels[m.meter_index("in", i)] for i in range(m.n_in) if ch["matrix"] >> i & 1)
|
|
lv = 0.0 if ch["mute"] else min(1.5, src * 10 ** (P.gain_to_db(ch["gain"]) / 20))
|
|
levels[m.meter_index("out", o)] = lv
|
|
if lv > 0 and 20 * math.log10(lv) > P.threshold_to_db(ch["limit"]["threshold"]):
|
|
flags |= 1 << o
|
|
return cmd, P.encode_meters(levels, flags)
|
|
if cmd == P.CMD_PRESET_RECALL:
|
|
preset = self.presets[p[0] - 1]
|
|
if preset:
|
|
self.state = copy.deepcopy(preset)
|
|
elif cmd == P.CMD_PRESET_STORE_NAME:
|
|
self.pending_name = p.decode("latin1").strip()
|
|
elif cmd == P.CMD_PRESET_STORE:
|
|
self.state["preset_name"] = self.pending_name or f"Preset {p[0]}"
|
|
self.presets[p[0] - 1] = copy.deepcopy(self.state)
|
|
else:
|
|
P.apply_command(m, self.state, cmd, p)
|
|
return P.CMD_ACK, b"\x00"
|
|
|
|
async def serve(self, reader, writer):
|
|
peer = writer.get_extra_info("peername")
|
|
log.info("client %s connected", peer)
|
|
self.unlocked = not self.lock_code
|
|
parser = P.FrameParser()
|
|
try:
|
|
while data := await reader.read(4096):
|
|
for f in parser.feed(data):
|
|
if f.cmd != P.CMD_METER:
|
|
log.info("rx cmd=0x%02x payload=%s", f.cmd, f.payload.hex(" "))
|
|
cmd, payload = self.handle(f.cmd, f.payload)
|
|
writer.write(P.build_frame(cmd, payload, addr=0, src=1))
|
|
await writer.drain()
|
|
except ConnectionError:
|
|
pass
|
|
log.info("client %s disconnected", peer)
|
|
writer.close()
|
|
|
|
|
|
async def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--model", choices=list(P.MODELS), default="204")
|
|
ap.add_argument("--port", type=int, default=P.PORT)
|
|
ap.add_argument("--host", default="127.0.0.1")
|
|
ap.add_argument("--lock", default="", help="lock code (4 characters A-Z/0-9) the DSP asks for")
|
|
args = ap.parse_args()
|
|
logging.basicConfig(level=logging.INFO, format=f"%(asctime)s sim{args.model} %(message)s")
|
|
sim = SimDSP(args.model, args.lock.upper())
|
|
server = await asyncio.start_server(sim.serve, args.host, args.port)
|
|
log.info("simulated DSP %s on %s:%d", args.model, args.host, args.port)
|
|
async with server:
|
|
await server.serve_forever()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
asyncio.run(main())
|
|
except KeyboardInterrupt:
|
|
pass
|