Commit f9a9ce53 by PLN (Algolia)

fix(foundry): silence was the grader's optimum — 24 of 153 loops were empty

kitgate blocked the Fred pack and the reason turned out to be much worse than the
flag it raised. Measuring the flagged files: 24 of 153 shipped loops have a peak
between **-104 and -75 dBFS**. They are not quiet. They are digital silence. And
they graded **0.707 to 0.896 — tier A and B**.

The mechanism is that every sub-score in the loop rubric is vacuously perfect on
nothing. The seam between two silences is immaculate. The DC of silence is exactly
zero. Its zero crossings are trivially fine. Its bar length is whatever you cut it
to, so it is bar-exact by construction. Level carries 10% of the composite, so a
silent window scores 0.90 × perfect + 0.10 × nothing ≈ 0.90 and comes out tier A.
The composite had a **degenerate optimum at "no audio at all"**, and the finder,
doing exactly its job, walked into it: `fred_marea_bass` shipped three loops and
all three were silence, from a stem measuring -22 dBFS RMS at 54% activity.

Nothing downstream could catch it either. `_has_dead_bars` compares bars to EACH
OTHER, so it finds a loop that fades out and is blind to one that was never there —
a uniformly silent clip has a perfectly flat bar profile. This is the same trap as
the fade-out loops grading S, one level deeper.

So presence stops being a weighted term and becomes a **precondition**: peak below
`empty_dbfs` ⇒ flag `empty`, grade 0.0, tier D, and the existing MIN_TIER filter
drops it at export. The floor is -60 dBFS, and it is placed rather than tuned: the
pack's peaks run -104…-75 and then jump to -47.1, a **28.4 dB empty gap**, so -60
sits in clean air with nothing near it.

Three details the measurements forced:

* **Presence is measured on the CHANNELS, not the mono sum.** An anti-phase stereo
  pair sums to exact zero; calling that "empty" would both mis-report it and
  suppress the mono-incompatible flag, which is the one useful thing to say about
  it. (Caught by an existing test, which is why it is an existing test.)
* **`near-silent` (RMS < -45) is advisory and now excludes `empty`.** RMS on sparse
  material measures how much silence it contains, not whether it is silent — a hat
  loop peaking at -14.7 reads -48.8 RMS. Two questions, two lenses.
* **`mono-incompatible` no longer fires on silence.** Two independent noise floors
  are uncorrelated, so silence reads as maximally mono-incompatible; four of
  MAREA's flagged vox loops were simply empty.

`kitcheck` asks the presence question too, from its own code path, sharing the one
constant by import so the two lenses cannot drift apart.

Also: restage was not idempotent. Export stages in float and writes PCM_24, so the
peak read back off disk misses the ceiling by a quantization step, measures a hair
hot, and asks to be staged again — by -0.00003 dB, every run, requantizing every
file in the kit each time. `DEAD_BAND_DB = 0.05` (inaudible, four orders of
magnitude above the quantization floor). The dry run now reports 0 of 42 kits,
which is what it was supposed to say in the first place.

7 tests; suite 115 → 119.
parent bfc17e73
......@@ -40,6 +40,14 @@ THRESH = {
"rms_lo_dbfs": -28.0, # A-profile moderate-level band …
"rms_hi_dbfs": -12.0, # … RMS in here is ideal
"clip_dbfs": -0.1, # peak at/above this ⇒ clipping
# Peak below this ⇒ there is no audio here. NOT a taste threshold: every other
# sub-score is undefined on silence (the seam between two silences is perfect, the
# DC of silence is zero, the zero crossings are trivially fine), so a silent window
# scores 0.90 × perfect + 0.10 × nothing and comes out tier A. Measured on Fred
# again..'s pack: 24 of 153 cut loops were digital silence at -104…-75 dBFS peak,
# graded 0.707-0.896, and the next file up was -47.1 — a 28 dB empty gap, so the
# floor is placed inside it rather than tuned.
"empty_dbfs": -60.0,
"bars": (1, 2, 4, 8), # B-rule bar interpretations to test
"beats_per_bar": 4,
"tempo_lo": 60.0,
......@@ -256,6 +264,19 @@ def grade_array(y: np.ndarray, sr: int, *, path: str = "<array>",
grade = sum(w[k] * sub[k] for k in w)
flags: list[str] = []
# Presence is a PRECONDITION, not a weighted term. Everything above is a measure of
# how cleanly this audio loops, and all of it is vacuously true of silence — so the
# composite has a degenerate optimum at "nothing", which the finder duly walked into.
# A loop nobody can hear is not an A-grade loop whatever its seam looks like.
# Measured on the CHANNELS, not the mono sum: a deliberately wide stereo file can
# have almost no mono content — an anti-phase pair sums to exact zero — and calling
# that "empty" would both mis-report it and suppress the mono-incompatible flag that
# is the actually useful thing to say about it.
file_peak_db = float(20 * np.log10(float(np.max(np.abs(y))) + _EPS))
empty = file_peak_db < THRESH["empty_dbfs"]
if empty:
flags.append("empty")
grade = 0.0
if one_shot:
flags.append("one-shot")
if abs(dc) > THRESH["dc_ok"]:
......@@ -266,9 +287,15 @@ def grade_array(y: np.ndarray, sr: int, *, path: str = "<array>",
flags.append("off-grid")
if clipped:
flags.append("clipping")
if rms_db < -45:
if rms_db < -45 and not empty:
# RMS this low with a real peak means SPARSE, not silent — a hat loop with peaks
# at -14.7 reads -48.8 RMS because RMS measures how much silence it contains
# (`feedback_right_lens_per_control`). Advisory; `empty` is the blocking one.
flags.append("near-silent")
if (role == "bass" or y.ndim > 1) and corr < 0.2:
if (role == "bass" or y.ndim > 1) and corr < 0.2 and not empty:
# Interchannel correlation of two independent noise floors is ~0, so silence
# reads as maximally mono-incompatible; 4 of MAREA's flagged vox loops were
# just empty.
flags.append("mono-incompatible")
metrics = {
......
......@@ -50,6 +50,11 @@ DUPE_CORR = 0.93 # normalised cross-correlation above this ⇒ same sou
# have condemned 12 of 20 vocal loops for the crime of being vocal
# (`feedback_right_lens_per_control`), so the threshold is per family and `None` means
# "measured and reported, never flagged".
# Peak below this ⇒ nothing in the file. Same number as `grade.THRESH["empty_dbfs"]`,
# imported rather than restated so the two lenses cannot drift apart.
from .grade import THRESH as _GRADE_THRESH # noqa: E402
EMPTY_DBFS = _GRADE_THRESH["empty_dbfs"]
PERIODICITY_FLOOR = {
"drums": 0.30, # p25 is 0.615 — 0.30 catches only genuine outliers
"hits": 0.30,
......@@ -161,6 +166,16 @@ def check_cuts(cuts: list[dict]) -> list[KitCheck]:
if sr != 44100:
r.ok = False; r.problems.append(f"sample rate {sr}, expected 44100")
# Presence, asked here as well as in `grade`, on purpose: the two answers come
# from different code and a disagreement means one of them is broken. This is
# the check that would have caught 24 silent loops shipping at tier A/B — the
# dead-bar test could not, because it compares bars to EACH OTHER and a
# uniformly silent clip has a perfectly flat profile.
peak_db = float(20 * np.log10(float(np.max(np.abs(y))) + 1e-12))
if peak_db < EMPTY_DBFS:
r.ok = False
r.problems.append(f"empty: peak {peak_db:.1f} dBFS — there is no audio here")
bars, bpm = c["bars"], c["bpm"]
if bars > 0 and bpm > 0:
want = bars * 4 * 60.0 / bpm
......
......@@ -9,6 +9,8 @@ commercial master and land at full scale, which SuperDirt cannot use: amp = gain
This applies the SAME function `stempack` uses at export (`kit_headroom_gain` — one gain
per kit, attenuate only), so a kit restaged here is bit-identical to one cut with the
fix in place. Reads and rewrites in place, 24-bit, and reports what it changed.
Idempotent: a kit already within `DEAD_BAND_DB` of the ceiling is left untouched,
so re-running never requantizes audio for a gain change nobody can hear.
python3 restage.py <kit dir> [<kit dir> …] # explicit kits
python3 restage.py --cuts fred_kits.json # every kit named in a cut manifest
......@@ -28,6 +30,16 @@ sys.path.insert(0, str(Path(__file__).resolve().parent))
from engine.stempack import HEADROOM_CEILING_DB, kit_headroom_gain # noqa: E402
# An attenuation smaller than this is not applied. Export stages in float and writes
# PCM_24, so the peak that comes back off disk differs from the one it aimed at by up to
# a quantization step — enough that a freshly-staged kit measures a hair ABOVE the
# ceiling and asks to be staged again, by -0.00003 dB, forever. Each of those rounds is a
# real read-modify-write and a fresh 24-bit requantization of every file in the kit, so
# the non-idempotence is not merely untidy. 0.05 dB is inaudible and four orders of
# magnitude above the quantization floor.
DEAD_BAND_DB = 0.05
def _peak_db(path: Path) -> float:
y, _ = sf.read(str(path), always_2d=True, dtype="float32")
return float(20 * np.log10(np.max(np.abs(y)) + 1e-12))
......@@ -42,7 +54,7 @@ def restage_kit(kit_dir: Path, *, ceiling_db: float = HEADROOM_CEILING_DB,
peaks = [_peak_db(p) for p in wavs]
g = kit_headroom_gain(peaks, ceiling_db)
gain_db = 20 * np.log10(g) if g > 0 else 0.0
if g >= 1.0:
if g >= 1.0 or -gain_db < DEAD_BAND_DB:
return {"kit": kit_dir.name, "files": len(wavs), "gain_db": 0.0,
"changed": False, "peak_before": max(peaks)}
if not dry_run:
......
......@@ -64,9 +64,42 @@ def test_clipping_flagged():
assert "clipping" in G.grade_array(y, sr).flags
def test_near_silent_flagged():
def test_silence_is_empty_not_merely_near_silent():
"""-62 dBFS peak is not quiet material, it is nothing — and nothing used to grade A.
Every other sub-score is vacuously perfect on silence (a seam between two silences,
zero DC, trivial zero crossings), so the composite's optimum was "no audio at all"
and the finder walked straight into it: 24 of 153 loops cut from the Fred pack were
digital silence at -104…-75 dBFS, graded 0.707-0.896."""
y, sr = _sine(amp=0.0008) # ~ -62 dBFS
assert "near-silent" in G.grade_array(y, sr).flags
g = G.grade_array(y, sr)
assert "empty" in g.flags
assert g.grade == 0.0 and g.tier == "D"
def test_a_sparse_loop_is_near_silent_but_not_empty():
"""RMS and peak answer different questions. A hat loop with peaks at -14.7 dBFS reads
-48.8 RMS because RMS measures how much SILENCE a sparse signal contains — that is
advisory, not disqualifying, and it must not be confused with an empty file."""
sr, rng = 44100, np.random.RandomState(0)
y = np.zeros(sr * 4)
for i in range(4): # four very short ticks in four seconds
j = i * sr
y[j:j + 32] = 0.3 * rng.randn(32) * np.hanning(32)
g = G.grade_array(y, sr)
assert g.metrics["peak_dbfs"] - g.metrics["rms_dbfs"] > 30 # a real sparse crest
g = G.grade_array(y, sr)
assert "near-silent" in g.flags # RMS is way down …
assert "empty" not in g.flags # … but there is clearly audio here
assert g.grade > 0.0
def test_an_anti_phase_pair_is_mono_incompatible_not_empty():
"""The presence test has to look at the channels, not the mono sum: L/-R sums to
exact zero, and calling that "empty" would suppress the one flag worth raising."""
mono, sr = _sine(periods=200)
g = G.grade_array(np.stack([mono, -mono]), sr)
assert "empty" not in g.flags
assert "mono-incompatible" in g.flags
def test_mono_compatible_vs_anti_phase():
......
......@@ -304,3 +304,63 @@ def test_an_orphan_that_sorts_last_is_still_reported_but_indices_hold(tmp_path):
ok, lines = _kitgate().check_indices(cuts)
assert not ok
assert "indices still line up" in " ".join(lines)
# ── restaging is idempotent ──────────────────────────────────────────────────
def _restage():
import importlib.util
spec = importlib.util.spec_from_file_location("restage", Path(__file__).parent.parent / "restage.py")
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
def test_restaging_a_staged_kit_is_a_no_op(tmp_path):
"""Export stages in float and writes PCM_24, so the peak read back off disk sits a
quantization step away from the ceiling it aimed at — which read as "still too hot"
and asked for another -0.00003 dB, every run, requantizing every file each time."""
r = _restage()
y = _click_track()
y = y / (np.max(np.abs(y)) + 1e-9)
_write(tmp_path, "00_kit_4b.wav", y)
_write(tmp_path, "01_kit_4b.wav", y * 0.5)
first = r.restage_kit(tmp_path)
assert first["changed"] and first["gain_db"] == pytest.approx(-6.0, abs=0.05)
second = r.restage_kit(tmp_path)
assert not second["changed"], second # the run that used to loop forever
assert r.restage_kit(tmp_path, dry_run=True)["changed"] is False
def test_a_kit_that_really_is_too_hot_is_still_restaged(tmp_path):
"""The dead band must not swallow a gain that matters — 0.05 dB, not 0.5."""
r = _restage()
y = _click_track()
_write(tmp_path, "00_kit_4b.wav", y / (np.max(np.abs(y)) + 1e-9) * 0.9)
out = r.restage_kit(tmp_path, dry_run=True)
assert out["changed"] and out["gain_db"] < -5.0
# ── silence is not a loop ────────────────────────────────────────────────────
def test_a_silent_loop_passes_every_structural_test_and_is_still_rejected(tmp_path):
"""The deepest version of the dead-bar trap. A window of pure silence is bar-exact,
has a perfect seam, zero DC, trivial zero crossings — and a perfectly FLAT bar
profile, so the dead-bar check (which compares bars to each other) sees nothing
wrong. 24 of the 153 loops cut from the Fred pack were digital silence at -104…-75
dBFS peak, graded 0.707-0.896, tier A and B."""
sr = SR
n = int(round(4 * 4 * 60.0 / 120.0 * sr))
quiet = (np.random.RandomState(0).randn(n) * 1e-5).astype(np.float32) # ~ -95 dBFS
p = _write(tmp_path, "00_bass_4b.wav", quiet)
lv = K.bar_levels(quiet, sr, 4)
assert max(lv) - min(lv) < K.DEAD_BAR_DB # the old check sees a clean loop
(c,) = K.check_cuts([_cut(p, family="bass")])
assert not c.ok
assert any("empty" in x for x in c.problems), c.problems
def test_the_two_presence_lenses_use_one_number():
"""kitcheck and grade both decide 'is there audio here'. If they used two constants
one would eventually be edited alone, and the gate would pass what the grader
failed."""
from engine import grade as G
assert K.EMPTY_DBFS == G.THRESH["empty_dbfs"]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment