Skip to content

Overview

This reference details the classes and methods provided by the Python implementation of Meantonal.

External links to the project repo and PyPI page can be found below.

If you haven’t already, get started by installing the meantonal package from PyPI:

pip install meantonal

The Python implementation mirrors the JS implementation class for class and method for method, adapted to Python idiom:

  • Method and property names are snake_case: SPN.toPitch becomes SPN.to_pitch, Interval.fromName becomes Interval.from_name, and so on. Class names are unchanged.
  • Derived values that are getters in TypeScript are properties in Python, accessed identically: p.midi, m.quality, m.name.
  • Anywhere the JS implementation throws an Error, the Python implementation raises a ValueError with the same message.
  • Range methods like Pitch.range.diatonic return generators; iterate them with for, or collect them with list().
  • All vector classes support == structural equality and have a useful repr().

Uniquely to the Python implementation, Pitch and Interval support natural arithmetic through operator overloading, in the style of datetime/timedelta. A Pitch is a point and an Interval is a displacement, so:

from meantonal import SPN, Interval
p = SPN.to_pitch("C4")
m = Interval.from_name("M3")
q = p + m # E4 -- same as p.transpose_real(m)
q - p # M3 -- same as Interval.between(p, q)
p - m # Ab3 -- transpose down
m + m # A5 -- same as m.add(m)
-m # descending M3 -- same as m.negative
m * 3 # A7 -- same as m.times(3)

Pitch + Pitch is deliberately undefined (adding two points is meaningless) and raises a TypeError.

The named methods (add, subtract, times, transpose_real, between, …) are all still present, keeping the vocabulary consistent across the JS, Python and C implementations.

If you want to get a better understanding of how Meantonal works under the hood, check out the Learn category.