LZ-Inspired Audio Variations — User Guide
Lempel–Ziv-based audio variation: overlapping audio windows are quantized into a symbol sequence, parsed into a variable-length LZ78 phrase dictionary, and recombined as contiguous source runs for transformation and resynthesis.
What this does
This script converts a continuous audio recording into a symbolic sequence and applies LZ78 parsing to derive a dictionary of variable-length phrases. Each phrase maps back to a contiguous region of the source, allowing the symbolic structure to guide audio recombination.
The process is:
- Convert the selected Sound to mono when necessary.
- Segment it into overlapping windows on a whole-sample grid.
- Extract two acoustic features from every window.
- Min-max normalize those two features and vector-quantize them by k-means into discrete symbols.
- Parse the symbol string left-to-right with LZ78.
- Generate a new sequence of contiguous source runs using one of three LZ-derived generation modes.
- Apply one optional variation process to each generated run.
- Join run boundaries using the analysis overlap, then trim or pad to the exact requested duration.
Key Features:
- Symbolization — 2D acoustic feature vectors become a discrete alphabet through k-means++ / Lloyd vector quantization.
- LZ78 parsing — variable-length, prefix-closed phrases stored in a trie.
- Contiguous phrase rendering — an LZ phrase spanning several windows is extracted as one continuous source segment.
- Three generation modes — Parse and substitute, Incremental parsing, LZ77 copy and deviate.
- Single Novelty control — mode-dependent probability controlling departure from source-derived continuation.
- LZ diagnostics — phrase count, normalized complexity, symbol entropy, phrase-length statistics, and a rough coded-size estimate.
- Whole-sample window/hop quantization — prevents accumulated sub-sample extraction drift.
- Seven variation choices — six transformations plus None.
- Exact-duration output with one final pair of edge fades.
- Random_seed for reproducible quantization, generation, and variation choices.
- Parse-centered visualization — symbol stream, phrase boundaries, LZ growth, dictionary profile, generated token map, waveform.
Quick start
- Select exactly one Sound object.
- Run
LZ-Inspired_Audio_Variations.praat. - Choose a preset or Custom.
- Choose the acoustic analysis: Pitch, Spectrum, or Intensity.
- Set Window_size_s and Overlap.
- Choose an Alphabet_size.
- Choose one generation mode:
- Parse and substitute (LZ78 re-decode)
- Incremental parsing (LZ78 continuation)
- LZ77 copy and deviate
- Set Novelty, Max_copy_windows, one Variation_method, Variation_amount, Output_duration_s, and optional Random_seed.
- Click OK. Output is named
<source>_LZ_<preset>.
Symbolization
Whole-Sample Analysis Grid
The script requires at least four complete analysis windows. Window and hop are quantized to integer samples so repeated extraction boundaries do not alternate between fractional-sample phases.
Two Features per Window
| Analysis | Feature 1 | Feature 2 |
|---|---|---|
| Pitch | Mean F0 (75–600 Hz Pitch analysis) | F0 standard deviation |
| Spectrum | Spectral centre of gravity | Spectral standard deviation |
| Intensity | Mean intensity using Praat “energy” averaging | Maximum intensity |
Feature 1 determines whether a window is considered defined. In Pitch mode, an unvoiced window normally has undefined mean F0 and therefore receives the dedicated undefined symbol described below. If feature 1 is defined but feature 2 is undefined, the normalized second coordinate is set to 0.5.
2D Min-Max Normalization
Vector Quantization
Renumbering gives the symbol labels an interpretable order along the primary feature. If undefined windows exist, they receive one additional symbol after the k-means codewords. Consequently, the actual alphabet can contain one more symbol than the requested Alphabet_size.
Symbol Entropy
This describes the marginal symbol distribution only; it does not measure sequential phrase structure. LZ complexity addresses that separately.
LZ78 Parse & Measures
Incremental LZ78 Parse
Because every phrase corresponds to a consecutive span in the symbol stream, its first occurrence maps directly to one contiguous stretch of source audio. If the stream ends while traversing an already existing phrase, the remaining suffix is emitted as the final token without adding another node.
LZ Measures Reported
| Measure | Current implementation |
|---|---|
| LZ complexity c(n) | Number of parsed LZ78 phrase tokens |
| Normalized complexity | c(n) × log_A(n) / n |
| Mean phrase length | Mean number of source windows per parsed token |
| Longest phrase | Maximum phrase length in windows |
| Symbol entropy | Shannon entropy of the quantized symbol stream |
| Coded-size estimate | c(n) × [log2(max(2,c(n))) + log2(A)] compared with n × log2(A) |
Generation Modes
All three modes ultimately emit the same renderable object: a run represented by a source-window start and a length in windows. A run is always a contiguous stretch of the source and therefore has no internal splice boundaries.
1. Parse and Substitute — LZ78 Re-decode
Substitution preserves phrase depth, so it preserves the number of windows advanced by that token. Actual substitution rate can be lower than Novelty when no alternative phrase of the same depth exists or no different node is found within the retry limit.
2. Incremental Parsing — Variable-Order LZ78 Continuation
The mean context order actually used is reported. This is what makes the mode variable-order rather than a simple carried-node walk that repeatedly dies at trie leaves.
3. LZ77 Copy and Deviate
This is best understood as an LZ77-style generative context matcher, not as a second literal compression pass. The code searches a source continuation predicted by a matching recent output context; it does not write an encoded bitstream or persist explicit offset/length/literal triples as a compressed file.
Novelty by Mode
| Mode | Novelty means |
|---|---|
| Parse and substitute | Probability of attempting to replace the current phrase by another dictionary phrase of the same depth |
| Incremental parsing | Probability of forcing the predictor to root context before choosing the next symbol |
| LZ77 copy and deviate | Probability of appending a literal after the copied run |
Max_copy_windows by Mode
- Parse and substitute: not used for phrase length; LZ78 phrase depth determines the run length.
- Incremental parsing: caps how many source-consecutive predicted windows are merged into one rendered run.
- LZ77 copy and deviate: upper bound for the randomly chosen copied run length.
Parameters & Presets
| Parameter | Default | Behavior |
|---|---|---|
| Preset | Custom | Custom + five musical presets + Faithful Re-decode test |
| Analysis_type | Pitch | Pitch / Spectrum / Intensity |
| Window_size_s | 0.1 | Minimum 10 ms, then quantized to whole samples |
| Overlap | 0.5 | Clamped 0–0.95, then effective value follows sample-quantized hop |
| Alphabet_size | 12 | Requested k-means codewords, clamped 2–64; actual alphabet may include one extra undefined symbol |
| Generation_mode | Incremental parsing | Parse/substitute / Incremental parsing / LZ77 copy+deviate |
| Novelty | 0.35 | Clamped 0–1; mode-specific departure probability |
| Max_copy_windows | 8 | Minimum 1; mode-dependent run-length limit |
| Variation_method | Pitch shift | Pitch shift / Time stretch / AM / Spectral lowpass / Reverse / Granular shuffle / None |
| Variation_amount | 0.5 | Clamped 0–1 |
| Output_duration_s | 10 | Non-positive value becomes 1 s |
| Random_seed | 0 | 0 = unpredictable; positive = reproducible |
| Draw_visualization | On | Draw v0.8 parse-centered visualization |
| Play_result | On | Play final result |
Built-in Presets
| Preset | Analysis | Window / overlap | Alphabet | Generation | Novelty | Max copy | Variation |
|---|---|---|---|---|---|---|---|
| Subtle Texture | Pitch | 150 ms / 60% | 10 | Incremental parsing | .25 | 8 | Pitch shift .3 |
| Rhythmic Shuffle | Intensity | 80 ms / 40% | 8 | LZ77 copy+deviate | .45 | 6 | Granular shuffle .6 |
| Spectral Morph | Spectrum | 120 ms / 50% | 14 | Parse+substitute | .50 | 8 | Spectral lowpass .5 |
| Glitch Variations | Spectrum | 50 ms / 30% | 16 | LZ77 copy+deviate | .80 | 4 | Reverse .8 |
| Ambient Drift | Pitch | 200 ms / 70% | 8 | Incremental parsing | .15 | 12 | Time stretch .4 |
| Faithful Re-decode | Spectrum | 50 ms / 50% | 20 | Parse+substitute | 0 | 8 | None / 0 |
Variation Methods
Variation is applied to the entire generated run, not separately to every analysis window inside it. Because an LZ phrase can span several windows, this preserves continuity inside the phrase.
1. Pitch Shift
Segments shorter than 3 / 75 = 40 ms bypass the PSOLA transformation and are counted in the Info report.
2. Time Stretch
The same 40 ms PSOLA guard applies. Time stretch is the only shipped variation that intentionally changes run duration, so the generator creates extra material before the final trim.
3. Amplitude Modulation
4. Spectral Lowpass
The Spectrum-to-Sound result is trimmed back to the original run duration.
5. Reverse
6. Granular Shuffle
The trailing partial grain is retained. This is local resampling, not necessarily a permutation: source grains may repeat and others may be omitted.
7. None
The generated contiguous run passes through unchanged. This is useful when the musical effect of the symbolic/LZ process itself should be heard without an additional transformation layer.
Rendering
One Run = One Contiguous Extract
There are no joins between the windows inside a phrase/run. Crossfades occur only between generated run boundaries.
Overlap-Derived Join
This aligns phrase-level audio advance with the underlying symbol/window stream. It is the key difference from the old fixed 2 ms phrase join.
Exact Output Duration
Stereo or multichannel input is converted to mono before feature extraction and rendering, so the output is mono.
Visualization
The visualization exposes the symbolic parse, phrase structure, generated source mapping, and rendered result.
Applications
LZ Phrase Recomposition
Parse and substitute preserves the source's phrase-length grammar while replacing some phrases with other dictionary entries of the same depth.
Variable-Order Machine Improvisation
Incremental parsing uses recent generated symbols as context, backs off to shorter contexts as necessary, and samples continuations from LZ78 trie traversal statistics.
Context-Matched Copying and Deviation
LZ77 copy and deviate finds source locations whose preceding symbol context resembles the recent generated output, then copies forward and optionally injects literal deviations.
Structure / Complexity Analysis
The visualization and Info report can be used to compare symbol entropy, phrase counts, phrase growth, phrase-length distributions, and the normalized LZ complexity of different sounds or parameterizations. These values depend on the chosen analysis, window/hop, and alphabet size; they are not source-invariant perceptual complexity scores.
Hear the Parse Without Effects
Select Variation = None to separate the symbolic recomposition mechanism from pitch, time, filtering, reversal, or granular processing.
Troubleshooting
Use Spectrum or Intensity analysis. Undefined Pitch windows do receive a valid dedicated symbol, but the script aborts when too few defined windows remain to establish a meaningful 2D codebook.
A very large alphabet can suppress recurrence. Reduce Alphabet_size, increase overlap, or use a coarser analysis/window configuration.
Set Output_duration_s manually to the source duration. The preset intentionally changes the LZ/variation controls but does not override output duration.
Runs shorter than 40 ms bypass the PSOLA stage to avoid Praat's minimum-duration failure.
Expected. Time stretch changes individual run duration. The script over-generates material, may cap the join overlap if a transformed segment becomes too short, and finally trims/pads to the exact requested duration.