EliMasonTech commited on
Commit
1e496aa
·
verified ·
1 Parent(s): c8d18bc

Upload stuff/generate_dataset.py

Browse files
Files changed (1) hide show
  1. stuff/generate_dataset.py +245 -0
stuff/generate_dataset.py ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import random
4
+ from music21 import stream, roman, note, key, meter, tempo
5
+
6
+ # --- Configuration ---
7
+ OUTPUT_DIR = "midi"
8
+ JSONL_FILE = "dataset.jsonl"
9
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
10
+
11
+ NUM_RUNS = 2
12
+
13
+ # -- Major Data --
14
+ MAJOR_KEYS = ['C', 'G', 'D', 'A', 'E', 'B', 'F#', 'C#', 'F', 'Bb', 'Eb', 'Ab', 'Db', 'Gb', 'Cb']
15
+ MAJOR_PROGRESSIONS = [
16
+ # (Your previous 34 Major Progressions go here)
17
+ ['I', 'V', 'vi', 'IV'],
18
+ ['I', 'V', 'vi', 'iii', 'IV'],
19
+ ['vi', 'V', 'IV', 'V'],
20
+ ['I', 'vi', 'IV', 'V'],
21
+ ['I', 'IV', 'vi', 'V'],
22
+ ['I', 'V', 'IV', 'V'],
23
+ ['IV', 'ii', 'I64', 'V', 'I'],
24
+ ['I', 'V6', 'vi', 'V'],
25
+ ['I', 'ii7', 'I6', 'IV'],
26
+ ['I', 'iii6', 'vi', 'IV'],
27
+ ['IV', 'I6', 'V'],
28
+ ['IV', 'I6', 'ii'],
29
+ ['IV', 'V', 'V6/vi', 'vi'],
30
+ ['I', 'V/vi', 'vi'],
31
+ ['vi', 'vi42', 'IV'],
32
+ ['I', 'V7/IV', 'IV'],
33
+ ['V', 'viio/vi', 'vi'],
34
+ ['IV', 'iv', 'I'],
35
+ ['I', 'bVII', 'IV', 'I'],
36
+ ['I', 'bVI', 'V'],
37
+ ['I', 'V', 'IV', 'bVII', 'I'],
38
+ ['I', 'V', 'vi', 'iii', 'IV', 'I', 'IV', 'V'],
39
+ ['I', 'IV', 'viio', 'iii', 'vi', 'ii', 'V', 'I'],
40
+ ['I', 'Imaj7', 'I7', 'IV', 'iv', 'I', 'V7', 'I'],
41
+ ['I', 'vi', 'ii7', 'V7', 'iii7', 'V7/ii', 'ii7', 'V7', 'Imaj7'],
42
+ ['vi', 'V6', 'I', 'IV', 'I6', 'ii7', 'I64', 'V7', 'I'],
43
+ ['vi', 'V', 'IV', 'III'],
44
+ ['ii7', 'bII7', 'Imaj7'],
45
+ ['Imaj7', 'bIIImaj7', 'bVImaj7', 'bIImaj7', 'Imaj7'],
46
+ ['I', 'bIII', 'IV', 'iv', 'I'],
47
+ ['I', 'II', 'IV', 'I'],
48
+ ['IV', 'iv', 'bVI', 'bVII', 'I'],
49
+ ['Imaj7', 'vi7', 'ii7', 'V7'],
50
+ ['vi', 'ii', 'v', 'I']
51
+ ]
52
+
53
+ # -- Minor Data --
54
+ # In music21, lowercase strings strictly define minor keys
55
+ MINOR_KEYS = ['c', 'g', 'd', 'a', 'e', 'b', 'f#', 'c#', 'f', 'bb', 'eb', 'ab', 'db', 'gb']
56
+ MINOR_PROGRESSIONS = [
57
+ ['i', 'iv', 'V'], # Minor standard
58
+ ['i', 'VI', 'III', 'VII'], # Modern pop minor
59
+ ['i', 'v', 'VI', 'VII'], # Natural minor climb
60
+ ['i', 'iiø7', 'V7', 'i'], # Classic minor ii-V-i (half-diminished)
61
+ ['i', 'bVII', 'bVI', 'V7'], # Andalusian Cadence (correct minor numerals)
62
+ ['i', 'iv6', 'V7', 'i'], # Minor with inverted subdominant
63
+ ['i', 'VI', 'iv', 'V7', 'i'], # Extended minor cadence
64
+ ['i', 'VII', 'v', 'VI'] # Descending minor sequence
65
+ ]
66
+
67
+ # Combine into a "Recipe" list so the loop can handle both easily
68
+ DATASET_RECIPES = [
69
+ {"scale_type": "Major", "keys": MAJOR_KEYS, "progressions": MAJOR_PROGRESSIONS},
70
+ {"scale_type": "Minor", "keys": MINOR_KEYS, "progressions": MINOR_PROGRESSIONS}
71
+ ]
72
+
73
+ # (Data Augmentation Sets like QUALITY_MAPS, CONCISE_PROMPTS, DETAILED_PROMPTS stay here)
74
+ QUALITY_MAPS = [
75
+ {'major': 'major', 'minor': 'minor', 'dominant': 'dominant 7', 'diminished': 'diminished', 'augmented': 'augmented', 'half-diminished': 'half-diminished 7'},
76
+ {'major': 'maj', 'minor': 'min', 'dominant': 'dom 7', 'diminished': 'dim', 'augmented': 'aug', 'half-diminished': 'half-dim 7'},
77
+ {'major': 'major', 'minor': 'minor', 'dominant': 'dominant seven', 'diminished': 'diminished', 'augmented': 'augmented', 'half-diminished': 'half-diminished seven'},
78
+ {'major': 'major', 'minor': 'minor', 'dominant': 'dom. 7', 'diminished': 'dim.', 'augmented': 'aug.', 'half-diminished': 'half-dim. 7'}
79
+ ]
80
+
81
+ CONCISE_PROMPTS = [
82
+ "Listen to this and briefly identify the chord progression.",
83
+ "What chord progression is being played?",
84
+ "Identify the Roman numeral progression in this clip.",
85
+ "Can you name the broad chord progression heard here?",
86
+ "Briefly state the harmony played.",
87
+ "What is the underlying chord progression of this excerpt?",
88
+ "Listen to the track and provide the standard chord progression.",
89
+ "Name the progression and the key of this clip.",
90
+ "What chords are being played here? Keep it brief.",
91
+ "Analyze the clip and give me the primary chord progression."
92
+ ]
93
+
94
+ DETAILED_PROMPTS = [
95
+ "Provide a detailed theoretical breakdown of the harmony in this clip, including specific voicings.",
96
+ "Analyze this in detail, listing each chord, its quality, and inversion.",
97
+ "What exactly is being played here? Break down the progression chord by chord with inversions.",
98
+ "Give me a comprehensive harmonic analysis of this clip, including bass notes and voicings.",
99
+ "Break down the harmony of this sequence. Be specific about the chord qualities and positions.",
100
+ "Listen closely and describe the exact chord voicings and inversions used in this progression.",
101
+ "Provide a full theoretical breakdown of the chords heard in this MIDI excerpt.",
102
+ "Can you detail the harmony in this clip, identifying each specific chord and its inversion?",
103
+ "Write out a detailed description of the chords, their qualities, and their voicings in this track.",
104
+ "Analyze the harmonic structure in detail, outlining the exact chords and inversions played."
105
+ ]
106
+
107
+ # --- Humanization Engine ---
108
+ def humanize_chord(c, base_velocity):
109
+ highest_note = c.sortAscending()[-1]
110
+ for n in c.notes:
111
+ if n == highest_note:
112
+ n.volume.velocity = min(127, base_velocity + random.randint(15, 25))
113
+ else:
114
+ n.volume.velocity = base_velocity
115
+ n.offset = random.uniform(0.0, 0.05)
116
+ n.quarterLength = random.uniform(0.9, 1.1)
117
+
118
+ def get_chord_name(c, quality_map):
119
+ root_name = c.root().name
120
+ quality = c.quality
121
+ quality_text = quality_map.get(quality, quality)
122
+
123
+ if c.inversion() == 0:
124
+ return f"{root_name} {quality_text} in root position"
125
+ else:
126
+ inversion_names = {1: "first inversion", 2: "second inversion", 3: "third inversion"}
127
+ inv_text = inversion_names.get(c.inversion(), "inverted")
128
+ bass_note = c.bass().name
129
+ return f"{root_name} {quality_text} over {bass_note} ({inv_text})"
130
+
131
+ # --- Text Generation Helpers ---
132
+ def generate_detailed_assistant_response(key_name, scale_type, chords_list, bpm):
133
+ chords_str = ', '.join(chords_list)
134
+ key_display = f"{key_name.capitalize()} {scale_type}" # Formats 'c' to 'C Minor'
135
+
136
+ templates = [
137
+ f"This progression is in {key_display}. It moves through the following chords: {chords_str}.",
138
+ f"Set in {key_display}, the harmony consists of: {chords_str}.",
139
+ f"The underlying key is {key_display}. The specific chord sequence played is {chords_str}.",
140
+ f"In the key of {key_display}, this clip plays the following sequence: {chords_str}.",
141
+ f"This clip features a {key_display} progression, specifically playing {chords_str}."
142
+ ]
143
+
144
+ response = random.choice(templates)
145
+
146
+ if random.random() < 0.05:
147
+ tempo_texts = [
148
+ f" The progression is performed at {bpm} beats per minute, assuming each chord is held for one beat.",
149
+ 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."
150
+ ]
151
+ response += random.choice(tempo_texts)
152
+
153
+ return response
154
+
155
+ # --- Generation Loop ---
156
+ def generate_dataset():
157
+ dataset_entries = []
158
+ global_id = 1
159
+ midi_id = 1
160
+
161
+ for run in range(NUM_RUNS):
162
+ for recipe in DATASET_RECIPES:
163
+ scale_type = recipe["scale_type"]
164
+
165
+ for prog in recipe["progressions"]:
166
+ for k in recipe["keys"]:
167
+ s = stream.Score()
168
+ p = stream.Part()
169
+
170
+ p.append(meter.TimeSignature('4/4'))
171
+ bpm = random.randint(60, 75)
172
+ p.append(tempo.MetronomeMark(number=bpm))
173
+
174
+ key_obj = key.Key(k)
175
+ p.append(key_obj)
176
+
177
+ detailed_chord_descriptions = []
178
+ current_quality_map = random.choice(QUALITY_MAPS)
179
+
180
+ num_chords = len(prog)
181
+ raw_vols = sorted([random.randint(60, 85) for _ in range(num_chords)])
182
+ phrase_vols = [0] * num_chords
183
+
184
+ if num_chords >= 2:
185
+ phrase_vols[-1] = raw_vols[0]
186
+ phrase_vols[0] = raw_vols[1]
187
+ if num_chords > 2:
188
+ phrase_vols[1:-1] = raw_vols[2:]
189
+ else:
190
+ phrase_vols = raw_vols
191
+
192
+ for i, numeral in enumerate(prog):
193
+ c = roman.RomanNumeral(numeral, key_obj)
194
+ humanize_chord(c, phrase_vols[i])
195
+ p.append(c)
196
+ detailed_chord_descriptions.append(get_chord_name(c, current_quality_map))
197
+
198
+ s.insert(0, p)
199
+
200
+ prog_str = "_".join(prog)
201
+ # Replace forward slashes with dashes for the filename to prevent folder errors
202
+ filename = f"prog_{midi_id:04d}_run{run+1}_{k.capitalize()}{scale_type}_{prog_str}.mid".replace('#', 's').replace('/', '-')
203
+ filepath = os.path.join(OUTPUT_DIR, filename)
204
+ s.write('midi', fp=filepath)
205
+
206
+ relative_path = f"midi/{filename}"
207
+ key_display = f"{k.capitalize()} {scale_type}"
208
+
209
+ # Concise Version
210
+ concise_entry = {
211
+ "id": global_id,
212
+ "progression": prog_str,
213
+ "key": key_display,
214
+ "midi_path": relative_path,
215
+ "messages": [
216
+ {"role": "user", "content": random.choice(CONCISE_PROMPTS)},
217
+ {"role": "assistant", "content": f"This is a {' - '.join(prog)} progression in {key_display}."}
218
+ ]
219
+ }
220
+ dataset_entries.append(concise_entry)
221
+ global_id += 1
222
+
223
+ # Detailed Version
224
+ detailed_entry = {
225
+ "id": global_id,
226
+ "progression": prog_str,
227
+ "key": key_display,
228
+ "midi_path": relative_path,
229
+ "messages": [
230
+ {"role": "user", "content": random.choice(DETAILED_PROMPTS)},
231
+ {"role": "assistant", "content": generate_detailed_assistant_response(k, scale_type, detailed_chord_descriptions, bpm)}
232
+ ]
233
+ }
234
+ dataset_entries.append(detailed_entry)
235
+ global_id += 1
236
+ midi_id += 1
237
+
238
+ with open(JSONL_FILE, 'w') as f:
239
+ for entry in dataset_entries:
240
+ f.write(json.dumps(entry) + '\n')
241
+
242
+ print(f"Success! Generated {midi_id - 1} MIDI files and {len(dataset_entries)} JSONL text pairs.")
243
+
244
+ if __name__ == "__main__":
245
+ generate_dataset()