D♭ Tutorial: TEMPO AND METER

Tempo, meter and time signatures are all about time. Therefore, we should start the intermediate block of tutorials by reviewing and extending information about time handling in D♭. Each entity has the span attribute which determines its duration and the time attribute which sets its position (in seconds) in the generated music.

Time and Span

The following example shows how span can be used to manipulate duration of notes that are already anchored to a certain position in time. While shortening a note is usually safe, making a note longer may disrupt sheet music export as the longer note no longer fits the given time signature.

0
C-1
Code Editor
0.000

Shifting time has various applications. The first example shows how the a melody can be reused in a second layer. It gets cloned and then shifted in time by 3 beats. To improve the harmony, it is also inverted by degree *= -1.

0
C-1
Code Editor
0.000

Pay attention to the correct typing in the previous example, actually at two places.

  • At the duration assignment. Rhythm is an integer array, so its sum will be an integer as well. Dividing it by an integer will be inaccurate, rounding the result down to the next lower integer. For example, rhtythm.Sum() / 3 would reduce the duration slightly. To mitigate, explicitly use a floating number like 3f or 3.0.

  • At the @time addition. If duration is an integer, since 3 and rhythm.Sum() both ar integers, the whole division will be an integer, resulting in a wrong shift. The canon will not be aligned correctly. For example duration = rhtythm.Sum() / 3 or duration = melody.Length both implicitly type duration as an integer. To mitigate, the safest practice is to use explicit typing float duration or double duration.

The following example shows the issues mentioned.

0
C-1
Code Editor
0.000

The second usage example shows how to attach a grace note to an entity. This is not the recommended way to do so, since adding more than a single note gets quite tedious. Due to the split, there are two entities, each with a grace note.

0
C-1
Code Editor
0.000

Prepend and Append

Attaching new content to entities can be achieved by Prepend and Append commands. They either create a new entity of a given duration, or attach a prepared music object (see the Scope tutorial -- in preparation). Prepend places the new content right before the current entity, Append places it at its end.

The previous example can be rewritten using Prepend.

0
C-1
Code Editor
0.000

Often it is better to perform an attachment to the current working set only once as if it was a single entity. PrependOnce and AppendOnce achive exactly that.

0
C-1
Code Editor
0.000

Meter

For generation of music in D♭, the concept of measures is unnecessary. All note have the exact time attribute and the span (duration) attribute. A D♭ program usually sets the total song duration early on. But there are situations where it is rather preferable to set a fixed tempo and keep the duration unbound. Also the meta data in form of measures and beats information can be beneficial. This tutorial introduces the respective controls and provides a few practical examples.

The main reason to include meter infromation is to enable the computation of tempo from duration and vice versa. The adaptive music mode (work in progress, not published yet) is based on the tempo, the span is computed accordingly. Also sheet music export requires meter information to add correct time signatures to the staff.

Meter data is associated with two built-in entity attributes: beats and beatType. The former specifies how many beats are present in a single measure. The latter sets the note type for the beat, by default this is a quarter note denoted by 4. Other common beat types include eight notes (8) and half notes (2).

For the first meter example, we will reuse the melody from the Melody tutorial, but with a simpler rhythm which now fits into the 3/4 signature. We will return to the original rhythm further below.

0
C-1
Code Editor
0.000

The previous example defined the time signature which is essential for correct sheet music export. Without that, it would try to fit the music into the standard 4/4 signature resulting in a wrong score. But it had no impact on the generated music itself.

Tempo

The tempo is a built-in attribute given in quarters per minute. The very last example in this tutorial shows why D♭ uses quarters as a normalized unit instead of beats which may vary.

The tempo value has no effect on the generated music. It only appears as a value in the exported sheet music. But indirectly it enables the computation of the duration required for a given number of beats. The following example sets a tempo first and then computes the span using the Span command.

0
C-1
Code Editor
0.000

When the tempo is given in quarters per minute, it is invariant to the meter type. In the previous example, 100 qpm means that there will be 100 quarter notes played each minute, that corresponds to roughly 33 measures. Try changing the meter to 3/8 to see the effect. Since a beat is now an eigth note, there will be 200 eigth notes played each minute, which means roughly 67 measures. So the whole music gets twice as fast and Span will return half the original duration.

Other way round, Tempo can be used to compute the tempo given the current span.

0
C-1
Code Editor
0.000

If Span or Tempo are used without parameters, they will assume a single beat. They can both take a number beats, measures or both. For details see the inlined documentation.

Utilities

With help of the Meter utility class, it is possible to perform various computations and conversions with respect to the meter. D♭ is not aware of the metric note lengths, but the composer can usually introduces own structures that help to keep everything aligned in the envisioned metric structure.

A common use case is to determine note spans given the current tempo, for example when prepending grace notes.

0
C-1
Code Editor
0.000
0
C-1
Code Editor
0.000

Same way it is possible to insert a whole measure to the end of the melody.

0
C-1
Code Editor
0.000

This can be combined with the shifted and inverted melody from above. With help of the Meter.Beat utility it is much easier to compute the time offset. The result contains several disonancies in the second half. One of the advanced tutorials will address automated ways to resolve this.

0
C-1
Code Editor
0.000

The original melody had a slightly different rhythm using the following array for relative durations: 1f 1f 1f 2f 2f _ 1f 1f 1f 2f 2f. This is impossible to achieve in the 3/4 meter, so the most simple option would be to take 7/4 instead and use just two measures, each with 5 tones.

0
C-1
Code Editor
0.000

This option is valid only technically, because the accent on the first long note would be lost. The following example sets an altering 3/4 and 2/2 meter as the best musical solution. However, its implementation is not so straight forward.

0
C-1
Code Editor
0.000

A big topic are tempo transitions which are not supported by D♭ so far. This tutorial will be extended once they are available.

The next tutorial will dive deep into scopesd, variable extraction and music objects.