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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import librosa
|
| 3 |
+
import numpy as np
|
| 4 |
+
from pydub import AudioSegment, effects
|
| 5 |
+
import uuid, os, json, time
|
| 6 |
+
|
| 7 |
+
UPLOAD_DIR = "uploads"
|
| 8 |
+
OUTPUT_DIR = "outputs"
|
| 9 |
+
DATA_DIR = "data"
|
| 10 |
+
|
| 11 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 12 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 13 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 14 |
+
|
| 15 |
+
MODEL_PATH = os.path.join(DATA_DIR, "weights.json")
|
| 16 |
+
|
| 17 |
+
# -----------------------------
|
| 18 |
+
# MODELO DE IA (PESOS APRENDIBLES)
|
| 19 |
+
# -----------------------------
|
| 20 |
+
|
| 21 |
+
DEFAULT_WEIGHTS = {
|
| 22 |
+
"bpm": 0.35,
|
| 23 |
+
"energy": 0.30,
|
| 24 |
+
"smooth": 0.20,
|
| 25 |
+
"drop_penalty": 0.15
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
def load_weights():
|
| 29 |
+
if os.path.exists(MODEL_PATH):
|
| 30 |
+
with open(MODEL_PATH, "r") as f:
|
| 31 |
+
return json.load(f)
|
| 32 |
+
return DEFAULT_WEIGHTS.copy()
|
| 33 |
+
|
| 34 |
+
def save_weights(w):
|
| 35 |
+
with open(MODEL_PATH, "w") as f:
|
| 36 |
+
json.dump(w, f)
|
| 37 |
+
|
| 38 |
+
weights = load_weights()
|
| 39 |
+
|
| 40 |
+
# -----------------------------
|
| 41 |
+
# FEATURE EXTRACTION
|
| 42 |
+
# -----------------------------
|
| 43 |
+
|
| 44 |
+
def analyze_audio(path):
|
| 45 |
+
y, sr = librosa.load(path, mono=True)
|
| 46 |
+
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
|
| 47 |
+
rms = librosa.feature.rms(y=y)[0]
|
| 48 |
+
rms = librosa.util.normalize(rms)
|
| 49 |
+
|
| 50 |
+
# detectar drops (picos bruscos)
|
| 51 |
+
drops = []
|
| 52 |
+
for i in range(10, len(rms)-10):
|
| 53 |
+
if rms[i] > np.mean(rms[i-10:i-1]) * 1.4:
|
| 54 |
+
drops.append(i)
|
| 55 |
+
|
| 56 |
+
return {
|
| 57 |
+
"tempo": float(tempo),
|
| 58 |
+
"energy": rms,
|
| 59 |
+
"drops": drops
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
# -----------------------------
|
| 63 |
+
# SCORING DE TRANSICIÓN (IA)
|
| 64 |
+
# -----------------------------
|
| 65 |
+
|
| 66 |
+
def score_transition(a, b, t):
|
| 67 |
+
bpm_sim = 1 - abs(a["tempo"] - b["tempo"]) / 20
|
| 68 |
+
bpm_sim = max(0, bpm_sim)
|
| 69 |
+
|
| 70 |
+
energy_sim = 1 - abs(a["energy"][t] - b["energy"][0])
|
| 71 |
+
energy_sim = max(0, energy_sim)
|
| 72 |
+
|
| 73 |
+
drop_penalty = 1 if t in a["drops"] else 0
|
| 74 |
+
|
| 75 |
+
score = (
|
| 76 |
+
weights["bpm"] * bpm_sim +
|
| 77 |
+
weights["energy"] * energy_sim +
|
| 78 |
+
weights["smooth"] * ((bpm_sim + energy_sim) / 2) -
|
| 79 |
+
weights["drop_penalty"] * drop_penalty
|
| 80 |
+
)
|
| 81 |
+
return score
|
| 82 |
+
|
| 83 |
+
# -----------------------------
|
| 84 |
+
# BEATMATCHING NO DESTRUCTIVO
|
| 85 |
+
# -----------------------------
|
| 86 |
+
|
| 87 |
+
def bpm_adjust(segment, from_bpm, to_bpm):
|
| 88 |
+
if from_bpm <= 0 or to_bpm <= 0:
|
| 89 |
+
return segment
|
| 90 |
+
|
| 91 |
+
ratio = from_bpm / to_bpm
|
| 92 |
+
if abs(1 - ratio) > 0.06:
|
| 93 |
+
return segment
|
| 94 |
+
|
| 95 |
+
new_rate = int(segment.frame_rate * ratio)
|
| 96 |
+
adjusted = segment._spawn(segment.raw_data, overrides={"frame_rate": new_rate})
|
| 97 |
+
return adjusted.set_frame_rate(segment.frame_rate)
|
| 98 |
+
|
| 99 |
+
# -----------------------------
|
| 100 |
+
# IA DJ MIXER PRINCIPAL
|
| 101 |
+
# -----------------------------
|
| 102 |
+
|
| 103 |
+
def auto_dj_mix(files, durations, base_crossfade, progress=gr.Progress()):
|
| 104 |
+
if not files or len(files) < 2:
|
| 105 |
+
raise gr.Error("Sube al menos 2 canciones")
|
| 106 |
+
|
| 107 |
+
durs = [float(x.strip()) for x in durations.split(",")]
|
| 108 |
+
if len(durs) != len(files):
|
| 109 |
+
raise gr.Error("Debes poner una duración por canción")
|
| 110 |
+
|
| 111 |
+
crossfade_ms = int(base_crossfade * 1000)
|
| 112 |
+
|
| 113 |
+
progress(0, desc="🎧 Cargando y analizando canciones…")
|
| 114 |
+
time.sleep(0.5)
|
| 115 |
+
|
| 116 |
+
tracks = []
|
| 117 |
+
analyses = []
|
| 118 |
+
|
| 119 |
+
for f in files:
|
| 120 |
+
ext = f.name.split(".")[-1].lower()
|
| 121 |
+
path = os.path.join(UPLOAD_DIR, f"{uuid.uuid4().hex}.{ext}")
|
| 122 |
+
|
| 123 |
+
with open(f.name, "rb") as src, open(path, "wb") as dst:
|
| 124 |
+
dst.write(src.read())
|
| 125 |
+
|
| 126 |
+
audio = AudioSegment.from_file(path)
|
| 127 |
+
audio = effects.normalize(audio)
|
| 128 |
+
|
| 129 |
+
tracks.append(audio)
|
| 130 |
+
analyses.append(analyze_audio(path))
|
| 131 |
+
|
| 132 |
+
mix = AudioSegment.silent(0)
|
| 133 |
+
|
| 134 |
+
for i, track in enumerate(tracks):
|
| 135 |
+
play_ms = min(int(durs[i] * 1000), len(track))
|
| 136 |
+
segment = track[:play_ms]
|
| 137 |
+
|
| 138 |
+
if i == 0:
|
| 139 |
+
mix = segment
|
| 140 |
+
continue
|
| 141 |
+
|
| 142 |
+
prev_analysis = analyses[i - 1]
|
| 143 |
+
curr_analysis = analyses[i]
|
| 144 |
+
|
| 145 |
+
progress(25 + i * 10, desc="🧠 Seleccionando punto óptimo…")
|
| 146 |
+
time.sleep(0.4)
|
| 147 |
+
|
| 148 |
+
# evaluar varios puntos candidatos
|
| 149 |
+
candidates = range(5, min(len(prev_analysis["energy"]) - 1, 50))
|
| 150 |
+
best_t = max(candidates, key=lambda t: score_transition(prev_analysis, curr_analysis, t))
|
| 151 |
+
|
| 152 |
+
# BPM objetivo SOLO durante transición
|
| 153 |
+
target_bpm = (prev_analysis["tempo"] + curr_analysis["tempo"]) / 2
|
| 154 |
+
|
| 155 |
+
progress(40 + i * 10, desc="🔄 Sincronizando BPM…")
|
| 156 |
+
time.sleep(0.4)
|
| 157 |
+
|
| 158 |
+
mix_adj = bpm_adjust(mix, prev_analysis["tempo"], target_bpm)
|
| 159 |
+
seg_adj = bpm_adjust(segment, curr_analysis["tempo"], target_bpm)
|
| 160 |
+
|
| 161 |
+
safe_cf = min(crossfade_ms, len(mix_adj), len(seg_adj))
|
| 162 |
+
|
| 163 |
+
progress(55 + i * 10, desc="🎚️ Mezclando…")
|
| 164 |
+
time.sleep(0.5)
|
| 165 |
+
|
| 166 |
+
mixed = mix_adj.append(seg_adj, crossfade=safe_cf)
|
| 167 |
+
|
| 168 |
+
# volver al BPM natural del track entrante
|
| 169 |
+
mix = bpm_adjust(mixed, target_bpm, curr_analysis["tempo"])
|
| 170 |
+
|
| 171 |
+
progress(85, desc="📦 Exportando mix…")
|
| 172 |
+
time.sleep(0.6)
|
| 173 |
+
|
| 174 |
+
out_name = f"monx_ai_dj_mix_{uuid.uuid4().hex}.m4a"
|
| 175 |
+
out_path = os.path.join(OUTPUT_DIR, out_name)
|
| 176 |
+
|
| 177 |
+
mix.export(out_path, format="ipod", codec="aac", bitrate="192k")
|
| 178 |
+
|
| 179 |
+
# -------- aprendizaje simple (feedback implícito positivo)
|
| 180 |
+
for k in weights:
|
| 181 |
+
weights[k] += 0.01 * weights[k]
|
| 182 |
+
save_weights(weights)
|
| 183 |
+
|
| 184 |
+
progress(100, desc="✅ Mix listo 🎧🔥")
|
| 185 |
+
|
| 186 |
+
return out_path
|
| 187 |
+
|
| 188 |
+
# -----------------------------
|
| 189 |
+
# UI GRADIO
|
| 190 |
+
# -----------------------------
|
| 191 |
+
|
| 192 |
+
with gr.Blocks(title="MONX AI DJ") as demo:
|
| 193 |
+
gr.Markdown(
|
| 194 |
+
"""
|
| 195 |
+
<div style="text-align:center">
|
| 196 |
+
<h1>🎚️ MONX AI DJ</h1>
|
| 197 |
+
<p>IA automática de mezcla con aprendizaje</p>
|
| 198 |
+
</div>
|
| 199 |
+
"""
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
files = gr.File(
|
| 203 |
+
label="Sube 2 a 4 canciones",
|
| 204 |
+
file_count="multiple",
|
| 205 |
+
file_types=[".mp3", ".wav", ".flac", ".m4a"]
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
durations = gr.Textbox(
|
| 209 |
+
label="Duración por canción (segundos)",
|
| 210 |
+
value="30,30"
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
crossfade = gr.Slider(
|
| 214 |
+
minimum=6,
|
| 215 |
+
maximum=20,
|
| 216 |
+
value=12,
|
| 217 |
+
step=1,
|
| 218 |
+
label="Crossfade base (segundos)"
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
btn = gr.Button("🔥 Auto DJ Mix", variant="primary")
|
| 222 |
+
|
| 223 |
+
output = gr.Audio(label="Resultado (M4A)", type="filepath")
|
| 224 |
+
|
| 225 |
+
btn.click(
|
| 226 |
+
fn=auto_dj_mix,
|
| 227 |
+
inputs=[files, durations, crossfade],
|
| 228 |
+
outputs=output
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
if __name__ == "__main__":
|
| 232 |
+
demo.launch()
|