LoginSignup
12
5

More than 3 years have passed since last update.

Google Colaboratory上でmusic21を利用

Last updated at Posted at 2019-11-23

はじめに

Jupyter上で楽譜が表示できると聞いて、music21というパッケージを試してみた。
Google Colaboratory上で表示させるのに手間取ったので備忘録として残しておく。

インストール方法

X virtual framebufferを以下のコードのように設定をすることで、Colaboratory上でも楽譜が表示されるようになる。

setup.py
# music21のインストール(Jupyterと共通)
!pip install --upgrade music21
!apt-get install musescore
# 仮想フレームバッファの設定(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
# パスの設定(Jupyterと共通)
from music21 import *
us = environment.UserSettings()
us['musescoreDirectPNGPath'] = '/usr/bin/mscore'
us['musicxmlPath'] = '/usr/bin/mscore'
us['directoryScratch'] = '/tmp'

実行結果

『カエルの歌』の楽譜などを表示してみた。

plot.py
#TinyNotation記法による『カエルの歌』
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')
#楽譜の表示
cp.show()
#ピアノロール式表示
cp.plot()
#ヒストグラム表示
cp.plot('histogram', 'pitch')

楽譜の表示

1.png

ピアノロール式の表示

2.png

ヒストグラムの表示

3.png

課題

MIDI再生がうまくいかない。
Web Audio APIと紐付けるか、ローカルマシンのデバイスが利用できれば聞こえるはず、時間のあるときにでも検証したい。

play.py
#リファレンス上は下記のコードで再生されはず...
cp.show('midi')

誰か分かる人がいたらぜひ教えて下さい。

12
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
5