I'm trying to replace one pitch in a score with another pitch (the end goal being to generate harmony parts). I tried this:
>>> from music21 import *
>>> score = converter.parse('test.mid')
>>> type(score)
<class 'music21.stream.Score'>
>>> p0 = score.parts[0].pitches[0]
>>> p0sharp = p0.transpose(1)
>>> print p0
A3
>>> print p0sharp
B-3
>>> score.replace(p0, p0sharp)
>>> print score.parts[0].pitches[0]
A3
Why isn't the pitch changed after score.replace
? How can I modify the score?
I have just checked the code for transpose
. Just pass inPlace=True
and it will work like magic. Hope it helps!
from music21 import *
score = converter.parse('test.mid')
p0 = score.parts[0].pitches[0]
print(p0)
p0.transpose(1, inPlace=True)
print(score.parts[0].pitches[0])
And for those who want a complete working example without loading an existing midi file:
from music21 import stream, instrument, meter
from music21.note import Note
from music21.stream import Score
# Creating the example score
n = Note("A2", type='quarter')
part = stream.Part()
measure = stream.Measure()
measure.append(n)
part.append(measure)
score = Score()
score.append(part)
p0 = score.parts[0].pitches[0]
print(p0)
p0.transpose(1, inPlace=True)
print(score.parts[0].pitches[0])