Search code examples
python-3.xmusic21

Why is music21 using pitch attributes in an unexpected way?


Consider the following testing code.

from music21 import pitch

C0 = 16.35

for f in [261, 130, 653, 64, 865]:
    p = pitch.Pitch()
    p.frequency = f

    # Compare manual frequency with music21 frequency
    f1 = p.frequency
    f2 = C0 * pow(2, p.octave) * pow(2, p.pitchClass / 12) * pow(2, p.microtone.cents / 1200)
    print(f, f1, f2)

    # Compare manual pitchspace with music21 pitchspace
    ps1 = p.ps
    ps2 = 12 * (p.octave + 1) + p.pitchClass + p.microtone.cents / 100
    print(ps1, ps2)
    print()

The output of this is

261 260.99999402174154 521.9489797003519
59.958555 71.95855499999999

130 129.99999854289362 259.974590631057
47.892097 59.892097

653 653.0000144741496 652.9362051837928
75.834954 75.834954

64 63.999998381902046 65.86890433005668
35.623683 36.123683

865 864.9999846113213 890.2594167561009
80.702359 81.202359

There is often a difference between my manual computation of the frequency resp. the pitch space and the music21 value. Note that sometimes this difference can be about an octave (like the first two C note frequencies), but mostly it is about one tone. Another weird thing is that for the third testing frequency the pitchspace values are the same while the frequencies are not.

What could be wrong about my manual formulas?


Solution

  • So it appears that while the deviation of an octave was a bug, the other deviations are intended behaviour. See https://github.com/cuthbertLab/music21/issues/96 for detailed explanation.