Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import mido | |
| from mido import Message, MidiFile, MidiTrack | |
| import numpy as np | |
| import os | |
| def generate_chords(hexachord): | |
| # Placeholder for your actual chord generation function | |
| # Assuming hexachord is a list of MIDI note numbers | |
| chords = [] | |
| for i in range(4): # Generate 4 chords | |
| chords.append([note + (i * 2) for note in hexachord]) # Simple transposition | |
| return chords | |
| def create_midi(chords): | |
| mid = MidiFile() | |
| track = MidiTrack() | |
| mid.tracks.append(track) | |
| for chord in chords: | |
| for note in chord: | |
| track.append(Message('note_on', note=int(note), velocity=64, time=0)) | |
| for note in chord: | |
| track.append(Message('note_off', note=int(note), velocity=64, time=480)) | |
| midi_path = "output.mid" | |
| mid.save(midi_path) | |
| return midi_path | |
| def process_hexachord(note1, note2, note3, note4, note5, note6): | |
| notes = [note1, note2, note3, note4, note5, note6] | |
| notes = [int(note) for note in notes if note is not None] # Convert to int, remove None values | |
| if len(notes) != 6 or len(set(notes)) != 6: | |
| return "Please select exactly 6 unique notes." | |
| chords = generate_chords(notes) | |
| midi_path = create_midi(chords) | |
| return midi_path | |
| # UI Components | |
| note_options = list(range(21, 109)) # MIDI note numbers | |
| default_notes = [60, 62, 64, 65, 67, 69] # Default MIDI notes for C major hexachord | |
| with gr.Blocks() as ui: | |
| gr.Markdown("# Hexachord-based Chord Generator") | |
| with gr.Row(): | |
| note_inputs = [gr.Dropdown(choices=note_options, label=f"Note {i + 1}", value=default_notes[i]) for i in | |
| range(6)] | |
| generate_button = gr.Button("Generate Chords") | |
| midi_output = gr.File(label="Generated MIDI") | |
| generate_button.click( | |
| fn=process_hexachord, | |
| inputs=note_inputs, | |
| outputs=[midi_output] | |
| ) | |
| # Detect if running on Hugging Face Spaces | |
| on_huggingface = "HUGGINGFACE_SPACE" in os.environ | |
| def launch_app(): | |
| if on_huggingface: | |
| ui.launch(server_name="0.0.0.0", server_port=7860) | |
| else: | |
| ui.launch() | |
| launch_app() | |