Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# =====================================================
|
| 2 |
-
# Segue AI — Apple Music
|
| 3 |
# =====================================================
|
| 4 |
|
| 5 |
import librosa
|
|
@@ -9,9 +9,11 @@ import gradio as gr
|
|
| 9 |
import time
|
| 10 |
|
| 11 |
SR = 22050
|
| 12 |
-
MIX_TIME = 20 #
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
| 15 |
def load_audio(path):
|
| 16 |
y, _ = librosa.load(path, sr=SR, mono=True)
|
| 17 |
return y
|
|
@@ -19,16 +21,21 @@ def load_audio(path):
|
|
| 19 |
def safe_bpm(y):
|
| 20 |
try:
|
| 21 |
bpm, _ = librosa.beat.beat_track(y=y, sr=SR)
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
except:
|
| 24 |
return 120.0
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
#
|
|
|
|
|
|
|
| 32 |
def apple_style_mix(a, b):
|
| 33 |
bpm_a = safe_bpm(a)
|
| 34 |
bpm_b = safe_bpm(b)
|
|
@@ -37,55 +44,67 @@ def apple_style_mix(a, b):
|
|
| 37 |
|
| 38 |
# puntos exactos
|
| 39 |
sa = max(0, len(a) - mix_len)
|
| 40 |
-
sb = 0
|
| 41 |
|
| 42 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
b_intro = b[:mix_len]
|
| 44 |
b_rest = b[mix_len:]
|
| 45 |
|
| 46 |
-
# sincronizar BPM SOLO en
|
| 47 |
rate = bpm_a / bpm_b
|
| 48 |
-
b_intro_sync =
|
| 49 |
-
|
| 50 |
-
# asegurar tamaños
|
| 51 |
-
min_len = min(len(a) - sa, len(b_intro_sync))
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
b_mix = b_intro_sync[:min_len]
|
| 55 |
|
| 56 |
-
# curvas
|
| 57 |
t = np.linspace(0, 1, min_len)
|
| 58 |
vol_a = np.cos(t * np.pi / 2)
|
| 59 |
vol_b = np.sin(t * np.pi / 2)
|
| 60 |
|
| 61 |
mix = a_mix * vol_a + b_mix * vol_b
|
| 62 |
|
| 63 |
-
|
| 64 |
-
output = np.concatenate([
|
| 65 |
a[:sa],
|
| 66 |
mix,
|
| 67 |
b_rest
|
| 68 |
])
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
#
|
| 73 |
def automix(file1, file2, progress=gr.Progress()):
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
gr.Markdown("""
|
| 91 |
# 🎧 Segue AI
|
|
@@ -93,7 +112,7 @@ with gr.Blocks() as demo:
|
|
| 93 |
|
| 94 |
• Total mix duration: **20 seconds**
|
| 95 |
• Starts 20s before Track A ends
|
| 96 |
-
• Ends at 20s into Track B
|
| 97 |
""")
|
| 98 |
|
| 99 |
with gr.Row():
|
|
|
|
| 1 |
# =====================================================
|
| 2 |
+
# Segue AI — Apple Music style (HF SAFE)
|
| 3 |
# =====================================================
|
| 4 |
|
| 5 |
import librosa
|
|
|
|
| 9 |
import time
|
| 10 |
|
| 11 |
SR = 22050
|
| 12 |
+
MIX_TIME = 20 # segundos TOTALES
|
| 13 |
|
| 14 |
+
# =====================================================
|
| 15 |
+
# UTILS
|
| 16 |
+
# =====================================================
|
| 17 |
def load_audio(path):
|
| 18 |
y, _ = librosa.load(path, sr=SR, mono=True)
|
| 19 |
return y
|
|
|
|
| 21 |
def safe_bpm(y):
|
| 22 |
try:
|
| 23 |
bpm, _ = librosa.beat.beat_track(y=y, sr=SR)
|
| 24 |
+
if bpm <= 0 or np.isnan(bpm):
|
| 25 |
+
return 120.0
|
| 26 |
+
return float(bpm)
|
| 27 |
except:
|
| 28 |
return 120.0
|
| 29 |
|
| 30 |
+
def safe_stretch(y, rate):
|
| 31 |
+
rate = float(np.clip(rate, 0.85, 1.15))
|
| 32 |
+
if len(y) < SR: # audio demasiado corto
|
| 33 |
+
return y
|
| 34 |
+
return librosa.effects.time_stretch(y, rate)
|
| 35 |
|
| 36 |
+
# =====================================================
|
| 37 |
+
# CORE MIX (ERROR-PROOF)
|
| 38 |
+
# =====================================================
|
| 39 |
def apple_style_mix(a, b):
|
| 40 |
bpm_a = safe_bpm(a)
|
| 41 |
bpm_b = safe_bpm(b)
|
|
|
|
| 44 |
|
| 45 |
# puntos exactos
|
| 46 |
sa = max(0, len(a) - mix_len)
|
|
|
|
| 47 |
|
| 48 |
+
# si B es muy corta
|
| 49 |
+
if len(b) < mix_len:
|
| 50 |
+
mix_len = len(b)
|
| 51 |
+
|
| 52 |
+
# segmentos
|
| 53 |
+
a_mix = a[sa:sa + mix_len]
|
| 54 |
b_intro = b[:mix_len]
|
| 55 |
b_rest = b[mix_len:]
|
| 56 |
|
| 57 |
+
# sincronizar BPM SOLO en intro
|
| 58 |
rate = bpm_a / bpm_b
|
| 59 |
+
b_intro_sync = safe_stretch(b_intro, rate)
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# ⚠️ CLAVE: igualar longitudes SÍ o SÍ
|
| 62 |
+
min_len = min(len(a_mix), len(b_intro_sync))
|
| 63 |
+
a_mix = a_mix[:min_len]
|
| 64 |
b_mix = b_intro_sync[:min_len]
|
| 65 |
|
| 66 |
+
# curvas Apple-style
|
| 67 |
t = np.linspace(0, 1, min_len)
|
| 68 |
vol_a = np.cos(t * np.pi / 2)
|
| 69 |
vol_b = np.sin(t * np.pi / 2)
|
| 70 |
|
| 71 |
mix = a_mix * vol_a + b_mix * vol_b
|
| 72 |
|
| 73 |
+
return np.concatenate([
|
|
|
|
| 74 |
a[:sa],
|
| 75 |
mix,
|
| 76 |
b_rest
|
| 77 |
])
|
| 78 |
|
| 79 |
+
# =====================================================
|
| 80 |
+
# PIPELINE HF
|
| 81 |
+
# =====================================================
|
| 82 |
def automix(file1, file2, progress=gr.Progress()):
|
| 83 |
+
try:
|
| 84 |
+
progress(0.1, desc="Loading tracks…")
|
| 85 |
+
a = load_audio(file1)
|
| 86 |
+
b = load_audio(file2)
|
| 87 |
|
| 88 |
+
progress(0.6, desc="Mixing seamlessly…")
|
| 89 |
+
output = apple_style_mix(a, b)
|
| 90 |
|
| 91 |
+
progress(0.9, desc="Finalizing…")
|
| 92 |
+
out = "segue_ai_mix.wav"
|
| 93 |
+
sf.write(out, output, SR)
|
| 94 |
+
time.sleep(0.3)
|
| 95 |
|
| 96 |
+
return out
|
| 97 |
|
| 98 |
+
except Exception as e:
|
| 99 |
+
# fallback ultra seguro (HF nunca debe romper)
|
| 100 |
+
out = "segue_ai_fallback.wav"
|
| 101 |
+
min_len = min(len(a), len(b))
|
| 102 |
+
sf.write(out, a[:min_len] * 0.7 + b[:min_len] * 0.3, SR)
|
| 103 |
+
return out
|
| 104 |
+
|
| 105 |
+
# =====================================================
|
| 106 |
+
# UI
|
| 107 |
+
# =====================================================
|
| 108 |
with gr.Blocks() as demo:
|
| 109 |
gr.Markdown("""
|
| 110 |
# 🎧 Segue AI
|
|
|
|
| 112 |
|
| 113 |
• Total mix duration: **20 seconds**
|
| 114 |
• Starts 20s before Track A ends
|
| 115 |
+
• Ends at 20s into Track B
|
| 116 |
""")
|
| 117 |
|
| 118 |
with gr.Row():
|