Random Walk Melody — User Guide

A bounded discrete random walk on scale degrees. Each note transition makes a Bernoulli hold-or-move decision; a move then chooses uniformly among the legal non-zero signed steps available at the current degree. The resulting degree sequence is mapped to a selected tuning/scale and rendered as contiguous Hann-windowed sine notes.

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

Scope

This generator uses a finite-state random walk whose state is the current scale degree. The walk is stochastic, but the available pitch material is explicitly bounded by the selected scale and optional degree limit.

Process:
Bernoulli hold/move decision → legal signed degree step → bounded degree state → degree-to-frequency map → phase-reset sine × Hann window → mono sum

The model is intentionally simple and inspectable. It does not learn melodic grammar, infer harmony, use Markov transition matrices between individual pitch classes, or vary note duration dynamically.

Quick start

  1. Run Random_Walk_Melody.praat. No input Sound is required.
  2. Choose Custom or one of the six presets.
  3. For Custom, set Duration, Note duration, Base frequency, Scale, Step probability, and Max step size.
  4. Enable Edit details to change Sample rate, the optional Scale degrees override, Output peak, and Random seed.
  5. Run the script. The degree walk is generated first, then mapped to frequency and rendered as a mono Sound.

Random-walk rule

The walk starts at the middle available degree:

initial degree = ceil(numberOfDegrees / 2)

For every transition from note n to note n+1, the script first draws one Bernoulli decision:

probability of move = StepProbability
probability of hold = 1 - StepProbability

If the decision is a hold, the current degree is repeated.

If the decision is a move, the script constructs the set of all legal non-zero signed offsets up to Max step size. One of those offsets is then chosen uniformly.

For example, at an interior degree with Max step size = 2, the legal choices are:

-2, -1, +1, +2

Each has equal probability conditional on a move.

Because zero is not part of the move-choice set, Step probability is the actual probability of changing degree, apart from the trivial one-note case where there is no transition.

Boundary behavior

The walk never generates an illegal degree and then clamps it back into the scale. Instead, illegal offsets are removed before the random step is chosen.

At the lowest degree, for example, downward steps are unavailable; at the highest degree, upward steps are unavailable. Near an edge, the set of permitted magnitudes is reduced accordingly.

leftSteps = min(MaxStep, currentDegree - 1)
rightSteps = min(MaxStep, numberOfDegrees - currentDegree)

This avoids artificial edge residence caused by clamp-based implementations where several illegal draws can collapse onto the same boundary pitch.

The boundary still changes the transition distribution naturally because only inward legal moves remain available there.

Scale and tuning mapping

After the degree walk is generated, each degree is mapped to one fixed frequency ratio above Base frequency.

ScaleDegreesRatios / intervals
Just Intonation Major (5-limit)71, 9/8, 5/4, 4/3, 3/2, 5/3, 15/8.
Equal Temperament Major (12-TET)70, 2, 4, 5, 7, 9, 11 semitones.
Major Pentatonic (5-limit)51, 9/8, 5/4, 3/2, 5/3.
Whole Tone (12-TET)60, 2, 4, 6, 8, 10 semitones.
Chromatic (12-TET)120 through 11 semitones.

The mapping is:

frequency[degree] = BaseFrequency × scaleRatio[degree]

Scale degrees override

The Details-page override can restrict the walk to the first N degrees of the selected scale. A value of 0 uses the complete scale; values of 2 or more are accepted and capped at the scale's native degree count.

The override does not retune or redistribute the scale. It simply truncates the available degree table from the top.

Timing and note kernel

Notes occur on a fixed-duration sequence:

note onset[n] = (n - 1) × NoteDuration

The number of notes is:

totalNotes = ceil(Duration / NoteDuration)

This ensures that the requested Duration is filled. If Duration is not an exact multiple of Note duration, the final note is shortened to the remaining time.

There are no rests and no intentional overlap between notes: each regular onset is one Note duration after the previous onset. A held random-walk state means the pitch repeats, not that the oscillator is sustained continuously across the boundary.

Note synthesis

Each note is a sine oscillator whose phase restarts at zero at that note's onset:

g(τ) = 0.7 × sin(2πfτ) × [1 - cos(2πτ/D)] / 2 0 ≤ τ < D

The raised-cosine/Hann envelope reaches zero at both complete note boundaries. Consequently, even repeated degrees are rendered as separate articulated notes rather than as one phase-continuous tone.

The final partial note uses its shortened realized duration as the Hann-window duration, so its endpoint also approaches zero at the requested Sound boundary.

For implementation stability, note terms are accumulated into the output in chunks of 15. Chunking does not alter the generated melody.

Controls

Main page

ControlDefaultBehavior
PresetCustomCustom plus six complete melodic configurations.
Duration15 sTotal output duration.
Note duration.5 sFixed onset spacing and nominal note duration.
Base frequency220 HzFrequency of scale degree 1.
ScaleJI MajorSelects the degree-to-frequency table.
Step probability.7Probability that a transition changes degree; valid range 0–1.
Max step size2Largest allowed degree displacement before boundary restrictions.
Edit detailsoffOpens sample rate, degree override, output peak, and seed controls.

Details page

ControlDefaultBehavior
Sample rate44100 HzDirect synthesis and final output sample rate; minimum 1000 Hz.
Scale degrees override00 = full scale; 2+ = use only the first N available degrees.
Output peak.90Final target peak normalization; valid range >0…1.
Random seed00 = unpredictable walk; positive = reproducible degree sequence.

Presets

PresetDuration / noteBaseMove / max stepScale
Slow Wandering20 s / .8 s180 Hz.5 / 1JI Major
Quick Steps12 s / .2 s330 Hz.9 / 1JI Major
Wide Leaps15 s / .4 s220 Hz.8 / 312-TET Major
Sticky Notes15 s / .6 s196 Hz.3 / 1JI Major
Pentatonic Float18 s / .5 s261.63 Hz.7 / 2Major Pentatonic
Chromatic Drift10 s / .25 s440 Hz.85 / 112-TET Chromatic

Presets override Duration, Note duration, Base frequency, Step probability, Max step size, and Scale. They do not override Sample rate, Scale degrees override, Output peak, or Random seed from the Details page.

Sampling and frequency safety

The complete active scale table is materialized before synthesis. The highest available pitch must remain below:

0.95 × Nyquist

If it does not, the script stops rather than rescaling the scale.

The Note duration must also span at least eight samples at the selected sample rate.

No resampling, oversampling, pitch shifting, or anti-aliasing stage is applied after the scale frequencies are chosen.

Randomness and reproducibility

Only the degree transitions are stochastic. Note timing, synthesis waveform, scale mapping, and level operation are deterministic.

With Random seed = 0, the walk uses Praat's unpredictable random state. With a positive seed, the same settings generate the same degree sequence.

After a fixed-seed walk is generated, the script restores Praat's global RNG to an unpredictable state so that later scripts are not left in deterministic mode.

Output and level

PropertyBehavior
InputNo input Sound required.
ChannelsMono.
DurationExactly the requested Duration.
Sample rateExactly the Details-page Sample rate.
Rests / overlapNone intentionally; notes occupy consecutive fixed-duration slots, with only the last slot shortened when necessary.
Global fadeNone. Each note already has its own Hann envelope.
NormalizationEvery non-silent result is peak-scaled once to Output peak. This is target normalization, not an attenuate-only ceiling.
Object namerandom_walk_<preset name>.

Visualization and QC

PanelWhat it shows
A — Random decisionsEvery transition as a hold at 0 or an applied signed degree step. Positive and negative moves are visually distinguished.
B — State trajectoryThe realized bounded degree sequence through time, explicitly showing that the walk remains inside degrees 1…N without clamp-induced edge holds.
C — Scale mapping / note kernelLeft: the actual degree-to-frequency table. Right: the analytical Hann note envelope plus the local oscillator formula and phase-reset interpretation.
D — Measured outputThe final mono waveform after target peak normalization. It is output verification rather than the explanation of the stochastic mechanism.

The QC summary reports scale, note count, realized degree and frequency ranges, target/realized move probability, mean absolute move size, counts of upward/downward/hold transitions, Note duration, effective Max step, Nyquist frequency, and measured final peak/RMS.