teszenofficial commited on
Commit
43e7cf3
·
verified ·
1 Parent(s): 7f64c1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -92
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # =====================================================
2
- # Segue AI — HF Stable Edition
3
  # =====================================================
4
 
5
  import librosa
@@ -8,125 +8,92 @@ import soundfile as sf
8
  import gradio as gr
9
  import time
10
 
11
- # =====================================================
12
- # AUDIO UTILS
13
- # =====================================================
14
- def load_audio(path, sr=22050):
15
- y, _ = librosa.load(path, sr=sr, mono=True)
16
- return y, sr
 
17
 
18
- def safe_bpm(y, sr):
19
  try:
20
- tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
21
- if tempo <= 0 or np.isnan(tempo):
22
- return 120.0
23
- return float(tempo)
24
  except:
25
  return 120.0
26
 
27
- def mel_embedding(y, sr):
28
- mel = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=64)
29
- mel = librosa.power_to_db(mel)
30
- return np.mean(mel, axis=1)
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
- if len(y1) < win or len(y2) < win:
40
- return int(len(y1) * 0.7), int(len(y2) * 0.05)
 
 
41
 
42
- emb2 = mel_embedding(y2[:win], sr)
43
- best_score = -1e9
44
- best_a = int(len(y1) * 0.6)
45
 
46
- for i in range(0, len(y1) - win, hop):
47
- seg = y1[i:i+win]
48
- emb1 = mel_embedding(seg, sr)
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
- return best_a, int(len(y2) * 0.05)
 
 
57
 
58
- # =====================================================
59
- # SAFE MIX
60
- # =====================================================
61
- def seamless_mix(a, b, sr, sa, sb, fade_sec):
62
- fade_len = int(sr * fade_sec)
63
 
64
- sa = min(sa, len(a) - fade_len - 1)
65
- sb = min(sb, len(b) - fade_len - 1)
66
 
67
- fade_len = max(1024, min(fade_len, len(a)-sa, len(b)-sb))
 
68
 
69
- t = np.linspace(0, 1, fade_len)
 
70
  vol_a = np.cos(t * np.pi / 2)
71
  vol_b = np.sin(t * np.pi / 2)
72
 
73
- mix = a[sa:sa+fade_len] * vol_a + b[sb:sb+fade_len] * vol_b
74
 
75
- return np.concatenate([
 
76
  a[:sa],
77
  mix,
78
- b[sb+fade_len:]
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
- y2_sync = librosa.effects.time_stretch(y2, rate=ratio)
102
-
103
- progress(0.85, desc="Blending seamlessly…")
104
- mix = seamless_mix(
105
- y1, y2_sync, sr,
106
- sa, sb,
107
- fade_sec=14
108
- )
109
 
110
- out = "segue_ai_mix.wav"
111
- sf.write(out, mix, sr)
112
- time.sleep(0.4)
113
 
114
- return out
 
 
 
115
 
116
- except Exception as e:
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
- **Seamless Audio Intelligence**
 
 
 
 
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, inputs=[song1, song2], outputs=output)
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()