Measuring tool for meter, gain, threshold and limiter on real hardware

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
GivouandClaude Opus 5.5 committed 2026-11-02 21:34:29 +01:00
1 parent 6d467d4f2d
commit 4cd771eb5c
2 files changed
+265

No files matched your search

+2
View File
@@ -115,6 +115,8 @@ python sim.py --model 408 --port 9762
python server.py --port 8080 --config <some temp file>.json python server.py --port 8080 --config <some temp file>.json
``` ```
`tools/dsp_measure.py` measures on real hardware how meter, gain, threshold and limiter relate (lowers only, restores itself).
Log in with PIN `1234`, add devices `127.0.0.1:9761` / `:9762`. Check both languages after UI changes (⚙ → Log in with PIN `1234`, add devices `127.0.0.1:9761` / `:9762`. Check both languages after UI changes (⚙ →
Language / Sprache). For the desktop app run `python desktop.py` (needs `pywebview`). Language / Sprache). For the desktop app run `python desktop.py` (needs `pywebview`).
+263
View File
@@ -0,0 +1,263 @@
"""Finds out how a real DSP's output meter relates to its limiter threshold and output gain.
Play steady music into the DSP, close every other control software and run:
python tools/dsp_measure.py --usb
python tools/dsp_measure.py --host 192.168.1.101
It only ever lowers one output's gain (by 6 dB) and limiter threshold, never raises anything above where it was, stores
nothing in a preset slot and puts both back at the end, also on Ctrl+C. A preset export is written first as a backup.
Takes about three minutes. The result goes to dsp-measure-<time>.json.
"""
import argparse
import asyncio
import json
import math
import signal
import statistics
import sys
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from dsp import preset # noqa: E402
from dsp import protocol as P # noqa: E402
from dsp.device import Device # noqa: E402
SIGNAL_MIN_DB = -50.0
THRESHOLD_FLOOR = 60 # raw, -60 dB: below that something is wrong
def db(power: float) -> float:
return 10 * math.log10(power) if power > 1e-12 else -120.0
class Run:
def __init__(self, dev: Device):
self.dev = dev
self.polls: list[dict] = []
self.o = 0
self.ins: list[int] = []
self.gain = 0
self.thr = 0
def on_event(self, ev: dict):
if ev["type"] == "meters" and self.dev.state:
self.polls.append({"t": time.monotonic(), "out": ev["out"], "in": ev["in"], "limit": ev["limit"],
"gain": self.gain, "thr": self.thr})
async def set(self, gain: int | None = None, thr: int | None = None):
if gain is not None:
self.gain = gain
self.dev.set_param("out", self.o, "gain", gain)
if thr is not None:
self.thr = thr
self.dev.set_param("out", self.o, "limit.threshold", thr)
await self.dev.flush()
def window(self, since: float) -> list[dict]:
return [p for p in self.polls if p["t"] >= since]
def level(self, polls: list[dict]) -> dict:
"""Mean power and upper percentile of the output, and the mean power of the inputs feeding it."""
out = [p["out"][self.o] for p in polls]
loud = sorted(20 * math.log10(v) if v > 0 else -120.0 for v in out) or [-120.0]
inp = [sum(p["in"][i] ** 2 for i in self.ins) for p in polls]
return {"mean_db": round(db(statistics.fmean(v * v for v in out)) if out else -120.0, 2),
"p95_db": round(loud[int(len(loud) * 0.95) - 1 if len(loud) > 1 else 0], 2), "max_db": round(loud[-1], 2),
"in_db": round(db(statistics.fmean(inp)) if inp else -120.0, 2), "polls": len(polls),
"limit_share": round(sum(bool(p["limit"][self.o]) for p in polls) / max(1, len(polls)), 2)}
async def hold(self, seconds: float, settle: float = 1.0) -> dict:
await asyncio.sleep(settle)
since = time.monotonic()
await asyncio.sleep(seconds)
return self.level(self.window(since))
async def find_limit(self, start: int, gain: int) -> dict:
"""Lowers the threshold step by step until the limiter acts, then goes back up to where it was."""
await self.set(gain=gain)
await asyncio.sleep(1.0)
raw = start
result = {"gain_db": P.gain_to_db(gain), "hit": False}
while raw >= THRESHOLD_FLOOR:
await self.set(thr=raw)
since = time.monotonic()
await asyncio.sleep(0.45)
if any(p["limit"][self.o] for p in self.window(since)):
before = self.level(self.window(since - 3.0))
result.update(hit=True, threshold_db=P.threshold_to_db(raw), before=before,
offset_p95_db=round(P.threshold_to_db(raw) - before["p95_db"], 2),
offset_max_db=round(P.threshold_to_db(raw) - before["max_db"], 2))
break
raw -= 2
await self.set(thr=self.start_thr, gain=self.start_gain)
await asyncio.sleep(3.0) # let the limiter release
return result
def conclude(res: dict) -> list[str]:
lines = []
d = res.get("gain_test", {}).get("meter_change_db")
if d is not None:
if abs(d + 6) < 1.5:
lines.append(f"Meter is AFTER the output gain: -6 dB gain moved it by {d:+.1f} dB.")
elif abs(d) < 1.5:
lines.append(f"Meter is BEFORE the output gain: -6 dB gain moved it by {d:+.1f} dB.")
else:
lines.append(f"Meter and gain: unclear, -6 dB gain moved it by {d:+.1f} dB.")
hits = [r for r in res.get("limit_rounds", []) if r.get("hit")]
plain = [r["offset_p95_db"] for r in hits if r["gain_db"] == res["start"]["gain_db"]]
if plain:
lines.append(f"Threshold minus meter at the moment the limiter starts: {statistics.fmean(plain):+.1f} dB "
f"(rounds: {plain}, spread {max(plain) - min(plain):.1f} dB).")
low = [r for r in hits if r["gain_db"] != res["start"]["gain_db"]]
if plain and low:
base = statistics.fmean(r["threshold_db"] for r in hits if r["gain_db"] == res["start"]["gain_db"])
moved = statistics.fmean(r["threshold_db"] for r in low) - base
lines.append(f"With -6 dB gain the limiter started {moved:+.1f} dB in threshold: "
+ ("it works AFTER the gain." if abs(moved + 6) < 2 else "it works BEFORE the gain." if abs(moved) < 2 else "unclear."))
c = res.get("limiting")
if c:
drop = c["limiting"]["mean_db"] - c["free"]["mean_db"]
lines.append(f"Holding the threshold 6 dB below the start of limiting changed the meter by {drop:+.1f} dB: "
+ ("the meter shows the level AFTER the limiter." if drop < -1.5 else "the meter shows the level BEFORE the limiter."))
lines.append(f"While limiting, threshold minus meter: p95 {c['threshold_db'] - c['limiting']['p95_db']:+.1f} dB, "
f"mean {c['threshold_db'] - c['limiting']['mean_db']:+.1f} dB.")
return lines
async def measure(run: Run, args) -> dict:
dev = run.dev
print("Listening for music ...", flush=True)
since = time.monotonic()
await asyncio.sleep(5)
polls = run.window(since)
if not polls:
raise RuntimeError("no meter readings from the DSP")
outs = dev.state["outputs"]
power = [statistics.fmean(p["out"][i] ** 2 for p in polls) for i in range(len(outs))]
if args.output:
run.o = args.output - 1
else:
run.o = max((i for i in range(len(outs)) if not outs[i]["mute"]), key=lambda i: power[i], default=0)
ch = outs[run.o]
run.ins = [i for i in range(len(dev.state["inputs"])) if ch["matrix"] >> i & 1] or list(range(len(dev.state["inputs"])))
if ch["mute"] or db(power[run.o]) < SIGNAL_MIN_DB:
raise RuntimeError(f"Out {run.o + 1} has no signal ({db(power[run.o]):.0f} dB) or is muted: play music into the DSP and run again")
run.gain = run.start_gain = ch["gain"]
run.thr = run.start_thr = ch["limit"]["threshold"]
gain6 = P.db_to_gain(P.gain_to_db(run.gain) - 6)
print(f"Using Out {run.o + 1}: gain {P.gain_to_db(run.gain):+.1f} dB, limiter threshold {P.threshold_to_db(run.thr):+.1f} dB, "
f"level {db(power[run.o]):.1f} dB", flush=True)
res = {"device": dev.info, "model": dev.model_name, "output": run.o + 1, "inputs_feeding": [i + 1 for i in run.ins],
"start": {"gain_db": P.gain_to_db(run.gain), "threshold_db": P.threshold_to_db(run.thr), "limit": ch["limit"],
"level_mean_db": round(db(power[run.o]), 2)}}
print("1/3 Output gain -6 dB, three times ...", flush=True)
rows = []
for _ in range(3):
await run.set(gain=run.start_gain)
a = await run.hold(5)
await run.set(gain=gain6)
b = await run.hold(5)
rows.append({"normal": a, "lowered": b, "change_db": round((b["mean_db"] - b["in_db"]) - (a["mean_db"] - a["in_db"]), 2)})
await run.set(gain=run.start_gain)
res["gain_test"] = {"rounds": rows, "meter_change_db": round(statistics.fmean(r["change_db"] for r in rows), 2)}
print("2/3 Lowering the limiter threshold until it acts, several rounds ...", flush=True)
rounds, start = [], run.start_thr
for gain in (run.start_gain, run.start_gain, run.start_gain, gain6, gain6):
r = await run.find_limit(start, gain)
rounds.append(r)
print(" ", {k: r.get(k) for k in ("gain_db", "hit", "threshold_db", "offset_p95_db")}, flush=True)
if r["hit"]: # no need to walk all the way down again
start = min(run.start_thr, int(r["threshold_db"] * 2 + 180) + 12)
res["limit_rounds"] = rounds
hits = [r for r in rounds if r["hit"] and r["gain_db"] == res["start"]["gain_db"]]
if hits:
print("3/3 Holding the limiter on for a moment ...", flush=True)
hit_db = statistics.fmean(r["threshold_db"] for r in hits)
raw = max(THRESHOLD_FLOOR, round((hit_db - 6) * 2 + 180))
free = await run.hold(6)
await run.set(thr=raw)
limiting = await run.hold(8)
await run.set(thr=run.start_thr)
res["limiting"] = {"threshold_db": P.threshold_to_db(raw), "free": free, "limiting": limiting}
res["conclusions"] = conclude(res)
return res
async def main(args):
run: Run | None = None
dev = Device("m", "measure", args.host or "", args.port, args.model, autoconnect=False, transport="usb" if args.usb else "tcp",
on_event=lambda ev: run.on_event(ev))
run = Run(dev)
task = asyncio.current_task()
if sys.platform != "win32":
for sig in (signal.SIGTERM, signal.SIGHUP):
asyncio.get_running_loop().add_signal_handler(sig, task.cancel)
stamp = time.strftime("%Y%m%d-%H%M%S")
outdir = Path(args.dir)
res, error = {}, ""
dev.start()
try:
for _ in range(100):
if dev.status == "online" and dev.state:
break
await asyncio.sleep(0.2)
else:
raise RuntimeError(f"DSP not reached ({dev.status}: {dev.error}). Is other control software still open?")
doc = await dev.export_preset()
backup = outdir / f"dsp-measure-{stamp}-backup.json"
backup.write_text(json.dumps(doc), encoding="utf-8")
before = json.loads(json.dumps(dev.state))
print(f"Connected: {dev.info}. Backup: {backup}", flush=True)
try:
res = await measure(run, args)
except RuntimeError as e:
error = str(e)
except asyncio.CancelledError:
error = "interrupted"
finally:
# always back to where it was, whatever happened above
if hasattr(run, "start_gain"):
await run.set(gain=run.start_gain, thr=run.start_thr)
await asyncio.sleep(0.5)
await dev.refresh()
diffs = preset.differences(before, dev.state)
res["restored"] = not diffs
if diffs:
res["not_restored"] = diffs[:20]
finally:
await dev.stop()
res["error"] = error
res["polls"] = [[round(p["t"] - run.polls[0]["t"], 2), round(db(p["out"][run.o] ** 2), 1), round(db(sum(p["in"][i] ** 2 for i in run.ins)), 1),
int(p["limit"][run.o]), P.gain_to_db(p["gain"]), P.threshold_to_db(p["thr"])] for p in run.polls] if run.ins else []
out = outdir / f"dsp-measure-{stamp}.json"
out.write_text(json.dumps(res), encoding="utf-8")
print("\nResult:", out)
for line in res.get("conclusions", []):
print(" *", line)
if error:
print(" ! Stopped:", error)
print(" * Settings restored:", "yes" if res["restored"] else f"NO, import the backup: {res.get('not_restored')}")
return 0 if res["restored"] and not error else 1
if __name__ == "__main__":
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("--usb", action="store_true", help="connect over USB")
ap.add_argument("--host", help="IP address of the DSP")
ap.add_argument("--port", type=int, default=P.PORT)
ap.add_argument("--model", default="auto", help="auto, 204 or 408")
ap.add_argument("--output", type=int, help="output number to use, default: the loudest one")
ap.add_argument("--dir", default=".", help="where to write the files")
a = ap.parse_args()
if not a.usb and not a.host:
ap.error("say --usb or --host")
sys.exit(asyncio.run(main(a)))