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) |
Source code in mmm_src/Patterns.py
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
Source code in mmm_src/Patterns.py
__init__(list)
¶
Initialize a random pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list
|
list[float]
|
A list of values to select from randomly |
required |
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
Source code in mmm_src/Patterns.py
__init__(list)
¶
Initialize a sequential pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list
|
list[float]
|
A list of values to cycle through sequentially |
required |
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. |
Source code in mmm_src/Patterns.py
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
Source code in mmm_src/Patterns.py
__init__(list)
¶
Initialize an exclusive random pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list
|
list[float]
|
A list of values to select from (should have at least 2 elements for proper exclusive behavior) |
required |
Source code in mmm_src/Patterns.py
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. |