Commit
·
e87e83d
1
Parent(s):
1355fb6
pyloudnorm is yelling at us
Browse files- one_shot_generation.py +18 -3
one_shot_generation.py
CHANGED
|
@@ -248,18 +248,33 @@ def apply_barwise_loudness_match(
|
|
| 248 |
out_adj = y.copy()
|
| 249 |
n_bars = max(1, int(np.ceil(need / float(bar_len))))
|
| 250 |
ramp = int(max(0, round(smooth_ms * sr / 1000.0)))
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
for i in range(n_bars):
|
| 253 |
s = i * bar_len
|
| 254 |
e = min(need, s + bar_len)
|
| 255 |
-
if e <= s:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
ref_bar = au.Waveform(ref_tiled[s:e], sr)
|
| 258 |
tgt_bar = au.Waveform(out_adj[s:e], sr)
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
matched_bar, stats = match_loudness_to_reference(
|
| 261 |
-
ref_bar, tgt_bar, method=
|
| 262 |
)
|
|
|
|
| 263 |
# compute linear gain we actually applied
|
| 264 |
g = matched_bar.samples.astype(np.float32, copy=False)
|
| 265 |
if tgt_bar.samples.size > 0:
|
|
@@ -274,7 +289,7 @@ def apply_barwise_loudness_match(
|
|
| 274 |
if i > 0 and ramp > 0:
|
| 275 |
ramp_len = min(ramp, e - s) # Don't ramp longer than the bar
|
| 276 |
t = np.linspace(0.0, 1.0, ramp_len, dtype=np.float32)[:, None]
|
| 277 |
-
# Blend from previous gain
|
| 278 |
out_adj[s:s+ramp_len] = (1.0 - t) * out_adj[s:s+ramp_len] + t * g[:ramp_len]
|
| 279 |
out_adj[s+ramp_len:e] = g[ramp_len:e-s]
|
| 280 |
else:
|
|
|
|
| 248 |
out_adj = y.copy()
|
| 249 |
n_bars = max(1, int(np.ceil(need / float(bar_len))))
|
| 250 |
ramp = int(max(0, round(smooth_ms * sr / 1000.0)))
|
| 251 |
+
|
| 252 |
+
# Minimum duration for LUFS measurement (400ms)
|
| 253 |
+
min_lufs_samples = int(0.4 * sr)
|
| 254 |
|
| 255 |
for i in range(n_bars):
|
| 256 |
s = i * bar_len
|
| 257 |
e = min(need, s + bar_len)
|
| 258 |
+
if e <= s:
|
| 259 |
+
break
|
| 260 |
+
|
| 261 |
+
bar_duration = (e - s) / float(sr)
|
| 262 |
+
bar_samples = e - s
|
| 263 |
|
| 264 |
ref_bar = au.Waveform(ref_tiled[s:e], sr)
|
| 265 |
tgt_bar = au.Waveform(out_adj[s:e], sr)
|
| 266 |
|
| 267 |
+
# Skip loudness matching for bars shorter than LUFS minimum
|
| 268 |
+
if method in ("auto", "lufs") and bar_samples < min_lufs_samples:
|
| 269 |
+
# Fallback: use RMS for short segments, or skip entirely
|
| 270 |
+
effective_method = "rms"
|
| 271 |
+
else:
|
| 272 |
+
effective_method = method
|
| 273 |
+
|
| 274 |
matched_bar, stats = match_loudness_to_reference(
|
| 275 |
+
ref_bar, tgt_bar, method=effective_method, headroom_db=headroom_db
|
| 276 |
)
|
| 277 |
+
|
| 278 |
# compute linear gain we actually applied
|
| 279 |
g = matched_bar.samples.astype(np.float32, copy=False)
|
| 280 |
if tgt_bar.samples.size > 0:
|
|
|
|
| 289 |
if i > 0 and ramp > 0:
|
| 290 |
ramp_len = min(ramp, e - s) # Don't ramp longer than the bar
|
| 291 |
t = np.linspace(0.0, 1.0, ramp_len, dtype=np.float32)[:, None]
|
| 292 |
+
# Blend from previous gain to current bar's gain
|
| 293 |
out_adj[s:s+ramp_len] = (1.0 - t) * out_adj[s:s+ramp_len] + t * g[:ramp_len]
|
| 294 |
out_adj[s+ramp_len:e] = g[ramp_len:e-s]
|
| 295 |
else:
|