summaryrefslogtreecommitdiff
path: root/examples/micscope/src/tuner.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 00:19:38 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitc37f1b3b1374df2a2a434d16cd772a4bb00c567e (patch)
tree4feccf16cf1f6808bae6ba26602705d557ab2fb7 /examples/micscope/src/tuner.zig
parent9bd77e54943d954291b5faef287ccca5dcebd7a4 (diff)
downloadsketchbook-c37f1b3b1374df2a2a434d16cd772a4bb00c567e.tar.gz
sketchbook-c37f1b3b1374df2a2a434d16cd772a4bb00c567e.zip
micscope: on the scope module, its copy of the scope code gone
micscope carried the code the scope module was lifted from: seven shaders byte for byte, its layouts but for a comment, and a private live view (Live, analyseLive, drawLive) that was Scope.push, scroll and spectrum. Now the live view is ss.live, a scope.Scope, pushed the microphone and drawn with the panel's world rects and the camera (Views.camera), every frame, clipped away while a recording stands in for it so its columns keep up as before; the tuner reads its history from ss.live.gpu.tape. The take panels run scope.kernels and scope.pipelines over the tape with scope.layouts' blocks, wavePass and spectrogramPass inlined at their one use; maxFreq, min_freq and floor_db are the scope's. src/layouts.zig keeps only the tuner's blocks, the build compiles only the tuner's two shaders, and the tuner names notes with scope.midi, scope.hz and scope.note_names. Takes, transport and tuner are as they were. 1003 lines out, 91 in. Tests: zig build test, 67/67 steps, 10/10 tests (the live history's own test went with it; the scope module tests the same). Pictures: the four scripts' 18 screenshots, captured before and after with the same scripts and compared pixel by pixel (two runs before differ by themselves wherever wall-clock playback or microphone timing shows: the playheads, how far the live view has filled). The one difference not of that kind is the live spectrum: micscope made every image at most 132 rows, so its 184-row spectrum was stretched from 132; the scope makes it at its size, so the live spectrum's lines come out crisp (at most 86 in a channel, inside its rect). The take spectra keep micscope's 132. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples/micscope/src/tuner.zig')
-rw-r--r--examples/micscope/src/tuner.zig25
1 files changed, 5 insertions, 20 deletions
diff --git a/examples/micscope/src/tuner.zig b/examples/micscope/src/tuner.zig
index 92a8482..159798b 100644
--- a/examples/micscope/src/tuner.zig
+++ b/examples/micscope/src/tuner.zig
@@ -10,10 +10,9 @@
//! once the pitch is 60 cents from it, so a note held a quarter tone sharp
//! does not flicker between two names.
const std = @import("std");
+const scope = @import("scope");
const layouts = @import("layouts");
-pub const names = [12][]const u8{ "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
-
/// Estimates clearer than this are a note (YIN's threshold is 0.12, so
/// clarity 0.88 at least when its first dip was found).
pub const min_clarity = 0.85;
@@ -22,17 +21,8 @@ pub const min_level = -60.0;
/// How long the last note stays (shown as fading) after the sound stops.
pub const hold_seconds = 1.0;
-/// Fractional MIDI number of `freq`.
-pub fn midi(freq: f32, a4: f32) f32 {
- return 69 + 12 * std.math.log2(freq / a4);
-}
-
-pub fn frequency(m: f32, a4: f32) f32 {
- return a4 * std.math.pow(f32, 2, (m - 69) / 12);
-}
-
pub fn name(note: i32) []const u8 {
- return names[@intCast(@mod(note, 12))];
+ return scope.note_names[@intCast(@mod(note, 12))];
}
pub fn octave(note: i32) i32 {
@@ -67,7 +57,7 @@ pub const Tracker = struct {
}
return;
}
- const m = midi(p.freq, a4);
+ const m = scope.midi(p.freq, a4);
if (t.count > 0 and @abs(m - t.median()) > 1) t.count = 0;
if (t.count == t.recent.len) {
std.mem.copyForwards(f32, t.recent[0 .. t.recent.len - 1], t.recent[1..]);
@@ -97,21 +87,16 @@ fn pitchOf(freq: f32) layouts.Pitch {
}
test "notes and octaves on the chromatic scale" {
- try std.testing.expectApproxEqAbs(@as(f32, 69), midi(440, 440), 1e-4);
- try std.testing.expectApproxEqAbs(@as(f32, 40), midi(82.4069, 440), 1e-3);
try std.testing.expectEqualStrings("E", name(40));
try std.testing.expectEqual(@as(i32, 2), octave(40));
try std.testing.expectEqualStrings("B", name(23));
try std.testing.expectEqual(@as(i32, 0), octave(23));
- try std.testing.expectApproxEqAbs(@as(f32, 261.626), frequency(60, 440), 1e-2);
- // Another reference pitch moves every note with it.
- try std.testing.expectApproxEqAbs(@as(f32, 69), midi(442, 442), 1e-4);
}
test "a held note settles, in cents from it" {
var t: Tracker = .{};
// A4, 12 cents sharp.
- const sharp = frequency(69.12, 440);
+ const sharp = scope.hz(69.12, 440);
for (0..20) |_| t.feed(pitchOf(sharp), 440, 1.0 / 60.0);
const r = t.reading.?;
try std.testing.expectEqual(@as(i32, 69), r.note);
@@ -132,7 +117,7 @@ test "a new note shows at once, a quarter tone does not flicker" {
for (0..3) |_| t.feed(pitchOf(146.83), 440, 1.0 / 60.0);
try std.testing.expectEqual(@as(i32, 50), t.reading.?.note);
// 55 cents sharp of D3 stays D3.
- for (0..30) |_| t.feed(pitchOf(frequency(50.55, 440)), 440, 1.0 / 60.0);
+ for (0..30) |_| t.feed(pitchOf(scope.hz(50.55, 440)), 440, 1.0 / 60.0);
try std.testing.expectEqual(@as(i32, 50), t.reading.?.note);
}