Brightness Sorted Grains — User Guide

Randomly samples short grains from a Sound, measures each grain's spectral centre of gravity, optionally exaggerates spectral differences, and reorders the grains from dark to bright or bright to dark.

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

What this does

Brightness Sorted Grains creates a new mono sound from short excerpts sampled at random positions in the source. Each grain is analyzed in the frequency domain, assigned a spectral-brightness value, and then optionally reordered by that value. The result can form a trajectory from darker to brighter spectra or the reverse.

The script is a sampling and reordering process, not a conventional time-stretcher. Source positions are random, so the original chronology is not preserved even when Sort_grains is off.

Important: the current script does not pitch-shift grains. The control is Gain_scatter_dB: a random gain variation applied after every non-silent grain has first been normalized to the same 0.30 peak target.

What is brightness?

In timbre perception, brightness usually describes how strongly a sound is weighted toward higher frequencies. A common numerical correlate is the spectral centroid, or spectral centre of gravity: a spectrum with more high-frequency energy tends to have a higher centroid.

This script uses Praat's Spectrum: Get centre of gravity... with Power = 2. Praat therefore computes the average frequency weighted by the power spectrum:

centre of gravity = integral(f × |S(f)|² df) / integral(|S(f)|² df)

The value is measured in hertz. It is used here as the ordering variable for the grains. It is a useful correlate of perceived brightness, but it is not a complete model of timbre or brightness perception.

Quick start

  1. Select exactly one Sound object in Praat.
  2. Run Brightness_Sorted_Grains.praat.
  3. Choose a preset, or keep Custom.
  4. Set the base grain size and choose Fixed or Random grain duration.
  5. Use Analysis_overlap and Density_factor to control how many random grains are drawn.
  6. Choose Dark to bright or Bright to dark.
  7. Use Exaggerate_spectral if you want the spectral differences pushed farther apart before sorting.
  8. Set the silence gap between output grains and run the script.
Custom defaults: 150 ms fixed grains, analysis overlap 0.3, density 1.5, gain scatter 1 dB, Triangular window, dark-to-bright sorting, 50 ms gaps, Moderate spectral exaggeration, no random reversal.

Processing pipeline

1. Mono source and random grain sampling

Multichannel input is converted to mono. Each grain is extracted from a uniformly random valid source position. Non-zero Sound time domains are handled correctly: random positions are zero-based offsets internally, then converted back to the Sound's absolute time domain for extraction.

2. Grain count

baseGrain = Grain_size_ms / 1000 nominalHop = baseGrain × (1 - Analysis_overlap) targetGrains = round((sourceDuration / nominalHop) × Density_factor)

The target is clamped to a maximum of 500 grains. Analysis_overlap is not an output overlap or crossfade. It only changes the nominal analysis hop used to determine how many random source excerpts are generated.

3. Grain duration and window

In Fixed mode, every grain uses the base duration. In Random mode, a uniform random variation in ±Grain_size_variation_ms is added, then the duration is constrained to a practical range. If the entered variation exceeds the base grain size, the script first reduces it to 80% of the base size.

The extraction window can be Rectangular, Triangular, or Parabolic. The default is Triangular.

4. Initial brightness measurement

Every extracted grain is converted to a Spectrum and measured with Praat's power-weighted centre of gravity. Silent or undefined measurements are stored as 0 Hz.

5. Source-adaptive spectral exaggeration

If exaggeration is enabled, the script first computes a brightness pivot: the mean of all valid original grain centroids. It then pushes grains away from that source-specific pivot:

if grainCentroid >= pivot: multiply spectrum at frequencies >= grainCentroid by spectralBoost else: multiply spectrum at frequencies <= grainCentroid by spectralBoost Subtle = 1.2× Moderate = 1.5× Strong = 2.0×

After this spectral modification, the grain is converted back to Sound and its centroid is measured again. The measured post-processing value — not an estimated multiplier — becomes the brightness used for sorting.

6. Reverse and gain scatter

When Reverse_grains is enabled, each grain has a 30% probability of being reversed. Reversal does not alter its stored brightness because time reversal does not change the magnitude spectrum.

Every non-silent grain is then peak-normalized to 0.30. After that, Gaussian gain scatter is applied in dB:

gainDB ~ Gaussian(mean = 0, SD = Gain_scatter_dB) gainDB is limited to ±min(12 dB, 3 × Gain_scatter_dB) gain = 10^(gainDB / 20)

This separates brightness from systematic loudness differences while still allowing controlled random level variation.

7. Sorting and assembly

When sorting is enabled, grains are sorted by their measured post-exaggeration centroid. Dark to bright uses ascending centroid; Bright to dark uses descending centroid. When sorting is disabled, the random generation order is retained.

Copies of the grains are created in final order and concatenated in one pass. A mono silence object is inserted between adjacent grains when Gap_between_grains_ms is greater than zero.

Presets

PresetGrainModeAnalysis overlapDensityGain scatterGapExaggerationReverse
Gentle Sweep200 ±30 msFixed0.20.80.5 dB80 msSubtle 1.2×Off
Dense Cloud100 ±40 msRandom0.52.51.5 dB20 msModerate 1.5×30%
Micro Grains50 ±20 msRandom0.43.02.0 dB10 msOffOff
Long Drones400 ±100 msRandom0.60.50.25 dB150 msSubtle 1.2×Off
Extreme Sort120 ±50 msRandom0.32.02.5 dB40 msStrong 2.0×30%

All named presets sort Dark to bright. Visualization and playback switches are not changed by the presets.

Parameters

ParameterDefaultMeaning
PresetCustomCustom or five named parameter sets.
Grain_size_ms150Base grain duration. Must be at least two samples and no longer than the source.
Grain_size_variation_ms50Uniform ± duration variation in Random mode; must be non-negative.
Grain_size_modeFixedFixed or Random grain duration.
Analysis_overlap0.30 ≤ value < 1. Controls nominal analysis hop and therefore target grain count.
Density_factor1.5Positive multiplier for target grain count.
Gain_scatter_dB1.0 dBGaussian per-grain gain variation after 0.30 peak normalization.
Reverse_grainsOffWhen On, each grain has a 30% chance of reversal.
Window_typeTriangularRectangular, Triangular, or Parabolic extraction window.
Sort_grainsOnSort by measured final centroid; Off preserves random generation order.
Sort_directionDark to brightAscending or descending centroid.
Gap_between_grains_ms50Silence inserted between successive output grains.
Exaggerate_spectralOnPushes grain spectra away from the source-adaptive brightness pivot.
Exaggeration_intensityModerateSpectral boost of 1.2×, 1.5×, or 2.0×.
Show_spectrogramsOffShows an output spectrogram instead of the output waveform in the Picture window.
Draw_visualizationOnDraw the transformation map.
Play_resultOnPlay the result after processing.

Visualization

The Picture window follows the transformation itself:

When the Output panel is a waveform, Source and Output use the same amplitude scale.

Output behavior

Normalization: final peak normalization to 0.9 can raise or lower the overall result level. The per-grain gain scatter remains audible as relative level variation inside the cloud.