🎼

music21で遊ぼう

2023/05/23に公開

はじめに

本記事では、music21というライブラリを用いてPythonを使用した楽譜制作の基礎を簡単に解説していきたいと思います。

筆者は以下の環境で実行しています。

  • Windows10 home
  • Python3.9.12
  • vscode 1.78.2
  • MuseScore version (64-bit): 3.6.2.548021803

なお、この記事は大学の授業の一環で作成された簡易的な説明のため、詳細な情報は公式ドキュメントなどを参考にして頂きたいと思います。

https://web.mit.edu/music21/doc/moduleReference/index.html

music21とは

music21とは、MITによって開発された音楽情報処理ライブラリです。本ライブラリでは、楽譜の解析や編集など多岐にわたった処理を行うことができます。

今回は、楽譜作成ソフトであるMuseScore3を用いてmusic21のライブラリ機能を使った楽譜作成を行います。

MuseScore3の導入

まず初めに、MuseScore3の導入を行う。MuseScoreのインストールに関しては公式のハンドブックがありますのでそちらを参考にしてください。

windowsの方
https://musescore.org/ja/ハンドブック/windows-tenoinsutoru

macの方
https://musescore.org/ja/ハンドブック/macos-tenoinsutoru

music21の導入

続いてmusic21のインストールに入ります。筆者はここで一苦労しました。

手順

  1. まずPythonが実行できる作業フォルダを用意してください。
    今回はフォルダ名を"m21"と置きます。

  2. pipを用いてmusic21をインストールします。以下のコードを各コマンドプロンプト上で実行してください。

*注意:music21を使用するファイルがあるパス上で実行してください。
このコードは最初の1回のみ実行すれば、2回目以降は実行する必要はありません。

pip install music21
  1. 続いてPythonファイルを作成し、以下のコードを実行してください。Pythonファイル名は"m21_pra.py"とします。
m21_pra.py
import music21
music21.configure.run()

もしくは以下のコマンドをプロンプトで実行してください。

python -m music21.configure
  1. 上記プログラムを実行すると、下のような文が表示されると思います。
Defining an XML Reader permits automatically opening music21-generated
MusicXML in an editor for display and manipulation when calling the show()
method. Setting this option is highly recommended.

[1] C:\Program Files\MuseScore 3\bin\MuseScore3.exe
[2] C:\......

select a number from the preceding options (default is 1):

これは楽譜を表示するために必要なソフト(ここではMuseScore3)がインストールされたパスが表示されます。入力が求められるので、該当したパスが書かれている番号を入力し、Enterを押しましょう。

  1. BSDライセンス規約の同意確認プロセスです。
The BSD-licensed music21 software is distributed with a corpus of encoded compositions 
which are distributed with the permission of the encoders (and, where needed, 
the composers or arrangers) and where permitted under...

In addition to the corpus distributed with the music21, other pieces are not included in ....

Would you like to:
[1] Acknowledge these terms and allow music21 to aid in finding pieces in the corpus
[2] Acknowledge these terms and block the virtual corpus
[3] Do not agree to these terms and will not use music21 (agreeing to the terms of the 
corpus is mandatory for using the system).

selecta a number from the preceding options (default is 1): 

簡潔に訳すと、
[1] 規約に同意し、楽曲検索支援を認めます。
[2] 規約に同意したうえで、コーパスをブロックします。
[3] music21を使用しません。

1を入力し、Enterを押してください。

  1. メール送信選択とディスカッショングループへの参加有無の選択、そしてmusic21のドキュメント閲覧の有無選択です。
Would you like to send a pre-formatted email to music21 regarding your installation? 
Installation reports help us make music21 work better for you:
Enter Yes or No (default is Yes): 
The music21 discussion group provides a forum for asking questions and getting help. 
Would you like to see the music21 discussion list or sign up for updates? 
Enter Yes or No (default is Yes): 
Would you like to view the music21 documentation in a web browser? 
Enter Yes or No (default is Yes): 

これらの選択は、すべてNoを入力して問題ありません。

そして最後に、以下のメッセージが表示されます。

The music21 Configuration Assistant is complete. Press return to continue.

Enterを押して準備完了です!


楽譜の作成

・音符と休符

いよいよ楽譜の制作に入ります!

まず以下のプログラムを実行してください。(プログラム実行前に予めmuseScore3を起動させておくとよいでしょう)

m21_pra.py
from music21 import *

# 楽譜となるオブジェクト
score = stream.Score()

# 音符を格納する配列
notes = []

# C4の四分音符を生成
n1 = note.Note("C4")
notes.append(n1)

# 四分休符を生成
n2 = note.Rest()
notes.append(n2)

# 楽譜に音符情報を追加
score.append(notes)

# 楽譜の表示
score.show()

実行すると次のような楽譜が表示されると思います。

音符を表示させるにはnote.Note("音名")を、休符を表示させるにはnote.Rest()を入力します。また、Note()とRest()には音価情報を追加することができます。上記のコードに音価情報を追加してみましょう。

m21_pra.py
from music21 import *

# 楽譜となるオブジェクト
score = stream.Score()

# 音符を格納する配列
notes = []

# C4の二分音符を生成
- n1 = note.Note("C4")
+ n1 = note.Note("C4", quarterLength = 2.0)
notes.append(n1)

# 二分休符を生成
- n2 = note.Rest()
+ n2 = note.Rest(quarterLength = 2.0)
notes.append(n2)

# 楽譜に音符情報を追加
score.append(notes)

# 楽譜の表示
score.show()

実行すると次のような楽譜が表示されます。

・和音の作成

続いて和音を楽譜に反映させます。和音を挿入するには、note.Note()ではなくchord.Chord()を用います。

m21_pra.py
from music21 import *

# 楽譜となるオブジェクト
score = stream.Score()

# 音符を格納する配列
notes = []

# C4の四分音符を生成
n1 = note.Note("C4", quarterLength = 2.0)
notes.append(n1)

# 四分休符を生成
n2 = note.Rest(quarterLength = 2.0)
notes.append(n2)

+ # 和音を生成
+ n3 = chord.Chord(["C4", "E4", "G4"], quarterLength = 4.0)
+ notes.append(n3)

# 楽譜に音符情報を追加
score.append(notes)

# 楽譜の表示
score.show()

これを実行した結果がこちらです。

このように、和音も同様に表示させることができます。

おわりに

いかがでしたでしょうか?今回はmusic21を使った楽譜の作り方について解説しました。今回紹介できなかった機能を使えばより多岐にわたった楽譜を作ることができるので、是非ともたくさん遊んでみましょう!

参考ページ

https://miseruit.com/2022/02/14/post-1986/

Discussion