thecollabagepatch commited on
Commit
7ae8a62
·
1 Parent(s): 7457794

much closer now with the loudness match inside ableton

Browse files
Files changed (1) hide show
  1. one_shot_generation.py +19 -10
one_shot_generation.py CHANGED
@@ -225,7 +225,7 @@ def apply_barwise_loudness_match(
225
  """
226
  Bar-locked loudness matching that establishes the correct starting level
227
  then maintains consistency. Only the first bar is matched to the reference;
228
- subsequent bars maintain relative dynamics while preventing drift.
229
  """
230
  sr = int(out.sample_rate)
231
  spb = (60.0 / float(bpm)) * int(beats_per_bar)
@@ -244,7 +244,7 @@ def apply_barwise_loudness_match(
244
 
245
  from utils import match_loudness_to_reference
246
 
247
- # Measure reference loudness once (use first bar's worth if reference is long)
248
  ref_bar_len = min(ref.shape[0], bar_len)
249
  ref_bar = au.Waveform(ref[:ref_bar_len], sr)
250
 
@@ -254,7 +254,10 @@ def apply_barwise_loudness_match(
254
  n_bars = max(1, int(np.ceil(need / float(bar_len))))
255
  ramp = int(max(0, round(smooth_ms * sr / 1000.0)))
256
  min_lufs_samples = int(0.4 * sr)
257
-
 
 
 
258
  for i in range(n_bars):
259
  s = i * bar_len
260
  e = min(need, s + bar_len)
@@ -262,21 +265,27 @@ def apply_barwise_loudness_match(
262
  break
263
 
264
  bar_samples = e - s
265
- tgt_bar = au.Waveform(out_adj[s:e], sr)
266
 
267
- # First bar: match to reference to establish starting level
268
  if i == 0:
269
  effective_method = "rms" if bar_samples < min_lufs_samples else method
270
  matched_bar, stats = match_loudness_to_reference(
271
  ref_bar, tgt_bar, method=effective_method, headroom_db=headroom_db
272
  )
 
 
 
 
 
 
 
 
273
  else:
274
- # Subsequent bars: just copy through (preserves model's dynamics)
275
- matched_bar = tgt_bar
276
 
277
- g = matched_bar.samples.astype(np.float32, copy=False)
278
-
279
- # Calculate gain that was applied
280
  if tgt_bar.samples.size > 0:
281
  eps = 1e-12
282
  g_lin = float(np.sqrt((np.mean(g**2) + eps) / (np.mean(tgt_bar.samples**2) + eps)))
 
225
  """
226
  Bar-locked loudness matching that establishes the correct starting level
227
  then maintains consistency. Only the first bar is matched to the reference;
228
+ subsequent bars use the same gain to maintain relative dynamics.
229
  """
230
  sr = int(out.sample_rate)
231
  spb = (60.0 / float(bpm)) * int(beats_per_bar)
 
244
 
245
  from utils import match_loudness_to_reference
246
 
247
+ # Measure reference loudness once
248
  ref_bar_len = min(ref.shape[0], bar_len)
249
  ref_bar = au.Waveform(ref[:ref_bar_len], sr)
250
 
 
254
  n_bars = max(1, int(np.ceil(need / float(bar_len))))
255
  ramp = int(max(0, round(smooth_ms * sr / 1000.0)))
256
  min_lufs_samples = int(0.4 * sr)
257
+
258
+ # Calculate gain from bar 0 matching
259
+ first_bar_gain_linear = 1.0
260
+
261
  for i in range(n_bars):
262
  s = i * bar_len
263
  e = min(need, s + bar_len)
 
265
  break
266
 
267
  bar_samples = e - s
268
+ tgt_bar = au.Waveform(y[s:e], sr) # Always read from ORIGINAL
269
 
270
+ # First bar: match to reference to establish gain
271
  if i == 0:
272
  effective_method = "rms" if bar_samples < min_lufs_samples else method
273
  matched_bar, stats = match_loudness_to_reference(
274
  ref_bar, tgt_bar, method=effective_method, headroom_db=headroom_db
275
  )
276
+
277
+ # Calculate the linear gain that was applied
278
+ eps = 1e-12
279
+ first_bar_gain_linear = float(np.sqrt(
280
+ (np.mean(matched_bar.samples**2) + eps) /
281
+ (np.mean(tgt_bar.samples**2) + eps)
282
+ ))
283
+ g = matched_bar.samples.astype(np.float32, copy=False)
284
  else:
285
+ # Subsequent bars: apply the same gain from bar 0
286
+ g = (tgt_bar.samples * first_bar_gain_linear).astype(np.float32, copy=False)
287
 
288
+ # Calculate gain in dB for stats
 
 
289
  if tgt_bar.samples.size > 0:
290
  eps = 1e-12
291
  g_lin = float(np.sqrt((np.mean(g**2) + eps) / (np.mean(tgt_bar.samples**2) + eps)))