// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Audio in with the time it is to be heard, audio out when it is. //! //! The server sends each chunk ahead of time, stamped with the moment on its //! own clock at which its first sample should reach the listener. `push` //! takes decoded chunks as they arrive, from the connection's task; `fill` //! hands samples to the audio device, from its real-time thread, given the //! moment on this machine's clock that the first of them will be heard. The //! clock filter joins the two. //! //! Playing is locking on and then staying on: //! //! - when nothing is locked on -- at the start, after a clear, after an //! underrun -- the first `fill` skips what is already too late and pads //! with silence what is still too early, so the audio starts exactly on //! time; //! - after that the samples are simply played in order, and the difference //! between where they are and where they should be is watched. A small //! difference is corrected a frame at a time, by playing one twice or //! skipping one, which is inaudible; a large one -- the clock stepped, or a //! chunk went missing -- locks on again. //! //! The real-time thread must never wait, so it only ever *tries* the lock, //! and plays silence for the one cycle it loses the race. Everything the lock //! protects is only held for a copy. const Scheduler = @This(); const std = @import("std"); const TimeFilter = @import("TimeFilter.zig"); /// Channels a stream can have. pub const max_channels = 8; pub const Options = struct { /// How much audio can wait, in microseconds. Music Assistant sends no /// further ahead than 30 seconds, whatever the player's `buffer_capacity` /// allows; and that capacity is in bytes of *encoded* audio, which in a /// quiet passage of FLAC can be many more seconds than it would be of /// PCM. So the room here is reckoned in time, just past the server's own /// limit -- about 12 MB at 48 kHz in stereo. Audio that does not fit is /// dropped, and every drop is a gap to lock on across. capacity_us: u64 = 32 * std.time.us_per_s, /// Past this far off, in microseconds, lock on again rather than drift /// back. resync_us: i64 = 20_000, /// Past this far off, in microseconds, correct a frame per `fill`. correct_us: i64 = 2_000, }; /// A run of samples whose first frame is to be heard at `timestamp_us` /// (server time), each following frame one sample period later. const Segment = struct { frame: u64, timestamp_us: i64, }; const max_segments = 4096; gpa: std.mem.Allocator, options: Options, lock: std.atomic.Mutex = .unlocked, // Everything below is under `lock`. /// Interleaved, `capacity * channels` long. samples: []f32 = &.{}, /// Frames of room: `capacity_us` at the stream's rate. capacity: u64 = 0, channels: u8 = 0, rate: u32 = 0, /// Absolute frame numbers: the next to be written and the next to be read. /// `write_frame - read_frame` frames are waiting. write_frame: u64 = 0, read_frame: u64 = 0, segments: [max_segments]Segment = undefined, segment_head: usize = 0, segment_count: usize = 0, clock: TimeFilter = .init(), gain: f32 = 1, fade: Fade = .none, locked_on: bool = false, counters: Counters = .{}, /// A fade the output is going through, counted in output frames. const Fade = union(enum) { none, /// Down to silence over `total` frames, `done` of them so far; then /// silent for `hold` frames more. out: struct { total: u64, done: u64, hold: u64 }, /// Silent, until the stream is replaced -- or for `left` frames more, /// after which the audio fades back in. silent: struct { left: u64, total: u64 }, /// Back up over `total` frames, `done` of them so far. in: struct { total: u64, done: u64 }, }; pub const Counters = struct { /// Cycles that ran out of audio while locked on. underruns: u32 = 0, /// Frames skipped because they arrived too late to be heard on time. late_frames: u64 = 0, /// Frames of audio refused because there was no room. overflow_frames: u64 = 0, /// Single frames played twice or skipped to keep in step. corrections: u64 = 0, /// Times playback locked on. locks: u32 = 0, /// Cycles lost to the lock being held elsewhere. contended: u32 = 0, }; pub fn init(gpa: std.mem.Allocator, options: Options) Scheduler { return .{ .gpa = gpa, .options = options }; } pub fn deinit(self: *Scheduler) void { self.gpa.free(self.samples); } fn acquire(self: *Scheduler) void { while (!self.lock.tryLock()) std.atomic.spinLoopHint(); } /// Start a stream of `channels` at `rate`, throwing away whatever was /// waiting. pub fn start(self: *Scheduler, channels: u8, rate: u32) error{ OutOfMemory, UnsupportedChannels }!void { if (channels == 0 or channels > max_channels) return error.UnsupportedChannels; const frames = @max(1, self.options.capacity_us * rate / std.time.us_per_s); const needed: usize = @intCast(frames * channels); // Allocated outside the lock, swapped in under it: the real-time thread // must never find it half made. const fresh: ?[]f32 = if (needed == self.samples.len) null else try self.gpa.alloc(f32, needed); self.acquire(); const old = self.samples; if (fresh) |f| self.samples = f; self.channels = channels; self.rate = rate; self.capacity = frames; self.clearLocked(); self.lock.unlock(); if (fresh != null) self.gpa.free(old); } /// No stream: everything waiting is dropped and `fill` plays silence. pub fn stop(self: *Scheduler) void { self.acquire(); defer self.lock.unlock(); self.clearLocked(); self.rate = 0; } /// Drop everything waiting, and lock on afresh to whatever comes next: the /// server is about to send audio from somewhere else. pub fn clear(self: *Scheduler) void { self.acquire(); defer self.lock.unlock(); self.clearLocked(); } fn clearLocked(self: *Scheduler) void { self.read_frame = self.write_frame; self.segment_count = 0; self.locked_on = false; // Whatever was fading out has been replaced. self.fade = .none; } /// Fade what is playing down to silence over `fade_us`, and keep it silent: /// for when the server is about to replace it -- a skip, a seek, a pause -- /// and a second of the old audio carrying on would be worse than a moment /// of quiet. The audio goes on being consumed, so it stays in step. /// /// The next `start`, `stop` or `clear` ends the silence, and the new audio /// plays at full level. If none comes within `hold_us` of the fade ending /// -- a skip past the last track, which the server does nothing about -- /// the audio fades back in. pub fn fadeOut(self: *Scheduler, fade_us: u64, hold_us: u64) void { self.acquire(); defer self.lock.unlock(); if (self.rate == 0) return; self.fade = .{ .out = .{ .total = @max(1, usToFrames(fade_us, self.rate)), .done = 0, .hold = usToFrames(hold_us, self.rate), } }; } /// Take the output through whatever fade is under way, frame by frame. /// /// Where one stage of the fade gives way to the next, what the next is made /// from is copied out first: `f` points into `self.fade`, and building the /// new stage in its place would overwrite fields of the old one before they /// were read. fn applyFade(self: *Scheduler, planes: []const []f32, frames: u32) void { for (0..frames) |i| { const g: f32 = switch (self.fade) { .none => return, .out => |*f| blk: { const g = equalPower(f.done, f.total, false); f.done += 1; if (f.done >= f.total) { const hold = f.hold; const total = f.total; self.fade = .{ .silent = .{ .left = hold, .total = total } }; } break :blk g; }, .silent => |*f| blk: { if (f.left == 0) { const total = f.total; self.fade = .{ .in = .{ .total = total, .done = 0 } }; } else f.left -= 1; break :blk 0; }, .in => |*f| blk: { const g = equalPower(f.done, f.total, true); f.done += 1; if (f.done >= f.total) self.fade = .none; break :blk g; }, }; for (planes) |p| p[i] *= g; } } /// The gain `done` frames into a fade of `total`: a quarter cosine, which /// sounds even where a straight line would seem to drop away at the end. fn equalPower(done: u64, total: u64, rising: bool) f32 { const t = @as(f32, @floatFromInt(done)) / @as(f32, @floatFromInt(total)); const x = if (rising) 1 - t else t; return @cos(x * std.math.pi / 2); } /// The clock filter's latest estimate, which `fill` converts by. pub fn setClock(self: *Scheduler, clock: TimeFilter) void { self.acquire(); defer self.lock.unlock(); self.clock = clock; } /// 0 to 100, as Sendspin carries it, and whether muted. pub fn setVolume(self: *Scheduler, volume: u8, muted: bool) void { const v: f32 = @as(f32, @floatFromInt(@min(volume, 100))) / 100; self.acquire(); defer self.lock.unlock(); // Linear, as Music Assistant's own web player applies it, so that a // slider position sounds the same through either. A curve on top of // the server's already perceptual scale would make the bottom half of // the slider all but silent: a cube puts 25 at -36 dB. self.gain = if (muted) 0 else v; } pub fn stats(self: *Scheduler) Counters { self.acquire(); defer self.lock.unlock(); return self.counters; } /// How much is waiting, in microseconds. pub fn bufferedUs(self: *Scheduler) i64 { self.acquire(); defer self.lock.unlock(); if (self.rate == 0) return 0; return framesToUs(self.write_frame - self.read_frame, self.rate); } /// Take a chunk: interleaved samples in the stream's channel count, the first /// frame to be heard at `timestamp_us` on the server's clock. Returns how /// many frames were kept; those that did not fit are dropped. pub fn push(self: *Scheduler, timestamp_us: i64, interleaved: []const f32) usize { self.acquire(); defer self.lock.unlock(); if (self.rate == 0) return 0; const ch: usize = self.channels; const capacity: u64 = self.capacity; const frames: u64 = interleaved.len / ch; const room = capacity - (self.write_frame - self.read_frame); const kept = @min(frames, room); self.counters.overflow_frames += frames - kept; if (kept == 0) return 0; // A chunk that carries on exactly where the last one ended is part of // the same run; anything else starts a new one. const continues = if (self.lastSegment()) |last| blk: { const expected = last.timestamp_us +| framesToUs(self.write_frame - last.frame, self.rate); break :blk @abs(timestamp_us -| expected) <= framesToUs(1, self.rate); } else false; if (!continues) { if (self.segment_count == max_segments) { // More runs than room to remember them is nothing a real server // does; drop the chunk rather than lose track of when older // audio plays. self.counters.overflow_frames += kept; return 0; } self.segments[(self.segment_head + self.segment_count) % max_segments] = .{ .frame = self.write_frame, .timestamp_us = timestamp_us, }; self.segment_count += 1; } var src = interleaved[0 .. kept * ch]; while (src.len > 0) { const at: usize = @intCast(self.write_frame % capacity); const n = @min(src.len / ch, @as(usize, @intCast(capacity)) - at); @memcpy(self.samples[at * ch ..][0 .. n * ch], src[0 .. n * ch]); src = src[n * ch ..]; self.write_frame += n; } return kept; } /// Fill one plane per output channel with `frames` samples, the first of /// which will be heard at `play_at_us` on this machine's clock (`now` plus /// however long the audio device takes to play it). /// /// Called from the real-time thread: it never waits, and it never fails -- /// whatever it cannot supply is silence. Output channels beyond the stream's /// repeat its last channel. /// /// Returns whether there is a stream -- one started and not yet stopped, /// whether or not its audio has arrived -- so that a device with none for a /// while can stop asking. A cycle lost to the lock counts as having one. pub fn fill(self: *Scheduler, planes: []const []f32, frames: u32, play_at_us: i64) bool { for (planes) |p| @memset(p[0..frames], 0); if (!self.lock.tryLock()) { self.counters.contended +%= 1; return true; } defer self.lock.unlock(); if (self.rate == 0) return false; if (!self.clock.synchronized()) return true; self.fillLocked(planes, frames, play_at_us); self.applyFade(planes, frames); return true; } fn fillLocked(self: *Scheduler, planes: []const []f32, frames: u32, play_at_us: i64) void { var out: u32 = 0; const due = self.clock.serverTime(play_at_us); if (self.read_frame == self.write_frame) { if (self.locked_on) self.counters.underruns += 1; self.locked_on = false; return; } // Saturating: the timestamps are the server's, and can be anything. const off = self.timeOf(self.read_frame) -| due; if (!self.locked_on or @abs(off) > self.options.resync_us) { if (off < 0) { // Too late: skip what can no longer be heard on time. const late = @min(usToFrames(@abs(off), self.rate), self.write_frame - self.read_frame); self.read_frame += late; self.counters.late_frames += late; if (self.read_frame == self.write_frame) { self.locked_on = false; self.trimSegments(); return; } } else { // Too early: silence until it is due. const early = usToFrames(@intCast(off), self.rate); if (early >= frames) { self.locked_on = false; return; } out = @intCast(early); } self.locked_on = true; self.counters.locks += 1; } else if (off > self.options.correct_us) { // A little early: hold the first frame for one extra period. self.copyFrames(planes, out, 1, false); out += 1; self.counters.corrections += 1; } else if (off < -self.options.correct_us and self.write_frame - self.read_frame > 1) { // A little late: skip one. self.read_frame += 1; self.counters.corrections += 1; } const available = self.write_frame - self.read_frame; const n: u32 = @intCast(@min(frames - out, available)); self.copyFrames(planes, out, n, true); out += n; if (out < frames) { self.counters.underruns += 1; self.locked_on = false; } self.trimSegments(); if (self.gain != 1) for (planes) |p| for (p[0..frames]) |*s| { s.* *= self.gain; }; } /// Copy `n` frames from the read position into the planes at `at`, and /// advance past them if `advance`. fn copyFrames(self: *Scheduler, planes: []const []f32, at: u32, n: u32, advance: bool) void { const ch: usize = self.channels; const capacity = self.capacity; for (0..n) |i| { const frame: usize = @intCast((self.read_frame + i) % capacity); const src = self.samples[frame * ch ..][0..ch]; for (planes, 0..) |p, c| p[at + i] = src[@min(c, ch - 1)]; } if (advance) self.read_frame += n; } /// When `frame` is to be heard, in server time. fn timeOf(self: *const Scheduler, frame: u64) i64 { var found: ?Segment = null; for (0..self.segment_count) |i| { const s = self.segments[(self.segment_head + i) % max_segments]; if (s.frame > frame) break; found = s; } const s = found orelse return 0; return s.timestamp_us +| framesToUs(frame - s.frame, self.rate); } fn lastSegment(self: *const Scheduler) ?Segment { if (self.segment_count == 0) return null; return self.segments[(self.segment_head + self.segment_count - 1) % max_segments]; } /// Forget runs that have been played through. fn trimSegments(self: *Scheduler) void { while (self.segment_count > 1) { const next = self.segments[(self.segment_head + 1) % max_segments]; if (next.frame > self.read_frame) break; self.segment_head = (self.segment_head + 1) % max_segments; self.segment_count -= 1; } if (self.segment_count == 1 and self.read_frame == self.write_frame and !self.locked_on) { self.segment_count = 0; } } fn framesToUs(frames: u64, rate: u32) i64 { return @intCast(frames * std.time.us_per_s / rate); } /// Frames in `us`, as many as a `u64` holds. fn usToFrames(us: u64, rate: u32) u64 { return @intCast(@min(@as(u128, us) * rate / std.time.us_per_s, std.math.maxInt(u64))); } /// This machine's clock, as the clock filter and `fill` want it: monotonic /// microseconds. The same on every thread, which is the point of it. pub fn now() i64 { var ts: std.os.linux.timespec = undefined; _ = std.os.linux.clock_gettime(.MONOTONIC, &ts); return @as(i64, ts.sec) * std.time.us_per_s + @divTrunc(@as(i64, ts.nsec), std.time.ns_per_us); } const testing = std.testing; /// A clock filter that says the server's clock is ours plus `offset`. fn fixedClock(offset: i64) TimeFilter { var f: TimeFilter = .init(); f.update(offset, 100, 1_000_000); f.update(offset, 100, 2_000_000); return f; } /// A mono chunk whose samples count up from `first`, to tell them apart. fn ramp(buf: []f32, first: f32) []f32 { for (buf, 0..) |*s, i| s.* = first + @as(f32, @floatFromInt(i)); return buf; } test "audio starts exactly when it is due" { var s: Scheduler = .init(testing.allocator, .{ .capacity_us = 1_000_000 }); defer s.deinit(); try s.start(1, 1000); // one frame a millisecond s.setClock(fixedClock(500_000)); var chunk: [100]f32 = undefined; // To be heard at server 600 ms, which is client 100 ms. try testing.expectEqual(@as(usize, 100), s.push(600_000, ramp(&chunk, 1))); var plane: [20]f32 = undefined; const planes = [_][]f32{&plane}; // Client 90 ms: ten frames of silence, then the first ten samples. _ = s.fill(&planes, 20, 90_000); try testing.expectEqual(@as(f32, 0), plane[9]); try testing.expectEqual(@as(f32, 1), plane[10]); try testing.expectEqual(@as(f32, 10), plane[19]); // And it carries on in order. _ = s.fill(&planes, 20, 110_000); try testing.expectEqual(@as(f32, 11), plane[0]); try testing.expectEqual(@as(u32, 1), s.stats().locks); } test "audio that is already late is skipped" { var s: Scheduler = .init(testing.allocator, .{ .capacity_us = 1_000_000 }); defer s.deinit(); try s.start(1, 1000); s.setClock(fixedClock(0)); var chunk: [100]f32 = undefined; _ = s.push(100_000, ramp(&chunk, 1)); var plane: [10]f32 = undefined; const planes = [_][]f32{&plane}; _ = s.fill(&planes, 10, 130_000); try testing.expectEqual(@as(f32, 31), plane[0]); try testing.expectEqual(@as(u64, 30), s.stats().late_frames); } test "a small drift is corrected a frame at a time" { var s: Scheduler = .init(testing.allocator, .{ .capacity_us = 1_000_000, .correct_us = 2_000 }); defer s.deinit(); try s.start(1, 1000); s.setClock(fixedClock(0)); var chunk: [200]f32 = undefined; _ = s.push(0, ramp(&chunk, 0)); var plane: [10]f32 = undefined; const planes = [_][]f32{&plane}; _ = s.fill(&planes, 10, 0); try testing.expectEqual(@as(f32, 0), plane[0]); // The device asks for the next ten 5 ms later than it should: behind by // five frames, so one is skipped. _ = s.fill(&planes, 10, 15_000); try testing.expectEqual(@as(f32, 11), plane[0]); try testing.expectEqual(@as(u64, 1), s.stats().corrections); // And 5 ms early: the first is played twice. _ = s.fill(&planes, 10, 16_000); try testing.expectEqual(plane[0], plane[1]); try testing.expectEqual(@as(u64, 2), s.stats().corrections); } test "a clear drops what was waiting, and running out is an underrun" { var s: Scheduler = .init(testing.allocator, .{ .capacity_us = 1_000_000 }); defer s.deinit(); try s.start(2, 1000); s.setClock(fixedClock(0)); var chunk: [20]f32 = undefined; // ten stereo frames _ = s.push(0, ramp(&chunk, 1)); var left: [8]f32 = undefined; var right: [8]f32 = undefined; var extra: [8]f32 = undefined; const planes = [_][]f32{ &left, &right, &extra }; _ = s.fill(&planes, 8, 0); try testing.expectEqual(@as(f32, 1), left[0]); try testing.expectEqual(@as(f32, 2), right[0]); // A third output channel repeats the last one there is. try testing.expectEqual(@as(f32, 2), extra[0]); _ = s.fill(&planes, 8, 8_000); try testing.expectEqual(@as(f32, 17), left[0]); try testing.expectEqual(@as(f32, 0), left[2]); try testing.expectEqual(@as(u32, 1), s.stats().underruns); _ = s.push(20_000, ramp(&chunk, 1)); s.clear(); try testing.expectEqual(@as(i64, 0), s.bufferedUs()); } test "chunks that follow on are one run; a gap starts another" { var s: Scheduler = .init(testing.allocator, .{ .capacity_us = 1_000_000 }); defer s.deinit(); try s.start(1, 1000); var chunk: [10]f32 = undefined; _ = s.push(0, ramp(&chunk, 0)); _ = s.push(10_000, ramp(&chunk, 10)); try testing.expectEqual(@as(usize, 1), s.segment_count); _ = s.push(50_000, ramp(&chunk, 50)); try testing.expectEqual(@as(usize, 2), s.segment_count); try testing.expectEqual(@as(i64, 50_000), s.timeOf(20)); try testing.expectEqual(@as(i64, 15_000), s.timeOf(15)); } test "more than fits is refused" { var s: Scheduler = .init(testing.allocator, .{ .capacity_us = 16_000 }); defer s.deinit(); try s.start(1, 1000); var chunk: [10]f32 = undefined; try testing.expectEqual(@as(usize, 10), s.push(0, ramp(&chunk, 0))); try testing.expectEqual(@as(usize, 6), s.push(10_000, ramp(&chunk, 10))); try testing.expectEqual(@as(u64, 4), s.stats().overflow_frames); } test "the volume is linear, and mute is silence" { var s: Scheduler = .init(testing.allocator, .{ .capacity_us = 100_000 }); defer s.deinit(); try s.start(1, 1000); s.setClock(fixedClock(0)); var chunk: [10]f32 = @splat(1); _ = s.push(0, &chunk); s.setVolume(50, false); var plane: [2]f32 = undefined; const planes = [_][]f32{&plane}; _ = s.fill(&planes, 2, 0); try testing.expectEqual(@as(f32, 0.5), plane[0]); s.setVolume(50, true); _ = s.fill(&planes, 2, 2_000); try testing.expectEqual(@as(f32, 0), plane[0]); } test "fill says whether there is a stream, audio or not" { var s: Scheduler = .init(testing.allocator, .{ .capacity_us = 100_000 }); defer s.deinit(); var plane: [4]f32 = undefined; const planes = [_][]f32{&plane}; // Never started: nothing to ask for. try testing.expect(!s.fill(&planes, 4, 0)); // Started, with no clock and no audio yet: a stream all the same. try s.start(1, 1000); try testing.expect(s.fill(&planes, 4, 0)); s.setClock(fixedClock(0)); try testing.expect(s.fill(&planes, 4, 0)); s.stop(); try testing.expect(!s.fill(&planes, 4, 0)); } test "a fade out goes quiet, stays quiet until replaced, and comes back if not" { var s: Scheduler = .init(testing.allocator, .{ .capacity_us = 1_000_000 }); defer s.deinit(); try s.start(1, 1000); s.setClock(fixedClock(0)); var chunk: [100]f32 = @splat(1); _ = s.push(0, &chunk); var plane: [10]f32 = undefined; const planes = [_][]f32{&plane}; _ = s.fill(&planes, 10, 0); try testing.expectEqual(@as(f32, 1), plane[9]); // Over 4 ms, then 3 ms of silence, then back. s.fadeOut(4_000, 3_000); _ = s.fill(&planes, 10, 10_000); try testing.expectEqual(@as(f32, 1), plane[0]); try testing.expect(plane[1] < 1 and plane[1] > plane[2] and plane[2] > plane[3]); try testing.expectEqual(@as(f32, 0), plane[4]); try testing.expectEqual(@as(f32, 0), plane[7]); try testing.expectApproxEqAbs(@as(f32, 0), plane[8], 1e-6); try testing.expect(plane[9] > 0 and plane[9] < 1); _ = s.fill(&planes, 10, 20_000); try testing.expectEqual(@as(f32, 1), plane[9]); // Replaced before the hold runs out: the new audio plays at full level. s.fadeOut(2_000, 50_000); _ = s.fill(&planes, 10, 30_000); try testing.expectEqual(@as(f32, 0), plane[9]); s.clear(); _ = s.push(40_000, &chunk); _ = s.fill(&planes, 10, 40_000); try testing.expectEqual(@as(f32, 1), plane[0]); } test "timestamps at the ends of an i64 play as silence rather than overflow" { var s: Scheduler = .init(testing.allocator, .{ .capacity_us = 1_000_000 }); defer s.deinit(); try s.start(1, 48000); s.setClock(fixedClock(0)); var chunk: [100]f32 = undefined; var plane: [10]f32 = undefined; const planes = [_][]f32{&plane}; // The server's times are whatever it says; so is where the device is. for ([_]i64{ std.math.minInt(i64), std.math.maxInt(i64), -1, 0 }) |at| { _ = s.push(at, ramp(&chunk, 1)); _ = s.push(at +% 1, ramp(&chunk, 1)); _ = s.fill(&planes, 10, std.math.maxInt(i64)); _ = s.fill(&planes, 10, std.math.minInt(i64)); } s.fadeOut(std.math.maxInt(u64), std.math.maxInt(u64)); _ = s.fill(&planes, 10, 0); } test "a fade out, its silence and its fade back in stay finite" { var s: Scheduler = .init(testing.allocator, .{ .capacity_us = 1_000_000 }); defer s.deinit(); try s.start(1, 1000); s.setClock(fixedClock(0)); var chunk: [100]f32 = undefined; for (&chunk) |*c| c.* = 1; _ = s.push(0, &chunk); // Two frames down, none held: the silence gives way at once to the // fade back in, which must know how long it is. s.fadeOut(2_000, 0); var plane: [10]f32 = undefined; const planes = [_][]f32{&plane}; _ = s.fill(&planes, 10, 0); for (plane) |x| try testing.expect(std.math.isFinite(x)); try testing.expectEqual(@as(f32, 1), plane[9]); }