Spaces:
Sleeping
Sleeping
Next
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,55 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
clip = mp.ColorClip(size=(640, 480), color=(255, 255, 255), duration=5) # 5 seconds white screen
|
23 |
-
video_file = "visualization.mp4"
|
24 |
-
clip.write_videofile(video_file, fps=24)
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
# Gradio Interface
|
29 |
with gr.Blocks() as demo:
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import mido
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import numpy as np
|
5 |
+
import tempfile
|
6 |
+
from midi2audio import FluidSynth
|
7 |
+
|
8 |
+
def midi_to_audio(midi_file):
|
9 |
+
# Convert MIDI to audio using FluidSynth
|
10 |
+
fs = FluidSynth()
|
11 |
+
with tempfile.NamedTemporaryFile(suffix=".wav") as tmp:
|
12 |
+
fs.midi_to_audio(midi_file.name, tmp.name)
|
13 |
+
return tmp.read()
|
14 |
+
|
15 |
+
def visualize_midi(midi_file):
|
16 |
+
# Visualize MIDI data (a simple piano roll visualization)
|
17 |
+
midi = mido.MidiFile(midi_file.name)
|
18 |
+
notes = []
|
19 |
+
for msg in midi.play():
|
20 |
+
if msg.type == 'note_on' and msg.velocity > 0:
|
21 |
+
notes.append((msg.note, msg.time))
|
|
|
|
|
|
|
22 |
|
23 |
+
if not notes:
|
24 |
+
return "No note data found in the MIDI file."
|
25 |
+
|
26 |
+
fig, ax = plt.subplots()
|
27 |
+
notes = np.array(notes)
|
28 |
+
ax.scatter(notes[:, 1].cumsum(), notes[:, 0])
|
29 |
+
ax.set_xlabel("Time (s)")
|
30 |
+
ax.set_ylabel("Note")
|
31 |
+
ax.set_title("MIDI Note Visualization")
|
32 |
+
|
33 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
34 |
+
plt.savefig(tmp.name)
|
35 |
+
plt.close(fig)
|
36 |
+
return tmp.name
|
37 |
+
|
38 |
+
def process_midi(file):
|
39 |
+
audio = midi_to_audio(file)
|
40 |
+
visualization = visualize_midi(file)
|
41 |
+
return audio, visualization
|
42 |
|
|
|
43 |
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("# MIDI Visualizer and Player")
|
45 |
+
with gr.Row():
|
46 |
+
with gr.Column():
|
47 |
+
midi_input = gr.File(label="Upload MIDI File")
|
48 |
+
play_button = gr.Button("Play and Visualize")
|
49 |
+
with gr.Column():
|
50 |
+
midi_audio_output = gr.Audio(label="MIDI Audio")
|
51 |
+
midi_visualization_output = gr.Image(label="MIDI Visualization")
|
52 |
+
|
53 |
+
play_button.click(process_midi, inputs=midi_input, outputs=[midi_audio_output, midi_visualization_output])
|
54 |
|
55 |
demo.launch()
|