From 6d467d4f2dac4693bcfd2f00769173985435381d Mon Sep 17 00:00:00 2001 From: Givou Date: Mon, 2 Nov 2026 21:24:32 +0100 Subject: [PATCH] DJ Brake: learn the limiter from its onset, for the whole DSP Replaying real tracks through a limiter model showed the level during limiting teaches little. One hit now counts for all outputs of a DSP. Co-Authored-By: Claude Opus 5.5 --- server.py | 16 ++++++++++------ tests/test_protocol.py | 24 ++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/server.py b/server.py index 7d1a28c..a934f08 100644 --- a/server.py +++ b/server.py @@ -331,7 +331,7 @@ class App: self.dj_rooms: dict = {} # device id -> dB left to the nearest watched limiter, False = nothing watched self.dj_avgs: dict = {} # the same for the average level self.dj_env: dict = {} # "device:output" -> peak and average of the level - self.dj_cal: dict = {} # "device:output" -> dB the limiter sits below its threshold on the meter, learned from hits + self.dj_cal: dict = {} # device id -> dB the limiters sit below their threshold on the meter, learned from hits self.dj_cal_saved = 0.0 self.dj_streams = 0 self.logo = "" @@ -385,8 +385,8 @@ class App: self.djbrake = bool(cfg.get("djbrake")) self.dj_ignore = clean_dj_ignore(cfg.get("dj_ignore")) self.dj_margin = clean_dj_margin(cfg.get("dj_margin")) - self.dj_cal = {str(k)[:80]: float(v) for k, v in (cfg.get("dj_cal") if isinstance(cfg.get("dj_cal"), dict) else {}).items() - if type(v) in (int, float)} + self.dj_cal = {str(k)[:64]: float(v) for k, v in (cfg.get("dj_cal") if isinstance(cfg.get("dj_cal"), dict) else {}).items() + if type(v) in (int, float) and ":" not in str(k)} self.dj_hint = bool(cfg.get("dj_hint", True)) self.dj_hint_en = bool(cfg.get("dj_hint_en", True)) if "logo" in cfg: @@ -970,10 +970,14 @@ class App: e["peak"] = max(db, e["peak"] - DJ_PEAK_FALL * dt) e["pow"] += (level * level - e["pow"]) * (1 - math.exp(-dt / DJ_AVG_TAU)) thr = P.threshold_to_db(threshold) - if limit and e["peak"] > -70: # the limiter acts: this is where it really sits on the meter + # the moment a limiter starts is where it really sits on the meter. Later the level is what the limiter + # left of it. One DSP has one meter, so this counts for all its outputs + dev_id = key.rsplit(":", 1)[0] + if limit and not e.get("acting") and e["peak"] > -70: seen = max(-6.0, min(30.0, thr - e["peak"])) - self.dj_cal[key] = seen if key not in self.dj_cal else self.dj_cal[key] + (seen - self.dj_cal[key]) * (1 - math.exp(-dt)) - top = thr - self.dj_cal.get(key, 0.0) - self.dj_margin + self.dj_cal[dev_id] = (self.dj_cal[dev_id] + seen) / 2 if dev_id in self.dj_cal else seen + e["acting"] = limit + top = thr - self.dj_cal.get(dev_id, 0.0) - self.dj_margin left = 0.0 if limit else max(0.0, top - e["peak"]) if e["peak"] > -70 else None mean = max(0.0, top - 10 * math.log10(e["pow"])) if e["pow"] > 1e-7 else None return left, mean, limit or bool(self.dj_margin and db >= top) diff --git a/tests/test_protocol.py b/tests/test_protocol.py index 1787bc0..abb72b0 100644 --- a/tests/test_protocol.py +++ b/tests/test_protocol.py @@ -224,7 +224,7 @@ def test_dj_brake_follows_music(): assert 10 < app.dj_rooms["x"] < 14 # the limiter acts at -12 on the meter although the threshold says 0: learned, and the distance is true afterwards note(-12, 110.0, True) - assert abs(app.dj_cal["x:0"] - 12) < 3 and app.dj_rooms["x"] == 0 + assert abs(app.dj_cal["x"] - 12) < 3 and app.dj_rooms["x"] == 0 assert app.dj_hit == 110.0 for i in range(30): note(-30, 120.0 + i) @@ -238,7 +238,27 @@ def test_dj_brake_follows_music(): app.save() again = server.App(Path(d) / "c.json") again.load() - assert abs(again.dj_cal["x:0"] - app.dj_cal["x:0"]) < 0.01 + assert abs(again.dj_cal["x"] - app.dj_cal["x"]) < 0.01 + + +def test_dj_brake_one_hit_counts_for_the_whole_dsp(): + import server, tempfile + from types import SimpleNamespace + with tempfile.TemporaryDirectory() as d: + app = server.App(Path(d) / "c.json") + out = {"mute": False, "limit": {"threshold": 180}} + app.devices["x"] = SimpleNamespace(id="x", demo=True, state={"outputs": [dict(out), dict(out)]}, status="online") + lv = lambda db: 10 ** (db / 20) + app.dj_ignore = {"x": [1]} + app.dj_note({"device": "x", "out": [lv(-12), lv(-40)], "limit": [True, False]}, now=100.0) + assert app.dj_cal == {"x": 12.0} + # the level the limiter leaves over while it keeps acting teaches nothing + app.dj_note({"device": "x", "out": [lv(-20), lv(-40)], "limit": [True, False]}, now=100.2) + assert app.dj_cal == {"x": 12.0} + # another output of this DSP has never limited, its distance is true anyway + app.dj_ignore = {"x": [0]} + app.dj_note({"device": "x", "out": [lv(-40), lv(-18)], "limit": [False, False]}, now=101.0) + assert abs(app.dj_rooms["x"] - 6) < 0.01 def test_reset_config():