diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-28 21:07:13 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-12 13:22:02 -0300 |
| commit | 590ba7b9b579b97e34b51ab7e3460ebc85713343 (patch) | |
| tree | 6a73a7570ff4f79f41deff078455877b9bc6571b /src/host/audio.zig | |
| parent | bc8e6eb1bfef0c5e9b3c40fc1e4fd5a1659e2b03 (diff) | |
| download | sketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.tar.gz sketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.zip | |
sketchbook: Zig 0.17, a Store on disk, requests, a scope (waveform, spectrum, spectrogram), audio codecs and fonts, web host
Diffstat (limited to 'src/host/audio.zig')
| -rw-r--r-- | src/host/audio.zig | 94 |
1 files changed, 67 insertions, 27 deletions
diff --git a/src/host/audio.zig b/src/host/audio.zig index 87332b1..aebb909 100644 --- a/src/host/audio.zig +++ b/src/host/audio.zig @@ -1,9 +1,11 @@ -//! Sound output: decoded clips played by reconciled voices, plus raw streams -//! the sketch fills itself. The audio thread only ever sees SDL's own copies -//! of the samples; sketch code and memory never run on it. +//! Sound: decoded clips played by reconciled voices, raw streams the sketch +//! fills itself, and the microphone, open while declared. The audio thread +//! only ever sees SDL's own copies of the samples; sketch code and memory +//! never run on it. const std = @import("std"); const c = @import("c"); const abi = @import("abi"); +const memory = @import("memory.zig"); pub const sample_rate = 48_000; const spec: c.SDL_AudioSpec = .{ .format = c.SDL_AUDIO_F32, .channels = 1, .freq = sample_rate }; @@ -21,6 +23,11 @@ pub const Audio = struct { streams: std.ArrayList(Stream) = .empty, status: std.ArrayList(abi.Stream) = .empty, scope: Scope = .{}, + /// The default recording device, bound to this stream while declared. + microphone: ?*c.SDL_AudioStream = null, + listening: bool = false, + /// Set when opening failed, so it is tried once per declaration. + deaf: bool = false, const Playing = struct { voice: abi.Voice, cursor: usize = 0, done: bool = false, seen: bool = true }; const Stream = struct { key: u32, out: *c.SDL_AudioStream, pushed: u64 = 0 }; @@ -34,6 +41,7 @@ pub const Audio = struct { } pub fn deinit(self: *Audio) void { + if (self.microphone) |m| c.SDL_DestroyAudioStream(m); self.scope.deinit(); for (self.streams.items) |s| c.SDL_DestroyAudioStream(s.out); if (self.voices_out) |s| c.SDL_DestroyAudioStream(s); @@ -70,7 +78,7 @@ pub const Audio = struct { /// `.none` when `wav` does not decode. pub fn decode(self: *Audio, wav: []const u8) abi.Audio { - return self.add(decodeWav(self.gpa, wav) catch return .none); + return self.add(decodeBytes(self.gpa, wav) catch return .none); } fn add(self: *Audio, samples: []f32) abi.Audio { @@ -78,17 +86,52 @@ pub const Audio = struct { self.gpa.free(samples); return .none; }; - return @enumFromInt(self.clips.items.len); + return @fromBackingInt(@intCast(self.clips.items.len)); } - fn samplesOf(self: *Audio, audio: abi.Audio) []const f32 { - const index = @intFromEnum(audio); + pub fn samplesOf(self: *Audio, audio: abi.Audio) []const f32 { + const index = @backingInt(audio); if (index == 0 or index > self.clips.items.len) return &.{}; return self.clips.items[index - 1]; } pub fn beginFrame(self: *Audio) void { for (self.voices.items) |*v| v.seen = false; + self.listening = false; + } + + /// The microphone was declared this frame. + pub fn listen(self: *Audio) void { + self.listening = true; + } + + /// What the microphone heard since the last call, into `arena`. + pub fn record(self: *Audio, arena: std.mem.Allocator) []const f32 { + const m = self.microphone orelse return &.{}; + const available = c.SDL_GetAudioStreamAvailable(m); + if (available <= 0) return &.{}; + const samples = arena.alloc(f32, @as(usize, @intCast(available)) / @sizeOf(f32)) catch return &.{}; + const got = c.SDL_GetAudioStreamData(m, samples.ptr, @intCast(samples.len * @sizeOf(f32))); + if (got <= 0) return &.{}; + return samples[0 .. @as(usize, @intCast(got)) / @sizeOf(f32)]; + } + + /// Opens or closes the microphone to match this frame's declaration. + fn reconcileMicrophone(self: *Audio) void { + if (!self.listening) { + if (self.microphone) |m| c.SDL_DestroyAudioStream(m); + self.microphone = null; + self.deaf = false; + return; + } + if (self.microphone != null or self.deaf) return; + const m = c.SDL_OpenAudioDeviceStream(c.SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &spec, null, null) orelse { + std.log.warn("microphone: {s}", .{c.SDL_GetError()}); + self.deaf = true; + return; + }; + if (!c.SDL_ResumeAudioStreamDevice(m)) std.log.warn("microphone: {s}", .{c.SDL_GetError()}); + self.microphone = m; } /// Reconciles one declared voice with what is playing. @@ -128,8 +171,10 @@ pub const Audio = struct { return &self.streams.items[self.streams.items.len - 1]; } - /// Drops undeclared voices and keeps the voice mix queued ahead. + /// Drops undeclared voices, keeps the voice mix queued ahead, and opens + /// or closes the microphone. pub fn endFrame(self: *Audio) void { + self.reconcileMicrophone(); var i: usize = 0; while (i < self.voices.items.len) { if (self.voices.items[i].seen) i += 1 else _ = self.voices.swapRemove(i); @@ -183,28 +228,23 @@ pub const Audio = struct { } }; -fn decodeWav(gpa: std.mem.Allocator, wav: []const u8) ![]f32 { - const io = c.SDL_IOFromConstMem(wav.ptr, wav.len) orelse return error.OpenWav; - var from: c.SDL_AudioSpec = undefined; - var data: [*c]u8 = null; - var len: u32 = 0; - if (!c.SDL_LoadWAV_IO(io, true, &from, &data, &len)) return error.DecodeWav; - defer c.SDL_free(data); - return convert(gpa, &from, data[0..len]); +/// WAV, FLAC or Ogg Vorbis, by content (see `vendor/codecs.h`). +fn decodeBytes(gpa: std.mem.Allocator, bytes: []const u8) ![]f32 { + var channels: u32 = 0; + var rate: u32 = 0; + var frames: u64 = 0; + const raw = c.sketchbook_decode_audio(bytes.ptr, bytes.len, &channels, &rate, &frames) orelse return error.DecodeAudio; + defer c.sketchbook_free_audio(raw); + if (rate == 0 or rate > 192000 or channels == 0 or channels > 8 or frames == 0 or frames > @as(u64, rate) * 600) return error.UnsupportedSoundFormat; + const from: c.SDL_AudioSpec = .{ .format = c.SDL_AUDIO_F32, .channels = @intCast(channels), .freq = @intCast(rate) }; + return convert(gpa, &from, std.mem.sliceAsBytes(raw[0..@intCast(frames * channels)])); } fn decodeFile(gpa: std.mem.Allocator, path: []const u8) ![]f32 { - var buf: [std.fs.max_path_bytes]u8 = undefined; - const z = try std.fmt.bufPrintZ(&buf, "{s}", .{path}); - var info: c.SF_INFO = std.mem.zeroes(c.SF_INFO); - const file = c.sf_open(z.ptr, c.SFM_READ, &info) orelse return error.OpenSoundFile; - defer _ = c.sf_close(file); - if (info.samplerate <= 0 or info.samplerate > 192000 or info.channels <= 0 or info.channels > 8 or info.frames <= 0 or info.frames > @as(i64, info.samplerate) * 600) return error.UnsupportedSoundFormat; - const raw = try gpa.alloc(f32, @intCast(info.frames * info.channels)); - defer gpa.free(raw); - if (c.sf_readf_float(file, raw.ptr, info.frames) != info.frames) return error.TruncatedAudio; - const from: c.SDL_AudioSpec = .{ .format = c.SDL_AUDIO_F32, .channels = info.channels, .freq = info.samplerate }; - return convert(gpa, &from, std.mem.sliceAsBytes(raw)); + const bytes = memory.mapReadOnly(path); + if (bytes.len == 0) return error.OpenSoundFile; + defer std.posix.munmap(bytes); + return decodeBytes(gpa, bytes); } /// To host format: f32 mono at `sample_rate`, all finite. |
