teszenofficial commited on
Commit
33f2ac1
·
verified ·
1 Parent(s): 72a826e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -5,7 +5,7 @@ from pydub import AudioSegment, effects
5
  import uuid, os, json
6
 
7
  # =====================================================
8
- # ARCHIVO DE ESTADO (MISMA CARPETA)
9
  # =====================================================
10
 
11
  STATE_PATH = "monx_dj_state.json"
@@ -17,7 +17,7 @@ os.makedirs(UPLOAD_DIR, exist_ok=True)
17
  os.makedirs(OUTPUT_DIR, exist_ok=True)
18
 
19
  # =====================================================
20
- # UTILIDAD: CONVERTIR A TIPOS JSON
21
  # =====================================================
22
 
23
  def to_python(obj):
@@ -54,30 +54,37 @@ def load_state():
54
  return DEFAULT_STATE.copy()
55
 
56
  def save_state(state):
57
- clean_state = to_python(state)
58
  with open(STATE_PATH, "w") as f:
59
- json.dump(clean_state, f, indent=2)
60
 
61
  state = load_state()
62
  weights = state["weights"]
63
 
64
  # =====================================================
65
- # ANÁLISIS MUSICAL
66
  # =====================================================
67
 
68
  def analyze_audio(path):
69
  y, sr = librosa.load(path, mono=True)
70
 
 
71
  onset_env = librosa.onset.onset_strength(y=y, sr=sr)
72
- tempo = float(
73
- librosa.feature.rhythm.tempo(
 
74
  onset_envelope=onset_env, sr=sr
75
- )[0]
76
- )
 
 
 
 
77
 
 
78
  rms = librosa.feature.rms(y=y)[0]
79
  rms = librosa.util.normalize(rms)
80
 
 
81
  drops = [
82
  i for i in range(10, len(rms) - 10)
83
  if rms[i] > np.mean(rms[i-10:i-1]) * 1.4
@@ -162,9 +169,6 @@ def auto_dj_mix(files, durations, crossfade_sec):
162
 
163
  prev, curr = analyses[i - 1], analyses[i]
164
 
165
- log += f"\n🧠 Decidiendo transición {i} → {i+1}\n"
166
- yield log, None
167
-
168
  candidates = range(5, min(len(prev["energy"]) - 1, 50))
169
  best_t = max(candidates, key=lambda t: score_transition(prev, curr, t))
170
  scores.append(score_transition(prev, curr, best_t))
@@ -177,20 +181,15 @@ def auto_dj_mix(files, durations, crossfade_sec):
177
  safe_cf = min(crossfade_ms, len(mix_adj), len(seg_adj))
178
  mix = bpm_adjust(
179
  mix_adj.append(seg_adj, crossfade=safe_cf),
180
- target_bpm,
181
- curr["tempo"]
182
  )
183
 
184
  out_path = os.path.join(
185
- OUTPUT_DIR,
186
- f"monx_dj_mix_{uuid.uuid4().hex}.m4a"
187
  )
188
  mix.export(out_path, format="ipod", codec="aac", bitrate="192k")
189
 
190
- # =========================
191
- # APRENDIZAJE AUTOMÁTICO
192
- # =========================
193
-
194
  if scores:
195
  new_avg = sum(scores) / len(scores)
196
  reward = 1 if new_avg > state["avg_score"] else -1
 
5
  import uuid, os, json
6
 
7
  # =====================================================
8
+ # ESTADO DEL MODELO (MISMA CARPETA QUE app.py)
9
  # =====================================================
10
 
11
  STATE_PATH = "monx_dj_state.json"
 
17
  os.makedirs(OUTPUT_DIR, exist_ok=True)
18
 
19
  # =====================================================
20
+ # UTILIDAD: CONVERSIÓN SEGURA A JSON
21
  # =====================================================
22
 
23
  def to_python(obj):
 
54
  return DEFAULT_STATE.copy()
55
 
56
  def save_state(state):
 
57
  with open(STATE_PATH, "w") as f:
58
+ json.dump(to_python(state), f, indent=2)
59
 
60
  state = load_state()
61
  weights = state["weights"]
62
 
63
  # =====================================================
64
+ # ANÁLISIS MUSICAL (ROBUSTO, SIN DEPENDER DE VERSIONES)
65
  # =====================================================
66
 
67
  def analyze_audio(path):
68
  y, sr = librosa.load(path, mono=True)
69
 
70
+ # ---------- BPM SEGURO ----------
71
  onset_env = librosa.onset.onset_strength(y=y, sr=sr)
72
+
73
+ try:
74
+ tempo = float(librosa.beat.tempo(
75
  onset_envelope=onset_env, sr=sr
76
+ )[0])
77
+ except Exception:
78
+ tempo = float(np.mean(onset_env) * 60)
79
+
80
+ if tempo <= 0 or np.isnan(tempo):
81
+ tempo = 120.0 # BPM seguro por defecto
82
 
83
+ # ---------- ENERGÍA ----------
84
  rms = librosa.feature.rms(y=y)[0]
85
  rms = librosa.util.normalize(rms)
86
 
87
+ # ---------- DROPS ----------
88
  drops = [
89
  i for i in range(10, len(rms) - 10)
90
  if rms[i] > np.mean(rms[i-10:i-1]) * 1.4
 
169
 
170
  prev, curr = analyses[i - 1], analyses[i]
171
 
 
 
 
172
  candidates = range(5, min(len(prev["energy"]) - 1, 50))
173
  best_t = max(candidates, key=lambda t: score_transition(prev, curr, t))
174
  scores.append(score_transition(prev, curr, best_t))
 
181
  safe_cf = min(crossfade_ms, len(mix_adj), len(seg_adj))
182
  mix = bpm_adjust(
183
  mix_adj.append(seg_adj, crossfade=safe_cf),
184
+ target_bpm, curr["tempo"]
 
185
  )
186
 
187
  out_path = os.path.join(
188
+ OUTPUT_DIR, f"monx_dj_mix_{uuid.uuid4().hex}.m4a"
 
189
  )
190
  mix.export(out_path, format="ipod", codec="aac", bitrate="192k")
191
 
192
+ # ---------- APRENDIZAJE AUTOMÁTICO ----------
 
 
 
193
  if scores:
194
  new_avg = sum(scores) / len(scores)
195
  reward = 1 if new_avg > state["avg_score"] else -1