summaryrefslogtreecommitdiff
path: root/examples/micscope/src/tuner.zig
diff options
context:
space:
mode:
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);
}