Spaces:
Sleeping
Sleeping
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206import gradio as grimport soundfile as sfimport numpy as npimport osfrom scipy.signal import lfilterfrom pedalboard import ( Pedalboard, Compressor, Limiter, Gain, HighShelfFilter, LowShelfFilter, PeakFilter, Reverb)
# ===================================================# 6HD SPATIAL & ENTROPY MODULES# ===================================================
def spatial_6hd(audio): mid = (audio[:,0] + audio[:,1]) * 0.5 side = (audio[:,0] - audio[:,1]) * 0.5 width_motion = np.random.normal(1.15, 0.01, len(side)) width_motion = np.convolve(width_motion, np.ones(8000)/8000, mode='same') side *= width_motion left = mid + side right = mid - side return np.clip(np.column_stack((left, right)), -1.0, 1.0)
def inject_6hd_entropy(audio, sr): n_samples = len(audio)
t_jitter = np.cumsum(np.random.normal(0, 0.0000008, n_samples)) indices = np.clip(np.arange(n_samples) + t_jitter * sr, 0, n_samples - 1).astype(np.intp) audio = audio[indices]
s_drift = np.random.normal(1.0, 0.0004, len(audio)) b, a = [1.0], [1.0, -0.9998] s_drift = lfilter(b, a, s_drift)
audio[:, 0] *= (1.0 + 0.0004 * s_drift) audio[:, 1] *= (1.0 - 0.0004 * s_drift) return np.clip(audio, -1.0, 1.0)
# ===================================================# 6HD CORE COMPONENTS# ===================================================
def normalize(audio): peak = np.max(np.abs(audio)) return audio / peak if peak > 0 else audio
def analog_6hd(audio): return np.tanh(audio * 1.012)
def transient_evolution(audio): transient = np.diff(audio, axis=0, prepend=audio[:1]) audio += transient * 0.025 return np.clip(audio, -1.0, 1.0)
# ===================================================# ✨ NEW: COMMERCIAL CLARITY ENHANCEMENT LAYER# ===================================================
def commercial_clarity_layer(audio): """Adds streaming-grade polish (Spotify / Apple Music style mastering behavior)."""
# Air + presence lift (soft brightness without harsh digital edges) air = np.mean(audio, axis=1, keepdims=True) * 0.012 audio[:, 0] += air[:, 0] audio[:, 1] += air[:, 0]
# Ultra subtle stereo micro-enhancement (safe widening) stereo_phase = np.sin(np.linspace(0, np.pi * 2, len(audio))) * 0.002 audio[:, 0] *= (1.0 + stereo_phase) audio[:, 1] *= (1.0 - stereo_phase)
return np.clip(audio, -1.0, 1.0)
# ===================================================# ✨ NEW: STREAMING SAFE FINALIZATION (TRUE PEAK + DITHER)# ===================================================
def streaming_safe_finalize(audio): """Prevents distortion on Apple Music / Spotify transcoding."""
# Soft true-peak safety audio = np.tanh(audio * 0.995)
# 24-bit style dithering (very light noise floor realism) dither = np.random.normal(0, 1e-5, audio.shape) audio += dither
return np.clip(audio, -1.0, 1.0)
# ===================================================# MASTER 6HD ENGINE# ===================================================
def adaptive_6hd_master(audio_path, style, lufs_target, auto_glass): if not audio_path: return None audio, sr = sf.read(audio_path)
if len(audio.shape) == 1: audio = np.column_stack([audio, audio]) elif audio.shape[1] == 2: if np.max(np.abs(audio[:, 1])) == 0: audio[:, 1] = audio[:, 0]
audio = audio.astype(np.float32)
audio = normalize(audio) audio = spatial_6hd(audio) audio = transient_evolution(audio) audio = inject_6hd_entropy(audio, sr) audio = analog_6hd(audio)
# ✨ NEW COMMERCIAL LAYER (ADDED, DOES NOT BREAK CORE) audio = commercial_clarity_layer(audio)
eq_gains = { "Universal": {"low": 1.2, "mid": 0.5, "high": 2.0, "gain": 4.0}, "Cinematic": {"low": 1.8, "mid": 0.2, "high": 1.5, "gain": 3.5}, "Spatial": {"low": 1.0, "mid": 0.8, "high": 2.5, "gain": 4.2}, "Slam": {"low": 2.2, "mid": 0.4, "high": 1.0, "gain": 4.8}, "Stumper": {"low": 1.5, "mid": 0.6, "high": 1.8, "gain": 4.0}, "Clarity HD": {"low": 0.8, "mid": 0.3, "high": 3.0, "gain": 3.8} }
cfg = eq_gains.get(style, eq_gains["Universal"])
lufs_shift = float(-14.0 - lufs_target) final_gain = cfg["gain"] + lufs_shift
limiter_ceiling = -1.0 if auto_glass else -0.2
board = Pedalboard([ LowShelfFilter(cutoff_frequency_hz=90, gain_db=cfg["low"]), PeakFilter(cutoff_frequency_hz=2500, gain_db=cfg["mid"], q=0.7), HighShelfFilter(cutoff_frequency_hz=14000, gain_db=cfg["high"]),
Compressor(threshold_db=-16, ratio=1.5, attack_ms=50, release_ms=250),
# ✨ extra polish inside mastering chain Reverb(room_size=0.08, damping=0.9, wet_level=0.015, width=1.0),
Gain(gain_db=final_gain), Limiter(threshold_db=limiter_ceiling), ])
processed = board(audio, sr)
# ✨ FINAL STREAMING SAFE STAGE processed = streaming_safe_finalize(processed)
processed = normalize(processed)
base_name = "".join([ c for c in os.path.splitext(os.path.basename(audio_path))[0] if c.isalnum() or c in ('_', '-') ])
if not base_name: base_name = "UCDM_Track"
output_path = f"{base_name}_Master6HD.wav"
sf.write(output_path, processed, sr, subtype='PCM_24') return output_path
# ===================================================# UI (GRADIO)# ===================================================
with gr.Blocks(theme=gr.themes.Soft()) as app: gr.Markdown("# HumanNaturalMaster: Ultra-6HD Engine")
with gr.Row(): input_audio = gr.Audio(label="Upload UCDM Track", type="filepath")
with gr.Row(): style = gr.Radio( ["Universal", "Cinematic", "Spatial", "Slam", "Stumper", "Clarity HD"], value="Universal", label="Master Style Profile" )
with gr.Row(): lufs_target = gr.Slider( minimum=-20, maximum=-6, value=-14, step=1, label="Loudness Target (LUFS)" )
auto_glass = gr.Checkbox( value=True, label="Auto-Glass (Streaming Optimization)" )
process_btn = gr.Button("GENERATE 6HD HUMAN MASTER", variant="primary") output_audio = gr.Audio(label="6HD Spatial Output", type="filepath")
process_btn.click( fn=adaptive_6hd_master, inputs=[input_audio, style, lufs_target, auto_glass], outputs=output_audio )
if __name__ == "__main__": app.launch()app.pyAdded+206