This document provides a high-level, but not necessarily comprehensive, overview of language constructs that your abc player will need to handle. For information on the exact syntac of abc, you should consult the grammar in the EBNF.
An abc file consists of two parts: (1) the header, which contains various information about the musical piece, such as its title, composer, tempo, meter, and note length, and (2) the body, which contains a sequence of notes that make up the musical piece.
Each line in the header corresponds to a field, and begins with a single uppercase letter and a colon (':'), followed by the content of the field. The following shows an example of what a header may look like:
X: 3 T: Turkish March C: W. Mozart M: 2/4 L: 1/8 K: AmAccording to this header, the title of the piece contained in this file is "Turkish March", and was composed by a person named "W. Mozart". The piece is in A minor ('K: Am'), and the default length of each note or beat in the music is one eighth ('L: 1/8'). Each bar contains two quarter beats ('M: 2/4').
The abc standard specifies a large number of fields. You are required to handle only the following subset:
There are several additional requirements on the header:
All fields other than 'X', 'T', and 'K' are optional, and may appear in any order. When omitted, the default values are 1/8 for the note length, 4/4 for the meter, and 100 beats per minute for the tempo. When the field 'C' is omitted, any reasonable string will suffice (e.g. "Unknown").
The body of an abc file consists of a sequence of notes separated by bar lines. Middle C is denoted by
Cand the scale of C in this octave is represented by the sequence
CDEFGABThe note C in the next higher octave is denoted by a lower case C
cTherefore, the sequence of notes that represents the scale of C twice, going through two octaves, and starting with middle C is
CDEFGABcdefgabNotes in the higher octave are denoted by appending an appropriate number of apostrophes (') immediately after their names. For example,
c'represents C in the two octaves up from the middle C, and
c''represents C in the three octaves up. Similarly, notes in the octaves below the middle C can be denoted by appending commas (,).
A bar line separates a sequence of notes into segments and is denoted by '|'. A double bar line (e.g. '||' or '|]') indicates the end of a major section in the music. There are also repeat lines (e.g. '|:'), which are described in Section 2.7.
As a summarizing example, the following sequence of notes
may be represented by
C, D, E, F,|G, A, B, C|D E F G|A B c d|e f g a|b c' d' e'|f' g' a' b'|]
2.2 Note Lengths
By default, every note has the length specified in the 'L' header field (and when this field is absent, a length of 1/8). The length of a particular note may be augmented (diminished) by appending a multiplicative factor to the note. For example, if the default note length is 1/8 (i.e. an eighth note), then the following sequence of notes
is represented by
A1/4 A/4 A/ A A2 A3 A4 A6 A8| A,1/4 A,/4 A,/ A, A,2 A,3 A,4 A,6 A,8 |]Note that either the denominator or the numerator (or both) in a fractional multiplicative factor can be omitted. An absent numerator should be treated as 1, and an absent denominator as 2. In addition, any apostrophes or commas that indicate the deviation from the middle octave must precede the multiplicative factor.
The effect of the key signature as discussed in Section 1 ('K' field) can be temporarily overridden using accidentals. An accidental can be a sharp (denoted by '^' in abc), a flat ('_'), or a natural ('='). A sharp causes a note to be played one semitone higher, a flat one semitone lower, and a natural causes the note to be played as if the key were in C without any accidental. Double sharps ('^^') and flats ('__') are also allowed.
The effect of an accidental on a note lasts only up to the end of the current bar, and can be overridden by another accidental that occurs in the same bar. The effect of the key signature is restored at the beginning of the next bar.
A rest is denoted by
zand has the same default length as a note. Multiplicative factors have the same effect on rests as they do on notes, but accidentals cannot be applied to rests.
One or more notes may be played simultaneously in a chord. In abc, a chord is denoted by a group of notes between square brackets ('[' and ']'):
[CEG]Notes within a chord can be embellished with an accidental or a multiplicative factor.
(3GABis a tuplet that consists of three notes, and called triplet.
In this project, we require you to handle ONLY duplets (a group of two notes), triplets, and quadruplet (a group of four notes). The duration of each type of the tuplets are as follows:
For example, a triplet that contains three eighth notes is equal in duration to one quarter note; therefore, each eighth note in the triplet should be played 2/3 the duration of a standard eighth note.
A section of music that is enclosed within '|:' and ':|' is to be repeated once. For example, in the following fragment of abc,
|: C D E F | G A B c :|the two bars are repeated, so the sequence of notes that your abc player should produce as output is:
C D E F G A B c C D E F G A B cThe begin repeat bar '|:' may be omitted; in this case, the repeat is from the beginning of a major section (i.e. the bar that immediately follows '|]'), or the beginning of the musical piece.
A repeated section may have a differet ending when it is played the second time. In abc, alternate endings are indicated using '[1' and '[2'. For example,
|: C D E F |[1 G A B c :|[2 F E D C |should be played as
C D E F G A B c C D E F F E D CNote that when the repeat is played the second time, the bar that begins with '[1' is entirely skipped over.
All of the abc constructs that we have discussed so far allow us to play only a single melodic line. Many pieces of music (e.g. orchestral or band music), however, involve multiple instruments or voices that are played simultaneously. Multiple voices do not belong to the official abc language definition, but have nevertheless become a part of the de facto standard among abc users.
Different definitions for multiple voices can be found online. Your abc player should be able to parse and play multiple voices as defined as follows. Your abc player is NOT required to be able to play different instruments.
Voices are listed in the header of an abc file using one or more field lines that begin with 'V'. The content of each voice field is the identifier for a particular voice, and can be an arbitrary string. For example, the header
X:0 T: Prelude No. 1 C: J. S. Bach M:4/4 L:1/16 Q:100 V: upper V: middle V: lower K:Csays that this piece of music contains three different voices, labeled "upper", "middle", and "lower". There is no limit on the number of voices in a piece.
A 'V' field line may re-appear in the middle of the body to indicate that the following sequence of notes belongs to a particular voice, until another voice field with a different identifier appears. For increased readability, middle-of-body voice fields are often placed between small sequences of bars for different voices in alternating fashion (as it is normally done in sheet music):
V: upper z2 Gc eGce z2 Gc eGce | z2 Ad fAdf z2 Ad fAdf | V: middle z E7 z E7 | z D7 z D7 | V: lower C8 C8 | C8 C8 | V: upper z2 Gd fGdf z2 Gd fGdf | z2 Ae aAea z2 Ae aAea |] V: middle z D7 z D7 | z E7 z E7 |] V: lower B,8 B,8 | C8 C8 |]However, your abc player should not make any assumptions about the order or frequency of middle-of-body voice fields. For example, instead of interleaving voices as in the previous example, an abc author could write each voice in its entirety under a single middle-of-body voice field:
V: upper z2 Gc eGce z2 Gc eGce | z2 Ad fAdf z2 Ad fAdf | z2 Gd fGdf z2 Gd fGdf | z2 Ae aAea z2 Ae aAea |] V: middle z E7 z E7 | z D7 z D7 | z D7 z D7 | z E7 z E7 |] V: lower C8 C8 | C8 C8 | B,8 B,8 | C8 C8 |]