Next commited on
Commit
0bf9c89
·
verified ·
1 Parent(s): df0a19c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -29
app.py CHANGED
@@ -1,35 +1,55 @@
1
  import gradio as gr
2
- import pygame
3
- import moviepy.editor as mp
4
- import midi # You might need a different package based on your needs, e.g., mido or pretty_midi
5
-
6
- # Function to play MIDI and generate video
7
- def play_midi_and_generate_video(midi_file):
8
- # Initialize Pygame
9
- pygame.mixer.init()
10
- pygame.mixer.music.load(midi_file)
11
-
12
- # Play the MIDI file
13
- pygame.mixer.music.play()
14
-
15
- # Process the MIDI file to generate a visualizer
16
- pattern = midi.read_midifile(midi_file)
17
-
18
- # Placeholder for visualization creation logic
19
- # Here you would convert the MIDI data into some form of visual output
20
- # For the purpose of this example, we'll just create a blank video
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
- return video_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- # Gradio Interface
29
  with gr.Blocks() as demo:
30
- midi_input = gr.File(label="Upload MIDI file")
31
- video_output = gr.Video()
32
-
33
- midi_input.change(play_midi_and_generate_video, inputs=[midi_input], outputs=[video_output])
 
 
 
 
 
 
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()