Spaces:
Sleeping
Sleeping
Update app.py and add second_version.py
Browse files- app.py +1 -1
- second_version.py +99 -0
app.py
CHANGED
|
@@ -123,7 +123,7 @@ with gr.Blocks() as ui:
|
|
| 123 |
|
| 124 |
generate_button = gr.Button("Generate Chords")
|
| 125 |
midi_output = gr.File(label="Generated MIDI")
|
| 126 |
-
piano_roll_output = gr.Image(label="Piano Roll
|
| 127 |
music_score_output = gr.Image(label="Score Visualization")
|
| 128 |
audio_output = gr.Audio(label="Play Generated Chords", value=None, interactive=False)
|
| 129 |
|
|
|
|
| 123 |
|
| 124 |
generate_button = gr.Button("Generate Chords")
|
| 125 |
midi_output = gr.File(label="Generated MIDI")
|
| 126 |
+
piano_roll_output = gr.Image(label="Piano Roll")
|
| 127 |
music_score_output = gr.Image(label="Score Visualization")
|
| 128 |
audio_output = gr.Audio(label="Play Generated Chords", value=None, interactive=False)
|
| 129 |
|
second_version.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import mido
|
| 3 |
+
from mido import Message, MidiFile, MidiTrack
|
| 4 |
+
import numpy as np
|
| 5 |
+
import os
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def generate_chords(hexachord):
|
| 10 |
+
# Placeholder for your actual chord generation function
|
| 11 |
+
# Assuming hexachord is a list of MIDI note numbers
|
| 12 |
+
chords = []
|
| 13 |
+
for i in range(4): # Generate 4 chords
|
| 14 |
+
chords.append([note + (i * 2) for note in hexachord]) # Simple transposition
|
| 15 |
+
return chords
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def create_midi(chords):
|
| 19 |
+
mid = MidiFile()
|
| 20 |
+
track = MidiTrack()
|
| 21 |
+
mid.tracks.append(track)
|
| 22 |
+
|
| 23 |
+
for chord in chords:
|
| 24 |
+
for note in chord:
|
| 25 |
+
track.append(Message('note_on', note=int(note), velocity=64, time=0))
|
| 26 |
+
for note in chord:
|
| 27 |
+
track.append(Message('note_off', note=int(note), velocity=64, time=480))
|
| 28 |
+
|
| 29 |
+
midi_path = "output.mid"
|
| 30 |
+
mid.save(midi_path)
|
| 31 |
+
return midi_path
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def generate_piano_roll(chords):
|
| 35 |
+
fig, ax = plt.subplots(figsize=(8, 4))
|
| 36 |
+
|
| 37 |
+
for i, chord in enumerate(chords):
|
| 38 |
+
for note in chord:
|
| 39 |
+
ax.broken_barh([(i * 1, 0.8)], (note - 0.4, 0.8), facecolors='blue')
|
| 40 |
+
|
| 41 |
+
ax.set_xlabel("Chord Progression")
|
| 42 |
+
ax.set_ylabel("MIDI Note Number")
|
| 43 |
+
ax.set_yticks(range(min(min(chords)), max(max(chords)) + 1, 2))
|
| 44 |
+
ax.set_xticks(range(len(chords)))
|
| 45 |
+
ax.set_xticklabels([f"Chord {i + 1}" for i in range(len(chords))])
|
| 46 |
+
ax.invert_yaxis()
|
| 47 |
+
|
| 48 |
+
plt.grid(True, linestyle='--', alpha=0.5)
|
| 49 |
+
plt.savefig("piano_roll.png")
|
| 50 |
+
return "piano_roll.png"
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def process_hexachord(note1, note2, note3, note4, note5, note6):
|
| 54 |
+
notes = [note1, note2, note3, note4, note5, note6]
|
| 55 |
+
notes = [int(note) for note in notes if note is not None] # Convert to int, remove None values
|
| 56 |
+
|
| 57 |
+
if len(notes) != 6 or len(set(notes)) != 6:
|
| 58 |
+
return "Please select exactly 6 unique notes."
|
| 59 |
+
|
| 60 |
+
chords = generate_chords(notes)
|
| 61 |
+
midi_path = create_midi(chords)
|
| 62 |
+
piano_roll_path = generate_piano_roll(chords)
|
| 63 |
+
|
| 64 |
+
return midi_path, piano_roll_path
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# UI Components
|
| 68 |
+
note_options = list(range(21, 109)) # MIDI note numbers
|
| 69 |
+
default_notes = [60, 62, 64, 65, 67, 69] # Default MIDI notes for C major hexachord
|
| 70 |
+
|
| 71 |
+
with gr.Blocks() as ui:
|
| 72 |
+
gr.Markdown("# Hexachord-based Chord Generator")
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
note_inputs = [gr.Dropdown(choices=note_options, label=f"Note {i + 1}", value=default_notes[i]) for i in
|
| 76 |
+
range(6)]
|
| 77 |
+
|
| 78 |
+
generate_button = gr.Button("Generate Chords")
|
| 79 |
+
midi_output = gr.File(label="Generated MIDI")
|
| 80 |
+
piano_roll_output = gr.Image(label="Piano Roll Visualization")
|
| 81 |
+
|
| 82 |
+
generate_button.click(
|
| 83 |
+
fn=process_hexachord,
|
| 84 |
+
inputs=note_inputs,
|
| 85 |
+
outputs=[midi_output, piano_roll_output]
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Detect if running on Hugging Face Spaces
|
| 89 |
+
on_huggingface = "HUGGINGFACE_SPACE" in os.environ
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def launch_app():
|
| 93 |
+
if on_huggingface:
|
| 94 |
+
ui.launch(server_name="0.0.0.0", server_port=7860)
|
| 95 |
+
else:
|
| 96 |
+
ui.launch()
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
launch_app()
|