Note value data type

MNX encodes note values in a microsyntax. Some examples:

Note valueEncoding
A whole note/1
A half note/2
A quarter note/4
An eighth note/8
A dotted eighth note/8d
A double-dotted eighth note/8dd
A breve (double whole note)*2
A dotted breve*2d

Syntax definition

Note values less than or equal to a whole note consist of the following, in order:

  1. The character U+002F SLASH.
  2. One or more ASCII digits encoding the rhythmic duration as a power-of-two fractional denominator.
  3. Zero or more occurrences of U+0064 LOWERCASE D characters — one for each augmentation dot.

Note values greater than a whole note consist of the following, in order:

  1. The character U+002A ASTERISK.
  2. One or more ASCII digits encoding the base note value as a power-of-two multiplying factor.
  3. Zero or more occurrences of U+0064 LOWERCASE D characters — one for each augmentation dot.

Algorithm for parsing

To parse a note value, use the following procedure:

  1. Let input be the string being parsed.
  2. Let position be a pointer into input, initially pointing at the start of the string.
  3. Let number of dots be 0.
  4. If the character indicated by position is a U+002A ASTERISK character (*), let fractional be false and advance position by 1.
  5. Else, if the character indicated by position is a U+002E SLASH character (/), let fractional be true and advance position by 1.
  6. Else, return an error.
  7. Collect a sequence of characters that are ASCII digits only and let unparsed number be the result.
  8. Let base value be the result of parsing unparsed number using the rules for parsing integers.
  9. Collect a sequence of characters that are U+0064 LOWERCASE D characters. Set number of dots to the length of this sequence.
  10. If position is not at the end of the string, return an error.
  11. If base value is not equal to a power of 2, return an error.
  12. If base value is equal to 1 and fractional is false, return an error.
  13. If fractional is true, set base value to (1 / base value).
  14. Return base value and number of dots.