Spaces:
Sleeping
Sleeping
| 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() | |