projectlosangeles commited on
Commit
cee03a8
1 Parent(s): 1af5730

Upload 10 files

Browse files
Los_Angeles_MIDI_Dataset_Search_and_Explore.ipynb CHANGED
@@ -11,7 +11,7 @@
11
  "id": "SiTIpPjArIyr"
12
  },
13
  "source": [
14
- "# Los Angeles MIDI Dataset: Search and Explore (ver. 2.2)\n",
15
  "\n",
16
  "***\n",
17
  "\n",
@@ -145,23 +145,6 @@
145
  "\n",
146
  "#================================================\n",
147
  "\n",
148
- "%cd /content/Los-Angeles-MIDI-Dataset/MIDI-MATRIXES\n",
149
- "\n",
150
- "print('=' * 70)\n",
151
- "print('Unzipping MIDI-MATRIXES...Please wait...')\n",
152
- "\n",
153
- "!cat LAMDa_MIDI_MATRIXES.zip* > LAMDa_MIDI_MATRIXES.zip\n",
154
- "print('=' * 70)\n",
155
- "\n",
156
- "!unzip -j LAMDa_MIDI_MATRIXES.zip\n",
157
- "print('=' * 70)\n",
158
- "\n",
159
- "print('Done! Enjoy! :)')\n",
160
- "print('=' * 70)\n",
161
- "%cd /content/\n",
162
- "\n",
163
- "#==================================================\n",
164
- "\n",
165
  "%cd /content/Los-Angeles-MIDI-Dataset/TOTALS\n",
166
  "\n",
167
  "print('=' * 70)\n",
@@ -192,10 +175,6 @@
192
  "meta_data = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/META-DATA/LAMDa_META_DATA.pickle', 'rb'))\n",
193
  "print('Done!')\n",
194
  "print('=' * 70)\n",
195
- "print('Loading LAMDa MIDI-MATRIXES...')\n",
196
- "midi_matrixes = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/MIDI-MATRIXES/LAMDa_MIDI_MATRIXES.pickle', 'rb'))\n",
197
- "print('Done!')\n",
198
- "print('=' * 70)\n",
199
  "print('Loading LAMDa TOTALS...')\n",
200
  "totals = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/TOTALS/LAMDa_TOTALS.pickle', 'rb'))\n",
201
  "print('Done!')\n",
@@ -227,10 +206,10 @@
227
  "print('Downloading Los Angeles MIDI Dataset...Please wait...')\n",
228
  "print('=' * 70)\n",
229
  "\n",
230
- "hf_hub_download(repo_id='projectlosangeles/Los-Angeles-MIDI-Dataset', \n",
231
- " filename='Los-Angeles-MIDI-Dataset-Ver-2-0-CC-BY-NC-SA.zip',\n",
232
  " repo_type=\"dataset\",\n",
233
- " local_dir='/content/LAMD', \n",
234
  " local_dir_use_symlinks=False)\n",
235
  "print('=' * 70)\n",
236
  "print('Done! Enjoy! :)')\n",
@@ -251,7 +230,7 @@
251
  "\n",
252
  "print('=' * 70)\n",
253
  "print('Unzipping Los Angeles MIDI Dataset...Please wait...')\n",
254
- "!unzip 'Los-Angeles-MIDI-Dataset-Ver-2-0-CC-BY-NC-SA.zip'\n",
255
  "print('=' * 70)\n",
256
  "\n",
257
  "print('Done! Enjoy! :)')\n",
@@ -316,7 +295,7 @@
316
  "#@title Plot totals from MIDI matrixes (legacy)\n",
317
  "\n",
318
  "cos_sim = pairwise.cosine_similarity(\n",
319
- " totals[0][0][4] \n",
320
  " )\n",
321
  "plt.figure(figsize=(8, 8))\n",
322
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
@@ -329,7 +308,7 @@
329
  "plt.plot()\n",
330
  "\n",
331
  "cos_sim = pairwise.cosine_similarity(\n",
332
- " totals[0][0][5] \n",
333
  " )\n",
334
  "plt.figure(figsize=(8, 8))\n",
335
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
@@ -342,7 +321,7 @@
342
  "plt.plot()\n",
343
  "\n",
344
  "cos_sim = pairwise.cosine_similarity(\n",
345
- " totals[0][0][6] \n",
346
  " )\n",
347
  "plt.figure(figsize=(8, 8))\n",
348
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
@@ -355,7 +334,7 @@
355
  "plt.plot()\n",
356
  "\n",
357
  "cos_sim = pairwise.cosine_similarity(\n",
358
- " totals[0][0][7] \n",
359
  " )\n",
360
  "plt.figure(figsize=(8, 8))\n",
361
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
@@ -368,7 +347,7 @@
368
  "plt.plot()\n",
369
  "\n",
370
  "cos_sim = pairwise.cosine_similarity(\n",
371
- " totals[0][0][8] \n",
372
  " )\n",
373
  "plt.figure(figsize=(8, 8))\n",
374
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
@@ -381,7 +360,7 @@
381
  "plt.plot()\n",
382
  "\n",
383
  "cos_sim = pairwise.cosine_similarity(\n",
384
- " totals[0][0][9] \n",
385
  " )\n",
386
  "plt.figure(figsize=(8, 8))\n",
387
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
@@ -419,8 +398,8 @@
419
  "plt.plot(y, pitches_counts_totals)\n",
420
  "\n",
421
  "plt.title('MIDI Instruments Pitches')\n",
422
- "plt.xlabel(\"Ratio\")\n",
423
- "plt.ylabel(\"Pitch\")\n",
424
  "plt.tight_layout()\n",
425
  "plt.plot()\n",
426
  "\n",
@@ -437,15 +416,15 @@
437
  " x += 1\n",
438
  "\n",
439
  "cos_sim = pairwise.cosine_similarity(\n",
440
- " sim_mat \n",
441
  " )\n",
442
  "plt.figure(figsize=(8, 8))\n",
443
  "plt.imshow(sim_mat, cmap=\"inferno\", interpolation=\"none\")\n",
444
  "im_ratio = 1\n",
445
  "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n",
446
- "plt.title('MIDI Instruments Pitches')\n",
447
- "plt.xlabel(\"Ratio\")\n",
448
- "plt.ylabel(\"Pitch\")\n",
449
  "plt.tight_layout()\n",
450
  "plt.plot()\n",
451
  "\n",
@@ -464,8 +443,8 @@
464
  "plt.plot(y, pitches_counts_totals)\n",
465
  "\n",
466
  "plt.title('MIDI Drums Pitches')\n",
467
- "plt.xlabel(\"Ratio\")\n",
468
- "plt.ylabel(\"Pitch\")\n",
469
  "plt.tight_layout()\n",
470
  "plt.plot()\n",
471
  "\n",
@@ -482,15 +461,15 @@
482
  " x += 1\n",
483
  "\n",
484
  "cos_sim = pairwise.cosine_similarity(\n",
485
- " sim_mat \n",
486
  " )\n",
487
  "plt.figure(figsize=(8, 8))\n",
488
  "plt.imshow(sim_mat, cmap=\"inferno\", interpolation=\"none\")\n",
489
  "im_ratio = 1\n",
490
  "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n",
491
  "plt.title('MIDI Drums Pitches')\n",
492
- "plt.xlabel(\"Ratio\")\n",
493
- "plt.ylabel(\"Pitch\")\n",
494
  "plt.tight_layout()\n",
495
  "plt.plot()\n",
496
  "\n",
@@ -500,7 +479,7 @@
500
  "\n",
501
  "\n",
502
  "for m in tqdm(meta_data):\n",
503
- " for mm in m[1][11][1]:\n",
504
  " patches_counts_totals[mm[0]] += mm[1]\n",
505
  "\n",
506
  "\n",
@@ -509,8 +488,8 @@
509
  "plt.plot(y, patches_counts_totals[:128])\n",
510
  "\n",
511
  "plt.title('MIDI Patches')\n",
512
- "plt.xlabel(\"Ratio\")\n",
513
- "plt.ylabel('Patch')\n",
514
  "plt.tight_layout()\n",
515
  "plt.plot()"
516
  ],
@@ -545,82 +524,6 @@
545
  "print('=' * 70)\n",
546
  "print('Loading MIDI file...')\n",
547
  "\n",
548
- "score = MIDI.midi2ms_score(open(f, 'rb').read())\n",
549
- "\n",
550
- "events_matrix = []\n",
551
- "\n",
552
- "itrack = 1\n",
553
- "\n",
554
- "while itrack < len(score):\n",
555
- " for event in score[itrack]: \n",
556
- " events_matrix.append(event)\n",
557
- " itrack += 1\n",
558
- "\n",
559
- "# Sorting...\n",
560
- "events_matrix.sort(key=lambda x: x[1])\n",
561
- "\n",
562
- "# recalculating timings\n",
563
- "for e in events_matrix:\n",
564
- " e[1] = int(e[1] / 10)\n",
565
- " if e[0] == 'note':\n",
566
- " e[2] = int(e[2] / 20)\n",
567
- "\n",
568
- "# final processing...\n",
569
- "\n",
570
- "melody_chords = []\n",
571
- "\n",
572
- "patches = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
573
- "\n",
574
- "pe = events_matrix[0]\n",
575
- "for e in events_matrix:\n",
576
- "\n",
577
- " if e[0] == 'note':\n",
578
- " # ['note', start_time, duration, channel, note, velocity]\n",
579
- " time = max(0, min(255, e[1]-pe[1]))\n",
580
- " duration = max(1, min(255, e[2]))\n",
581
- " channel = max(0, min(15, e[3]))\n",
582
- "\n",
583
- " if e[3] != 9:\n",
584
- " instrument = max(0, min(127, patches[e[3]]))\n",
585
- " else:\n",
586
- " instrument = max(128, min(255, patches[e[3]]+128))\n",
587
- "\n",
588
- " if e[3] != 9:\n",
589
- "\n",
590
- " pitch = max(1, min(127, e[4]))\n",
591
- " else:\n",
592
- " pitch = max(129, min(255, e[4]+128))\n",
593
- "\n",
594
- " if e[3] != 9:\n",
595
- " velocity = max(1, min(127, e[5]))\n",
596
- " else:\n",
597
- " velocity = max(129, min(255, e[5]+128))\n",
598
- "\n",
599
- " melody_chords.append([time, duration, channel, instrument, pitch, velocity])\n",
600
- "\n",
601
- " if e[0] == 'patch_change':\n",
602
- " # ['patch_change', dtime, channel, patch]\n",
603
- " time = max(0, min(127, e[1]-pe[1]))\n",
604
- " channel = max(0, min(15, e[2]))\n",
605
- " patch = max(0, min(127, e[3]))\n",
606
- "\n",
607
- " patches[channel] = patch\n",
608
- "\n",
609
- " pe = e # Previous event\n",
610
- "\n",
611
- "MATRIX = [[0]*256 for i in range(38)]\n",
612
- "\n",
613
- "for m in melody_chords:\n",
614
- "\n",
615
- " MATRIX[0][m[0]] += 1\n",
616
- " MATRIX[1][m[1]] += 1\n",
617
- " MATRIX[2][m[2]] += 1 \n",
618
- " MATRIX[3][m[3]] += 1\n",
619
- " MATRIX[4][m[4]] += 1\n",
620
- " MATRIX[5][m[5]] += 1\n",
621
- " MATRIX[m[2]+6][m[3]] += 1\n",
622
- " MATRIX[m[2]+22][m[4]] += 1\n",
623
- "\n",
624
  "#==================================================\n",
625
  "\n",
626
  "score = MIDI.midi2score(open(f, 'rb').read())\n",
@@ -630,7 +533,7 @@
630
  "track_count = 0\n",
631
  "\n",
632
  "for s in score:\n",
633
- " \n",
634
  " if track_count > 0:\n",
635
  " track = s\n",
636
  " track.sort(key=lambda x: x[1])\n",
@@ -639,7 +542,7 @@
639
  " midi_ticks = s\n",
640
  "\n",
641
  " track_count += 1\n",
642
- " \n",
643
  "events_matrix.sort(key=lambda x: x[1])\n",
644
  "\n",
645
  "mult_pitches_counts = []\n",
@@ -662,7 +565,7 @@
662
  "\n",
663
  " pitches_counts = [[y[0],y[1]] for y in Counter([y[4] for y in events_matrix1 if y[0] == 'note']).most_common()]\n",
664
  " pitches_counts.sort(key=lambda x: x[0], reverse=True)\n",
665
- " \n",
666
  " mult_pitches_counts.append(pitches_counts)\n",
667
  "\n",
668
  "patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))\n",
@@ -677,7 +580,7 @@
677
  "itrack1 = 1\n",
678
  "\n",
679
  "while itrack1 < len(ms_score):\n",
680
- " for event in ms_score[itrack1]: \n",
681
  " if event[0] == 'note':\n",
682
  " ms_events_matrix.append(event)\n",
683
  " itrack1 += 1\n",
@@ -702,7 +605,7 @@
702
  " cho.append(e[4] % 12)\n",
703
  "\n",
704
  " pe = e\n",
705
- " \n",
706
  "if len(cho) > 0:\n",
707
  " chords.append(sorted(cho))\n",
708
  "\n",
@@ -716,7 +619,7 @@
716
  " times.append((e[1]-pt))\n",
717
  " start = False\n",
718
  " pt = e[1]\n",
719
- " \n",
720
  "durs = [e[2] for e in ms_events_matrix]\n",
721
  "vels = [e[5] for e in ms_events_matrix]\n",
722
  "\n",
@@ -748,7 +651,7 @@
748
  "song_f = []\n",
749
  "\n",
750
  "while itrack < len(ms_score):\n",
751
- " for event in ms_score[itrack]: \n",
752
  " if event[0] == 'note':\n",
753
  " song_f.append(event)\n",
754
  " itrack += 1\n",
@@ -800,296 +703,7 @@
800
  {
801
  "cell_type": "code",
802
  "source": [
803
- "#@title Legacy MIDI Matrixes Search (Slow)\n",
804
- "\n",
805
- "#@markdown NOTE: You can stop the search at any time to render partial results\n",
806
- "\n",
807
- "minimum_match_ratio_to_search_for = 0 #@param {type:\"slider\", min:0, max:500, step:1}\n",
808
- "stop_search_on_exact_match = True #@param {type:\"boolean\"}\n",
809
- "skip_exact_matches = False #@param {type:\"boolean\"}\n",
810
- "render_MIDI_to_audio = False #@param {type:\"boolean\"}\n",
811
- "download_MIDI = False #@param {type:\"boolean\"}\n",
812
- "\n",
813
- "#=================================================================================\n",
814
- "\n",
815
- "matching_type = \"minkowski\"\n",
816
- "\n",
817
- "def compress_matrix(midi_matrix):\n",
818
- "\n",
819
- " MX = 38\n",
820
- " MY = 256\n",
821
- "\n",
822
- " if len(midi_matrix) == MX:\n",
823
- "\n",
824
- " compressed_matrix = []\n",
825
- " zeros = 0\n",
826
- " zeros_shift = 0\n",
827
- " zeros_count = 0\n",
828
- "\n",
829
- " for m in midi_matrix:\n",
830
- " for mm in m:\n",
831
- " zeros_shift = max(zeros_shift, mm) + 1\n",
832
- "\n",
833
- " compressed_matrix.append(zeros_shift)\n",
834
- "\n",
835
- " for m in midi_matrix:\n",
836
- " if len(m) == MY:\n",
837
- " for mm in m:\n",
838
- " if mm != 0:\n",
839
- " if zeros > 0:\n",
840
- " compressed_matrix.append(zeros+zeros_shift)\n",
841
- " zeros = 0\n",
842
- " compressed_matrix.append(mm)\n",
843
- " \n",
844
- " else:\n",
845
- " zeros += 1\n",
846
- " zeros_count += 1\n",
847
- " \n",
848
- " else:\n",
849
- " print('Wrong matrix format!')\n",
850
- " return [1]\n",
851
- "\n",
852
- " if zeros > 0:\n",
853
- " compressed_matrix.append(zeros+zeros_shift)\n",
854
- "\n",
855
- " compressed_matrix.append(zeros_count+zeros_shift)\n",
856
- " compressed_matrix.append(zeros_shift)\n",
857
- "\n",
858
- " return compressed_matrix\n",
859
- "\n",
860
- " else:\n",
861
- " print('Wrong matrix format!')\n",
862
- " return [0]\n",
863
- "\n",
864
- "#=================================================================================\n",
865
- "\n",
866
- "def decompress_matrix(compressed_midi_matrix):\n",
867
- "\n",
868
- " MX = 38\n",
869
- " MY = 256\n",
870
- "\n",
871
- " zeros_count = 0\n",
872
- "\n",
873
- " temp_matrix = []\n",
874
- " decompressed_matrix = [[0]*MY for i in range(MX)]\n",
875
- "\n",
876
- " if compressed_midi_matrix[0] == compressed_midi_matrix[-1]:\n",
877
- " zeros_shift = compressed_midi_matrix[0]\n",
878
- " mcount = 0\n",
879
- "\n",
880
- " for c in compressed_midi_matrix[1:-2]:\n",
881
- " if c > zeros_shift:\n",
882
- " temp_matrix.extend([0] * (c-zeros_shift))\n",
883
- " zeros_count += (c-zeros_shift)\n",
884
- "\n",
885
- " else:\n",
886
- " temp_matrix.extend([c])\n",
887
- "\n",
888
- " if len(temp_matrix) == (MX * MY):\n",
889
- "\n",
890
- " for i in range(MX):\n",
891
- " for j in range(MY):\n",
892
- " decompressed_matrix[i][j] = copy.deepcopy(temp_matrix[(i*MY) + j])\n",
893
- " \n",
894
- " if len(decompressed_matrix) == MX and zeros_count == (compressed_midi_matrix[-2]-zeros_shift):\n",
895
- " return decompressed_matrix\n",
896
- "\n",
897
- " else:\n",
898
- " print('Matrix is corrupted!')\n",
899
- " return [len(decompressed_matrix), (MX * MY), zeros_count, (compressed_midi_matrix[-2]-zeros_shift)]\n",
900
- " \n",
901
- " else:\n",
902
- " print('Matrix is corrupted!')\n",
903
- " return [len(temp_matrix), zeros_count]\n",
904
- "\n",
905
- " else:\n",
906
- " print('Matrix is corrupted!')\n",
907
- " return [0]\n",
908
- "\n",
909
- "#=================================================================================\n",
910
- "\n",
911
- "def batched_scores(matbatch, matrix):\n",
912
- "\n",
913
- " sco= []\n",
914
- " for D in matbatch:\n",
915
- "\n",
916
- " dist = pairwise_distances(matrix, decompress_matrix(D[1]), metric=matching_type)[0][0]\n",
917
- "\n",
918
- " if skip_exact_matches:\n",
919
- " if dist == 0:\n",
920
- " dist = 999999\n",
921
- "\n",
922
- " if dist <= minimum_match_ratio_to_search_for:\n",
923
- " dist = 999999\n",
924
- "\n",
925
- " sco.append(dist)\n",
926
- "\n",
927
- " return sco\n",
928
- "\n",
929
- "#=================================================================================\n",
930
- "\n",
931
- "print('=' * 70)\n",
932
- "print('Searching...Please wait...')\n",
933
- "print('=' * 70)\n",
934
- "\n",
935
- "scores = []\n",
936
- "\n",
937
- "c_count = multiprocessing.cpu_count()\n",
938
- "\n",
939
- "par = Parallel(n_jobs=c_count)\n",
940
- "\n",
941
- "num_jobs = c_count\n",
942
- "scores_per_job = 100\n",
943
- "\n",
944
- "MATRIX_X = [MATRIX] * num_jobs\n",
945
- "\n",
946
- "for i in tqdm(range(0, len(midi_matrixes), (num_jobs*scores_per_job))):\n",
947
- " \n",
948
- " try:\n",
949
- "\n",
950
- " MAT_BATCHES = []\n",
951
- "\n",
952
- " for j in range(num_jobs):\n",
953
- " MAT_BATCHES.append(midi_matrixes[i+(j*scores_per_job):i+((j+1)*scores_per_job)])\n",
954
- "\n",
955
- " output = par(delayed(batched_scores) (MB, MAT) for MB, MAT in zip(MAT_BATCHES, MATRIX_X))\n",
956
- "\n",
957
- " output1 = []\n",
958
- "\n",
959
- " for o in output:\n",
960
- " output1.extend(o)\n",
961
- "\n",
962
- " scores.extend(output1) \n",
963
- "\n",
964
- " if stop_search_on_exact_match:\n",
965
- " if 0 in output1:\n",
966
- " print('=' * 70)\n",
967
- " print('Found exact match!')\n",
968
- " print('Stoping further search...')\n",
969
- " print('=' * 70)\n",
970
- " break\n",
971
- "\n",
972
- " else:\n",
973
- " if 0 in output1:\n",
974
- " print('=' * 70)\n",
975
- " print('Found exact match!')\n",
976
- " print('=' * 70)\n",
977
- " print('LAMDa Index:', scores.index(min(scores)))\n",
978
- " print('LAMDa File Name:', midi_matrixes[scores.index(min(scores))][0])\n",
979
- " print('=' * 70)\n",
980
- " print('Continuing search...')\n",
981
- " print('=' * 70)\n",
982
- "\n",
983
- " except KeyboardInterrupt:\n",
984
- " break\n",
985
- " \n",
986
- " except Exception as e:\n",
987
- " print('WARNING !!!')\n",
988
- " print('=' * 70)\n",
989
- " print('Error detected:', e)\n",
990
- " print('=' * 70)\n",
991
- " continue\n",
992
- "\n",
993
- "print('Done!')\n",
994
- "print('=' * 70)\n",
995
- "print('Best match:')\n",
996
- "print('=' * 70) \n",
997
- "print(matching_type.title(), 'distance ==', min(scores))\n",
998
- "print('LAMDa Index:', scores.index(min(scores)))\n",
999
- "print('LAMDa File Name:', midi_matrixes[scores.index(min(scores))][0])\n",
1000
- "print('=' * 70)\n",
1001
- "\n",
1002
- "#============================================\n",
1003
- "# MIDI rendering code\n",
1004
- "#============================================\n",
1005
- "\n",
1006
- "print('Rendering source MIDI...')\n",
1007
- "print('=' * 70)\n",
1008
- "\n",
1009
- "fn = midi_matrixes[scores.index(min(scores))][0]\n",
1010
- "\n",
1011
- "try:\n",
1012
- " fn_idx = [y[0] for y in LAMD_files_list].index(fn)\n",
1013
- "\n",
1014
- " f = LAMD_files_list[fn_idx][1]\n",
1015
- "\n",
1016
- " ms_score = MIDI.midi2ms_score(open(f, 'rb').read())\n",
1017
- "\n",
1018
- " itrack = 1\n",
1019
- " song_f = []\n",
1020
- "\n",
1021
- " while itrack < len(ms_score):\n",
1022
- " for event in ms_score[itrack]: \n",
1023
- " if event[0] == 'note':\n",
1024
- " song_f.append(event)\n",
1025
- " itrack += 1\n",
1026
- "\n",
1027
- " song_f.sort(key=lambda x: x[1])\n",
1028
- "\n",
1029
- " fname = f.split('.mid')[0]\n",
1030
- "\n",
1031
- " x = []\n",
1032
- " y =[]\n",
1033
- " c = []\n",
1034
- "\n",
1035
- " colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver', 'aqua', 'azure', 'bisque', 'coral']\n",
1036
- "\n",
1037
- " for s in song_f:\n",
1038
- " x.append(s[1] / 1000)\n",
1039
- " y.append(s[4])\n",
1040
- " c.append(colors[s[3]])\n",
1041
- "\n",
1042
- " if render_MIDI_to_audio:\n",
1043
- " FluidSynth(\"/usr/share/sounds/sf2/FluidR3_GM.sf2\", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))\n",
1044
- " display(Audio(str(fname + '.wav'), rate=16000))\n",
1045
- "\n",
1046
- " plt.figure(figsize=(14,5))\n",
1047
- " ax=plt.axes(title=fname)\n",
1048
- " ax.set_facecolor('black')\n",
1049
- "\n",
1050
- " plt.scatter(x,y, c=c)\n",
1051
- " plt.xlabel(\"Time\")\n",
1052
- " plt.ylabel(\"Pitch\")\n",
1053
- " plt.show()\n",
1054
- "\n",
1055
- "except:\n",
1056
- " pass\n",
1057
- "#==============================================\n",
1058
- "\n",
1059
- "if download_MIDI:\n",
1060
- " print('=' * 70)\n",
1061
- " print('Downloading MIDI file', str(fn) + '.mid')\n",
1062
- " files.download(f)\n",
1063
- " print('=' * 70)\n",
1064
- " \n",
1065
- "#============================================\n",
1066
- "\n",
1067
- "print('Top 100 matches')\n",
1068
- "print('=' * 70)\n",
1069
- "\n",
1070
- "top_matches = []\n",
1071
- "\n",
1072
- "for i in range(len(scores)):\n",
1073
- " top_matches.append([midi_matrixes[i][0], scores[i]])\n",
1074
- "\n",
1075
- "top_matches.sort(key=lambda x: x[1])\n",
1076
- "\n",
1077
- "for t in top_matches[:100]:\n",
1078
- " print(t)\n",
1079
- " \n",
1080
- "print('=' * 70)"
1081
- ],
1082
- "metadata": {
1083
- "cellView": "form",
1084
- "id": "pNQG6Qgfrlfi"
1085
- },
1086
- "execution_count": null,
1087
- "outputs": []
1088
- },
1089
- {
1090
- "cell_type": "code",
1091
- "source": [
1092
- "#@title MIDI Pitches Search (Fast)\n",
1093
  "\n",
1094
  "#@markdown NOTE: You can stop the search at any time to render partial results\n",
1095
  "\n",
@@ -1128,7 +742,7 @@
1128
  " max_p_count = p_counts[0][1]\n",
1129
  " trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)]\n",
1130
  " total_p_counts = sum([y[1] for y in trimmed_p_counts])\n",
1131
- " \n",
1132
  " if search_transposed_pitches:\n",
1133
  " search_pitches = mult_pitches_counts\n",
1134
  " else:\n",
@@ -1144,8 +758,8 @@
1144
  "\n",
1145
  " if add_timings_ratios:\n",
1146
  "\n",
1147
- " source_times = [avg_time, \n",
1148
- " median_time, \n",
1149
  " mode_time]\n",
1150
  "\n",
1151
  " match_times = meta_data[0][1][3][1]\n",
@@ -1197,7 +811,7 @@
1197
  "\n",
1198
  " same_pitches = set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts])\n",
1199
  " num_same_pitches = len(same_pitches)\n",
1200
- " \n",
1201
  " if num_same_pitches == len(trimmed_pitches_counts):\n",
1202
  " same_pitches_ratio = (num_same_pitches / len(trimmed_p_counts))\n",
1203
  " else:\n",
@@ -1243,36 +857,36 @@
1243
  "\n",
1244
  " if add_pitches_counts_ratios:\n",
1245
  " r_list.append(spcrat[0])\n",
1246
- " \n",
1247
  " if add_timings_ratios:\n",
1248
  " r_list.append(atrat[0])\n",
1249
- " \n",
1250
  " if add_durations_ratios:\n",
1251
  " r_list.append(adrat[0])\n",
1252
  "\n",
1253
  " ratios_list.append(r_list)\n",
1254
  "\n",
1255
  " #===================================================\n",
1256
- " \n",
1257
  " avg_ratios_list = []\n",
1258
  "\n",
1259
  " for r in ratios_list:\n",
1260
  " avg_ratios_list.append(sum(r) / len(r))\n",
1261
  "\n",
1262
  " #===================================================\n",
1263
- " \n",
1264
  " final_ratio = max(avg_ratios_list)\n",
1265
- " \n",
1266
  " if final_ratio > maximum_match_ratio_to_search_for:\n",
1267
  " final_ratio = 0\n",
1268
  "\n",
1269
  " final_ratios.append(final_ratio)\n",
1270
  "\n",
1271
  " #===================================================\n",
1272
- " \n",
1273
  " except KeyboardInterrupt:\n",
1274
  " break\n",
1275
- " \n",
1276
  " except Exception as e:\n",
1277
  " print('WARNING !!!')\n",
1278
  " print('=' * 70)\n",
@@ -1310,7 +924,7 @@
1310
  "song_f = []\n",
1311
  "\n",
1312
  "while itrack < len(ms_score):\n",
1313
- " for event in ms_score[itrack]: \n",
1314
  " if event[0] == 'note':\n",
1315
  " song_f.append(event)\n",
1316
  " itrack += 1\n",
@@ -1361,7 +975,7 @@
1361
  {
1362
  "cell_type": "code",
1363
  "source": [
1364
- "#@title MIDI Chords Search (Fast)\n",
1365
  "\n",
1366
  "#@markdown NOTE: You can stop the search at any time to render partial results\n",
1367
  "\n",
@@ -1387,19 +1001,19 @@
1387
  "\n",
1388
  " c_counts.sort(reverse = True, key = lambda x: x[0][1])\n",
1389
  " max_c_count = c_counts[0][1]\n",
1390
- " trimmed_c_counts = [y for y in c_counts if y[1] >= (max_c_count * chords_counts_cutoff_threshold_ratio)] \n",
1391
  " trimmed_c_counts.sort(reverse = True, key = lambda x: x[1])\n",
1392
- " \n",
1393
  " max_chords_count = ms_chords_counts[0][1]\n",
1394
- " trimmed_chords_counts = [y for y in ms_chords_counts if y[1] >= (max_chords_count * chords_counts_cutoff_threshold_ratio)] \n",
1395
  "\n",
1396
  " 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",
1397
- " \n",
1398
  " if num_same_chords == len(trimmed_chords_counts):\n",
1399
  " same_chords_ratio = (num_same_chords / len(trimmed_c_counts))\n",
1400
  " else:\n",
1401
  " same_chords_ratio = (num_same_chords / max(len(trimmed_c_counts), len(trimmed_chords_counts)))\n",
1402
- " \n",
1403
  " if skip_exact_matches:\n",
1404
  " if same_chords_ratio == 1:\n",
1405
  " same_chords_ratio = 0\n",
@@ -1408,10 +1022,10 @@
1408
  " same_chords_ratio = 0\n",
1409
  "\n",
1410
  " ratios.append(same_chords_ratio)\n",
1411
- " \n",
1412
  " except KeyboardInterrupt:\n",
1413
  " break\n",
1414
- " \n",
1415
  " except Exception as e:\n",
1416
  " print('WARNING !!!')\n",
1417
  " print('=' * 70)\n",
@@ -1449,7 +1063,7 @@
1449
  "song_f = []\n",
1450
  "\n",
1451
  "while itrack < len(ms_score):\n",
1452
- " for event in ms_score[itrack]: \n",
1453
  " if event[0] == 'note':\n",
1454
  " song_f.append(event)\n",
1455
  " itrack += 1\n",
@@ -1500,7 +1114,7 @@
1500
  {
1501
  "cell_type": "code",
1502
  "source": [
1503
- "#@title MIDI Patches Search (Fast)\n",
1504
  "\n",
1505
  "#@markdown NOTE: You can stop the search at any time to render partial results\n",
1506
  "\n",
@@ -1519,17 +1133,17 @@
1519
  "\n",
1520
  " try:\n",
1521
  "\n",
1522
- " p_list= d[1][12][1]\n",
1523
  "\n",
1524
  " num_same_patches = len(set(p_list) & set(patches_list))\n",
1525
- " \n",
1526
  " if len(set(p_list + patches_list)) > 0:\n",
1527
- " \n",
1528
  " if num_same_patches == len(patches_list):\n",
1529
  " same_patches_ratio = num_same_patches / len(p_list)\n",
1530
  " else:\n",
1531
  " same_patches_ratio = num_same_patches / max(len(p_list), len(patches_list))\n",
1532
- " \n",
1533
  " else:\n",
1534
  " same_patches_ratio = 0\n",
1535
  "\n",
@@ -1539,12 +1153,12 @@
1539
  "\n",
1540
  " if same_patches_ratio > maximum_match_ratio_to_search_for:\n",
1541
  " same_patches_ratio = 0\n",
1542
- " \n",
1543
  " ratios.append(same_patches_ratio)\n",
1544
- " \n",
1545
  " except KeyboardInterrupt:\n",
1546
  " break\n",
1547
- " \n",
1548
  " except Exception as e:\n",
1549
  " print('WARNING !!!')\n",
1550
  " print('=' * 70)\n",
@@ -1582,7 +1196,7 @@
1582
  "song_f = []\n",
1583
  "\n",
1584
  "while itrack < len(ms_score):\n",
1585
- " for event in ms_score[itrack]: \n",
1586
  " if event[0] == 'note':\n",
1587
  " song_f.append(event)\n",
1588
  " itrack += 1\n",
@@ -1641,10 +1255,10 @@
1641
  "md5_hash_MIDI_file_name = \"d9a7e1c6a375b8e560155a5977fc10f8\" #@param {type:\"string\"}\n",
1642
  "case_sensitive_search = False #@param {type:\"boolean\"}\n",
1643
  "\n",
1644
- "fields_to_search = ['track_name', \n",
1645
- " 'text_event', \n",
1646
- " 'lyric', \n",
1647
- " 'copyright_text_event', \n",
1648
  " 'marker',\n",
1649
  " 'text_event_08',\n",
1650
  " 'text_event_09',\n",
@@ -1674,12 +1288,12 @@
1674
  " pprint.pprint(['Result:', d[1][:16]], compact = True)\n",
1675
  " print('=' * 70)\n",
1676
  " break\n",
1677
- " \n",
1678
  " except KeyboardInterrupt:\n",
1679
  " print('Ending search...')\n",
1680
  " print('=' * 70)\n",
1681
  " break\n",
1682
- " \n",
1683
  " except Exception as e:\n",
1684
  " print('WARNING !!!')\n",
1685
  " print('=' * 70)\n",
@@ -1708,7 +1322,7 @@
1708
  " print('-' * 70)\n",
1709
  " pprint.pprint(['Result:', dd[2][:16]], compact = True)\n",
1710
  " print('=' * 70)\n",
1711
- " \n",
1712
  " else:\n",
1713
  " if str(search_query).lower() in str(dd[2]).lower():\n",
1714
  " print('Found!')\n",
@@ -1718,12 +1332,12 @@
1718
  " print('-' * 70)\n",
1719
  " pprint.pprint(['Result:', dd[2][:16]], compact = True)\n",
1720
  " print('=' * 70)\n",
1721
- " \n",
1722
  " except KeyboardInterrupt:\n",
1723
  " print('Ending search...')\n",
1724
  " print('=' * 70)\n",
1725
  " break\n",
1726
- " \n",
1727
  " except:\n",
1728
  " print('Ending search...')\n",
1729
  " print('=' * 70)\n",
@@ -1775,9 +1389,9 @@
1775
  " fn = md5_hash_MIDI_file_name\n",
1776
  " fn_idx = [y[0] for y in LAMD_files_list].index(fn)\n",
1777
  " f = LAMD_files_list[fn_idx][1]\n",
1778
- " \n",
1779
  " print('Using md5 hash filename')\n",
1780
- " \n",
1781
  " print('=' * 70)\n",
1782
  " print('Rendering MIDI...')\n",
1783
  " print('=' * 70)\n",
@@ -1788,7 +1402,7 @@
1788
  " song_f = []\n",
1789
  "\n",
1790
  " while itrack < len(ms_score):\n",
1791
- " for event in ms_score[itrack]: \n",
1792
  " if event[0] == 'note':\n",
1793
  " song_f.append(event)\n",
1794
  " itrack += 1\n",
@@ -1996,11 +1610,11 @@
1996
  " score = MIDI.midi2score(open(f, 'rb').read())\n",
1997
  "\n",
1998
  " events_matrix = []\n",
1999
- " \n",
2000
  " itrack = 1\n",
2001
  "\n",
2002
  " while itrack < len(score):\n",
2003
- " for event in score[itrack]: \n",
2004
  " events_matrix.append(event)\n",
2005
  " itrack += 1\n",
2006
  "\n",
@@ -2008,7 +1622,7 @@
2008
  " events_matrix.sort(key=lambda x: x[1])\n",
2009
  "\n",
2010
  " if len(events_matrix) > 0:\n",
2011
- " \n",
2012
  " #=======================================================\n",
2013
  " # INSERT YOUR CUSTOM ANAYLSIS CODE RIGHT HERE\n",
2014
  " #=======================================================\n",
@@ -2024,7 +1638,7 @@
2024
  "\n",
2025
  " except KeyboardInterrupt:\n",
2026
  " print('Saving current progress and quitting...')\n",
2027
- " break \n",
2028
  "\n",
2029
  " except Exception as ex:\n",
2030
  " print('WARNING !!!')\n",
@@ -2042,7 +1656,7 @@
2042
  "print('=' * 70)\n",
2043
  "print('Total good processed MIDI files:', files_count)\n",
2044
  "print('=' * 70)\n",
2045
- "print('Done!') \n",
2046
  "print('=' * 70)"
2047
  ],
2048
  "metadata": {
 
11
  "id": "SiTIpPjArIyr"
12
  },
13
  "source": [
14
+ "# Los Angeles MIDI Dataset: Search and Explore (ver. 3.0)\n",
15
  "\n",
16
  "***\n",
17
  "\n",
 
145
  "\n",
146
  "#================================================\n",
147
  "\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  "%cd /content/Los-Angeles-MIDI-Dataset/TOTALS\n",
149
  "\n",
150
  "print('=' * 70)\n",
 
175
  "meta_data = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/META-DATA/LAMDa_META_DATA.pickle', 'rb'))\n",
176
  "print('Done!')\n",
177
  "print('=' * 70)\n",
 
 
 
 
178
  "print('Loading LAMDa TOTALS...')\n",
179
  "totals = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/TOTALS/LAMDa_TOTALS.pickle', 'rb'))\n",
180
  "print('Done!')\n",
 
206
  "print('Downloading Los Angeles MIDI Dataset...Please wait...')\n",
207
  "print('=' * 70)\n",
208
  "\n",
209
+ "hf_hub_download(repo_id='projectlosangeles/Los-Angeles-MIDI-Dataset',\n",
210
+ " filename='Los-Angeles-MIDI-Dataset-Ver-3-0-CC-BY-NC-SA.zip',\n",
211
  " repo_type=\"dataset\",\n",
212
+ " local_dir='/content/LAMD',\n",
213
  " local_dir_use_symlinks=False)\n",
214
  "print('=' * 70)\n",
215
  "print('Done! Enjoy! :)')\n",
 
230
  "\n",
231
  "print('=' * 70)\n",
232
  "print('Unzipping Los Angeles MIDI Dataset...Please wait...')\n",
233
+ "!unzip 'Los-Angeles-MIDI-Dataset-Ver-3-0-CC-BY-NC-SA.zip'\n",
234
  "print('=' * 70)\n",
235
  "\n",
236
  "print('Done! Enjoy! :)')\n",
 
295
  "#@title Plot totals from MIDI matrixes (legacy)\n",
296
  "\n",
297
  "cos_sim = pairwise.cosine_similarity(\n",
298
+ " totals[0][0][4]\n",
299
  " )\n",
300
  "plt.figure(figsize=(8, 8))\n",
301
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
 
308
  "plt.plot()\n",
309
  "\n",
310
  "cos_sim = pairwise.cosine_similarity(\n",
311
+ " totals[0][0][5]\n",
312
  " )\n",
313
  "plt.figure(figsize=(8, 8))\n",
314
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
 
321
  "plt.plot()\n",
322
  "\n",
323
  "cos_sim = pairwise.cosine_similarity(\n",
324
+ " totals[0][0][6]\n",
325
  " )\n",
326
  "plt.figure(figsize=(8, 8))\n",
327
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
 
334
  "plt.plot()\n",
335
  "\n",
336
  "cos_sim = pairwise.cosine_similarity(\n",
337
+ " totals[0][0][7]\n",
338
  " )\n",
339
  "plt.figure(figsize=(8, 8))\n",
340
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
 
347
  "plt.plot()\n",
348
  "\n",
349
  "cos_sim = pairwise.cosine_similarity(\n",
350
+ " totals[0][0][8]\n",
351
  " )\n",
352
  "plt.figure(figsize=(8, 8))\n",
353
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
 
360
  "plt.plot()\n",
361
  "\n",
362
  "cos_sim = pairwise.cosine_similarity(\n",
363
+ " totals[0][0][9]\n",
364
  " )\n",
365
  "plt.figure(figsize=(8, 8))\n",
366
  "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n",
 
398
  "plt.plot(y, pitches_counts_totals)\n",
399
  "\n",
400
  "plt.title('MIDI Instruments Pitches')\n",
401
+ "plt.xlabel(\"Pitch\")\n",
402
+ "plt.ylabel(\"Count\")\n",
403
  "plt.tight_layout()\n",
404
  "plt.plot()\n",
405
  "\n",
 
416
  " x += 1\n",
417
  "\n",
418
  "cos_sim = pairwise.cosine_similarity(\n",
419
+ " sim_mat\n",
420
  " )\n",
421
  "plt.figure(figsize=(8, 8))\n",
422
  "plt.imshow(sim_mat, cmap=\"inferno\", interpolation=\"none\")\n",
423
  "im_ratio = 1\n",
424
  "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n",
425
+ "plt.title('MIDI Drums Pitches')\n",
426
+ "plt.xlabel(\"Pitch\")\n",
427
+ "plt.ylabel(\"Count\")\n",
428
  "plt.tight_layout()\n",
429
  "plt.plot()\n",
430
  "\n",
 
443
  "plt.plot(y, pitches_counts_totals)\n",
444
  "\n",
445
  "plt.title('MIDI Drums Pitches')\n",
446
+ "plt.xlabel(\"Pitch\")\n",
447
+ "plt.ylabel(\"Count\")\n",
448
  "plt.tight_layout()\n",
449
  "plt.plot()\n",
450
  "\n",
 
461
  " x += 1\n",
462
  "\n",
463
  "cos_sim = pairwise.cosine_similarity(\n",
464
+ " sim_mat\n",
465
  " )\n",
466
  "plt.figure(figsize=(8, 8))\n",
467
  "plt.imshow(sim_mat, cmap=\"inferno\", interpolation=\"none\")\n",
468
  "im_ratio = 1\n",
469
  "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n",
470
  "plt.title('MIDI Drums Pitches')\n",
471
+ "plt.xlabel(\"Pitch\")\n",
472
+ "plt.ylabel(\"Count\")\n",
473
  "plt.tight_layout()\n",
474
  "plt.plot()\n",
475
  "\n",
 
479
  "\n",
480
  "\n",
481
  "for m in tqdm(meta_data):\n",
482
+ " for mm in m[1][12][1]:\n",
483
  " patches_counts_totals[mm[0]] += mm[1]\n",
484
  "\n",
485
  "\n",
 
488
  "plt.plot(y, patches_counts_totals[:128])\n",
489
  "\n",
490
  "plt.title('MIDI Patches')\n",
491
+ "plt.xlabel(\"Patch\")\n",
492
+ "plt.ylabel('Count')\n",
493
  "plt.tight_layout()\n",
494
  "plt.plot()"
495
  ],
 
524
  "print('=' * 70)\n",
525
  "print('Loading MIDI file...')\n",
526
  "\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
527
  "#==================================================\n",
528
  "\n",
529
  "score = MIDI.midi2score(open(f, 'rb').read())\n",
 
533
  "track_count = 0\n",
534
  "\n",
535
  "for s in score:\n",
536
+ "\n",
537
  " if track_count > 0:\n",
538
  " track = s\n",
539
  " track.sort(key=lambda x: x[1])\n",
 
542
  " midi_ticks = s\n",
543
  "\n",
544
  " track_count += 1\n",
545
+ "\n",
546
  "events_matrix.sort(key=lambda x: x[1])\n",
547
  "\n",
548
  "mult_pitches_counts = []\n",
 
565
  "\n",
566
  " pitches_counts = [[y[0],y[1]] for y in Counter([y[4] for y in events_matrix1 if y[0] == 'note']).most_common()]\n",
567
  " pitches_counts.sort(key=lambda x: x[0], reverse=True)\n",
568
+ "\n",
569
  " mult_pitches_counts.append(pitches_counts)\n",
570
  "\n",
571
  "patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))\n",
 
580
  "itrack1 = 1\n",
581
  "\n",
582
  "while itrack1 < len(ms_score):\n",
583
+ " for event in ms_score[itrack1]:\n",
584
  " if event[0] == 'note':\n",
585
  " ms_events_matrix.append(event)\n",
586
  " itrack1 += 1\n",
 
605
  " cho.append(e[4] % 12)\n",
606
  "\n",
607
  " pe = e\n",
608
+ "\n",
609
  "if len(cho) > 0:\n",
610
  " chords.append(sorted(cho))\n",
611
  "\n",
 
619
  " times.append((e[1]-pt))\n",
620
  " start = False\n",
621
  " pt = e[1]\n",
622
+ "\n",
623
  "durs = [e[2] for e in ms_events_matrix]\n",
624
  "vels = [e[5] for e in ms_events_matrix]\n",
625
  "\n",
 
651
  "song_f = []\n",
652
  "\n",
653
  "while itrack < len(ms_score):\n",
654
+ " for event in ms_score[itrack]:\n",
655
  " if event[0] == 'note':\n",
656
  " song_f.append(event)\n",
657
  " itrack += 1\n",
 
703
  {
704
  "cell_type": "code",
705
  "source": [
706
+ "#@title MIDI Pitches Search\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707
  "\n",
708
  "#@markdown NOTE: You can stop the search at any time to render partial results\n",
709
  "\n",
 
742
  " max_p_count = p_counts[0][1]\n",
743
  " trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)]\n",
744
  " total_p_counts = sum([y[1] for y in trimmed_p_counts])\n",
745
+ "\n",
746
  " if search_transposed_pitches:\n",
747
  " search_pitches = mult_pitches_counts\n",
748
  " else:\n",
 
758
  "\n",
759
  " if add_timings_ratios:\n",
760
  "\n",
761
+ " source_times = [avg_time,\n",
762
+ " median_time,\n",
763
  " mode_time]\n",
764
  "\n",
765
  " match_times = meta_data[0][1][3][1]\n",
 
811
  "\n",
812
  " same_pitches = set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts])\n",
813
  " num_same_pitches = len(same_pitches)\n",
814
+ "\n",
815
  " if num_same_pitches == len(trimmed_pitches_counts):\n",
816
  " same_pitches_ratio = (num_same_pitches / len(trimmed_p_counts))\n",
817
  " else:\n",
 
857
  "\n",
858
  " if add_pitches_counts_ratios:\n",
859
  " r_list.append(spcrat[0])\n",
860
+ "\n",
861
  " if add_timings_ratios:\n",
862
  " r_list.append(atrat[0])\n",
863
+ "\n",
864
  " if add_durations_ratios:\n",
865
  " r_list.append(adrat[0])\n",
866
  "\n",
867
  " ratios_list.append(r_list)\n",
868
  "\n",
869
  " #===================================================\n",
870
+ "\n",
871
  " avg_ratios_list = []\n",
872
  "\n",
873
  " for r in ratios_list:\n",
874
  " avg_ratios_list.append(sum(r) / len(r))\n",
875
  "\n",
876
  " #===================================================\n",
877
+ "\n",
878
  " final_ratio = max(avg_ratios_list)\n",
879
+ "\n",
880
  " if final_ratio > maximum_match_ratio_to_search_for:\n",
881
  " final_ratio = 0\n",
882
  "\n",
883
  " final_ratios.append(final_ratio)\n",
884
  "\n",
885
  " #===================================================\n",
886
+ "\n",
887
  " except KeyboardInterrupt:\n",
888
  " break\n",
889
+ "\n",
890
  " except Exception as e:\n",
891
  " print('WARNING !!!')\n",
892
  " print('=' * 70)\n",
 
924
  "song_f = []\n",
925
  "\n",
926
  "while itrack < len(ms_score):\n",
927
+ " for event in ms_score[itrack]:\n",
928
  " if event[0] == 'note':\n",
929
  " song_f.append(event)\n",
930
  " itrack += 1\n",
 
975
  {
976
  "cell_type": "code",
977
  "source": [
978
+ "#@title MIDI Chords Search\n",
979
  "\n",
980
  "#@markdown NOTE: You can stop the search at any time to render partial results\n",
981
  "\n",
 
1001
  "\n",
1002
  " c_counts.sort(reverse = True, key = lambda x: x[0][1])\n",
1003
  " max_c_count = c_counts[0][1]\n",
1004
+ " trimmed_c_counts = [y for y in c_counts if y[1] >= (max_c_count * chords_counts_cutoff_threshold_ratio)]\n",
1005
  " trimmed_c_counts.sort(reverse = True, key = lambda x: x[1])\n",
1006
+ "\n",
1007
  " max_chords_count = ms_chords_counts[0][1]\n",
1008
+ " trimmed_chords_counts = [y for y in ms_chords_counts if y[1] >= (max_chords_count * chords_counts_cutoff_threshold_ratio)]\n",
1009
  "\n",
1010
  " 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",
1011
+ "\n",
1012
  " if num_same_chords == len(trimmed_chords_counts):\n",
1013
  " same_chords_ratio = (num_same_chords / len(trimmed_c_counts))\n",
1014
  " else:\n",
1015
  " same_chords_ratio = (num_same_chords / max(len(trimmed_c_counts), len(trimmed_chords_counts)))\n",
1016
+ "\n",
1017
  " if skip_exact_matches:\n",
1018
  " if same_chords_ratio == 1:\n",
1019
  " same_chords_ratio = 0\n",
 
1022
  " same_chords_ratio = 0\n",
1023
  "\n",
1024
  " ratios.append(same_chords_ratio)\n",
1025
+ "\n",
1026
  " except KeyboardInterrupt:\n",
1027
  " break\n",
1028
+ "\n",
1029
  " except Exception as e:\n",
1030
  " print('WARNING !!!')\n",
1031
  " print('=' * 70)\n",
 
1063
  "song_f = []\n",
1064
  "\n",
1065
  "while itrack < len(ms_score):\n",
1066
+ " for event in ms_score[itrack]:\n",
1067
  " if event[0] == 'note':\n",
1068
  " song_f.append(event)\n",
1069
  " itrack += 1\n",
 
1114
  {
1115
  "cell_type": "code",
1116
  "source": [
1117
+ "#@title MIDI Patches Search\n",
1118
  "\n",
1119
  "#@markdown NOTE: You can stop the search at any time to render partial results\n",
1120
  "\n",
 
1133
  "\n",
1134
  " try:\n",
1135
  "\n",
1136
+ " p_list= d[1][11][1]\n",
1137
  "\n",
1138
  " num_same_patches = len(set(p_list) & set(patches_list))\n",
1139
+ "\n",
1140
  " if len(set(p_list + patches_list)) > 0:\n",
1141
+ "\n",
1142
  " if num_same_patches == len(patches_list):\n",
1143
  " same_patches_ratio = num_same_patches / len(p_list)\n",
1144
  " else:\n",
1145
  " same_patches_ratio = num_same_patches / max(len(p_list), len(patches_list))\n",
1146
+ "\n",
1147
  " else:\n",
1148
  " same_patches_ratio = 0\n",
1149
  "\n",
 
1153
  "\n",
1154
  " if same_patches_ratio > maximum_match_ratio_to_search_for:\n",
1155
  " same_patches_ratio = 0\n",
1156
+ "\n",
1157
  " ratios.append(same_patches_ratio)\n",
1158
+ "\n",
1159
  " except KeyboardInterrupt:\n",
1160
  " break\n",
1161
+ "\n",
1162
  " except Exception as e:\n",
1163
  " print('WARNING !!!')\n",
1164
  " print('=' * 70)\n",
 
1196
  "song_f = []\n",
1197
  "\n",
1198
  "while itrack < len(ms_score):\n",
1199
+ " for event in ms_score[itrack]:\n",
1200
  " if event[0] == 'note':\n",
1201
  " song_f.append(event)\n",
1202
  " itrack += 1\n",
 
1255
  "md5_hash_MIDI_file_name = \"d9a7e1c6a375b8e560155a5977fc10f8\" #@param {type:\"string\"}\n",
1256
  "case_sensitive_search = False #@param {type:\"boolean\"}\n",
1257
  "\n",
1258
+ "fields_to_search = ['track_name',\n",
1259
+ " 'text_event',\n",
1260
+ " 'lyric',\n",
1261
+ " 'copyright_text_event',\n",
1262
  " 'marker',\n",
1263
  " 'text_event_08',\n",
1264
  " 'text_event_09',\n",
 
1288
  " pprint.pprint(['Result:', d[1][:16]], compact = True)\n",
1289
  " print('=' * 70)\n",
1290
  " break\n",
1291
+ "\n",
1292
  " except KeyboardInterrupt:\n",
1293
  " print('Ending search...')\n",
1294
  " print('=' * 70)\n",
1295
  " break\n",
1296
+ "\n",
1297
  " except Exception as e:\n",
1298
  " print('WARNING !!!')\n",
1299
  " print('=' * 70)\n",
 
1322
  " print('-' * 70)\n",
1323
  " pprint.pprint(['Result:', dd[2][:16]], compact = True)\n",
1324
  " print('=' * 70)\n",
1325
+ "\n",
1326
  " else:\n",
1327
  " if str(search_query).lower() in str(dd[2]).lower():\n",
1328
  " print('Found!')\n",
 
1332
  " print('-' * 70)\n",
1333
  " pprint.pprint(['Result:', dd[2][:16]], compact = True)\n",
1334
  " print('=' * 70)\n",
1335
+ "\n",
1336
  " except KeyboardInterrupt:\n",
1337
  " print('Ending search...')\n",
1338
  " print('=' * 70)\n",
1339
  " break\n",
1340
+ "\n",
1341
  " except:\n",
1342
  " print('Ending search...')\n",
1343
  " print('=' * 70)\n",
 
1389
  " fn = md5_hash_MIDI_file_name\n",
1390
  " fn_idx = [y[0] for y in LAMD_files_list].index(fn)\n",
1391
  " f = LAMD_files_list[fn_idx][1]\n",
1392
+ "\n",
1393
  " print('Using md5 hash filename')\n",
1394
+ "\n",
1395
  " print('=' * 70)\n",
1396
  " print('Rendering MIDI...')\n",
1397
  " print('=' * 70)\n",
 
1402
  " song_f = []\n",
1403
  "\n",
1404
  " while itrack < len(ms_score):\n",
1405
+ " for event in ms_score[itrack]:\n",
1406
  " if event[0] == 'note':\n",
1407
  " song_f.append(event)\n",
1408
  " itrack += 1\n",
 
1610
  " score = MIDI.midi2score(open(f, 'rb').read())\n",
1611
  "\n",
1612
  " events_matrix = []\n",
1613
+ "\n",
1614
  " itrack = 1\n",
1615
  "\n",
1616
  " while itrack < len(score):\n",
1617
+ " for event in score[itrack]:\n",
1618
  " events_matrix.append(event)\n",
1619
  " itrack += 1\n",
1620
  "\n",
 
1622
  " events_matrix.sort(key=lambda x: x[1])\n",
1623
  "\n",
1624
  " if len(events_matrix) > 0:\n",
1625
+ "\n",
1626
  " #=======================================================\n",
1627
  " # INSERT YOUR CUSTOM ANAYLSIS CODE RIGHT HERE\n",
1628
  " #=======================================================\n",
 
1638
  "\n",
1639
  " except KeyboardInterrupt:\n",
1640
  " print('Saving current progress and quitting...')\n",
1641
+ " break\n",
1642
  "\n",
1643
  " except Exception as ex:\n",
1644
  " print('WARNING !!!')\n",
 
1656
  "print('=' * 70)\n",
1657
  "print('Total good processed MIDI files:', files_count)\n",
1658
  "print('=' * 70)\n",
1659
+ "print('Done!')\n",
1660
  "print('=' * 70)"
1661
  ],
1662
  "metadata": {
Master_MIDI_Dataset_Search_and_Filter.ipynb CHANGED
@@ -11,7 +11,7 @@
11
  "id": "SiTIpPjArIyr"
12
  },
13
  "source": [
14
- "# Master MIDI Dataset Search and Filter (ver. 1.0)\n",
15
  "\n",
16
  "***\n",
17
  "\n",
@@ -134,10 +134,10 @@
134
  "print('Downloading Los Angeles MIDI Dataset...Please wait...')\n",
135
  "print('=' * 70)\n",
136
  "\n",
137
- "hf_hub_download(repo_id='projectlosangeles/Los-Angeles-MIDI-Dataset', \n",
138
- " filename='Los-Angeles-MIDI-Dataset-Ver-2-0-CC-BY-NC-SA.zip',\n",
139
  " repo_type=\"dataset\",\n",
140
- " local_dir='/content/Main-MIDI-Dataset', \n",
141
  " local_dir_use_symlinks=False)\n",
142
  "print('=' * 70)\n",
143
  "print('Done! Enjoy! :)')\n",
@@ -158,7 +158,7 @@
158
  "\n",
159
  "print('=' * 70)\n",
160
  "print('Unzipping Los Angeles MIDI Dataset...Please wait...')\n",
161
- "!unzip 'Los-Angeles-MIDI-Dataset-Ver-2-0-CC-BY-NC-SA.zip'\n",
162
  "print('=' * 70)\n",
163
  "\n",
164
  "print('Done! Enjoy! :)')\n",
@@ -298,7 +298,7 @@
298
  "\n",
299
  "for f in filez:\n",
300
  " try:\n",
301
- " \n",
302
  " input_files_count += 1\n",
303
  "\n",
304
  " fn = os.path.basename(f)\n",
@@ -309,88 +309,10 @@
309
  "\n",
310
  " print('Processing MIDI File #', files_count+1, 'out of', len(filez))\n",
311
  " print('MIDI file name', fn)\n",
312
- " print('-' * 70) \n",
313
  "\n",
314
  " #=======================================================\n",
315
  "\n",
316
- " score = TMIDIX.midi2ms_score(open(f, 'rb').read())\n",
317
- "\n",
318
- " events_matrix = []\n",
319
- "\n",
320
- " itrack = 1\n",
321
- "\n",
322
- " while itrack < len(score):\n",
323
- " for event in score[itrack]: \n",
324
- " events_matrix.append(event)\n",
325
- " itrack += 1\n",
326
- "\n",
327
- " # Sorting...\n",
328
- " events_matrix.sort(key=lambda x: x[1])\n",
329
- "\n",
330
- " # recalculating timings\n",
331
- " for e in events_matrix:\n",
332
- " e[1] = int(e[1] / 10)\n",
333
- " if e[0] == 'note':\n",
334
- " e[2] = int(e[2] / 20)\n",
335
- "\n",
336
- " # final processing...\n",
337
- "\n",
338
- " melody_chords = []\n",
339
- "\n",
340
- " patches = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
341
- "\n",
342
- " pe = events_matrix[0]\n",
343
- " for e in events_matrix:\n",
344
- "\n",
345
- " if e[0] == 'note':\n",
346
- " # ['note', start_time, duration, channel, note, velocity]\n",
347
- " time = max(0, min(255, e[1]-pe[1]))\n",
348
- " duration = max(1, min(255, e[2]))\n",
349
- " channel = max(0, min(15, e[3]))\n",
350
- "\n",
351
- " if e[3] != 9:\n",
352
- " instrument = max(0, min(127, patches[e[3]]))\n",
353
- " else:\n",
354
- " instrument = max(128, min(255, patches[e[3]]+128))\n",
355
- "\n",
356
- " if e[3] != 9:\n",
357
- "\n",
358
- " pitch = max(1, min(127, e[4]))\n",
359
- " else:\n",
360
- " pitch = max(129, min(255, e[4]+128))\n",
361
- "\n",
362
- " if e[3] != 9:\n",
363
- " velocity = max(1, min(127, e[5]))\n",
364
- " else:\n",
365
- " velocity = max(129, min(255, e[5]+128))\n",
366
- "\n",
367
- " melody_chords.append([time, duration, channel, instrument, pitch, velocity])\n",
368
- "\n",
369
- " if e[0] == 'patch_change':\n",
370
- " # ['patch_change', dtime, channel, patch]\n",
371
- " time = max(0, min(127, e[1]-pe[1]))\n",
372
- " channel = max(0, min(15, e[2]))\n",
373
- " patch = max(0, min(127, e[3]))\n",
374
- "\n",
375
- " patches[channel] = patch\n",
376
- "\n",
377
- " pe = e # Previous event\n",
378
- "\n",
379
- " MATRIX = [[0]*256 for i in range(38)]\n",
380
- "\n",
381
- " for m in melody_chords:\n",
382
- "\n",
383
- " MATRIX[0][m[0]] += 1\n",
384
- " MATRIX[1][m[1]] += 1\n",
385
- " MATRIX[2][m[2]] += 1 \n",
386
- " MATRIX[3][m[3]] += 1\n",
387
- " MATRIX[4][m[4]] += 1\n",
388
- " MATRIX[5][m[5]] += 1\n",
389
- " MATRIX[m[2]+6][m[3]] += 1\n",
390
- " MATRIX[m[2]+22][m[4]] += 1\n",
391
- "\n",
392
- " #==================================================\n",
393
- "\n",
394
  " score = TMIDIX.midi2score(open(f, 'rb').read())\n",
395
  "\n",
396
  " events_matrix = []\n",
@@ -398,7 +320,7 @@
398
  " track_count = 0\n",
399
  "\n",
400
  " for s in score:\n",
401
- " \n",
402
  " if track_count > 0:\n",
403
  " track = s\n",
404
  " track.sort(key=lambda x: x[1])\n",
@@ -407,7 +329,7 @@
407
  " midi_ticks = s\n",
408
  "\n",
409
  " track_count += 1\n",
410
- " \n",
411
  " events_matrix.sort(key=lambda x: x[1])\n",
412
  "\n",
413
  " mult_pitches_counts = []\n",
@@ -430,7 +352,7 @@
430
  "\n",
431
  " pitches_counts = [[y[0],y[1]] for y in Counter([y[4] for y in events_matrix1 if y[0] == 'note']).most_common()]\n",
432
  " pitches_counts.sort(key=lambda x: x[0], reverse=True)\n",
433
- " \n",
434
  " mult_pitches_counts.append(pitches_counts)\n",
435
  "\n",
436
  " patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))\n",
@@ -444,7 +366,7 @@
444
  " itrack1 = 1\n",
445
  "\n",
446
  " while itrack1 < len(ms_score):\n",
447
- " for event in ms_score[itrack1]: \n",
448
  " if event[0] == 'note':\n",
449
  " ms_events_matrix.append(event)\n",
450
  " itrack1 += 1\n",
@@ -469,7 +391,7 @@
469
  " cho.append(e[4] % 12)\n",
470
  "\n",
471
  " pe = e\n",
472
- " \n",
473
  " if len(cho) > 0:\n",
474
  " chords.append(sorted(cho))\n",
475
  "\n",
@@ -483,7 +405,7 @@
483
  " times.append((e[1]-pt))\n",
484
  " start = False\n",
485
  " pt = e[1]\n",
486
- " \n",
487
  " durs = [e[2] for e in ms_events_matrix]\n",
488
  " vels = [e[5] for e in ms_events_matrix]\n",
489
  "\n",
@@ -510,7 +432,7 @@
510
  " max_p_count = p_counts[0][1]\n",
511
  " trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)]\n",
512
  " total_p_counts = sum([y[1] for y in trimmed_p_counts])\n",
513
- " \n",
514
  " if search_transposed_pitches:\n",
515
  " search_pitches = mult_pitches_counts\n",
516
  " else:\n",
@@ -526,8 +448,8 @@
526
  "\n",
527
  " if add_timings_ratios:\n",
528
  "\n",
529
- " source_times = [avg_time, \n",
530
- " median_time, \n",
531
  " mode_time]\n",
532
  "\n",
533
  " match_times = meta_data[0][1][3][1]\n",
@@ -579,12 +501,12 @@
579
  "\n",
580
  " same_pitches = set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts])\n",
581
  " num_same_pitches = len(same_pitches)\n",
582
- " \n",
583
  " if num_same_pitches == len(trimmed_pitches_counts):\n",
584
  " same_pitches_ratio = (num_same_pitches / len(trimmed_p_counts))\n",
585
  " else:\n",
586
  " same_pitches_ratio = (num_same_pitches / max(len(trimmed_p_counts), len(trimmed_pitches_counts)))\n",
587
- " \n",
588
  " if skip_exact_matches:\n",
589
  " if same_pitches_ratio == 1:\n",
590
  " same_pitches_ratio = 0\n",
@@ -625,26 +547,26 @@
625
  "\n",
626
  " if add_pitches_counts_ratios:\n",
627
  " r_list.append(spcrat[0])\n",
628
- " \n",
629
  " if add_timings_ratios:\n",
630
  " r_list.append(atrat[0])\n",
631
- " \n",
632
  " if add_durations_ratios:\n",
633
  " r_list.append(adrat[0])\n",
634
  "\n",
635
  " ratios_list.append(r_list)\n",
636
  "\n",
637
  " #===================================================\n",
638
- " \n",
639
  " avg_ratios_list = []\n",
640
  "\n",
641
  " for r in ratios_list:\n",
642
  " avg_ratios_list.append(sum(r) / len(r))\n",
643
  "\n",
644
  " #===================================================\n",
645
- " \n",
646
  " final_ratio = max(avg_ratios_list)\n",
647
- " \n",
648
  " if final_ratio > maximum_match_ratio_to_search_for:\n",
649
  " final_ratio = 0\n",
650
  "\n",
@@ -670,7 +592,7 @@
670
  " ff = LAMD_files_list[ffn_idx][1]\n",
671
  "\n",
672
  " #=======================================================\n",
673
- " \n",
674
  " dir_str = str(fn1)\n",
675
  " copy_path = '/content/Output-MIDI-Dataset/'+dir_str\n",
676
  " if not os.path.exists(copy_path):\n",
@@ -679,11 +601,11 @@
679
  " fff = str(m * 100) + '_' + ffn + '.mid'\n",
680
  "\n",
681
  " shutil.copy2(ff, copy_path+'/'+fff)\n",
682
- " \n",
683
  " shutil.copy2(f, copy_path+'/'+fn)\n",
684
  "\n",
685
  " #=======================================================\n",
686
- " \n",
687
  " print('Done!')\n",
688
  " print('=' * 70)\n",
689
  "\n",
@@ -691,12 +613,12 @@
691
  "\n",
692
  " # Processed files counter\n",
693
  " files_count += 1\n",
694
- " \n",
695
  " except KeyboardInterrupt:\n",
696
  " print('Quitting...')\n",
697
  " print('Total number of processed MIDI files', files_count)\n",
698
  " print('=' * 70)\n",
699
- " break \n",
700
  "\n",
701
  " except Exception as ex:\n",
702
  " print('WARNING !!!')\n",
 
11
  "id": "SiTIpPjArIyr"
12
  },
13
  "source": [
14
+ "# Master MIDI Dataset Search and Filter (ver. 3.0)\n",
15
  "\n",
16
  "***\n",
17
  "\n",
 
134
  "print('Downloading Los Angeles MIDI Dataset...Please wait...')\n",
135
  "print('=' * 70)\n",
136
  "\n",
137
+ "hf_hub_download(repo_id='projectlosangeles/Los-Angeles-MIDI-Dataset',\n",
138
+ " filename='Los-Angeles-MIDI-Dataset-Ver-3-0-CC-BY-NC-SA.zip',\n",
139
  " repo_type=\"dataset\",\n",
140
+ " local_dir='/content/Main-MIDI-Dataset',\n",
141
  " local_dir_use_symlinks=False)\n",
142
  "print('=' * 70)\n",
143
  "print('Done! Enjoy! :)')\n",
 
158
  "\n",
159
  "print('=' * 70)\n",
160
  "print('Unzipping Los Angeles MIDI Dataset...Please wait...')\n",
161
+ "!unzip 'Los-Angeles-MIDI-Dataset-Ver-3-0-CC-BY-NC-SA.zip'\n",
162
  "print('=' * 70)\n",
163
  "\n",
164
  "print('Done! Enjoy! :)')\n",
 
298
  "\n",
299
  "for f in filez:\n",
300
  " try:\n",
301
+ "\n",
302
  " input_files_count += 1\n",
303
  "\n",
304
  " fn = os.path.basename(f)\n",
 
309
  "\n",
310
  " print('Processing MIDI File #', files_count+1, 'out of', len(filez))\n",
311
  " print('MIDI file name', fn)\n",
312
+ " print('-' * 70)\n",
313
  "\n",
314
  " #=======================================================\n",
315
  "\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
  " score = TMIDIX.midi2score(open(f, 'rb').read())\n",
317
  "\n",
318
  " events_matrix = []\n",
 
320
  " track_count = 0\n",
321
  "\n",
322
  " for s in score:\n",
323
+ "\n",
324
  " if track_count > 0:\n",
325
  " track = s\n",
326
  " track.sort(key=lambda x: x[1])\n",
 
329
  " midi_ticks = s\n",
330
  "\n",
331
  " track_count += 1\n",
332
+ "\n",
333
  " events_matrix.sort(key=lambda x: x[1])\n",
334
  "\n",
335
  " mult_pitches_counts = []\n",
 
352
  "\n",
353
  " pitches_counts = [[y[0],y[1]] for y in Counter([y[4] for y in events_matrix1 if y[0] == 'note']).most_common()]\n",
354
  " pitches_counts.sort(key=lambda x: x[0], reverse=True)\n",
355
+ "\n",
356
  " mult_pitches_counts.append(pitches_counts)\n",
357
  "\n",
358
  " patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))\n",
 
366
  " itrack1 = 1\n",
367
  "\n",
368
  " while itrack1 < len(ms_score):\n",
369
+ " for event in ms_score[itrack1]:\n",
370
  " if event[0] == 'note':\n",
371
  " ms_events_matrix.append(event)\n",
372
  " itrack1 += 1\n",
 
391
  " cho.append(e[4] % 12)\n",
392
  "\n",
393
  " pe = e\n",
394
+ "\n",
395
  " if len(cho) > 0:\n",
396
  " chords.append(sorted(cho))\n",
397
  "\n",
 
405
  " times.append((e[1]-pt))\n",
406
  " start = False\n",
407
  " pt = e[1]\n",
408
+ "\n",
409
  " durs = [e[2] for e in ms_events_matrix]\n",
410
  " vels = [e[5] for e in ms_events_matrix]\n",
411
  "\n",
 
432
  " max_p_count = p_counts[0][1]\n",
433
  " trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)]\n",
434
  " total_p_counts = sum([y[1] for y in trimmed_p_counts])\n",
435
+ "\n",
436
  " if search_transposed_pitches:\n",
437
  " search_pitches = mult_pitches_counts\n",
438
  " else:\n",
 
448
  "\n",
449
  " if add_timings_ratios:\n",
450
  "\n",
451
+ " source_times = [avg_time,\n",
452
+ " median_time,\n",
453
  " mode_time]\n",
454
  "\n",
455
  " match_times = meta_data[0][1][3][1]\n",
 
501
  "\n",
502
  " same_pitches = set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts])\n",
503
  " num_same_pitches = len(same_pitches)\n",
504
+ "\n",
505
  " if num_same_pitches == len(trimmed_pitches_counts):\n",
506
  " same_pitches_ratio = (num_same_pitches / len(trimmed_p_counts))\n",
507
  " else:\n",
508
  " same_pitches_ratio = (num_same_pitches / max(len(trimmed_p_counts), len(trimmed_pitches_counts)))\n",
509
+ "\n",
510
  " if skip_exact_matches:\n",
511
  " if same_pitches_ratio == 1:\n",
512
  " same_pitches_ratio = 0\n",
 
547
  "\n",
548
  " if add_pitches_counts_ratios:\n",
549
  " r_list.append(spcrat[0])\n",
550
+ "\n",
551
  " if add_timings_ratios:\n",
552
  " r_list.append(atrat[0])\n",
553
+ "\n",
554
  " if add_durations_ratios:\n",
555
  " r_list.append(adrat[0])\n",
556
  "\n",
557
  " ratios_list.append(r_list)\n",
558
  "\n",
559
  " #===================================================\n",
560
+ "\n",
561
  " avg_ratios_list = []\n",
562
  "\n",
563
  " for r in ratios_list:\n",
564
  " avg_ratios_list.append(sum(r) / len(r))\n",
565
  "\n",
566
  " #===================================================\n",
567
+ "\n",
568
  " final_ratio = max(avg_ratios_list)\n",
569
+ "\n",
570
  " if final_ratio > maximum_match_ratio_to_search_for:\n",
571
  " final_ratio = 0\n",
572
  "\n",
 
592
  " ff = LAMD_files_list[ffn_idx][1]\n",
593
  "\n",
594
  " #=======================================================\n",
595
+ "\n",
596
  " dir_str = str(fn1)\n",
597
  " copy_path = '/content/Output-MIDI-Dataset/'+dir_str\n",
598
  " if not os.path.exists(copy_path):\n",
 
601
  " fff = str(m * 100) + '_' + ffn + '.mid'\n",
602
  "\n",
603
  " shutil.copy2(ff, copy_path+'/'+fff)\n",
604
+ "\n",
605
  " shutil.copy2(f, copy_path+'/'+fn)\n",
606
  "\n",
607
  " #=======================================================\n",
608
+ "\n",
609
  " print('Done!')\n",
610
  " print('=' * 70)\n",
611
  "\n",
 
613
  "\n",
614
  " # Processed files counter\n",
615
  " files_count += 1\n",
616
+ "\n",
617
  " except KeyboardInterrupt:\n",
618
  " print('Quitting...')\n",
619
  " print('Total number of processed MIDI files', files_count)\n",
620
  " print('=' * 70)\n",
621
+ " break\n",
622
  "\n",
623
  " except Exception as ex:\n",
624
  " print('WARNING !!!')\n",
TMIDIX.py ADDED
The diff for this file is too large to render. See raw diff
 
los_angeles_midi_dataset_search_and_explore.py CHANGED
@@ -6,7 +6,7 @@ Automatically generated by Colaboratory.
6
  Original file is located at
7
  https://colab.research.google.com/github/asigalov61/Los-Angeles-MIDI-Dataset/blob/main/Los_Angeles_MIDI_Dataset_Search_and_Explore.ipynb
8
 
9
- # Los Angeles MIDI Dataset: Search and Explore (ver. 2.2)
10
 
11
  ***
12
 
@@ -89,23 +89,6 @@ print('=' * 70)
89
 
90
  #================================================
91
 
92
- # %cd /content/Los-Angeles-MIDI-Dataset/MIDI-MATRIXES
93
-
94
- print('=' * 70)
95
- print('Unzipping MIDI-MATRIXES...Please wait...')
96
-
97
- !cat LAMDa_MIDI_MATRIXES.zip* > LAMDa_MIDI_MATRIXES.zip
98
- print('=' * 70)
99
-
100
- !unzip -j LAMDa_MIDI_MATRIXES.zip
101
- print('=' * 70)
102
-
103
- print('Done! Enjoy! :)')
104
- print('=' * 70)
105
- # %cd /content/
106
-
107
- #==================================================
108
-
109
  # %cd /content/Los-Angeles-MIDI-Dataset/TOTALS
110
 
111
  print('=' * 70)
@@ -126,10 +109,6 @@ print('Loading LAMDa META-DATA...')
126
  meta_data = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/META-DATA/LAMDa_META_DATA.pickle', 'rb'))
127
  print('Done!')
128
  print('=' * 70)
129
- print('Loading LAMDa MIDI-MATRIXES...')
130
- midi_matrixes = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/MIDI-MATRIXES/LAMDa_MIDI_MATRIXES.pickle', 'rb'))
131
- print('Done!')
132
- print('=' * 70)
133
  print('Loading LAMDa TOTALS...')
134
  totals = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/TOTALS/LAMDa_TOTALS.pickle', 'rb'))
135
  print('Done!')
@@ -144,10 +123,10 @@ print('=' * 70)
144
  print('Downloading Los Angeles MIDI Dataset...Please wait...')
145
  print('=' * 70)
146
 
147
- hf_hub_download(repo_id='projectlosangeles/Los-Angeles-MIDI-Dataset',
148
- filename='Los-Angeles-MIDI-Dataset-Ver-2-0-CC-BY-NC-SA.zip',
149
  repo_type="dataset",
150
- local_dir='/content/LAMD',
151
  local_dir_use_symlinks=False)
152
  print('=' * 70)
153
  print('Done! Enjoy! :)')
@@ -159,7 +138,7 @@ print('=' * 70)
159
 
160
  print('=' * 70)
161
  print('Unzipping Los Angeles MIDI Dataset...Please wait...')
162
- !unzip 'Los-Angeles-MIDI-Dataset-Ver-2-0-CC-BY-NC-SA.zip'
163
  print('=' * 70)
164
 
165
  print('Done! Enjoy! :)')
@@ -197,7 +176,7 @@ print('=' * 70)
197
  #@title Plot totals from MIDI matrixes (legacy)
198
 
199
  cos_sim = pairwise.cosine_similarity(
200
- totals[0][0][4]
201
  )
202
  plt.figure(figsize=(8, 8))
203
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
@@ -210,7 +189,7 @@ plt.tight_layout()
210
  plt.plot()
211
 
212
  cos_sim = pairwise.cosine_similarity(
213
- totals[0][0][5]
214
  )
215
  plt.figure(figsize=(8, 8))
216
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
@@ -223,7 +202,7 @@ plt.tight_layout()
223
  plt.plot()
224
 
225
  cos_sim = pairwise.cosine_similarity(
226
- totals[0][0][6]
227
  )
228
  plt.figure(figsize=(8, 8))
229
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
@@ -236,7 +215,7 @@ plt.tight_layout()
236
  plt.plot()
237
 
238
  cos_sim = pairwise.cosine_similarity(
239
- totals[0][0][7]
240
  )
241
  plt.figure(figsize=(8, 8))
242
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
@@ -249,7 +228,7 @@ plt.tight_layout()
249
  plt.plot()
250
 
251
  cos_sim = pairwise.cosine_similarity(
252
- totals[0][0][8]
253
  )
254
  plt.figure(figsize=(8, 8))
255
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
@@ -262,7 +241,7 @@ plt.tight_layout()
262
  plt.plot()
263
 
264
  cos_sim = pairwise.cosine_similarity(
265
- totals[0][0][9]
266
  )
267
  plt.figure(figsize=(8, 8))
268
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
@@ -290,8 +269,8 @@ plt.figure(figsize=(8, 8))
290
  plt.plot(y, pitches_counts_totals)
291
 
292
  plt.title('MIDI Instruments Pitches')
293
- plt.xlabel("Ratio")
294
- plt.ylabel("Pitch")
295
  plt.tight_layout()
296
  plt.plot()
297
 
@@ -308,15 +287,15 @@ for p in pitches_counts_totals:
308
  x += 1
309
 
310
  cos_sim = pairwise.cosine_similarity(
311
- sim_mat
312
  )
313
  plt.figure(figsize=(8, 8))
314
  plt.imshow(sim_mat, cmap="inferno", interpolation="none")
315
  im_ratio = 1
316
  plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)
317
- plt.title('MIDI Instruments Pitches')
318
- plt.xlabel("Ratio")
319
- plt.ylabel("Pitch")
320
  plt.tight_layout()
321
  plt.plot()
322
 
@@ -335,8 +314,8 @@ plt.figure(figsize=(8, 8))
335
  plt.plot(y, pitches_counts_totals)
336
 
337
  plt.title('MIDI Drums Pitches')
338
- plt.xlabel("Ratio")
339
- plt.ylabel("Pitch")
340
  plt.tight_layout()
341
  plt.plot()
342
 
@@ -353,15 +332,15 @@ for p in pitches_counts_totals:
353
  x += 1
354
 
355
  cos_sim = pairwise.cosine_similarity(
356
- sim_mat
357
  )
358
  plt.figure(figsize=(8, 8))
359
  plt.imshow(sim_mat, cmap="inferno", interpolation="none")
360
  im_ratio = 1
361
  plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)
362
  plt.title('MIDI Drums Pitches')
363
- plt.xlabel("Ratio")
364
- plt.ylabel("Pitch")
365
  plt.tight_layout()
366
  plt.plot()
367
 
@@ -371,7 +350,7 @@ patches_counts_totals = [0] * 256
371
 
372
 
373
  for m in tqdm(meta_data):
374
- for mm in m[1][11][1]:
375
  patches_counts_totals[mm[0]] += mm[1]
376
 
377
 
@@ -380,8 +359,8 @@ plt.figure(figsize=(8, 8))
380
  plt.plot(y, patches_counts_totals[:128])
381
 
382
  plt.title('MIDI Patches')
383
- plt.xlabel("Ratio")
384
- plt.ylabel('Patch')
385
  plt.tight_layout()
386
  plt.plot()
387
 
@@ -399,82 +378,6 @@ f = full_path_to_source_MIDI
399
  print('=' * 70)
400
  print('Loading MIDI file...')
401
 
402
- score = MIDI.midi2ms_score(open(f, 'rb').read())
403
-
404
- events_matrix = []
405
-
406
- itrack = 1
407
-
408
- while itrack < len(score):
409
- for event in score[itrack]:
410
- events_matrix.append(event)
411
- itrack += 1
412
-
413
- # Sorting...
414
- events_matrix.sort(key=lambda x: x[1])
415
-
416
- # recalculating timings
417
- for e in events_matrix:
418
- e[1] = int(e[1] / 10)
419
- if e[0] == 'note':
420
- e[2] = int(e[2] / 20)
421
-
422
- # final processing...
423
-
424
- melody_chords = []
425
-
426
- patches = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
427
-
428
- pe = events_matrix[0]
429
- for e in events_matrix:
430
-
431
- if e[0] == 'note':
432
- # ['note', start_time, duration, channel, note, velocity]
433
- time = max(0, min(255, e[1]-pe[1]))
434
- duration = max(1, min(255, e[2]))
435
- channel = max(0, min(15, e[3]))
436
-
437
- if e[3] != 9:
438
- instrument = max(0, min(127, patches[e[3]]))
439
- else:
440
- instrument = max(128, min(255, patches[e[3]]+128))
441
-
442
- if e[3] != 9:
443
-
444
- pitch = max(1, min(127, e[4]))
445
- else:
446
- pitch = max(129, min(255, e[4]+128))
447
-
448
- if e[3] != 9:
449
- velocity = max(1, min(127, e[5]))
450
- else:
451
- velocity = max(129, min(255, e[5]+128))
452
-
453
- melody_chords.append([time, duration, channel, instrument, pitch, velocity])
454
-
455
- if e[0] == 'patch_change':
456
- # ['patch_change', dtime, channel, patch]
457
- time = max(0, min(127, e[1]-pe[1]))
458
- channel = max(0, min(15, e[2]))
459
- patch = max(0, min(127, e[3]))
460
-
461
- patches[channel] = patch
462
-
463
- pe = e # Previous event
464
-
465
- MATRIX = [[0]*256 for i in range(38)]
466
-
467
- for m in melody_chords:
468
-
469
- MATRIX[0][m[0]] += 1
470
- MATRIX[1][m[1]] += 1
471
- MATRIX[2][m[2]] += 1
472
- MATRIX[3][m[3]] += 1
473
- MATRIX[4][m[4]] += 1
474
- MATRIX[5][m[5]] += 1
475
- MATRIX[m[2]+6][m[3]] += 1
476
- MATRIX[m[2]+22][m[4]] += 1
477
-
478
  #==================================================
479
 
480
  score = MIDI.midi2score(open(f, 'rb').read())
@@ -484,7 +387,7 @@ events_matrix = []
484
  track_count = 0
485
 
486
  for s in score:
487
-
488
  if track_count > 0:
489
  track = s
490
  track.sort(key=lambda x: x[1])
@@ -493,7 +396,7 @@ for s in score:
493
  midi_ticks = s
494
 
495
  track_count += 1
496
-
497
  events_matrix.sort(key=lambda x: x[1])
498
 
499
  mult_pitches_counts = []
@@ -516,7 +419,7 @@ for i in range(-6, 6):
516
 
517
  pitches_counts = [[y[0],y[1]] for y in Counter([y[4] for y in events_matrix1 if y[0] == 'note']).most_common()]
518
  pitches_counts.sort(key=lambda x: x[0], reverse=True)
519
-
520
  mult_pitches_counts.append(pitches_counts)
521
 
522
  patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))
@@ -531,7 +434,7 @@ ms_events_matrix = []
531
  itrack1 = 1
532
 
533
  while itrack1 < len(ms_score):
534
- for event in ms_score[itrack1]:
535
  if event[0] == 'note':
536
  ms_events_matrix.append(event)
537
  itrack1 += 1
@@ -556,7 +459,7 @@ for e in ms_events_matrix:
556
  cho.append(e[4] % 12)
557
 
558
  pe = e
559
-
560
  if len(cho) > 0:
561
  chords.append(sorted(cho))
562
 
@@ -570,7 +473,7 @@ for e in ms_events_matrix:
570
  times.append((e[1]-pt))
571
  start = False
572
  pt = e[1]
573
-
574
  durs = [e[2] for e in ms_events_matrix]
575
  vels = [e[5] for e in ms_events_matrix]
576
 
@@ -602,7 +505,7 @@ itrack = 1
602
  song_f = []
603
 
604
  while itrack < len(ms_score):
605
- for event in ms_score[itrack]:
606
  if event[0] == 'note':
607
  song_f.append(event)
608
  itrack += 1
@@ -637,286 +540,7 @@ plt.show()
637
 
638
  """# (SEARCH AND EXPLORE)"""
639
 
640
- #@title Legacy MIDI Matrixes Search (Slow)
641
-
642
- #@markdown NOTE: You can stop the search at any time to render partial results
643
-
644
- minimum_match_ratio_to_search_for = 0 #@param {type:"slider", min:0, max:500, step:1}
645
- stop_search_on_exact_match = True #@param {type:"boolean"}
646
- skip_exact_matches = False #@param {type:"boolean"}
647
- render_MIDI_to_audio = False #@param {type:"boolean"}
648
- download_MIDI = False #@param {type:"boolean"}
649
-
650
- #=================================================================================
651
-
652
- matching_type = "minkowski"
653
-
654
- def compress_matrix(midi_matrix):
655
-
656
- MX = 38
657
- MY = 256
658
-
659
- if len(midi_matrix) == MX:
660
-
661
- compressed_matrix = []
662
- zeros = 0
663
- zeros_shift = 0
664
- zeros_count = 0
665
-
666
- for m in midi_matrix:
667
- for mm in m:
668
- zeros_shift = max(zeros_shift, mm) + 1
669
-
670
- compressed_matrix.append(zeros_shift)
671
-
672
- for m in midi_matrix:
673
- if len(m) == MY:
674
- for mm in m:
675
- if mm != 0:
676
- if zeros > 0:
677
- compressed_matrix.append(zeros+zeros_shift)
678
- zeros = 0
679
- compressed_matrix.append(mm)
680
-
681
- else:
682
- zeros += 1
683
- zeros_count += 1
684
-
685
- else:
686
- print('Wrong matrix format!')
687
- return [1]
688
-
689
- if zeros > 0:
690
- compressed_matrix.append(zeros+zeros_shift)
691
-
692
- compressed_matrix.append(zeros_count+zeros_shift)
693
- compressed_matrix.append(zeros_shift)
694
-
695
- return compressed_matrix
696
-
697
- else:
698
- print('Wrong matrix format!')
699
- return [0]
700
-
701
- #=================================================================================
702
-
703
- def decompress_matrix(compressed_midi_matrix):
704
-
705
- MX = 38
706
- MY = 256
707
-
708
- zeros_count = 0
709
-
710
- temp_matrix = []
711
- decompressed_matrix = [[0]*MY for i in range(MX)]
712
-
713
- if compressed_midi_matrix[0] == compressed_midi_matrix[-1]:
714
- zeros_shift = compressed_midi_matrix[0]
715
- mcount = 0
716
-
717
- for c in compressed_midi_matrix[1:-2]:
718
- if c > zeros_shift:
719
- temp_matrix.extend([0] * (c-zeros_shift))
720
- zeros_count += (c-zeros_shift)
721
-
722
- else:
723
- temp_matrix.extend([c])
724
-
725
- if len(temp_matrix) == (MX * MY):
726
-
727
- for i in range(MX):
728
- for j in range(MY):
729
- decompressed_matrix[i][j] = copy.deepcopy(temp_matrix[(i*MY) + j])
730
-
731
- if len(decompressed_matrix) == MX and zeros_count == (compressed_midi_matrix[-2]-zeros_shift):
732
- return decompressed_matrix
733
-
734
- else:
735
- print('Matrix is corrupted!')
736
- return [len(decompressed_matrix), (MX * MY), zeros_count, (compressed_midi_matrix[-2]-zeros_shift)]
737
-
738
- else:
739
- print('Matrix is corrupted!')
740
- return [len(temp_matrix), zeros_count]
741
-
742
- else:
743
- print('Matrix is corrupted!')
744
- return [0]
745
-
746
- #=================================================================================
747
-
748
- def batched_scores(matbatch, matrix):
749
-
750
- sco= []
751
- for D in matbatch:
752
-
753
- dist = pairwise_distances(matrix, decompress_matrix(D[1]), metric=matching_type)[0][0]
754
-
755
- if skip_exact_matches:
756
- if dist == 0:
757
- dist = 999999
758
-
759
- if dist <= minimum_match_ratio_to_search_for:
760
- dist = 999999
761
-
762
- sco.append(dist)
763
-
764
- return sco
765
-
766
- #=================================================================================
767
-
768
- print('=' * 70)
769
- print('Searching...Please wait...')
770
- print('=' * 70)
771
-
772
- scores = []
773
-
774
- c_count = multiprocessing.cpu_count()
775
-
776
- par = Parallel(n_jobs=c_count)
777
-
778
- num_jobs = c_count
779
- scores_per_job = 100
780
-
781
- MATRIX_X = [MATRIX] * num_jobs
782
-
783
- for i in tqdm(range(0, len(midi_matrixes), (num_jobs*scores_per_job))):
784
-
785
- try:
786
-
787
- MAT_BATCHES = []
788
-
789
- for j in range(num_jobs):
790
- MAT_BATCHES.append(midi_matrixes[i+(j*scores_per_job):i+((j+1)*scores_per_job)])
791
-
792
- output = par(delayed(batched_scores) (MB, MAT) for MB, MAT in zip(MAT_BATCHES, MATRIX_X))
793
-
794
- output1 = []
795
-
796
- for o in output:
797
- output1.extend(o)
798
-
799
- scores.extend(output1)
800
-
801
- if stop_search_on_exact_match:
802
- if 0 in output1:
803
- print('=' * 70)
804
- print('Found exact match!')
805
- print('Stoping further search...')
806
- print('=' * 70)
807
- break
808
-
809
- else:
810
- if 0 in output1:
811
- print('=' * 70)
812
- print('Found exact match!')
813
- print('=' * 70)
814
- print('LAMDa Index:', scores.index(min(scores)))
815
- print('LAMDa File Name:', midi_matrixes[scores.index(min(scores))][0])
816
- print('=' * 70)
817
- print('Continuing search...')
818
- print('=' * 70)
819
-
820
- except KeyboardInterrupt:
821
- break
822
-
823
- except Exception as e:
824
- print('WARNING !!!')
825
- print('=' * 70)
826
- print('Error detected:', e)
827
- print('=' * 70)
828
- continue
829
-
830
- print('Done!')
831
- print('=' * 70)
832
- print('Best match:')
833
- print('=' * 70)
834
- print(matching_type.title(), 'distance ==', min(scores))
835
- print('LAMDa Index:', scores.index(min(scores)))
836
- print('LAMDa File Name:', midi_matrixes[scores.index(min(scores))][0])
837
- print('=' * 70)
838
-
839
- #============================================
840
- # MIDI rendering code
841
- #============================================
842
-
843
- print('Rendering source MIDI...')
844
- print('=' * 70)
845
-
846
- fn = midi_matrixes[scores.index(min(scores))][0]
847
-
848
- try:
849
- fn_idx = [y[0] for y in LAMD_files_list].index(fn)
850
-
851
- f = LAMD_files_list[fn_idx][1]
852
-
853
- ms_score = MIDI.midi2ms_score(open(f, 'rb').read())
854
-
855
- itrack = 1
856
- song_f = []
857
-
858
- while itrack < len(ms_score):
859
- for event in ms_score[itrack]:
860
- if event[0] == 'note':
861
- song_f.append(event)
862
- itrack += 1
863
-
864
- song_f.sort(key=lambda x: x[1])
865
-
866
- fname = f.split('.mid')[0]
867
-
868
- x = []
869
- y =[]
870
- c = []
871
-
872
- colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver', 'aqua', 'azure', 'bisque', 'coral']
873
-
874
- for s in song_f:
875
- x.append(s[1] / 1000)
876
- y.append(s[4])
877
- c.append(colors[s[3]])
878
-
879
- if render_MIDI_to_audio:
880
- FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
881
- display(Audio(str(fname + '.wav'), rate=16000))
882
-
883
- plt.figure(figsize=(14,5))
884
- ax=plt.axes(title=fname)
885
- ax.set_facecolor('black')
886
-
887
- plt.scatter(x,y, c=c)
888
- plt.xlabel("Time")
889
- plt.ylabel("Pitch")
890
- plt.show()
891
-
892
- except:
893
- pass
894
- #==============================================
895
-
896
- if download_MIDI:
897
- print('=' * 70)
898
- print('Downloading MIDI file', str(fn) + '.mid')
899
- files.download(f)
900
- print('=' * 70)
901
-
902
- #============================================
903
-
904
- print('Top 100 matches')
905
- print('=' * 70)
906
-
907
- top_matches = []
908
-
909
- for i in range(len(scores)):
910
- top_matches.append([midi_matrixes[i][0], scores[i]])
911
-
912
- top_matches.sort(key=lambda x: x[1])
913
-
914
- for t in top_matches[:100]:
915
- print(t)
916
-
917
- print('=' * 70)
918
-
919
- #@title MIDI Pitches Search (Fast)
920
 
921
  #@markdown NOTE: You can stop the search at any time to render partial results
922
 
@@ -955,7 +579,7 @@ for d in tqdm(meta_data):
955
  max_p_count = p_counts[0][1]
956
  trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)]
957
  total_p_counts = sum([y[1] for y in trimmed_p_counts])
958
-
959
  if search_transposed_pitches:
960
  search_pitches = mult_pitches_counts
961
  else:
@@ -971,8 +595,8 @@ for d in tqdm(meta_data):
971
 
972
  if add_timings_ratios:
973
 
974
- source_times = [avg_time,
975
- median_time,
976
  mode_time]
977
 
978
  match_times = meta_data[0][1][3][1]
@@ -1024,7 +648,7 @@ for d in tqdm(meta_data):
1024
 
1025
  same_pitches = set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts])
1026
  num_same_pitches = len(same_pitches)
1027
-
1028
  if num_same_pitches == len(trimmed_pitches_counts):
1029
  same_pitches_ratio = (num_same_pitches / len(trimmed_p_counts))
1030
  else:
@@ -1070,36 +694,36 @@ for d in tqdm(meta_data):
1070
 
1071
  if add_pitches_counts_ratios:
1072
  r_list.append(spcrat[0])
1073
-
1074
  if add_timings_ratios:
1075
  r_list.append(atrat[0])
1076
-
1077
  if add_durations_ratios:
1078
  r_list.append(adrat[0])
1079
 
1080
  ratios_list.append(r_list)
1081
 
1082
  #===================================================
1083
-
1084
  avg_ratios_list = []
1085
 
1086
  for r in ratios_list:
1087
  avg_ratios_list.append(sum(r) / len(r))
1088
 
1089
  #===================================================
1090
-
1091
  final_ratio = max(avg_ratios_list)
1092
-
1093
  if final_ratio > maximum_match_ratio_to_search_for:
1094
  final_ratio = 0
1095
 
1096
  final_ratios.append(final_ratio)
1097
 
1098
  #===================================================
1099
-
1100
  except KeyboardInterrupt:
1101
  break
1102
-
1103
  except Exception as e:
1104
  print('WARNING !!!')
1105
  print('=' * 70)
@@ -1137,7 +761,7 @@ itrack = 1
1137
  song_f = []
1138
 
1139
  while itrack < len(ms_score):
1140
- for event in ms_score[itrack]:
1141
  if event[0] == 'note':
1142
  song_f.append(event)
1143
  itrack += 1
@@ -1178,7 +802,7 @@ if download_MIDI:
1178
  files.download(f)
1179
  print('=' * 70)
1180
 
1181
- #@title MIDI Chords Search (Fast)
1182
 
1183
  #@markdown NOTE: You can stop the search at any time to render partial results
1184
 
@@ -1204,19 +828,19 @@ for d in tqdm(meta_data):
1204
 
1205
  c_counts.sort(reverse = True, key = lambda x: x[0][1])
1206
  max_c_count = c_counts[0][1]
1207
- trimmed_c_counts = [y for y in c_counts if y[1] >= (max_c_count * chords_counts_cutoff_threshold_ratio)]
1208
  trimmed_c_counts.sort(reverse = True, key = lambda x: x[1])
1209
-
1210
  max_chords_count = ms_chords_counts[0][1]
1211
- trimmed_chords_counts = [y for y in ms_chords_counts if y[1] >= (max_chords_count * chords_counts_cutoff_threshold_ratio)]
1212
 
1213
  num_same_chords = len(set([tuple(T[0]) for T in trimmed_c_counts]) & set([tuple(t[0]) for t in trimmed_chords_counts]))
1214
-
1215
  if num_same_chords == len(trimmed_chords_counts):
1216
  same_chords_ratio = (num_same_chords / len(trimmed_c_counts))
1217
  else:
1218
  same_chords_ratio = (num_same_chords / max(len(trimmed_c_counts), len(trimmed_chords_counts)))
1219
-
1220
  if skip_exact_matches:
1221
  if same_chords_ratio == 1:
1222
  same_chords_ratio = 0
@@ -1225,10 +849,10 @@ for d in tqdm(meta_data):
1225
  same_chords_ratio = 0
1226
 
1227
  ratios.append(same_chords_ratio)
1228
-
1229
  except KeyboardInterrupt:
1230
  break
1231
-
1232
  except Exception as e:
1233
  print('WARNING !!!')
1234
  print('=' * 70)
@@ -1266,7 +890,7 @@ itrack = 1
1266
  song_f = []
1267
 
1268
  while itrack < len(ms_score):
1269
- for event in ms_score[itrack]:
1270
  if event[0] == 'note':
1271
  song_f.append(event)
1272
  itrack += 1
@@ -1307,7 +931,7 @@ if download_MIDI:
1307
  files.download(f)
1308
  print('=' * 70)
1309
 
1310
- #@title MIDI Patches Search (Fast)
1311
 
1312
  #@markdown NOTE: You can stop the search at any time to render partial results
1313
 
@@ -1326,17 +950,17 @@ for d in tqdm(meta_data):
1326
 
1327
  try:
1328
 
1329
- p_list= d[1][12][1]
1330
 
1331
  num_same_patches = len(set(p_list) & set(patches_list))
1332
-
1333
  if len(set(p_list + patches_list)) > 0:
1334
-
1335
  if num_same_patches == len(patches_list):
1336
  same_patches_ratio = num_same_patches / len(p_list)
1337
  else:
1338
  same_patches_ratio = num_same_patches / max(len(p_list), len(patches_list))
1339
-
1340
  else:
1341
  same_patches_ratio = 0
1342
 
@@ -1346,12 +970,12 @@ for d in tqdm(meta_data):
1346
 
1347
  if same_patches_ratio > maximum_match_ratio_to_search_for:
1348
  same_patches_ratio = 0
1349
-
1350
  ratios.append(same_patches_ratio)
1351
-
1352
  except KeyboardInterrupt:
1353
  break
1354
-
1355
  except Exception as e:
1356
  print('WARNING !!!')
1357
  print('=' * 70)
@@ -1389,7 +1013,7 @@ itrack = 1
1389
  song_f = []
1390
 
1391
  while itrack < len(ms_score):
1392
- for event in ms_score[itrack]:
1393
  if event[0] == 'note':
1394
  song_f.append(event)
1395
  itrack += 1
@@ -1438,10 +1062,10 @@ search_query = "Come To My Window" #@param {type:"string"}
1438
  md5_hash_MIDI_file_name = "d9a7e1c6a375b8e560155a5977fc10f8" #@param {type:"string"}
1439
  case_sensitive_search = False #@param {type:"boolean"}
1440
 
1441
- fields_to_search = ['track_name',
1442
- 'text_event',
1443
- 'lyric',
1444
- 'copyright_text_event',
1445
  'marker',
1446
  'text_event_08',
1447
  'text_event_09',
@@ -1471,12 +1095,12 @@ if md5_hash_MIDI_file_name != '':
1471
  pprint.pprint(['Result:', d[1][:16]], compact = True)
1472
  print('=' * 70)
1473
  break
1474
-
1475
  except KeyboardInterrupt:
1476
  print('Ending search...')
1477
  print('=' * 70)
1478
  break
1479
-
1480
  except Exception as e:
1481
  print('WARNING !!!')
1482
  print('=' * 70)
@@ -1505,7 +1129,7 @@ else:
1505
  print('-' * 70)
1506
  pprint.pprint(['Result:', dd[2][:16]], compact = True)
1507
  print('=' * 70)
1508
-
1509
  else:
1510
  if str(search_query).lower() in str(dd[2]).lower():
1511
  print('Found!')
@@ -1515,12 +1139,12 @@ else:
1515
  print('-' * 70)
1516
  pprint.pprint(['Result:', dd[2][:16]], compact = True)
1517
  print('=' * 70)
1518
-
1519
  except KeyboardInterrupt:
1520
  print('Ending search...')
1521
  print('=' * 70)
1522
  break
1523
-
1524
  except:
1525
  print('Ending search...')
1526
  print('=' * 70)
@@ -1555,9 +1179,9 @@ try:
1555
  fn = md5_hash_MIDI_file_name
1556
  fn_idx = [y[0] for y in LAMD_files_list].index(fn)
1557
  f = LAMD_files_list[fn_idx][1]
1558
-
1559
  print('Using md5 hash filename')
1560
-
1561
  print('=' * 70)
1562
  print('Rendering MIDI...')
1563
  print('=' * 70)
@@ -1568,7 +1192,7 @@ try:
1568
  song_f = []
1569
 
1570
  while itrack < len(ms_score):
1571
- for event in ms_score[itrack]:
1572
  if event[0] == 'note':
1573
  song_f.append(event)
1574
  itrack += 1
@@ -1742,11 +1366,11 @@ for f in tqdm(filez[START_FILE_NUMBER:]):
1742
  score = MIDI.midi2score(open(f, 'rb').read())
1743
 
1744
  events_matrix = []
1745
-
1746
  itrack = 1
1747
 
1748
  while itrack < len(score):
1749
- for event in score[itrack]:
1750
  events_matrix.append(event)
1751
  itrack += 1
1752
 
@@ -1754,7 +1378,7 @@ for f in tqdm(filez[START_FILE_NUMBER:]):
1754
  events_matrix.sort(key=lambda x: x[1])
1755
 
1756
  if len(events_matrix) > 0:
1757
-
1758
  #=======================================================
1759
  # INSERT YOUR CUSTOM ANAYLSIS CODE RIGHT HERE
1760
  #=======================================================
@@ -1770,7 +1394,7 @@ for f in tqdm(filez[START_FILE_NUMBER:]):
1770
 
1771
  except KeyboardInterrupt:
1772
  print('Saving current progress and quitting...')
1773
- break
1774
 
1775
  except Exception as ex:
1776
  print('WARNING !!!')
@@ -1788,7 +1412,7 @@ print('Resulting Stats:')
1788
  print('=' * 70)
1789
  print('Total good processed MIDI files:', files_count)
1790
  print('=' * 70)
1791
- print('Done!')
1792
  print('=' * 70)
1793
 
1794
  """# Congrats! You did it! :)"""
 
6
  Original file is located at
7
  https://colab.research.google.com/github/asigalov61/Los-Angeles-MIDI-Dataset/blob/main/Los_Angeles_MIDI_Dataset_Search_and_Explore.ipynb
8
 
9
+ # Los Angeles MIDI Dataset: Search and Explore (ver. 3.0)
10
 
11
  ***
12
 
 
89
 
90
  #================================================
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  # %cd /content/Los-Angeles-MIDI-Dataset/TOTALS
93
 
94
  print('=' * 70)
 
109
  meta_data = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/META-DATA/LAMDa_META_DATA.pickle', 'rb'))
110
  print('Done!')
111
  print('=' * 70)
 
 
 
 
112
  print('Loading LAMDa TOTALS...')
113
  totals = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/TOTALS/LAMDa_TOTALS.pickle', 'rb'))
114
  print('Done!')
 
123
  print('Downloading Los Angeles MIDI Dataset...Please wait...')
124
  print('=' * 70)
125
 
126
+ hf_hub_download(repo_id='projectlosangeles/Los-Angeles-MIDI-Dataset',
127
+ filename='Los-Angeles-MIDI-Dataset-Ver-3-0-CC-BY-NC-SA.zip',
128
  repo_type="dataset",
129
+ local_dir='/content/LAMD',
130
  local_dir_use_symlinks=False)
131
  print('=' * 70)
132
  print('Done! Enjoy! :)')
 
138
 
139
  print('=' * 70)
140
  print('Unzipping Los Angeles MIDI Dataset...Please wait...')
141
+ !unzip 'Los-Angeles-MIDI-Dataset-Ver-3-0-CC-BY-NC-SA.zip'
142
  print('=' * 70)
143
 
144
  print('Done! Enjoy! :)')
 
176
  #@title Plot totals from MIDI matrixes (legacy)
177
 
178
  cos_sim = pairwise.cosine_similarity(
179
+ totals[0][0][4]
180
  )
181
  plt.figure(figsize=(8, 8))
182
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
 
189
  plt.plot()
190
 
191
  cos_sim = pairwise.cosine_similarity(
192
+ totals[0][0][5]
193
  )
194
  plt.figure(figsize=(8, 8))
195
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
 
202
  plt.plot()
203
 
204
  cos_sim = pairwise.cosine_similarity(
205
+ totals[0][0][6]
206
  )
207
  plt.figure(figsize=(8, 8))
208
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
 
215
  plt.plot()
216
 
217
  cos_sim = pairwise.cosine_similarity(
218
+ totals[0][0][7]
219
  )
220
  plt.figure(figsize=(8, 8))
221
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
 
228
  plt.plot()
229
 
230
  cos_sim = pairwise.cosine_similarity(
231
+ totals[0][0][8]
232
  )
233
  plt.figure(figsize=(8, 8))
234
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
 
241
  plt.plot()
242
 
243
  cos_sim = pairwise.cosine_similarity(
244
+ totals[0][0][9]
245
  )
246
  plt.figure(figsize=(8, 8))
247
  plt.imshow(cos_sim, cmap="inferno", interpolation="none")
 
269
  plt.plot(y, pitches_counts_totals)
270
 
271
  plt.title('MIDI Instruments Pitches')
272
+ plt.xlabel("Pitch")
273
+ plt.ylabel("Count")
274
  plt.tight_layout()
275
  plt.plot()
276
 
 
287
  x += 1
288
 
289
  cos_sim = pairwise.cosine_similarity(
290
+ sim_mat
291
  )
292
  plt.figure(figsize=(8, 8))
293
  plt.imshow(sim_mat, cmap="inferno", interpolation="none")
294
  im_ratio = 1
295
  plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)
296
+ plt.title('MIDI Drums Pitches')
297
+ plt.xlabel("Pitch")
298
+ plt.ylabel("Count")
299
  plt.tight_layout()
300
  plt.plot()
301
 
 
314
  plt.plot(y, pitches_counts_totals)
315
 
316
  plt.title('MIDI Drums Pitches')
317
+ plt.xlabel("Pitch")
318
+ plt.ylabel("Count")
319
  plt.tight_layout()
320
  plt.plot()
321
 
 
332
  x += 1
333
 
334
  cos_sim = pairwise.cosine_similarity(
335
+ sim_mat
336
  )
337
  plt.figure(figsize=(8, 8))
338
  plt.imshow(sim_mat, cmap="inferno", interpolation="none")
339
  im_ratio = 1
340
  plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)
341
  plt.title('MIDI Drums Pitches')
342
+ plt.xlabel("Pitch")
343
+ plt.ylabel("Count")
344
  plt.tight_layout()
345
  plt.plot()
346
 
 
350
 
351
 
352
  for m in tqdm(meta_data):
353
+ for mm in m[1][12][1]:
354
  patches_counts_totals[mm[0]] += mm[1]
355
 
356
 
 
359
  plt.plot(y, patches_counts_totals[:128])
360
 
361
  plt.title('MIDI Patches')
362
+ plt.xlabel("Patch")
363
+ plt.ylabel('Count')
364
  plt.tight_layout()
365
  plt.plot()
366
 
 
378
  print('=' * 70)
379
  print('Loading MIDI file...')
380
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
381
  #==================================================
382
 
383
  score = MIDI.midi2score(open(f, 'rb').read())
 
387
  track_count = 0
388
 
389
  for s in score:
390
+
391
  if track_count > 0:
392
  track = s
393
  track.sort(key=lambda x: x[1])
 
396
  midi_ticks = s
397
 
398
  track_count += 1
399
+
400
  events_matrix.sort(key=lambda x: x[1])
401
 
402
  mult_pitches_counts = []
 
419
 
420
  pitches_counts = [[y[0],y[1]] for y in Counter([y[4] for y in events_matrix1 if y[0] == 'note']).most_common()]
421
  pitches_counts.sort(key=lambda x: x[0], reverse=True)
422
+
423
  mult_pitches_counts.append(pitches_counts)
424
 
425
  patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))
 
434
  itrack1 = 1
435
 
436
  while itrack1 < len(ms_score):
437
+ for event in ms_score[itrack1]:
438
  if event[0] == 'note':
439
  ms_events_matrix.append(event)
440
  itrack1 += 1
 
459
  cho.append(e[4] % 12)
460
 
461
  pe = e
462
+
463
  if len(cho) > 0:
464
  chords.append(sorted(cho))
465
 
 
473
  times.append((e[1]-pt))
474
  start = False
475
  pt = e[1]
476
+
477
  durs = [e[2] for e in ms_events_matrix]
478
  vels = [e[5] for e in ms_events_matrix]
479
 
 
505
  song_f = []
506
 
507
  while itrack < len(ms_score):
508
+ for event in ms_score[itrack]:
509
  if event[0] == 'note':
510
  song_f.append(event)
511
  itrack += 1
 
540
 
541
  """# (SEARCH AND EXPLORE)"""
542
 
543
+ #@title MIDI Pitches Search
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
544
 
545
  #@markdown NOTE: You can stop the search at any time to render partial results
546
 
 
579
  max_p_count = p_counts[0][1]
580
  trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)]
581
  total_p_counts = sum([y[1] for y in trimmed_p_counts])
582
+
583
  if search_transposed_pitches:
584
  search_pitches = mult_pitches_counts
585
  else:
 
595
 
596
  if add_timings_ratios:
597
 
598
+ source_times = [avg_time,
599
+ median_time,
600
  mode_time]
601
 
602
  match_times = meta_data[0][1][3][1]
 
648
 
649
  same_pitches = set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts])
650
  num_same_pitches = len(same_pitches)
651
+
652
  if num_same_pitches == len(trimmed_pitches_counts):
653
  same_pitches_ratio = (num_same_pitches / len(trimmed_p_counts))
654
  else:
 
694
 
695
  if add_pitches_counts_ratios:
696
  r_list.append(spcrat[0])
697
+
698
  if add_timings_ratios:
699
  r_list.append(atrat[0])
700
+
701
  if add_durations_ratios:
702
  r_list.append(adrat[0])
703
 
704
  ratios_list.append(r_list)
705
 
706
  #===================================================
707
+
708
  avg_ratios_list = []
709
 
710
  for r in ratios_list:
711
  avg_ratios_list.append(sum(r) / len(r))
712
 
713
  #===================================================
714
+
715
  final_ratio = max(avg_ratios_list)
716
+
717
  if final_ratio > maximum_match_ratio_to_search_for:
718
  final_ratio = 0
719
 
720
  final_ratios.append(final_ratio)
721
 
722
  #===================================================
723
+
724
  except KeyboardInterrupt:
725
  break
726
+
727
  except Exception as e:
728
  print('WARNING !!!')
729
  print('=' * 70)
 
761
  song_f = []
762
 
763
  while itrack < len(ms_score):
764
+ for event in ms_score[itrack]:
765
  if event[0] == 'note':
766
  song_f.append(event)
767
  itrack += 1
 
802
  files.download(f)
803
  print('=' * 70)
804
 
805
+ #@title MIDI Chords Search
806
 
807
  #@markdown NOTE: You can stop the search at any time to render partial results
808
 
 
828
 
829
  c_counts.sort(reverse = True, key = lambda x: x[0][1])
830
  max_c_count = c_counts[0][1]
831
+ trimmed_c_counts = [y for y in c_counts if y[1] >= (max_c_count * chords_counts_cutoff_threshold_ratio)]
832
  trimmed_c_counts.sort(reverse = True, key = lambda x: x[1])
833
+
834
  max_chords_count = ms_chords_counts[0][1]
835
+ trimmed_chords_counts = [y for y in ms_chords_counts if y[1] >= (max_chords_count * chords_counts_cutoff_threshold_ratio)]
836
 
837
  num_same_chords = len(set([tuple(T[0]) for T in trimmed_c_counts]) & set([tuple(t[0]) for t in trimmed_chords_counts]))
838
+
839
  if num_same_chords == len(trimmed_chords_counts):
840
  same_chords_ratio = (num_same_chords / len(trimmed_c_counts))
841
  else:
842
  same_chords_ratio = (num_same_chords / max(len(trimmed_c_counts), len(trimmed_chords_counts)))
843
+
844
  if skip_exact_matches:
845
  if same_chords_ratio == 1:
846
  same_chords_ratio = 0
 
849
  same_chords_ratio = 0
850
 
851
  ratios.append(same_chords_ratio)
852
+
853
  except KeyboardInterrupt:
854
  break
855
+
856
  except Exception as e:
857
  print('WARNING !!!')
858
  print('=' * 70)
 
890
  song_f = []
891
 
892
  while itrack < len(ms_score):
893
+ for event in ms_score[itrack]:
894
  if event[0] == 'note':
895
  song_f.append(event)
896
  itrack += 1
 
931
  files.download(f)
932
  print('=' * 70)
933
 
934
+ #@title MIDI Patches Search
935
 
936
  #@markdown NOTE: You can stop the search at any time to render partial results
937
 
 
950
 
951
  try:
952
 
953
+ p_list= d[1][11][1]
954
 
955
  num_same_patches = len(set(p_list) & set(patches_list))
956
+
957
  if len(set(p_list + patches_list)) > 0:
958
+
959
  if num_same_patches == len(patches_list):
960
  same_patches_ratio = num_same_patches / len(p_list)
961
  else:
962
  same_patches_ratio = num_same_patches / max(len(p_list), len(patches_list))
963
+
964
  else:
965
  same_patches_ratio = 0
966
 
 
970
 
971
  if same_patches_ratio > maximum_match_ratio_to_search_for:
972
  same_patches_ratio = 0
973
+
974
  ratios.append(same_patches_ratio)
975
+
976
  except KeyboardInterrupt:
977
  break
978
+
979
  except Exception as e:
980
  print('WARNING !!!')
981
  print('=' * 70)
 
1013
  song_f = []
1014
 
1015
  while itrack < len(ms_score):
1016
+ for event in ms_score[itrack]:
1017
  if event[0] == 'note':
1018
  song_f.append(event)
1019
  itrack += 1
 
1062
  md5_hash_MIDI_file_name = "d9a7e1c6a375b8e560155a5977fc10f8" #@param {type:"string"}
1063
  case_sensitive_search = False #@param {type:"boolean"}
1064
 
1065
+ fields_to_search = ['track_name',
1066
+ 'text_event',
1067
+ 'lyric',
1068
+ 'copyright_text_event',
1069
  'marker',
1070
  'text_event_08',
1071
  'text_event_09',
 
1095
  pprint.pprint(['Result:', d[1][:16]], compact = True)
1096
  print('=' * 70)
1097
  break
1098
+
1099
  except KeyboardInterrupt:
1100
  print('Ending search...')
1101
  print('=' * 70)
1102
  break
1103
+
1104
  except Exception as e:
1105
  print('WARNING !!!')
1106
  print('=' * 70)
 
1129
  print('-' * 70)
1130
  pprint.pprint(['Result:', dd[2][:16]], compact = True)
1131
  print('=' * 70)
1132
+
1133
  else:
1134
  if str(search_query).lower() in str(dd[2]).lower():
1135
  print('Found!')
 
1139
  print('-' * 70)
1140
  pprint.pprint(['Result:', dd[2][:16]], compact = True)
1141
  print('=' * 70)
1142
+
1143
  except KeyboardInterrupt:
1144
  print('Ending search...')
1145
  print('=' * 70)
1146
  break
1147
+
1148
  except:
1149
  print('Ending search...')
1150
  print('=' * 70)
 
1179
  fn = md5_hash_MIDI_file_name
1180
  fn_idx = [y[0] for y in LAMD_files_list].index(fn)
1181
  f = LAMD_files_list[fn_idx][1]
1182
+
1183
  print('Using md5 hash filename')
1184
+
1185
  print('=' * 70)
1186
  print('Rendering MIDI...')
1187
  print('=' * 70)
 
1192
  song_f = []
1193
 
1194
  while itrack < len(ms_score):
1195
+ for event in ms_score[itrack]:
1196
  if event[0] == 'note':
1197
  song_f.append(event)
1198
  itrack += 1
 
1366
  score = MIDI.midi2score(open(f, 'rb').read())
1367
 
1368
  events_matrix = []
1369
+
1370
  itrack = 1
1371
 
1372
  while itrack < len(score):
1373
+ for event in score[itrack]:
1374
  events_matrix.append(event)
1375
  itrack += 1
1376
 
 
1378
  events_matrix.sort(key=lambda x: x[1])
1379
 
1380
  if len(events_matrix) > 0:
1381
+
1382
  #=======================================================
1383
  # INSERT YOUR CUSTOM ANAYLSIS CODE RIGHT HERE
1384
  #=======================================================
 
1394
 
1395
  except KeyboardInterrupt:
1396
  print('Saving current progress and quitting...')
1397
+ break
1398
 
1399
  except Exception as ex:
1400
  print('WARNING !!!')
 
1412
  print('=' * 70)
1413
  print('Total good processed MIDI files:', files_count)
1414
  print('=' * 70)
1415
+ print('Done!')
1416
  print('=' * 70)
1417
 
1418
  """# Congrats! You did it! :)"""
master_midi_dataset_search_and_filter.py CHANGED
@@ -6,7 +6,7 @@ Automatically generated by Colaboratory.
6
  Original file is located at
7
  https://colab.research.google.com/github/asigalov61/Los-Angeles-MIDI-Dataset/blob/main/Extras/Master_MIDI_Dataset_Search_and_Filter.ipynb
8
 
9
- # Master MIDI Dataset Search and Filter (ver. 1.0)
10
 
11
  ***
12
 
@@ -72,10 +72,10 @@ print('=' * 70)
72
  print('Downloading Los Angeles MIDI Dataset...Please wait...')
73
  print('=' * 70)
74
 
75
- hf_hub_download(repo_id='projectlosangeles/Los-Angeles-MIDI-Dataset',
76
- filename='Los-Angeles-MIDI-Dataset-Ver-2-0-CC-BY-NC-SA.zip',
77
  repo_type="dataset",
78
- local_dir='/content/Main-MIDI-Dataset',
79
  local_dir_use_symlinks=False)
80
  print('=' * 70)
81
  print('Done! Enjoy! :)')
@@ -87,7 +87,7 @@ print('=' * 70)
87
 
88
  print('=' * 70)
89
  print('Unzipping Los Angeles MIDI Dataset...Please wait...')
90
- !unzip 'Los-Angeles-MIDI-Dataset-Ver-2-0-CC-BY-NC-SA.zip'
91
  print('=' * 70)
92
 
93
  print('Done! Enjoy! :)')
@@ -191,7 +191,7 @@ files_count = 0
191
 
192
  for f in filez:
193
  try:
194
-
195
  input_files_count += 1
196
 
197
  fn = os.path.basename(f)
@@ -202,88 +202,10 @@ for f in filez:
202
 
203
  print('Processing MIDI File #', files_count+1, 'out of', len(filez))
204
  print('MIDI file name', fn)
205
- print('-' * 70)
206
 
207
  #=======================================================
208
 
209
- score = TMIDIX.midi2ms_score(open(f, 'rb').read())
210
-
211
- events_matrix = []
212
-
213
- itrack = 1
214
-
215
- while itrack < len(score):
216
- for event in score[itrack]:
217
- events_matrix.append(event)
218
- itrack += 1
219
-
220
- # Sorting...
221
- events_matrix.sort(key=lambda x: x[1])
222
-
223
- # recalculating timings
224
- for e in events_matrix:
225
- e[1] = int(e[1] / 10)
226
- if e[0] == 'note':
227
- e[2] = int(e[2] / 20)
228
-
229
- # final processing...
230
-
231
- melody_chords = []
232
-
233
- patches = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
234
-
235
- pe = events_matrix[0]
236
- for e in events_matrix:
237
-
238
- if e[0] == 'note':
239
- # ['note', start_time, duration, channel, note, velocity]
240
- time = max(0, min(255, e[1]-pe[1]))
241
- duration = max(1, min(255, e[2]))
242
- channel = max(0, min(15, e[3]))
243
-
244
- if e[3] != 9:
245
- instrument = max(0, min(127, patches[e[3]]))
246
- else:
247
- instrument = max(128, min(255, patches[e[3]]+128))
248
-
249
- if e[3] != 9:
250
-
251
- pitch = max(1, min(127, e[4]))
252
- else:
253
- pitch = max(129, min(255, e[4]+128))
254
-
255
- if e[3] != 9:
256
- velocity = max(1, min(127, e[5]))
257
- else:
258
- velocity = max(129, min(255, e[5]+128))
259
-
260
- melody_chords.append([time, duration, channel, instrument, pitch, velocity])
261
-
262
- if e[0] == 'patch_change':
263
- # ['patch_change', dtime, channel, patch]
264
- time = max(0, min(127, e[1]-pe[1]))
265
- channel = max(0, min(15, e[2]))
266
- patch = max(0, min(127, e[3]))
267
-
268
- patches[channel] = patch
269
-
270
- pe = e # Previous event
271
-
272
- MATRIX = [[0]*256 for i in range(38)]
273
-
274
- for m in melody_chords:
275
-
276
- MATRIX[0][m[0]] += 1
277
- MATRIX[1][m[1]] += 1
278
- MATRIX[2][m[2]] += 1
279
- MATRIX[3][m[3]] += 1
280
- MATRIX[4][m[4]] += 1
281
- MATRIX[5][m[5]] += 1
282
- MATRIX[m[2]+6][m[3]] += 1
283
- MATRIX[m[2]+22][m[4]] += 1
284
-
285
- #==================================================
286
-
287
  score = TMIDIX.midi2score(open(f, 'rb').read())
288
 
289
  events_matrix = []
@@ -291,7 +213,7 @@ for f in filez:
291
  track_count = 0
292
 
293
  for s in score:
294
-
295
  if track_count > 0:
296
  track = s
297
  track.sort(key=lambda x: x[1])
@@ -300,7 +222,7 @@ for f in filez:
300
  midi_ticks = s
301
 
302
  track_count += 1
303
-
304
  events_matrix.sort(key=lambda x: x[1])
305
 
306
  mult_pitches_counts = []
@@ -323,7 +245,7 @@ for f in filez:
323
 
324
  pitches_counts = [[y[0],y[1]] for y in Counter([y[4] for y in events_matrix1 if y[0] == 'note']).most_common()]
325
  pitches_counts.sort(key=lambda x: x[0], reverse=True)
326
-
327
  mult_pitches_counts.append(pitches_counts)
328
 
329
  patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))
@@ -337,7 +259,7 @@ for f in filez:
337
  itrack1 = 1
338
 
339
  while itrack1 < len(ms_score):
340
- for event in ms_score[itrack1]:
341
  if event[0] == 'note':
342
  ms_events_matrix.append(event)
343
  itrack1 += 1
@@ -362,7 +284,7 @@ for f in filez:
362
  cho.append(e[4] % 12)
363
 
364
  pe = e
365
-
366
  if len(cho) > 0:
367
  chords.append(sorted(cho))
368
 
@@ -376,7 +298,7 @@ for f in filez:
376
  times.append((e[1]-pt))
377
  start = False
378
  pt = e[1]
379
-
380
  durs = [e[2] for e in ms_events_matrix]
381
  vels = [e[5] for e in ms_events_matrix]
382
 
@@ -403,7 +325,7 @@ for f in filez:
403
  max_p_count = p_counts[0][1]
404
  trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)]
405
  total_p_counts = sum([y[1] for y in trimmed_p_counts])
406
-
407
  if search_transposed_pitches:
408
  search_pitches = mult_pitches_counts
409
  else:
@@ -419,8 +341,8 @@ for f in filez:
419
 
420
  if add_timings_ratios:
421
 
422
- source_times = [avg_time,
423
- median_time,
424
  mode_time]
425
 
426
  match_times = meta_data[0][1][3][1]
@@ -472,12 +394,12 @@ for f in filez:
472
 
473
  same_pitches = set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts])
474
  num_same_pitches = len(same_pitches)
475
-
476
  if num_same_pitches == len(trimmed_pitches_counts):
477
  same_pitches_ratio = (num_same_pitches / len(trimmed_p_counts))
478
  else:
479
  same_pitches_ratio = (num_same_pitches / max(len(trimmed_p_counts), len(trimmed_pitches_counts)))
480
-
481
  if skip_exact_matches:
482
  if same_pitches_ratio == 1:
483
  same_pitches_ratio = 0
@@ -518,26 +440,26 @@ for f in filez:
518
 
519
  if add_pitches_counts_ratios:
520
  r_list.append(spcrat[0])
521
-
522
  if add_timings_ratios:
523
  r_list.append(atrat[0])
524
-
525
  if add_durations_ratios:
526
  r_list.append(adrat[0])
527
 
528
  ratios_list.append(r_list)
529
 
530
  #===================================================
531
-
532
  avg_ratios_list = []
533
 
534
  for r in ratios_list:
535
  avg_ratios_list.append(sum(r) / len(r))
536
 
537
  #===================================================
538
-
539
  final_ratio = max(avg_ratios_list)
540
-
541
  if final_ratio > maximum_match_ratio_to_search_for:
542
  final_ratio = 0
543
 
@@ -563,7 +485,7 @@ for f in filez:
563
  ff = LAMD_files_list[ffn_idx][1]
564
 
565
  #=======================================================
566
-
567
  dir_str = str(fn1)
568
  copy_path = '/content/Output-MIDI-Dataset/'+dir_str
569
  if not os.path.exists(copy_path):
@@ -572,11 +494,11 @@ for f in filez:
572
  fff = str(m * 100) + '_' + ffn + '.mid'
573
 
574
  shutil.copy2(ff, copy_path+'/'+fff)
575
-
576
  shutil.copy2(f, copy_path+'/'+fn)
577
 
578
  #=======================================================
579
-
580
  print('Done!')
581
  print('=' * 70)
582
 
@@ -584,12 +506,12 @@ for f in filez:
584
 
585
  # Processed files counter
586
  files_count += 1
587
-
588
  except KeyboardInterrupt:
589
  print('Quitting...')
590
  print('Total number of processed MIDI files', files_count)
591
  print('=' * 70)
592
- break
593
 
594
  except Exception as ex:
595
  print('WARNING !!!')
 
6
  Original file is located at
7
  https://colab.research.google.com/github/asigalov61/Los-Angeles-MIDI-Dataset/blob/main/Extras/Master_MIDI_Dataset_Search_and_Filter.ipynb
8
 
9
+ # Master MIDI Dataset Search and Filter (ver. 3.0)
10
 
11
  ***
12
 
 
72
  print('Downloading Los Angeles MIDI Dataset...Please wait...')
73
  print('=' * 70)
74
 
75
+ hf_hub_download(repo_id='projectlosangeles/Los-Angeles-MIDI-Dataset',
76
+ filename='Los-Angeles-MIDI-Dataset-Ver-3-0-CC-BY-NC-SA.zip',
77
  repo_type="dataset",
78
+ local_dir='/content/Main-MIDI-Dataset',
79
  local_dir_use_symlinks=False)
80
  print('=' * 70)
81
  print('Done! Enjoy! :)')
 
87
 
88
  print('=' * 70)
89
  print('Unzipping Los Angeles MIDI Dataset...Please wait...')
90
+ !unzip 'Los-Angeles-MIDI-Dataset-Ver-3-0-CC-BY-NC-SA.zip'
91
  print('=' * 70)
92
 
93
  print('Done! Enjoy! :)')
 
191
 
192
  for f in filez:
193
  try:
194
+
195
  input_files_count += 1
196
 
197
  fn = os.path.basename(f)
 
202
 
203
  print('Processing MIDI File #', files_count+1, 'out of', len(filez))
204
  print('MIDI file name', fn)
205
+ print('-' * 70)
206
 
207
  #=======================================================
208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  score = TMIDIX.midi2score(open(f, 'rb').read())
210
 
211
  events_matrix = []
 
213
  track_count = 0
214
 
215
  for s in score:
216
+
217
  if track_count > 0:
218
  track = s
219
  track.sort(key=lambda x: x[1])
 
222
  midi_ticks = s
223
 
224
  track_count += 1
225
+
226
  events_matrix.sort(key=lambda x: x[1])
227
 
228
  mult_pitches_counts = []
 
245
 
246
  pitches_counts = [[y[0],y[1]] for y in Counter([y[4] for y in events_matrix1 if y[0] == 'note']).most_common()]
247
  pitches_counts.sort(key=lambda x: x[0], reverse=True)
248
+
249
  mult_pitches_counts.append(pitches_counts)
250
 
251
  patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))
 
259
  itrack1 = 1
260
 
261
  while itrack1 < len(ms_score):
262
+ for event in ms_score[itrack1]:
263
  if event[0] == 'note':
264
  ms_events_matrix.append(event)
265
  itrack1 += 1
 
284
  cho.append(e[4] % 12)
285
 
286
  pe = e
287
+
288
  if len(cho) > 0:
289
  chords.append(sorted(cho))
290
 
 
298
  times.append((e[1]-pt))
299
  start = False
300
  pt = e[1]
301
+
302
  durs = [e[2] for e in ms_events_matrix]
303
  vels = [e[5] for e in ms_events_matrix]
304
 
 
325
  max_p_count = p_counts[0][1]
326
  trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)]
327
  total_p_counts = sum([y[1] for y in trimmed_p_counts])
328
+
329
  if search_transposed_pitches:
330
  search_pitches = mult_pitches_counts
331
  else:
 
341
 
342
  if add_timings_ratios:
343
 
344
+ source_times = [avg_time,
345
+ median_time,
346
  mode_time]
347
 
348
  match_times = meta_data[0][1][3][1]
 
394
 
395
  same_pitches = set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts])
396
  num_same_pitches = len(same_pitches)
397
+
398
  if num_same_pitches == len(trimmed_pitches_counts):
399
  same_pitches_ratio = (num_same_pitches / len(trimmed_p_counts))
400
  else:
401
  same_pitches_ratio = (num_same_pitches / max(len(trimmed_p_counts), len(trimmed_pitches_counts)))
402
+
403
  if skip_exact_matches:
404
  if same_pitches_ratio == 1:
405
  same_pitches_ratio = 0
 
440
 
441
  if add_pitches_counts_ratios:
442
  r_list.append(spcrat[0])
443
+
444
  if add_timings_ratios:
445
  r_list.append(atrat[0])
446
+
447
  if add_durations_ratios:
448
  r_list.append(adrat[0])
449
 
450
  ratios_list.append(r_list)
451
 
452
  #===================================================
453
+
454
  avg_ratios_list = []
455
 
456
  for r in ratios_list:
457
  avg_ratios_list.append(sum(r) / len(r))
458
 
459
  #===================================================
460
+
461
  final_ratio = max(avg_ratios_list)
462
+
463
  if final_ratio > maximum_match_ratio_to_search_for:
464
  final_ratio = 0
465
 
 
485
  ff = LAMD_files_list[ffn_idx][1]
486
 
487
  #=======================================================
488
+
489
  dir_str = str(fn1)
490
  copy_path = '/content/Output-MIDI-Dataset/'+dir_str
491
  if not os.path.exists(copy_path):
 
494
  fff = str(m * 100) + '_' + ffn + '.mid'
495
 
496
  shutil.copy2(ff, copy_path+'/'+fff)
497
+
498
  shutil.copy2(f, copy_path+'/'+fn)
499
 
500
  #=======================================================
501
+
502
  print('Done!')
503
  print('=' * 70)
504
 
 
506
 
507
  # Processed files counter
508
  files_count += 1
509
+
510
  except KeyboardInterrupt:
511
  print('Quitting...')
512
  print('Total number of processed MIDI files', files_count)
513
  print('=' * 70)
514
+ break
515
 
516
  except Exception as ex:
517
  print('WARNING !!!')