Latent STFT Decoder β User Guide
Trains a convolutional Beta-VAE on log-magnitude STFT patches extracted from event-segmented audio, then navigates the latent space to synthesise new audio via decoded STFT patches. Waveform reconstruction via phase borrowing or Griffin-Lim.
What this does
This script implements a Latent STFT Decoder β a system that trains a convolutional Beta-VAE on log-magnitude STFT patches extracted from event-segmented audio, then navigates the latent space to synthesise new audio via decoded STFT patches. Waveform reconstruction is achieved via phase borrowing from nearest real events or iterative Griffin-Lim phase reconstruction.
ποΈ What is STFT VAE Decoding?
This approach combines several advanced techniques:
- Event segmentation: Audio is divided into events (200msβ3s)
- STFT patches: For each event, extract log-magnitude STFT patches (F Γ T)
- Downsampled VAE: Patches are block-averaged to a smaller grid (vae_freq Γ vae_frames) for fast training
- Beta-VAE: Learns a latent space with adjustable KL weight (Ξ²) for disentanglement
- Latent navigation: Three modes: interpolate (cyclic through events), random_walk, drift (coherent with momentum)
- Waveform reconstruction: Phase borrowed from real events or Griffin-Lim iteration
- Visit penalty: Prevents repetition by inflating distances for recently used events
Key Features:
- 3 Preset Strategies β Quick, Standard, High Quality, plus Custom
- Beta-VAE Architecture β Adjustable KL weight for disentangled representations
- Downsampled VAE Input β Speed optimization via block averaging (vae_freq Γ vae_frames)
- 3 Navigation Modes β interpolate (cyclic), random_walk, drift (coherent)
- 2 Phase Modes β borrow (phase from nearest event) or griffinlim (iterative)
- Visit Penalty System β Prevents repetition with decaying visit scores
- Teleport Mechanism β Random jumps to avoid getting stuck
- Comprehensive Visualization β 5-panel display with waveforms, spectrograms, config panel, summary
Technical Implementation: (1) Event Segmentation: Intensity-based detection. (2) STFT Patches: log-magnitude STFT [F Γ patch_frames] per event. (3) Downsampling: Block average to vae_freq Γ vae_frames for VAE input. (4) Beta-VAE: Train on downsampled patches with adjustable Ξ². (5) Latent Navigation: Generate trajectory through latent space. (6) Decode: Upsample decoded patches, reconstruct waveform via phase borrow or Griffin-Lim. (7) Assemble: Crossfade segments, normalize, stereo Haas delay.
Quick start
- In Praat, select exactly one Sound object (any duration, any content).
- Run scriptβ¦ β select
LatentSTFTDecoder.praat. - Choose Preset (2-4 for specific strategies, 1 for custom).
- Set latent size, duration, seed.
- Configure VAE training parameters (beta, epochs, batch size).
- Set STFT parameters (n_fft, hop_length, patch_frames).
- Set VAE grid size (vae_freq, vae_frames) β smaller = faster.
- Choose navigation mode and parameters (steps, K, step size, temperature, etc.).
- Select phase mode, normalization mode.
- Enable Draw_visualization for analysis display.
- Click OK β engine segments, extracts STFT patches, trains VAE, navigates, reconstructs.
STFT VAE Theory
STFT Patch Extraction
Downsampling for VAE
π Block Averaging for Speed
To keep VAE weights small and training fast, patches are block-averaged:
This reduces a typical 257Γ32 patch (8224 dimensions) to 32Γ16 (512 dimensions) β 16Γ smaller, enabling fast training.
Beta-VAE Architecture
Latent Navigation Modes
Visit Penalty System
Phase Reconstruction Modes
Preset Strategies
Preset 2: Quick (small, fast)
β‘ Fastest Option
Latent: 6 | Epochs: 15 | n_fft: 256 | Hop: 64
Patch frames: 16 | VAE grid: 16Γ8 | Nav steps: 20
Character: Small VAE, minimal settings β fastest processing, lower quality
Use on: Quick previews, experimentation
Preset 3: Standard
βοΈ Balanced
Latent: 8 | Epochs: 30 | n_fft: 512 | Hop: 128
Patch frames: 32 | VAE grid: 32Γ16 | Nav steps: 30
Character: Balanced speed/quality β good for most uses
Use on: General purpose, exploration
Preset 4: High Quality
β¨ Maximum Quality
Latent: 16 | Epochs: 60 | n_fft: 1024 | Hop: 256
Patch frames: 64 | VAE grid: 48Γ24 | Nav steps: 50
Character: High-resolution STFT, larger VAE, more epochs β best quality, slower
Use on: Final renderings, high-fidelity material
Parameters & Controls
Core Parameters
| Parameter | Default | Description |
|---|---|---|
| Latent_size | 8 | VAE latent dimensions (2β64) |
| Duration (0 = original) | 0 | Target output duration (seconds) |
| Seed | 42 | Random seed for reproducibility |
VAE Training Parameters
| Parameter | Default | Description |
|---|---|---|
| Beta (KL_weight) | 0.5 | Weight for KL loss (higher = more disentangled) |
| Epochs | 30 | Training epochs (5β500) |
| Batch_size | 8 | Batch size for training (1β64) |
STFT Parameters
| Parameter | Default | Description |
|---|---|---|
| N_fft | 512 | FFT size (64β2048) |
| Hop_length | 128 | Hop length (1βn_fft) |
| Patch_frames | 32 | Fixed time frames per patch (4β256) |
VAE Grid Size (key speed control)
| Parameter | Default | Description |
|---|---|---|
| Vae_freq | 32 | Frequency bins fed to VAE after downsampling |
| Vae_frames | 16 | Time frames fed to VAE after downsampling |
Latent Navigation Parameters
| Parameter | Default | Description |
|---|---|---|
| Nav_mode | interpolate | interpolate, random_walk, drift |
| Nav_steps | 30 | Number of navigation steps (4β1000) |
| K_neighbors | 4 | K nearest events for barycentric mixing (1β10) |
| Step_size | 0.30 | Movement step size (0β5) |
| Temperature | 0.25 | Noise level for exploration (0β5) |
| P_jump (teleport_prob) | 0.05 | Probability of teleport to random event (0β1) |
| Visit_weight | 2.0 | Strength of visit penalty (higher = less repetition) |
| Visit_decay | 0.92 | Decay factor for visit scores per step |
Output Parameters
| Parameter | Default | Description |
|---|---|---|
| Normalize_mode | rms | none, peak, rms |
| Phase_mode | borrow | borrow (phase from real events) or griffinlim |
Output
| Parameter | Default | Description |
|---|---|---|
| Draw_visualization | 1 | Generate 5-panel analysis display |
| Play_result | 1 | Audition after processing |
Visualization & Analysis
5-Panel Display
Reading the Config Panel
- STFT: n_fft (frequency resolution), hop (time resolution), patch_frames (event length), freq_bins (FFT/2+1)
- VAE: latent size, beta (KL weight), epochs trained, loss reduction %
- Nav: navigation mode, steps taken, phase mode (borrow/griffinlim)
- Events: number of events, original duration β output duration, normalize mode
Comparing Spectrograms
- Original: Detailed spectral structure from source
- Output: Reconstructed from VAE-decoded patches β should preserve broad spectral shape
- Phase mode: "borrow" may sound more realistic (phase from real events); "griffinlim" may have artifacts but more flexibility
Applications
Electroacoustic Composition
Use case: Creating spectral morphs and variations from source material
Technique: Standard preset with interpolate navigation
Workflow:
- Select a 20-60 second recording with varied spectral content
- Run with Standard preset, nav_mode=interpolate
- Listen to smooth morphs through all events
- Export and use as movement in larger work
Sound Design for Media
Use case: Creating evolving textures, spectral transformations
Technique: Quick preset for preview, High Quality for final
Applications:
- Ambient: drift mode with low temperature
- Transitions: random_walk with high temperature
- Atmospheres: interpolate through all events
Music Production
Use case: Creating spectral variations, morphing between sounds
Technique: Different navigation modes on same source
Examples:
- Pad evolution: drift mode with momentum
- Glitch textures: random_walk with high teleport probability
- Cyclic variations: interpolate with low step size
Research & Education
Use case: Studying VAE latent spaces, spectral reconstruction, phase reconstruction
Technique: Compare phase modes on same source
Learning outcomes:
- Understand how VAE learns compressed spectral representations
- See effect of Ξ² on latent space organization
- Compare phase borrowing vs Griffin-Lim
- Explore visit penalty effects on repetition
Practical Workflow Examples
π¬ Film Scene: Spectral Morph
Goal: Create 60-second spectral morph from 30-second ambient recording
Settings:
- Source: 30-second ambient
- Preset: High Quality
- Nav: interpolate, steps=60
- Phase: borrow (realistic)
Result: Smooth spectral evolution through all events β continuous ambient morph
ποΈ Electronic Music: Random Texture
Goal: Create unpredictable texture from synth stab
Settings:
- Source: 8-second synth stab
- Preset: Quick
- Nav: random_walk, temp=1.0, p_jump=0.2
Result: Unpredictable spectral variations β glitchy texture
ποΈ Voice Processing: Spectral Exploration
Goal: Explore spectral space of vocal recording
Settings:
- Source: 10-second vocal phrase
- Preset: Standard
- Nav: drift, step_size=0.4, temp=0.3
Result: Coherent drift through vocal spectral space β explores variations
Troubleshooting Common Issues
Cause: Python not installed, or packages missing
Solution: Install Python and required packages: pip install numpy scipy soundfile
Cause: Too few epochs, too small latent, or data too complex
Solution: Increase epochs, increase latent_size, or use smaller vae_grid
Cause: Crossfade insufficient at segment boundaries
Solution: Increase xfade_sec in assemble_output() (currently 10ms)
Cause: Visit penalty too low, or teleport probability too low
Solution: Increase visit_weight, increase p_jump, reduce visit_decay
Cause: Too few iterations, or inconsistent magnitude
Solution: Increase gl_n_iter, or use borrow mode
Advanced Techniques
In NumpySTFTVAE class, modify hidden layer size calculation or add more layers for deeper networks.
In _block_avg(), change from mean to max or median for different downsampling characteristics.
Adjust visit_weight and visit_decay to control repetition vs. variety. Higher visit_weight = more variety, but may lose coherence.
Script extracts mono for analysis; output is stereo with Haas delay (0.3ms). For true stereo processing, modify to process each channel separately.