Chromatic pitch data type
MNX encodes chromatic pitches in a microsyntax. Some examples:
Chromatic pitch | Encoding |
---|---|
Middle C | C4 |
The C-sharp above middle C | C#4 |
The D-flat above middle C | Db4 |
The D-double-flat very near middle C | Dbb4 |
The pitch one quarter-tone above middle C | C4+0.5 |
The pitch one quarter-tone above middle C (identical to the above, but expressed in whole tone units) | C4+0.25w |
The pitch one quarter-tone above middle C (identical to the above, but expressed as whole tone fraction) | C4+1/4w |
The pitch one quarter-tone above middle C (identical to the above, but expressed as octave fraction) | C4+1/24o |
Algorithm for parsing
To parse a chromatic pitch, use the following procedure:
- Let input be the string being parsed.
- Let position be a pointer into input, initially pointing at the start of the string.
- Let alteration be
0
. - If the character at position is not an uppercase ASCII letter in the range from U+0041 UPPERCASE A - U+0047 UPPERCASE G, return an error.
- Let step be the character at position, and advance position by 1.
- If the character at position is U+0023 HASH,
- While the character at position is U+0023 HASH,
- Increase alteration by
1
. - Advance position by
1
.
- Increase alteration by
- While the character at position is U+0023 HASH,
- Else, if the character at position is U+0062 b,
- While the character at position is U+0062 b,
- Decrease alteration by
1
. - Advance position by 1.
- Decrease alteration by
- While the character at position is U+0062 b,
- Collect a sequence of characters that are ASCII digits only and let unparsed number be the result.
- Let octave be the result of parsing unparsed number using the rules for parsing integers.
- Let alteration factor be
0
. - If the character at position is U+002B PLUS,
- Set alteration factor to
1
. - Advance position by
1
.
- Set alteration factor to
- If the character at position is U+002D HYPHEN-MINUS,
- Set alteration factor to
-1
. - Advance position by
1
.
- Set alteration factor to
- If alteration factor is not equal to zero,
- Collect a sequence of characters that are ASCII digits, U+002E FULL STOP, or U+002F SLASH and place the result in unparsed number.
- If the character at position is U+006F LOWERCASE o,
- Multiply alteration factor by
12
. - Advance position by
1
.
- Multiply alteration factor by
- Else, if the character at position is U+0077 LOWERCASE w,
- Multiply alteration factor by
2
. - Advance position by
1
.
- Multiply alteration factor by
- If unparsed number contains U+002F SLASH, parse it as a rational number. Otherwise parse it as a valid floating-point number. Multiply the result by alteration factor and add this to alteration.
- If position is not at the end of the string, return an error.
- Return step, octave and alteration.