summaryrefslogtreecommitdiff
path: root/test/voices.zig
blob: 141e562bd768e80eca4de9e0672b70ca0d522814 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//! A sound the sketch made itself, played as a voice: space bumps the
//! 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 { coins: u32 = 0 };
pub const Session = struct { blip: sk.Audio = .none };

pub fn update(s: *State, in: *const sk.Input) void {
    if (in.pressed(sk.Key.space)) s.coins += 1;
}

pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
    f.clear(0x202020);
    // 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 });
}