[PYTHON] Use music21 on Google Colaboratory

Introduction

When I heard that the score could be displayed on Jupyter, I tried a package called music21. It took a long time to display it on Google Colaboratory, so I will keep it as a memorandum.

Installation method

By setting the X virtual framebuffer as shown in the code below, the score will be displayed on the Colaboratory as well.

setup.py


#Installation of music21 (common with Jupyter)
!pip install --upgrade music21
!apt-get install musescore
#Virtual framebuffer settings (required settings for Google Colaboratory)
!apt-get install xvfb
!sh -e /etc/init.d/x11-common start
import os
os.putenv('DISPLAY', ':99.0')
!start-stop-daemon --start --pidfile /var/run/xvfb.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -screen 0 1024x768x24 -ac +extension GLX +render -noreset
#Path setting (common with Jupyter)
from music21 import *
us = environment.UserSettings()
us['musescoreDirectPNGPath'] = '/usr/bin/mscore'
us['musicxmlPath'] = '/usr/bin/mscore'
us['directoryScratch'] = '/tmp'

Execution result

I tried to display the score of "Frog Song".

plot.py


#"Frog Song" in TinyNotation notation
cp = converter.parse('tinyNotation: 4/4 c4 d4 e4 f4 e4 d4 c4 r e4 f4 g4 a4 g4 f4 e4 r c4 r c4 r c4 r c4 r c8 c8 d8 d8 e8 e8 f8 f8 e4 d4 c4 r')
#Display of sheet music
cp.show()
#Piano roll type display
cp.plot()
#Histogram display
cp.plot('histogram', 'pitch')

Display of sheet music

1.png

Piano roll type display

2.png ## Histogram display 3.png

Task

MIDI playback does not work. It should be audible if it is linked with the Web Audio API or if the device on the local machine is available, and I want to verify it even when I have time.

play.py


#It should be played with the following code on the reference...
cp.show('midi')

If anyone knows, please let me know.

Recommended Posts

Use music21 on Google Colaboratory
Use The Metabolic Disassembler on Google Colaboratory
Try StyleGAN on Google Colaboratory
Pandas 100 knocks on Google Colaboratory
How to use Google Colaboratory
Google colaboratory
■ [Google Colaboratory] Use morphological analysis (janome)
■ [Google Colaboratory] Use morphological analysis (MeCab)
Use ndb.tasklet on Google App Engine
Run Keras on Google Colaboratory TPU
How to use Google Assistant on Windows 10
Use cartopy without bugs in Google Colaboratory
Use external modules on Google App Engine
Use TPU and Keras with Google Colaboratory
Google Colaboratory 90-minute session disconnection countermeasures --- Use Python! ---
Google Colaboratory setup summary
Is it Google Colaboratory?
Use pyvenv on Windows
Use Ansible on Windows
Use QuTiP on Windows
Use pip on Windows
Run Keras on Google Colaboratory TPU
How to use Django on Google App Engine / Python
How to use Spacy Japanese model in Google Colaboratory
I can't use the darknet command in Google Colaboratory!
Building an environment to use CaboCha with google colaboratory
ls -R on Google Drive
Study Python with Google Colaboratory
Use Github Desktop on Linux
Plotly Dash on Google Colab
Use matplot libwidget on mac
Use sshpass on Amazon linux2
Try OpenCV with Google Colaboratory
Use Python on Windows (PyCharm)
Tool organization: Google Colaboratory (updated 2020.2.24)
Use NeoPixel on Raspberry Pi
Use Linux on Windows 10 (WSL2)
PyPI package for super easy use of Cotoha on Google colab
How to use Google Colaboratory and usage example (PyTorch x DCGAN)
Use host.docker.internal on linux (docker-compose required)
Use Numpy, Scipy, scikit-learn on Heroku
Display multiple markers on Google Map
How to use Dataiku on Windows
Show grass on Google Nest Hub
Use Github-Flavored-Markdown on your Pelican blog
Notes on how to use pywinauto
Use jupyter on AWS GPU instance
Notes on how to use featuretools
Forcibly use Google Translate from python
Rip Music CDs on Arch Linux
How to use homebrew on Debian
[Memo] How to use Google MµG
Snippets (scraping) registered in Google Colaboratory
Use Tensorflow 2.1.0 with Anaconda on Windows 10!
OpenCV feature detection with Google Colaboratory
100 language processing knock 2020 "for Google Colaboratory"
Until you use Google cola boratory
Use Chrome Remote Desktop on Linux
Notes on how to use doctest
Sakura Use Python on the Internet
Play with Turtle on Google Colab