From 896855648c3371ede3c672bbfa75175fa3a1e42e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:09:10 -0300 Subject: sk.sfxr: the sfxr sound-effect generator, from 02-dont-rat-me-out 02-dont-rat-me-out's synth.zig, a pure and deterministic port of DrPetter's sfxr, moves into the library as src/sketch/sfxr.zig, exposed as sk.sfxr: render(params, rate, out) gives a coin, blip, laser or explosion as samples for host.newSound. Its knobs count in samples at 44.1 kHz, so the one change is the rate: every length (envelope, arpeggio, repeat, phaser offset, period) grows by rate/44.1 kHz and every per-sample step (slides, duty sweep, vibrato, filter coefficients and their ramps) shrinks by it, to first order for the slides and filters; at 44.1 kHz each factor is 1 and the samples are the original's to the bit. Two spellings change without changing a sample: the phaser buffer is @splat (Zig 0.17 has no ** on arrays) and the phaser delay is clamped before it is converted, so a long sweep cannot overflow it. Test: four parameter sets that between them turn every knob render the lengths and Wyhash of the samples the original rendered at 44.1 kHz (run once, outside the repo), and at 48 kHz a sound as long within 1 %. test/voices.zig makes its blip with sfxr in draw, at in.sample_rate, and is the README's example: a blip in six lines. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'README.md') diff --git a/README.md b/README.md index f9ad615..6eb24d0 100644 --- a/README.md +++ b/README.md @@ -441,6 +441,25 @@ full-scale peak plays at 0.875 and any number of loud voices stays bounded. Streams are not limited: SDL adds each, as pushed, to the voice mix and clips the total at ±1. +A sketch can make its own sounds: `host.newSound(samples)` copies mono +samples at `input.sample_rate` into the host and returns an `Audio` that +voices play like a loaded one, so a synthesised one-shot is replay-safe +too. Like every sound it lives as long as the host: make each once and +keep the handle in Session. `sk.sfxr` is sfxr, the classic generator of +game blips, coins, lasers and explosions (`sfxr.Params`, its usual knobs, +rendered at any rate; at 44.1 kHz to the bit as the original): + +```zig +if (ss.blip == .none) { + var buf: [4096]f32 = undefined; + const n = sk.sfxr.render(.{ .freq = 0.5, .sustain = 0.05, .decay = 0.1 }, f.in.sample_rate, &buf); + ss.blip = f.host.newSound(buf[0..n]); +} +if (s.coins != 0) f.voice(.{ .key = 1, .audio = ss.blip, .serial = s.coins }); +``` + +(`test/voices.zig` is this sketch.) + Sounds decode with single-file C libraries in `vendor/` (dr_wav, dr_flac, stb_vorbis): WAV, FLAC and Ogg Vorbis, chosen by content. -- cgit v1.3