[][src]Crate tonality

A library for handling tonal pitch classes, keys, intervals, accidentals and alterations. A tonal pitch class (Tpc) does not distinguish pitches in different octaves, but it does distinguish different enharmonic spellings of notes, intervals, and keys. This is done based on the "line of fifths" concept.

Distinguishing enharmonic spellings is desirable in several applications:

  • In musical notation, where using an incorrect enharmonic spelling harms legibility
  • When using other tunings than twelve tone equal temperament (12TET), in which case notes normally considered enharmonic should actually be played at different pitches.

Another important type is the Step, which represents the fact that G sharp and G flat are written on the same line of the staff. A Step combined with a Key or Accidental gives a Tpc.

Using the Step type also helps you handle octaves. If you want the F above A flat, for instance, you would compare their Steps, see that F has a lower step than A flat, and therefore should be raised an octave.

Arithmetic operations with Tpcs and Intervals return optional values, because they may result in alterations beyond the domain of the library. Triple sharps/flats or double diminished/augmented intervals are not supported.

Alteration versus accidental

Though they are similar, these two types serve different purposes. An Alteration is a relative change that applies to a Tpc. An accidental is an absolute change that can only apply to a Step - turning it into a Tpc.


It can be used for finding the tonal pitch classes in a chord:

let root = Tpc::Fs;
type Chord = Vec<Interval>;
let dom7: Chord = {
    use Interval::*;
    vec![Unison, Maj3, P5, Min7]
let tpcs: Vec<Tpc> = dom7
    .filter_map(|&interval| root + interval)
let expected = vec![Tpc::Fs, Tpc::As, Tpc::Cs, Tpc::E];
assert_eq!(expected, tpcs);


pub use accidental::Accidental;
pub use alteration::Alteration;
pub use interval::Interval;
pub use key::Key;
pub use step::Step;
pub use tpc::Tpc;





The difference from the normal value of the step in the key, in semitones


Intervals with enharmonic distinction


Key signatures


A position on a music staff


Tonal pitch classes