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