Created
May 12, 2019 21:18
-
-
Save angelormrl/09bdad0b20b05b721198341c10d94d54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 74, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## ================ INCLUDES ================ ##\n", | |
"\n", | |
"# control and rendering of vst plugins\n", | |
"import librenderman as rm\n", | |
"\n", | |
"# mel-frequency cepstrum extraction\n", | |
"import librosa\n", | |
"\n", | |
"# math operations\n", | |
"import numpy as np\n", | |
"\n", | |
"# retries list of filenames in a directory\n", | |
"from os import listdir\n", | |
"\n", | |
"# clears output and will allow us to create a progress indicator\n", | |
"from IPython.display import clear_output" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 75, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## ================ FUNCTION DEFINITIONS ================ ##\n", | |
"\n", | |
"\n", | |
"# returns means mfcc values across render duration\n", | |
"def mean_mfccs(note, velocity, n_mfcc):\n", | |
"\n", | |
" engine.render_patch(note, velocity, 3.0, 4.0)\n", | |
"\n", | |
" audio_frames = np.asarray(engine.get_audio_frames())\n", | |
" \n", | |
" mfccs = librosa.feature.mfcc(y=audio_frames, sr=44100, hop_length=512, n_mfcc=n_mfcc)\n", | |
" \n", | |
" means = [np.mean(coefficient) for coefficient in mfccs]\n", | |
" \n", | |
" return means\n", | |
"\n", | |
"\n", | |
"def indicate_progress(current, total, label, clear=True):\n", | |
" \n", | |
" progress = str(current) + \"/\" + str(total) + \" \" + label\n", | |
" \n", | |
" if(clear):\n", | |
" \n", | |
" clear_output(wait = True)\n", | |
" \n", | |
" return progress\n", | |
"\n", | |
"\n", | |
"# creates input data from patch at all midi notes and for some velocities\n", | |
"def patch_examples(path, file_num, total_files):\n", | |
" \n", | |
" data = []\n", | |
" \n", | |
" velocities = [0, 63, 127]\n", | |
" \n", | |
" n_notes = 128\n", | |
" \n", | |
" n_mfcc = 20\n", | |
" \n", | |
" engine.load_preset(path)\n", | |
" \n", | |
" for note in range(n_notes):\n", | |
" \n", | |
" for i in range(len(velocities)):\n", | |
" \n", | |
" means = mean_mfccs(note, velocities[i], n_mfcc)\n", | |
" \n", | |
" data.append(means)\n", | |
" \n", | |
" current = i + note * len(velocities)\n", | |
" \n", | |
" total = n_notes * len(velocities)\n", | |
" \n", | |
" line_1 = indicate_progress(current, total, \"examples extracted from current patch!\")\n", | |
" \n", | |
" line_2 = indicate_progress(file_num, total_files, \"files searched!\", clear = False)\n", | |
" \n", | |
" print line_1 + \"\\n\" + line_2\n", | |
" \n", | |
" return data\n", | |
"\n", | |
"\n", | |
"# generates input and target data from each preset in a directory\n", | |
"def generate_dataset(load_path, patch_types):\n", | |
" \n", | |
" X = []\n", | |
" y = []\n", | |
" \n", | |
" file_names = os.listdir(load_path)\n", | |
" \n", | |
" for i in range(len(file_names)):\n", | |
" \n", | |
" for j in range(len(patch_types)):\n", | |
" \n", | |
" if(patch_types[j] in file_names[i]):\n", | |
" \n", | |
" target_index = j\n", | |
" \n", | |
" concatenated_path = load_path + file_names[i]\n", | |
" \n", | |
" data = patch_examples(concatenated_path, i, len(file_names))\n", | |
" \n", | |
" targets = np.full(len(data), target_index)\n", | |
" \n", | |
" X.extend(data)\n", | |
" \n", | |
" y.extend(targets)\n", | |
" \n", | |
" clear_output()\n", | |
" \n", | |
" print(\"*** dataset generated! ***\")\n", | |
" \n", | |
" return X, y" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 76, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"*** plugin loaded succesfully! ***\n" | |
] | |
} | |
], | |
"source": [ | |
"## ================ SETUP ================ ##\n", | |
"\n", | |
"\n", | |
"engine = rm.RenderEngine(44100, 512)\n", | |
"\n", | |
"plugin_path = '/Library/Audio/Plug-Ins/VST/KORG/MonoPoly.vst'\n", | |
"\n", | |
"if engine.load_plugin(plugin_path):\n", | |
" \n", | |
" print '*** plugin loaded succesfully! ***'\n", | |
" \n", | |
"load_path = \"/Users/angelorussell/Downloads/monopoly_presets\"\n", | |
"\n", | |
"# list of all file names in a directory\n", | |
"files = listdir(load_path)\n", | |
"\n", | |
"# titles of each class as substrings to be found in file name strings\n", | |
"patch_types = ['Bass', 'Pad', 'Lead']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"110/384 examples extracted from current patch!\n", | |
"43/86 files searched!\n" | |
] | |
}, | |
{ | |
"ename": "KeyboardInterrupt", | |
"evalue": "", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-77-d432cb546adc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mX\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgenerate_dataset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mload_path\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mpatch_types\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;32m<ipython-input-75-c8588dba7b32>\u001b[0m in \u001b[0;36mgenerate_dataset\u001b[0;34m(load_path, patch_types)\u001b[0m\n\u001b[1;32m 79\u001b[0m \u001b[0mconcatenated_path\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mload_path\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mfile_names\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 80\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 81\u001b[0;31m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpatch_examples\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mconcatenated_path\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfile_names\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 82\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 83\u001b[0m \u001b[0mtargets\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfull\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtarget_index\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m<ipython-input-75-c8588dba7b32>\u001b[0m in \u001b[0;36mpatch_examples\u001b[0;34m(path, file_num, total_files)\u001b[0m\n\u001b[1;32m 44\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvelocities\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 46\u001b[0;31m \u001b[0mmeans\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmean_mfccs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnote\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvelocities\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_mfcc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 47\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmeans\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;32m<ipython-input-75-c8588dba7b32>\u001b[0m in \u001b[0;36mmean_mfccs\u001b[0;34m(note, velocity, n_mfcc)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmean_mfccs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnote\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvelocity\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_mfcc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0mengine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrender_patch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnote\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvelocity\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0maudio_frames\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0masarray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mengine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_audio_frames\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mKeyboardInterrupt\u001b[0m: " | |
] | |
} | |
], | |
"source": [ | |
"## ================ GENERATE DATA ================ ##\n", | |
"\n", | |
"\n", | |
"X, y = generate_dataset(load_path, patch_types)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"12288 samples in dataset.\n", | |
"\n", | |
"6912 Bass patch examples.\n", | |
"\n", | |
"1536 Pad patch examples.\n", | |
"\n", | |
"3840 Lead patch examples.\n" | |
] | |
} | |
], | |
"source": [ | |
"## ================ DATA DESCRIPTION ================ ##\n", | |
"\n", | |
"\n", | |
"print str(len(X)) + \" samples in dataset.\"\n", | |
"\n", | |
"counts = np.bincount(y)\n", | |
"\n", | |
"for i in range(len(patch_types)):\n", | |
" \n", | |
" print \"\\n\" + str(counts[i]) + \" \" + patch_types[i] + \" patch examples.\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 80, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[-313.54278582 106.84572427 -25.429015 30.08328347 -5.32818981\n", | |
" 0.75808556 6.27498085 5.00479316 6.84010424 5.36592338\n", | |
" 4.11957381 4.11169945 5.2659075 4.35764479 4.77529445\n", | |
" 3.490972 3.73166278 3.76179877 4.60956964 3.5109039 ]\n", | |
"[-313.37309742 106.8237494 -25.3588622 30.12473205 -5.3326032\n", | |
" 0.74215542 6.28365495 4.96143867 6.86089893 5.36243742\n", | |
" 4.06786453 4.11844223 5.27006533 4.30207667 4.79911608\n", | |
" 3.49508771 3.74554194 3.77495628 4.62650527 3.51819289]\n", | |
"[-313.37724166 106.76919212 -25.37837269 30.10655499 -5.3370088\n", | |
" 0.72270189 6.28541453 4.9766507 6.86226075 5.38604629\n", | |
" 4.08843162 4.10583529 5.27744382 4.31883936 4.80139762\n", | |
" 3.49578531 3.72388733 3.76674386 4.62119455 3.49729737]\n" | |
] | |
} | |
], | |
"source": [ | |
"## ================ DATA ANALYSIS ================ ##\n", | |
"\n", | |
"n_mfcc = 20\n", | |
"\n", | |
"totals = np.zeros([len(patch_types), n_mfcc])\n", | |
"\n", | |
"for i in range(len(X)):\n", | |
" \n", | |
" for j in range(len(X[0])):\n", | |
" \n", | |
" totals[y[i]][j] += X[i][j]\n", | |
" \n", | |
"means = [totals[i] / counts[i] for i in range(len(patch_types))]\n", | |
"\n", | |
"for class_mean in means:\n", | |
" \n", | |
" print class_mean" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## ================ NETWOR TRAINING ================ ##\n", | |
"\n", | |
"\n", | |
"from sklearn import datasets\n", | |
"from sklearn.neural_network import MLPClassifier\n", | |
"\n", | |
"\n", | |
"def normalize_data(x):\n", | |
" data = zeros(shape(x))\n", | |
" for i in range(len(x[0])):\n", | |
" feature = [example[i] for example in x]\n", | |
" max_val = np.max(feature)\n", | |
" min_val = np.min(feature)\n", | |
" val_range = max_val - min_val\n", | |
" for j in range(len(x)):\n", | |
" data[j][i] = (x[j][i] - min_val) / val_range\n", | |
" return data\n", | |
"\n", | |
"\n", | |
"def accuracy_score(pred, true):\n", | |
" correct = np.sum(pred==true)\n", | |
" accuracy = correct/len(pred)\n", | |
" return accuracy\n", | |
"\n", | |
"\n", | |
"#X = normalize_data(X)\n", | |
"\n", | |
"\n", | |
"X = np.asarray(X)\n", | |
"\n", | |
"y = np.asarray(y)\n", | |
"\n", | |
"indices = np.random.permutation(len(X))\n", | |
"\n", | |
"train, test = indices[200:], indices[:200]\n", | |
"\n", | |
"net = MLPClassifier(activation='logistic', solver = 'sgd', hidden_layer_sizes=([100, 4]), learning_rate='invscaling', learning_rate_init=0.01)\n", | |
"\n", | |
"net.fit(X[train], y[train])\n", | |
"\n", | |
"preds = net.predict(X[test])\n", | |
"\n", | |
"accuracy = accuracy_score(preds, y[test])\n", | |
"print(accuracy)\n", | |
"print(preds)\n", | |
"print(y[test])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.14" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment