Parse
Meantonal provides some classes to parse to and from common formats:
SPNhandles Scientific Pitch NotationLilyPondhandles LilyPond strings.Helmholtzhandles Helmholtz Pitch Notation.ABChandles ABC notation.
Each provides static toPitch and fromPitch methods to convert between strings and Pitch vectors.
toPitch
Section titled “toPitch”static toPitch(spn: string): Pitch;Creates a Pitch vector from a Scientific Pitch Notation string.
let p = SPN.toPitch("C#4");fromPitch
Section titled “fromPitch”static fromPitch(p: Pitch): string;Returns the SPN name of a Pitch. Throws an error if the Pitch’s accidental is altered by more than 8 sharps/flats — an arbitrary limit chosen to avoid handling strings of unbounded size, and because exceeding it almost certainly indicates a logic error upstream.
let p = SPN.toPitch("C#4");SPN.fromPitch(p); // "C#4"
p = Pitch.fromChroma(63, 4); // an extreme accidental (+9), unlikely from real usageSPN.fromPitch(p); // Error: Cannot render SPN name: accidental (9) exceeds the maximum of ±8 sharps/flats.LilyPond
Section titled “LilyPond”toPitch
Section titled “toPitch”static toPitch(str: string): Pitch;Creates a Pitch vector from a LilyPond note name.
let p = LilyPond.toPitch("cis'");fromPitch
Section titled “fromPitch”static fromPitch(p: Pitch): string;Returns the (absolute) LilyPond name of a Pitch. Throws an error if the Pitch’s accidental goes beyond a double sharp/flat, or if its octave falls outside -3 to 11 (a healthy margin beyond the range of human hearing). LilyPond can’t represent either case, and both almost always indicate a logic error upstream.
let p = LilyPond.toPitch("cis'");LilyPond.fromPitch(p); // "cis'"
p = Pitch.fromChroma(21, 4); // accidental of +3, beyond LilyPond's double sharpLilyPond.fromPitch(p); // Error: Cannot render LilyPond name: accidental (3) exceeds the double sharp/flat limit (±2).relative
Section titled “relative”static relative(start: Pitch): RelativeParser;Returns a new instance of a relative mode LilyPond parser, with a .toPitch method that works exactly like the LilyPond.toPitch static method, but positioning each Pitch relative to the most recently parsed Pitch, before adjusting via octave marks.
let p = SPN.toPitch("C4");let parser = LilyPond.relative(p);
p = parser.toPitch("g"); // G3, since it's the nearest G to C4p = parser.toPitch("fis"); // F#3, since it's the nearest F# to G3p = parser.toPitch("d'"); // D4, since the ' mark adjusts the octave by +1Relative mode is often more convenient to reason around when entering pitches by hand.
Helmholtz
Section titled “Helmholtz”toPitch
Section titled “toPitch”static toPitch(str: string): Pitch;Creates a Pitch vector from a Helmholtz note name.
let p = Helmholtz.toPitch("c#'");fromPitch
Section titled “fromPitch”static fromPitch(p: Pitch): string;Returns the Helmholtz note name of a Pitch. Throws an error if the Pitch’s accidental is altered by more than 8 sharps/flats — an arbitrary limit chosen to avoid handling strings of unbounded size, and because it almost always indicates a logic error upstream.
let p = Helmholtz.toPitch("c#'");Helmholtz.fromPitch(p); // "c#'"
p = Pitch.fromChroma(70, 4); // an extreme accidental (+10), unlikely from real usageHelmholtz.fromPitch(p); // Error: Cannot render Helmholtz name: accidental (10) exceeds the maximum of ±8 sharps/flats.toPitch
Section titled “toPitch”static toPitch(str: string): Pitch;Creates a Pitch vector from an ABC note name.
let p = ABC.toPitch("^c'");fromPitch
Section titled “fromPitch”static fromPitch(p: Pitch): string;Returns the ABC note name of a Pitch. Throws an error if the Pitch’s accidental goes beyond a double sharp/flat, or if its octave falls outside -3 to 11 (a healthy margin beyond the range of human hearing). ABC notation can’t represent either case, and both almost always indicate a logic error upstream.
let p = ABC.toPitch("^c'");ABC.fromPitch(p); // "^c'"
p = Pitch.fromChroma(21, 4); // accidental of +3, beyond ABC's double sharpABC.fromPitch(p); // Error: Cannot render ABC name: accidental (3) exceeds the double sharp/flat limit (±2).