diff options
Diffstat (limited to 'examples/micscope/src/main.zig')
| -rw-r--r-- | examples/micscope/src/main.zig | 1299 |
1 files changed, 1299 insertions, 0 deletions
diff --git a/examples/micscope/src/main.zig b/examples/micscope/src/main.zig new file mode 100644 index 0000000..99aac76 --- /dev/null +++ b/examples/micscope/src/main.zig @@ -0,0 +1,1299 @@ +//! micscope: records the microphone or opens sound files, plays them +//! forwards or backwards, and shows each as a waveform, its spectrogram on +//! the same time axis, and the spectrum at its playhead. The window is a +//! vertical strip: on top, what the microphone hears right now (or the take +//! being recorded); under it every take, newest first. The signal +//! processing (windowed FFTs, log-frequency interpolation, smoothing, +//! waveform reduction) and all the drawing run on the GPU; the CPU only +//! moves samples: microphone to tape, tape to the GPU once, tape to the +//! speakers. +//! +//! Memory by lifetime: State is the transport, the selection and the views, +//! the takes (their ids and names), changed by `update` from keys and the +//! mouse, and replayable. A take's samples are a file of its own, written +//! by `draw` as it records and by `open` (see `Host.appendFile`): they are +//! still there after a restart. Session is this process's copy of the +//! takes' samples, the playback cursor, the microphone's recent past and +//! GPU objects. Transport changes and seeks are `sk.Request`s, which `draw` +//! carries out once each. +const std = @import("std"); +const sk = @import("sketch"); +const gpu = sk.abi.gpu; +const scope = @import("scope"); +const layouts = scope.layouts; +/// The tuner's blocks; its shaders are `shaders`. +const tuner_layouts = @import("layouts"); +const shaders = @import("shaders"); +const tuner = @import("tuner.zig"); + +pub const name = "micscope"; +pub const width = 1280; +pub const height = 900; +pub const commands = .{ "open", "next-id" }; + +/// The longest take: ten minutes at the host's rate. +const max_seconds = 600; +const max_takes = 64; +/// The playback stream's key. +const stream = 1; +/// How far ahead playback keeps the stream filled. +const lead_seconds = 0.06; +/// The live view's span. +const live_seconds = 6; + +pub const Transport = enum { stopped, recording, forward, backward }; + +/// The part of a take its timeline shows, as fractions of it. +pub const View = struct { + lo: f32 = 0, + hi: f32 = 1, +}; + +pub const State = struct { + transport: sk.Request(Transport) = .{}, + /// The direction space resumes in. + backward: bool = false, + /// To a fraction of the selected take. + seek: sk.Request(f32) = .{}, + scrubbing: bool = false, + dialog: sk.Request(void) = .{}, + /// Oldest first. + takes: sk.List(Take, max_takes) = .{}, + /// The next take's id. `draw` moves it past take files already there + /// with the `next-id` command: other runs' (`--fresh` keeps the files), + /// or the future a seek went back from. + next_id: u32 = 1, + /// The take the transport and the keys work on, by index (oldest 0). + selected: u32 = 0, + views: [max_takes]View = @splat(.{}), + /// Maps the world (the panels, stacked) to the screen. + camera: sk.Camera = .{}, + /// The camera keeps the bottom, where now is, in view. + follow: bool = true, + panning: bool = false, + /// The pointer's height when panning last moved. + grab: f32 = 0, + gain: f32 = 1, + /// The tuner's reference pitch: A4, in Hz. + a4: f32 = 440, +}; + +pub const Take = struct { + /// Names its file, `take-<id>.f32`: its mono samples at + /// `Input.sample_rate`. Stays the same when earlier takes are deleted: + /// Session and the GPU caches go by it. + id: u32 = 0, + /// A file's name, or empty for the microphone. + source: [64]u8 = @splat(0), + source_len: u32 = 0, +}; + +/// A take's samples in `Session.tape`. +const Clip = struct { + id: u32 = 0, + start: usize = 0, + len: usize = 0, + /// Where the listener is, in samples into the take. + heard: f64 = 0, +}; + +pub const Session = struct { + transport: sk.Handled = .{}, + seek: sk.Handled = .{}, + dialog: sk.Handled = .{}, + /// State's takes' samples, read from their files once a process, one + /// after another in the order read. A recording grows the last one. + tape: std.ArrayList(f32) = .empty, + clips: sk.List(Clip, max_takes) = .{}, + /// The take playback follows, by id, and the next of its samples to + /// queue. + playing: u32 = 0, + cursor: usize = 0, + /// The microphone's recent past and the live view. + live: scope.Scope = .{}, + /// Pitch, by tuner.comp: of the microphone (live, or recording), and + /// of the selected take at its playhead. + tuners: [2]tuner.Tracker = @splat(.{}), + /// The take `tuners[1]` follows, by id. + tuned: u32 = 0, + gpu: Gpu = .{}, +}; + +/// Which tracker a readback belongs to: its index, as the download key. +const Tuned = enum(u32) { input, take }; + +/// GPU objects, made on first use. +const Gpu = struct { + ready: bool = false, + /// `Session.tape`, and how many of its samples are in it. + tape: sk.Buffer = .none, + capacity: usize = 0, + uploaded: usize = 0, + /// spectrum.comp's FFT, shared by every panel. + scratch: sk.Buffer = .none, + /// tuner.comp's results, a `tuner_layouts.Pitch` each, by `Tuned`, and + /// the difference functions it searches. + pitches: [2]sk.Buffer = @splat(.none), + differences: [2]sk.Buffer = @splat(.none), + /// For the takes on screen, by take id; the least recently used is + /// reused. + panels: [16]Panel = @splat(.{}), +}; + +/// One panel's buffers and images, and the inputs they were last made from: +/// unchanged, they are not made again. +const Panel = struct { + id: u32 = 0, + used: u64 = 0, + wave_columns: sk.Buffer = .none, + spectrum_columns: sk.Buffer = .none, + levels: sk.Buffer = .none, + wave: sk.Image = .none, + wave_size: [2]u32 = .{ 0, 0 }, + spectrogram: sk.Image = .none, + spectrogram_size: [2]u32 = .{ 0, 0 }, + spectrum: sk.Image = .none, + spectrum_size: [2]u32 = .{ 0, 0 }, + wave_key: u64 = 0, + spectrogram_key: u64 = 0, + spectrum_key: u64 = 0, + wave_drawn: u64 = 0, + spectrogram_drawn: u64 = 0, + + fn forget(p: *Panel) void { + p.wave_key = 0; + p.spectrogram_key = 0; + p.spectrum_key = 0; + p.wave_drawn = 0; + p.spectrogram_drawn = 0; + } +}; + +// --------------------------------------------------------------------------- +// The world: panels stacked down in the order they were made, take 0 on top +// and the live microphone at the bottom, where now is. A take keeps its +// place as others are added. The camera maps the world to the screen: it +// follows the bottom unless moved, and zooms out to overview the strip. +// Panels are rendered at world resolution and scaled by the camera, so +// moving it never recomputes anything. + +const spectrogram_height = 132; +const min_zoom = 0.3; +/// The canvas follows the window, but no smaller than this. +const min_width = 320; +const min_height = 300; + +/// Sizes, from the canvas. Wide, a panel is a waveform over its spectrogram +/// with the spectrum beside them, and the buttons sit in the header. Narrow +/// (a phone), the spectrum goes under them, full width, and the buttons +/// along the bottom, where thumbs are, big enough to hit. +const Layout = struct { + w: f32, + h: f32, + narrow: bool, + /// World units are canvas pixels at zoom 1. + world_width: f32, + panel_height: f32, + /// Where the world shows, between the header and the footer or buttons. + view: sk.Rect, + + const header = 36; + const footer = 22; + const toolbar = 56; + const spectrum_width = 320; + + fn of(w: f32, h: f32) Layout { + const narrow = w < 760; + const margin: f32 = if (narrow) 8 else 16; + const bottom: f32 = if (narrow) toolbar else footer; + return .{ + .w = w, + .h = h, + .narrow = narrow, + .world_width = w - 2 * margin, + .panel_height = if (narrow) 412 else 236, + .view = .{ .x = 0, .y = header, .w = w, .h = h - header - bottom }, + }; + } + + /// The panel in world slot `k`. + fn box(l: Layout, k: u32) Box { + const y = @as(f32, @floatFromInt(k)) * l.panel_height; + const timeline_w = if (l.narrow) l.world_width else l.world_width - spectrum_width - 16; + const wave: sk.Rect = .{ .x = 0, .y = y + 18, .w = timeline_w, .h = 64 }; + const spectrogram: sk.Rect = .{ .x = 0, .y = wave.y + wave.h + 2, .w = timeline_w, .h = spectrogram_height }; + const timeline: sk.Rect = .{ .x = 0, .y = wave.y, .w = timeline_w, .h = spectrogram.y + spectrogram.h - wave.y }; + // Under the timeline and its time labels, or beside it; room for + // the frequency labels under it either way. + const levels: sk.Rect = if (l.narrow) + .{ .x = 0, .y = spectrogram.y + spectrogram.h + 22, .w = l.world_width, .h = 150 } + else + .{ .x = l.world_width - spectrum_width, .y = wave.y, .w = spectrum_width, .h = timeline.h - 14 }; + return .{ + .whole = .{ .x = 0, .y = y, .w = l.world_width, .h = l.panel_height }, + .wave = wave, + .spectrogram = spectrogram, + .timeline = timeline, + .spectrum = levels, + }; + } + + /// Button `i` of `buttons`: right to left in the header, or left to + /// right across the bottom. + fn button(l: Layout, i: usize) sk.Rect { + const n: f32 = @floatFromInt(buttons.len); + const at: f32 = @floatFromInt(i); + if (l.narrow) { + const w = (l.w - 8 - (n - 1) * 4) / n; + return .{ .x = 4 + (n - 1 - at) * (w + 4), .y = l.h - toolbar + 6, .w = w, .h = toolbar - 10 }; + } + return .{ .x = l.w - 16 - (at + 1) * (button_width + 6) + 6, .y = 5, .w = button_width, .h = 26 }; + } + + fn buttonAt(l: Layout, x: f32, y: f32) ?Action { + for (buttons, 0..) |a, i| if (l.button(i).contains(x, y)) return a; + return null; + } +}; + +const Box = struct { + whole: sk.Rect, + wave: sk.Rect, + spectrogram: sk.Rect, + /// Waveform and spectrogram: one time axis, where clicks seek and + /// ctrl+wheel (or a pinch) zooms. + timeline: sk.Rect, + spectrum: sk.Rect, + + fn toScreen(b: Box, cam: sk.Camera) Box { + return .{ + .whole = cam.toScreen(b.whole), + .wave = cam.toScreen(b.wave), + .spectrogram = cam.toScreen(b.spectrogram), + .timeline = cam.toScreen(b.timeline), + .spectrum = cam.toScreen(b.spectrum), + }; + } +}; + +const World = struct { + /// Slots `0..takes` are takes; slot `takes` is the live view, unless a + /// recording (the last take) stands in for it. + takes: u32, + live: bool, + + fn of(s: *const State) World { + return .{ .takes = s.takes.count(), .live = s.transport.value != .recording }; + } + fn slots(w: World) u32 { + return w.takes + @intFromBool(w.live); + } + fn height(w: World, l: Layout) f32 { + return @as(f32, @floatFromInt(w.slots())) * l.panel_height; + } + /// The take at world point `p`. + fn takeAt(w: World, l: Layout, p: sk.shape.Point) ?u32 { + if (p.y < 0 or p.x < 0 or p.x > l.world_width) return null; + const k: u32 = @intFromFloat(@floor(p.y / l.panel_height)); + return if (k < w.takes) k else null; + } +}; + +/// The camera's vertical range: from the world's top at the viewport's top +/// to its bottom at the viewport's bottom. +fn cameraRange(world: World, scale: f32, l: Layout) [2]f32 { + const highest = l.view.y + 4; + const lowest = l.view.y + l.view.h - 4 - world.height(l) * scale; + return .{ @min(lowest, highest), highest }; +} + +/// The camera as drawn: centred across, inside the world, and on the +/// bottom while following. +fn cameraOf(s: *const State, l: Layout) sk.Camera { + var cam = s.camera; + cam.zoom = std.math.clamp(cam.zoom, min_zoom, 1); + cam.x = @round((l.w - l.world_width * cam.zoom) / 2); + const range = cameraRange(World.of(s), cam.zoom, l); + cam.y = @round(if (s.follow) range[0] else std.math.clamp(cam.y, range[0], range[1])); + return cam; +} + +/// Moves the camera, which follows again once it reaches the bottom. +fn moveCamera(s: *State, cam: sk.Camera, in: *const sk.Input) void { + const l = Layout.of(in.width, in.height); + s.camera = cam; + s.follow = false; + s.camera = cameraOf(s, l); + const range = cameraRange(World.of(s), s.camera.zoom, l); + s.follow = s.camera.y <= range[0] + 0.5; +} + +// --------------------------------------------------------------------------- +// Update: transport, selection, views and camera, from input alone. + +pub fn update(s: *State, in: *const sk.Input) void { + if (in.started) { + // Nothing resumes on its own after a restart; the takes are kept. + s.transport.value = .stopped; + s.scrubbing = false; + s.panning = false; + } + const recording = s.transport.value == .recording; + for (keys) |k| if (in.pressed(k[0])) act(s, k[1], in); + if (in.pressed(sk.Key.equals)) s.gain = @min(s.gain * 1.25, 64); + if (in.pressed(sk.Key.minus)) s.gain = @max(s.gain / 1.25, 0.25); + if (in.pressed(sk.Key.z) and s.selected < max_takes) s.views[s.selected] = .{}; + if (!recording and s.takes.count() > 0) { + // Up the strip is older. + if (in.pressed(sk.Key.up)) select(s, s.selected -| 1, in); + if (in.pressed(sk.Key.down)) select(s, @min(s.selected + 1, s.takes.count() - 1), in); + } + const l = Layout.of(in.width, in.height); + const page = l.view.h - l.panel_height / 2; + if (in.pressed(sk.Key.page_down)) panBy(s, -page, in); + if (in.pressed(sk.Key.page_up)) panBy(s, page, in); + + const view = l.view; + for (in.events) |e| { + const cam = cameraOf(s, l); + const world = World.of(s); + const p = cam.toWorld(sk.shape.Point{ .x = e.x, .y = e.y }); + switch (e.kind) { + .mouse_down => if (e.code == 1 and l.buttonAt(e.x, e.y) != null) { + act(s, l.buttonAt(e.x, e.y).?, in); + } else if (e.code == 2 or e.code == 3) { + // Middle or right drag pans. + s.panning = true; + s.grab = e.y; + } else if (e.code == 1 and !recording and view.contains(e.x, e.y)) { + const index = world.takeAt(l, p) orelse continue; + select(s, index, in); + const b = l.box(index); + if (b.timeline.contains(p.x, p.y)) { + s.scrubbing = true; + seekTo(s, fractionAt(s.views[index], b.timeline, p.x)); + } + }, + .mouse_move => if (s.panning) { + panBy(s, e.y - s.grab, in); + s.grab = e.y; + } else if (s.scrubbing and s.selected < world.takes) { + seekTo(s, fractionAt(s.views[s.selected], l.box(s.selected).timeline, p.x)); + }, + .mouse_up, .cancel => { + s.scrubbing = false; + s.panning = false; + }, + // Ctrl+wheel, or a pinch (which browsers and the web host send + // as ctrl+wheel), zooms what is under it: a take's time on its + // timeline, the view anywhere else. + .wheel => if (view.contains(e.x, e.y)) { + const ctrl = e.modifiers() & sk.Key.Mod.ctrl != 0; + const over = world.takeAt(l, p); + if (ctrl and over != null and l.box(over.?).timeline.contains(p.x, p.y)) { + const index = over.?; + zoom(&s.views[index], fractionAt(s.views[index], l.box(index).timeline, p.x), std.math.pow(f32, 0.8, e.wheel())); + } else if (ctrl) { + var moved = cam; + moved.zoomAt(sk.shape.Point{ .x = e.x, .y = e.y }, e.wheel(), .{ .min = min_zoom, .max = 1 }); + moveCamera(s, moved, in); + } else panBy(s, e.wheel() * 60, in); + }, + else => {}, + } + } +} + +/// What keys and the buttons on top do. +const Action = enum { record, play, forward, backward, start, end, open, delete, overview, a4_down, a4_up }; + +const keys = [_]struct { sk.Key, Action }{ + .{ .r, .record }, .{ .space, .play }, .{ .right, .forward }, .{ .left, .backward }, + .{ .home, .start }, .{ .end, .end }, .{ .o, .open }, .{ .delete, .delete }, + .{ .backspace, .delete }, .{ .@"0", .overview }, .{ .left_bracket, .a4_down }, .{ .right_bracket, .a4_up }, +}; + +/// Everything a touch screen cannot reach by key, as buttons (see +/// `Layout.button`). Labels change with what they would do. +const buttons = [_]Action{ .a4_up, .a4_down, .overview, .delete, .open, .backward, .play, .record }; +const button_width = 52; + +fn act(s: *State, a: Action, in: *const sk.Input) void { + const now = s.transport.value; + switch (a) { + .record => if (now == .recording) change(s, .stopped) else record(s), + .play => change(s, switch (now) { + .forward, .backward => .stopped, + .stopped, .recording => if (s.backward) .backward else .forward, + }), + .forward => change(s, .forward), + .backward => change(s, .backward), + .start => seekTo(s, 0), + .end => seekTo(s, 1), + .open => s.dialog.send({}), + .delete => if (now != .recording and s.takes.count() > 0) remove(s, in), + .overview => { + // The whole strip, or back to full size. + const l = Layout.of(in.width, in.height); + var cam = cameraOf(s, l); + cam.zoom = if (cam.zoom < 1) 1 else l.view.h / @max(World.of(s).height(l), 1); + moveCamera(s, cam, in); + s.follow = true; + }, + .a4_down => s.a4 = @max(s.a4 - 1, 400), + .a4_up => s.a4 = @min(s.a4 + 1, 480), + } +} + +/// Starts a new take, unless the strip is full. It takes the live view's +/// place, which the camera follows. +fn record(s: *State) void { + s.takes.appendBounded(.{ .id = s.next_id }) catch return; + s.next_id += 1; + s.selected = s.takes.count() - 1; + s.views[s.selected] = .{}; + s.follow = true; + change(s, .recording); +} + +fn change(s: *State, to: Transport) void { + if (to == .forward or to == .backward) s.backward = to == .backward; + s.transport.send(to); +} + +fn seekTo(s: *State, fraction: f32) void { + if (s.transport.value == .recording) return; + s.seek.send(std.math.clamp(fraction, 0, 1)); +} + +/// Selects a take and brings it into view. +fn select(s: *State, index: u32, in: *const sk.Input) void { + s.selected = index; + const l = Layout.of(in.width, in.height); + var cam = cameraOf(s, l); + const b = cam.toScreen(l.box(index).whole); + const view = l.view; + if (b.y < view.y) cam.y += view.y - b.y + 4; + if (b.y + b.h > view.y + view.h) cam.y -= b.y + b.h - (view.y + view.h) + 4; + moveCamera(s, cam, in); +} + +/// Moves the world down by `pixels` on screen. +fn panBy(s: *State, pixels: f32, in: *const sk.Input) void { + var cam = cameraOf(s, Layout.of(in.width, in.height)); + cam.y += pixels; + moveCamera(s, cam, in); +} + +/// Deletes the selected take and selects the one above it. Its file stays: +/// any keyframe or mark from before the delete brings the take back, and +/// neither the sketch nor the host can tell when none is left. +fn remove(s: *State, in: *const sk.Input) void { + const n = s.takes.count(); + const index = @min(s.selected, n - 1); + _ = s.takes.orderedRemove(index); + std.mem.copyForwards(View, s.views[index .. n - 1], s.views[index + 1 .. n]); + s.selected = index -| 1; + moveCamera(s, cameraOf(s, Layout.of(in.width, in.height)), in); +} + +fn fractionAt(v: View, timeline: sk.Rect, x: f32) f32 { + return v.lo + (x - timeline.x) / timeline.w * (v.hi - v.lo); +} + +/// Scales the view by `factor` around `at`, keeping it inside the take. +fn zoom(v: *View, at: f32, factor: f32) void { + const span = std.math.clamp((v.hi - v.lo) * factor, 1e-6, 1); + var lo = at - (at - v.lo) / (v.hi - v.lo) * span; + lo = std.math.clamp(lo, 0, 1 - span); + v.lo = lo; + v.hi = lo + span; +} + +/// `open PATH` adds a file as a take, writing its samples to the take's +/// file; `open` alone shows the dialog. `next-id N` moves the counter +/// (`draw` sends it). +pub fn command(s: *State, ss: *Session, c: *sk.Console, line: []const u8) !bool { + var words = std.mem.tokenizeAny(u8, line, " \t"); + const verb = words.next() orelse return false; + if (std.mem.eql(u8, verb, "next-id")) { + s.next_id = try std.fmt.parseInt(u32, words.next() orelse return error.ExpectedId, 10); + return true; + } + if (!std.mem.eql(u8, verb, "open")) return false; + const path = std.mem.trim(u8, words.rest(), " \t"); + if (path.len == 0) { + s.dialog.send({}); + return true; + } + if (s.takes.count() >= max_takes) return error.TooManyTakes; + const samples = c.host.samples(c.host.loadSound(path)); + if (samples.len == 0) return error.CannotOpen; + // Recording grows the last take: stop before this one goes after it. + if (s.transport.value == .recording) change(s, .stopped); + const kept = samples[0..@min(samples.len, max_seconds * c.in.sample_rate)]; + var take: Take = .{ .id = s.next_id }; + const base = std.fs.path.basename(path); + take.source_len = @intCast(@min(base.len, take.source.len)); + @memcpy(take.source[0..take.source_len], base[0..take.source_len]); + // Into Session now, so the web, which keeps no files, has it too. + try ss.tape.appendSlice(c.session, kept); + try ss.clips.appendBounded(.{ .id = take.id, .start = ss.tape.items.len - kept.len, .len = kept.len }); + var file: [32]u8 = undefined; + _ = c.host.writeFile(try std.fmt.bufPrint(&file, "take-{d}.f32", .{take.id}), std.mem.sliceAsBytes(kept)); + try s.takes.appendBounded(take); + s.next_id += 1; + s.selected = s.takes.count() - 1; + s.views[s.selected] = .{}; + s.follow = true; + change(s, .stopped); + c.print("opened {s}: {d:.1} s", .{ path, @as(f64, @floatFromInt(kept.len)) / @as(f64, @floatFromInt(c.in.sample_rate)) }); + return true; +} + +// --------------------------------------------------------------------------- +// Draw: the takes, the microphone, the speakers and the GPU. + +pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { + const in = f.in; + const rate: f64 = @floatFromInt(in.sample_rate); + const dt: f64 = @min(@as(f64, @floatFromInt(in.dt)) / std.time.ns_per_s, 0.1); + // As big as the window: the layout follows (see `Layout`). + f.canvas(@intFromFloat(@max(in.window_width, min_width)), @intFromFloat(@max(in.window_height, min_height))); + f.clear(0x0b0c0f); + + if (ss.dialog.take(s.dialog) != null) f.openFile("open", .{ .pattern = "wav;flac;ogg" }); + // Files are written live, never while a recording of the session plays + // back: like sound, a write is not replayed. + const writing = in.mode == .live or in.mode == .recording; + if (in.jumped and writing) { + // State came from elsewhere: the next id's file may hold samples. + var id = s.next_id; + while (f.host.readFile(f.host.appendFile(f.format("take-{d}.f32", .{id}), &.{})).len > 0) id += 1; + if (id != s.next_id) f.run(f.format("next-id {d}", .{id})); + } + // Requests from State. Any of them, or another take selected, flushes + // what is queued, so the cursor restarts from what was heard. + const transport = ss.transport.take(s.transport); + const seek = ss.seek.take(s.seek); + const takes = s.takes.constSlice(); + // A take that begins here (not one a jump brought) starts its file + // empty, whatever a file of its id held. + if (transport == .recording and writing and !in.jumped and takes.len > 0) _ = f.host.writeFile(f.format("take-{d}.f32", .{takes[takes.len - 1].id}), &.{}); + readTakes(s, ss, f); + const selected: ?*Clip = if (takes.len > 0) clipOf(ss, takes[@min(s.selected, takes.len - 1)].id) else null; + var queued: usize = in.stream(stream).queued; + if (selected) |t| { + if (transport != null or seek != null or t.id != ss.playing) { + f.audio(stream, &.{}, .{ .clear = true }); + queued = 0; + ss.playing = t.id; + ss.cursor = @intFromFloat(std.math.clamp(@round(t.heard), 0, @as(f64, @floatFromInt(t.len)))); + } + if (transport) |to| { + switch (to) { + .forward => if (ss.cursor >= t.len) { + ss.cursor = 0; + }, + .backward => if (ss.cursor == 0) { + ss.cursor = t.len; + }, + .stopped, .recording => {}, + } + t.heard = @floatFromInt(ss.cursor); + } + if (seek) |fraction| if (s.transport.value != .recording) { + ss.cursor = @intFromFloat(@round(fraction * @as(f32, @floatFromInt(t.len)))); + t.heard = @floatFromInt(ss.cursor); + }; + } + + // The microphone is always open: the live view shows it, and a + // recording keeps it. + f.microphone(); + ss.live.push(in.microphone); + switch (s.transport.value) { + // Into the last take, at the tape's end. + .recording => if (takes.len > 0 and writing) if (clipOf(ss, takes[takes.len - 1].id)) |t| if (t.start + t.len == ss.tape.items.len) { + const room = max_seconds * in.sample_rate -| t.len; + const heard = in.microphone[0..@min(in.microphone.len, room)]; + ss.tape.appendSlice(f.session, heard) catch f.host.log("out of memory: recording stopped"); + _ = f.host.appendFile(f.format("take-{d}.f32", .{t.id}), std.mem.sliceAsBytes(heard)); + t.len = ss.tape.items.len - t.start; + t.heard = @floatFromInt(t.len); + }, + .forward, .backward => if (selected) |t| play(ss, t, ss.tape.items[t.start..][0..t.len], f, s.transport.value == .forward, queued, rate, dt), + .stopped => {}, + } + + // The tuner's readbacks, a frame or two old. + if (selected) |t| if (t.id != ss.tuned) { + ss.tuned = t.id; + ss.tuners[@intFromEnum(Tuned.take)] = .{}; + }; + for (in.downloads) |d| { + if (d.key >= ss.tuners.len or d.bytes.len < @sizeOf(tuner_layouts.Pitch)) continue; + ss.tuners[d.key].feed(std.mem.bytesToValue(tuner_layouts.Pitch, d.bytes[0..@sizeOf(tuner_layouts.Pitch)]), s.a4, @floatCast(dt)); + } + + if (!f.host.gpu()) { + f.text(16, Layout.header, 2, "micscope needs a GPU: the host has none.", 0xff8080); + return; + } + render(s, ss, f, rate, dt); +} + +/// Session's tape follows State's takes: a take State no longer has leaves +/// it, and one it lacks is read from its file (a new one's is empty). +fn readTakes(s: *const State, ss: *Session, f: *sk.Frame) void { + var i: u32 = 0; + while (i < ss.clips.count()) { + const t = ss.clips.buffer[i]; + if (for (s.takes.constSlice()) |take| { + if (take.id == t.id) break true; + } else false) { + i += 1; + continue; + } + _ = ss.clips.orderedRemove(i); + const tape = ss.tape.items; + std.mem.copyForwards(f32, tape[t.start .. tape.len - t.len], tape[t.start + t.len ..]); + ss.tape.shrinkRetainingCapacity(tape.len - t.len); + for (ss.clips.slice()[i..]) |*later| later.start -= t.len; + ss.gpu.uploaded = @min(ss.gpu.uploaded, t.start); + for (&ss.gpu.panels) |*p| if (p.id == t.id) { + p.id = 0; + p.forget(); + }; + } + for (s.takes.constSlice()) |take| if (clipOf(ss, take.id) == null) { + const bytes = f.host.readFile(f.host.appendFile(f.format("take-{d}.f32", .{take.id}), &.{})); + const samples = std.mem.bytesAsSlice(f32, @as([]align(4) const u8, @alignCast(bytes[0 .. bytes.len / 4 * 4]))); + ss.tape.appendSlice(f.session, samples) catch return f.host.log("out of memory: takes not read"); + ss.clips.appendBounded(.{ .id = take.id, .start = ss.tape.items.len - samples.len, .len = samples.len }) catch return; + }; +} + +fn clipOf(ss: *Session, id: u32) ?*Clip { + for (ss.clips.slice()) |*t| if (t.id == id) return t; + return null; +} + +/// Keeps the stream `lead_seconds` ahead, reading the take either way, and +/// follows the listener. +fn play(ss: *Session, t: *Clip, tape: []const f32, f: *sk.Frame, forward: bool, queued: usize, rate: f64, dt: f64) void { + const lead: usize = @intFromFloat(rate * lead_seconds); + const want = lead -| queued; + // What is heard now: the cursor, less what is queued in front of it. + const measured: f64 = @floatFromInt(if (forward) ss.cursor -| queued else @min(ss.cursor + queued, tape.len)); + if (forward) { + const n = @min(want, tape.len -| ss.cursor); + f.audio(stream, tape[@min(ss.cursor, tape.len)..][0..n], .{}); + ss.cursor += n; + } else { + const n = @min(want, ss.cursor); + const reversed = f.scratch.alloc(f32, n) catch return; + for (reversed, 0..) |*sample, i| sample.* = tape[ss.cursor - 1 - i]; + f.audio(stream, reversed, .{}); + ss.cursor -= n; + } + // The queue drains in device-sized blocks: advance by the clock and + // pull gently towards the measurement, snapping when far off. + const direction: f64 = if (forward) 1 else -1; + t.heard += direction * dt * rate; + const off = measured - t.heard; + t.heard = if (@abs(off) > rate * 0.25) measured else t.heard + off * 0.15; + t.heard = std.math.clamp(t.heard, 0, @as(f64, @floatFromInt(tape.len))); +} + +// --------------------------------------------------------------------------- +// GPU: buffers, compute, passes. + +const tuner_difference_kernel: gpu.Kernel = .{ + .code = shaders.tuner_difference_comp, + .readonly_buffers = 1, + .readwrite_buffers = 1, + .uniform_buffers = 1, + .threads = .{ 256, 1, 1 }, +}; +const tuner_kernel: gpu.Kernel = .{ + .code = shaders.tuner_comp, + .readonly_buffers = 2, + .readwrite_buffers = 1, + .uniform_buffers = 1, + .threads = .{ 256, 1, 1 }, +}; + +fn setup(g: *Gpu, f: *sk.Frame) void { + if (g.ready) return; + g.ready = true; + g.scratch = f.host.newBuffer(layouts.fft_size * 2 * @sizeOf([2]f32)); + for (&g.pitches) |*b| b.* = f.host.newBuffer(@sizeOf(tuner_layouts.Pitch)); + for (&g.differences) |*b| b.* = f.host.newBuffer(tuner_layouts.tuner_lags * @sizeOf(f32)); +} + +fn makeBuffers(p: *Panel, f: *sk.Frame) void { + if (p.wave_columns != .none) return; + p.wave_columns = f.host.newBuffer(layouts.max_columns * @sizeOf([4]f32)); + p.spectrum_columns = f.host.newBuffer(layouts.max_columns * @sizeOf([2]f32)); + p.levels = f.host.newBuffer(layouts.max_columns * spectrogram_height * @sizeOf(f32)); +} + +/// The panel for take `id`: the one it had, or the least recently used. +fn panelFor(g: *Gpu, f: *sk.Frame, id: u32) *Panel { + const p = for (&g.panels) |*p| { + if (p.id == id) break p; + } else reuse: { + var oldest = &g.panels[0]; + for (&g.panels) |*p| if (p.used < oldest.used) { + oldest = p; + }; + oldest.id = id; + oldest.forget(); + break :reuse oldest; + }; + p.used = f.in.frame; + makeBuffers(p, f); + return p; +} + +/// An image of `size`, (re)made when the size changes. +fn target(f: *sk.Frame, image: *sk.Image, size: *[2]u32, r: sk.Rect) [2]u32 { + const want: [2]u32 = .{ @intFromFloat(@min(r.w, layouts.max_columns)), @intFromFloat(@min(r.h, spectrogram_height)) }; + if (image.* == .none or size[0] != want[0] or size[1] != want[1]) { + if (image.* != .none) f.host.freeImage(image.*); + image.* = f.host.newImage(want[0], want[1]); + size.* = want; + } + return want; +} + +/// Puts the tape on the GPU: new samples only, unless the buffer had to grow. +fn sync(g: *Gpu, f: *sk.Frame, tape: []const f32) void { + if (tape.len > g.capacity or g.tape == .none) { + if (g.tape != .none) f.host.freeBuffer(g.tape); + g.capacity = @max(tape.len, g.capacity * 2, 60 * f.in.sample_rate); + g.tape = f.host.newBuffer(@intCast(g.capacity * @sizeOf(f32))); + g.uploaded = 0; + } + g.uploaded = @min(g.uploaded, tape.len); + if (g.uploaded < tape.len) { + f.upload(g.tape, @intCast(g.uploaded * @sizeOf(f32)), std.mem.sliceAsBytes(tape[g.uploaded..])); + g.uploaded = tape.len; + } +} + +fn render(s: *const State, ss: *Session, f: *sk.Frame, rate: f64, dt: f64) void { + const g = &ss.gpu; + setup(g, f); + sync(g, f, ss.tape.items); + // New code may mean new shaders: make everything again (the live view + // sees to its own). + if (f.in.reloaded) for (&g.panels) |*p| p.forget(); + const takes = s.takes.constSlice(); + const selected = if (takes.len > 0) @min(s.selected, takes.len - 1) else 0; + const recording = s.transport.value == .recording; + const world: World = .of(s); + const l = Layout.of(f.in.width, f.in.height); + const cam = cameraOf(s, l); + const view = l.view; + // Images are made at world size; the camera only scales them. + const size = l.box(0); + + f.clip(view); + // The live view, in the world's last slot. It keeps up even while it is + // hidden: drawn clipped away while a recording stands in for it. + const live = l.box(world.takes); + if (!world.live) f.clip(.{ .x = 0, .y = 0, .w = 0, .h = 0 }); + ss.live.draw(f, .{ .seconds = live_seconds, .wave = live.wave, .spectrogram = live.spectrogram, .spectrum = live.spectrum, .gain = s.gain, .camera = cam }); + f.clip(view); + // The tuner: on the microphone's newest sound, and on the selected take + // around its playhead. + const reach = tuner_layouts.tuner_window + tuner_layouts.tuner_lags; + const last = if (recording and takes.len > 0) clipOf(ss, takes[takes.len - 1].id) else null; + if (last) |t| { + tune(g, f, .input, g.tape, t.start, t.len, t.len, rate); + } else tune(g, f, .input, ss.live.gpu.tape, 0, scope.history, scope.history, rate); + if (!recording and takes.len > 0) if (clipOf(ss, takes[selected].id)) |t| { + const end: usize = @intFromFloat(@max(t.heard, 0)); + tune(g, f, .take, g.tape, t.start, t.len, end + reach / 2, rate); + }; + for (0..world.takes) |slot| { + const k: u32 = @intCast(slot); + const b = l.box(k).toScreen(cam); + if (b.whole.y + b.whole.h <= view.y or b.whole.y >= view.y + view.h) continue; + const tint: layouts.Tint = if (recording and k + 1 == world.takes) .recording else .take; + drawTake(s, g, f, size, b, clipOf(ss, takes[k].id) orelse continue, s.views[k], k == selected, tint, rate, dt); + } + f.clip(null); + labels(s, ss, f, world, cam, rate); +} + +/// Finds the pitch of the `tuner_window` samples before `end` (of the +/// `samples` from `offset`), and reads it back. +fn tune(g: *Gpu, f: *sk.Frame, which: Tuned, tape: sk.Buffer, offset: usize, samples: usize, end: usize, rate: f64) void { + const result = g.pitches[@intFromEnum(which)]; + const difference = g.differences[@intFromEnum(which)]; + const params = gpu.uniform(f.scratch, tuner_layouts.TunerParams{ + .offset = @intCast(offset), + .end = @intCast(end), + .samples = @intCast(samples), + .rate = @floatCast(rate), + .min_freq = tuner_low, + .max_freq = tuner_high, + .threshold = 0.12, + }); + f.compute(.{ + .label = "tuner_difference.comp", + .kernel = tuner_difference_kernel, + .readonly = &.{tape}, + .readwrite = &.{difference}, + .uniforms = params, + // Match tuner_difference.comp's LAGS_PER_GROUP. + .groups = .{ (tuner_layouts.tuner_lags + 15) / 16, 1, 1 }, + }); + f.compute(.{ + .label = "tuner.comp", + .kernel = tuner_kernel, + .readonly = &.{ tape, difference }, + .readwrite = &.{result}, + .uniforms = params, + }); + f.download(result, .{ .size = @sizeOf(tuner_layouts.Pitch), .key = @intFromEnum(which) }); +} + +/// The pitches the tuner looks for: a five-string bass's low B to a +/// piccolo's top. +const tuner_low = 30.0; +const tuner_high = 4200.0; + +/// The spectrum's analysis of `tape`, then its pass. +fn spectrum(g: *Gpu, f: *sk.Frame, tape: sk.Buffer, p: *Panel, size: [2]u32, params: layouts.SpectrumParams, rate: f64) void { + f.compute(.{ + .label = "spectrum.comp", + .kernel = scope.kernels.spectrum, + .readonly = &.{tape}, + .readwrite = &.{ g.scratch, p.spectrum_columns }, + .uniforms = gpu.uniform(f.scratch, params), + }); + f.gpu(.{ + .label = "spectrum.frag", + .image = p.spectrum, + .width = size[0], + .height = size[1], + .clear = null, + .pipelines = &scope.pipelines.spectrum, + .draws = &.{.{ + .pipeline = 0, + .vertex_count = 3, + .fragment_uniforms = gpu.uniform(f.scratch, layouts.SpectrumView{ + .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) }, + .floor_db = scope.floor_db, + .ceil_db = 0, + .min_freq = scope.min_freq, + .max_freq = scope.maxFreq(rate), + }), + .fragment_storage = &.{p.spectrum_columns}, + }}, + }); +} + +fn drawTake(s: *const State, g: *Gpu, f: *sk.Frame, size: Box, b: Box, t: *const Clip, view: View, selected: bool, tint: layouts.Tint, rate: f64, dt: f64) void { + const p = panelFor(g, f, t.id); + + // Waveform columns, when what they show changed. + const wave = target(f, &p.wave, &p.wave_size, size.wave); + const span = visible(view, t.len, wave[0]); + const first: u32 = @intFromFloat(@floor(span[0])); + const spp: f32 = @floatCast((span[1] - span[0]) / @as(f64, @floatFromInt(wave[0]))); + const params: layouts.WaveParams = .{ + .offset = @intCast(t.start), + .first = first, + .samples = @intCast(t.len), + .columns = wave[0], + .spp = spp, + .slot = 0, + .ring = layouts.max_columns, + }; + const key = std.hash.Wyhash.hash(0, std.mem.asBytes(¶ms)); + if (key != p.wave_key) { + p.wave_key = key; + f.compute(.{ + .label = "waveform.comp", + .kernel = scope.kernels.waveform, + .readonly = &.{g.tape}, + .readwrite = &.{p.wave_columns}, + .uniforms = gpu.uniform(f.scratch, params), + .groups = .{ wave[0], 1, 1 }, + }); + } + + // The spectrogram over the same columns. + const spectrogram = target(f, &p.spectrogram, &p.spectrogram_size, size.spectrogram); + const analysis: layouts.SpectrogramParams = .{ + .offset = @intCast(t.start), + .first = first, + .samples = @intCast(t.len), + .columns = spectrogram[0], + .rows = spectrogram[1], + .spp = spp, + .rate = @floatCast(rate), + .min_freq = scope.min_freq, + .max_freq = scope.maxFreq(rate), + .slot = 0, + .ring = layouts.max_columns, + }; + const analysis_key = std.hash.Wyhash.hash(0, std.mem.asBytes(&analysis)); + if (analysis_key != p.spectrogram_key) { + p.spectrogram_key = analysis_key; + f.compute(.{ + .label = "spectrogram.comp", + .kernel = scope.kernels.spectrogram, + .readonly = &.{g.tape}, + .readwrite = &.{p.levels}, + .uniforms = gpu.uniform(f.scratch, analysis), + .groups = .{ spectrogram[0], 1, 1 }, + }); + } + + // The spectrum around the playhead, or of the latest while recording: + // every frame for the selected take, which plays and glides; the others + // only when it would change, and without smoothing. + const half: f64 = layouts.window / 2; + const len: f64 = @floatFromInt(t.len); + const end: f64 = if (tint == .recording) len else @min(t.heard + half, len + half); + const shown = target(f, &p.spectrum, &p.spectrum_size, size.spectrum); + var sparams: layouts.SpectrumParams = .{ + .offset = @intCast(t.start), + .end = @intFromFloat(@max(end, 0)), + .samples = @intCast(t.len), + .columns = shown[0], + .reset = 0, + .rate = @floatCast(rate), + .min_freq = scope.min_freq, + .max_freq = scope.maxFreq(rate), + .dt = 0, + }; + const spectrum_key = std.hash.Wyhash.hash(1, std.mem.asBytes(&sparams)); + if (selected or spectrum_key != p.spectrum_key) { + sparams.reset = @intFromBool(!selected or p.spectrum_key == 0); + sparams.dt = @floatCast(dt); + p.spectrum_key = spectrum_key; + spectrum(g, f, g.tape, p, shown, sparams, rate); + } + + const at: f32 = @floatCast((t.heard - span[0]) / (span[1] - span[0]) * @as(f64, @floatFromInt(wave[0]))); + const playhead: f32 = if (!selected or t.len == 0 or at < 0 or at > @as(f32, @floatFromInt(wave[0]))) -1 else at; + const wave_view: layouts.WaveView = .{ + .size = .{ @floatFromInt(wave[0]), @floatFromInt(wave[1]) }, + .playhead = playhead, + .gain = s.gain, + .tint = @intFromEnum(tint), + .scroll = 0, + .filled = wave[0], + }; + const wave_drawn = std.hash.Wyhash.hash(p.wave_key, std.mem.asBytes(&wave_view)); + if (wave_drawn != p.wave_drawn) { + p.wave_drawn = wave_drawn; + f.gpu(.{ + .label = "waveform.frag", + .image = p.wave, + .width = wave[0], + .height = wave[1], + .clear = null, + .pipelines = &scope.pipelines.wave, + .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, wave_view), .fragment_storage = &.{p.wave_columns} }}, + }); + } + const spectrogram_view: layouts.SpectrogramView = .{ + .size = .{ @floatFromInt(spectrogram[0]), @floatFromInt(spectrogram[1]) }, + .floor_db = scope.floor_db, + .ceil_db = -6, + .playhead = playhead, + .scroll = 0, + .filled = spectrogram[0], + }; + const spectrogram_drawn = std.hash.Wyhash.hash(p.spectrogram_key, std.mem.asBytes(&spectrogram_view)); + if (spectrogram_drawn != p.spectrogram_drawn) { + p.spectrogram_drawn = spectrogram_drawn; + f.gpu(.{ + .label = "spectrogram.frag", + .image = p.spectrogram, + .width = spectrogram[0], + .height = spectrogram[1], + .clear = null, + .pipelines = &scope.pipelines.spectrogram, + .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, spectrogram_view), .fragment_storage = &.{p.levels} }}, + }); + } + f.image(p.wave, b.wave, .{}); + f.image(p.spectrogram, b.spectrogram, .{}); + f.image(p.spectrum, b.spectrum, .{}); +} + +/// The samples a take's timeline spans, at least 1/32 of a sample per +/// column. +fn visible(v: View, len: usize, columns: u32) [2]f64 { + const total: f64 = @floatFromInt(len); + var lo = v.lo * total; + var hi = v.hi * total; + const least = @as(f64, @floatFromInt(columns)) / 32; + if (hi - lo < least) { + const mid = (lo + hi) / 2; + lo = @max(mid - least / 2, 0); + hi = lo + least; + } + return .{ lo, hi }; +} + +// --------------------------------------------------------------------------- +// Text. + +const dim = 0x7a8090; +const bright = 0xe6e9ef; +const red = 0xff5a5a; +const green = 0x6fdc8c; +const yellow = 0xffd84d; + +fn labels(s: *const State, ss: *Session, f: *sk.Frame, world: World, cam: sk.Camera, rate: f64) void { + const takes = s.takes.constSlice(); + const recording = s.transport.value == .recording; + const selected = if (takes.len > 0) @min(s.selected, takes.len - 1) else 0; + + // What the transport is doing, to which take. + const status: struct { []const u8, u24 } = switch (s.transport.value) { + .recording => .{ "REC", red }, + .forward => .{ "PLAY >", 0x7fd7ff }, + .backward => .{ "PLAY <", 0x7fd7ff }, + .stopped => .{ "PAUSED", dim }, + }; + const l = Layout.of(f.in.width, f.in.height); + f.text(16, 10, 2, status[0], status[1]); + if (if (takes.len > 0) clipOf(ss, takes[selected].id) else null) |t| { + const times = .{ Time{ .seconds = t.heard / rate }, Time{ .seconds = @as(f64, @floatFromInt(t.len)) / rate } }; + // As much as fits beside the buttons (or, narrow, the corner). + const room = if (l.narrow) l.w - 16 * 7 else l.button(buttons.len - 1).x - 16; + if (room >= 480) { + f.print(140, 10, 2, bright, "{f} / {f}", times); + if (room >= 740) f.print(500, 14, 1, dim, "take {d} of {d}: {s}", .{ selected + 1, takes.len, source(&takes[selected]) }); + } else f.print(16 + 16 * 7, 14, 1, bright, "{f} / {f}", times); + } + for (buttons, 0..) |a, i| button(s, f, a, l.button(i)); + const corner = f.format("{s}{s}", .{ + if (s.gain != 1) f.format("gain x{d:.2} ", .{s.gain}) else "", + if (cam.zoom < 1) f.format("view x{d:.2}", .{cam.zoom}) else "", + }); + f.text(f.in.width - 16 - @as(f32, @floatFromInt(corner.len * 8)), if (l.narrow) 14 else f.in.height - 16, 1, corner, dim); + + const view = l.view; + f.clip(view); + for (0..world.slots()) |slot| { + const k: u32 = @intCast(slot); + const b = l.box(k).toScreen(cam); + if (b.whole.y + b.whole.h <= view.y or b.whole.y >= view.y + view.h) continue; + // Centred in the band above the waveform, whatever the zoom. + const title_y = @round(b.whole.y + (b.wave.y - b.whole.y - 8) / 2); + if (k == world.takes) { + f.print(b.whole.x, title_y, 1, green, "LIVE the microphone, the last {d} s", .{live_seconds}); + if (ss.live.heard == 0) f.text(b.wave.x + 8, b.wave.y + 8, 1, "nothing from the microphone yet", dim); + if (cam.zoom >= 0.8) axes(f, b, rate, null); + tunerOverlay(f, b.spectrum, ss.tuners[@intFromEnum(Tuned.input)].reading, s.a4, rate, cam.zoom >= 0.8); + continue; + } + const t = clipOf(ss, takes[k].id) orelse continue; + const seconds = Time{ .seconds = @as(f64, @floatFromInt(t.len)) / rate }; + if (recording and k + 1 == world.takes) { + f.print(b.whole.x, title_y, 1, red, "RECORDING take {d} {f}", .{ k + 1, seconds }); + } else { + const chosen = k == selected; + if (chosen) f.fill(.{ .x = b.whole.x - if (l.narrow) @as(f32, 6) else 12, .y = b.wave.y, .w = 3, .h = b.timeline.h }, yellow); + f.print(b.whole.x, title_y, 1, @as(u24, if (chosen) bright else dim), "#{d} {s} {f}", .{ k + 1, source(&takes[k]), seconds }); + } + if (cam.zoom >= 0.8) { + const span = visible(s.views[k], t.len, @intFromFloat(@min(l.box(0).wave.w, layouts.max_columns))); + axes(f, b, rate, .{ span[0] / rate, span[1] / rate }); + } + const which: ?Tuned = if (recording) (if (k + 1 == world.takes) .input else null) else if (k == selected) .take else null; + if (which) |w| tunerOverlay(f, b.spectrum, ss.tuners[@intFromEnum(w)].reading, s.a4, rate, cam.zoom >= 0.8); + } + f.clip(null); + if (world.takes == 0) f.text(16, view.y + view.h - 12, 1, "No takes yet: REC records one, OPEN opens a file.", dim); + // Keys, where there is a keyboard to press them. + if (!l.narrow) f.text(16, f.in.height - 16, 1, "R SPACE LEFT RIGHT HOME END O DEL 0 [ ] UP/DOWN take wheel or 2 fingers pan CTRL+wheel or pinch zoom", dim); +} + +/// The tuner on a spectrum: the pitch and its harmonics where they fall on +/// it, the span of two semitones either side shaded, and that span +/// magnified across the top: the nearest notes of the chromatic scale, +/// the pitch as a needle between them, and the note in cents and Hz. +fn tunerOverlay(f: *sk.Frame, r: sk.Rect, reading: ?tuner.Reading, a4: f32, rate: f64, detailed: bool) void { + const at = reading orelse return; + const max_freq = scope.maxFreq(rate); + const fade: u8 = @intFromFloat(255 * std.math.clamp(1 - at.quiet / tuner.hold_seconds, 0.2, 1)); + const cents = at.cents(); + const color = sk.Color.hex(if (@abs(cents) <= 5) green else if (@abs(cents) <= 15) yellow else 0xff8a4d).alpha(fade); + const x = struct { + fn of(rect: sk.Rect, hz: f32, ceiling: f32) f32 { + return rect.x + @log(hz / scope.min_freq) / @log(ceiling / scope.min_freq) * rect.w; + } + }.of; + + const freq = scope.hz(at.pitch, a4); + const lo = scope.hz(@as(f32, @floatFromInt(at.note)) - 2.5, a4); + const hi = scope.hz(@as(f32, @floatFromInt(at.note)) + 2.5, a4); + f.fill(.{ .x = x(r, lo, max_freq), .y = r.y, .w = @max(x(r, hi, max_freq) - x(r, lo, max_freq), 1), .h = r.h }, sk.Color.white.alpha(fade / 10)); + f.fill(.{ .x = @round(x(r, freq, max_freq)), .y = r.y, .w = 1, .h = r.h }, color); + // The harmonics the pitch accounts for. + for (2..9) |h| { + const hz = freq * @as(f32, @floatFromInt(h)); + if (hz > max_freq) break; + f.fill(.{ .x = @round(x(r, hz, max_freq)), .y = r.y + r.h - 10, .w = 1, .h = 10 }, color.alpha(fade / 2)); + } + if (!detailed) return; + + // The magnified scale: semitones `note - 2.5` to `note + 2.5`. + const strip: sk.Rect = .{ .x = r.x + 6, .y = r.y + 4, .w = r.w - 12, .h = 30 }; + const semitone = strip.w / 5; + const centre = strip.x + strip.w / 2; + f.fill(strip, sk.Color.hex(0x0b0c0f).alpha(220)); + var c: f32 = -250; + while (c <= 250) : (c += 10) { + const whole = @mod(c, 100) == 0; + const h: f32 = if (whole) 10 else if (@mod(c, 50) == 0) 6 else 3; + f.fill(.{ .x = @round(centre + c / 100 * semitone), .y = strip.y + strip.h - h, .w = 1, .h = h }, sk.Color.hex(if (c == 0) bright else dim).alpha(if (whole) 220 else 120)); + } + for (0..5) |i| { + const note = at.note - 2 + @as(i32, @intCast(i)); + const label = f.format("{s}{d}", .{ tuner.name(note), tuner.octave(note) }); + const lx = centre + (@as(f32, @floatFromInt(i)) - 2) * semitone - @as(f32, @floatFromInt(label.len * 4)); + f.text(lx, strip.y + 4, 1, label, if (note == at.note) color else sk.Color.hex(dim).alpha(fade)); + } + const needle = std.math.clamp(centre + cents / 100 * semitone, strip.x, strip.x + strip.w - 2); + f.fill(.{ .x = @round(needle) - 1, .y = strip.y + 14, .w = 3, .h = strip.h - 14 }, color); + + // The reading. + const big = f.format("{s}{d}", .{ tuner.name(at.note), tuner.octave(at.note) }); + const panel: sk.Rect = .{ .x = r.x + r.w - 6 - 124, .y = strip.y + strip.h + 4, .w = 124, .h = 50 }; + f.fill(panel, sk.Color.hex(0x0b0c0f).alpha(200)); + f.text(panel.x + 6, panel.y + 4, 3, big, color); + const whole = @round(cents); + f.print(panel.x + 6, panel.y + 30, 1, sk.Color.hex(bright).alpha(fade), "{s}{d:.0} cents", .{ if (whole > 0) "+" else "", if (whole == 0) 0 else whole }); + f.print(panel.x + 6, panel.y + 40, 1, sk.Color.hex(dim).alpha(fade), "{d:.2} Hz", .{freq}); + f.print(panel.x + 70, panel.y + 4, 1, sk.Color.hex(dim).alpha(fade), "A4 {d:.0}", .{a4}); +} + +fn button(s: *const State, f: *sk.Frame, a: Action, r: sk.Rect) void { + const now = s.transport.value; + const label: []const u8, const lit: ?u24 = switch (a) { + .record => if (now == .recording) .{ "STOP", red } else .{ "REC", null }, + .play => if (now == .forward or now == .backward) .{ "PAUSE", 0x7fd7ff } else .{ "PLAY", null }, + .backward => .{ "REV", if (now == .backward) 0x7fd7ff else null }, + .open => .{ "OPEN", null }, + .delete => .{ "DEL", null }, + .overview => .{ "ALL", null }, + .a4_down => .{ "A4-", null }, + .a4_up => .{ "A4+", null }, + .forward, .start, .end => .{ "", null }, + }; + f.fill(r, sk.Color.hex(if (lit) |c| c else 0x1c2029).alpha(if (lit != null) 70 else 255)); + f.fill(.{ .x = r.x, .y = r.y + r.h - 1, .w = r.w, .h = 1 }, sk.Color.hex(lit orelse 0x2e3440)); + f.text(r.x + (r.w - @as(f32, @floatFromInt(label.len * 8))) / 2, r.y + 9, 1, label, sk.Color.hex(lit orelse bright)); +} + +fn source(t: *const Take) []const u8 { + return if (t.source_len > 0) t.source[0..t.source_len] else "microphone"; +} + +/// A panel's scales: its time span (seconds; null for the live view, which +/// ends now) under the spectrogram, frequencies up the spectrogram, and +/// frequency and level on the spectrum. +fn axes(f: *sk.Frame, b: Box, rate: f64, span: ?[2]f64) void { + const below = b.spectrogram.y + b.spectrogram.h + 4; + const left = if (span) |sp| f.format("{f}", .{Time{ .seconds = sp[0] }}) else f.format("-{d} s", .{live_seconds}); + const right = if (span) |sp| f.format("{f}", .{Time{ .seconds = sp[1] }}) else "now"; + f.text(b.wave.x, below, 1, left, dim); + f.text(b.wave.x + b.wave.w - @as(f32, @floatFromInt(right.len * 8)), below, 1, right, dim); + + const max_freq = scope.maxFreq(rate); + for ([_]f64{ 100, 1000, 10000 }) |hz| { + const y = b.spectrogram.y + b.spectrogram.h * (1 - @as(f32, @floatCast(@log(hz / scope.min_freq) / @log(max_freq / scope.min_freq)))); + f.fill(.{ .x = b.spectrogram.x, .y = y, .w = 6, .h = 1 }, sk.Color.white.alpha(140)); + f.text(b.spectrogram.x + 9, y - 4, 1, hzLabel(f, hz), sk.Color.white.alpha(170)); + } + + for ([_]f64{ 50, 200, 1000, 5000, 20000 }) |hz| { + if (hz > max_freq) continue; + const x = b.spectrum.x + @as(f32, @floatCast(@log(hz / scope.min_freq) / @log(max_freq / scope.min_freq))) * b.spectrum.w; + f.text(@min(x + 3, b.spectrum.x + b.spectrum.w - 24), b.spectrum.y + b.spectrum.h + 4, 1, hzLabel(f, hz), dim); + } + var db: f32 = -24; + while (db > scope.floor_db) : (db -= 24) { + const y = b.spectrum.y + b.spectrum.h * (db / scope.floor_db); + f.print(b.spectrum.x + 4, y + 3, 1, dim, "{d}", .{db}); + } +} + +fn hzLabel(f: *sk.Frame, hz: f64) []const u8 { + return if (hz >= 1000) f.format("{d}k", .{hz / 1000}) else f.format("{d}", .{hz}); +} + +const Time = struct { + seconds: f64, + + pub fn format(t: Time, w: *std.Io.Writer) std.Io.Writer.Error!void { + const ms: u64 = @intFromFloat(@max(t.seconds, 0) * 1000); + try w.print("{d}:{d:0>2}.{d:0>3}", .{ ms / 60_000, ms / 1000 % 60, ms % 1000 }); + } +}; + +test "zoom keeps the view inside the take and around the pointer" { + var v: View = .{}; + zoom(&v, 0.5, 0.5); + try std.testing.expectApproxEqAbs(@as(f32, 0.25), v.lo, 1e-6); + try std.testing.expectApproxEqAbs(@as(f32, 0.75), v.hi, 1e-6); + zoom(&v, 0.0, 4); + try std.testing.expectEqual(@as(f32, 0), v.lo); + try std.testing.expectEqual(@as(f32, 1), v.hi); +} + +test "the world stacks takes in order with the live view last" { + const world: World = .{ .takes = 3, .live = true }; + try std.testing.expectEqual(@as(u32, 4), world.slots()); + const l = Layout.of(width, height); + try std.testing.expectEqual(@as(?u32, 0), world.takeAt(l, .{ .x = 10, .y = 1 })); + try std.testing.expectEqual(@as(?u32, 2), world.takeAt(l, .{ .x = 10, .y = 2 * l.panel_height + 1 })); + try std.testing.expectEqual(@as(?u32, null), world.takeAt(l, .{ .x = 10, .y = 3 * l.panel_height + 1 })); +} + +test "the camera follows the bottom and stays inside the world, wide or narrow" { + for ([_]Layout{ .of(width, height), .of(390, 844) }) |l| { + var s: State = .{}; + s.takes.len = 5; + const cam = cameraOf(&s, l); + // Following: the live view's bottom sits at the viewport's bottom. + try std.testing.expectApproxEqAbs(l.view.y + l.view.h - 4, cam.toScreen(l.box(5).whole).y + l.panel_height, 1); + s.follow = false; + s.camera.y = 1e6; + try std.testing.expectApproxEqAbs(l.view.y + 4, cameraOf(&s, l).toScreen(l.box(0).whole).y, 1); + } +} + +test "a narrow layout stacks the spectrum under the timeline, inside the panel" { + const l: Layout = .of(390, 844); + try std.testing.expect(l.narrow); + const b = l.box(0); + try std.testing.expect(b.spectrum.y > b.timeline.y + b.timeline.h); + try std.testing.expect(b.spectrum.y + b.spectrum.h + 16 <= b.whole.y + b.whole.h); + try std.testing.expectEqual(l.world_width, b.timeline.w); + // Buttons fill the bottom, inside the canvas, without overlapping. + for (0..buttons.len) |i| { + const r = l.button(i); + try std.testing.expect(r.x >= 0 and r.x + r.w <= l.w and r.y + r.h <= l.h); + try std.testing.expect(r.w >= 40); + } +} + +test { + _ = tuner; +} + |
