pachet commited on
Commit
05f05be
·
1 Parent(s): 2115f27

Add app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -2
app.py CHANGED
@@ -2,6 +2,7 @@ import shutil
2
 
3
  import gradio as gr
4
  import mido
 
5
  from mido import Message, MidiFile, MidiTrack
6
  import numpy as np
7
  import os
@@ -76,6 +77,33 @@ def convert_midi_to_audio(midi_path):
76
  except Exception as e:
77
  return f"Error converting MIDI to audio: {str(e)}"
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  def launch_score_editor(midi_path):
80
  try:
81
  score = converter.parse(midi_path)
@@ -94,9 +122,10 @@ def process_hexachord(hexachord_str):
94
 
95
  chords = generate_chords(notes)
96
  midi_path = create_midi(chords)
 
97
  audio_path = convert_midi_to_audio(midi_path)
98
 
99
- return midi_path, audio_path
100
 
101
 
102
  with gr.Blocks() as ui:
@@ -105,12 +134,13 @@ with gr.Blocks() as ui:
105
  hexachord_input = gr.Textbox(label="Enter 6 MIDI note numbers (separated by spaces)", value="60 62 64 65 67 69")
106
  generate_button = gr.Button("Generate Chords")
107
  midi_output = gr.File(label="Download MIDI File")
 
108
  audio_output = gr.Audio(label="Play Generated Chords", value=None, interactive=False)
109
 
110
  generate_button.click(
111
  fn=process_hexachord,
112
  inputs=[hexachord_input],
113
- outputs=[midi_output, audio_output]
114
  )
115
 
116
  on_huggingface = "HUGGINGFACE_SPACE" in os.environ
 
2
 
3
  import gradio as gr
4
  import mido
5
+ from matplotlib import pyplot as plt
6
  from mido import Message, MidiFile, MidiTrack
7
  import numpy as np
8
  import os
 
77
  except Exception as e:
78
  return f"Error converting MIDI to audio: {str(e)}"
79
 
80
+
81
+ def generate_piano_roll(chords):
82
+ fig, ax = plt.subplots(figsize=(8, 4))
83
+
84
+ for i, chord in enumerate(chords):
85
+ for note in chord:
86
+ ax.broken_barh([(i * 1, 0.8)], (note.pitch.midi - 0.4, 0.8), facecolors='blue')
87
+
88
+ ax.set_xlabel("Chord Progression")
89
+ ax.set_ylabel("MIDI Note Number")
90
+ min_min_chords = 128
91
+ max_max_chords = 0
92
+ for ch in chords:
93
+ for note in ch:
94
+ if note.pitch.midi < min_min_chords:
95
+ min_min_chords = note.pitch.midi
96
+ if note.pitch.midi > max_max_chords:
97
+ max_max_chords = note.pitch.midi
98
+ ax.set_yticks(range(min_min_chords, max_max_chords + 1, 2))
99
+ ax.set_xticks(range(len(chords)))
100
+ ax.set_xticklabels([f"Chord {i + 1}" for i in range(len(chords))])
101
+
102
+ plt.grid(True, linestyle='--', alpha=0.5)
103
+ plt.savefig("piano_roll.png")
104
+ return "piano_roll.png"
105
+
106
+
107
  def launch_score_editor(midi_path):
108
  try:
109
  score = converter.parse(midi_path)
 
122
 
123
  chords = generate_chords(notes)
124
  midi_path = create_midi(chords)
125
+ piano_roll_path = generate_piano_roll(chords)
126
  audio_path = convert_midi_to_audio(midi_path)
127
 
128
+ return midi_path, piano_roll_path, audio_path
129
 
130
 
131
  with gr.Blocks() as ui:
 
134
  hexachord_input = gr.Textbox(label="Enter 6 MIDI note numbers (separated by spaces)", value="60 62 64 65 67 69")
135
  generate_button = gr.Button("Generate Chords")
136
  midi_output = gr.File(label="Download MIDI File")
137
+ piano_roll_output = gr.Image(label="Piano Roll Visualization")
138
  audio_output = gr.Audio(label="Play Generated Chords", value=None, interactive=False)
139
 
140
  generate_button.click(
141
  fn=process_hexachord,
142
  inputs=[hexachord_input],
143
+ outputs=[midi_output, piano_roll_output, audio_output]
144
  )
145
 
146
  on_huggingface = "HUGGINGFACE_SPACE" in os.environ