💾 Archived View for thrig.me › blog › 2023 › 08 › 08 › least-recently-used.gmi captured on 2024-07-09 at 01:08:14. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-11-14)

-=-=-=-=-=-=-

Least Recently Used

A technique for algorithmic composition is to pick something least recently used. This could be deterministic, or one might randomize the selection. Atonality uses this with a pitch set, say of C D E G, where if you use C then D E G are available because they are the least recently used. Computers can help with this.

    #!/usr/bin/perl
    use 5.36.0;
    use List::Util 'shuffle';

    # given an array reference, picks one item randomly from the front,
    # and returns that item after moving said item to the end
    sub picker ( $ref, $shuffle = 1, $bounds = ~0 ) {
        if    ( !$bounds or $bounds > $#$ref ) { $bounds = $#$ref }
        elsif ( $bounds < 1 )                  { $bounds = 1 }
        my @intervals = $shuffle ? shuffle @$ref : @$ref;
        my $last      = $#intervals;

        sub () {
            my $pick = rand $bounds;
            @intervals[ $pick, $last ] = @intervals[ $last, $pick ];
            return $intervals[$last];
        }
    }

    my $fn = picker [ 1, 2, 3 ];
    say $fn->() for 1 .. 10;

least-recently-used.pl

Choices here are how much of the list to use when picking a new value, and whether to shuffle the list in advance. A downside of a closure is that modifying the original list is then difficult, though the callback sub could modify the items somehow.

    $ perl least-recently-used.pl
    3
    2
    1
    2
    1
    3
    2
    1
    3
    2

One use for this is to construct random scales comprised of minor, major seconds and minor thirds, though the values could also be used to pick a rhythm or a chord to use. Here's descending made-up scales that reset up to some random high starting note whenever the drop has gone on for too long. The length of the drop is also randomized.

/music/loops/blippybeats.mp3

Someone who actually knows music called it "Kraftwerkian".

The following version of the code modifies the intervals on the fly, and picks a random duration, though it takes a longer run to see how wacky the results can get. Probably for a background pad you would want something short and then to repeat that, maybe with variations.

pad-drop.mp3

out.midi

Mostly this is fiddling with the code and re-rolling the random numbers until something not quite terrible emerges.

Priorities

/music/priorities-4.2.mp3

Here a priority queue ranks what pattern to use, though again there is randomization on where a used item is stuck into the queue. The code sometimes gets stuck on particular patterns, though.

tags #composition #music