Simple L-system implementation for melody generation


I’v been reading about melody generation and stumbled to this article describing melodic interpretation of L-systems. I tested the idea with sonic pi using simple regexp magic. This idea somehow resembles use of markov chains, but instead of playing the next variation the melody is generated in multiple generations. Now its super simple, but i’m planning to implement more features as part of ziffers notation.

axioms = {"0"=> "60 0 0 60 0", "60"=>"? 60 ?" }
start = "0"

6.times {start = start.gsub(Regexp.union(axioms.keys),axioms) }
notes = start.split(" ")

notes.each do |note|
  if note=="0" then
    sleep 0.25
  elsif note=="?" then
    play rrand_i(60,80) if note=="?"
    sleep 0.25
    play note.to_i
    sleep 0.25

Have fun trying out your own rules/axioms.