Monday, December 22, 2014

Tone music: how to encode songs





Abstract
In this article we explain how to encode tone music for your Arduino projects. In particular, we explain how to encode pauses, ties, repetitions, etc. This new encoding extends the one we have proposed in a past post. The main advantage is that it can considerably shorten melodies physical description and hence save memory space.
Keywords: tone music; pauses; augmentations; ties; repetitions.

Which music sheets to use?
It is not clear which sheets translate better into tone music. I prefer to use easy piano music sheets, or simply start from scratch by trials and errors.

Basic encoding
Encoding a music sheet essentially consists in defining the following three objects:
uint16_t instr_len:this is the length of the array melody. Remark that the size of a melody is limited by the size of int. This is not a true limitation since this is far beyond the memory capabilities of Arduino Uno.
uint16_t instr[]:array containing the instructions for playing the melody
uint16_t durs[]:array containing the instructions for the duration of the current note or of the current pause.



Encoding pauses
Pauses are encoded similarly to notes. Indeed, one has to add NOTE_PAUSE instruction in the melody array and adding the corresponding duration in the noteDurations array.

Encoding augmentations
An augmentation is graphically represented by a dot which follows a note and it is meant to augment the duration of the note by one half. Therefore, to encode a note with an augmentation it is enough to encode the value of the pitch as usual; in the durs array we will add the note duration plus the duration of its half. For example, considering the situation in Figure 1, one would add NOTE_RE4 to the instr array and DUR_MINIM+DUR_CROTCHET to the durs array.

Figure 1. Example of an augmentation.

Encoding ties
A tie is a symbol which connects two notes or three notes of the same pitch. Even if the two (or three) notes have different durations, their tie should be played as if it was a unique note with a duration which is the sum of the durations of the composing notes. For example, the tie in Figure 2 is encoded by adding NOTE_SOL3 in the melody array and DUR_MINIM+DUR_SEMIBREVE in the noteDurations array.

Figure 2. Example of tie.

Encoding repetitions
Repetitions are a bit complicated and consists in some symbols and some Italian phrases. If you are not familiar with them you can learn more in this excellent tutorial. We implemented all repetition types and symbols. In order to add a repetition one just needs to add its corresponding encoding to the instr array and nothing to the durs array. The coding for the coda symbol (NOTE_CODA) has to be inserted right before to the note that it refers to. Here is a table of the repetitions sign/phrases and their corresponding encoding.

Symbol/PhraseEncoding
RepeatNOTE_REPEAT
Inverse repeatNOTE_INVREPEAT
CodaNOTE_CODA
Da capo (DC)NOTE_DACAPO
Da capo a codaNOTE_DACAPOCODA
SegnoNOTE_SEGNO
Dal segno (DS)NOTE_DALSEGNO
Dal segno a codaNOTE_DALSEGNOCODA
FineNOTE_FINE
Da capo a fine (DC fine)NOTE_DACAPOFINE
Dal segno a fine (DS fine)NOTE_DALSEGNOFINE

Warning:
In order to get a code as short as possible no effort is made to check if the sequence of repetition symbols/phrases is correct (for example, if a NOTE_DACAPO is preceded by a NOTE_CAPO). In the case of incorrect encoding the behavior is unpredictable.

Alternate endings
The program allows to encode alternate ending by putting the instruction NOTE_ALTBEGIN at the beginning of the alternate ending and NOTE_ALTEND at the end. You may add as many alternate ending as needed. The instruction NOTE_ALTEND has to be omitted for the very last one. For example, considering the situation in Figure 3, one should have encoded
      ..., NOTE_ALTBEGIN, ..., NOTE_ALTEND, NOTE_ALTBEGIN, ...
Remark that in order to keep the code as simple (and compact) as possible, the technique described here allows to describe only "linear" ending sequences. Indeed, in complex music sheets you may find several endings by groups, for example one should play the first ending, the second; then the first one again, followed by the second one, etc. In such complicated case, you should "unroll" the music yourself by duplicating some parts.

Figure 3. Example of alternate ending.

Encoding chords
Another difficulty is to encode chords. Indeed, due to the limited capabilities of the speaker, the best and quickest way to encode chords is by encoding just the fundamental note of the chord and forget about the others (recall that the fundamental note of a chord is normally the one that is at the bottom of the chord). Figure 4 illustrates this idea.
Figure 4. Illustration of how to encode chords.

See also
Tone music surprises!
Tone music: advanced playing software
Tone music: changing the Tempo of your melodies

Enjoy!

No comments :

Post a Comment