Marcepelaez commited on
Commit
fc2e2af
1 Parent(s): 19d2821
Files changed (1) hide show
  1. app.py +94 -1
app.py CHANGED
@@ -11,6 +11,37 @@ NOTES = {
11
  "G4": 392.00, "A4": 440.00, "B4": 493.88, "C5": 523.25
12
  }
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def generate_tone(frequency: float, duration: float, sample_rate: int,
15
  amplitude: int, wave_type: str, subdivisions: int = 1) -> np.ndarray:
16
  """
@@ -165,6 +196,32 @@ def generate_music(melody: str, counter_melody: str, wave_type: str,
165
  except Exception as e:
166
  raise gr.Error(f"Error generando la música: {str(e)}")
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  # Interfaz de Gradio
169
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="blue")) as ui:
170
  gr.Markdown("""
@@ -189,6 +246,21 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="blue"))
189
  placeholder="Ejemplo: 330:1, 349:0.5",
190
  lines=3
191
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
  with gr.Row():
194
  with gr.Column():
@@ -235,6 +307,22 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="blue"))
235
  output_audio = gr.Audio(label="Audio Generado")
236
 
237
  generate_btn = gr.Button("Generar Música", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  generate_btn.click(
239
  fn=generate_music,
240
  inputs=[melody, counter_melody, wave_type, tempo,
@@ -251,4 +339,9 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="blue"))
251
  """)
252
 
253
  if __name__ == "__main__":
254
- ui.launch()
 
 
 
 
 
 
11
  "G4": 392.00, "A4": 440.00, "B4": 493.88, "C5": 523.25
12
  }
13
 
14
+ # Melodías predefinidas
15
+ PREDEFINED_MELODIES = {
16
+ "Melodía Simple": {
17
+ "melody": "261.63:1, 329.63:0.5, 392.00:1, 440.00:0.5",
18
+ "counter_melody": "130.81:1, 164.81:0.5, 196.00:1, 220.00:0.5",
19
+ "wave_type": "square",
20
+ "rhythm_pattern": "1,0,1,0",
21
+ "tempo": 120,
22
+ "melody_subdivisions": 2,
23
+ "counter_subdivisions": 1
24
+ },
25
+ "Onda Triangular": {
26
+ "melody": "440.00:0.5, 493.88:0.5, 523.25:1, 493.88:0.5",
27
+ "counter_melody": "329.63:0.5, 349.23:0.5, 392.00:1, 349.23:0.5",
28
+ "wave_type": "triangle",
29
+ "rhythm_pattern": "1,1,0,0",
30
+ "tempo": 140,
31
+ "melody_subdivisions": 3,
32
+ "counter_subdivisions": 2
33
+ },
34
+ "Ritmo Complejo": {
35
+ "melody": "261.63:0.25, 329.63:0.25, 392.00:0.5, 440.00:0.25, 493.88:0.25",
36
+ "counter_melody": "130.81:0.25, 164.81:0.25, 196.00:0.5, 220.00:0.25, 246.94:0.25",
37
+ "wave_type": "sawtooth",
38
+ "rhythm_pattern": "1,0,1,1,0",
39
+ "tempo": 180,
40
+ "melody_subdivisions": 4,
41
+ "counter_subdivisions": 3
42
+ }
43
+ }
44
+
45
  def generate_tone(frequency: float, duration: float, sample_rate: int,
46
  amplitude: int, wave_type: str, subdivisions: int = 1) -> np.ndarray:
47
  """
 
196
  except Exception as e:
197
  raise gr.Error(f"Error generando la música: {str(e)}")
198
 
199
+ def create_note_buttons():
200
+ """Crea botones para las notas predefinidas."""
201
+ return [gr.Button(note) for note in NOTES.keys()]
202
+
203
+ def add_note(note: str, current_melody: str) -> str:
204
+ """Añade una nota al campo de melodía."""
205
+ freq = NOTES[note]
206
+ if current_melody:
207
+ return f"{current_melody}, {freq}:1"
208
+ return f"{freq}:1"
209
+
210
+ def load_predefined_melody(melody_name: str):
211
+ """
212
+ Carga los parámetros de una melodía predefinida.
213
+ """
214
+ melody_params = PREDEFINED_MELODIES.get(melody_name, {})
215
+ return (
216
+ melody_params.get('melody', ''),
217
+ melody_params.get('counter_melody', ''),
218
+ melody_params.get('wave_type', 'square'),
219
+ melody_params.get('tempo', 120),
220
+ melody_params.get('rhythm_pattern', ''),
221
+ melody_params.get('melody_subdivisions', 1),
222
+ melody_params.get('counter_subdivisions', 1)
223
+ )
224
+
225
  # Interfaz de Gradio
226
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", secondary_hue="blue")) as ui:
227
  gr.Markdown("""
 
246
  placeholder="Ejemplo: 330:1, 349:0.5",
247
  lines=3
248
  )
249
+
250
+ with gr.Column(scale=1):
251
+ gr.Markdown("### Notas Predefinidas")
252
+ with gr.Row():
253
+ note_buttons = create_note_buttons()
254
+ for btn in note_buttons:
255
+ btn.click(fn=add_note, inputs=[btn, melody], outputs=melody)
256
+
257
+ with gr.Accordion("Melodías Predefinidas", open=False):
258
+ predefined_melody_dropdown = gr.Dropdown(
259
+ list(PREDEFINED_MELODIES.keys()),
260
+ label="Seleccionar Melodía Predefinida"
261
+ )
262
+
263
+ load_melody_btn = gr.Button("Cargar Melodía")
264
 
265
  with gr.Row():
266
  with gr.Column():
 
307
  output_audio = gr.Audio(label="Audio Generado")
308
 
309
  generate_btn = gr.Button("Generar Música", variant="primary")
310
+
311
+ # Conexión de eventos
312
+ load_melody_btn.click(
313
+ fn=load_predefined_melody,
314
+ inputs=predefined_melody_dropdown,
315
+ outputs=[
316
+ melody,
317
+ counter_melody,
318
+ wave_type,
319
+ tempo,
320
+ rhythm_pattern,
321
+ melody_subdivisions,
322
+ counter_subdivisions
323
+ ]
324
+ )
325
+
326
  generate_btn.click(
327
  fn=generate_music,
328
  inputs=[melody, counter_melody, wave_type, tempo,
 
339
  """)
340
 
341
  if __name__ == "__main__":
342
+ ui.launch()
343
+
344
+ # requirements.txt content
345
+ # numpy==1.23.5
346
+ # gradio==3.50.2
347
+ # wave==0.0.2