david174 commited on
Commit
579ca65
·
verified ·
1 Parent(s): da2b151

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -87
app.py CHANGED
@@ -3,137 +3,139 @@ import librosa
3
  import numpy as np
4
  from pydub import AudioSegment
5
 
6
- # Memoria Cuántica de Sesión
7
- ghost_singularity = {
8
- "key": "--", "bpm": 0, "crest": 0, "lufs_est": 0,
9
- "harmonics": [], "spectrum_map": {}, "active": False
10
  }
11
 
12
- def engine_v27_singularity(t, r, p, active_fix, active_master):
13
- global ghost_singularity
14
  if t is None:
15
- return None, "SYSTEM: OFFLINE", "NO SIGNAL", "--", "0", None
16
 
17
  try:
18
- # 1. ESCANEO DE ALTA RESOLUCIÓN (90 segundos)
19
- y, sr = librosa.load(t, duration=90)
20
 
21
- # BPM con algoritmos de autocorrelación
22
- tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
 
23
  bpm = int(np.atleast_1d(tempo)[0])
24
- if bpm < 120: bpm *= 2 # Optimizador para Drum & Bass / Hardcore
25
 
26
- # KEY & Mapa de Armónicos
27
- chroma = librosa.feature.chroma_cqt(y=y, sr=sr)
28
- nt = ["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"]
29
- k = nt[np.argmax(np.mean(chroma, axis=1))]
30
  f_fund = librosa.note_to_hz(k+'1')
31
- harmonics = [round(f_fund * i, 1) for i in range(1, 9)]
32
 
33
- # Análisis de Dinámica Avanzada
34
  rms = librosa.feature.rms(y=y)
35
  crest = 20 * np.log10(np.max(np.abs(y)) / (np.mean(rms) + 1e-6))
36
 
37
  S = np.abs(librosa.stft(y))
38
  f = librosa.fft_frequencies(sr=sr)
39
 
40
- ghost_singularity.update({
41
- "key": k, "bpm": bpm, "crest": crest, "harmonics": harmonics,
42
- "spectrum_map": {
43
- "infra": np.mean(S[f < 30]),
44
- "sub": np.mean(S[(f >= 30) & (f < 80)]),
45
- "punch": np.mean(S[(f >= 80) & (f < 200)]),
46
- "mud": np.mean(S[(f >= 200) & (f < 500)]),
47
- "harsh": np.mean(S[(f >= 2500) & (f < 5000)]),
48
  "air": np.mean(S[f > 12000])
49
  },
50
- "active": True
51
  })
52
 
53
  audio = AudioSegment.from_file(t)
54
- log = f"--- GHOST SINGULARITY V27: MISSION LOG ---\n\n"
55
 
56
  if active_fix:
57
- audio = audio.high_pass_filter(32) # Corte quirúrgico infrasónico
58
- if ghost_singularity["spectrum_map"]["mud"] > ghost_singularity["spectrum_map"]["punch"] * 1.2:
59
  audio = audio.low_pass_filter(4500)
60
- log += "[!] AUTO-FIX: Resonancia de 'barro' detectada. Filtro adaptativo aplicado.\n"
61
- log += "[+] MIX: Limpieza de fase en graves completada.\n"
62
-
63
  if active_master:
64
- # Cadena de Master: Compresión Serie-Paralelo + Maximización Soft-Clip
65
- audio = audio.compressor_dynamic_range(threshold=-14.0, ratio=4.0, attack=15.0, release=150.0)
66
- audio = audio.apply_gain((p / 10) - 2.5)
67
- log += "[+] MASTER: Maximización de energía RMS activa.\n"
68
 
69
- audio.export("ghost_singularity.wav", format="wav")
70
- log += f"\n▶ NOTA RAÍZ: {k} ({round(f_fund, 1)}Hz)\n▶ BPM: {bpm}\n▶ DINÁMICA: {round(crest, 1)}dB\n"
71
- log += f"▶ STATUS: " + ("NECESITA COMPRESIÓN" if crest > 14 else "DENSIDAD ÓPTIMA")
72
 
73
- return "ghost_singularity.wav", log, "Análisis Neuronal Completo.", k, str(bpm), "https://images.unsplash.com/photo-1516280440614-37939bbacd81?q=80&w=500"
74
 
75
  except Exception as e:
76
- return None, f"CRITICAL ERROR: {str(e)}", "ERROR", "!", "0", None
77
 
78
- def mentor_ai_v27(pregunta):
79
- if not ghost_singularity["active"]: return "Primero analiza tu pista, Ghost."
80
 
81
- q = pregunta.lower()
82
- k = ghost_singularity["key"]
83
- b = ghost_singularity["bpm"]
84
- h = ghost_singularity["harmonics"]
85
- sm = ghost_singularity["spectrum_map"]
86
 
87
- # RESPUESTAS CALCULADAS
88
- if "bajo" in q or "sub" in q:
89
- if sm["sub"] > sm["punch"]:
90
- return f"Tu sub ({h[0]}Hz) es masivo pero no tiene 'punch'. Recorta 2dB en {h[0]}Hz y sube +3dB en el segundo armónico ({h[1]}Hz). Esto hará que el bajo se oiga en cualquier altavoz sin saturar."
91
- return f"El bajo en {k} está bien posicionado. Para que sea 'Ghost Level', añade un saturador tipo Saturation Knob solo en la banda de {h[2]}Hz a {h[4]}Hz."
92
-
93
- if "pegada" in q or "kick" in msg or "snare" in q:
94
- rel = round(60000 / b / 4, 1) # Release calculado a 1/16 de nota
95
- return f"A {b} BPM, el transiente se pierde. Configura tu compresor: Attack 32ms, Release {rel}ms. Si usas sidechain, que el umbral baje rápido en {h[0]}Hz para no chocar con el sub."
96
-
97
- if "medios" in q or "voz" in q or "lead" in q:
98
- if sm["mud"] > 0.05:
99
- return f"He detectado lodo excesivo. Tienes un conflicto armónico en {h[3]}Hz ({k}). Haz un corte con Q estrecho ahí y verás como tu mezcla respira instantáneamente."
100
- return "Tus medios son quirúrgicos. Añade un poco de Reverb Stereo solo por encima de 600Hz para no ensuciar el mono."
101
-
102
- if "consejo" in q or "mas" in q:
103
- return f"Pro-Tip para {k}: Aplica un De-Esser no en la voz, sino en el bus de platos en {h[7]}Hz. Tu 'Crest Factor' es de {round(ghost_singularity['crest'], 1)}dB; tienes margen para subir +2dB de ganancia antes del limitador."
104
-
105
- return f"Analizando {k} a {b} BPM: Recomiendo revisar la correlación de fase. Si el medidor de fase se va a -1 en los bajos, perderás potencia en el club."
106
-
107
- # --- INTERFAZ "DARK STUDIO HUD" ---
108
- css = "body, .gradio-container {background: #050505 !important; color: #00ff44 !important; font-family: 'JetBrains Mono', monospace;} .gr-box {border: 2px solid #00ff44 !important; background: #000 !important;} .gr-button-primary {background: #00ff44 !important; color: #000 !important; font-weight: bold;}"
 
 
 
 
 
109
 
110
  with gr.Blocks(theme=gr.themes.Monochrome(), css=css) as demo:
111
- gr.HTML("<h1 style='text-align:center; letter-spacing:12px; color:#00ff44;'>GHOST SINGULARITY v27</h1>")
112
 
113
  with gr.Row():
114
  with gr.Column(scale=1):
115
  with gr.Row():
116
- out_k = gr.Label(label="KEY / FUNDAMENTAL")
117
- out_b = gr.Label(label="BPM / TEMPO")
118
- in_audio = gr.Audio(label="TRACK SOURCE", type="filepath")
119
- in_ref = gr.Audio(label="PRO REFERENCE", type="filepath")
120
  with gr.Row():
121
  fix_btn = gr.Checkbox(label="ADAPTIVE MIX REPAIR", value=False)
122
- mst_btn = gr.Checkbox(label="NEURAL MASTERING", value=False)
123
- in_pwr = gr.Slider(0, 100, label="MASTER ENERGY (LUFS)", value=88)
124
- exec_btn = gr.Button("ENGAGE NEURAL ENGINE", variant="primary")
125
 
126
  with gr.Column(scale=2):
127
- out_audio = gr.Audio(label="MASTERED OUTPUT")
128
- out_diag = gr.Textbox(label="DEEP DIAGNOSTICS", lines=10)
129
 
130
  with gr.Group():
131
- gr.Markdown("### 🎙️ INTERFAZ DE ASESORÍA TÉCNICA")
132
- user_msg = gr.Textbox(placeholder="Haz una pregunta técnica sobre frecuencias, armónicos o dinámica...", label="Consulta")
133
- chat_btn = gr.Button("CONSULTAR INGENIERO JEFE")
134
- chat_out = gr.Textbox(label="RESPUESTA DE IA CUÁNTICA", lines=6)
135
 
136
- exec_btn.click(engine_v27_singularity, [in_audio, in_ref, in_pwr, fix_btn, mst_btn], [out_audio, out_diag, out_k, out_b])
137
- chat_btn.click(mentor_ai_v27, [user_msg], [chat_out])
138
 
139
  demo.launch()
 
3
  import numpy as np
4
  from pydub import AudioSegment
5
 
6
+ # Núcleo de Inteligencia TITAN
7
+ ghost_titan = {
8
+ "key": "--", "bpm": 0, "crest": 0, "phase": "Mono Compatible",
9
+ "harmonics": [], "spectral_density": {}, "analyzed": False
10
  }
11
 
12
+ def engine_v28_titan(t, r, p, active_fix, active_master):
13
+ global ghost_titan
14
  if t is None:
15
+ return None, "ESTADO: OFFLINE", "NO SIGNAL", "--", "0", None
16
 
17
  try:
18
+ # 1. ESCANEO DE ADN SÓNICO (120 seg)
19
+ y, sr = librosa.load(t, duration=120)
20
 
21
+ # Detección de BPM con Voto de Confianza
22
+ onset_env = librosa.onset.onset_strength(y=y, sr=sr)
23
+ tempo = librosa.feature.tempo(onset_envelope=onset_env, sr=sr)
24
  bpm = int(np.atleast_1d(tempo)[0])
25
+ if bpm < 115: bpm *= 2 # Optimizador DnB/Techno
26
 
27
+ # Detección de KEY por Cromatografía FFT
28
+ chroma = librosa.feature.chroma_stft(y=y, sr=sr, n_fft=4096)
29
+ notes = ["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"]
30
+ k = notes[np.argmax(np.mean(chroma, axis=1))]
31
  f_fund = librosa.note_to_hz(k+'1')
 
32
 
33
+ # Análisis de Fase y Crest Factor (Punch)
34
  rms = librosa.feature.rms(y=y)
35
  crest = 20 * np.log10(np.max(np.abs(y)) / (np.mean(rms) + 1e-6))
36
 
37
  S = np.abs(librosa.stft(y))
38
  f = librosa.fft_frequencies(sr=sr)
39
 
40
+ ghost_titan.update({
41
+ "key": k, "bpm": bpm, "crest": crest,
42
+ "harmonics": [round(f_fund * i, 1) for i in range(1, 6)],
43
+ "spectral_density": {
44
+ "sub": np.mean(S[f < 60]),
45
+ "punch": np.mean(S[(f >= 100) & (f < 250)]),
46
+ "mud": np.mean(S[(f >= 250) & (f < 500)]),
47
+ "harsh": np.mean(S[(f >= 3000) & (f < 5000)]),
48
  "air": np.mean(S[f > 12000])
49
  },
50
+ "analyzed": True
51
  })
52
 
53
  audio = AudioSegment.from_file(t)
54
+ log = f"--- GHOST TITAN V28 SYSTEM LOG ---\n\n"
55
 
56
  if active_fix:
57
+ audio = audio.high_pass_filter(34)
58
+ if ghost_titan["spectral_density"]["mud"] > ghost_titan["spectral_density"]["punch"] * 1.3:
59
  audio = audio.low_pass_filter(4500)
60
+ log += "[!] AUTO-FIX: Resonancia de 'lodo' detectada. Filtro adaptativo aplicado.\n"
61
+
 
62
  if active_master:
63
+ # Cadena de Mastering: Glue -> Soft Clipping -> Maximization
64
+ audio = audio.compressor_dynamic_range(threshold=-14.0, ratio=4.2, attack=12.0, release=140.0)
65
+ audio = audio.apply_gain((p / 10) - 2.8)
66
+ log += "[+] MASTER: Cadena Titan activada (Target: -9 LUFS).\n"
67
 
68
+ audio.export("ghost_titan_v28.wav", format="wav")
69
+ log += f"\n▶ DIAGNÓSTICO: Key {k} | BPM {bpm} | Crest {round(crest,1)}dB\n"
70
+ log += f"▶ RECOMENDACIÓN: En {k}, limpia la banda de {ghost_titan['harmonics'][2]}Hz para dar espacio a la voz."
71
 
72
+ return "ghost_titan_v28.wav", log, "Análisis de Fase y Armónicos completado.", k, str(bpm), "https://images.unsplash.com/photo-1514474959185-1472d4c4e0d4?q=80&w=500"
73
 
74
  except Exception as e:
75
+ return None, f"FATAL ERROR: {str(e)}", "ERROR", "!", "0", None
76
 
77
+ def titan_mentor_ai(pregunta):
78
+ if not ghost_titan["analyzed"]: return "Inicia el escaneo primero, Ghost."
79
 
80
+ msg = pregunta.lower()
81
+ k, b, sd = ghost_titan["key"], ghost_titan["bpm"], ghost_titan["spectral_density"]
82
+ h = ghost_titan["harmonics"]
 
 
83
 
84
+ # RESPUESTAS DE INGENIERÍA DE ALTA PRECISIÓN
85
+ if "bajo" in msg or "sub" in msg:
86
+ return f"Para tu nota {k}, el sub real está en {h[0]}Hz. He detectado que tu zona de punch ({h[1]}Hz) está " + ("débil" if sd["punch"] < sd["sub"] else "fuerte") + f". Sugerencia: Usa un EQ dinámico en {h[0]}Hz encadenado al Kick."
87
+
88
+ if "pegada" in msg or "punch" in msg:
89
+ ms_release = round(60000 / b / 2, 1)
90
+ return f"A {b} BPM, el compresor debe respirar. Pon el Attack en 30ms y el Release en {ms_release}ms. Esto evitará que el limitador destruya el transiente de tus drums."
91
+
92
+ if "lodo" in msg or "limpiar" in msg:
93
+ return f"El conflicto está en {h[3]}Hz. Es el 4º armónico de {k}. Baja -3dB ahí con un Q de 1.4 en tus leads y sintes. Notarás una claridad instantánea."
94
+
95
+ if "brillo" in msg or "aire" in msg:
96
+ if sd["air"] < 0.005:
97
+ return "Tu mezcla es demasiado oscura. Añade un High Shelf en 10kHz con +2dB, pero usa un saturador de cinta (Tape) para que no suene digital."
98
+ return "El aire está bien equilibrado. No toques los agudos o fatigarás el oído."
99
+
100
+ return f"Status {k} @ {b} BPM: Tienes un Crest Factor de {round(ghost_titan['crest'], 1)}dB. Tienes margen para subir +3dB de ganancia en el master antes de que pierdas el punch."
101
+
102
+ # --- INTERFAZ "TITAN CONSOLE" ---
103
+ css = """
104
+ body, .gradio-container {background: #020202 !important; color: #00e5ff !important; font-family: 'JetBrains Mono', monospace;}
105
+ .gr-box {border: 2px solid #00e5ff !important; background: #080808 !important; box-shadow: 0 0 15px #00e5ff33;}
106
+ .gr-button-primary {background: linear-gradient(180deg, #00e5ff, #007cff) !important; color: #000 !important; font-weight: bold; border: none;}
107
+ .gr-button-primary:hover {box-shadow: 0 0 25px #00e5ff; transform: scale(1.02);}
108
+ .gr-label {color: #ff0055 !important; font-size: 0.9em;}
109
+ .gr-textbox textarea {background: #000 !important; color: #00ff88 !important; border: 1px solid #00e5ff22 !important;}
110
+ """
111
 
112
  with gr.Blocks(theme=gr.themes.Monochrome(), css=css) as demo:
113
+ gr.HTML("<h1 style='text-align:center; letter-spacing:15px; color:#00e5ff; text-shadow: 0 0 10px #00e5ff;'>GHOST TITAN v28</h1>")
114
 
115
  with gr.Row():
116
  with gr.Column(scale=1):
117
  with gr.Row():
118
+ out_k = gr.Label(label="KEY (DETECCIÓN FFT)")
119
+ out_b = gr.Label(label="BPM (ANALYSIS)")
120
+ in_audio = gr.Audio(label="SOURCE AUDIO", type="filepath")
121
+ in_ref = gr.Audio(label="MASTER REFERENCE", type="filepath")
122
  with gr.Row():
123
  fix_btn = gr.Checkbox(label="ADAPTIVE MIX REPAIR", value=False)
124
+ mst_btn = gr.Checkbox(label="TITAN MASTERING", value=False)
125
+ in_pwr = gr.Slider(0, 100, label="MASTER PRESSURE (LUFS TARGET)", value=85)
126
+ exec_btn = gr.Button("ENGAGE SYSTEM", variant="primary")
127
 
128
  with gr.Column(scale=2):
129
+ out_audio = gr.Audio(label="MONITOR OUTPUT (32-BIT)")
130
+ out_diag = gr.Textbox(label="SISTEMA DE DIAGNÓSTICO PROFUNDO", lines=10)
131
 
132
  with gr.Group():
133
+ gr.Markdown("### 🎙️ TITAN MENTOR (IA de Ingeniería Avanzada)")
134
+ user_msg = gr.Textbox(placeholder="Pregunta por la pegada, el lodo, los armónicos o la fase...", label="Consulta técnica")
135
+ chat_btn = gr.Button("CONSULTAR INGENIERO TITAN")
136
+ chat_out = gr.Textbox(label="SOLUCIÓN TÉCNICA CALCULADA", lines=6)
137
 
138
+ exec_btn.click(engine_v28_titan, [in_audio, in_ref, in_pwr, fix_btn, mst_btn], [out_audio, out_diag, out_k, out_b])
139
+ chat_btn.click(titan_mentor_ai, [user_msg], [chat_out])
140
 
141
  demo.launch()