projectlosangeles
commited on
Commit
•
83dac28
1
Parent(s):
3bd77c9
Upload 2 files
Browse files
Los_Angeles_MIDI_Dataset_Maker.ipynb
ADDED
@@ -0,0 +1,394 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {
|
6 |
+
"gradient": {
|
7 |
+
"editing": false,
|
8 |
+
"id": "ac5a4cf0-d9d2-47b5-9633-b53f8d99a4d2",
|
9 |
+
"kernelId": ""
|
10 |
+
},
|
11 |
+
"id": "SiTIpPjArIyr"
|
12 |
+
},
|
13 |
+
"source": [
|
14 |
+
"# Los Angeles MIDI Dataset Maker (ver. 1.0)\n",
|
15 |
+
"\n",
|
16 |
+
"***\n",
|
17 |
+
"\n",
|
18 |
+
"Powered by tegridy-tools: https://github.com/asigalov61/tegridy-tools\n",
|
19 |
+
"\n",
|
20 |
+
"***\n",
|
21 |
+
"\n",
|
22 |
+
"#### Project Los Angeles\n",
|
23 |
+
"\n",
|
24 |
+
"#### Tegridy Code 2023\n",
|
25 |
+
"\n",
|
26 |
+
"***"
|
27 |
+
]
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"cell_type": "markdown",
|
31 |
+
"metadata": {
|
32 |
+
"gradient": {
|
33 |
+
"editing": false,
|
34 |
+
"id": "fa0a611c-1803-42ae-bdf6-a49b5a4e781b",
|
35 |
+
"kernelId": ""
|
36 |
+
},
|
37 |
+
"id": "gOd93yV0sGd2"
|
38 |
+
},
|
39 |
+
"source": [
|
40 |
+
"# (SETUP ENVIRONMENT)"
|
41 |
+
]
|
42 |
+
},
|
43 |
+
{
|
44 |
+
"cell_type": "code",
|
45 |
+
"execution_count": null,
|
46 |
+
"metadata": {
|
47 |
+
"cellView": "form",
|
48 |
+
"gradient": {
|
49 |
+
"editing": false,
|
50 |
+
"id": "a1a45a91-d909-4fd4-b67a-5e16b971d179",
|
51 |
+
"kernelId": ""
|
52 |
+
},
|
53 |
+
"id": "fX12Yquyuihc"
|
54 |
+
},
|
55 |
+
"outputs": [],
|
56 |
+
"source": [
|
57 |
+
"#@title Install all dependencies (run only once per session)\n",
|
58 |
+
"\n",
|
59 |
+
"!git clone https://github.com/asigalov61/tegridy-tools\n",
|
60 |
+
"!pip install tqdm"
|
61 |
+
]
|
62 |
+
},
|
63 |
+
{
|
64 |
+
"cell_type": "code",
|
65 |
+
"execution_count": null,
|
66 |
+
"metadata": {
|
67 |
+
"cellView": "form",
|
68 |
+
"gradient": {
|
69 |
+
"editing": false,
|
70 |
+
"id": "b8207b76-9514-4c07-95db-95a4742e52c5",
|
71 |
+
"kernelId": ""
|
72 |
+
},
|
73 |
+
"id": "z7n9vnKmug1J"
|
74 |
+
},
|
75 |
+
"outputs": [],
|
76 |
+
"source": [
|
77 |
+
"#@title Import all needed modules\n",
|
78 |
+
"\n",
|
79 |
+
"print('Loading needed modules. Please wait...')\n",
|
80 |
+
"import os\n",
|
81 |
+
"\n",
|
82 |
+
"import math\n",
|
83 |
+
"import statistics\n",
|
84 |
+
"import random\n",
|
85 |
+
"from collections import Counter\n",
|
86 |
+
"import shutil\n",
|
87 |
+
"import difflib\n",
|
88 |
+
"from tqdm import tqdm\n",
|
89 |
+
"\n",
|
90 |
+
"if not os.path.exists('/content/Dataset'):\n",
|
91 |
+
" os.makedirs('/content/Dataset')\n",
|
92 |
+
"\n",
|
93 |
+
"if not os.path.exists('/content/Output'):\n",
|
94 |
+
" os.makedirs('/content/Output')\n",
|
95 |
+
"\n",
|
96 |
+
"print('Loading TMIDIX module...')\n",
|
97 |
+
"os.chdir('/content/tegridy-tools/tegridy-tools')\n",
|
98 |
+
"\n",
|
99 |
+
"import TMIDIX\n",
|
100 |
+
"\n",
|
101 |
+
"print('Done!')\n",
|
102 |
+
"\n",
|
103 |
+
"os.chdir('/content/')\n",
|
104 |
+
"print('Enjoy! :)')"
|
105 |
+
]
|
106 |
+
},
|
107 |
+
{
|
108 |
+
"cell_type": "markdown",
|
109 |
+
"metadata": {
|
110 |
+
"gradient": {
|
111 |
+
"editing": false,
|
112 |
+
"id": "20b8698a-0b4e-4fdb-ae49-24d063782e77",
|
113 |
+
"kernelId": ""
|
114 |
+
},
|
115 |
+
"id": "ObPxlEutsQBj"
|
116 |
+
},
|
117 |
+
"source": [
|
118 |
+
"# (DOWNLOAD SOURCE MIDI DATASET)"
|
119 |
+
]
|
120 |
+
},
|
121 |
+
{
|
122 |
+
"cell_type": "code",
|
123 |
+
"source": [
|
124 |
+
"#@title Download original LAKH MIDI Dataset\n",
|
125 |
+
"\n",
|
126 |
+
"%cd /content/Dataset/\n",
|
127 |
+
"\n",
|
128 |
+
"!wget 'http://hog.ee.columbia.edu/craffel/lmd/lmd_full.tar.gz'\n",
|
129 |
+
"!tar -xvf 'lmd_full.tar.gz'\n",
|
130 |
+
"!rm 'lmd_full.tar.gz'\n",
|
131 |
+
"\n",
|
132 |
+
"%cd /content/"
|
133 |
+
],
|
134 |
+
"metadata": {
|
135 |
+
"cellView": "form",
|
136 |
+
"id": "7aItlhq9cRxZ"
|
137 |
+
},
|
138 |
+
"execution_count": null,
|
139 |
+
"outputs": []
|
140 |
+
},
|
141 |
+
{
|
142 |
+
"cell_type": "markdown",
|
143 |
+
"metadata": {
|
144 |
+
"id": "JwrqQeie08t0"
|
145 |
+
},
|
146 |
+
"source": [
|
147 |
+
"# (FILE LIST)"
|
148 |
+
]
|
149 |
+
},
|
150 |
+
{
|
151 |
+
"cell_type": "code",
|
152 |
+
"execution_count": null,
|
153 |
+
"metadata": {
|
154 |
+
"cellView": "form",
|
155 |
+
"id": "DuVWtdDNcqKh"
|
156 |
+
},
|
157 |
+
"outputs": [],
|
158 |
+
"source": [
|
159 |
+
"#@title Save file list\n",
|
160 |
+
"###########\n",
|
161 |
+
"\n",
|
162 |
+
"print('Loading MIDI files...')\n",
|
163 |
+
"print('This may take a while on a large dataset in particular.')\n",
|
164 |
+
"\n",
|
165 |
+
"dataset_addr = \"/content/Dataset\"\n",
|
166 |
+
"# os.chdir(dataset_addr)\n",
|
167 |
+
"filez = list()\n",
|
168 |
+
"for (dirpath, dirnames, filenames) in os.walk(dataset_addr):\n",
|
169 |
+
" filez += [os.path.join(dirpath, file) for file in filenames]\n",
|
170 |
+
"print('=' * 70)\n",
|
171 |
+
"\n",
|
172 |
+
"if filez == []:\n",
|
173 |
+
" print('Could not find any MIDI files. Please check Dataset dir...')\n",
|
174 |
+
" print('=' * 70)\n",
|
175 |
+
"\n",
|
176 |
+
"print('Randomizing file list...')\n",
|
177 |
+
"random.shuffle(filez)\n",
|
178 |
+
"\n",
|
179 |
+
"TMIDIX.Tegridy_Any_Pickle_File_Writer(filez, '/content/filez')"
|
180 |
+
]
|
181 |
+
},
|
182 |
+
{
|
183 |
+
"cell_type": "code",
|
184 |
+
"execution_count": null,
|
185 |
+
"metadata": {
|
186 |
+
"cellView": "form",
|
187 |
+
"id": "qI_adhjojrJ9"
|
188 |
+
},
|
189 |
+
"outputs": [],
|
190 |
+
"source": [
|
191 |
+
"#@title Load file list\n",
|
192 |
+
"filez = TMIDIX.Tegridy_Any_Pickle_File_Reader('/content/filez')\n",
|
193 |
+
"print('Done!')"
|
194 |
+
]
|
195 |
+
},
|
196 |
+
{
|
197 |
+
"cell_type": "markdown",
|
198 |
+
"metadata": {
|
199 |
+
"id": "FLxHvO-wlwfU"
|
200 |
+
},
|
201 |
+
"source": [
|
202 |
+
"# (PROCESS)"
|
203 |
+
]
|
204 |
+
},
|
205 |
+
{
|
206 |
+
"cell_type": "code",
|
207 |
+
"execution_count": null,
|
208 |
+
"metadata": {
|
209 |
+
"id": "CeGo7CruaCJQ",
|
210 |
+
"cellView": "form"
|
211 |
+
},
|
212 |
+
"outputs": [],
|
213 |
+
"source": [
|
214 |
+
"#@title Process MIDIs with TMIDIX MIDI processor\n",
|
215 |
+
"\n",
|
216 |
+
"print('=' * 70)\n",
|
217 |
+
"print('TMIDIX MIDI Processor')\n",
|
218 |
+
"print('=' * 70)\n",
|
219 |
+
"print('Starting up...')\n",
|
220 |
+
"print('=' * 70)\n",
|
221 |
+
"\n",
|
222 |
+
"###########\n",
|
223 |
+
"\n",
|
224 |
+
"START_FILE_NUMBER = 0\n",
|
225 |
+
"LAST_SAVED_BATCH_COUNT = 0\n",
|
226 |
+
"\n",
|
227 |
+
"input_files_count = START_FILE_NUMBER\n",
|
228 |
+
"files_count = LAST_SAVED_BATCH_COUNT\n",
|
229 |
+
"\n",
|
230 |
+
"melody_chords_f = []\n",
|
231 |
+
"\n",
|
232 |
+
"pitches_sums = []\n",
|
233 |
+
"pitches_counts = []\n",
|
234 |
+
"\n",
|
235 |
+
"pitches_data = []\n",
|
236 |
+
"\n",
|
237 |
+
"stats = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
|
238 |
+
"\n",
|
239 |
+
"print('Processing MIDI files. Please wait...')\n",
|
240 |
+
"print('=' * 70)\n",
|
241 |
+
"\n",
|
242 |
+
"for f in tqdm(filez[START_FILE_NUMBER:]):\n",
|
243 |
+
" try:\n",
|
244 |
+
" input_files_count += 1\n",
|
245 |
+
"\n",
|
246 |
+
" fn = os.path.basename(f)\n",
|
247 |
+
" fn1 = fn.split('.mid')[0]\n",
|
248 |
+
"\n",
|
249 |
+
" #=======================================================\n",
|
250 |
+
" # START PROCESSING\n",
|
251 |
+
"\n",
|
252 |
+
" # Convering MIDI to ms score with MIDI.py module\n",
|
253 |
+
" score = TMIDIX.midi2ms_score(open(f, 'rb').read())\n",
|
254 |
+
"\n",
|
255 |
+
" events_matrix = []\n",
|
256 |
+
" itrack = 1\n",
|
257 |
+
" \n",
|
258 |
+
" while itrack < len(score):\n",
|
259 |
+
" for event in score[itrack]: \n",
|
260 |
+
" if event[0] == 'note':\n",
|
261 |
+
" if event[3] == 9:\n",
|
262 |
+
" event[4] = (event[4] % 128)+128\n",
|
263 |
+
" else:\n",
|
264 |
+
" event[4] = (event[4] % 128)\n",
|
265 |
+
" \n",
|
266 |
+
" events_matrix.append(event)\n",
|
267 |
+
" itrack += 1\n",
|
268 |
+
" \n",
|
269 |
+
" if len(events_matrix) >= 256:\n",
|
270 |
+
"\n",
|
271 |
+
" events_matrix.sort(key=lambda x: x[1])\n",
|
272 |
+
"\n",
|
273 |
+
" times = [e[1] for e in events_matrix]\n",
|
274 |
+
" durs = [e[2] for e in events_matrix]\n",
|
275 |
+
"\n",
|
276 |
+
" if min(times) >= 0 and min(durs) >= 0:\n",
|
277 |
+
" if len([k for k,v in Counter(times).items() if v>1]) != 0:\n",
|
278 |
+
" \n",
|
279 |
+
" pitches = [e[4] for e in events_matrix]\n",
|
280 |
+
" pitches_sum = sum(pitches)\n",
|
281 |
+
" pitches_number = len(pitches)\n",
|
282 |
+
"\n",
|
283 |
+
" if pitches_sum not in pitches_sums:\n",
|
284 |
+
" pitches_count = sorted([[key, val] for key,val in Counter(pitches).most_common()], reverse=True)\n",
|
285 |
+
" #=======================================================\n",
|
286 |
+
" \n",
|
287 |
+
" if [y[1] for y in pitches_count] not in pitches_counts:\n",
|
288 |
+
"\n",
|
289 |
+
" # Saving every 5000 processed files\n",
|
290 |
+
" if files_count % 50000 == 0:\n",
|
291 |
+
" dir_count = ((files_count // 50000)+1) * 50000\n",
|
292 |
+
" dir_count_str = str(dir_count).zfill(7)\n",
|
293 |
+
" copy_path = '/content/Output/'+dir_count_str\n",
|
294 |
+
" if not os.path.exists(copy_path):\n",
|
295 |
+
" os.mkdir(copy_path)\n",
|
296 |
+
" print('SAVING !!!')\n",
|
297 |
+
" print('=' * 70)\n",
|
298 |
+
" print('Saving processed files...')\n",
|
299 |
+
" print('=' * 70)\n",
|
300 |
+
" print('Processed so far:', files_count, 'out of', input_files_count, '===', files_count / input_files_count, 'good files ratio')\n",
|
301 |
+
" print('=' * 70)\n",
|
302 |
+
"\n",
|
303 |
+
" if files_count % 5000 == 0:\n",
|
304 |
+
" print('=' * 70)\n",
|
305 |
+
" print('Processed so far:', files_count, 'out of', input_files_count, '===', files_count / input_files_count, 'good files ratio')\n",
|
306 |
+
" print('=' * 70)\n",
|
307 |
+
" TMIDIX.Tegridy_Any_Pickle_File_Writer(pitches_data, '/content/Output/pitches_data')\n",
|
308 |
+
"\n",
|
309 |
+
" shutil.copy2(f, copy_path+'/'+fn)\n",
|
310 |
+
"\n",
|
311 |
+
" # Processed files counter\n",
|
312 |
+
" files_count += 1\n",
|
313 |
+
" \n",
|
314 |
+
" data = []\n",
|
315 |
+
" \n",
|
316 |
+
" data = [fn.split('.mid')[0], times[-1], len(set(times))]\n",
|
317 |
+
" for p in pitches_count:\n",
|
318 |
+
" data.extend(p)\n",
|
319 |
+
" \n",
|
320 |
+
" pitches_data.append(data)\n",
|
321 |
+
" \n",
|
322 |
+
" pitches_sums.append(pitches_sum)\n",
|
323 |
+
" pitches_counts.append([y[1] for y in pitches_count])\n",
|
324 |
+
"\n",
|
325 |
+
"\n",
|
326 |
+
" except KeyboardInterrupt:\n",
|
327 |
+
" print('Saving current progress and quitting...')\n",
|
328 |
+
" break \n",
|
329 |
+
"\n",
|
330 |
+
" except Exception as ex:\n",
|
331 |
+
" print('WARNING !!!')\n",
|
332 |
+
" print('=' * 70)\n",
|
333 |
+
" print('Bad MIDI:', f)\n",
|
334 |
+
" print('Error detected:', ex)\n",
|
335 |
+
" print('=' * 70)\n",
|
336 |
+
" continue\n",
|
337 |
+
"\n",
|
338 |
+
"# Saving last processed files...\n",
|
339 |
+
"print('=' * 70)\n",
|
340 |
+
"print('Saving processed files...')\n",
|
341 |
+
"print('=' * 70)\n",
|
342 |
+
"print('Processed so far:', files_count, 'out of', input_files_count, '===', files_count / input_files_count, 'good files ratio')\n",
|
343 |
+
"print('=' * 70)\n",
|
344 |
+
"TMIDIX.Tegridy_Any_Pickle_File_Writer(pitches_data, '/content/Output/pitches_data')\n",
|
345 |
+
"\n",
|
346 |
+
"# Displaying resulting processing stats...\n",
|
347 |
+
"print('=' * 70)\n",
|
348 |
+
"print('Done!') \n",
|
349 |
+
"print('=' * 70)\n",
|
350 |
+
"\n",
|
351 |
+
"print('Resulting Stats:')\n",
|
352 |
+
"print('=' * 70)\n",
|
353 |
+
"print('Total good processed MIDI files:', files_count)\n",
|
354 |
+
"print('=' * 70)"
|
355 |
+
]
|
356 |
+
},
|
357 |
+
{
|
358 |
+
"cell_type": "markdown",
|
359 |
+
"metadata": {
|
360 |
+
"id": "YzCMd94Tu_gz"
|
361 |
+
},
|
362 |
+
"source": [
|
363 |
+
"# Congrats! You did it! :)"
|
364 |
+
]
|
365 |
+
}
|
366 |
+
],
|
367 |
+
"metadata": {
|
368 |
+
"colab": {
|
369 |
+
"machine_shape": "hm",
|
370 |
+
"private_outputs": true,
|
371 |
+
"provenance": []
|
372 |
+
},
|
373 |
+
"gpuClass": "standard",
|
374 |
+
"kernelspec": {
|
375 |
+
"display_name": "Python 3 (ipykernel)",
|
376 |
+
"language": "python",
|
377 |
+
"name": "python3"
|
378 |
+
},
|
379 |
+
"language_info": {
|
380 |
+
"codemirror_mode": {
|
381 |
+
"name": "ipython",
|
382 |
+
"version": 3
|
383 |
+
},
|
384 |
+
"file_extension": ".py",
|
385 |
+
"mimetype": "text/x-python",
|
386 |
+
"name": "python",
|
387 |
+
"nbconvert_exporter": "python",
|
388 |
+
"pygments_lexer": "ipython3",
|
389 |
+
"version": "3.9.7"
|
390 |
+
}
|
391 |
+
},
|
392 |
+
"nbformat": 4,
|
393 |
+
"nbformat_minor": 0
|
394 |
+
}
|
los_angeles_midi_dataset_maker.py
ADDED
@@ -0,0 +1,245 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Los_Angeles_MIDI_Dataset_Maker.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1OF5Ag1GHu-KNPzeI_2V-A5P2AF7F8Kue
|
8 |
+
|
9 |
+
# Los Angeles MIDI Dataset Maker (ver. 1.0)
|
10 |
+
|
11 |
+
***
|
12 |
+
|
13 |
+
Powered by tegridy-tools: https://github.com/asigalov61/tegridy-tools
|
14 |
+
|
15 |
+
***
|
16 |
+
|
17 |
+
#### Project Los Angeles
|
18 |
+
|
19 |
+
#### Tegridy Code 2023
|
20 |
+
|
21 |
+
***
|
22 |
+
|
23 |
+
# (SETUP ENVIRONMENT)
|
24 |
+
"""
|
25 |
+
|
26 |
+
#@title Install all dependencies (run only once per session)
|
27 |
+
|
28 |
+
!git clone https://github.com/asigalov61/tegridy-tools
|
29 |
+
!pip install tqdm
|
30 |
+
|
31 |
+
#@title Import all needed modules
|
32 |
+
|
33 |
+
print('Loading needed modules. Please wait...')
|
34 |
+
import os
|
35 |
+
|
36 |
+
import math
|
37 |
+
import statistics
|
38 |
+
import random
|
39 |
+
from collections import Counter
|
40 |
+
import shutil
|
41 |
+
import difflib
|
42 |
+
from tqdm import tqdm
|
43 |
+
|
44 |
+
if not os.path.exists('/content/Dataset'):
|
45 |
+
os.makedirs('/content/Dataset')
|
46 |
+
|
47 |
+
if not os.path.exists('/content/Output'):
|
48 |
+
os.makedirs('/content/Output')
|
49 |
+
|
50 |
+
print('Loading TMIDIX module...')
|
51 |
+
os.chdir('/content/tegridy-tools/tegridy-tools')
|
52 |
+
|
53 |
+
import TMIDIX
|
54 |
+
|
55 |
+
print('Done!')
|
56 |
+
|
57 |
+
os.chdir('/content/')
|
58 |
+
print('Enjoy! :)')
|
59 |
+
|
60 |
+
"""# (DOWNLOAD SOURCE MIDI DATASET)"""
|
61 |
+
|
62 |
+
# Commented out IPython magic to ensure Python compatibility.
|
63 |
+
#@title Download original LAKH MIDI Dataset
|
64 |
+
|
65 |
+
# %cd /content/Dataset/
|
66 |
+
|
67 |
+
!wget 'http://hog.ee.columbia.edu/craffel/lmd/lmd_full.tar.gz'
|
68 |
+
!tar -xvf 'lmd_full.tar.gz'
|
69 |
+
!rm 'lmd_full.tar.gz'
|
70 |
+
|
71 |
+
# %cd /content/
|
72 |
+
|
73 |
+
"""# (FILE LIST)"""
|
74 |
+
|
75 |
+
#@title Save file list
|
76 |
+
###########
|
77 |
+
|
78 |
+
print('Loading MIDI files...')
|
79 |
+
print('This may take a while on a large dataset in particular.')
|
80 |
+
|
81 |
+
dataset_addr = "/content/Dataset"
|
82 |
+
# os.chdir(dataset_addr)
|
83 |
+
filez = list()
|
84 |
+
for (dirpath, dirnames, filenames) in os.walk(dataset_addr):
|
85 |
+
filez += [os.path.join(dirpath, file) for file in filenames]
|
86 |
+
print('=' * 70)
|
87 |
+
|
88 |
+
if filez == []:
|
89 |
+
print('Could not find any MIDI files. Please check Dataset dir...')
|
90 |
+
print('=' * 70)
|
91 |
+
|
92 |
+
print('Randomizing file list...')
|
93 |
+
random.shuffle(filez)
|
94 |
+
|
95 |
+
TMIDIX.Tegridy_Any_Pickle_File_Writer(filez, '/content/filez')
|
96 |
+
|
97 |
+
#@title Load file list
|
98 |
+
filez = TMIDIX.Tegridy_Any_Pickle_File_Reader('/content/filez')
|
99 |
+
print('Done!')
|
100 |
+
|
101 |
+
"""# (PROCESS)"""
|
102 |
+
|
103 |
+
#@title Process MIDIs with TMIDIX MIDI processor
|
104 |
+
|
105 |
+
print('=' * 70)
|
106 |
+
print('TMIDIX MIDI Processor')
|
107 |
+
print('=' * 70)
|
108 |
+
print('Starting up...')
|
109 |
+
print('=' * 70)
|
110 |
+
|
111 |
+
###########
|
112 |
+
|
113 |
+
START_FILE_NUMBER = 0
|
114 |
+
LAST_SAVED_BATCH_COUNT = 0
|
115 |
+
|
116 |
+
input_files_count = START_FILE_NUMBER
|
117 |
+
files_count = LAST_SAVED_BATCH_COUNT
|
118 |
+
|
119 |
+
melody_chords_f = []
|
120 |
+
|
121 |
+
pitches_sums = []
|
122 |
+
pitches_counts = []
|
123 |
+
|
124 |
+
pitches_data = []
|
125 |
+
|
126 |
+
stats = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
127 |
+
|
128 |
+
print('Processing MIDI files. Please wait...')
|
129 |
+
print('=' * 70)
|
130 |
+
|
131 |
+
for f in tqdm(filez[START_FILE_NUMBER:]):
|
132 |
+
try:
|
133 |
+
input_files_count += 1
|
134 |
+
|
135 |
+
fn = os.path.basename(f)
|
136 |
+
fn1 = fn.split('.mid')[0]
|
137 |
+
|
138 |
+
#=======================================================
|
139 |
+
# START PROCESSING
|
140 |
+
|
141 |
+
# Convering MIDI to ms score with MIDI.py module
|
142 |
+
score = TMIDIX.midi2ms_score(open(f, 'rb').read())
|
143 |
+
|
144 |
+
events_matrix = []
|
145 |
+
itrack = 1
|
146 |
+
|
147 |
+
while itrack < len(score):
|
148 |
+
for event in score[itrack]:
|
149 |
+
if event[0] == 'note':
|
150 |
+
if event[3] == 9:
|
151 |
+
event[4] = (event[4] % 128)+128
|
152 |
+
else:
|
153 |
+
event[4] = (event[4] % 128)
|
154 |
+
|
155 |
+
events_matrix.append(event)
|
156 |
+
itrack += 1
|
157 |
+
|
158 |
+
if len(events_matrix) >= 256:
|
159 |
+
|
160 |
+
events_matrix.sort(key=lambda x: x[1])
|
161 |
+
|
162 |
+
times = [e[1] for e in events_matrix]
|
163 |
+
durs = [e[2] for e in events_matrix]
|
164 |
+
|
165 |
+
if min(times) >= 0 and min(durs) >= 0:
|
166 |
+
if len([k for k,v in Counter(times).items() if v>1]) != 0:
|
167 |
+
|
168 |
+
pitches = [e[4] for e in events_matrix]
|
169 |
+
pitches_sum = sum(pitches)
|
170 |
+
pitches_number = len(pitches)
|
171 |
+
|
172 |
+
if pitches_sum not in pitches_sums:
|
173 |
+
pitches_count = sorted([[key, val] for key,val in Counter(pitches).most_common()], reverse=True)
|
174 |
+
#=======================================================
|
175 |
+
|
176 |
+
if [y[1] for y in pitches_count] not in pitches_counts:
|
177 |
+
|
178 |
+
# Saving every 5000 processed files
|
179 |
+
if files_count % 50000 == 0:
|
180 |
+
dir_count = ((files_count // 50000)+1) * 50000
|
181 |
+
dir_count_str = str(dir_count).zfill(7)
|
182 |
+
copy_path = '/content/Output/'+dir_count_str
|
183 |
+
if not os.path.exists(copy_path):
|
184 |
+
os.mkdir(copy_path)
|
185 |
+
print('SAVING !!!')
|
186 |
+
print('=' * 70)
|
187 |
+
print('Saving processed files...')
|
188 |
+
print('=' * 70)
|
189 |
+
print('Processed so far:', files_count, 'out of', input_files_count, '===', files_count / input_files_count, 'good files ratio')
|
190 |
+
print('=' * 70)
|
191 |
+
|
192 |
+
if files_count % 5000 == 0:
|
193 |
+
print('=' * 70)
|
194 |
+
print('Processed so far:', files_count, 'out of', input_files_count, '===', files_count / input_files_count, 'good files ratio')
|
195 |
+
print('=' * 70)
|
196 |
+
TMIDIX.Tegridy_Any_Pickle_File_Writer(pitches_data, '/content/Output/pitches_data')
|
197 |
+
|
198 |
+
shutil.copy2(f, copy_path+'/'+fn)
|
199 |
+
|
200 |
+
# Processed files counter
|
201 |
+
files_count += 1
|
202 |
+
|
203 |
+
data = []
|
204 |
+
|
205 |
+
data = [fn.split('.mid')[0], times[-1], len(set(times))]
|
206 |
+
for p in pitches_count:
|
207 |
+
data.extend(p)
|
208 |
+
|
209 |
+
pitches_data.append(data)
|
210 |
+
|
211 |
+
pitches_sums.append(pitches_sum)
|
212 |
+
pitches_counts.append([y[1] for y in pitches_count])
|
213 |
+
|
214 |
+
|
215 |
+
except KeyboardInterrupt:
|
216 |
+
print('Saving current progress and quitting...')
|
217 |
+
break
|
218 |
+
|
219 |
+
except Exception as ex:
|
220 |
+
print('WARNING !!!')
|
221 |
+
print('=' * 70)
|
222 |
+
print('Bad MIDI:', f)
|
223 |
+
print('Error detected:', ex)
|
224 |
+
print('=' * 70)
|
225 |
+
continue
|
226 |
+
|
227 |
+
# Saving last processed files...
|
228 |
+
print('=' * 70)
|
229 |
+
print('Saving processed files...')
|
230 |
+
print('=' * 70)
|
231 |
+
print('Processed so far:', files_count, 'out of', input_files_count, '===', files_count / input_files_count, 'good files ratio')
|
232 |
+
print('=' * 70)
|
233 |
+
TMIDIX.Tegridy_Any_Pickle_File_Writer(pitches_data, '/content/Output/pitches_data')
|
234 |
+
|
235 |
+
# Displaying resulting processing stats...
|
236 |
+
print('=' * 70)
|
237 |
+
print('Done!')
|
238 |
+
print('=' * 70)
|
239 |
+
|
240 |
+
print('Resulting Stats:')
|
241 |
+
print('=' * 70)
|
242 |
+
print('Total good processed MIDI files:', files_count)
|
243 |
+
print('=' * 70)
|
244 |
+
|
245 |
+
"""# Congrats! You did it! :)"""
|