Next commited on
Commit
5e58148
·
verified ·
1 Parent(s): e949f37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -48
app.py CHANGED
@@ -1,55 +1,55 @@
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()
 
1
  import gradio as gr
 
 
 
 
 
2
 
3
+ # HTML and JavaScript for the MidiVisualizer
4
+ visualizer_html = """
5
+ <midi-visualizer></midi-visualizer>
6
+ <div id="midi_visualizer_container" style="width: 100%;"></div>
7
+ <audio id="midi_audio" controls></audio>
8
+
9
+ <script>
10
+ // Insert the provided JavaScript code here
11
+ </script>
12
+ """
13
+
14
+ def process_midi(midi_file):
15
+ # Process the MIDI file and return the visualization and audio
16
+ midi_data = midi_file.read()
17
+ # You would need to implement the actual MIDI parsing and event extraction logic here.
18
+ # For the sake of example, let's assume we generate MIDI events and an audio file.
19
+ midi_events = extract_midi_events(midi_data)
20
+ audio_file = generate_audio_from_midi(midi_data)
21
 
22
+ # Send MIDI events to the visualizer via messages
23
+ for event in midi_events:
24
+ gr.send_message("visualizer_append", {"data": event})
25
+ gr.send_message("visualizer_end")
26
+
27
+ return visualizer_html, audio_file
28
+
29
+ def extract_midi_events(midi_data):
30
+ # Dummy implementation to extract MIDI events from midi_data
31
+ # Replace this with actual MIDI parsing logic
32
+ return [
33
+ ["note", 0, 1, 1, 0, 60, 127],
34
+ ["note", 1, 1, 1, 0, 64, 127],
35
+ ["note", 2, 1, 1, 0, 67, 127],
36
+ ]
37
+
38
+ def generate_audio_from_midi(midi_data):
39
+ # Dummy implementation to generate an audio file from midi_data
40
+ # Replace this with actual audio generation logic
41
+ return "path/to/generated/audio/file.mp3"
42
+
43
+ # Gradio UI
44
  with gr.Blocks() as demo:
 
45
  with gr.Row():
46
+ gr.Markdown("# MIDI Visualizer")
47
+ with gr.Row():
48
+ midi_input = gr.File(label="Upload MIDI File")
49
+ with gr.Row():
50
+ output_html = gr.HTML()
51
+ audio_output = gr.Audio()
52
+
53
+ midi_input.change(process_midi, inputs=midi_input, outputs=[output_html, audio_output])
54
 
55
  demo.launch()