Spaces:
Sleeping
Sleeping
Next
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,35 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
for
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
plt.title('MIDI Visualization')
|
| 30 |
-
plt.grid(True)
|
| 31 |
-
|
| 32 |
-
# Save plot to a buffer
|
| 33 |
-
buf = io.BytesIO()
|
| 34 |
-
plt.savefig(buf, format='png')
|
| 35 |
-
buf.seek(0)
|
| 36 |
-
plt.close()
|
| 37 |
-
|
| 38 |
-
return buf
|
| 39 |
-
|
| 40 |
-
def play_midi(midi_file):
|
| 41 |
-
# Convert MIDI to audio using pydub
|
| 42 |
-
audio = AudioSegment.from_file(midi_file.name, format='mid')
|
| 43 |
-
play(audio)
|
| 44 |
-
return "Playing MIDI..."
|
| 45 |
-
|
| 46 |
-
# Create Gradio blocks interface
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
-
midi_input = gr.File(label="Upload MIDI
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
midi_input.change(visualize_midi, inputs=midi_input, outputs=midi_visual)
|
| 53 |
-
play_button.click(play_midi, inputs=midi_input, outputs=None)
|
| 54 |
|
| 55 |
demo.launch()
|
|
|
|
| 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()
|