Self-modifying algorithms

Another easy and neverending source for self-modifying or self-similar melodies are different kinds of string rewrite systems, such as lindenmayer system.

The idea is similar to cellular automata in some ways. Define set of rules and iterate them over same string over and over again. For the rules you can invent your own “vocabulary” and the meaning of things. Here is a simple example where the rules and results are interpreted as degrees and applied using gsub. You could also add more meaning and use some characters to represent rests or certain samples or anything you like:

rules = {
  "1"=>"1 3",
  "3"=>"2 4",
  "4"=> "6",
  "6"=>"1"
}

melody = "1 2"

live_loop :lindenmayer do
  melody = melody.gsub(Regexp.union(rules.keys), rules)
  print melody
  melody.split(" ").each do |d|
    play degree d, :e, :mixolydian
    sleep 0.25
  end
end
3 Likes