{ "cells": [ { "cell_type": "markdown", "metadata": { "gradient": { "editing": false, "id": "ac5a4cf0-d9d2-47b5-9633-b53f8d99a4d2", "kernelId": "" }, "id": "SiTIpPjArIyr" }, "source": [ "# Los Angeles MIDI Dataset Maker (ver. 1.0)\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": { "cellView": "form", "gradient": { "editing": false, "id": "a1a45a91-d909-4fd4-b67a-5e16b971d179", "kernelId": "" }, "id": "fX12Yquyuihc" }, "outputs": [], "source": [ "#@title Install all dependencies (run only once per session)\n", "\n", "!git clone https://github.com/asigalov61/tegridy-tools\n", "!pip install tqdm" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "gradient": { "editing": false, "id": "b8207b76-9514-4c07-95db-95a4742e52c5", "kernelId": "" }, "id": "z7n9vnKmug1J" }, "outputs": [], "source": [ "#@title Import all needed modules\n", "\n", "print('Loading needed modules. Please wait...')\n", "import os\n", "\n", "import math\n", "import statistics\n", "import random\n", "from collections import Counter\n", "import shutil\n", "import difflib\n", "from tqdm import tqdm\n", "\n", "if not os.path.exists('/content/Dataset'):\n", " os.makedirs('/content/Dataset')\n", "\n", "if not os.path.exists('/content/Output'):\n", " os.makedirs('/content/Output')\n", "\n", "print('Loading TMIDIX module...')\n", "os.chdir('/content/tegridy-tools/tegridy-tools')\n", "\n", "import TMIDIX\n", "\n", "print('Done!')\n", "\n", "os.chdir('/content/')\n", "print('Enjoy! :)')" ] }, { "cell_type": "markdown", "metadata": { "gradient": { "editing": false, "id": "20b8698a-0b4e-4fdb-ae49-24d063782e77", "kernelId": "" }, "id": "ObPxlEutsQBj" }, "source": [ "# (DOWNLOAD SOURCE MIDI DATASET)" ] }, { "cell_type": "code", "source": [ "#@title Download original LAKH MIDI Dataset\n", "\n", "%cd /content/Dataset/\n", "\n", "!wget 'http://hog.ee.columbia.edu/craffel/lmd/lmd_full.tar.gz'\n", "!tar -xvf 'lmd_full.tar.gz'\n", "!rm 'lmd_full.tar.gz'\n", "\n", "%cd /content/" ], "metadata": { "cellView": "form", "id": "7aItlhq9cRxZ" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "JwrqQeie08t0" }, "source": [ "# (FILE LIST)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "DuVWtdDNcqKh" }, "outputs": [], "source": [ "#@title Save file list\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/Dataset\"\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", "print('=' * 70)\n", "\n", "if filez == []:\n", " print('Could not find any MIDI files. Please check Dataset dir...')\n", " print('=' * 70)\n", "\n", "print('Randomizing file list...')\n", "random.shuffle(filez)\n", "\n", "TMIDIX.Tegridy_Any_Pickle_File_Writer(filez, '/content/filez')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "cellView": "form", "id": "qI_adhjojrJ9" }, "outputs": [], "source": [ "#@title Load file list\n", "filez = TMIDIX.Tegridy_Any_Pickle_File_Reader('/content/filez')\n", "print('Done!')" ] }, { "cell_type": "markdown", "metadata": { "id": "FLxHvO-wlwfU" }, "source": [ "# (PROCESS)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "CeGo7CruaCJQ", "cellView": "form" }, "outputs": [], "source": [ "#@title Process MIDIs with TMIDIX MIDI processor\n", "\n", "print('=' * 70)\n", "print('TMIDIX MIDI Processor')\n", "print('=' * 70)\n", "print('Starting up...')\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", "melody_chords_f = []\n", "\n", "pitches_sums = []\n", "pitches_counts = []\n", "\n", "pitches_data = []\n", "\n", "stats = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n", "\n", "print('Processing 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", " # Convering MIDI to ms score with MIDI.py module\n", " score = TMIDIX.midi2ms_score(open(f, 'rb').read())\n", "\n", " events_matrix = []\n", " itrack = 1\n", " \n", " while itrack < len(score):\n", " for event in score[itrack]: \n", " if event[0] == 'note':\n", " if event[3] == 9:\n", " event[4] = (event[4] % 128)+128\n", " else:\n", " event[4] = (event[4] % 128)\n", " \n", " events_matrix.append(event)\n", " itrack += 1\n", " \n", " if len(events_matrix) >= 256:\n", "\n", " events_matrix.sort(key=lambda x: x[1])\n", "\n", " times = [e[1] for e in events_matrix]\n", " durs = [e[2] for e in events_matrix]\n", "\n", " if min(times) >= 0 and min(durs) >= 0:\n", " if len([k for k,v in Counter(times).items() if v>1]) != 0:\n", " \n", " pitches = [e[4] for e in events_matrix]\n", " pitches_sum = sum(pitches)\n", " pitches_number = len(pitches)\n", "\n", " if pitches_sum not in pitches_sums:\n", " pitches_count = sorted([[key, val] for key,val in Counter(pitches).most_common()], reverse=True)\n", " #=======================================================\n", " \n", " if [y[1] for y in pitches_count] not in pitches_counts:\n", "\n", " # Saving every 5000 processed files\n", " if files_count % 50000 == 0:\n", " dir_count = ((files_count // 50000)+1) * 50000\n", " dir_count_str = str(dir_count).zfill(7)\n", " copy_path = '/content/Output/'+dir_count_str\n", " if not os.path.exists(copy_path):\n", " os.mkdir(copy_path)\n", " print('SAVING !!!')\n", " print('=' * 70)\n", " print('Saving processed files...')\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", " if files_count % 5000 == 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", " TMIDIX.Tegridy_Any_Pickle_File_Writer(pitches_data, '/content/Output/pitches_data')\n", "\n", " shutil.copy2(f, copy_path+'/'+fn)\n", "\n", " # Processed files counter\n", " files_count += 1\n", " \n", " data = []\n", " \n", " data = [fn.split('.mid')[0], times[-1], len(set(times))]\n", " for p in pitches_count:\n", " data.extend(p)\n", " \n", " pitches_data.append(data)\n", " \n", " pitches_sums.append(pitches_sum)\n", " pitches_counts.append([y[1] for y in pitches_count])\n", "\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", "# Saving last processed files...\n", "print('=' * 70)\n", "print('Saving processed files...')\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", "TMIDIX.Tegridy_Any_Pickle_File_Writer(pitches_data, '/content/Output/pitches_data')\n", "\n", "# Displaying resulting processing stats...\n", "print('=' * 70)\n", "print('Done!') \n", "print('=' * 70)\n", "\n", "print('Resulting Stats:')\n", "print('=' * 70)\n", "print('Total good processed MIDI files:', files_count)\n", "print('=' * 70)" ] }, { "cell_type": "markdown", "metadata": { "id": "YzCMd94Tu_gz" }, "source": [ "# Congrats! You did it! :)" ] } ], "metadata": { "colab": { "machine_shape": "hm", "private_outputs": true, "provenance": [] }, "gpuClass": "standard", "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" } }, "nbformat": 4, "nbformat_minor": 0 }