Commit ·
182105b
1
Parent(s): 2e73e3b
Level the finished take: even section loudness before it is served
Browse filesChunks render independently and the model's delivery level drifts between
them, so one global normalization gain still left louder and quieter
stretches. When the full take lands, ride a slow gain envelope over it:
3 s windows every second, gated to speech, each nudged toward the take's
median loudness (capped at ±8 dB), smoothed, then soft-limited. Overall
loudness is unchanged; Studio mode still sets the delivery target. Applied
once at storage time, so playback in the result stage and every export
inherit it. Fails open: any error serves the take as generated.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017bjX8vNkBk9Rjt2SToVF75
Files changed (3)
- README.md+1
- app.py+94
- static/app.js+1-1
57 unmodified lines58596061626357 unmodified lines2. Renders chunks in batched waves on one A100 (12 at a time for 1.5B, 6 for 7B), backing off to smaller waves on out-of-memory3. Runs a quality gate on every chunk — speaking-rate band, silence fraction, spectral flatness, and a replay detector for cloned voices — and re-rolls failures on a fresh seed4. Crossfades chunks together (0.25 s seams) and measures each turn's onset from the audio for the synced transcriptMeasured on production (A100-40GB, VibeVoice-1.5B):57 unmodified lines5859606162636457 unmodified lines2. Renders chunks in batched waves on one A100 (12 at a time for 1.5B, 6 for 7B), backing off to smaller waves on out-of-memory3. Runs a quality gate on every chunk — speaking-rate band, silence fraction, spectral flatness, and a replay detector for cloned voices — and re-rolls failures on a fresh seed4. Crossfades chunks together (0.25 s seams) and measures each turn's onset from the audio for the synced transcript5. Once the full take lands, evens out section-to-section loudness with a slow gain ride (4 s windows, at most ±6 dB, soft-limited), so playback and every export sit at a consistent level from start to finishMeasured on production (A100-40GB, VibeVoice-1.5B):
1043 unmodified lines104410451046104710481049120 unmodified lines1170117111721173117411751043 unmodified linesaudio_payload = event.pop("audio", None)if audio_payload is not None:sample_rate, audio_array = audio_payloadwav_bytes = _encode_wav(sample_rate, audio_array)audio_id = uuid.uuid4().hexAUDIO_STORE[audio_id] = (time.time(), wav_bytes)120 unmodified linesreturn 20 * np.log10(max(gated, 1e-9))# Noise cleanup: STFT spectral gating. The noise print is the per-bin 10th# percentile of magnitude over time — a stationary-floor estimate that speech# (sparse in any one bin) barely lifts. Bins are attenuated at most down to1043 unmodified lines10441045104610471048104910501051105210531054120 unmodified lines117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691043 unmodified linesaudio_payload = event.pop("audio", None)if audio_payload is not None:sample_rate, audio_array = audio_payloadtry:audio_array, level_stats = _level_take(sample_rate, audio_array)print(f"LEVELED take {level_stats}", flush=True)except Exception as level_err: # never lose a take to post-processingprint(f"Take leveling failed (serving as generated): {level_err}", flush=True)wav_bytes = _encode_wav(sample_rate, audio_array)audio_id = uuid.uuid4().hexAUDIO_STORE[audio_id] = (time.time(), wav_bytes)120 unmodified linesreturn 20 * np.log10(max(gated, 1e-9))# --- Take leveling (2026-09-12) ---------------------------------------------# A take is stitched from chunks rendered independently, and the model's# delivery level drifts from chunk to chunk (and within long monologues), so# a single normalization gain leaves sections that are noticeably louder or# quieter than their neighbours. Once the full take lands, ride a slow gain# envelope over it: measure speech loudness in overlapping windows, nudge each# window toward the take's median loudness (never more than LEVEL_MAX_DB# either way), interpolate the gains smoothly so nothing pumps, and soft-limit# peaks. Overall loudness is left where it was — the Studio sound mode still# owns the delivery target — so this only evens the ride, not the level.LEVEL_WINDOW_S = 3.0 # loudness windowLEVEL_HOP_S = 1.0 # window hop (3 s windows every second)LEVEL_MAX_DB = 8.0 # cap on per-window correctionLEVEL_FRAME_S = 0.4 # RMS frame (matches _gated_loudness_db)LEVEL_KNEE = 0.8 # soft limiter knee after gaindef _level_take(sample_rate: int, audio: np.ndarray) -> tuple[np.ndarray, dict]:"""Even out section-to-section loudness across a finished take.Returns int16 samples and a stats dict; the input is returned unchanged(as int16) when the take is too short to have more than one window."""x = np.asarray(audio)if x.ndim > 1:x = x[:, 0]if x.dtype.kind == "f":xf = np.clip(x, -1.0, 1.0).astype(np.float32)else:xf = (x.astype(np.float32) / 32768.0)n = len(xf)untouched = x if x.dtype == np.int16 else _to_int16(xf)frame = max(1, int(LEVEL_FRAME_S * sample_rate))n_frames = n // frameif n_frames < 2:return untouched, {"windows": 0}frame_rms = np.sqrt(np.mean(np.square(xf[: n_frames * frame].reshape(-1, frame)), axis=1))win_f = max(1, int(round(LEVEL_WINDOW_S / LEVEL_FRAME_S)))hop_f = max(1, int(round(LEVEL_HOP_S / LEVEL_FRAME_S)))starts = list(range(0, max(1, n_frames - win_f + 1), hop_f))if len(starts) < 2:return untouched, {"windows": len(starts)}# Gate against the whole take's loudest frames so a quiet window full of# pauses isn't measured (and boosted) as speech.thresh = max(float(frame_rms.max()) * 10 ** (NORM_GATE_DB / 20), 10 ** (NORM_GATE_FLOOR / 20))centers, louds = [], []for s0 in starts:seg = frame_rms[s0:s0 + win_f]active = seg[seg >= thresh]if active.size < max(2, win_f // 4): # mostly silence: no opinion herecontinuecenters.append((s0 + min(win_f, n_frames - s0) / 2.0) * frame)louds.append(20 * np.log10(max(float(np.sqrt(np.mean(np.square(active)))), 1e-9)))if len(louds) < 2:return untouched, {"windows": len(starts), "speech_windows": len(louds)}louds_arr = np.asarray(louds)target = float(np.median(louds_arr))gains_db = np.clip(target - louds_arr, -LEVEL_MAX_DB, LEVEL_MAX_DB)# Smooth across neighbours so one odd window can't swing the envelope.if len(gains_db) >= 3:gains_db = np.convolve(np.pad(gains_db, 1, mode="edge"), np.array([0.25, 0.5, 0.25]), mode="valid")gains = 10 ** (gains_db / 20)out = np.empty(n, dtype=np.int16)block = POLISH_BLOCK_SECONDS * sample_ratefor b in range(0, n, block):idx = np.arange(b, min(n, b + block), dtype=np.float64)y = xf[b:b + len(idx)] * np.interp(idx, centers, gains).astype(np.float32)mag = np.abs(y)over = mag > LEVEL_KNEEif over.any():excess = (mag[over] - LEVEL_KNEE) / (1.0 - LEVEL_KNEE)y[over] = np.sign(y[over]) * (LEVEL_KNEE + (1.0 - LEVEL_KNEE) * np.tanh(excess))out[b:b + len(idx)] = _to_int16(y)stats = {"windows": len(starts),"speech_windows": len(louds),"spread_before_db": round(float(louds_arr.max() - louds_arr.min()), 1),"gain_min_db": round(float(gains_db.min()), 1),"gain_max_db": round(float(gains_db.max()), 1),}return out, statsdef _to_int16(y: np.ndarray) -> np.ndarray:return (np.clip(y, -1.0, 1.0) * 32767.0).astype(np.int16)# Noise cleanup: STFT spectral gating. The noise print is the per-bin 10th# percentile of magnitude over time — a stationary-floor estimate that speech# (sparse in any one bin) barely lifts. Bins are attenuated at most down to
1637 unmodified lines16381639164016411642164316441637 unmodified linesoptional tone shelf. Applied live through a small Web Audio graph, anddownloads are rendered server-side with the same settings. */const SOUND_MODES = {original: { tone: "neutral", norm: false, hint: "The untouched take, exactly as generated" },studio: { tone: "neutral", norm: true, hint: "Loudness normalized to −16 LUFS podcast standard" },warm: { tone: "warm", norm: true, hint: "Normalized · warm tone — richer lows, softer highs" },bright: { tone: "bright", norm: true, hint: "Normalized · bright tone — crisper, more present" },1637 unmodified lines16381639164016411642164316441637 unmodified linesoptional tone shelf. Applied live through a small Web Audio graph, anddownloads are rendered server-side with the same settings. */const SOUND_MODES = {original: { tone: "neutral", norm: false, hint: "The take as generated — section levels evened, nothing else touched" },studio: { tone: "neutral", norm: true, hint: "Loudness normalized to −16 LUFS podcast standard" },warm: { tone: "warm", norm: true, hint: "Normalized · warm tone — richer lows, softer highs" },bright: { tone: "bright", norm: true, hint: "Normalized · bright tone — crisper, more present" },
README.md+1
app.py+94
static/app.js+1-1