# Patterns

### Pattern

Renardo uses in its Player() objects Python lists, known more commonly as arrays in other languages, to sequence themselves. It has been used here already previously but they aren’t exactly flexible for manipulation.

For example, try multiplying a list by two like this:

*Console output >> [1,2,3,1,2,3]*

*Does the result meet your expectations?*

Renardo uses a container type called a ‘Pattern’ to help solve this problem. They act like regular lists but any mathematical operation performed on it is done to each item in the list and done so pair-wise if using a second pattern. A basic pattern is created as you would with a normal list or tuple, but with a ‘P’ preceeding it.

In this operation, the output consists of all the combinations of the two patterns i.e. [1+3, 2+4, 3+3, 1+4, 2+3, 3+4]

You can use Python’s slicing syntax to generate a series of numbers:

Try some other mathematical operators and see what results you get.

Pattern objects also automatically interlace any nested list. Compare a normal list:

with Pattern

Use PGroups if you want this behavior to be avoided. These can be implicitly specified as tuples in Patterns:

This is a PGroup:

In Python, you can generate a range of integers with the syntax range(start, stop, step). By default, start is 0 and step is 1.

You can use PRange(start, stop, step) to create a Pattern object with the equivalent values:

P[0, 2, 2, 6, 4, 10, 6, 14, 8, 18]
[0*1, 1*2, 2*1, 3*2, 4*1, 5*2, 6*1, 7*2, 8*1…]

Adding a list (or Pattern) to a Pattern will add the values of the elements to the other where Python lists would be concatonated.

To concatonate Patterns, use the pipe operator like so:

FoxDot automatically converts any object being piped to a Pattern to the base Pattern class so you don’t have to worry about making sure everything is the right type. Plays all the values together:

Spreads the values across the current “dur” e.g. if the dur is 2 beats then it will play each value 2/3 beats apart:

Is the same as P* but every other time the notes are played they are spread over the dur value.

Spreads the values across the current “sus” e.g. if the dur is 2 beats and the sus is 3 beats then it will play each value 1 beat apart.

Spreads the first (length-1) values with a gap of the last value between each Plays 0,2,4 with a gap of 0.5:

Patterns come with several methods for manipulating the contents

Standard pattern

Shuffle pattern by randomizing it

Append a reversed pattern to the pattern

Shift the pattern by n (default 1)

Takes the pattern and appends it as many times as needed to reach n number of elements in the pattern:

Reverses a pattern

Loops a pattern n number of times

Add an offset

Add a multiplied offset

Stutter - Repeat each element n times

**Amen** - Merges and laces the first and last two items such that a drum pattern “x-o-” would become “(x[xo])-o([-o]-)” and mimics the rhythm of the famous “amen break”

**Bubble** - Merges and laces the first and last two items such that a drum pattern “x-o-” would become “(x[xo])-o([-o]-)

If you want to edit the internal values in Python you need to use a for loop:

or in the list understanding:

*Console output >> [2,4,6]*

But what if you want to multiply the values in a list by 2 and 3 alternately?

Renardo uses a type of container called “Pattern” to solve this problem. They behave like regular lists, but any math operation performed on them is performed on each item in the list, and paired if a second pattern is used.

The basic pattern can be created as follows:

*Console output >> P[2,4,6]*

*Console output >> P[4,6,6,5,5,7]*

Notice how in the second operation the output is any combination of the two patterns >> [1+3,2+4,3+3,1+4,2+3,3+4].

### Pattern

*Try some other math operators and see what results you get!*

What if you group numbers in brackets like P[1,2,3] * (1,2)?

There are several other pattern classes in Renardo that you can use to generate arrays of numbers, but they behave just like the base pattern.

In Python you can use the syntax area (start, stop, step) to generate a range of integers. By default, Start is 0 and Step 1.

With PRange (start,stop,step) you can create a sample object with the appropriate values. The first example shows the equivalent function in Python, the second is the simplified sample function in Renardo PRange:

*Console output >> [0,1,2,3,4,5,6,7,8,9]*

*Console output >> P[0,1,2,3,4,5,6,7,8,9]*

*Console output >> P[0,2,2,6,4,10,6,14,8,18]*

But what about combining patterns? In Python you can concatenate (append) two lists with the + operator. However, Renardo patterns use this to supplement the data in the list. To connect two Pattern objects together, you can use the pipe symbol, which Linux users may be familiar with. It is used to connect command line programs by sending the output of one process as input to another.

*Console output >> P[0,1,2,3,1,7,6]*

There are several types of pattern sequences in Renardo (and the list is still growing) that make generating these numbers a little easier. For example, to play the first octave of a pentatonic scale from bottom to top and back again, you can use two PRange objects:

The PTri class does this for you:

### Pattern functions

There are several functions that generate a pattern of values for us to do useful things in Renardo, such as: Rhythms and melodies. This section is a list of pattern functions with descriptions and examples.

Used as input arguments for Renardo players, these can themselves be treated as patterns and their methods applied directly, e.g. B. *PDur(3,8).reverse()*. You can also replace each input argument with a pattern or a TimeVar function to create an advanced pattern or a Pvar pattern. Let’s look at some examples:

**PStep(n,value,default=0)** >> Returns a pattern where every **n**-term is **value**, otherwise **default**.

Every 4, make it 1, otherwise default to 0

Every 8, make it 6, otherwise, 4

Every 5, make it 2, otherwise, 1

**PSum(n,total,**kwargs)** >> Returns a pattern of length **n**, the sum of which is **total**. For example: PSum(3,8) -> P[3,3,2] PSum(5,4) -> P[1,0.75,0.75,0.75,0.75].

Returns a pattern of length 2, with elements summed up to 8

Returns a pattern of length 5, with elements summed up to 4

**PRange(start,stop=None,step=None)** >> Returns a pattern equivalent to Pattern(range(start,stop,step)).

**PTri(start,stop=None,step=None)** >> Returns a pattern equivalent to Pattern(range(start,stop,step)) with the inverted shape appended. Think of it like a “Tri”angle.

Up to 5 then down to 1:

Up to 8 then down to 1:

From 3 to 10, then down to 4:

From 3 to 30, by 2, then down to 4:

Up to 4, then down to 1, then up to 8, then down to 1:

Same as:

**PEuclid(n,k)** >> Returns the Euclidean rhythm that distributes **n** pulses as evenly as possible over **k** steps. e.g. PEuclid(3,8) returns P[1,0,0,1,0,0,1,0].
3 pulses over 8 steps:

**PSine(n=16)** >> Returns values of one cycle of a sine wave divided into **n** parts.

Split into 5 parts:

Split into 10:

**PDur(n,k,dur=0.25)** >> Returns the actual duration based on Euclidean rhythms (see PEuclid), where **dur** is the length of each step. e.g. PDur(3,8) returns P[0.75,0.75,0.5].

Gives a list of 3 dur, appened with a list of 5 dur

**PBern(size=16,ratio=0.5)** >> Returns a pattern of ones and zeros based on the **ratio** value (between 0 and 1). This is known as the Bernoulli sequence.

**PBeat(string,start=0,dur=0.5)** >> Returns a pattern of durations based on an input string, where non-spaces denote a pulse.

**PSq(a=1,b=2,c=3)**

**PIndex** >> Returns the index being accessed

### Pattern generators

We know that patterns have a fixed length and can be generated based on a function. However, sometimes it is useful to have patterns of infinite length, for example when generating random numbers. This is where pattern generators come into play. Similar to Python generators where not all values are kept in memory at once, except when Python generators usually have an end - Renardo pattern generators don’t!

**PRand(lo,hi,seed=None)/PRand([values])** >> Returns a series of random integers between lo and hi, inclusive. If hi is omitted, the range is 0 to lo. A list of values can be provided in place of the range and PRand returns a series of values chosen at random from the list.

Returns a random integer between 0 and start.

Returns a random integer between start and stop.

If start is a container-type it returns a random item for that container.

You can supply a seed

Keeps generating random tune

Creates a random list, and iterates over that same list

**PxRand(lo, hi) / PxRand([values])** >> Identical to PRand, but no elements are repeated.

**PwRand([values], [weights])** >> Uses a list of weights to indicate how often items with the same index are selected from the list of values.
A weight of 2 means it is twice as likely to be picked as an item weighing 1.

**P10(n)**>> Returns a pattern of length n of a randomly generated series of ones and zeros.

**PAlt(pat1, pat2, *patN)** >> Returns a pattern generated by alternating the values in the specified sequences.

**PJoin(patterns)** >> Assembles a list of patterns.

**PPairs(seq,func= )** >> Links a sequence to a second sequence obtained by executing a function on the original. By default, this is lambda n: 8-n.

**PQuicken(dur=0.5,stepsize=3,steps=6)** >> Returns a group of delay amounts that gradually decrease.

**PRhythm(durations)** >> Converts all tuples / PGroups into delays, which are calculated with the PDur algorithm.

The following plays the hi hat with a Euclidean Rhythm of 3 pulses in 8 steps

**PShuf(seq)** >> Returns a mixed version of seq. This example uses a function to automatically shuffle the list.

**PStretch(seq,size)** >> Returns ‘seq’ as a pattern and is looped until its length is ‘size’, e.g. PStretch ([0,1,2], 5) returns P[0,1,2,0,1].

**PStrum(n=4)**

**PStutter(seq,n=2)** >> Creates a pattern so that each element in the array is repeated n times (n can be a pattern).

**PZip(pat1,pat2, patN)** >> Generates a pattern that ‘zips’ multiple patterns. PZip([0,1,2], [3,4]) creates the pattern P[(0,3),(1,4),(2,3),(0,4),(1,3),(2,4)].

**PZip2(pat1,pat2,rule= )** >> Like PZip, but only uses two patterns. Connects values if they meet the rule.

**Pvar** >> TimeVar, which saves lists instead of individual values (var, sinvar, linvar, expvar).

**PWhite(lo,hi)** >> Returns random floating point numbers between lo and hi.

Lo defaults to 0, hi defaults to 1

Returns random numbers between 1 and 5

**PChain(mapping_dictionary)** >> Based on a simple Markov chain with equal probabilities. Takes a dictionary of elements, states, and possible future states. Every future state has an equal chance of being selected. If a possible future state is not valid, a KeyError is raised.

**PWalk(max=7,step=1,start=0)** >> Returns a series of integers with each element an increment apart and with a value in the range of +/- the maximum. The first element can be selected with start.

By default, returns a pattern with each element randomly 1 higher or lower than the previous

Changing step

With max

Start at a non-zero number

**PFibMod()** >> Returns the Fibonacci sequence.

### Custom Pattern Generator

Custom generator patterns can be made by subclassing GeneratorPattern and overriding `GeneratorPattern.func`

This can be done more consisely using `GeneratorPattern.from_func`

, passing in a function which takes an index and returns some pattern item.

We can use lambdas too