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.
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.
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
- Run
Random_Walk_Melody.praat. No input Sound is required. - Choose Custom or one of the six presets.
- For Custom, set Duration, Note duration, Base frequency, Scale, Step probability, and Max step size.
- Enable Edit details to change Sample rate, the optional Scale degrees override, Output peak, and Random seed.
- 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:
For every transition from note n to note n+1, the script first draws one Bernoulli decision:
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:
Each has equal probability conditional on a move.
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.
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.
| Scale | Degrees | Ratios / intervals |
|---|---|---|
| Just Intonation Major (5-limit) | 7 | 1, 9/8, 5/4, 4/3, 3/2, 5/3, 15/8. |
| Equal Temperament Major (12-TET) | 7 | 0, 2, 4, 5, 7, 9, 11 semitones. |
| Major Pentatonic (5-limit) | 5 | 1, 9/8, 5/4, 3/2, 5/3. |
| Whole Tone (12-TET) | 6 | 0, 2, 4, 6, 8, 10 semitones. |
| Chromatic (12-TET) | 12 | 0 through 11 semitones. |
The mapping is:
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:
The number of notes is:
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.
Note synthesis
Each note is a sine oscillator whose phase restarts at zero at that note's onset:
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
| Control | Default | Behavior |
|---|---|---|
| Preset | Custom | Custom plus six complete melodic configurations. |
| Duration | 15 s | Total output duration. |
| Note duration | .5 s | Fixed onset spacing and nominal note duration. |
| Base frequency | 220 Hz | Frequency of scale degree 1. |
| Scale | JI Major | Selects the degree-to-frequency table. |
| Step probability | .7 | Probability that a transition changes degree; valid range 0–1. |
| Max step size | 2 | Largest allowed degree displacement before boundary restrictions. |
| Edit details | off | Opens sample rate, degree override, output peak, and seed controls. |
Details page
| Control | Default | Behavior |
|---|---|---|
| Sample rate | 44100 Hz | Direct synthesis and final output sample rate; minimum 1000 Hz. |
| Scale degrees override | 0 | 0 = full scale; 2+ = use only the first N available degrees. |
| Output peak | .90 | Final target peak normalization; valid range >0…1. |
| Random seed | 0 | 0 = unpredictable walk; positive = reproducible degree sequence. |
Presets
| Preset | Duration / note | Base | Move / max step | Scale |
|---|---|---|---|---|
| Slow Wandering | 20 s / .8 s | 180 Hz | .5 / 1 | JI Major |
| Quick Steps | 12 s / .2 s | 330 Hz | .9 / 1 | JI Major |
| Wide Leaps | 15 s / .4 s | 220 Hz | .8 / 3 | 12-TET Major |
| Sticky Notes | 15 s / .6 s | 196 Hz | .3 / 1 | JI Major |
| Pentatonic Float | 18 s / .5 s | 261.63 Hz | .7 / 2 | Major Pentatonic |
| Chromatic Drift | 10 s / .25 s | 440 Hz | .85 / 1 | 12-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:
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
| Property | Behavior |
|---|---|
| Input | No input Sound required. |
| Channels | Mono. |
| Duration | Exactly the requested Duration. |
| Sample rate | Exactly the Details-page Sample rate. |
| Rests / overlap | None intentionally; notes occupy consecutive fixed-duration slots, with only the last slot shortened when necessary. |
| Global fade | None. Each note already has its own Hann envelope. |
| Normalization | Every non-silent result is peak-scaled once to Output peak. This is target normalization, not an attenuate-only ceiling. |
| Object name | random_walk_<preset name>. |
Visualization and QC
| Panel | What it shows |
|---|---|
| A — Random decisions | Every transition as a hold at 0 or an applied signed degree step. Positive and negative moves are visually distinguished. |
| B — State trajectory | The 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 kernel | Left: the actual degree-to-frequency table. Right: the analytical Hann note envelope plus the local oscillator formula and phase-reset interpretation. |
| D — Measured output | The 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.