diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 00:09:10 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 896855648c3371ede3c672bbfa75175fa3a1e42e (patch) | |
| tree | 62dbf603661c970efc0b2547e23fe9c0c2b37e32 /test | |
| parent | cddefdd6e5c7fb632088d504d2ed96368fa44e0f (diff) | |
| download | sketchbook-896855648c3371ede3c672bbfa75175fa3a1e42e.tar.gz sketchbook-896855648c3371ede3c672bbfa75175fa3a1e42e.zip | |
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 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'test')
| -rw-r--r-- | test/voices.script | 4 | ||||
| -rw-r--r-- | test/voices.zig | 24 |
2 files changed, 13 insertions, 15 deletions
diff --git a/test/voices.script b/test/voices.script index 43283f7..4b444d0 100644 --- a/test/voices.script +++ b/test/voices.script @@ -4,7 +4,7 @@ frames 3 expect-no-audio 1 key space -expect serial 1 +expect coins 1 expect-audio 1 frames 10 expect-no-audio 1 @@ -23,7 +23,7 @@ expect-no-audio 1 console stop # A new serial starts it again. key space -expect serial 2 +expect coins 2 expect-audio 1 frames 10 expect-no-audio 1 diff --git a/test/voices.zig b/test/voices.zig index 9932642..141e562 100644 --- a/test/voices.zig +++ b/test/voices.zig @@ -1,28 +1,26 @@ //! A sound the sketch made itself, played as a voice: space bumps the -//! serial that starts it (test/voices.script). +//! serial that starts it (test/voices.script). The README's sfxr blip. const sk = @import("sketch"); pub const name = "voices"; pub const width = 160; pub const height = 120; -pub const State = struct { serial: u32 = 0 }; +pub const State = struct { coins: u32 = 0 }; pub const Session = struct { blip: sk.Audio = .none }; -pub fn setup(ss: *Session, init: *sk.Init) !void { - // Shorter than the host's lead (2048 samples), so the first frame that - // declares it mixes all of it. - var samples: [1000]f32 = undefined; - for (&samples, 0..) |*v, i| v.* = 0.5 * @sin(@as(f32, @floatFromInt(i)) * 0.06); - ss.blip = init.host.newSound(&samples); - if (ss.blip == .none) return error.NewSound; -} - pub fn update(s: *State, in: *const sk.Input) void { - if (in.pressed(sk.Key.space)) s.serial += 1; + if (in.pressed(sk.Key.space)) s.coins += 1; } pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { f.clear(0x202020); - if (s.serial != 0) f.voice(.{ .key = 1, .audio = ss.blip, .serial = s.serial }); + // About 1300 samples: shorter than the host's lead (2048), so the first + // frame that declares it mixes all of it. + 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 }); } |
