D♭ Tutorial: MELODY

In the previous tutorials we introduced attributes and batch functions. We should have all the tools necessary to start creating some interesting stuff. Let us create a simple melody first. We will later develop it further.

Raw melody

Imagine you are in C major key. The basic melody goes like this: C D E F F _ G F E F F. Since D♭ operates in diatonic mode by default (i.e. degree attribute relatess to the degrees of the current scale), we will encode the melody into an array of relative diatonic distances to the tonic: 0 1 2 3 3 _ 4 3 2 3 3.

Let us translate this into D♭ code. We store the relative distances in an array. Setting the span (in seconds) to equal the array length should be fine for now. We will use the Split function to divide the initial entity into as many new entities as we defined in the melody. Remember that Split orders the linearly so that will sound one after each other in regular intervals. Finally, using the split indexer we assign the degree of each new entity to the corresponding melody item.

0
C-1
Code Editor
0.000

Varying tone durations

All tones in the melody are equally long. It would be better to close the short phrases by longer notes. Along with the melody array we can create a second array with the respective durations. Since we will use Split again, it will fill the available span. So it is sufficient that their relative ratios are correct, the exact scaling will be computed by Split in the background.

Let us make each pair of the F notes twice as long, so they will be weighted by 2f while others will get 1f. So the array will be 1f 1f 1f 2f 2f _ 1f 1f 1f 2f 2f. Relative durations may be floats but also integers.

0
C-1
Code Editor
0.000

This melody sounds like it is resolving too fast. What about producing a variation that would make it an open question and a concluded response. We will use a custom function Sentence for that. In the previous examples, melodies and durations were limited to the local scope of Main. To make them accessible from the custom Sentence function, they need to be moved upwards to the global scope.

0
C-1
Code Editor
0.000

A second voice would certainly make the melody more interesing. Voicing is part of later tutorials on harmony and orchestration, but we can use Split to emulate the effect by playing the voices one after each other. In the following we invert the melody by multiplying the degree by -1 to change from a parallel movement of the voices to counter movement.

0
C-1
Code Editor
0.000

The example melodies we created were all constant without any variation. The next tutorial will introduce randomization of attributes like degree to produce a new melody each time the music script is executed.

D♭ also has a specialized melody generator which is based on constraint programming. It will be covered by one of the advanced tutorials on constraint generation.