Spaces:
Sleeping

MasterHuman / app.py
irelandcuca's picture
Update app.py
b7ea1f1 verified
Raw History Blame Contribute Delete
5.94 kB
import gradio as gr
import soundfile as sf
import numpy as np
import os
import requests
from scipy.signal import lfilter, butter
from pedalboard import (
Pedalboard,
Compressor,
Gain,
HighShelfFilter,
LowShelfFilter,
PeakFilter,
Reverb,
Limiter
)
# ===================================================
# NORMALIZE
# ===================================================
def normalize(audio):
peak = np.max(np.abs(audio))
if peak > 0:
audio = audio / peak
return audio
# ===================================================
# 🧼 SUB-BASS CLEANUP (NEW CORRECTION)
# ===================================================
def highpass_filter(audio, sr, cutoff=30.0, order=4):
"""Removes mud below 30Hz to optimize system headroom."""
nyq = 0.5 * sr
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='high', analog=False)
# Process channels independently to preserve stereo integrity
output = np.zeros_like(audio)
for ch in range(audio.shape[1]):
output[:, ch] = lfilter(b, a, audio[:, ch])
return output
# ===================================================
# 🎧 ENTROPY ENGINE (PRESERVED)
# ===================================================
def inject_6hd_entropy(audio, sr):
n = len(audio)
jitter = np.cumsum(np.random.normal(0, 1e-7, n))
# Explicit shape handling ensuring matrix indexing is stable
idx = np.clip(np.arange(n) + jitter * sr, 0, n - 1).astype(np.intp)
audio = audio[idx, :]
drift = np.random.normal(1.0, 0.00006, n)
drift = lfilter([0.0003], [1, -0.9997], drift)
audio[:, 0] *= (1.0 + drift)
audio[:, 1] *= (1.0 - drift)
return audio
# ===================================================
# 🎧 STEREO STABILITY
# ===================================================
def stereo_balance_lock(audio):
mid = (audio[:, 0] + audio[:, 1]) * 0.5
side = (audio[:, 0] - audio[:, 1]) * 0.5
side *= 1.015
left = mid + side
right = mid - side
l_rms = np.sqrt(np.mean(left ** 2))
r_rms = np.sqrt(np.mean(right ** 2))
if l_rms > 0 and r_rms > 0:
target = (l_rms + r_rms) * 0.5
left *= target / l_rms
right *= target / r_rms
return np.column_stack((left, right))
# ===================================================
# 🌌 SPATIAL WIDTH
# ===================================================
def spatial_6hd(audio, width=1.01):
mid = (audio[:, 0] + audio[:, 1]) * 0.5
side = (audio[:, 0] - audio[:, 1]) * 0.5
side *= width
return np.column_stack((mid + side, mid - side))
# ===================================================
# 🌐 OPTIONAL API
# ===================================================
def analyze_song(file_path):
return {"status": "API placeholder - safe mode active"}
# ===================================================
# 🎚 MASTER ENGINE (CLEAN + STABLE)
# ===================================================
def process(audio_path, preset, run_api):
if not audio_path:
return None, {}
audio, sr = sf.read(audio_path)
if audio.ndim == 1:
audio = np.column_stack([audio, audio])
audio = audio.astype(np.float32)
presets = {
"Universal": {"low": 0.8, "mid": 0.3, "high": 1.8, "gain": 1.2, "width": 1.01},
"Cinematic": {"low": 1.2, "mid": 0.2, "high": 1.4, "gain": 1.0, "width": 1.02},
"Spatial": {"low": 0.6, "mid": 0.4, "high": 2.2, "gain": 1.4, "width": 1.04},
"Slam": {"low": 1.5, "mid": 0.1, "high": 1.0, "gain": 1.8, "width": 1.00},
"Clarity HD": {"low": 0.4, "mid": 0.2, "high": 3.0, "gain": 1.2, "width": 1.02}
}
cfg = presets.get(preset, presets["Universal"])
# ===================================================
# MASTER CHAIN (STREAM SAFE)
# ===================================================
audio = normalize(audio)
# Run newly added subsonic cleanup to fix potential digital bounce errors
audio = highpass_filter(audio, sr, cutoff=30.0)
board = Pedalboard([
LowShelfFilter(80, cfg["low"]),
PeakFilter(2800, cfg["mid"], q=0.6),
HighShelfFilter(13500, cfg["high"]),
Compressor(
threshold_db=-14,
ratio=1.12,
attack_ms=100,
release_ms=350
),
Reverb(room_size=0.015, damping=0.95, wet_level=0.0015),
Gain(cfg["gain"]),
Limiter(threshold_db=-1.0)
])
audio = board(audio, sr)
# ===================================================
# POST PROCESSING (SAFE ORDER)
# ===================================================
audio = spatial_6hd(audio, cfg["width"])
audio = inject_6hd_entropy(audio, sr)
audio = stereo_balance_lock(audio)
audio *= 0.99
# ===================================================
# EXPORT
# ===================================================
base = os.path.splitext(os.path.basename(audio_path))[0]
output_path = f"{base}_MASTER_6HD.wav"
sf.write(output_path, audio, sr, subtype="PCM_24")
api_result = analyze_song(output_path) if run_api else {}
return output_path, api_result
# ===================================================
# πŸŽ› UI
# ===================================================
with gr.Blocks() as app:
gr.Markdown("# 🎧 MasterHuman 6HD Engine")
audio_in = gr.Audio(label="Upload Track", type="filepath")
preset = gr.Radio(
["Universal", "Cinematic", "Spatial", "Slam", "Clarity HD"],
value="Universal"
)
run_api = gr.Checkbox(label="Run API")
btn = gr.Button("MASTER")
audio_out = gr.Audio(label="Output")
api_out = gr.JSON()
btn.click(
process,
inputs=[audio_in, preset, run_api],
outputs=[audio_out, api_out]
)
if __name__ == "__main__":
app.launch(server_name="0.0.0.0", server_port=7860)