Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# =====================================================
|
| 2 |
-
# Segue AI —
|
| 3 |
# =====================================================
|
| 4 |
|
| 5 |
import librosa
|
|
@@ -8,125 +8,92 @@ import soundfile as sf
|
|
| 8 |
import gradio as gr
|
| 9 |
import time
|
| 10 |
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
def safe_bpm(y
|
| 19 |
try:
|
| 20 |
-
|
| 21 |
-
if
|
| 22 |
-
return 120.0
|
| 23 |
-
return float(tempo)
|
| 24 |
except:
|
| 25 |
return 120.0
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# =====================================================
|
| 33 |
-
# FIND TRANSITION (SAFE)
|
| 34 |
-
# =====================================================
|
| 35 |
-
def find_transition(y1, y2, sr):
|
| 36 |
-
win = int(sr * 12)
|
| 37 |
-
hop = int(sr * 2)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
best_score = -1e9
|
| 44 |
-
best_a = int(len(y1) * 0.6)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
sim = np.dot(emb1, emb2) / (
|
| 50 |
-
np.linalg.norm(emb1) * np.linalg.norm(emb2) + 1e-6
|
| 51 |
-
)
|
| 52 |
-
if sim > best_score:
|
| 53 |
-
best_score = sim
|
| 54 |
-
best_a = i
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def seamless_mix(a, b, sr, sa, sb, fade_sec):
|
| 62 |
-
fade_len = int(sr * fade_sec)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
|
|
|
|
| 70 |
vol_a = np.cos(t * np.pi / 2)
|
| 71 |
vol_b = np.sin(t * np.pi / 2)
|
| 72 |
|
| 73 |
-
mix =
|
| 74 |
|
| 75 |
-
|
|
|
|
| 76 |
a[:sa],
|
| 77 |
mix,
|
| 78 |
-
|
| 79 |
])
|
| 80 |
|
| 81 |
-
|
| 82 |
-
# MAIN PIPELINE (CRASH-PROOF)
|
| 83 |
-
# =====================================================
|
| 84 |
-
def automix(file1, file2, progress=gr.Progress()):
|
| 85 |
-
try:
|
| 86 |
-
progress(0.1, desc="Loading audio…")
|
| 87 |
-
y1, sr = load_audio(file1)
|
| 88 |
-
y2, _ = load_audio(file2)
|
| 89 |
-
|
| 90 |
-
progress(0.3, desc="Analyzing rhythm…")
|
| 91 |
-
bpm1 = safe_bpm(y1, sr)
|
| 92 |
-
bpm2 = safe_bpm(y2, sr)
|
| 93 |
-
|
| 94 |
-
progress(0.5, desc="Finding best transition…")
|
| 95 |
-
sa, sb = find_transition(y1, y2, sr)
|
| 96 |
-
|
| 97 |
-
progress(0.7, desc="Synchronizing BPM…")
|
| 98 |
-
ratio = bpm1 / bpm2 if bpm2 > 0 else 1.0
|
| 99 |
-
ratio = np.clip(ratio, 0.85, 1.15)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
sa, sb,
|
| 107 |
-
fade_sec=14
|
| 108 |
-
)
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
time.sleep(0.4)
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
# fallback ultra seguro
|
| 118 |
-
out = "segue_ai_fallback.wav"
|
| 119 |
-
min_len = min(len(y1), len(y2))
|
| 120 |
-
sf.write(out, y1[:min_len] * 0.7 + y2[:min_len] * 0.3, sr)
|
| 121 |
-
return out
|
| 122 |
|
| 123 |
-
#
|
| 124 |
-
# UI
|
| 125 |
-
# =====================================================
|
| 126 |
with gr.Blocks() as demo:
|
| 127 |
gr.Markdown("""
|
| 128 |
# 🎧 Segue AI
|
| 129 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
""")
|
| 131 |
|
| 132 |
with gr.Row():
|
|
@@ -136,6 +103,6 @@ with gr.Blocks() as demo:
|
|
| 136 |
output = gr.Audio(label="Generated Mix")
|
| 137 |
|
| 138 |
btn = gr.Button("Generate Segue")
|
| 139 |
-
btn.click(automix,
|
| 140 |
|
| 141 |
demo.launch()
|
|
|
|
| 1 |
# =====================================================
|
| 2 |
+
# Segue AI — Apple Music Correct Timing (20s total)
|
| 3 |
# =====================================================
|
| 4 |
|
| 5 |
import librosa
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
import time
|
| 10 |
|
| 11 |
+
SR = 22050
|
| 12 |
+
MIX_TIME = 20 # TOTAL, no 40
|
| 13 |
+
|
| 14 |
+
# -----------------------------------------------------
|
| 15 |
+
def load_audio(path):
|
| 16 |
+
y, _ = librosa.load(path, sr=SR, mono=True)
|
| 17 |
+
return y
|
| 18 |
|
| 19 |
+
def safe_bpm(y):
|
| 20 |
try:
|
| 21 |
+
bpm, _ = librosa.beat.beat_track(y=y, sr=SR)
|
| 22 |
+
return bpm if bpm > 0 else 120.0
|
|
|
|
|
|
|
| 23 |
except:
|
| 24 |
return 120.0
|
| 25 |
|
| 26 |
+
def stretch(y, rate):
|
| 27 |
+
return librosa.effects.time_stretch(
|
| 28 |
+
y, np.clip(rate, 0.85, 1.15)
|
| 29 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# -----------------------------------------------------
|
| 32 |
+
def apple_style_mix(a, b):
|
| 33 |
+
bpm_a = safe_bpm(a)
|
| 34 |
+
bpm_b = safe_bpm(b)
|
| 35 |
|
| 36 |
+
mix_len = MIX_TIME * SR
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# puntos exactos
|
| 39 |
+
sa = max(0, len(a) - mix_len)
|
| 40 |
+
sb = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# intro de B (solo 20 s)
|
| 43 |
+
b_intro = b[:mix_len]
|
| 44 |
+
b_rest = b[mix_len:]
|
| 45 |
|
| 46 |
+
# sincronizar BPM SOLO en la mezcla
|
| 47 |
+
rate = bpm_a / bpm_b
|
| 48 |
+
b_intro_sync = stretch(b_intro, rate)
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# asegurar tamaños
|
| 51 |
+
min_len = min(len(a) - sa, len(b_intro_sync))
|
| 52 |
|
| 53 |
+
a_mix = a[sa:sa+min_len]
|
| 54 |
+
b_mix = b_intro_sync[:min_len]
|
| 55 |
|
| 56 |
+
# curvas de volumen tipo Apple
|
| 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 |
+
# salida final (larga)
|
| 64 |
+
output = np.concatenate([
|
| 65 |
a[:sa],
|
| 66 |
mix,
|
| 67 |
+
b_rest
|
| 68 |
])
|
| 69 |
|
| 70 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# -----------------------------------------------------
|
| 73 |
+
def automix(file1, file2, progress=gr.Progress()):
|
| 74 |
+
progress(0.1, desc="Loading tracks…")
|
| 75 |
+
a = load_audio(file1)
|
| 76 |
+
b = load_audio(file2)
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
progress(0.6, desc="Synchronizing & mixing…")
|
| 79 |
+
mix = apple_style_mix(a, b)
|
|
|
|
| 80 |
|
| 81 |
+
progress(0.9, desc="Finalizing…")
|
| 82 |
+
out = "segue_ai_mix.wav"
|
| 83 |
+
sf.write(out, mix, SR)
|
| 84 |
+
time.sleep(0.3)
|
| 85 |
|
| 86 |
+
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# -----------------------------------------------------
|
|
|
|
|
|
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
gr.Markdown("""
|
| 91 |
# 🎧 Segue AI
|
| 92 |
+
**Apple Music–style Automix**
|
| 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():
|
|
|
|
| 103 |
output = gr.Audio(label="Generated Mix")
|
| 104 |
|
| 105 |
btn = gr.Button("Generate Segue")
|
| 106 |
+
btn.click(automix, [song1, song2], output)
|
| 107 |
|
| 108 |
demo.launch()
|