tonality

Crates.iotonality
lib.rstonality
version0.1.1
sourcesrc
created_at2020-05-15 10:07:09.132504
updated_at2020-05-19 11:11:10.756841
descriptionTonal pitch classes with enharmonic distinction
homepagehttps://github.com/stigjb/tonality
repositoryhttps://github.com/stigjb/tonality
max_upload_size
id241815
size37,023
Stig Johan Berggren (Stigjb)

documentation

https://docs.rs/tonality

README

Tonality

crate documentation build

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.

Example

You can find the tonal pitch classes of the notes in a chord by specifying the chord as a collection of Intervals.

use tonality::{Tpc, Interval};

type Chord = Vec<Interval>;

let dom7: Chord = vec![Interval::Unison, Interval::Maj3, Interval::P5, Interval::Min7];
let root = Tpc::Fs;
let chord_tones: Vec<Tpc> = dom7
    .iter()
    // `Tpc` + `Interval` returns `Option<Tpc>`
    .filter_map(|&interval| root + interval)
    .collect();

let expected = vec![Tpc::Fs, Tpc::As, Tpc::Cs, Tpc::E];
assert_eq!(expected, chord_tones);

Inspiration

Types and operations, in particular the Tpc type, are influenced by Musescore's internal library.

Related crates

pitch_calc lets you convert between a number of different pitch representations, including the frequency domain.

rust-music-theory supports procedurally utilizing music theory notions like Note, Chord, Scale, Interval and more.

This crate might support conversion to and from types in the above mentioned crates in the future.

Commit count: 22

cargo fmt