200ページ以上の分厚い本だが、多分にアルゴリズミックな内容なので、おそらくアルゴリズムだけ記述すれば10ページ以内に収まるような気もする。
ということで折角なのでpythonとmusic21を使って本書冒頭100ページほどを占めている"Equal Division of N octave into M parts"のアルゴリズムをmusic21の活用事例にもなるかと思って一部だけインプリしてみた。
from music21 import *
noteList = []
# from note name
n = note.Note("F#4",quarterLength = 1)
noteList.append(n)
# from pitch with note name
p = pitch.Pitch("F#4")
n = note.Note(pitch=p,quarterLength = 1)
noteList.append(n)
# from midi note F#4=66
curnote = 66
n = note.Note(midi=curnote,quarterLength = 1)
noteList.append(n)
# deep copy
n2 = note.Note(n.pitch,quarterLength=n.quarterLength)
noteList.append(n2)
meas = stream.Measure()
meas.append(noteList)
meas.show('lily.png')
from music21 import *
noteList = []
# Mid C
curnote = 60
n = note.Note(midi=curnote,quarterLength = 1)
noteList.append(n)
curnote = curnote + 2
for i in range(2):
n = note.Note(midi=curnote,quarterLength = 1./2.)
noteList.append(n)
curnote = curnote + 2
for i in range(3):
n = note.Note(midi=curnote,quarterLength = 1./3.)
noteList.append(n)
curnote = curnote + 2
for i in range(4):
n = note.Note(midi=curnote,quarterLength = 1./4.)
noteList.append(n)
curnote = curnote + 2
meas = stream.Measure()
meas.append(noteList)
#meas.show('lily.png') # not work?
meas.show('musicxml')
複雑な連符の場合にはappendTupletを使う。
from music21 import *
s = stream.Stream()
tuplets = [3, 5, 7, 9, 11, 13]
for t in tuplets:
d = duration.Duration('eighth')
d.appendTuplet(duration.Tuplet(t, 2))
n = note.Note(60)
n.duration = d
s.repeatAppend(n, t)
s.insert(0, meter.TimeSignature('2/4'))
s.makeNotation(inPlace=True)
s.makeBeams(inPlace=True)
s.show('lily.png')
休符はこんな感じ。音高の指定以外はnoteと同じ。
from music21 import *
meas = stream.Measure()
restList = []
curnote = 60
n = note.Rest(quarterLength = 1)
restList.append(n)
curnote = curnote + 2
for i in range(2):
n = note.Rest(quarterLength = 1./2.)
restList.append(n)
curnote = curnote + 2
for i in range(3):
n = note.Rest(quarterLength = 1./3.)
restList.append(n)
curnote = curnote + 2
for i in range(4):
n = note.Rest(quarterLength = 1./4.)
restList.append(n)
curnote = curnote + 2
meas.append(restList)
meas.show('musicxml')
タイの付け方。
from music21 import *
noteList = []
n = note.Note("F#4",quarterLength = 1)
noteList.append(n)
n = note.Note("F#4",quarterLength = 1)
noteList.append(n)
n = note.Note("F#4",quarterLength = 1)
noteList.append(n)
n = note.Note("F#4",quarterLength = 1)
noteList.append(n)
noteList[0].tie = tie.Tie('start')
noteList[1].tie = tie.Tie('continue')
noteList[2].tie = tie.Tie('continue')
noteList[3].tie = tie.Tie('stop')
meas = stream.Measure()
meas.append(noteList)
meas.show('lily.png')
スラーの付け方。(lilypondだと動かない)
from music21 import *
noteList = []
n = note.Note("C4",quarterLength = 1)
noteList.append(n)
n = note.Note("G#4",quarterLength = 1)
noteList.append(n)
n = note.Note("B4",quarterLength = 1)
noteList.append(n)
n = note.Note("E4",quarterLength = 1)
noteList.append(n)
sl1 = spanner.Slur([noteList[0], noteList[3]])
meas = stream.Measure()
meas.append(sl1)
meas.append(noteList)
#meas.insert(0.0, sl1) # also works
meas.show('musicxml')
和音を作るにはchordクラスを使う。
from music21 import *
meas = stream.Measure()
for i in range(8):
notes = []
for n in range(5):
p = pitch.Pitch(60 + (n * 5) + ((i * 7) % 12))
notes.append(p)
c = chord.Chord(notes,quarterLength = 1./2.)
meas.append(c)
meas.show('lily.png')
Grace Noteの作り方。(lilypondだと動かない)
from music21 import *
meas = stream.Measure()
n = note.Note("G4",quarterLength = 1)
meas.append(n)
d = note.Note('A4', type='eighth')
dGrace = d.getGrace()
meas.append(dGrace)
n = note.Note("G4",quarterLength = 1)
meas.append(n)
n = note.Note("F4",quarterLength = 1)
meas.append(n)
n = note.Note("E4",quarterLength = 1)
meas.append(n)
meas.show('musicxml')
Lyricの付け方。個人的には音符の素性をノートしたり本来の用途以外で多用している。
from music21 import *
meas = stream.Measure()
n = note.Note("G4",quarterLength = 1.5)
n.addLyric('ho-')
meas.append(n)
n = note.Note("C5",quarterLength = 0.5)
n.addLyric('ge')
meas.append(n)
n = note.Note("B4",quarterLength = 1.5)
n.addLyric('ho-')
meas.append(n)
n = note.Note("F4",quarterLength = 0.5)
n.addLyric('ge')
meas.append(n)
meas.show('lily.png')