💾 Archived View for thrig.me › blog › 2023 › 09 › 23 › random-melody-generation.gmi captured on 2024-06-16 at 12:47:18. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-11-14)
-=-=-=-=-=-=-
Weighted random assigns a particular weight to each possible note, and then you roll for each note in turn. For example with a 100-sided die the note "C" might be from 1 to 20, the note "D" from 21 to 35, etc.
$ perl wrandom.pl A C A G C C F G G C C G G F G B
This produces bad results, at least from a musical standpoint. Walker's Alias Method (which is what the Math::Random::Discrete module implements) might however be good to know about, as a fairly efficient way to make weighted random choices.
Instead of selecting a note to play, one can (with a starting note) roll a random interval to use, then repeat the interval selection from the next note and the next. This produces a wandering walk that often has "drunken" attached to it. There is a risk here that the walk could walk off the ambitus (range) of the instrument or MIDI standard, so there may need to be guards for that.
$ perl rinterval.pl 60 63 61 60 63 64 64 67 68 66 64 66 64 67 68 71
This may work better if the intervals worked up an down some scale rather than all the 12 notes in the typical Western octave, but in that case the intervals available would depend on the note in question: "C" moving to "D", "E", or "F" uses different intervals than "B" moving to "C", "D", or "E".
Some of these methods can also be used to "procgen" in computer games, by the way.
A recent innovation is fractal noise, which lies somewhere between complete chaos (unweighted random selection) and complete order (the same note over and over and over). Richard F. Voss devised some generative methods here.
Gardner-WhiteBrownFractalMusic.pdf
$ perl fractal.pl E D F F D C E D D C E E D D D E
This one does not move around much, though there are chants and other melodic lines that behave somewhat like this. Maybe with the addition of some rhythmic variation…
Atonality has the notion of a "pitch set" whereby one has a set of notes, and a note cannot be reused until the entire set has been used. So given A, B, C, D and you play a D, then you have to pick from A B C, and so on until nothing remains of the set. And then you do that again, or pick a different pitch set to use.
The method used to pick the next note to use could be unweighted, weighted, fractal, or whatever, so the unique thing here is to not repeat a note until all the notes of the set have been gone through.
$ atonal-util basic a b c d c,d,ees,f 122010 4-10
https://en.wikipedia.org/wiki/List_of_set_classes
This is similar to the atonal method, though may reuse a pitch before the set has been gotten through depending on the exact algorithm used to assign notes back onto the priority queue. One could have priority queues for the note, duration, and velocity (MIDI loudness). The results will depend on the particulars of the algorithms used to assign things back onto their appropriate queue.
Here the velocity priority queue placement depends on the pitch and duration, the duration on the pitch, and the pitch on the velocity and how frequently the particular pitch has been used. Or something like that. It's actually totally deterministic, but may sound random due to how the velocity and duration changes are fiddling with the pitch selection. (The MIDI velocity is randomized by 3d3-3 points, though this randomization is only applied to the MIDI output, and does not influence the priority queue logic.)
https://thrig.me/src/compositions.git
A markov chain can help solve various problems with the drunken walk; for one, the lowest available note would only move to higher notes, and particular scales can be encoded by only allowing the nodes on the markov chain to move to other members of the scale.
$ perl markov.pl 60 67 64 62 69 65 60 67 62 65 67 64 62 60 62 60
The rhythm can also be controlled in a similar fashion, though here you may want to pick the rhythm by what fits into a measure or set of measures, and then fill the notes into that.
Not award winning stuff, but these methods might help you generate melody ideas? Also many of these methods are computationally inexpensive and could be done with coins or a paper spinner or by throwing sticks at markings in the sand.
gemini://oberdada.pollux.casa/gemlog/2023-09-24_random_music.gmi
tags #composition