Skip to content

Patterns

Pattern classes for generating sequences of values.

This module provides pattern classes inspired by SuperCollider's pattern system for generating sequences, random selections, and non-repeating random selections from lists of values.

Classes:

Name Description
Pseq

Sequential pattern that cycles through a list

Prand

Random pattern that selects randomly from a list

Pxrand

Exclusive random pattern that avoids immediate repetition

PVoiceAllocator

Voice allocator for managing polyphonic voice assignments.

PVoiceAllocator keeps track of busy and free voices for polyphonic synthesis, allowing for efficient voice allocation and deallocation.

Attributes:

Name Type Description
num_voices

Total number of voices available

busy_list

List tracking the status of each voice (-1 for free, otherwise holds the note number)

__init__(num_voices)

Voice allocator for managing polyphonic voice assignments.

PVoiceAllocator keeps track of busy and free voices for polyphonic synthesis, allowing for efficient voice allocation and deallocation.

get_free_voice(note)

Looks for a free voice and assigns it to the given note. If a free voice is found, it marks it as busy with the note number and returns the voice index. If all voices are busy, it returns -1.

release_voice(note)

Looks through the busy_list for the provided note. If the note is found, frees the index and returns a Tuple with (True, index of found item). If the note is not found, returns (False, -1)

Prand

Random pattern that selects values randomly from a list.

Prand generates values by randomly selecting from a list with equal probability for each element. The same value can be selected consecutively.

Attributes:

Name Type Description
list

The list of values to select from randomly

Example
pattern = Prand([1, 2, 3, 4])
print(pattern.next())  # Random selection: 1, 2, 3, or 4
print(pattern.next())  # Another random selection

__init__(list)

Random pattern that selects values randomly from a list.

Prand generates values by randomly selecting from a list with equal probability for each element. The same value can be selected consecutively.

Attributes:

Name Type Description
list

The list of values to select from randomly

Example
pattern = Prand([1, 2, 3, 4])
print(pattern.next())  # Random selection: 1, 2, 3, or 4
print(pattern.next())  # Another random selection

next()

Get a random value from the list.

Returns:

Type Description
Optional[float]

A randomly selected value from the list. Returns None

Optional[float]

if the list is empty.

Pseq

Sequential pattern that cycles through a list of values.

Pseq generates values by iterating through a list sequentially, wrapping back to the beginning when it reaches the end.

Attributes:

Name Type Description
list

The list of values to cycle through

index

Current position in the list (starts at -1)

Example
pattern = Pseq([1, 2, 3])
print(pattern.next())  # 1
print(pattern.next())  # 2
print(pattern.next())  # 3
print(pattern.next())  # 1 (cycles back)

__init__(list)

Sequential pattern that cycles through a list of values.

Pseq generates values by iterating through a list sequentially, wrapping back to the beginning when it reaches the end.

Attributes:

Name Type Description
list

The list of values to cycle through

index

Current position in the list (starts at -1)

Example
pattern = Pseq([1, 2, 3])
print(pattern.next())  # 1
print(pattern.next())  # 2
print(pattern.next())  # 3
print(pattern.next())  # 1 (cycles back)

go_back(n=1)

Move the sequence index back by n steps.

Parameters:

Name Type Description Default
n

Number of steps to move back in the sequence

1

next()

Get the next value in the sequence.

Returns:

Type Description
Optional[float]

The next value in the list, cycling back to the beginning

Optional[float]

when reaching the end. Returns None if the list is empty.

Pxrand

Exclusive random pattern that avoids immediate repetition.

Pxrand generates values by randomly selecting from a list while ensuring that the same value is never selected twice in a row. This is useful for creating varied sequences without consecutive duplicates.

Attributes:

Name Type Description
list

The list of values to select from

last_index

Index of the previously selected value

Note

This pattern requires a list with at least 2 elements to function properly and avoid repetition.

Example
pattern = Pxrand([1, 2, 3, 4])
print(pattern.next())  # Random selection: 1, 2, 3, or 4
print(pattern.next())  # Different from previous selection

__init__(list)

Exclusive random pattern that avoids immediate repetition.

Pxrand generates values by randomly selecting from a list while ensuring that the same value is never selected twice in a row. This is useful for creating varied sequences without consecutive duplicates.

Attributes:

Name Type Description
list

The list of values to select from

last_index

Index of the previously selected value

Note

This pattern requires a list with at least 2 elements to function properly and avoid repetition.

Example
pattern = Pxrand([1, 2, 3, 4])
print(pattern.next())  # Random selection: 1, 2, 3, or 4
print(pattern.next())  # Different from previous selection

next()

Get a random value that differs from the previous selection.

Returns:

Type Description
Optional[float]

A randomly selected value from the list that is guaranteed

Optional[float]

to be different from the previous selection. Returns None

Optional[float]

if the list is empty.