{ "cells": [ { "cell_type": "markdown", "metadata": { "gradient": { "editing": false, "id": "ac5a4cf0-d9d2-47b5-9633-b53f8d99a4d2", "kernelId": "" }, "id": "SiTIpPjArIyr" }, "source": [ "# Los Angeles MIDI Dataset: Search and Explore (ver. 2.2)\n", "\n", "***\n", "\n", "Powered by tegridy-tools: https://github.com/asigalov61/tegridy-tools\n", "\n", "***\n", "\n", "#### Project Los Angeles\n", "\n", "#### Tegridy Code 2023\n", "\n", "***" ] }, { "cell_type": "markdown", "metadata": { "gradient": { "editing": false, "id": "fa0a611c-1803-42ae-bdf6-a49b5a4e781b", "kernelId": "" }, "id": "gOd93yV0sGd2" }, "source": [ "# (SETUP ENVIRONMENT)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "gradient": { "editing": false, "id": "a1a45a91-d909-4fd4-b67a-5e16b971d179", "kernelId": "" }, "id": "fX12Yquyuihc", "cellView": "form" }, "outputs": [], "source": [ "#@title Install all dependencies (run only once per session)\n", "!git clone --depth 1 https://github.com/asigalov61/Los-Angeles-MIDI-Dataset\n", "!pip install huggingface_hub\n", "!pip install matplotlib\n", "!pip install sklearn\n", "!pip install tqdm\n", "!apt install fluidsynth #Pip does not work for some reason. Only apt works\n", "!pip install midi2audio" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "gradient": { "editing": false, "id": "b8207b76-9514-4c07-95db-95a4742e52c5", "kernelId": "" }, "id": "z7n9vnKmug1J", "cellView": "form" }, "outputs": [], "source": [ "#@title Import all needed modules\n", "\n", "print('Loading core modules...')\n", "import os\n", "import copy\n", "from collections import Counter\n", "import random\n", "import pickle\n", "from tqdm import tqdm\n", "import pprint\n", "\n", "from joblib import Parallel, delayed\n", "import multiprocessing\n", "\n", "if not os.path.exists('/content/LAMD'):\n", " os.makedirs('/content/LAMD')\n", "\n", "print('Loading MIDI.py module...')\n", "os.chdir('/content/Los-Angeles-MIDI-Dataset')\n", "import MIDI\n", "\n", "print('Loading aux modules...')\n", "from sklearn.metrics import pairwise_distances, pairwise\n", "import matplotlib.pyplot as plt\n", "\n", "from midi2audio import FluidSynth\n", "from IPython.display import Audio, display\n", "\n", "from huggingface_hub import hf_hub_download\n", "\n", "from google.colab import files\n", "\n", "os.chdir('/content/')\n", "print('Done!')" ] }, { "cell_type": "markdown", "source": [ "# (PREP DATA)" ], "metadata": { "id": "Erj-5wMNA1Jd" } }, { "cell_type": "code", "source": [ "#@title Unzip LAMDa data\n", "%cd /content/Los-Angeles-MIDI-Dataset/META-DATA\n", "\n", "print('=' * 70)\n", "print('Unzipping META-DATA...Please wait...')\n", "\n", "!cat LAMDa_META_DATA.zip* > LAMDa_META_DATA.zip\n", "print('=' * 70)\n", "\n", "!unzip -j LAMDa_META_DATA.zip\n", "print('=' * 70)\n", "\n", "print('Done! Enjoy! :)')\n", "print('=' * 70)\n", "%cd /content/\n", "\n", "#================================================\n", "\n", "%cd /content/Los-Angeles-MIDI-Dataset/MIDI-MATRIXES\n", "\n", "print('=' * 70)\n", "print('Unzipping MIDI-MATRIXES...Please wait...')\n", "\n", "!cat LAMDa_MIDI_MATRIXES.zip* > LAMDa_MIDI_MATRIXES.zip\n", "print('=' * 70)\n", "\n", "!unzip -j LAMDa_MIDI_MATRIXES.zip\n", "print('=' * 70)\n", "\n", "print('Done! Enjoy! :)')\n", "print('=' * 70)\n", "%cd /content/\n", "\n", "#==================================================\n", "\n", "%cd /content/Los-Angeles-MIDI-Dataset/TOTALS\n", "\n", "print('=' * 70)\n", "print('Unzipping TOTALS...Please wait...')\n", "\n", "!unzip -j LAMDa_TOTALS.zip\n", "print('=' * 70)\n", "\n", "print('Done! Enjoy! :)')\n", "print('=' * 70)\n", "%cd /content/" ], "metadata": { "cellView": "form", "id": "Shs-6PR6ie7-" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title Load LAMDa data\n", "print('=' * 70)\n", "print('Loading LAMDa data...Please wait...')\n", "print('=' * 70)\n", "print('Loading LAMDa META-DATA...')\n", "meta_data = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/META-DATA/LAMDa_META_DATA.pickle', 'rb'))\n", "print('Done!')\n", "print('=' * 70)\n", "print('Loading LAMDa MIDI-MATRIXES...')\n", "midi_matrixes = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/MIDI-MATRIXES/LAMDa_MIDI_MATRIXES.pickle', 'rb'))\n", "print('Done!')\n", "print('=' * 70)\n", "print('Loading LAMDa TOTALS...')\n", "totals = pickle.load(open('/content/Los-Angeles-MIDI-Dataset/TOTALS/LAMDa_TOTALS.pickle', 'rb'))\n", "print('Done!')\n", "print('=' * 70)\n", "print('Enjoy!')\n", "print('=' * 70)" ], "metadata": { "id": "ML1CecXtjiGE", "cellView": "form" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# (PREP MIDI DATASET)" ], "metadata": { "id": "GDKQys8pFgtj" } }, { "cell_type": "code", "source": [ "#@title Download the dataset\n", "print('=' * 70)\n", "print('Downloading Los Angeles MIDI Dataset...Please wait...')\n", "print('=' * 70)\n", "\n", "hf_hub_download(repo_id='projectlosangeles/Los-Angeles-MIDI-Dataset', \n", " filename='Los-Angeles-MIDI-Dataset-Ver-2-0-CC-BY-NC-SA.zip',\n", " repo_type=\"dataset\",\n", " local_dir='/content/LAMD', \n", " local_dir_use_symlinks=False)\n", "print('=' * 70)\n", "print('Done! Enjoy! :)')\n", "print('=' * 70)" ], "metadata": { "id": "U_KPkpWMCkNO", "cellView": "form" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title Unzip the dataset\n", "%cd /content/LAMD\n", "\n", "print('=' * 70)\n", "print('Unzipping Los Angeles MIDI Dataset...Please wait...')\n", "!unzip 'Los-Angeles-MIDI-Dataset-Ver-2-0-CC-BY-NC-SA.zip'\n", "print('=' * 70)\n", "\n", "print('Done! Enjoy! :)')\n", "print('=' * 70)\n", "%cd /content/" ], "metadata": { "cellView": "form", "id": "mKFlrD98FMAP" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title Create dataset files list\n", "print('=' * 70)\n", "print('Creating dataset files list...')\n", "dataset_addr = \"/content/LAMD/MIDIs\"\n", "\n", "# os.chdir(dataset_addr)\n", "filez = list()\n", "for (dirpath, dirnames, filenames) in os.walk(dataset_addr):\n", " filez += [os.path.join(dirpath, file) for file in filenames]\n", "\n", "if filez == []:\n", " print('Could not find any MIDI files. Please check Dataset dir...')\n", " print('=' * 70)\n", "\n", "print('=' * 70)\n", "print('Randomizing file list...')\n", "random.shuffle(filez)\n", "print('=' * 70)\n", "\n", "LAMD_files_list = []\n", "\n", "for f in tqdm(filez):\n", " LAMD_files_list.append([f.split('/')[-1].split('.mid')[0], f])\n", "print('Done!')\n", "print('=' * 70)" ], "metadata": { "cellView": "form", "id": "nMRMnn7rHBg5" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# (PLOT TOTALS)" ], "metadata": { "id": "ta6K3AOOqhpM" } }, { "cell_type": "code", "source": [ "#@title Plot totals from MIDI matrixes (legacy)\n", "\n", "cos_sim = pairwise.cosine_similarity(\n", " totals[0][0][4] \n", " )\n", "plt.figure(figsize=(8, 8))\n", "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n", "im_ratio = 1\n", "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n", "plt.title('Times')\n", "plt.xlabel(\"Position\")\n", "plt.ylabel(\"Position\")\n", "plt.tight_layout()\n", "plt.plot()\n", "\n", "cos_sim = pairwise.cosine_similarity(\n", " totals[0][0][5] \n", " )\n", "plt.figure(figsize=(8, 8))\n", "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n", "im_ratio = 1\n", "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n", "plt.title('Durations')\n", "plt.xlabel(\"Position\")\n", "plt.ylabel(\"Position\")\n", "plt.tight_layout()\n", "plt.plot()\n", "\n", "cos_sim = pairwise.cosine_similarity(\n", " totals[0][0][6] \n", " )\n", "plt.figure(figsize=(8, 8))\n", "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n", "im_ratio = 1\n", "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n", "plt.title('Channels')\n", "plt.xlabel(\"Position\")\n", "plt.ylabel(\"Position\")\n", "plt.tight_layout()\n", "plt.plot()\n", "\n", "cos_sim = pairwise.cosine_similarity(\n", " totals[0][0][7] \n", " )\n", "plt.figure(figsize=(8, 8))\n", "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n", "im_ratio = 1\n", "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n", "plt.title('Instruments')\n", "plt.xlabel(\"Position\")\n", "plt.ylabel(\"Position\")\n", "plt.tight_layout()\n", "plt.plot()\n", "\n", "cos_sim = pairwise.cosine_similarity(\n", " totals[0][0][8] \n", " )\n", "plt.figure(figsize=(8, 8))\n", "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n", "im_ratio = 1\n", "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n", "plt.title('Pitches')\n", "plt.xlabel(\"Position\")\n", "plt.ylabel(\"Position\")\n", "plt.tight_layout()\n", "plt.plot()\n", "\n", "cos_sim = pairwise.cosine_similarity(\n", " totals[0][0][9] \n", " )\n", "plt.figure(figsize=(8, 8))\n", "plt.imshow(cos_sim, cmap=\"inferno\", interpolation=\"none\")\n", "im_ratio = 1\n", "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n", "plt.title('Velocities')\n", "plt.xlabel(\"Position\")\n", "plt.ylabel(\"Position\")\n", "plt.tight_layout()\n", "plt.plot()" ], "metadata": { "cellView": "form", "id": "Tk-3Q3UnqkpL" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title Plot totals from MIDI metadata\n", "\n", "#===============================================================================\n", "\n", "pitches_counts_totals = [0] * 128\n", "\n", "for m in tqdm(meta_data):\n", " for mm in m[1][10][1]:\n", " if mm[0] < 128:\n", " pitches_counts_totals[mm[0]] += mm[1]\n", "\n", "y = range(128)\n", "plt.figure(figsize=(8, 8))\n", "plt.plot(y, pitches_counts_totals)\n", "\n", "plt.title('MIDI Instruments Pitches')\n", "plt.xlabel(\"Ratio\")\n", "plt.ylabel(\"Pitch\")\n", "plt.tight_layout()\n", "plt.plot()\n", "\n", "sim_mat = [ [0]*128 for i in range(128)]\n", "x = 0\n", "\n", "for p in pitches_counts_totals:\n", " y = 0\n", " for pp in pitches_counts_totals:\n", "\n", " sim_mat[x][y] = min(10, (p / pp))\n", " y += 1\n", "\n", " x += 1\n", "\n", "cos_sim = pairwise.cosine_similarity(\n", " sim_mat \n", " )\n", "plt.figure(figsize=(8, 8))\n", "plt.imshow(sim_mat, cmap=\"inferno\", interpolation=\"none\")\n", "im_ratio = 1\n", "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n", "plt.title('MIDI Instruments Pitches')\n", "plt.xlabel(\"Ratio\")\n", "plt.ylabel(\"Pitch\")\n", "plt.tight_layout()\n", "plt.plot()\n", "\n", "#===============================================================================\n", "\n", "pitches_counts_totals = [1] * 128\n", "\n", "\n", "for m in tqdm(meta_data):\n", " for mm in m[1][10][1]:\n", " if mm[0] > 128:\n", " pitches_counts_totals[mm[0] % 128] += mm[1]\n", "\n", "y = range(128)\n", "plt.figure(figsize=(8, 8))\n", "plt.plot(y, pitches_counts_totals)\n", "\n", "plt.title('MIDI Drums Pitches')\n", "plt.xlabel(\"Ratio\")\n", "plt.ylabel(\"Pitch\")\n", "plt.tight_layout()\n", "plt.plot()\n", "\n", "sim_mat = [ [0]*128 for i in range(128)]\n", "x = 0\n", "\n", "for p in pitches_counts_totals:\n", " y = 0\n", " for pp in pitches_counts_totals:\n", "\n", " sim_mat[x][y] = min(10, (p / pp))\n", " y += 1\n", "\n", " x += 1\n", "\n", "cos_sim = pairwise.cosine_similarity(\n", " sim_mat \n", " )\n", "plt.figure(figsize=(8, 8))\n", "plt.imshow(sim_mat, cmap=\"inferno\", interpolation=\"none\")\n", "im_ratio = 1\n", "plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)\n", "plt.title('MIDI Drums Pitches')\n", "plt.xlabel(\"Ratio\")\n", "plt.ylabel(\"Pitch\")\n", "plt.tight_layout()\n", "plt.plot()\n", "\n", "#===============================================================================\n", "\n", "patches_counts_totals = [0] * 256\n", "\n", "\n", "for m in tqdm(meta_data):\n", " for mm in m[1][11][1]:\n", " patches_counts_totals[mm[0]] += mm[1]\n", "\n", "\n", "y = range(128)\n", "plt.figure(figsize=(8, 8))\n", "plt.plot(y, patches_counts_totals[:128])\n", "\n", "plt.title('MIDI Patches')\n", "plt.xlabel(\"Ratio\")\n", "plt.ylabel('Patch')\n", "plt.tight_layout()\n", "plt.plot()" ], "metadata": { "cellView": "form", "id": "NLo2pUSQjpgg" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# (LOAD SOURCE MIDI)" ], "metadata": { "id": "jiVsfC0u7aAQ" } }, { "cell_type": "code", "source": [ "#@title Load source MIDI\n", "\n", "full_path_to_source_MIDI = \"/content/Los-Angeles-MIDI-Dataset/Come-To-My-Window-Modified-Sample-MIDI.mid\" #@param {type:\"string\"}\n", "render_MIDI_to_audio = False #@param {type:\"boolean\"}\n", "\n", "#=================================================================================\n", "\n", "f = full_path_to_source_MIDI\n", "\n", "print('=' * 70)\n", "print('Loading MIDI file...')\n", "\n", "score = MIDI.midi2ms_score(open(f, 'rb').read())\n", "\n", "events_matrix = []\n", "\n", "itrack = 1\n", "\n", "while itrack < len(score):\n", " for event in score[itrack]: \n", " events_matrix.append(event)\n", " itrack += 1\n", "\n", "# Sorting...\n", "events_matrix.sort(key=lambda x: x[1])\n", "\n", "# recalculating timings\n", "for e in events_matrix:\n", " e[1] = int(e[1] / 10)\n", " if e[0] == 'note':\n", " e[2] = int(e[2] / 20)\n", "\n", "# final processing...\n", "\n", "melody_chords = []\n", "\n", "patches = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n", "\n", "pe = events_matrix[0]\n", "for e in events_matrix:\n", "\n", " if e[0] == 'note':\n", " # ['note', start_time, duration, channel, note, velocity]\n", " time = max(0, min(255, e[1]-pe[1]))\n", " duration = max(1, min(255, e[2]))\n", " channel = max(0, min(15, e[3]))\n", "\n", " if e[3] != 9:\n", " instrument = max(0, min(127, patches[e[3]]))\n", " else:\n", " instrument = max(128, min(255, patches[e[3]]+128))\n", "\n", " if e[3] != 9:\n", "\n", " pitch = max(1, min(127, e[4]))\n", " else:\n", " pitch = max(129, min(255, e[4]+128))\n", "\n", " if e[3] != 9:\n", " velocity = max(1, min(127, e[5]))\n", " else:\n", " velocity = max(129, min(255, e[5]+128))\n", "\n", " melody_chords.append([time, duration, channel, instrument, pitch, velocity])\n", "\n", " if e[0] == 'patch_change':\n", " # ['patch_change', dtime, channel, patch]\n", " time = max(0, min(127, e[1]-pe[1]))\n", " channel = max(0, min(15, e[2]))\n", " patch = max(0, min(127, e[3]))\n", "\n", " patches[channel] = patch\n", "\n", " pe = e # Previous event\n", "\n", "MATRIX = [[0]*256 for i in range(38)]\n", "\n", "for m in melody_chords:\n", "\n", " MATRIX[0][m[0]] += 1\n", " MATRIX[1][m[1]] += 1\n", " MATRIX[2][m[2]] += 1 \n", " MATRIX[3][m[3]] += 1\n", " MATRIX[4][m[4]] += 1\n", " MATRIX[5][m[5]] += 1\n", " MATRIX[m[2]+6][m[3]] += 1\n", " MATRIX[m[2]+22][m[4]] += 1\n", "\n", "#==================================================\n", "\n", "score = MIDI.midi2score(open(f, 'rb').read())\n", "\n", "events_matrix = []\n", "\n", "track_count = 0\n", "\n", "for s in score:\n", " \n", " if track_count > 0:\n", " track = s\n", " track.sort(key=lambda x: x[1])\n", " events_matrix.extend(track)\n", " else:\n", " midi_ticks = s\n", "\n", " track_count += 1\n", " \n", "events_matrix.sort(key=lambda x: x[1])\n", "\n", "mult_pitches_counts = []\n", "\n", "for i in range(-6, 6):\n", "\n", " events_matrix1 = []\n", "\n", " for e in events_matrix:\n", "\n", " ev = copy.deepcopy(e)\n", "\n", " if e[0] == 'note':\n", " if e[3] == 9:\n", " ev[4] = ((e[4] % 128) + 128)\n", " else:\n", " ev[4] = ((e[4] % 128) + i)\n", "\n", " events_matrix1.append(ev)\n", "\n", " pitches_counts = [[y[0],y[1]] for y in Counter([y[4] for y in events_matrix1 if y[0] == 'note']).most_common()]\n", " pitches_counts.sort(key=lambda x: x[0], reverse=True)\n", " \n", " mult_pitches_counts.append(pitches_counts)\n", "\n", "patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))\n", "\n", "print('=' * 70)\n", "print('Done!')\n", "print('=' * 70)\n", "\n", "#============================================\n", "# MIDI rendering code\n", "#============================================\n", "\n", "print('Rendering source MIDI...')\n", "print('=' * 70)\n", "\n", "ms_score = MIDI.midi2ms_score(open(f, 'rb').read())\n", "\n", "itrack = 1\n", "song_f = []\n", "\n", "while itrack < len(ms_score):\n", " for event in ms_score[itrack]: \n", " if event[0] == 'note':\n", " song_f.append(event)\n", " itrack += 1\n", "\n", "song_f.sort(key=lambda x: x[1])\n", "\n", "fname = f.split('.mid')[0]\n", "\n", "x = []\n", "y =[]\n", "c = []\n", "\n", "colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver', 'aqua', 'azure', 'bisque', 'coral']\n", "\n", "for s in song_f:\n", " x.append(s[1] / 1000)\n", " y.append(s[4])\n", " c.append(colors[s[3]])\n", "\n", "if render_MIDI_to_audio:\n", " FluidSynth(\"/usr/share/sounds/sf2/FluidR3_GM.sf2\", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))\n", " display(Audio(str(fname + '.wav'), rate=16000))\n", "\n", "plt.figure(figsize=(14,5))\n", "ax=plt.axes(title=fname)\n", "ax.set_facecolor('black')\n", "\n", "plt.scatter(x,y, c=c)\n", "plt.xlabel(\"Time\")\n", "plt.ylabel(\"Pitch\")\n", "plt.show()" ], "metadata": { "id": "z7mm6WsDmuRi", "cellView": "form" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# (SEARCH AND EXPLORE)" ], "metadata": { "id": "LRnuphuiqc5F" } }, { "cell_type": "code", "source": [ "#@title Legacy MIDI Matrixes Search (Slow)\n", "\n", "#@markdown NOTE: You can stop the search at any time to render partial results\n", "\n", "minimum_match_ratio_to_search_for = 0 #@param {type:\"slider\", min:0, max:500, step:1}\n", "stop_search_on_exact_match = True #@param {type:\"boolean\"}\n", "skip_exact_matches = False #@param {type:\"boolean\"}\n", "render_MIDI_to_audio = False #@param {type:\"boolean\"}\n", "download_MIDI = False #@param {type:\"boolean\"}\n", "\n", "#=================================================================================\n", "\n", "matching_type = \"minkowski\"\n", "\n", "def compress_matrix(midi_matrix):\n", "\n", " MX = 38\n", " MY = 256\n", "\n", " if len(midi_matrix) == MX:\n", "\n", " compressed_matrix = []\n", " zeros = 0\n", " zeros_shift = 0\n", " zeros_count = 0\n", "\n", " for m in midi_matrix:\n", " for mm in m:\n", " zeros_shift = max(zeros_shift, mm) + 1\n", "\n", " compressed_matrix.append(zeros_shift)\n", "\n", " for m in midi_matrix:\n", " if len(m) == MY:\n", " for mm in m:\n", " if mm != 0:\n", " if zeros > 0:\n", " compressed_matrix.append(zeros+zeros_shift)\n", " zeros = 0\n", " compressed_matrix.append(mm)\n", " \n", " else:\n", " zeros += 1\n", " zeros_count += 1\n", " \n", " else:\n", " print('Wrong matrix format!')\n", " return [1]\n", "\n", " if zeros > 0:\n", " compressed_matrix.append(zeros+zeros_shift)\n", "\n", " compressed_matrix.append(zeros_count+zeros_shift)\n", " compressed_matrix.append(zeros_shift)\n", "\n", " return compressed_matrix\n", "\n", " else:\n", " print('Wrong matrix format!')\n", " return [0]\n", "\n", "#=================================================================================\n", "\n", "def decompress_matrix(compressed_midi_matrix):\n", "\n", " MX = 38\n", " MY = 256\n", "\n", " zeros_count = 0\n", "\n", " temp_matrix = []\n", " decompressed_matrix = [[0]*MY for i in range(MX)]\n", "\n", " if compressed_midi_matrix[0] == compressed_midi_matrix[-1]:\n", " zeros_shift = compressed_midi_matrix[0]\n", " mcount = 0\n", "\n", " for c in compressed_midi_matrix[1:-2]:\n", " if c > zeros_shift:\n", " temp_matrix.extend([0] * (c-zeros_shift))\n", " zeros_count += (c-zeros_shift)\n", "\n", " else:\n", " temp_matrix.extend([c])\n", "\n", " if len(temp_matrix) == (MX * MY):\n", "\n", " for i in range(MX):\n", " for j in range(MY):\n", " decompressed_matrix[i][j] = copy.deepcopy(temp_matrix[(i*MY) + j])\n", " \n", " if len(decompressed_matrix) == MX and zeros_count == (compressed_midi_matrix[-2]-zeros_shift):\n", " return decompressed_matrix\n", "\n", " else:\n", " print('Matrix is corrupted!')\n", " return [len(decompressed_matrix), (MX * MY), zeros_count, (compressed_midi_matrix[-2]-zeros_shift)]\n", " \n", " else:\n", " print('Matrix is corrupted!')\n", " return [len(temp_matrix), zeros_count]\n", "\n", " else:\n", " print('Matrix is corrupted!')\n", " return [0]\n", "\n", "#=================================================================================\n", "\n", "def batched_scores(matbatch, matrix):\n", "\n", " sco= []\n", " for D in matbatch:\n", "\n", " dist = pairwise_distances(matrix, decompress_matrix(D[1]), metric=matching_type)[0][0]\n", "\n", " if skip_exact_matches:\n", " if dist == 0:\n", " dist = 999999\n", "\n", " if dist <= minimum_match_ratio_to_search_for:\n", " dist = 999999\n", "\n", " sco.append(dist)\n", "\n", " return sco\n", "\n", "#=================================================================================\n", "\n", "print('=' * 70)\n", "print('Searching...Please wait...')\n", "print('=' * 70)\n", "\n", "scores = []\n", "\n", "c_count = multiprocessing.cpu_count()\n", "\n", "par = Parallel(n_jobs=c_count)\n", "\n", "num_jobs = c_count\n", "scores_per_job = 100\n", "\n", "MATRIX_X = [MATRIX] * num_jobs\n", "\n", "for i in tqdm(range(0, len(midi_matrixes), (num_jobs*scores_per_job))):\n", " \n", " try:\n", "\n", " MAT_BATCHES = []\n", "\n", " for j in range(num_jobs):\n", " MAT_BATCHES.append(midi_matrixes[i+(j*scores_per_job):i+((j+1)*scores_per_job)])\n", "\n", " output = par(delayed(batched_scores) (MB, MAT) for MB, MAT in zip(MAT_BATCHES, MATRIX_X))\n", "\n", " output1 = []\n", "\n", " for o in output:\n", " output1.extend(o)\n", "\n", " scores.extend(output1) \n", "\n", " if stop_search_on_exact_match:\n", " if 0 in output1:\n", " print('=' * 70)\n", " print('Found exact match!')\n", " print('Stoping further search...')\n", " print('=' * 70)\n", " break\n", "\n", " else:\n", " if 0 in output1:\n", " print('=' * 70)\n", " print('Found exact match!')\n", " print('=' * 70)\n", " print('LAMDa Index:', scores.index(min(scores)))\n", " print('LAMDa File Name:', midi_matrixes[scores.index(min(scores))][0])\n", " print('=' * 70)\n", " print('Continuing search...')\n", " print('=' * 70)\n", "\n", " except KeyboardInterrupt:\n", " break\n", " \n", " except:\n", " continue\n", "\n", "print('Done!')\n", "print('=' * 70)\n", "print('Best match:')\n", "print('=' * 70) \n", "print(matching_type.title(), 'distance ==', min(scores))\n", "print('LAMDa Index:', scores.index(min(scores)))\n", "print('LAMDa File Name:', midi_matrixes[scores.index(min(scores))][0])\n", "print('=' * 70)\n", "\n", "#============================================\n", "# MIDI rendering code\n", "#============================================\n", "\n", "print('Rendering source MIDI...')\n", "print('=' * 70)\n", "\n", "fn = midi_matrixes[scores.index(min(scores))][0]\n", "\n", "try:\n", " fn_idx = [y[0] for y in LAMD_files_list].index(fn)\n", "\n", " f = LAMD_files_list[fn_idx][1]\n", "\n", " ms_score = MIDI.midi2ms_score(open(f, 'rb').read())\n", "\n", " itrack = 1\n", " song_f = []\n", "\n", " while itrack < len(ms_score):\n", " for event in ms_score[itrack]: \n", " if event[0] == 'note':\n", " song_f.append(event)\n", " itrack += 1\n", "\n", " song_f.sort(key=lambda x: x[1])\n", "\n", " fname = f.split('.mid')[0]\n", "\n", " x = []\n", " y =[]\n", " c = []\n", "\n", " colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver', 'aqua', 'azure', 'bisque', 'coral']\n", "\n", " for s in song_f:\n", " x.append(s[1] / 1000)\n", " y.append(s[4])\n", " c.append(colors[s[3]])\n", "\n", " if render_MIDI_to_audio:\n", " FluidSynth(\"/usr/share/sounds/sf2/FluidR3_GM.sf2\", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))\n", " display(Audio(str(fname + '.wav'), rate=16000))\n", "\n", " plt.figure(figsize=(14,5))\n", " ax=plt.axes(title=fname)\n", " ax.set_facecolor('black')\n", "\n", " plt.scatter(x,y, c=c)\n", " plt.xlabel(\"Time\")\n", " plt.ylabel(\"Pitch\")\n", " plt.show()\n", "\n", "except:\n", " pass\n", "#==============================================\n", "\n", "if download_MIDI:\n", " print('=' * 70)\n", " print('Downloading MIDI file', str(fn) + '.mid')\n", " files.download(f)\n", " print('=' * 70)\n", " \n", "#============================================\n", "\n", "print('Top 100 matches')\n", "print('=' * 70)\n", "\n", "top_matches = []\n", "\n", "for i in range(len(scores)):\n", " top_matches.append([midi_matrixes[i][0], scores[i]])\n", "\n", "top_matches.sort(key=lambda x: x[1])\n", "\n", "for t in top_matches[:100]:\n", " print(t)\n", " \n", "print('=' * 70)" ], "metadata": { "cellView": "form", "id": "pNQG6Qgfrlfi" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title MIDI Pitches Search (Fast)\n", "\n", "#@markdown NOTE: You can stop the search at any time to render partial results\n", "\n", "maximum_match_ratio_to_search_for = 1 #@param {type:\"slider\", min:0, max:1, step:0.01}\n", "pitches_counts_cutoff_threshold_ratio = 0 #@param {type:\"slider\", min:0, max:1, step:0.05}\n", "search_transposed_pitches = False #@param {type:\"boolean\"}\n", "skip_exact_matches = False #@param {type:\"boolean\"}\n", "render_MIDI_to_audio = False #@param {type:\"boolean\"}\n", "download_MIDI = False #@param {type:\"boolean\"}\n", "\n", "print('=' * 70)\n", "print('MIDI Pitches Search')\n", "print('=' * 70)\n", "\n", "ratios = []\n", "\n", "for d in tqdm(meta_data):\n", "\n", " try:\n", " p_counts = d[1][10][1]\n", " p_counts.sort(reverse = True, key = lambda x: x[1])\n", " max_p_count = p_counts[1][0]\n", " trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)] \n", " \n", " if search_transposed_pitches:\n", " search_pitches = mult_pitches_counts\n", " else:\n", " search_pitches = [mult_pitches_counts[6]]\n", " \n", " rat = []\n", "\n", " for m in search_pitches:\n", "\n", " m.sort(reverse = True, key = lambda x: x[1])\n", " max_pitches_count = m[1][0]\n", " trimmed_pitches_counts = [y for y in m if y[1] >= (max_pitches_count * pitches_counts_cutoff_threshold_ratio)] \n", "\n", " num_same_pitches = len(set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts]))\n", " same_pitches_ratio = (num_same_pitches / len(set([m[0] for m in trimmed_p_counts]+[T[0] for T in trimmed_pitches_counts])))\n", "\n", " if skip_exact_matches:\n", " if same_pitches_ratio == 1:\n", " same_pitches_ratio = 0\n", "\n", " if same_pitches_ratio > maximum_match_ratio_to_search_for:\n", " same_pitches_ratio = 0\n", "\n", " rat.append(same_pitches_ratio)\n", "\n", " ratios.append(max(rat))\n", " \n", " except KeyboardInterrupt:\n", " break\n", " \n", " except:\n", " break\n", "\n", "max_ratio = max(ratios)\n", "max_ratio_index = ratios.index(max(ratios))\n", "\n", "print('FOUND')\n", "print('=' * 70)\n", "print('Match ratio', max_ratio)\n", "print('MIDI file name', meta_data[max_ratio_index][0])\n", "print('=' * 70)\n", "pprint.pprint(['Sample metadata entries', meta_data[max_ratio_index][1][:8]], compact = True)\n", "print('=' * 70)\n", "\n", "#============================================\n", "# MIDI rendering code\n", "#============================================\n", "\n", "print('Rendering source MIDI...')\n", "print('=' * 70)\n", "\n", "fn = meta_data[max_ratio_index][0]\n", "fn_idx = [y[0] for y in LAMD_files_list].index(fn)\n", "\n", "f = LAMD_files_list[fn_idx][1]\n", "\n", "ms_score = MIDI.midi2ms_score(open(f, 'rb').read())\n", "\n", "itrack = 1\n", "song_f = []\n", "\n", "while itrack < len(ms_score):\n", " for event in ms_score[itrack]: \n", " if event[0] == 'note':\n", " song_f.append(event)\n", " itrack += 1\n", "\n", "song_f.sort(key=lambda x: x[1])\n", "\n", "fname = f.split('.mid')[0]\n", "\n", "x = []\n", "y =[]\n", "c = []\n", "\n", "colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver', 'aqua', 'azure', 'bisque', 'coral']\n", "\n", "for s in song_f:\n", " x.append(s[1] / 1000)\n", " y.append(s[4])\n", " c.append(colors[s[3]])\n", "\n", "if render_MIDI_to_audio:\n", " FluidSynth(\"/usr/share/sounds/sf2/FluidR3_GM.sf2\", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))\n", " display(Audio(str(fname + '.wav'), rate=16000))\n", "\n", "plt.figure(figsize=(14,5))\n", "ax=plt.axes(title=fname)\n", "ax.set_facecolor('black')\n", "\n", "plt.scatter(x,y, c=c)\n", "plt.xlabel(\"Time\")\n", "plt.ylabel(\"Pitch\")\n", "plt.show()\n", "\n", "#==============================================\n", "\n", "if download_MIDI:\n", " print('=' * 70)\n", " print('Downloading MIDI file', str(fn) + '.mid')\n", " files.download(f)\n", " print('=' * 70)" ], "metadata": { "cellView": "form", "id": "C-P71_9ojh8G" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title MIDI Patches Search (Fast)\n", "\n", "#@markdown NOTE: You can stop the search at any time to render partial results\n", "\n", "maximum_match_ratio_to_search_for = 1 #@param {type:\"slider\", min:0, max:1, step:0.01}\n", "skip_exact_matches = False #@param {type:\"boolean\"}\n", "render_MIDI_to_audio = False #@param {type:\"boolean\"}\n", "download_MIDI = False #@param {type:\"boolean\"}\n", "\n", "print('=' * 70)\n", "print('MIDI Patches Search')\n", "print('=' * 70)\n", "\n", "ratios = []\n", "\n", "for d in tqdm(meta_data):\n", "\n", " try:\n", "\n", " p_list= d[1][12][1]\n", "\n", " num_same_patches = len(set(p_list) & set(patches_list))\n", " \n", " if len(set(p_list + patches_list)) > 0:\n", " same_patches_ratio = num_same_patches / len(set(p_list + patches_list))\n", " else:\n", " same_patches_ratio = 0\n", "\n", " if skip_exact_matches:\n", " if same_patches_ratio == 1:\n", " same_patches_ratio = 0\n", "\n", " if same_patches_ratio > maximum_match_ratio_to_search_for:\n", " same_patches_ratio = 0\n", " \n", " ratios.append(same_patches_ratio)\n", " \n", " except KeyboardInterrupt:\n", " break\n", " \n", " except:\n", " break\n", "\n", "max_ratio = max(ratios)\n", "max_ratio_index = ratios.index(max(ratios))\n", "\n", "print('FOUND')\n", "print('=' * 70)\n", "print('Match ratio', max_ratio)\n", "print('MIDI file name', meta_data[max_ratio_index][0])\n", "print('=' * 70)\n", "print('Found MIDI patches list', meta_data[max_ratio_index][1][12][1])\n", "print('=' * 70)\n", "\n", "#============================================\n", "# MIDI rendering code\n", "#============================================\n", "\n", "print('Rendering source MIDI...')\n", "print('=' * 70)\n", "\n", "fn = meta_data[max_ratio_index][0]\n", "fn_idx = [y[0] for y in LAMD_files_list].index(fn)\n", "\n", "f = LAMD_files_list[fn_idx][1]\n", "\n", "ms_score = MIDI.midi2ms_score(open(f, 'rb').read())\n", "\n", "itrack = 1\n", "song_f = []\n", "\n", "while itrack < len(ms_score):\n", " for event in ms_score[itrack]: \n", " if event[0] == 'note':\n", " song_f.append(event)\n", " itrack += 1\n", "\n", "song_f.sort(key=lambda x: x[1])\n", "\n", "fname = f.split('.mid')[0]\n", "\n", "x = []\n", "y =[]\n", "c = []\n", "\n", "colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver', 'aqua', 'azure', 'bisque', 'coral']\n", "\n", "for s in song_f:\n", " x.append(s[1] / 1000)\n", " y.append(s[4])\n", " c.append(colors[s[3]])\n", "\n", "if render_MIDI_to_audio:\n", " FluidSynth(\"/usr/share/sounds/sf2/FluidR3_GM.sf2\", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))\n", " display(Audio(str(fname + '.wav'), rate=16000))\n", "\n", "plt.figure(figsize=(14,5))\n", "ax=plt.axes(title=fname)\n", "ax.set_facecolor('black')\n", "\n", "plt.scatter(x,y, c=c)\n", "plt.xlabel(\"Time\")\n", "plt.ylabel(\"Pitch\")\n", "plt.show()\n", "\n", "#==============================================\n", "\n", "if download_MIDI:\n", " print('=' * 70)\n", " print('Downloading MIDI file', str(fn) + '.mid')\n", " files.download(f)\n", " print('=' * 70)" ], "metadata": { "cellView": "form", "id": "JmV8VNj-tz1m" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#@title Metadata Search\n", "\n", "#@markdown You can search the metadata by search query or by MIDI md5 hash file name\n", "\n", "search_query = \"Come To My Window\" #@param {type:\"string\"}\n", "md5_hash_MIDI_file_name = \"d9a7e1c6a375b8e560155a5977fc10f8\" #@param {type:\"string\"}\n", "case_sensitive_search = False #@param {type:\"boolean\"}\n", "\n", "fields_to_search = ['track_name', \n", " 'text_event', \n", " 'lyric', \n", " 'copyright_text_event', \n", " 'marker',\n", " 'text_event_08',\n", " 'text_event_09',\n", " 'text_event_0a',\n", " 'text_event_0b',\n", " 'text_event_0c',\n", " 'text_event_0d',\n", " 'text_event_0e',\n", " 'text_event_0f',\n", " ]\n", "\n", "print('=' * 70)\n", "print('Los Angeles MIDI Dataset Metadata Search')\n", "print('=' * 70)\n", "print('Searching...')\n", "print('=' * 70)\n", "\n", "if md5_hash_MIDI_file_name != '':\n", " for d in tqdm(meta_data):\n", " try:\n", " if d[0] == md5_hash_MIDI_file_name:\n", " print('Found!')\n", " print('=' * 70)\n", " print('Metadata index:', meta_data.index(d))\n", " print('MIDI file name:', meta_data[meta_data.index(d)][0])\n", " print('-' * 70)\n", " pprint.pprint(['Result:', d[1][:16]], compact = True)\n", " print('=' * 70)\n", " break\n", " \n", " except KeyboardInterrupt:\n", " print('Ending search...')\n", " print('=' * 70)\n", " break\n", " \n", " except:\n", " print('Ending search...')\n", " print('=' * 70)\n", " break\n", "\n", " if d[0] != md5_hash_MIDI_file_name:\n", " print('Not found!')\n", " print('=' * 70)\n", " print('md5 hash was not found!')\n", " print('Ending search...')\n", " print('=' * 70)\n", "\n", "else:\n", " for d in tqdm(meta_data):\n", " try:\n", " for dd in d[1]:\n", " if dd[0] in fields_to_search:\n", " if case_sensitive_search:\n", " if str(search_query) in str(dd[2]):\n", " print('Found!')\n", " print('=' * 70)\n", " print('Metadata index:', meta_data.index(d))\n", " print('MIDI file name:', meta_data[meta_data.index(d)][0])\n", " print('-' * 70)\n", " pprint.pprint(['Result:', dd[2][:16]], compact = True)\n", " print('=' * 70)\n", " \n", " else:\n", " if str(search_query).lower() in str(dd[2]).lower():\n", " print('Found!')\n", " print('=' * 70)\n", " print('Metadata index:', meta_data.index(d))\n", " print('MIDI file name:', meta_data[meta_data.index(d)][0])\n", " print('-' * 70)\n", " pprint.pprint(['Result:', dd[2][:16]], compact = True)\n", " print('=' * 70)\n", " \n", " except KeyboardInterrupt:\n", " print('Ending search...')\n", " print('=' * 70)\n", " break\n", " \n", " except:\n", " print('Ending search...')\n", " print('=' * 70)\n", " break" ], "metadata": { "cellView": "form", "id": "98JuBEdMqIE3" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# (MIDI FILE PLAYER)" ], "metadata": { "id": "ih7bAzjv2amL" } }, { "cell_type": "code", "source": [ "#@title MIDI file player\n", "\n", "#@markdown NOTE: You can use md5 hash file name or full MIDI file path to play it\n", "\n", "md5_hash_MIDI_file_name = \"d9a7e1c6a375b8e560155a5977fc10f8\" #@param {type:\"string\"}\n", "full_path_to_MIDI = \"/content/Los-Angeles-MIDI-Dataset/Come-To-My-Window-Modified-Sample-MIDI.mid\" #@param {type:\"string\"}\n", "render_MIDI_to_audio = False #@param {type:\"boolean\"}\n", "download_MIDI = False #@param {type:\"boolean\"}\n", "\n", "#============================================\n", "# MIDI rendering code\n", "#============================================\n", "\n", "print('=' * 70)\n", "print('MIDI file player')\n", "print('=' * 70)\n", "\n", "try:\n", "\n", " if os.path.exists(full_path_to_MIDI):\n", " f = full_path_to_MIDI\n", " print('Using full path to MIDI')\n", "\n", " else:\n", " fn = md5_hash_MIDI_file_name\n", " fn_idx = [y[0] for y in LAMD_files_list].index(fn)\n", " f = LAMD_files_list[fn_idx][1]\n", " \n", " print('Using md5 hash filename')\n", " \n", " print('=' * 70)\n", " print('Rendering MIDI...')\n", " print('=' * 70)\n", "\n", " ms_score = MIDI.midi2ms_score(open(f, 'rb').read())\n", "\n", " itrack = 1\n", " song_f = []\n", "\n", " while itrack < len(ms_score):\n", " for event in ms_score[itrack]: \n", " if event[0] == 'note':\n", " song_f.append(event)\n", " itrack += 1\n", "\n", " song_f.sort(key=lambda x: x[1])\n", "\n", " fname = f.split('.mid')[0]\n", "\n", " x = []\n", " y =[]\n", " c = []\n", "\n", " colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver', 'aqua', 'azure', 'bisque', 'coral']\n", "\n", " for s in song_f:\n", " x.append(s[1] / 1000)\n", " y.append(s[4])\n", " c.append(colors[s[3]])\n", "\n", " if render_MIDI_to_audio:\n", " FluidSynth(\"/usr/share/sounds/sf2/FluidR3_GM.sf2\", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))\n", " display(Audio(str(fname + '.wav'), rate=16000))\n", "\n", " plt.figure(figsize=(14,5))\n", " ax=plt.axes(title=fname)\n", " ax.set_facecolor('black')\n", "\n", " plt.scatter(x,y, c=c)\n", " plt.xlabel(\"Time\")\n", " plt.ylabel(\"Pitch\")\n", " plt.show()\n", "\n", " #==============================================\n", "\n", " if download_MIDI:\n", " print('=' * 70)\n", " print('Downloading MIDI file', str(fn) + '.mid')\n", " files.download(f)\n", " print('=' * 70)\n", "\n", "except:\n", " print('File not found!!!')\n", " print('Check the filename!')\n", " print('=' * 70)" ], "metadata": { "cellView": "form", "id": "rfnp7dQw2egM" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# (COLAB MIDI FILES LOCATOR/DOWNLOADER)" ], "metadata": { "id": "pk1Wf3SfARmy" } }, { "cell_type": "code", "source": [ "#@title Loacate and/or download desired MIDI files by MIDI md5 hash file names\n", "\n", "MIDI_md5_hash_file_name_1 = \"d9a7e1c6a375b8e560155a5977fc10f8\" #@param {type:\"string\"}\n", "MIDI_md5_hash_file_name_2 = \"\" #@param {type:\"string\"}\n", "MIDI_md5_hash_file_name_3 = \"\" #@param {type:\"string\"}\n", "MIDI_md5_hash_file_name_4 = \"\" #@param {type:\"string\"}\n", "MIDI_md5_hash_file_name_5 = \"\" #@param {type:\"string\"}\n", "download_located_files = False #@param {type:\"boolean\"}\n", "\n", "print('=' * 70)\n", "print('MIDI files locator and downloader')\n", "print('=' * 70)\n", "\n", "md5_list = []\n", "\n", "if MIDI_md5_hash_file_name_1 != '':\n", " md5_list.append(MIDI_md5_hash_file_name_1)\n", "\n", "if MIDI_md5_hash_file_name_2 != '':\n", " md5_list.append(MIDI_md5_hash_file_name_2)\n", "\n", "if MIDI_md5_hash_file_name_3 != '':\n", " md5_list.append(MIDI_md5_hash_file_name_3)\n", "\n", "if MIDI_md5_hash_file_name_4 != '':\n", " md5_list.append(MIDI_md5_hash_file_name_4)\n", "\n", "if MIDI_md5_hash_file_name_5 != '':\n", " md5_list.append(MIDI_md5_hash_file_name_5)\n", "\n", "if len(md5_list) > 0:\n", " for m in md5_list:\n", " try:\n", "\n", " fn = m\n", " fn_idx = [y[0] for y in LAMD_files_list].index(fn)\n", " f = LAMD_files_list[fn_idx][1]\n", "\n", " print('Found md5 hash file name', m)\n", "\n", " location_str = ''\n", "\n", " fl = f.split('/')\n", " for fa in fl[:-1]:\n", " if fa != '' and fa != 'content':\n", " location_str += '/'\n", " location_str += str(fa)\n", "\n", " print('Colab location/folder', location_str)\n", "\n", " if download_located_files:\n", " print('Downloading MIDI file', str(m) + '.mid')\n", " files.download(f)\n", "\n", " print('=' * 70)\n", "\n", " except:\n", " print('md5 hash file name', m, 'not found!!!')\n", " print('Check the file name!')\n", " print('=' * 70)\n", " continue\n", "\n", "else:\n", " print('No md5 hash file names were specified!')\n", " print('Check input!')\n", " print('=' * 70)\n" ], "metadata": { "cellView": "form", "id": "MCehs2qxAaOl" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# (CUSTOM ANALYSIS TEMPLATE)" ], "metadata": { "id": "jNH-J2ry7sLj" } }, { "cell_type": "code", "source": [ "#@title Los Angeles MIDI Dataset Reader\n", "\n", "print('=' * 70)\n", "print('Los Angeles MIDI Dataset Reader')\n", "print('=' * 70)\n", "print('Starting up...')\n", "print('=' * 70)\n", "\n", "###########\n", "\n", "print('Loading MIDI files...')\n", "print('This may take a while on a large dataset in particular.')\n", "\n", "dataset_addr = \"/content/LAMD/MIDIs\"\n", "\n", "# os.chdir(dataset_addr)\n", "filez = list()\n", "for (dirpath, dirnames, filenames) in os.walk(dataset_addr):\n", " filez += [os.path.join(dirpath, file) for file in filenames]\n", "\n", "if filez == []:\n", " print('Could not find any MIDI files. Please check Dataset dir...')\n", " print('=' * 70)\n", "\n", "print('=' * 70)\n", "print('Randomizing file list...')\n", "random.shuffle(filez)\n", "print('=' * 70)\n", "\n", "###########\n", "\n", "START_FILE_NUMBER = 0\n", "LAST_SAVED_BATCH_COUNT = 0\n", "\n", "input_files_count = START_FILE_NUMBER\n", "files_count = LAST_SAVED_BATCH_COUNT\n", "\n", "stats = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n", "\n", "print('Reading MIDI files. Please wait...')\n", "print('=' * 70)\n", "\n", "for f in tqdm(filez[START_FILE_NUMBER:]):\n", " try:\n", " input_files_count += 1\n", "\n", " fn = os.path.basename(f)\n", " fn1 = fn.split('.mid')[0]\n", "\n", " #=======================================================\n", " # START PROCESSING\n", " #=======================================================\n", "\n", " # Convering MIDI to score with MIDI.py module\n", " score = MIDI.midi2score(open(f, 'rb').read())\n", "\n", " events_matrix = []\n", " \n", " itrack = 1\n", "\n", " while itrack < len(score):\n", " for event in score[itrack]: \n", " events_matrix.append(event)\n", " itrack += 1\n", "\n", " # Sorting...\n", " events_matrix.sort(key=lambda x: x[1])\n", "\n", " if len(events_matrix) > 0:\n", " \n", " #=======================================================\n", " # INSERT YOUR CUSTOM ANAYLSIS CODE RIGHT HERE\n", " #=======================================================\n", "\n", " # Processed files counter\n", " files_count += 1\n", "\n", " # Saving every 5000 processed files\n", " if files_count % 10000 == 0:\n", " print('=' * 70)\n", " print('Processed so far:', files_count, 'out of', input_files_count, '===', files_count / input_files_count, 'good files ratio')\n", " print('=' * 70)\n", "\n", " except KeyboardInterrupt:\n", " print('Saving current progress and quitting...')\n", " break \n", "\n", " except Exception as ex:\n", " print('WARNING !!!')\n", " print('=' * 70)\n", " print('Bad MIDI:', f)\n", " print('Error detected:', ex)\n", " print('=' * 70)\n", " continue\n", "\n", "print('=' * 70)\n", "print('Final files counts:', files_count, 'out of', input_files_count, '===', files_count / input_files_count, 'good files ratio')\n", "print('=' * 70)\n", "\n", "print('Resulting Stats:')\n", "print('=' * 70)\n", "print('Total good processed MIDI files:', files_count)\n", "print('=' * 70)\n", "print('Done!') \n", "print('=' * 70)" ], "metadata": { "id": "xrmnVUkXGsUi", "cellView": "form" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "YzCMd94Tu_gz" }, "source": [ "# Congrats! You did it! :)" ] } ], "metadata": { "colab": { "private_outputs": true, "provenance": [], "machine_shape": "hm" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" }, "gpuClass": "standard" }, "nbformat": 4, "nbformat_minor": 0 }