Spaces:
Sleeping
Sleeping
Next
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
<audio id="midi_audio" controls></audio>
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
<script>
|
10 |
-
//
|
|
|
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 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
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()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
def play_midi(midi_file):
|
4 |
+
# You would use a library like mido to handle the MIDI file
|
5 |
+
# For the purpose of this example, we'll just return the MIDI file path
|
6 |
+
return midi_file.name
|
|
|
7 |
|
8 |
+
css = """
|
9 |
+
<style>
|
10 |
+
.note.active {
|
11 |
+
stroke: black;
|
12 |
+
stroke-width: 0.75;
|
13 |
+
stroke-opacity: 0.75;
|
14 |
+
}
|
15 |
+
midi-visualizer {
|
16 |
+
display: block;
|
17 |
+
overflow-x: scroll;
|
18 |
+
}
|
19 |
+
</style>
|
20 |
+
"""
|
21 |
+
|
22 |
+
html = """
|
23 |
+
<div id="midi_visualizer_container"></div>
|
24 |
+
<audio id="midi_audio" controls>
|
25 |
+
<source src="{{ midi_url }}" type="audio/midi">
|
26 |
+
Your browser does not support the audio element.
|
27 |
+
</audio>
|
28 |
<script>
|
29 |
+
// JavaScript code for handling MIDI visualization and playback
|
30 |
+
// Paste the provided JavaScript code here
|
31 |
</script>
|
32 |
"""
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
with gr.Blocks() as demo:
|
35 |
+
midi_file = gr.File(label="Upload MIDI File", file_count="single", file_types=[".mid"])
|
36 |
+
gr.Markdown(css)
|
37 |
+
midi_url = gr.Variable(value="")
|
38 |
+
gr.Markdown(html)
|
39 |
+
midi_file.change(fn=play_midi, inputs=midi_file, outputs=midi_url)
|
|
|
|
|
|
|
|
|
40 |
|
41 |
demo.launch()
|