2

I'm trying to replace one pitch in a score with another pitch (the end goal being to generate harmony parts).

>>> 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

How should I be going about this?


Update: I posted my "test.mid" file here.

mathandy
  • 1,892
  • 25
  • 32

1 Answers1

5

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])
ThePhi
  • 2,373
  • 3
  • 28
  • 38
Almog Cohen
  • 1,283
  • 1
  • 13
  • 12
  • Old versions of music21 (< 2013) had `inPlace=True` by default for some functions, so you might have been following some old code demos that didn't include the flag. By the current v.4 release the only places where inPlace is True by default are places where it would make no sense to have inPlace=False. In the v.5 alpha those methods don't even have an inPlace tag. – Michael Scott Asato Cuthbert Aug 31 '17 at 01:40
  • @MichaelScottCuthbert does it mean you recommend something else here? :) – Almog Cohen Mar 12 '18 at 23:55
  • Nope -- looks great to me! I just wanted to mention why someone might have been following old code that worked and doesn't in new code. – Michael Scott Asato Cuthbert Mar 13 '18 at 13:44