RhythmPattern¶
rhythm_pattern ¶
Rhythm pattern generators.
The RhythmPattern namespace produces binary rhythm patterns (lists of
1s and 0s) from various encodings: integers (binary digits), hex strings,
onset spacing, random/probability vectors, and Euclidean distribution.
A pattern is just a list[int] where each element is 0 (rest) or 1
(onset).
Example
from tonal_py import RhythmPattern RhythmPattern.euclid(8, 3) # 3 beats spread over 8 steps [1, 0, 0, 1, 0, 0, 1, 0] RhythmPattern.binary(13) # 13 = 0b1101 [1, 1, 0, 1] RhythmPattern.onsets(1, 2, 1) # 1, then 2 zeros, then 1, then 1 zero [1, 0, 1, 0, 0, 1, 0]
Source parity: @tonaljs/rhythm-pattern
binary ¶
Build a pattern from the binary digits of one or more integers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*numbers
|
int
|
Integers whose binary representations will be concatenated. |
()
|
Returns:
| Type | Description |
|---|---|
RhythmPattern
|
Pattern as a list of 1s and 0s. |
Example
from tonal_py import RhythmPattern RhythmPattern.binary(13) [1, 1, 0, 1] RhythmPattern.binary(13, 5) [1, 1, 0, 1, 1, 0, 1]
Source code in src/tonal_py/rhythm_pattern.py
hex ¶
Build a pattern from a hex string. Each hex digit becomes 4 bits.
Invalid hex characters become 0000.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hex_number
|
str
|
A string of hex digits. |
required |
Returns:
| Type | Description |
|---|---|
RhythmPattern
|
Pattern as a list of 1s and 0s. |
Example
from tonal_py import RhythmPattern RhythmPattern.hex("a") [1, 0, 1, 0] RhythmPattern.hex("ff") [1, 1, 1, 1, 1, 1, 1, 1]
Source code in src/tonal_py/rhythm_pattern.py
onsets ¶
Build a pattern from onset spacings (1, then N zeros, repeat).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*numbers
|
int
|
Number of zeros between consecutive onsets. |
()
|
Returns:
| Type | Description |
|---|---|
RhythmPattern
|
Pattern that starts with |
RhythmPattern
|
then |
Example
from tonal_py import RhythmPattern RhythmPattern.onsets(1, 2, 2, 1) [1, 0, 1, 0, 0, 1, 0, 0, 1, 0]
Source code in src/tonal_py/rhythm_pattern.py
random ¶
random(length: int, probability: float = 0.5, rnd: Callable[[], float] | None = None) -> RhythmPattern
Build a random rhythm pattern of fixed length.
Each step is 1 if rnd() >= probability, else 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
Number of steps. |
required |
probability
|
float
|
Threshold (default 0.5). |
0.5
|
rnd
|
Callable[[], float] | None
|
Optional RNG function returning a float in [0, 1). Defaults
to |
None
|
Returns:
| Type | Description |
|---|---|
RhythmPattern
|
Pattern as a list of 1s and 0s. |
Example
from tonal_py import RhythmPattern
Deterministic RNG for testing¶
seq = iter([0.9, 0.1, 0.9, 0.1]) RhythmPattern.random(4, 0.5, rnd=lambda: next(seq)) [1, 0, 1, 0]
Source code in src/tonal_py/rhythm_pattern.py
probability ¶
Build a pattern with per-step probability thresholds.
Step i is 1 if rnd() <= probabilities[i].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
probabilities
|
list[float]
|
One probability per step (length determines pattern length). |
required |
rnd
|
Callable[[], float] | None
|
Optional RNG function. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
RhythmPattern
|
Pattern as a list of 1s and 0s. |
Example
from tonal_py import RhythmPattern seq = iter([0.1, 0.5, 0.9]) RhythmPattern.probability([0.5, 0.5, 0.5], rnd=lambda: next(seq)) [1, 1, 0]
Source code in src/tonal_py/rhythm_pattern.py
rotate ¶
Rotate a pattern right by rotations steps (negative shifts left).
Example
from tonal_py import RhythmPattern RhythmPattern.rotate([1, 0, 0, 1, 0, 0, 1, 0], 1) [0, 1, 0, 0, 1, 0, 0, 1] RhythmPattern.rotate([1, 0, 0, 1, 0, 0, 1, 0], -1) [0, 0, 1, 0, 0, 1, 0, 1]
Source code in src/tonal_py/rhythm_pattern.py
euclid ¶
Euclidean rhythm — distribute beats as evenly as possible across steps.
The Bjorklund algorithm produces a pattern with the most uniform spacing of beats. Many traditional rhythms (cinquillo, tresillo, etc.) are euclidean rhythms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
steps
|
int
|
Total length of the pattern. |
required |
beats
|
int
|
Number of onsets to distribute. |
required |
Returns:
| Type | Description |
|---|---|
RhythmPattern
|
Pattern as a list of 1s and 0s. |
Example
from tonal_py import RhythmPattern RhythmPattern.euclid(8, 3) # tresillo [1, 0, 0, 1, 0, 0, 1, 0] RhythmPattern.euclid(16, 5) [1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]