Add audio test tab with test generator, test fader and output mutes
- switch the DSP's test generator (pink/white noise, sine 20 Hz-20 kHz, command 0x39) - entering lowers all outputs to -50 dB before the generator starts; one test fader raises them together; mute buttons for every output - presets cannot be stored, recalled, imported or exported while the test runs - leaving switches the generator off first, then restores every output gain - a DSP found playing a test signal on connect goes straight into the audio test; saved gains survive an app restart (config.json) - simulator and memory-image support for the test signal Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
61400df667
commit
66f988cf08
12 files changed
+280
-12
No files matched your search
@@ -23,6 +23,7 @@ from urllib.parse import parse_qs, urlsplit
|
||||
|
||||
import sim
|
||||
from dsp import preset, usbhid
|
||||
from dsp import protocol as P
|
||||
from dsp.device import Device
|
||||
|
||||
ROOT = Path(__file__).resolve().parent
|
||||
@@ -35,6 +36,8 @@ MAX_SESSIONS = 50
|
||||
COOKIE = "dspsess"
|
||||
CONFIG_FORMAT = "dsp-control-config"
|
||||
MAX_UNDO = 20 # automatic backups kept per DSP before each import
|
||||
TEST_LEVEL = -50.0 # dB all outputs start at when an audio test begins (noise generators are loud)
|
||||
TEST_MAX = 0.0 # highest level the test fader can set
|
||||
|
||||
|
||||
def _num(v, lo: float, hi: float, default: float = 0.0) -> float:
|
||||
@@ -86,6 +89,13 @@ def clean_limiter(data: dict) -> dict:
|
||||
return {"amps": amps, "assign": assign, "ref": ref}
|
||||
|
||||
|
||||
def clean_gains(v) -> list[int] | None:
|
||||
"""Saved output gains of an interrupted audio test (from config.json)."""
|
||||
if not isinstance(v, list) or not v or len(v) > 8:
|
||||
return None
|
||||
return [int(_num(g, 0, 400, 280)) for g in v]
|
||||
|
||||
|
||||
def hash_pin(pin: str, salt: str) -> str:
|
||||
return hashlib.pbkdf2_hmac("sha256", pin.encode(), bytes.fromhex(salt), 100_000).hex()
|
||||
|
||||
@@ -222,7 +232,9 @@ class App:
|
||||
int(d.get("port", 9761)), str(d.get("model", "auto")), int(d.get("addr", 1)),
|
||||
bool(d.get("autoconnect", True)), on_event=self.broadcast,
|
||||
transport="usb" if usb else "tcp", usb=str(d.get("usb") or ""),
|
||||
lock_code=str(d.get("lock_code") or ""), auto_unlock=bool(d.get("auto_unlock")))
|
||||
lock_code=str(d.get("lock_code") or ""), auto_unlock=bool(d.get("auto_unlock")),
|
||||
test_saved=clean_gains(d.get("test_saved")))
|
||||
dev.on_online = self.check_audio_test
|
||||
self.devices[dev_id] = dev
|
||||
if dev.autoconnect:
|
||||
if self.demo["enabled"]:
|
||||
@@ -233,6 +245,60 @@ class App:
|
||||
self.save()
|
||||
return dev
|
||||
|
||||
# ------------------------------------------------------------ audio test
|
||||
|
||||
def set_test_level(self, dev: Device, db: float):
|
||||
"""Every output to the same level (the test fader)."""
|
||||
dev.test_level = _num(db, -60, TEST_MAX, TEST_LEVEL)
|
||||
raw = P.db_to_gain(dev.test_level)
|
||||
for i in range(len(dev.state["outputs"])):
|
||||
dev.set_param("out", i, "gain", raw)
|
||||
|
||||
def restore_gains(self, dev: Device):
|
||||
for i, g in enumerate((dev.test_saved or [])[: len(dev.state["outputs"])]):
|
||||
dev.set_param("out", i, "gain", g)
|
||||
dev.test_saved = None
|
||||
|
||||
async def audio_test(self, dev: Device, body: dict) -> dict:
|
||||
source = int(_num(body.get("source"), 0, len(P.TEST_SOURCES) - 1, 0))
|
||||
freq = int(_num(body.get("freq"), 0, 30, dev.state["test"]["freq"] if dev.state else 17))
|
||||
if not dev.state or dev.status != "online":
|
||||
raise RuntimeError("DSP is not connected")
|
||||
if source:
|
||||
if not dev.test_active: # entering: lower every output before the generator starts
|
||||
if dev.test_saved is None:
|
||||
dev.test_saved = [ch["gain"] for ch in dev.state["outputs"]]
|
||||
self.save()
|
||||
self.set_test_level(dev, TEST_LEVEL)
|
||||
await dev.set_test_signal(source, freq)
|
||||
else: # leaving: generator off first, then the faders go back
|
||||
await dev.set_test_signal(0, freq)
|
||||
if dev.test_saved is not None:
|
||||
self.restore_gains(dev)
|
||||
await dev.flush()
|
||||
self.save()
|
||||
self.broadcast({"type": "device", "device": dev.id, "data": dev.snapshot()})
|
||||
return {"ok": True}
|
||||
|
||||
async def check_audio_test(self, dev: Device):
|
||||
"""On connect: a DSP still playing a test signal goes straight into the audio test (outputs lowered);
|
||||
one back on its analog inputs gets the faders saved by an interrupted test back."""
|
||||
try:
|
||||
if dev.test_active:
|
||||
if dev.test_saved is None:
|
||||
dev.test_saved = [ch["gain"] for ch in dev.state["outputs"]]
|
||||
self.set_test_level(dev, TEST_LEVEL)
|
||||
elif dev.test_saved is not None:
|
||||
self.restore_gains(dev)
|
||||
else:
|
||||
return
|
||||
await dev.flush()
|
||||
if not dev.demo:
|
||||
self.save()
|
||||
self.broadcast({"type": "device", "device": dev.id, "data": dev.snapshot()})
|
||||
except Exception as e: # noqa: BLE001 - connection lost meanwhile: checked again on the next connect
|
||||
log.warning("%s: audio test check failed: %s", dev.name, e)
|
||||
|
||||
def real_devices(self) -> list[Device]:
|
||||
return [d for d in self.devices.values() if not d.demo]
|
||||
|
||||
@@ -265,6 +331,7 @@ class App:
|
||||
port = self.demo_server.sockets[0].getsockname()[1]
|
||||
dev = Device("demo", f"Demo DSP {model}", "127.0.0.1", port, model, autoconnect=False, on_event=self.broadcast)
|
||||
dev.demo = True
|
||||
dev.on_online = self.check_audio_test
|
||||
self.devices[dev.id] = dev
|
||||
self.demo["enabled"] = True
|
||||
self.broadcast({"type": "device", "device": dev.id, "data": dev.snapshot()})
|
||||
@@ -456,6 +523,8 @@ class App:
|
||||
action = "/".join(g for g in m.groups()[1:] if g)
|
||||
if dev.demo and (method in ("DELETE", "PUT") or action in ("unlock", "lockcode")):
|
||||
raise RuntimeError("Not available for the demo device")
|
||||
if dev.test_active and action in ("preset/recall", "preset/store", "export", "import", "undo"):
|
||||
raise RuntimeError("Audio test is on – switch it off first")
|
||||
if self.demo["enabled"] and not dev.demo and action == "connect":
|
||||
raise RuntimeError("Demo mode is on – switch it off to connect the real DSPs")
|
||||
if method == "DELETE" and not action:
|
||||
@@ -519,6 +588,13 @@ class App:
|
||||
if action == "refresh":
|
||||
await dev.refresh()
|
||||
return {"ok": True}
|
||||
if action == "test":
|
||||
return await self.audio_test(dev, body)
|
||||
if action == "testlevel":
|
||||
if not dev.test_active:
|
||||
raise RuntimeError("Audio test is off")
|
||||
self.set_test_level(dev, body.get("db"))
|
||||
return {"ok": True, "db": dev.test_level}
|
||||
if action == "unlock": # hardware lock of the DSP, independent of the app lock
|
||||
code = str(body.get("code") or "").strip().upper()
|
||||
ok = await dev.unlock(code)
|
||||
|
||||
Reference in new issue
Block a user