| import os |
| import json |
| import random |
| from music21 import stream, roman, note, key, meter, tempo |
|
|
| |
| OUTPUT_DIR = "midi" |
| JSONL_FILE = "dataset.jsonl" |
| os.makedirs(OUTPUT_DIR, exist_ok=True) |
|
|
| NUM_RUNS = 2 |
|
|
| |
| MAJOR_KEYS = ['C', 'G', 'D', 'A', 'E', 'B', 'F#', 'C#', 'F', 'Bb', 'Eb', 'Ab', 'Db', 'Gb', 'Cb'] |
| MAJOR_PROGRESSIONS = [ |
| |
| ['I', 'V', 'vi', 'IV'], |
| ['I', 'V', 'vi', 'iii', 'IV'], |
| ['vi', 'V', 'IV', 'V'], |
| ['I', 'vi', 'IV', 'V'], |
| ['I', 'IV', 'vi', 'V'], |
| ['I', 'V', 'IV', 'V'], |
| ['IV', 'ii', 'I64', 'V', 'I'], |
| ['I', 'V6', 'vi', 'V'], |
| ['I', 'ii7', 'I6', 'IV'], |
| ['I', 'iii6', 'vi', 'IV'], |
| ['IV', 'I6', 'V'], |
| ['IV', 'I6', 'ii'], |
| ['IV', 'V', 'V6/vi', 'vi'], |
| ['I', 'V/vi', 'vi'], |
| ['vi', 'vi42', 'IV'], |
| ['I', 'V7/IV', 'IV'], |
| ['V', 'viio/vi', 'vi'], |
| ['IV', 'iv', 'I'], |
| ['I', 'bVII', 'IV', 'I'], |
| ['I', 'bVI', 'V'], |
| ['I', 'V', 'IV', 'bVII', 'I'], |
| ['I', 'V', 'vi', 'iii', 'IV', 'I', 'IV', 'V'], |
| ['I', 'IV', 'viio', 'iii', 'vi', 'ii', 'V', 'I'], |
| ['I', 'Imaj7', 'I7', 'IV', 'iv', 'I', 'V7', 'I'], |
| ['I', 'vi', 'ii7', 'V7', 'iii7', 'V7/ii', 'ii7', 'V7', 'Imaj7'], |
| ['vi', 'V6', 'I', 'IV', 'I6', 'ii7', 'I64', 'V7', 'I'], |
| ['vi', 'V', 'IV', 'III'], |
| ['ii7', 'bII7', 'Imaj7'], |
| ['Imaj7', 'bIIImaj7', 'bVImaj7', 'bIImaj7', 'Imaj7'], |
| ['I', 'bIII', 'IV', 'iv', 'I'], |
| ['I', 'II', 'IV', 'I'], |
| ['IV', 'iv', 'bVI', 'bVII', 'I'], |
| ['Imaj7', 'vi7', 'ii7', 'V7'], |
| ['vi', 'ii', 'v', 'I'] |
| ] |
|
|
| |
| |
| MINOR_KEYS = ['c', 'g', 'd', 'a', 'e', 'b', 'f#', 'c#', 'f', 'bb', 'eb', 'ab', 'db', 'gb'] |
| MINOR_PROGRESSIONS = [ |
| ['i', 'iv', 'V'], |
| ['i', 'VI', 'III', 'VII'], |
| ['i', 'v', 'VI', 'VII'], |
| ['i', 'iiø7', 'V7', 'i'], |
| ['i', 'bVII', 'bVI', 'V7'], |
| ['i', 'iv6', 'V7', 'i'], |
| ['i', 'VI', 'iv', 'V7', 'i'], |
| ['i', 'VII', 'v', 'VI'] |
| ] |
|
|
| |
| DATASET_RECIPES = [ |
| {"scale_type": "Major", "keys": MAJOR_KEYS, "progressions": MAJOR_PROGRESSIONS}, |
| {"scale_type": "Minor", "keys": MINOR_KEYS, "progressions": MINOR_PROGRESSIONS} |
| ] |
|
|
| |
| QUALITY_MAPS = [ |
| {'major': 'major', 'minor': 'minor', 'dominant': 'dominant 7', 'diminished': 'diminished', 'augmented': 'augmented', 'half-diminished': 'half-diminished 7'}, |
| {'major': 'maj', 'minor': 'min', 'dominant': 'dom 7', 'diminished': 'dim', 'augmented': 'aug', 'half-diminished': 'half-dim 7'}, |
| {'major': 'major', 'minor': 'minor', 'dominant': 'dominant seven', 'diminished': 'diminished', 'augmented': 'augmented', 'half-diminished': 'half-diminished seven'}, |
| {'major': 'major', 'minor': 'minor', 'dominant': 'dom. 7', 'diminished': 'dim.', 'augmented': 'aug.', 'half-diminished': 'half-dim. 7'} |
| ] |
|
|
| CONCISE_PROMPTS = [ |
| "Listen to this and briefly identify the chord progression.", |
| "What chord progression is being played?", |
| "Identify the Roman numeral progression in this clip.", |
| "Can you name the broad chord progression heard here?", |
| "Briefly state the harmony played.", |
| "What is the underlying chord progression of this excerpt?", |
| "Listen to the track and provide the standard chord progression.", |
| "Name the progression and the key of this clip.", |
| "What chords are being played here? Keep it brief.", |
| "Analyze the clip and give me the primary chord progression." |
| ] |
|
|
| DETAILED_PROMPTS = [ |
| "Provide a detailed theoretical breakdown of the harmony in this clip, including specific voicings.", |
| "Analyze this in detail, listing each chord, its quality, and inversion.", |
| "What exactly is being played here? Break down the progression chord by chord with inversions.", |
| "Give me a comprehensive harmonic analysis of this clip, including bass notes and voicings.", |
| "Break down the harmony of this sequence. Be specific about the chord qualities and positions.", |
| "Listen closely and describe the exact chord voicings and inversions used in this progression.", |
| "Provide a full theoretical breakdown of the chords heard in this MIDI excerpt.", |
| "Can you detail the harmony in this clip, identifying each specific chord and its inversion?", |
| "Write out a detailed description of the chords, their qualities, and their voicings in this track.", |
| "Analyze the harmonic structure in detail, outlining the exact chords and inversions played." |
| ] |
|
|
| |
| def humanize_chord(c, base_velocity): |
| highest_note = c.sortAscending()[-1] |
| for n in c.notes: |
| if n == highest_note: |
| n.volume.velocity = min(127, base_velocity + random.randint(15, 25)) |
| else: |
| n.volume.velocity = base_velocity |
| n.offset = random.uniform(0.0, 0.05) |
| n.quarterLength = random.uniform(0.9, 1.1) |
|
|
| def get_chord_name(c, quality_map): |
| root_name = c.root().name |
| quality = c.quality |
| quality_text = quality_map.get(quality, quality) |
| |
| if c.inversion() == 0: |
| return f"{root_name} {quality_text} in root position" |
| else: |
| inversion_names = {1: "first inversion", 2: "second inversion", 3: "third inversion"} |
| inv_text = inversion_names.get(c.inversion(), "inverted") |
| bass_note = c.bass().name |
| return f"{root_name} {quality_text} over {bass_note} ({inv_text})" |
|
|
| |
| def generate_detailed_assistant_response(key_name, scale_type, chords_list, bpm): |
| chords_str = ', '.join(chords_list) |
| key_display = f"{key_name.capitalize()} {scale_type}" |
| |
| templates = [ |
| f"This progression is in {key_display}. It moves through the following chords: {chords_str}.", |
| f"Set in {key_display}, the harmony consists of: {chords_str}.", |
| f"The underlying key is {key_display}. The specific chord sequence played is {chords_str}.", |
| f"In the key of {key_display}, this clip plays the following sequence: {chords_str}.", |
| f"This clip features a {key_display} progression, specifically playing {chords_str}." |
| ] |
| |
| response = random.choice(templates) |
| |
| if random.random() < 0.05: |
| tempo_texts = [ |
| f" The progression is performed at {bpm} beats per minute, assuming each chord is held for one beat.", |
| f" It is played around {bpm} BPM, assuming each note is a quarter note, or {bpm*4} BPM if you consider each chord as a whole note." |
| ] |
| response += random.choice(tempo_texts) |
| |
| return response |
|
|
| |
| def generate_dataset(): |
| dataset_entries = [] |
| global_id = 1 |
| midi_id = 1 |
| |
| for run in range(NUM_RUNS): |
| for recipe in DATASET_RECIPES: |
| scale_type = recipe["scale_type"] |
| |
| for prog in recipe["progressions"]: |
| for k in recipe["keys"]: |
| s = stream.Score() |
| p = stream.Part() |
| |
| p.append(meter.TimeSignature('4/4')) |
| bpm = random.randint(60, 75) |
| p.append(tempo.MetronomeMark(number=bpm)) |
| |
| key_obj = key.Key(k) |
| p.append(key_obj) |
| |
| detailed_chord_descriptions = [] |
| current_quality_map = random.choice(QUALITY_MAPS) |
| |
| num_chords = len(prog) |
| raw_vols = sorted([random.randint(60, 85) for _ in range(num_chords)]) |
| phrase_vols = [0] * num_chords |
| |
| if num_chords >= 2: |
| phrase_vols[-1] = raw_vols[0] |
| phrase_vols[0] = raw_vols[1] |
| if num_chords > 2: |
| phrase_vols[1:-1] = raw_vols[2:] |
| else: |
| phrase_vols = raw_vols |
| |
| for i, numeral in enumerate(prog): |
| c = roman.RomanNumeral(numeral, key_obj) |
| humanize_chord(c, phrase_vols[i]) |
| p.append(c) |
| detailed_chord_descriptions.append(get_chord_name(c, current_quality_map)) |
| |
| s.insert(0, p) |
| |
| prog_str = "_".join(prog) |
| |
| filename = f"prog_{midi_id:04d}_run{run+1}_{k.capitalize()}{scale_type}_{prog_str}.mid".replace('#', 's').replace('/', '-') |
| filepath = os.path.join(OUTPUT_DIR, filename) |
| s.write('midi', fp=filepath) |
| |
| relative_path = f"midi/{filename}" |
| key_display = f"{k.capitalize()} {scale_type}" |
| |
| |
| concise_entry = { |
| "id": global_id, |
| "progression": prog_str, |
| "key": key_display, |
| "midi_path": relative_path, |
| "messages": [ |
| {"role": "user", "content": random.choice(CONCISE_PROMPTS)}, |
| {"role": "assistant", "content": f"This is a {' - '.join(prog)} progression in {key_display}."} |
| ] |
| } |
| dataset_entries.append(concise_entry) |
| global_id += 1 |
| |
| |
| detailed_entry = { |
| "id": global_id, |
| "progression": prog_str, |
| "key": key_display, |
| "midi_path": relative_path, |
| "messages": [ |
| {"role": "user", "content": random.choice(DETAILED_PROMPTS)}, |
| {"role": "assistant", "content": generate_detailed_assistant_response(k, scale_type, detailed_chord_descriptions, bpm)} |
| ] |
| } |
| dataset_entries.append(detailed_entry) |
| global_id += 1 |
| midi_id += 1 |
|
|
| with open(JSONL_FILE, 'w') as f: |
| for entry in dataset_entries: |
| f.write(json.dumps(entry) + '\n') |
| |
| print(f"Success! Generated {midi_id - 1} MIDI files and {len(dataset_entries)} JSONL text pairs.") |
|
|
| if __name__ == "__main__": |
| generate_dataset() |