Skip to content

Instantly share code, notes, and snippets.

@anandology
Created July 11, 2023 14:01
Show Gist options
  • Save anandology/9351a39163e7d84d7758572d420cf70a to your computer and use it in GitHub Desktop.
Save anandology/9351a39163e7d84d7758572d420cf70a to your computer and use it in GitHub Desktop.
Joy of Music
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "18053a74-6700-4000-984c-98daefef659a",
"metadata": {},
"source": [
"# Joy of Music"
]
},
{
"cell_type": "markdown",
"id": "d51813db-d58d-49ee-b60f-dd77c133378e",
"metadata": {
"user_expressions": []
},
"source": [
"An experiment to explore python programming through carnatic sangeetam."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3659c397-2009-42ba-b521-4f6020146a04",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting music.py\n"
]
}
],
"source": [
"%%file music.py\n",
"\"\"\"Joy of music!\n",
"\n",
"Joy of music is small python library to play musical notes in a jupyter notebook.\n",
"\"\"\"\n",
"from IPython.display import DisplayHandle, Javascript\n",
"from time import sleep\n",
"\n",
"_display = DisplayHandle(\"music\")\n",
"\n",
"lib = Javascript(\"https://unpkg.com/tone@14.7.77/build/Tone.js\")\n",
"_display.display(lib)\n",
"print(\"loaded tone.js library\")\n",
"\n",
"def play(note):\n",
" js = Javascript(f\"\"\"\n",
" const synth = new Tone.Synth().toDestination();\n",
" synth.triggerAttackRelease(\"{note}\", \"8n\");\n",
" \"\"\")\n",
" _display.update(js)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a13ffa4d-92a1-4f48-9f3c-458068f79fe2",
"metadata": {},
"outputs": [],
"source": [
"## Uncomment this if you want to reload the module\n",
"\n",
"# import sys\n",
"# sys.modules.pop(\"music\", None)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "035aadcf-9d90-455a-95e7-761acdd71fe0",
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"\n",
" const synth = new Tone.Synth().toDestination();\n",
" synth.triggerAttackRelease(\"C4\", \"8n\");\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"loaded tone.js library\n"
]
}
],
"source": [
"from music import play, sleep"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "06c94dc6-7a6d-4ec2-8798-889838e5fb50",
"metadata": {},
"outputs": [],
"source": [
"notes = {\n",
" \"s\": \"C4\",\n",
" \"r\": \"D4\",\n",
" \"g\": \"E4\",\n",
" \"m\": \"F4\",\n",
" \"p\": \"G4\",\n",
" \"d\": \"A4\",\n",
" \"n\": \"B4\",\n",
" \"S\": \"C5\"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b1c26057-51ee-4069-a01f-6b26eb229518",
"metadata": {},
"outputs": [],
"source": [
"def play_swara(swara):\n",
" \"\"\"Play a swara of carnatic music.\n",
" \"\"\"\n",
" note = notes[swara]\n",
" play(note)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "37eb8768-d44a-4d12-adce-1ef1be28a5fc",
"metadata": {},
"outputs": [],
"source": [
"play_swara(\"s\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "97611045-3c20-4a3d-978c-31898e5d9df0",
"metadata": {},
"outputs": [],
"source": [
"def play_swaras(swaras):\n",
" \"\"\"Play a list of swaras\"\"\"\n",
" for swara in swaras:\n",
" play_swara(swara)\n",
" sleep(0.5)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "66196f34-163a-49dd-847d-c98e4298019e",
"metadata": {},
"outputs": [],
"source": [
"play_swaras(\"srgrs\")"
]
},
{
"cell_type": "markdown",
"id": "5963d023-9880-45c0-adc6-eab29b7f0d56",
"metadata": {
"user_expressions": []
},
"source": [
"Let's define some ragas."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "5d0fd30b-1d2c-46ed-bce4-0b42d2334e5b",
"metadata": {},
"outputs": [],
"source": [
"ragas = {\n",
" \"kalyani\": \"srgmpdnS\",\n",
" \"mohanam\": \"srgpdS\"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "10a4ac62-3e17-4ab8-9154-e3f7f7f19b14",
"metadata": {},
"outputs": [],
"source": [
"def aarohanam(raga):\n",
" swaras = ragas[raga]\n",
" play_swaras(swaras)\n",
"\n",
"def avarohanam(raga):\n",
" swaras = ragas[raga]\n",
" play_swaras(reversed(swaras))\n",
" \n",
"def janta_varusalu(raga):\n",
" swaras = ragas[raga]\n",
" janta_swaras = []\n",
" for s in swaras:\n",
" janta_swaras.append(s)\n",
" janta_swaras.append(s)\n",
" play_swaras(janta_swaras)\n",
" play_swaras(janta_swaras[::-1])\n",
"\n",
"def play_raga(raga):\n",
" aarohanam(raga)\n",
" avarohanam(raga)\n",
" janta_varusalu(raga)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "470aff43-b5fe-4336-9dc6-01bef80002e5",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"play_raga(\"kalyani\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "d4df543b-703a-4bc9-a210-349500b0ab18",
"metadata": {},
"outputs": [],
"source": [
"def flatten(nested_values):\n",
" result = []\n",
" for values in nested_values:\n",
" for v in values:\n",
" result.append(v)\n",
" return result\n",
" \n",
"def triplets(swaras):\n",
" return flatten(zip(swaras, swaras[1:], swaras[2:]))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a7edc9de-91d4-4f31-ac29-f2ecce4fce28",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['s', 'r', 'g', 'r', 'g', 'm']"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"triplets(\"srgm\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e88e1a91-30b4-4a56-9dca-1f252bd3f7d4",
"metadata": {},
"outputs": [],
"source": [
"swaras = triplets(ragas[\"kalyani\"]) \n",
"play_swaras(swaras + swaras[::-1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96903794-05ff-49c6-8ed5-61df7077a05a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment