Last active
July 29, 2016 19:09
-
-
Save iacchus/35fcee9ca4fe705ba2b2f4dd0951e273 to your computer and use it in GitHub Desktop.
This file contains hidden or 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": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Displaying Sheet Music using Jupyter and music21 in a headless VPS\n", | |
"\n", | |
"This notebook shows how to display sheet music in a headless vps, where musescore, using **lilypond** \".ly\" instead of **musescore** \"musicxml\" which seems to be dependent on X display to generate sheet music to ipython. This is useful if you are running from a remote browser and also that lilypond seems to print better images than musicxml format by default." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"from music21 import *" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"us = environment.UserSettings()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['musicxmlPath',\n", | |
" 'vectorPath',\n", | |
" 'warnings',\n", | |
" 'musescoreDirectPNGPath',\n", | |
" 'localCorporaSettings',\n", | |
" 'lilypondVersion',\n", | |
" 'midiPath',\n", | |
" 'showFormat',\n", | |
" 'ipythonShowFormat',\n", | |
" 'manualCoreCorpusPath',\n", | |
" 'debug',\n", | |
" 'lilypondBackend',\n", | |
" 'graphicsPath',\n", | |
" 'autoDownload',\n", | |
" 'lilypondPath',\n", | |
" 'braillePath',\n", | |
" 'writeFormat',\n", | |
" 'localCorpusSettings',\n", | |
" 'pdfPath',\n", | |
" 'directoryScratch',\n", | |
" 'lilypondFormat',\n", | |
" 'localCorpusPath']" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"us.keys()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Assuring lilypond is installed and music21 is correctly set up" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"First we execute a shell `whereis lilypond`, which outputs the binary is at '/usr/bin/lilypond'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"b'lilypond: /usr/bin/lilypond /usr/share/lilypond /usr/share/man/man1/lilypond.1.gz /usr/share/info/lilypond\\n'\n" | |
] | |
} | |
], | |
"source": [ | |
"import subprocess\n", | |
"p = subprocess.Popen(['whereis','lilypond'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n", | |
"out, err = p.communicate()\n", | |
"print(out)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Then we see if the us (environment.UserSettings) we instantiated before has the right value, it is empty by default. Is this is the case, set it like you set any string in python." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'/usr/bin/lilypond'" | |
] | |
}, | |
"execution_count": 33, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"us['lilypondPath']" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Then we set the default display format for ipython/jupyter to `ipython.lily.png`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"us['ipythonShowFormat'] = 'ipython.lily.png'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"us['musicxmlPath'] = /usr/bin/musescore\n", | |
"us['vectorPath'] = None\n", | |
"us['warnings'] = 1\n", | |
"us['musescoreDirectPNGPath'] = /usr/bin/musescore\n", | |
"us['localCorporaSettings'] = {}\n", | |
"us['lilypondVersion'] = None\n", | |
"us['midiPath'] = /usr/bin/timidity\n", | |
"us['showFormat'] = lily.png\n", | |
"us['ipythonShowFormat'] = ipython.lily.png\n", | |
"us['manualCoreCorpusPath'] = None\n", | |
"us['debug'] = 1\n", | |
"us['lilypondBackend'] = ps\n", | |
"us['graphicsPath'] = /usr/bin/sxiv\n", | |
"us['autoDownload'] = ask\n", | |
"us['lilypondPath'] = /usr/bin/lilypond\n", | |
"us['braillePath'] = None\n", | |
"us['writeFormat'] = lily.png\n", | |
"us['localCorpusSettings'] = []\n", | |
"us['pdfPath'] = /usr/bin/zathura\n", | |
"us['directoryScratch'] = /home/iacchus/git-stuff/jupyter-venv/scratchdir\n", | |
"us['lilypondFormat'] = pdf\n" | |
] | |
} | |
], | |
"source": [ | |
"for k in us.keys():\n", | |
" try:\n", | |
" print(\"us['{0}'] = {1}\".format(k, us[k]))\n", | |
" except:\n", | |
" pass" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": { | |
"collapsed": false, | |
"scrolled": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"music21.base.py: Caller first is <music21.stream.Part 0x7fd8827e3860_flat> with offsetAppend 0.0\n", | |
"music21.base.py: looking now in derivedObject, <music21.stream.Part 0x7fd8827e3860_flat> with offsetAppend 0.0\n", | |
"music21.base.py: Caller first is <music21.stream.Part 0x7fd8827e3860_flat> with offsetAppend 0.0\n", | |
"music21.base.py: Yielding (<music21.stream.Part 0x7fd8827e3860_flat>, 0.0, 'flatten') from derivedObject contextSites\n", | |
"music21.base.py: --returning from derivedObject search\n", | |
"music21.base.py: --returning from derivedObject search\n", | |
"lily.translate2012.py: environment.py temporary file: /home/iacchus/git-stuff/jupyter-venv/scratchdir/tmp8tzabq03.ly\n" | |
] | |
}, | |
{ | |
"data": { | |
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAP4AAABJCAIAAACSOwzLAAAJUklEQVR4nO2dQWsTTRjHZ2sCSWNM\nUki3gmAoFXpRaHvpyVOCNy+yQRS/huldkfYjePAesyDeBBMEexIxBMwhijGKWtsESVpDTcSafQ/z\nvsO8u5vJ7uxsdtN9fqfdze4zM7vPzDzzn9mNpGkaAoDgMed1BgDAG8D1gYACrg8EFHB9IKCA6wMB\nBVwfCCjg+kBAAdcHAgq4PhBQRLq+qqoCrQGAqwhzfVVV8/m8JEmiDAKAq4hxfez3eJtsAICfkYQs\nX9M19rAkDvA/Alp9YzMPDT/gfwS0+nSTXyqVEEL5fB4afsDnOG31aVWnVCopiqIoCgK1B/A9wlwf\n+73xOAD4E2HiJu33AOB/xLg+DvFpoNUHfI4Y14dQB5g5QsItguv7n2q12uv1yG4qldrY2PAwP57g\nVNzE87i0EaJ1gr7pW1Kp1OHhId5OJpPJZBIhdHJyMhgMjCdHo9GzZ88mk8nhcEiu0p2wuLh4/vz5\nccklk8lMJsOuXdOvfhNa/Wq1urW1xTih0+kghHK5HL2LEJJlmRy0Rb1ep3dlWV5cXOSwM+sMBoPB\nYNDr9QaDwdHR0cnJyfr6ejweF2L8+PiYbB8eHpo6tC0ajYaTy8PhcCgUQghpmjYajXS/aprGLvve\n3l40Gl1YWOh2u91uFx9cXV199uwZK1XNMQihUqlEtjHkiF1TdJbwrqIozjPpc968eVMqlW7evHn1\n6tVMJiPLcjqd1j2pcrksKrlwOGzBIf1CJpNhlz2bzRYKBU3TCoUCuSqbzbJvgoBYX1EUVVUVRaHX\nL3BonWSQIEn/hmHY8ikbPPR6vWq1Wi6Xv3z58urVK4RQv98/Pj4eDofsC2u1WjabF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4JeRftPHz40GKp\nP378KCRF4+w9MBE/BMOapvF/cxNPSejma/FoktumcyqVyu7u7r1790x/xR9hDOYfBgJ6uCsN+W4C\nOTJuGer0MRYTmmRAB7+4Sdp7ev2C5rVYi6E7IviOFWCK0//NBYAZBWZzgYACrg8EFHB9IKCA6wMB\nBVwfCCjg+kBAAdcHAgq4PhBQwPWBgAKuDwSUfwBF9bhdxFTdUgAAAABJRU5ErkJggg==\n", | |
"text/plain": [ | |
"<music21.ipython21.objects.IPythonPNGObject at 0x7fd8a80f04a8>" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"littleMelody = converter.parse(\"tinynotation: 3/4 c4 d8 f g16 a g f#\")\n", | |
"littleMelody.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Useful stuff about avaliable values\n", | |
"\n", | |
"from music21 source https://github.com/cuthbertLab/music21/blob/master/music21/common/formats.py" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['musicxml',\n", | |
" 'lilypond',\n", | |
" 'text',\n", | |
" 'textline',\n", | |
" 'midi',\n", | |
" 'png',\n", | |
" 'pdf',\n", | |
" 'svg',\n", | |
" 'lily.pdf',\n", | |
" 'lily.png',\n", | |
" 'lily.svg',\n", | |
" 'braille',\n", | |
" 'vexflow',\n", | |
" 'vexflow.html',\n", | |
" 'vexflow.js',\n", | |
" 'ipython',\n", | |
" 'ipython.png',\n", | |
" 'musicxml.png']" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"common.formats.VALID_SHOW_FORMATS" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['musicxml',\n", | |
" 'lilypond',\n", | |
" 'text',\n", | |
" 'textline',\n", | |
" 'midi',\n", | |
" 'png',\n", | |
" 'pdf',\n", | |
" 'svg',\n", | |
" 'lily.pdf',\n", | |
" 'lily.png',\n", | |
" 'lily.svg',\n", | |
" 'braille',\n", | |
" 'vexflow',\n", | |
" 'vexflow.html',\n", | |
" 'vexflow.js',\n", | |
" 'ipython',\n", | |
" 'ipython.png',\n", | |
" 'musicxml.png']" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"common.formats.VALID_WRITE_FORMATS" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment