L-System

Example script (scamp_extensions): examples/Composition & form/Algorithmic approaches/l_system_example.py

Download: l_system_example.py

Requires the scamp_extensions package (pip install scamp_extensions).

Example usage of the from LSystem class, which allows you to set a vocabulary of symbols, set their rewrite rules and meanings, and evolve and play the resulting L-System.

Topics: Composition & form › Algorithmic approaches

from scamp import *
from scamp_extensions.process import LSystem

s = Session()
s.tempo = 100


perc1 = s.new_part("Perc. 1", preset="power")
perc2 = s.new_part("Perc. 2", preset="power")
perc3 = s.new_part("Perc. 3", preset="power")


l_syst1 = LSystem(
    "a",
    {
        "a": "c b",
        "b": "bac",
        "c": " aa"
    },
    {
        " ": None,
        "a": 56,
        "b": 60,
        "c": 81,
    }
)

l_syst2 = LSystem(
    "b",
    {
        "a": "c b",
        "b": "bac",
        "c": " aa"
    },
    {
        " ": None,
        "a": 50,
        "b": 67,
        "c": 63,
    }
)

l_syst3 = LSystem(
    "c",
    {
        "a": "c b",
        "b": "bac",
        "c": " aa"
    },
    {
        " ": None,
        "a": 70,
        "b": 75,
        "c": 82,
    }
)


def play_notes(instrument, pitches, volume, duration):
    for pitch in pitches:
        instrument.play_note(pitch, volume, duration)


for generation in range(6):
    print(f"Generation {generation}:")
    print(f"  Part 1: {l_syst1.get_generation(generation)}")
    print(f"  Part 2: {l_syst2.get_generation(generation)}")
    print(f"  Part 3: {l_syst3.get_generation(generation)}")
    fork(play_notes, args=(perc1, l_syst1.get_generation_meanings(generation), 0.8, 0.25))
    fork(play_notes, args=(perc2, l_syst2.get_generation_meanings(generation), 0.8, 0.25))
    fork(play_notes, args=(perc3, l_syst3.get_generation_meanings(generation), 0.8, 0.25))
    wait_for_children_to_finish()
    wait(2)