Delete app.py
Browse files
app.py
DELETED
|
@@ -1,151 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import librosa
|
| 3 |
-
import numpy as np
|
| 4 |
-
from pydub import AudioSegment
|
| 5 |
-
from scipy.signal import resample
|
| 6 |
-
import uuid, os
|
| 7 |
-
|
| 8 |
-
UPLOAD_DIR = "uploads"
|
| 9 |
-
OUTPUT_DIR = "outputs"
|
| 10 |
-
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 11 |
-
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 12 |
-
|
| 13 |
-
# -----------------------------
|
| 14 |
-
# UTILIDADES MUSICALES
|
| 15 |
-
# -----------------------------
|
| 16 |
-
|
| 17 |
-
def analyze_audio(path):
|
| 18 |
-
y, sr = librosa.load(path, mono=True)
|
| 19 |
-
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
|
| 20 |
-
beat_times = librosa.frames_to_time(beats, sr=sr)
|
| 21 |
-
|
| 22 |
-
# Detectar frases (cada 8 beats)
|
| 23 |
-
phrases = beat_times[::8] if len(beat_times) >= 8 else beat_times
|
| 24 |
-
|
| 25 |
-
return {
|
| 26 |
-
"tempo": tempo,
|
| 27 |
-
"beats": beat_times,
|
| 28 |
-
"phrases": phrases,
|
| 29 |
-
"sr": sr,
|
| 30 |
-
"y": y
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
def time_stretch_segment(audio, rate):
|
| 34 |
-
samples = np.array(audio.get_array_of_samples())
|
| 35 |
-
stretched = resample(samples, int(len(samples) / rate))
|
| 36 |
-
new_audio = audio._spawn(stretched.astype(np.int16).tobytes())
|
| 37 |
-
return new_audio.set_frame_rate(audio.frame_rate)
|
| 38 |
-
|
| 39 |
-
def dj_crossfade(a, b, crossfade_ms):
|
| 40 |
-
a = a.fade_out(crossfade_ms)
|
| 41 |
-
b = b.fade_in(crossfade_ms)
|
| 42 |
-
return a.append(b, crossfade=crossfade_ms)
|
| 43 |
-
|
| 44 |
-
# -----------------------------
|
| 45 |
-
# MEZCLADOR DJ REAL
|
| 46 |
-
# -----------------------------
|
| 47 |
-
|
| 48 |
-
def auto_dj_mix(files, progress=gr.Progress()):
|
| 49 |
-
if not files or len(files) < 2 or len(files) > 4:
|
| 50 |
-
raise gr.Error("Sube entre 2 y 4 canciones")
|
| 51 |
-
|
| 52 |
-
progress(0, desc="Analizando canciones…")
|
| 53 |
-
|
| 54 |
-
tracks = []
|
| 55 |
-
analyses = []
|
| 56 |
-
|
| 57 |
-
# Guardar y analizar
|
| 58 |
-
for f in files:
|
| 59 |
-
ext = f.name.split(".")[-1].lower()
|
| 60 |
-
path = os.path.join(UPLOAD_DIR, f"{uuid.uuid4().hex}.{ext}")
|
| 61 |
-
|
| 62 |
-
with open(f.name, "rb") as src, open(path, "wb") as dst:
|
| 63 |
-
dst.write(src.read())
|
| 64 |
-
|
| 65 |
-
tracks.append(AudioSegment.from_file(path))
|
| 66 |
-
analyses.append(analyze_audio(path))
|
| 67 |
-
|
| 68 |
-
progress(20, desc="Calculando estrategia de mezcla…")
|
| 69 |
-
|
| 70 |
-
mix = tracks[0]
|
| 71 |
-
|
| 72 |
-
for i in range(1, len(tracks)):
|
| 73 |
-
a = tracks[i - 1]
|
| 74 |
-
b = tracks[i]
|
| 75 |
-
|
| 76 |
-
info_a = analyses[i - 1]
|
| 77 |
-
info_b = analyses[i]
|
| 78 |
-
|
| 79 |
-
# BPM target (promedio)
|
| 80 |
-
target_bpm = (info_a["tempo"] + info_b["tempo"]) / 2
|
| 81 |
-
|
| 82 |
-
# Beatmatch SOLO B
|
| 83 |
-
rate = info_b["tempo"] / target_bpm
|
| 84 |
-
if abs(rate - 1) < 0.06:
|
| 85 |
-
b = time_stretch_segment(b, rate)
|
| 86 |
-
|
| 87 |
-
# Elegir puntos musicales
|
| 88 |
-
exit_point = int(info_a["phrases"][-1] * 1000)
|
| 89 |
-
entry_point = int(info_b["phrases"][0] * 1000)
|
| 90 |
-
|
| 91 |
-
a_seg = a[:exit_point]
|
| 92 |
-
b_seg = b[entry_point:]
|
| 93 |
-
|
| 94 |
-
crossfade_ms = 16_000 # 16 beats aprox
|
| 95 |
-
|
| 96 |
-
progress(20 + int((i / len(tracks)) * 60),
|
| 97 |
-
desc=f"Mezclando canción {i+1}")
|
| 98 |
-
|
| 99 |
-
mix = dj_crossfade(a_seg, b_seg, crossfade_ms)
|
| 100 |
-
|
| 101 |
-
progress(90, desc="Exportando mix DJ…")
|
| 102 |
-
|
| 103 |
-
out_name = f"monx_ai_dj_mix_{uuid.uuid4().hex}.m4a"
|
| 104 |
-
out_path = os.path.join(OUTPUT_DIR, out_name)
|
| 105 |
-
|
| 106 |
-
mix.export(
|
| 107 |
-
out_path,
|
| 108 |
-
format="ipod",
|
| 109 |
-
codec="aac",
|
| 110 |
-
bitrate="192k"
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
progress(100, desc="Mix DJ listo 🎚️🔥")
|
| 114 |
-
|
| 115 |
-
return out_path
|
| 116 |
-
|
| 117 |
-
# -----------------------------
|
| 118 |
-
# INTERFAZ GRADIO
|
| 119 |
-
# -----------------------------
|
| 120 |
-
|
| 121 |
-
with gr.Blocks(title="MONX AI DJ") as demo:
|
| 122 |
-
gr.Markdown(
|
| 123 |
-
"""
|
| 124 |
-
<div style="text-align:center">
|
| 125 |
-
<h1>🎚️ MONX AI DJ</h1>
|
| 126 |
-
<p>Mezcladora inteligente con beatmatching real</p>
|
| 127 |
-
</div>
|
| 128 |
-
"""
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
files = gr.File(
|
| 132 |
-
label="Sube 2 a 4 canciones",
|
| 133 |
-
file_count="multiple",
|
| 134 |
-
file_types=[".mp3", ".wav", ".flac", ".m4a"]
|
| 135 |
-
)
|
| 136 |
-
|
| 137 |
-
btn = gr.Button("🔥 Auto DJ Mix", variant="primary")
|
| 138 |
-
|
| 139 |
-
output = gr.Audio(
|
| 140 |
-
label="Resultado (M4A)",
|
| 141 |
-
type="filepath"
|
|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