1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
#version 450
#extension GL_GOOGLE_include_directive : require
// The pitch of the newest (or the playhead's) sound, by YIN (de Cheveigné
// and Kawahara, 2002), in one workgroup. Picking the strongest FFT peak
// finds the loudest harmonic, which is often not the note: low strings,
// voices and brass put more energy in the 2nd or 3rd harmonic, and a
// missing fundamental has no peak at all. YIN looks for the period instead:
//
// 1. the difference function d(tau) = sum over the window of
// (x[j] - x[j + tau])^2, which dips to 0 at every multiple of the
// period, whatever the harmonics' balance;
// 2. normalised by its running mean, d'(tau) = d(tau) * tau / sum d(1..tau),
// which starts at 1 and removes the dip-at-zero bias, so the dips are
// comparable;
// 3. the first dip below an absolute threshold, then the bottom of it: the
// shortest period that explains the signal, which keeps it from
// dropping an octave onto a multiple of the period;
// 4. a parabola through that dip of d and its neighbours, for a period
// between samples: the semitone is 6 %, the cent 0.06 %, and whole samples
// alone would be 30 cents apart at 1 kHz.
layout(local_size_x = 256) in;
#include "TunerParams.glsl"
layout(set = 0, binding = 0) readonly buffer Tape { float tape[]; };
// d(tau), from tuner_difference.comp, which does the bulk of the work.
layout(set = 0, binding = 1) readonly buffer Difference { float difference[]; };
// freq, clarity, level, period: layouts.Pitch.
layout(set = 1, binding = 0) buffer Result { vec4 result; };
// Match layouts.zig.
const uint W = 2048;
const uint LAGS = 1700;
const uint THREADS = 256;
// Each thread owns CHUNK consecutive lags.
const uint CHUNK = (LAGS + THREADS - 1u) / THREADS;
shared float d[THREADS * CHUNK];
// d before normalising: the parabola goes through these, since the
// normalisation's factor tau tilts a dip by more than a cent at high pitch.
shared float raw[THREADS * CHUNK];
shared float squares[THREADS];
shared float sums[THREADS];
shared uint first_dip;
shared float deepest[THREADS];
shared uint deepest_at[THREADS];
void main() {
uint t = gl_LocalInvocationID.x;
uint tau_max = min(LAGS - 2u, uint(ceil(u.rate / u.min_freq)));
uint tau_min = max(2u, uint(floor(u.rate / u.max_freq)));
// 1. The difference function (tuner_difference.comp), and the window's
// energy.
int first = int(u.end) - int(W);
float sq = 0.0;
for (uint j = t; j < W; j += THREADS) {
int at = first + int(j);
float v = at >= 0 && at < int(u.samples) ? tape[u.offset + uint(at)] : 0.0;
sq += v * v;
}
squares[t] = sq;
float own = 0.0;
for (uint i = 0u; i < CHUNK; i++) {
uint tau = t * CHUNK + i;
float v = tau >= 1u && tau <= tau_max + 1u ? difference[tau] : 0.0;
raw[tau] = v;
own += v;
}
sums[t] = own;
if (t == 0u) first_dip = 0xffffffffu;
barrier();
// 2. Normalised by the running sum: a scan of the threads' chunk sums
// (Hillis-Steele), then each chunk in order.
for (uint stride = 1u; stride < THREADS; stride <<= 1u) {
float add = t >= stride ? sums[t - stride] : 0.0;
barrier();
sums[t] += add;
barrier();
}
float running = t > 0u ? sums[t - 1u] : 0.0;
float low = 2.0;
uint low_at = 0u;
for (uint i = 0u; i < CHUNK; i++) {
uint tau = t * CHUNK + i;
running += raw[tau];
float v = tau == 0u ? 1.0 : (running > 0.0 ? raw[tau] * float(tau) / running : 1.0);
d[tau] = v;
if (tau >= tau_min && tau <= tau_max) {
// 3. The first dip under the threshold, and the deepest.
if (v < u.threshold) atomicMin(first_dip, tau);
if (v < low) {
low = v;
low_at = tau;
}
}
}
deepest[t] = low;
deepest_at[t] = low_at;
barrier();
for (uint stride = THREADS / 2u; stride > 0u; stride >>= 1u) {
if (t < stride && deepest[t + stride] < deepest[t]) {
deepest[t] = deepest[t + stride];
deepest_at[t] = deepest_at[t + stride];
}
barrier();
}
if (t != 0u) return;
float energy = 0.0;
for (uint i = 0u; i < THREADS; i++) energy += squares[i];
float level = energy > 0.0 ? 10.0 * log(energy / float(W)) / log(10.0) : -160.0;
// The first dip followed to its bottom; failing one, the deepest, which
// the CPU may still reject as unclear.
uint best = first_dip;
if (best == 0xffffffffu) {
best = max(deepest_at[0], tau_min);
} else {
while (best + 1u <= tau_max && d[best + 1u] < d[best]) best++;
}
// 4. The parabola through the dip.
float a = raw[best - 1u];
float b = raw[best];
float c = raw[best + 1u];
float bend = a - 2.0 * b + c;
float shift = bend > 0.0 ? clamp(0.5 * (a - c) / bend, -0.5, 0.5) : 0.0;
float period = float(best) + shift;
float depth = d[best];
bool silent = energy <= 0.0;
result = vec4(silent ? 0.0 : u.rate / period, silent ? 0.0 : clamp(1.0 - depth, 0.0, 1.0), level, period);
}
|