Skip to content

Instantly share code, notes, and snippets.

@andersy005
Last active July 16, 2019 04:25
Show Gist options
  • Select an option

  • Save andersy005/6dd5e8d699ae1976fc1b684ae97ea3f5 to your computer and use it in GitHub Desktop.

Select an option

Save andersy005/6dd5e8d699ae1976fc1b684ae97ea3f5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
"from ipywidgets import interact, interactive, fixed, interact_manual\n",
"from IPython.display import display"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import random\n",
"\n",
"def assign_scales(number_of_scales, modality):\n",
" \"\"\"\n",
" number_of_scales: defines how many scales will be listed\n",
" \n",
" modality: must enter key from list [0: 'Major', 1: 'Natural Minor', \n",
" 2: 'Harmonic Minor', 3: 'Melodic Minor', 4: 'All Modalities']\n",
" \"\"\"\n",
" tonic = ['C#', 'F#', 'B', 'E', 'A', 'G', 'C', 'F', 'B♭', 'E♭', 'A♭', \\\n",
" 'D♭', 'G♭', 'C♭']\n",
" modes = [' Major', ' Natural Minor', ' Harmonic Minor', ' Melodic Minor']\n",
"\n",
" assert any(np.arange(0,len(modes)+1) == modality), \\\n",
" 'Input modality must be a number from 0 to ' + str(len(modes)) + \\\n",
" '. See documentation for key assignment'\n",
" \n",
" possible_scales = np.ravel([[t + modes[m] for t in tonic] for m in \\\n",
" np.arange(0,len(modes))]) if modality == 4 \\\n",
" else [t + modes[modality] for t in tonic]\n",
" \n",
" try:\n",
" scale_indices = random.sample(range(0,len(possible_scales)),number_of_scales)\n",
" [print(possible_scales[i]) for i in scale_indices]\n",
" except ValueError:\n",
" print('Requested number of scales exceeded number of potential scales.')\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"w = interactive(assign_scales,\n",
" number_of_scales=widgets.IntSlider(min=1, max=20, step=1, value=1), \n",
" modality=widgets.IntSlider(min=0, max=4, step=1, value=0))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1671e6280d49458eab2dd8e4d3102202",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(IntSlider(value=1, description='number_of_scales', max=20, min=1), IntSlider(value=0, de…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(w)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment