Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,17 +7,26 @@ import random
|
|
| 7 |
from datasets import load_dataset
|
| 8 |
|
| 9 |
# ==========================================
|
| 10 |
-
# 1. DATA PREPARATION (
|
| 11 |
# ==========================================
|
| 12 |
-
|
| 13 |
-
dataset = load_dataset("ailsntua/Chordonomicon", split="train", streaming=True)
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
corpus_by_genre = {genre: set() for genre in target_genres}
|
| 17 |
pattern = re.compile(r'<([^>]+)>\s*([^<]+)')
|
| 18 |
|
| 19 |
for row in dataset:
|
| 20 |
-
|
|
|
|
| 21 |
break
|
| 22 |
|
| 23 |
main_genre = str(row.get('main_genre', '')).lower()
|
|
@@ -26,7 +35,7 @@ for row in dataset:
|
|
| 26 |
|
| 27 |
matched_genre = None
|
| 28 |
for g in target_genres:
|
| 29 |
-
if g in combined_genres and len(corpus_by_genre[g]) <
|
| 30 |
matched_genre = g
|
| 31 |
break
|
| 32 |
|
|
@@ -42,10 +51,9 @@ for row in dataset:
|
|
| 42 |
|
| 43 |
if chords and ('verse' in tag or 'chorus' in tag):
|
| 44 |
corpus_by_genre[matched_genre].add(chords)
|
| 45 |
-
if len(corpus_by_genre[matched_genre]) >= 100:
|
| 46 |
-
break
|
| 47 |
|
| 48 |
corpus_by_genre = {g: list(chords) for g, chords in corpus_by_genre.items()}
|
|
|
|
| 49 |
|
| 50 |
# ==========================================
|
| 51 |
# 2. MARKOV CHAIN LOGIC
|
|
@@ -110,6 +118,23 @@ NOTE_TO_MIDI = {'C': 60, 'Cs': 61, 'Db': 61, 'D': 62, 'Ds': 63, 'Eb': 63, 'E': 6
|
|
| 110 |
MIDI_TO_NOTE = {60: 'C', 61: 'Db', 62: 'D', 63: 'Eb', 64: 'E', 65: 'F', 66: 'Gb', 67: 'G', 68: 'Ab', 69: 'A', 70: 'Bb', 71: 'B'}
|
| 111 |
CHORD_INTERVALS = {'maj': [0, 4, 7], 'min': [0, 3, 7], '7': [0, 4, 7, 10], 'maj7': [0, 4, 7, 11], 'min7': [0, 3, 7, 10], 'sus4': [0, 5, 7], 'sus2': [0, 2, 7], 'dim': [0, 3, 6], 'no3d': [0, 7], '5': [0, 7]}
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
def parse_chord_to_midi(chord_string):
|
| 114 |
if not chord_string or chord_string == 'N': return [], ""
|
| 115 |
chord_string = chord_string.split('/')[0]
|
|
@@ -139,12 +164,14 @@ def apply_voicing(pitches, voicing_type):
|
|
| 139 |
elif voicing_type == "Open / Spread" and len(pitches) >= 3: pitches[0] -= 12; pitches[1] += 12
|
| 140 |
return sorted(pitches) if voicing_type != "Open / Spread" else pitches
|
| 141 |
|
| 142 |
-
def generate_audio_file(progression_string,
|
| 143 |
if not progression_string.strip(): return None, None, ""
|
| 144 |
|
| 145 |
-
|
| 146 |
-
prog_num =
|
| 147 |
-
|
|
|
|
|
|
|
| 148 |
|
| 149 |
midi = pretty_midi.PrettyMIDI(initial_tempo=120)
|
| 150 |
inst = pretty_midi.Instrument(program=prog_num)
|
|
@@ -160,7 +187,10 @@ def generate_audio_file(progression_string, instrument, transpose_semitones, voi
|
|
| 160 |
normalized_root = ((pitches[0] - 60) % 12) + 60
|
| 161 |
transposed_chord_names.append(MIDI_TO_NOTE.get(normalized_root, "C") + quality)
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
pitches = apply_voicing(pitches, voicing_type)
|
| 165 |
|
| 166 |
for pitch in pitches:
|
|
@@ -184,7 +214,7 @@ def app_logic(genre, order, length, instrument, transpose, voicing):
|
|
| 184 |
corpus = corpus_by_genre.get(genre, [])
|
| 185 |
|
| 186 |
if not corpus:
|
| 187 |
-
return f"Error: No chords found for {genre}.", "", None, None
|
| 188 |
|
| 189 |
model = train_markov_model(corpus, order=int(order))
|
| 190 |
raw_chords = generate_progression(model, target_length=int(length), order=int(order))
|
|
@@ -201,11 +231,11 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
|
| 201 |
|
| 202 |
with gr.Row():
|
| 203 |
with gr.Column(scale=1):
|
| 204 |
-
gr.Markdown("### 1. Training Data")
|
| 205 |
genre_dropdown = gr.Dropdown(
|
| 206 |
choices=[g.capitalize() for g in target_genres],
|
| 207 |
value="Pop",
|
| 208 |
-
label="Dataset Genre
|
| 209 |
)
|
| 210 |
|
| 211 |
gr.Markdown("### 2. Generation Settings")
|
|
@@ -216,10 +246,16 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
|
| 216 |
transpose_slider = gr.Slider(minimum=-12, maximum=12, step=1, value=0, label="Transpose (Semitones)")
|
| 217 |
voicing_dropdown = gr.Dropdown(
|
| 218 |
choices=["Root Position", "First Inversion", "Second Inversion", "Open / Spread", "Random Voice Leading"],
|
| 219 |
-
value="
|
| 220 |
label="Chord Voicings"
|
| 221 |
)
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
generate_btn = gr.Button("Generate Chords", variant="primary")
|
| 225 |
|
|
|
|
| 7 |
from datasets import load_dataset
|
| 8 |
|
| 9 |
# ==========================================
|
| 10 |
+
# 1. DATA PREPARATION (RANDOM SAMPLED)
|
| 11 |
# ==========================================
|
| 12 |
+
MAX_PROGRESSIONS = 2000
|
|
|
|
| 13 |
|
| 14 |
+
print(f"Downloading and shuffling dataset... targeting {MAX_PROGRESSIONS} random progressions per genre.")
|
| 15 |
+
|
| 16 |
+
# The magic happens here: .shuffle(buffer_size=10000) mixes the stream on the fly!
|
| 17 |
+
dataset = load_dataset(
|
| 18 |
+
"ailsntua/Chordonomicon",
|
| 19 |
+
split="train",
|
| 20 |
+
streaming=True
|
| 21 |
+
).shuffle(seed=random.randint(1, 1000), buffer_size=10000)
|
| 22 |
+
|
| 23 |
+
target_genres = ["pop", "rock", "jazz", "metal", "country", "blues", "r&b", "folk", "electronic"]
|
| 24 |
corpus_by_genre = {genre: set() for genre in target_genres}
|
| 25 |
pattern = re.compile(r'<([^>]+)>\s*([^<]+)')
|
| 26 |
|
| 27 |
for row in dataset:
|
| 28 |
+
# Stop processing once EVERY genre has hit the max cap
|
| 29 |
+
if all(len(progressions) >= MAX_PROGRESSIONS for progressions in corpus_by_genre.values()):
|
| 30 |
break
|
| 31 |
|
| 32 |
main_genre = str(row.get('main_genre', '')).lower()
|
|
|
|
| 35 |
|
| 36 |
matched_genre = None
|
| 37 |
for g in target_genres:
|
| 38 |
+
if g in combined_genres and len(corpus_by_genre[g]) < MAX_PROGRESSIONS:
|
| 39 |
matched_genre = g
|
| 40 |
break
|
| 41 |
|
|
|
|
| 51 |
|
| 52 |
if chords and ('verse' in tag or 'chorus' in tag):
|
| 53 |
corpus_by_genre[matched_genre].add(chords)
|
|
|
|
|
|
|
| 54 |
|
| 55 |
corpus_by_genre = {g: list(chords) for g, chords in corpus_by_genre.items()}
|
| 56 |
+
print("Randomized dataset loaded successfully!")
|
| 57 |
|
| 58 |
# ==========================================
|
| 59 |
# 2. MARKOV CHAIN LOGIC
|
|
|
|
| 118 |
MIDI_TO_NOTE = {60: 'C', 61: 'Db', 62: 'D', 63: 'Eb', 64: 'E', 65: 'F', 66: 'Gb', 67: 'G', 68: 'Ab', 69: 'A', 70: 'Bb', 71: 'B'}
|
| 119 |
CHORD_INTERVALS = {'maj': [0, 4, 7], 'min': [0, 3, 7], '7': [0, 4, 7, 10], 'maj7': [0, 4, 7, 11], 'min7': [0, 3, 7, 10], 'sus4': [0, 5, 7], 'sus2': [0, 2, 7], 'dim': [0, 3, 6], 'no3d': [0, 7], '5': [0, 7]}
|
| 120 |
|
| 121 |
+
# General MIDI Patch Numbers (0-indexed)
|
| 122 |
+
INSTRUMENT_MAP = {
|
| 123 |
+
"Acoustic Grand Piano": 0,
|
| 124 |
+
"Electric Piano (Rhodes)": 4,
|
| 125 |
+
"Drawbar Organ": 16,
|
| 126 |
+
"Acoustic Guitar (Nylon)": 24,
|
| 127 |
+
"Electric Guitar (Clean)": 27,
|
| 128 |
+
"Electric Guitar (Distortion)": 30,
|
| 129 |
+
"Synth Pad 1 (New Age)": 88,
|
| 130 |
+
"Synth Pad 2 (Warm)": 89,
|
| 131 |
+
"Synth Pad 3 (Polysynth)": 90,
|
| 132 |
+
"Synth Pad 4 (Choir)": 91,
|
| 133 |
+
"Synth Pad 7 (Halo)": 94,
|
| 134 |
+
"Synth Pad 8 (Sweep)": 95,
|
| 135 |
+
"Sci-Fi / Atmosphere": 103
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
def parse_chord_to_midi(chord_string):
|
| 139 |
if not chord_string or chord_string == 'N': return [], ""
|
| 140 |
chord_string = chord_string.split('/')[0]
|
|
|
|
| 164 |
elif voicing_type == "Open / Spread" and len(pitches) >= 3: pitches[0] -= 12; pitches[1] += 12
|
| 165 |
return sorted(pitches) if voicing_type != "Open / Spread" else pitches
|
| 166 |
|
| 167 |
+
def generate_audio_file(progression_string, instrument_name, transpose_semitones, voicing_type):
|
| 168 |
if not progression_string.strip(): return None, None, ""
|
| 169 |
|
| 170 |
+
# Look up the correct MIDI program number from our dictionary
|
| 171 |
+
prog_num = INSTRUMENT_MAP.get(instrument_name, 0)
|
| 172 |
+
|
| 173 |
+
# Give guitars and synths a slightly higher velocity so they cut through
|
| 174 |
+
velocity = 100 if prog_num > 20 else 85
|
| 175 |
|
| 176 |
midi = pretty_midi.PrettyMIDI(initial_tempo=120)
|
| 177 |
inst = pretty_midi.Instrument(program=prog_num)
|
|
|
|
| 187 |
normalized_root = ((pitches[0] - 60) % 12) + 60
|
| 188 |
transposed_chord_names.append(MIDI_TO_NOTE.get(normalized_root, "C") + quality)
|
| 189 |
|
| 190 |
+
# Drop the octave if it's a distorted metal guitar
|
| 191 |
+
if instrument_name == "Electric Guitar (Distortion)":
|
| 192 |
+
pitches = [p - 12 for p in pitches]
|
| 193 |
+
|
| 194 |
pitches = apply_voicing(pitches, voicing_type)
|
| 195 |
|
| 196 |
for pitch in pitches:
|
|
|
|
| 214 |
corpus = corpus_by_genre.get(genre, [])
|
| 215 |
|
| 216 |
if not corpus:
|
| 217 |
+
return f"Error: No chords found for {genre}. Wait for the dataset to finish loading in the console.", "", None, None
|
| 218 |
|
| 219 |
model = train_markov_model(corpus, order=int(order))
|
| 220 |
raw_chords = generate_progression(model, target_length=int(length), order=int(order))
|
|
|
|
| 231 |
|
| 232 |
with gr.Row():
|
| 233 |
with gr.Column(scale=1):
|
| 234 |
+
gr.Markdown(f"### 1. Training Data (Up to {MAX_PROGRESSIONS} songs per genre)")
|
| 235 |
genre_dropdown = gr.Dropdown(
|
| 236 |
choices=[g.capitalize() for g in target_genres],
|
| 237 |
value="Pop",
|
| 238 |
+
label="Dataset Genre"
|
| 239 |
)
|
| 240 |
|
| 241 |
gr.Markdown("### 2. Generation Settings")
|
|
|
|
| 246 |
transpose_slider = gr.Slider(minimum=-12, maximum=12, step=1, value=0, label="Transpose (Semitones)")
|
| 247 |
voicing_dropdown = gr.Dropdown(
|
| 248 |
choices=["Root Position", "First Inversion", "Second Inversion", "Open / Spread", "Random Voice Leading"],
|
| 249 |
+
value="Open / Spread", # Open spread sounds incredible on synth pads!
|
| 250 |
label="Chord Voicings"
|
| 251 |
)
|
| 252 |
+
|
| 253 |
+
# Feed the dictionary keys into the dropdown
|
| 254 |
+
instrument_dropdown = gr.Dropdown(
|
| 255 |
+
choices=list(INSTRUMENT_MAP.keys()),
|
| 256 |
+
value="Synth Pad 2 (Warm)",
|
| 257 |
+
label="Instrument"
|
| 258 |
+
)
|
| 259 |
|
| 260 |
generate_btn = gr.Button("Generate Chords", variant="primary")
|
| 261 |
|