Procedural thunder: far rolls made of booms (a third of them carrying a
faint, far crackle), claps drawn from statistics of recorded ones (about 800
numbers in src/clap_data.rs), and optional rain to play beside the rolls.
Every sound is synthesised from scratch; no recording or envelope is
included. Every seed gives another sound, and the same seed always the same
one. Zero dependencies, no unsafe in the library.
Try it in the browser: each click renders a new roll, clap or storm there, with or without rain and crackles (WebAssembly: a roll or clap in 40 to 90 ms, a storm's final clap about 110 ms, as the browser renders the roll and clap one after the other).
The demo is examples/web; to run it locally:
cargo build --release --target wasm32-unknown-unknown --example web
cp examples/web/index.html target/wasm32-unknown-unknown/release/examples/
python3 -m http.server -d target/wasm32-unknown-unknown/release/examplesuse thunder_synth::{Thunder, CLAP_SECS, ROLL_OVERLAP, SAMPLE_RATE};
let (seed, roll_seed) = (42, 7);
let roll: Vec<u8> = thunder_synth::wav(Thunder::Roll, seed); // a WAV file's bytes
let clap: Vec<u8> = thunder_synth::wav(Thunder::Clap, seed);
assert_eq!(SAMPLE_RATE, 22_050); // 16-bit mono
let secs: f32 = thunder_synth::secs(Thunder::Roll); // 2.99 s, every seed
assert_eq!(thunder_synth::secs(Thunder::Clap), CLAP_SECS); // 3.0 s
// a storm: start a roll every secs - ROLL_OVERLAP seconds while it lasts;
// at its end the clap takes over the roll still playing (its seed, seconds
// in): stop that roll and play this, which holds the roll's rest
let last: Vec<u8> = thunder_synth::clap_over(seed, Some((roll_seed, 1.7)));
// optional rain, never inside a roll: a clip beside every roll, started
// with it, the last one left to run out under the clap
let rain: Vec<u8> = thunder_synth::rain(roll_seed);
// whether a roll carries a faint crackle, and where (about one in three)
let crackle: Option<f32> = thunder_synth::crackle_at(roll_seed);Thunder::Roll: a roll, new booms every seed. It fades in over 0.25 s and out over its last 0.8 s, so rolls startedROLL_OVERLAP(0.5 s) before the last one ends run on without a seam. About one roll in three carries a faint crackle that builds and fades over 2.4 s.Thunder::Clap: a new crack and tail every seed.clap_oversteps the roll it takes over back 8 dB as the clap lands and fades it out under it over 2 s.rain: chained like rolls,RAIN_OVERLAPs apart (their fades sum to a steady level).- Levels are baked in: rolls levelled to -21 dBFS RMS and claps to -20 (peaks held under 0.98), each then scaled to the loudness it was rated at; a soft knee above 0.9 keeps a clap over a roll's rest from clipping.
Apple M5 Pro, release build, median of 15 (cargo run --release --example bench):
| call | time |
|---|---|
wav(Roll, _) |
28 ms (33 ms with a faint crackle) |
wav(Clap, _) |
44 ms |
clap_over(_, Some(_)) |
45 ms (the roll renders on a second thread) |
rain(_) |
8 ms |
Rolls and claps are bands of Gaussian noise, each shaped by an envelope and summed. The numbers were tuned in blind listening tests (1 to 5); quoted phrases in the source are listeners' notes on what a value did wrong.
- Roll: 28 ERB-spaced bands (30 Hz to 10 kHz) under one loudness made of slow booms over a floor. The spectrum peaks at 86 Hz with a growl around 330 Hz; the high bands follow the booms less than the low ones, and every band wobbles a little on its own. The numbers were found by a search scored by an AudioSet classifier, checked by two other models, then picked by ear.
- Clap: 32 ERB-spaced bands (4-pole Butterworth band-passes, the filters the statistics were measured with). Per band the mean decay curve of the recorded claps is drawn anew every seed (stretched, offset within the recorded clap-to-clap spread, roughened by the measured roughness), with a crisp first 70 ms, a few peals riding on the tail, small broadband grains, and a tail whose level wanders irregularly, mostly differently per band.
- Faint crackle: a soft bed and small shock pulses (N-waves in little clusters), tens a second, coming in short bunches, over a level that builds for about a second, holds and trails off; quiet, under the roll.
- Rain: a dense bed of far drops shaped in octaves, with distinct drops landing alone at random (from tiny to the odd splash) and a few bubble plinks; how hard it rains wanders, and the whole bed moves together.
The clap's texture fit gives two corrections per band, fitted so that the
envelope modulation spectrum (octaves 1 to 256 Hz, as in McDermott &
Simoncelli 2011) matches the recordings': beta flattens the noise's own
envelope, and sigma adds fast, 150 Hz low-passed log-amplitude modulation
(dB), the clap's grit.
examples/make_clap_data measures the table from your own recordings of
thunder claps, two or more:
cargo run --release --example make_clap_data -- src/clap_data.rs a.wav b.wav c.wav
cargo fmtEach WAV (16 or 24 bit PCM, any rate, mono or stereo; a windowed-sinc
resampler takes it to 22.05 kHz) holds one clap with at least 3 s after
the crack. The onset is found on its own and printed; a.wav@20.82 sets it
by hand. It cuts 3 s from 60 ms before each onset, measures the band RMS
every 1 ms for the first 150 ms and every 5 ms after (floored at each
band's 10th percentile, quantised over 72 dB), fits (beta, sigma) per band
on a 5 × 5 grid, and writes per band the mean curve, the spread, the
roughness and the averaged fit. The fit scores random noise carriers, so a
rerun on the same recordings moves a few numbers; the shipped table is the
one that was rated.