projectlosangeles
commited on
Commit
•
62b050c
1
Parent(s):
80ecbdb
Upload 2 files
Browse files
Los_Angeles_MIDI_Dataset_Search_and_Explore.ipynb
CHANGED
@@ -666,6 +666,49 @@
|
|
666 |
"\n",
|
667 |
"patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))\n",
|
668 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
669 |
"print('=' * 70)\n",
|
670 |
"print('Done!')\n",
|
671 |
"print('=' * 70)\n",
|
@@ -918,7 +961,11 @@
|
|
918 |
" except KeyboardInterrupt:\n",
|
919 |
" break\n",
|
920 |
" \n",
|
921 |
-
" except:\n",
|
|
|
|
|
|
|
|
|
922 |
" continue\n",
|
923 |
"\n",
|
924 |
"print('Done!')\n",
|
@@ -1042,7 +1089,7 @@
|
|
1042 |
" try:\n",
|
1043 |
" p_counts = d[1][10][1]\n",
|
1044 |
" p_counts.sort(reverse = True, key = lambda x: x[1])\n",
|
1045 |
-
" max_p_count = p_counts[
|
1046 |
" trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)] \n",
|
1047 |
" \n",
|
1048 |
" if search_transposed_pitches:\n",
|
@@ -1055,7 +1102,7 @@
|
|
1055 |
" for m in search_pitches:\n",
|
1056 |
"\n",
|
1057 |
" m.sort(reverse = True, key = lambda x: x[1])\n",
|
1058 |
-
" max_pitches_count = m[
|
1059 |
" trimmed_pitches_counts = [y for y in m if y[1] >= (max_pitches_count * pitches_counts_cutoff_threshold_ratio)] \n",
|
1060 |
"\n",
|
1061 |
" num_same_pitches = len(set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts]))\n",
|
@@ -1075,7 +1122,12 @@
|
|
1075 |
" except KeyboardInterrupt:\n",
|
1076 |
" break\n",
|
1077 |
" \n",
|
1078 |
-
" except:\n",
|
|
|
|
|
|
|
|
|
|
|
1079 |
" break\n",
|
1080 |
"\n",
|
1081 |
"max_ratio = max(ratios)\n",
|
@@ -1155,6 +1207,141 @@
|
|
1155 |
"execution_count": null,
|
1156 |
"outputs": []
|
1157 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1158 |
{
|
1159 |
"cell_type": "code",
|
1160 |
"source": [
|
@@ -1198,8 +1385,13 @@
|
|
1198 |
" except KeyboardInterrupt:\n",
|
1199 |
" break\n",
|
1200 |
" \n",
|
1201 |
-
" except:\n",
|
1202 |
-
"
|
|
|
|
|
|
|
|
|
|
|
1203 |
"\n",
|
1204 |
"max_ratio = max(ratios)\n",
|
1205 |
"max_ratio_index = ratios.index(max(ratios))\n",
|
@@ -1328,10 +1520,12 @@
|
|
1328 |
" print('=' * 70)\n",
|
1329 |
" break\n",
|
1330 |
" \n",
|
1331 |
-
" except:\n",
|
1332 |
-
" print('
|
1333 |
" print('=' * 70)\n",
|
1334 |
-
"
|
|
|
|
|
1335 |
"\n",
|
1336 |
" if d[0] != md5_hash_MIDI_file_name:\n",
|
1337 |
" print('Not found!')\n",
|
|
|
666 |
"\n",
|
667 |
"patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))\n",
|
668 |
"\n",
|
669 |
+
"\n",
|
670 |
+
"#==================================================\n",
|
671 |
+
"\n",
|
672 |
+
"ms_score = MIDI.midi2ms_score(open(f, 'rb').read())\n",
|
673 |
+
"\n",
|
674 |
+
"ms_events_matrix = []\n",
|
675 |
+
"\n",
|
676 |
+
"itrack1 = 1\n",
|
677 |
+
"\n",
|
678 |
+
"while itrack1 < len(ms_score):\n",
|
679 |
+
" for event in ms_score[itrack1]: \n",
|
680 |
+
" if event[0] == 'note':\n",
|
681 |
+
" ms_events_matrix.append(event)\n",
|
682 |
+
" itrack1 += 1\n",
|
683 |
+
"\n",
|
684 |
+
"ms_events_matrix.sort(key=lambda x: x[1])\n",
|
685 |
+
"\n",
|
686 |
+
"\n",
|
687 |
+
"chords = []\n",
|
688 |
+
"pe = ms_events_matrix[0]\n",
|
689 |
+
"cho = []\n",
|
690 |
+
"for e in ms_events_matrix:\n",
|
691 |
+
" if (e[1] - pe[1]) == 0:\n",
|
692 |
+
" if e[3] != 9:\n",
|
693 |
+
" if (e[4] % 12) not in cho:\n",
|
694 |
+
" cho.append(e[4] % 12)\n",
|
695 |
+
" else:\n",
|
696 |
+
" if len(cho) > 0:\n",
|
697 |
+
" chords.append(sorted(cho))\n",
|
698 |
+
" cho = []\n",
|
699 |
+
" if e[3] != 9:\n",
|
700 |
+
" if (e[4] % 12) not in cho:\n",
|
701 |
+
" cho.append(e[4] % 12)\n",
|
702 |
+
"\n",
|
703 |
+
" pe = e\n",
|
704 |
+
" \n",
|
705 |
+
"if len(cho) > 0:\n",
|
706 |
+
" chords.append(sorted(cho))\n",
|
707 |
+
"\n",
|
708 |
+
"ms_chords_counts = sorted([[list(key), val] for key,val in Counter([tuple(c) for c in chords if len(c) > 1]).most_common()], reverse=True, key = lambda x: x[1])\n",
|
709 |
+
"\n",
|
710 |
+
"#==================================================\n",
|
711 |
+
"\n",
|
712 |
"print('=' * 70)\n",
|
713 |
"print('Done!')\n",
|
714 |
"print('=' * 70)\n",
|
|
|
961 |
" except KeyboardInterrupt:\n",
|
962 |
" break\n",
|
963 |
" \n",
|
964 |
+
" except Exception as e:\n",
|
965 |
+
" print('WARNING !!!')\n",
|
966 |
+
" print('=' * 70)\n",
|
967 |
+
" print('Error detected:', e)\n",
|
968 |
+
" print('=' * 70)\n",
|
969 |
" continue\n",
|
970 |
"\n",
|
971 |
"print('Done!')\n",
|
|
|
1089 |
" try:\n",
|
1090 |
" p_counts = d[1][10][1]\n",
|
1091 |
" p_counts.sort(reverse = True, key = lambda x: x[1])\n",
|
1092 |
+
" max_p_count = p_counts[0][1]\n",
|
1093 |
" trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)] \n",
|
1094 |
" \n",
|
1095 |
" if search_transposed_pitches:\n",
|
|
|
1102 |
" for m in search_pitches:\n",
|
1103 |
"\n",
|
1104 |
" m.sort(reverse = True, key = lambda x: x[1])\n",
|
1105 |
+
" max_pitches_count = m[0][1]\n",
|
1106 |
" trimmed_pitches_counts = [y for y in m if y[1] >= (max_pitches_count * pitches_counts_cutoff_threshold_ratio)] \n",
|
1107 |
"\n",
|
1108 |
" num_same_pitches = len(set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts]))\n",
|
|
|
1122 |
" except KeyboardInterrupt:\n",
|
1123 |
" break\n",
|
1124 |
" \n",
|
1125 |
+
" except Exception as e:\n",
|
1126 |
+
" print('WARNING !!!')\n",
|
1127 |
+
" print('=' * 70)\n",
|
1128 |
+
" print('Error detected:', e)\n",
|
1129 |
+
" ratios.append(0)\n",
|
1130 |
+
" print('=' * 70)\n",
|
1131 |
" break\n",
|
1132 |
"\n",
|
1133 |
"max_ratio = max(ratios)\n",
|
|
|
1207 |
"execution_count": null,
|
1208 |
"outputs": []
|
1209 |
},
|
1210 |
+
{
|
1211 |
+
"cell_type": "code",
|
1212 |
+
"source": [
|
1213 |
+
"#@title MIDI Chords Search (Fast)\n",
|
1214 |
+
"\n",
|
1215 |
+
"#@markdown NOTE: You can stop the search at any time to render partial results\n",
|
1216 |
+
"\n",
|
1217 |
+
"maximum_match_ratio_to_search_for = 1 #@param {type:\"slider\", min:0, max:1, step:0.01}\n",
|
1218 |
+
"chords_counts_cutoff_threshold_ratio = 0 #@param {type:\"slider\", min:0, max:1, step:0.05}\n",
|
1219 |
+
"skip_exact_matches = False #@param {type:\"boolean\"}\n",
|
1220 |
+
"render_MIDI_to_audio = False #@param {type:\"boolean\"}\n",
|
1221 |
+
"download_MIDI = False #@param {type:\"boolean\"}\n",
|
1222 |
+
"\n",
|
1223 |
+
"print('=' * 70)\n",
|
1224 |
+
"print('MIDI Chords Search')\n",
|
1225 |
+
"print('=' * 70)\n",
|
1226 |
+
"\n",
|
1227 |
+
"ratios = []\n",
|
1228 |
+
"\n",
|
1229 |
+
"for d in tqdm(meta_data):\n",
|
1230 |
+
"\n",
|
1231 |
+
" try:\n",
|
1232 |
+
"\n",
|
1233 |
+
" c_counts = d[1][8][1]\n",
|
1234 |
+
" if len(c_counts) == 0:\n",
|
1235 |
+
" c_counts = copy.deepcopy([[[0, 0], 0]])\n",
|
1236 |
+
"\n",
|
1237 |
+
" c_counts.sort(reverse = True, key = lambda x: x[0][1])\n",
|
1238 |
+
" max_c_count = c_counts[0][1]\n",
|
1239 |
+
" trimmed_c_counts = [y for y in c_counts if y[1] >= (max_c_count * chords_counts_cutoff_threshold_ratio)] \n",
|
1240 |
+
" trimmed_c_counts.sort(reverse = True, key = lambda x: x[1])\n",
|
1241 |
+
" \n",
|
1242 |
+
" max_chords_count = ms_chords_counts[0][1]\n",
|
1243 |
+
" trimmed_chords_counts = [y for y in ms_chords_counts if y[1] >= (max_chords_count * chords_counts_cutoff_threshold_ratio)] \n",
|
1244 |
+
"\n",
|
1245 |
+
" num_same_chords = len(set([tuple(T[0]) for T in trimmed_c_counts]) & set([tuple(t[0]) for t in trimmed_chords_counts]))\n",
|
1246 |
+
" same_chords_ratio = (num_same_chords / len(set(tuple(x[0]) for x in [trimmed_c_counts+trimmed_chords_counts][0])))\n",
|
1247 |
+
"\n",
|
1248 |
+
" if skip_exact_matches:\n",
|
1249 |
+
" if same_chords_ratio == 1:\n",
|
1250 |
+
" same_chords_ratio = 0\n",
|
1251 |
+
"\n",
|
1252 |
+
" if same_chords_ratio > maximum_match_ratio_to_search_for:\n",
|
1253 |
+
" same_chords_ratio = 0\n",
|
1254 |
+
"\n",
|
1255 |
+
" ratios.append(same_chords_ratio)\n",
|
1256 |
+
" \n",
|
1257 |
+
" except KeyboardInterrupt:\n",
|
1258 |
+
" break\n",
|
1259 |
+
" \n",
|
1260 |
+
" except Exception as e:\n",
|
1261 |
+
" print('WARNING !!!')\n",
|
1262 |
+
" print('=' * 70)\n",
|
1263 |
+
" print('Error detected:', e)\n",
|
1264 |
+
" ratios.append(0)\n",
|
1265 |
+
" print('=' * 70)\n",
|
1266 |
+
" continue\n",
|
1267 |
+
"\n",
|
1268 |
+
"max_ratio = max(ratios)\n",
|
1269 |
+
"max_ratio_index = ratios.index(max(ratios))\n",
|
1270 |
+
"\n",
|
1271 |
+
"print('FOUND')\n",
|
1272 |
+
"print('=' * 70)\n",
|
1273 |
+
"print('Match ratio', max_ratio)\n",
|
1274 |
+
"print('MIDI file name', meta_data[max_ratio_index][0])\n",
|
1275 |
+
"print('=' * 70)\n",
|
1276 |
+
"pprint.pprint(['Sample metadata entries', meta_data[max_ratio_index][1][:8]], compact = True)\n",
|
1277 |
+
"print('=' * 70)\n",
|
1278 |
+
"\n",
|
1279 |
+
"#============================================\n",
|
1280 |
+
"# MIDI rendering code\n",
|
1281 |
+
"#============================================\n",
|
1282 |
+
"\n",
|
1283 |
+
"print('Rendering source MIDI...')\n",
|
1284 |
+
"print('=' * 70)\n",
|
1285 |
+
"\n",
|
1286 |
+
"fn = meta_data[max_ratio_index][0]\n",
|
1287 |
+
"fn_idx = [y[0] for y in LAMD_files_list].index(fn)\n",
|
1288 |
+
"\n",
|
1289 |
+
"f = LAMD_files_list[fn_idx][1]\n",
|
1290 |
+
"\n",
|
1291 |
+
"ms_score = MIDI.midi2ms_score(open(f, 'rb').read())\n",
|
1292 |
+
"\n",
|
1293 |
+
"itrack = 1\n",
|
1294 |
+
"song_f = []\n",
|
1295 |
+
"\n",
|
1296 |
+
"while itrack < len(ms_score):\n",
|
1297 |
+
" for event in ms_score[itrack]: \n",
|
1298 |
+
" if event[0] == 'note':\n",
|
1299 |
+
" song_f.append(event)\n",
|
1300 |
+
" itrack += 1\n",
|
1301 |
+
"\n",
|
1302 |
+
"song_f.sort(key=lambda x: x[1])\n",
|
1303 |
+
"\n",
|
1304 |
+
"fname = f.split('.mid')[0]\n",
|
1305 |
+
"\n",
|
1306 |
+
"x = []\n",
|
1307 |
+
"y =[]\n",
|
1308 |
+
"c = []\n",
|
1309 |
+
"\n",
|
1310 |
+
"colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver', 'aqua', 'azure', 'bisque', 'coral']\n",
|
1311 |
+
"\n",
|
1312 |
+
"for s in song_f:\n",
|
1313 |
+
" x.append(s[1] / 1000)\n",
|
1314 |
+
" y.append(s[4])\n",
|
1315 |
+
" c.append(colors[s[3]])\n",
|
1316 |
+
"\n",
|
1317 |
+
"if render_MIDI_to_audio:\n",
|
1318 |
+
" FluidSynth(\"/usr/share/sounds/sf2/FluidR3_GM.sf2\", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))\n",
|
1319 |
+
" display(Audio(str(fname + '.wav'), rate=16000))\n",
|
1320 |
+
"\n",
|
1321 |
+
"plt.figure(figsize=(14,5))\n",
|
1322 |
+
"ax=plt.axes(title=fname)\n",
|
1323 |
+
"ax.set_facecolor('black')\n",
|
1324 |
+
"\n",
|
1325 |
+
"plt.scatter(x,y, c=c)\n",
|
1326 |
+
"plt.xlabel(\"Time\")\n",
|
1327 |
+
"plt.ylabel(\"Pitch\")\n",
|
1328 |
+
"plt.show()\n",
|
1329 |
+
"\n",
|
1330 |
+
"#==============================================\n",
|
1331 |
+
"\n",
|
1332 |
+
"if download_MIDI:\n",
|
1333 |
+
" print('=' * 70)\n",
|
1334 |
+
" print('Downloading MIDI file', str(fn) + '.mid')\n",
|
1335 |
+
" files.download(f)\n",
|
1336 |
+
" print('=' * 70)"
|
1337 |
+
],
|
1338 |
+
"metadata": {
|
1339 |
+
"cellView": "form",
|
1340 |
+
"id": "HBV0CtPAA8nF"
|
1341 |
+
},
|
1342 |
+
"execution_count": null,
|
1343 |
+
"outputs": []
|
1344 |
+
},
|
1345 |
{
|
1346 |
"cell_type": "code",
|
1347 |
"source": [
|
|
|
1385 |
" except KeyboardInterrupt:\n",
|
1386 |
" break\n",
|
1387 |
" \n",
|
1388 |
+
" except Exception as e:\n",
|
1389 |
+
" print('WARNING !!!')\n",
|
1390 |
+
" print('=' * 70)\n",
|
1391 |
+
" print('Error detected:', e)\n",
|
1392 |
+
" ratios.append(0)\n",
|
1393 |
+
" print('=' * 70)\n",
|
1394 |
+
" continue\n",
|
1395 |
"\n",
|
1396 |
"max_ratio = max(ratios)\n",
|
1397 |
"max_ratio_index = ratios.index(max(ratios))\n",
|
|
|
1520 |
" print('=' * 70)\n",
|
1521 |
" break\n",
|
1522 |
" \n",
|
1523 |
+
" except Exception as e:\n",
|
1524 |
+
" print('WARNING !!!')\n",
|
1525 |
" print('=' * 70)\n",
|
1526 |
+
" print('Error detected:', e)\n",
|
1527 |
+
" print('=' * 70)\n",
|
1528 |
+
" continue\n",
|
1529 |
"\n",
|
1530 |
" if d[0] != md5_hash_MIDI_file_name:\n",
|
1531 |
" print('Not found!')\n",
|
los_angeles_midi_dataset_search_and_explore.py
CHANGED
@@ -520,6 +520,49 @@ for i in range(-6, 6):
|
|
520 |
|
521 |
patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))
|
522 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
523 |
print('=' * 70)
|
524 |
print('Done!')
|
525 |
print('=' * 70)
|
@@ -755,7 +798,11 @@ for i in tqdm(range(0, len(midi_matrixes), (num_jobs*scores_per_job))):
|
|
755 |
except KeyboardInterrupt:
|
756 |
break
|
757 |
|
758 |
-
except:
|
|
|
|
|
|
|
|
|
759 |
continue
|
760 |
|
761 |
print('Done!')
|
@@ -869,7 +916,7 @@ for d in tqdm(meta_data):
|
|
869 |
try:
|
870 |
p_counts = d[1][10][1]
|
871 |
p_counts.sort(reverse = True, key = lambda x: x[1])
|
872 |
-
max_p_count = p_counts[
|
873 |
trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)]
|
874 |
|
875 |
if search_transposed_pitches:
|
@@ -882,7 +929,7 @@ for d in tqdm(meta_data):
|
|
882 |
for m in search_pitches:
|
883 |
|
884 |
m.sort(reverse = True, key = lambda x: x[1])
|
885 |
-
max_pitches_count = m[
|
886 |
trimmed_pitches_counts = [y for y in m if y[1] >= (max_pitches_count * pitches_counts_cutoff_threshold_ratio)]
|
887 |
|
888 |
num_same_pitches = len(set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts]))
|
@@ -902,8 +949,138 @@ for d in tqdm(meta_data):
|
|
902 |
except KeyboardInterrupt:
|
903 |
break
|
904 |
|
905 |
-
except:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
906 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
907 |
|
908 |
max_ratio = max(ratios)
|
909 |
max_ratio_index = ratios.index(max(ratios))
|
@@ -1015,8 +1192,13 @@ for d in tqdm(meta_data):
|
|
1015 |
except KeyboardInterrupt:
|
1016 |
break
|
1017 |
|
1018 |
-
except:
|
1019 |
-
|
|
|
|
|
|
|
|
|
|
|
1020 |
|
1021 |
max_ratio = max(ratios)
|
1022 |
max_ratio_index = ratios.index(max(ratios))
|
@@ -1135,10 +1317,12 @@ if md5_hash_MIDI_file_name != '':
|
|
1135 |
print('=' * 70)
|
1136 |
break
|
1137 |
|
1138 |
-
except:
|
1139 |
-
print('
|
1140 |
print('=' * 70)
|
1141 |
-
|
|
|
|
|
1142 |
|
1143 |
if d[0] != md5_hash_MIDI_file_name:
|
1144 |
print('Not found!')
|
|
|
520 |
|
521 |
patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))
|
522 |
|
523 |
+
|
524 |
+
#==================================================
|
525 |
+
|
526 |
+
ms_score = MIDI.midi2ms_score(open(f, 'rb').read())
|
527 |
+
|
528 |
+
ms_events_matrix = []
|
529 |
+
|
530 |
+
itrack1 = 1
|
531 |
+
|
532 |
+
while itrack1 < len(ms_score):
|
533 |
+
for event in ms_score[itrack1]:
|
534 |
+
if event[0] == 'note':
|
535 |
+
ms_events_matrix.append(event)
|
536 |
+
itrack1 += 1
|
537 |
+
|
538 |
+
ms_events_matrix.sort(key=lambda x: x[1])
|
539 |
+
|
540 |
+
|
541 |
+
chords = []
|
542 |
+
pe = ms_events_matrix[0]
|
543 |
+
cho = []
|
544 |
+
for e in ms_events_matrix:
|
545 |
+
if (e[1] - pe[1]) == 0:
|
546 |
+
if e[3] != 9:
|
547 |
+
if (e[4] % 12) not in cho:
|
548 |
+
cho.append(e[4] % 12)
|
549 |
+
else:
|
550 |
+
if len(cho) > 0:
|
551 |
+
chords.append(sorted(cho))
|
552 |
+
cho = []
|
553 |
+
if e[3] != 9:
|
554 |
+
if (e[4] % 12) not in cho:
|
555 |
+
cho.append(e[4] % 12)
|
556 |
+
|
557 |
+
pe = e
|
558 |
+
|
559 |
+
if len(cho) > 0:
|
560 |
+
chords.append(sorted(cho))
|
561 |
+
|
562 |
+
ms_chords_counts = sorted([[list(key), val] for key,val in Counter([tuple(c) for c in chords if len(c) > 1]).most_common()], reverse=True, key = lambda x: x[1])
|
563 |
+
|
564 |
+
#==================================================
|
565 |
+
|
566 |
print('=' * 70)
|
567 |
print('Done!')
|
568 |
print('=' * 70)
|
|
|
798 |
except KeyboardInterrupt:
|
799 |
break
|
800 |
|
801 |
+
except Exception as e:
|
802 |
+
print('WARNING !!!')
|
803 |
+
print('=' * 70)
|
804 |
+
print('Error detected:', e)
|
805 |
+
print('=' * 70)
|
806 |
continue
|
807 |
|
808 |
print('Done!')
|
|
|
916 |
try:
|
917 |
p_counts = d[1][10][1]
|
918 |
p_counts.sort(reverse = True, key = lambda x: x[1])
|
919 |
+
max_p_count = p_counts[0][1]
|
920 |
trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)]
|
921 |
|
922 |
if search_transposed_pitches:
|
|
|
929 |
for m in search_pitches:
|
930 |
|
931 |
m.sort(reverse = True, key = lambda x: x[1])
|
932 |
+
max_pitches_count = m[0][1]
|
933 |
trimmed_pitches_counts = [y for y in m if y[1] >= (max_pitches_count * pitches_counts_cutoff_threshold_ratio)]
|
934 |
|
935 |
num_same_pitches = len(set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts]))
|
|
|
949 |
except KeyboardInterrupt:
|
950 |
break
|
951 |
|
952 |
+
except Exception as e:
|
953 |
+
print('WARNING !!!')
|
954 |
+
print('=' * 70)
|
955 |
+
print('Error detected:', e)
|
956 |
+
ratios.append(0)
|
957 |
+
print('=' * 70)
|
958 |
+
break
|
959 |
+
|
960 |
+
max_ratio = max(ratios)
|
961 |
+
max_ratio_index = ratios.index(max(ratios))
|
962 |
+
|
963 |
+
print('FOUND')
|
964 |
+
print('=' * 70)
|
965 |
+
print('Match ratio', max_ratio)
|
966 |
+
print('MIDI file name', meta_data[max_ratio_index][0])
|
967 |
+
print('=' * 70)
|
968 |
+
pprint.pprint(['Sample metadata entries', meta_data[max_ratio_index][1][:8]], compact = True)
|
969 |
+
print('=' * 70)
|
970 |
+
|
971 |
+
#============================================
|
972 |
+
# MIDI rendering code
|
973 |
+
#============================================
|
974 |
+
|
975 |
+
print('Rendering source MIDI...')
|
976 |
+
print('=' * 70)
|
977 |
+
|
978 |
+
fn = meta_data[max_ratio_index][0]
|
979 |
+
fn_idx = [y[0] for y in LAMD_files_list].index(fn)
|
980 |
+
|
981 |
+
f = LAMD_files_list[fn_idx][1]
|
982 |
+
|
983 |
+
ms_score = MIDI.midi2ms_score(open(f, 'rb').read())
|
984 |
+
|
985 |
+
itrack = 1
|
986 |
+
song_f = []
|
987 |
+
|
988 |
+
while itrack < len(ms_score):
|
989 |
+
for event in ms_score[itrack]:
|
990 |
+
if event[0] == 'note':
|
991 |
+
song_f.append(event)
|
992 |
+
itrack += 1
|
993 |
+
|
994 |
+
song_f.sort(key=lambda x: x[1])
|
995 |
+
|
996 |
+
fname = f.split('.mid')[0]
|
997 |
+
|
998 |
+
x = []
|
999 |
+
y =[]
|
1000 |
+
c = []
|
1001 |
+
|
1002 |
+
colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver', 'aqua', 'azure', 'bisque', 'coral']
|
1003 |
+
|
1004 |
+
for s in song_f:
|
1005 |
+
x.append(s[1] / 1000)
|
1006 |
+
y.append(s[4])
|
1007 |
+
c.append(colors[s[3]])
|
1008 |
+
|
1009 |
+
if render_MIDI_to_audio:
|
1010 |
+
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
|
1011 |
+
display(Audio(str(fname + '.wav'), rate=16000))
|
1012 |
+
|
1013 |
+
plt.figure(figsize=(14,5))
|
1014 |
+
ax=plt.axes(title=fname)
|
1015 |
+
ax.set_facecolor('black')
|
1016 |
+
|
1017 |
+
plt.scatter(x,y, c=c)
|
1018 |
+
plt.xlabel("Time")
|
1019 |
+
plt.ylabel("Pitch")
|
1020 |
+
plt.show()
|
1021 |
+
|
1022 |
+
#==============================================
|
1023 |
+
|
1024 |
+
if download_MIDI:
|
1025 |
+
print('=' * 70)
|
1026 |
+
print('Downloading MIDI file', str(fn) + '.mid')
|
1027 |
+
files.download(f)
|
1028 |
+
print('=' * 70)
|
1029 |
+
|
1030 |
+
#@title MIDI Chords Search (Fast)
|
1031 |
+
|
1032 |
+
#@markdown NOTE: You can stop the search at any time to render partial results
|
1033 |
+
|
1034 |
+
maximum_match_ratio_to_search_for = 1 #@param {type:"slider", min:0, max:1, step:0.01}
|
1035 |
+
chords_counts_cutoff_threshold_ratio = 0 #@param {type:"slider", min:0, max:1, step:0.05}
|
1036 |
+
skip_exact_matches = False #@param {type:"boolean"}
|
1037 |
+
render_MIDI_to_audio = False #@param {type:"boolean"}
|
1038 |
+
download_MIDI = False #@param {type:"boolean"}
|
1039 |
+
|
1040 |
+
print('=' * 70)
|
1041 |
+
print('MIDI Chords Search')
|
1042 |
+
print('=' * 70)
|
1043 |
+
|
1044 |
+
ratios = []
|
1045 |
+
|
1046 |
+
for d in tqdm(meta_data):
|
1047 |
+
|
1048 |
+
try:
|
1049 |
+
|
1050 |
+
c_counts = d[1][8][1]
|
1051 |
+
if len(c_counts) == 0:
|
1052 |
+
c_counts = copy.deepcopy([[[0, 0], 0]])
|
1053 |
+
|
1054 |
+
c_counts.sort(reverse = True, key = lambda x: x[0][1])
|
1055 |
+
max_c_count = c_counts[0][1]
|
1056 |
+
trimmed_c_counts = [y for y in c_counts if y[1] >= (max_c_count * chords_counts_cutoff_threshold_ratio)]
|
1057 |
+
trimmed_c_counts.sort(reverse = True, key = lambda x: x[1])
|
1058 |
+
|
1059 |
+
max_chords_count = ms_chords_counts[0][1]
|
1060 |
+
trimmed_chords_counts = [y for y in ms_chords_counts if y[1] >= (max_chords_count * chords_counts_cutoff_threshold_ratio)]
|
1061 |
+
|
1062 |
+
num_same_chords = len(set([tuple(T[0]) for T in trimmed_c_counts]) & set([tuple(t[0]) for t in trimmed_chords_counts]))
|
1063 |
+
same_chords_ratio = (num_same_chords / len(set(tuple(x[0]) for x in [trimmed_c_counts+trimmed_chords_counts][0])))
|
1064 |
+
|
1065 |
+
if skip_exact_matches:
|
1066 |
+
if same_chords_ratio == 1:
|
1067 |
+
same_chords_ratio = 0
|
1068 |
+
|
1069 |
+
if same_chords_ratio > maximum_match_ratio_to_search_for:
|
1070 |
+
same_chords_ratio = 0
|
1071 |
+
|
1072 |
+
ratios.append(same_chords_ratio)
|
1073 |
+
|
1074 |
+
except KeyboardInterrupt:
|
1075 |
break
|
1076 |
+
|
1077 |
+
except Exception as e:
|
1078 |
+
print('WARNING !!!')
|
1079 |
+
print('=' * 70)
|
1080 |
+
print('Error detected:', e)
|
1081 |
+
ratios.append(0)
|
1082 |
+
print('=' * 70)
|
1083 |
+
continue
|
1084 |
|
1085 |
max_ratio = max(ratios)
|
1086 |
max_ratio_index = ratios.index(max(ratios))
|
|
|
1192 |
except KeyboardInterrupt:
|
1193 |
break
|
1194 |
|
1195 |
+
except Exception as e:
|
1196 |
+
print('WARNING !!!')
|
1197 |
+
print('=' * 70)
|
1198 |
+
print('Error detected:', e)
|
1199 |
+
ratios.append(0)
|
1200 |
+
print('=' * 70)
|
1201 |
+
continue
|
1202 |
|
1203 |
max_ratio = max(ratios)
|
1204 |
max_ratio_index = ratios.index(max(ratios))
|
|
|
1317 |
print('=' * 70)
|
1318 |
break
|
1319 |
|
1320 |
+
except Exception as e:
|
1321 |
+
print('WARNING !!!')
|
1322 |
print('=' * 70)
|
1323 |
+
print('Error detected:', e)
|
1324 |
+
print('=' * 70)
|
1325 |
+
continue
|
1326 |
|
1327 |
if d[0] != md5_hash_MIDI_file_name:
|
1328 |
print('Not found!')
|