Markov Rhythm Generator — User Guide

A cyclic rhythm-template generator in which each Markov state is a complete binary rhythm necklace. State changes occur once per completed pattern cycle, and transition probabilities are derived from rhythmic Hamming distance.

Author: Shai Cohen Affiliation: Department of Music, Bar-Ilan University, Israel Version: 0.5.2 (2026) License: MIT License Repo: Praat AudioTools
Contents:

What this does

The generator uses eight binary rhythm templates as the states of a Markov chain. A template such as 10001010 is read on a regular pulse grid: 1 means onset and 0 means silence.

The important unit is the whole pattern cycle. The selected state remains active for one complete necklace, all of its onsets are rendered, and only then is the next Markov state chosen.

The Markov process therefore selects the next rhythm pattern, not the next individual hit.

All eight states inside one preset have the same number of steps. This keeps tempo and subdivision explicit instead of compressing patterns of different lengths into the same beat.

Quick start

  1. Run Markov_Rhythm_Generator.praat. No input Sound is required.
  2. Choose Custom or one of the eight rhythm families.
  3. Set Duration, Sample rate, Tempo, Base frequency, Decay rate, Wood character, and optional Canon mode.
  4. Open Edit Markov rhythm details for Steps per beat, Transition temperature, State persistence, Swing, Metric accent, Master amplitude, Edge fade, and Random seed.
  5. Run the script. One Markov rhythm realization is created first; canon voices, if selected, reuse that exact realization with delayed entries.
Custom does not provide a GUI editor for the eight binary necklaces. It uses the script's default eight 8-step templates and lets you customize their Markov/timing behavior. Editing the binary templates themselves requires editing the script.

Rhythm states as necklaces

Each Markov state is a cyclic binary pattern. The cycle duration is:

beatDuration = 60 / Tempo
stepDuration = beatDuration / StepsPerBeat
cycleDuration = PatternLength × stepDuration

For example, an 8-step pattern at 2 steps per beat lasts four beats; a 16-step pattern at 4 steps per beat also lasts four beats.

State-density compensation

Different states can contain different numbers of onsets. To prevent a denser pattern from becoming louder merely because it triggers more attacks, every state receives an approximate energy compensation:

stateGain = sqrt(meanOnsetCount / stateOnsetCount)

A state with many hits is therefore reduced relative to a sparse state. This is a density-balancing heuristic, not loudness normalization.

Metric accent

If an onset lands on the first grid step of a beat, its amplitude is multiplied by:

1 + MetricAccent

Other subdivisions keep their unaccented value.

Markov transition model

The transition matrix is calculated from the binary similarity between rhythm templates. For each pair of states, the normalized Hamming distance is:

d(i,j) = mismatching steps / pattern length

The current state receives an exact self-transition probability equal to State persistence. The remaining probability is distributed among the seven alternative states according to their Hamming distance.

P(i,i) = persistence

P(i,j) = (1 - persistence) × normalizedWeight(i,j)
for j ≠ i

Transition temperature

Lower temperature concentrates the remaining transition probability on rhythmically similar necklaces. Higher temperature makes distant templates more competitive.

Version 0.5.2 uses a numerically stable shifted exponential:

weight(i,j) =
    exp(-(d(i,j) - dNearestAlternative) / temperature)

Subtracting the nearest alternative distance does not change the normalized probability ratios. It prevents every alternative weight in a row from underflowing to zero when a very small positive temperature is used.

Theoretical versus realized persistence

State persistence is the theoretical probability of remaining in the same state at each cycle boundary. A finite realization may have a different empirical self-transition rate, especially when Duration contains only a few pattern cycles. The Info output reports both values.

Random seed

Random seed = 0 uses an unpredictable random state. A positive seed reproduces the initial state and the subsequent Markov choices for identical settings.

Tempo, grid, and swing

Steps per beat

Steps per beat is a real metric subdivision, not a display setting. It determines the duration of every binary step and therefore the total cycle duration.

Swing

Swing is available only when the grid has exactly two steps per beat. The second subdivision of each beat moves from the straight midpoint toward two-thirds of the beat:

offbeat delay = SwingAmount × beatDuration / 6

Thus:

The rhythm pattern itself is unchanged; swing changes onset timing.

Pulse sound

Every onset triggers the same compact synthetic woody transient. It is intentionally not presented as a physical model of actual claves or woodblocks.

The source contains three exponentially decaying resonant components:

fundamental:       f
wood resonance:    2.76 × f
click component:   4.00 × f

The approximate source is:

0.70 × exp(-Decay × t)       × sin(2π f t)
+
0.42 × WoodCharacter
     × exp(-1.45 × Decay × t) × sin(2π 2.76f t)
+
0.18 × exp(-3.5 × Decay × t) × sin(2π 4f t)

Wood character controls only the strength of the 2.76× inharmonic resonance. It does not change decay time, pitch, or the 4× click component.

Pulse duration

The local rendering window is derived from Decay rate:

pulseDuration =
    min(0.14,
        max(0.025, 6 / DecayRate))

Each pulse is rendered only inside that short local region rather than creating a full-duration temporary Sound.

Frequency safety

The script reserves 0.45 × Fs as safe sampling headroom and includes the highest 4× resonance and the highest canon register multiplier when calculating a common frequency scale.

frequencyScale =
    min(1,
        safeTop /
        (4 × maxCanonMultiplier × BaseFrequency))

The same scale is applied to the complete resonant model. If protection would move the effective base below 80 Hz, the script stops instead.

Rhythmic canon

The canon modes do not generate independent Markov chains. One base realization is generated and copied exactly into delayed voices.

ModeVoicesRegister multipliersSpatial positions
No Canon11.00×Mono
Canon 2 voices21.00×, 1.25×Left, right
Canon 3 voices31.00×, 1.25×, 0.80×Left, center, right

Canon delay is measured in beats

canonDelaySeconds =
    CanonDelayBeats × 60 / Tempo

This preserves the musical delay when tempo changes. The output duration becomes:

No canon:    Duration
2 voices:    Duration + 1 × canonDelay
3 voices:    Duration + 2 × canonDelay

The base Markov realization itself still occupies exactly the requested base Duration; only the delayed canon entries extend the final Sound.

Stereo canon voices use equal-power panning. The two-voice canon places its voices at the stereo extremes; the three-voice canon adds a centered middle voice.

Controls

ControlDefault / rangeBehavior
Duration12 s; max 180 sBase Markov realization duration before any delayed canon tail.
Sample rate44.1 kHz; 8–192 kHzDirect synthesis and final output rate.
Tempo120 BPM; 20–400Determines beat, grid-step, cycle, and canon-delay timing.
Base frequency1800 HzFundamental of the woody transient before common headroom scaling.
Decay rate60; >0–1000Exponential decay constant and basis for pulse rendering duration.
Wood character.40; 0–1Amplitude of the 2.76× inharmonic mode.
Canon modeNo CanonOne, two, or three copies of the same rhythm realization.
Canon delay1 beatMetric delay between canon entries; converted to seconds after tempo is known.

Markov / timing details

ControlDefaultBehavior
Steps per beat4Metric subdivision; 1–16.
Transition temperature.24Controls how strongly Hamming similarity favors nearby alternative states; >0–5.
State persistence.42Exact theoretical self-transition probability; 0–1.
Swing amount0Timing displacement for the offbeat; 0–1 and only valid with 2 steps/beat.
Metric accent.18Extra gain on onsets at the beginning of each beat.
Master amplitude.58Global pulse gain before overlap compensation and final protection.
Edge fade.015 sCommon linear fade-in/out, capped at 10% of complete output duration.
Random seed00 = unpredictable; positive = reproducible Markov realization.

Presets

PresetPattern/grid designMain overrides
Simple MarchEight 8-step necklaces; 2 steps/beat100 BPM; 1500 Hz; decay 50; wood .30; temperature .18; persistence .55
Complex FunkEight 16-step necklaces; 4 steps/beat110 BPM; 2000 Hz; decay 70; wood .50; temperature .28; persistence .30
Techno GridEight 16-step necklaces; 4 steps/beat130 BPM; 1200 Hz; decay 80; wood .20; temperature .22; persistence .45
Swing FeelEight 8-step necklaces; 2 steps/beat90 BPM; 1600 Hz; decay 55; wood .40; temperature .22; persistence .38; swing .92
Broken BeatEight 16-step necklaces; 4 steps/beat140 BPM; 1900 Hz; decay 65; wood .50; temperature .36; persistence .25
Clave / Timeline GeometryEight 16-step timeline-related necklaces; 4 steps/beat120 BPM; 2200 Hz; decay 75; wood .60; temperature .18; persistence .40
3:4 Composite GridEight rotations of a 12-step composite necklace; 3 steps/beat100 BPM; 1700 Hz; decay 60; wood .40; temperature .30; persistence .35
Euclidean E(5,8) RotationsEight cyclic rotations of E(5,8)=10110110; 2 steps/beat110 BPM; 1800 Hz; decay 65; wood .50; temperature .16; persistence .30

Clave / Timeline Geometry

This is an analytical binary-timeline family, not a claim of generating an “authentic Latin clave.” The first state is the Son-clave representation 1001001000101000; the remaining states provide related timeline patterns and rotations for the Markov rhythm-space comparison.

Euclidean E(5,8)

The Euclidean preset uses the rhythm E(5,8) = 10110110 and its cyclic rotations as the eight Markov states. The implementation treats rotations as distinct chain states without claiming that every rotation has the same metrical or cultural function.

Output and level

Overlap compensation

The script estimates total pulse-duration load across every canon voice:

overlapLoad =
    totalRenderedPulses × pulseDuration
    / totalOutputDuration

overlapGain =
    1 / sqrt(max(1, overlapLoad))

This reduces dense overlapping realizations but does not normalize them to a fixed peak or RMS.

PropertyBehavior
InputNo selected Sound is required.
DurationRequested base Duration, plus explicit canon delays when canon is enabled.
Sample rateExactly the selected 8–192 kHz rate.
ChannelsMono without canon; stereo for 2- and 3-voice canon.
Pulse limitMaximum 12000 rendered pulses across all canon voices; larger realizations stop with an explicit message.
Peak protectionIf enabled and the mixed peak exceeds .92, the complete Sound is scaled down once to .92.
NormalizationNo upward normalization. If the peak is already ≤ .92, its generated level is preserved.
Object nameMarkovRhythm_<preset name>, with spaces replaced by underscores.

Visualization and QC

PanelWhat it shows
A — Actual Rhythm NecklacesThe initial, most-visited, and final realized Markov states drawn as circular binary necklaces.
B — Markov Model / RealizationLeft: expected 8×8 transition matrix. Right: the actual state timeline across the base Duration.
C — Actual Onset RasterThe complete base pulse sequence and its delayed copies for each canon voice.
D — Sound Model → MeasurementMeasured spectrogram of a representative output channel with guides for the fundamental and 2.76× woody resonance of each canon register.

For stereo canon output, Panel D analyzes whichever complete output channel has the higher RMS.

The QC block reports cycle count, theoretical versus realized persistence, mean realized Hamming jump, base/canon pulse counts, canon delay, overlap load, swing amount, effective base frequency, Nyquist scaling, pre-protection peak/RMS, protection status, and seed.

Further reading

These references are directly relevant to the binary-rhythm geometry and Euclidean-rhythm ideas used by the rhythm families: