Welcome to pitchtypes’s documentation!

A library for handling musical pitches and intervals in a systematic way. For other (and mostly compatible) implementations see:

Arithmetics

You can, for instance, compute the interval class between a B♭ and an F♯, which is an augmented fifth:

>>> import pitchtypes as pt
>>> pt.SpelledPitchClass("F#") - pt.SpelledPitchClass("Bb")
a5

You can also perform all sensible arithmetic operations on intervals and pitches.

Generic Interface

More generally, the library defines different types of musical intervals and pitches as well as a generic interface for writing algorithms that work with different pitch and interval types. This allows you to write generic functions

>>> def transposeby(pitches, interval):
...     return [pitch + interval for pitch in pitches]

and use them with different pitch types, including SpelledPitch (corresponding to written notes in Western notation)

>>> transposeby([pt.SpelledPitch("C4"), pt.SpelledPitch("Eb4"), pt.SpelledPitch("G#4")],
...             pt.SpelledInterval("m3:0"))
[Eb4, Gb4, B4]

SpelledPitchClass (which work the same but ignore octaves)

>>> transposeby([pt.SpelledPitchClass("C"), pt.SpelledPitchClass("Eb"), pt.SpelledPitchClass("G#")],
...             pt.SpelledIntervalClass("m3"))
[Eb, Gb, B]

EnharmonicPitch (corresponding to keys on the piano)

>>> transposeby([pt.EnharmonicPitch(60), pt.EnharmonicPitch(63), pt.EnharmonicPitch(68)],
...             pt.EnharmonicInterval(3))
[D#4, F#4, B4]

LogFreqPitch (i.e. frequencies with intervals corresponding to frequency ratios)

>>> transposeby([pt.LogFreqPitch("261.63Hz"), pt.LogFreqPitch("311.13Hz"), pt.LogFreqPitch("415.30Hz")],
...             pt.LogFreqInterval("1.19"))
[311.34Hz, 370.24Hz, 494.21Hz]

and more.

Type Conversion

The library also provides type conversions, if they are reasonably well defined. For instance, from SpelledPitch to EnharmonicPitch

>>> pt.SpelledPitch("F##4").convert_to(pt.EnharmonicPitch)
G4

or from EnharmonicPitch to LogFreqPitch (assuming twelve-tone equal temperament)

>>> pt.EnharmonicPitch("A4").convert_to(pt.LogFreqPitch)
440.Hz

Installation

To get started, install via pip:

pip install pitchtypes

For more details on how to use this library, have a look at the API overview.