summaryrefslogtreecommitdiff
path: root/src/scope/shaders/spectrogram.comp
blob: b32f4c50808ac87bf7c9bd1f83b767e0621b28d2 (plain) (blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#version 450
#extension GL_GOOGLE_include_directive : require
// The spectrogram, one workgroup per column, the same columns as the
// waveform (waveform.comp), so the two line up in time.
//
// Two resolutions per column, because one window cannot serve both ends of
// a log axis: 2048 samples resolve 23 Hz, which is sharp in time and fine
// in the kilohertz, but leaves 20 to 100 Hz (a quarter of the height) to
// four bins. So the low end is analysed again from the signal low-passed
// and decimated to a quarter of the rate: the same 2048 points then span
// 170 ms and resolve 6 Hz. Rows take the long analysis below 250 Hz, the
// short one above 500 Hz, and blend in between.
//
// A single window's spectrum of noise scatters as much as its level (each
// bin is chi-squared with 2 degrees of freedom), which in dB shows as
// speckle full of black holes, and finer bins only make more of them. So
// the long analysis averages three windows a quarter window apart in time
// (Welch): steady lines stay as sharp, noise settles. Multitaper averaging
// was tried and measured against a reference (see test/README.md): sine
// tapers and DPSS both widen narrow lines into slabs and leak, which a
// 96 dB scale shows.
//
// Each analysis is a Blackman-Harris window zero-padded to 4096 points,
// transformed as 2048 complex points (even and odd samples as real and
// imaginary parts) by an in-place radix-2 FFT in shared memory, then split
// into the real signal's bins. Zoomed out, a column averages the power of
// up to four windows spread across its samples, so it neither skips sound
// nor flickers as the view moves; zoomed in, neighbouring windows overlap.
// Rows are log-spaced: a Catmull-Rom spline through the bins' levels where
// rows are finer than bins, the loudest bin where they are coarser.
layout(local_size_x = 256) in;

#include "SpectrogramParams.glsl"

layout(set = 0, binding = 0) readonly buffer Tape { float tape[]; };
// MAX_ROWS rows of MAX_COLUMNS levels, dBFS; row 0 is the lowest frequency.
layout(set = 1, binding = 0) buffer Levels { float levels[]; };

// Match layouts.zig.
const uint MAX_COLUMNS = 2048;
const uint W = 2048;
const uint N = 4096;
const uint HALF = N / 2;
const uint LOG2_HALF = 11;
const uint THREADS = 256;
// The long analysis: every DECIMATE-th sample of the low-passed signal.
const uint DECIMATE = 4;
// Its bins needed below the blend: 600 Hz at 12 kHz / 4096 is bin 205.
const uint LONG_BINS = 256;
// Windows the long analysis averages per column, a quarter window apart.
const uint WELCH = 3;
const float BLEND_LO = 250.0;
const float BLEND_HI = 500.0;
const float PI = 3.14159265358979;
const float SILENCE = -160.0;
// Columns past the end of the tape.
const float NO_TAPE = 1000.0;
const float A0 = 0.35875;
const float A1 = 0.48829;
const float A2 = 0.14128;
const float A3 = 0.01168;

shared vec2 z[HALF];
// Power per bin, summed over windows; then dBFS.
shared float short_bins[HALF + 1];
shared float long_bins[LONG_BINS];

float sample_at(int i) {
    return i >= 0 && i < int(u.samples) ? tape[u.offset + uint(i)] : 0.0;
}

// The signal low-passed for decimation: a triangle over 7 samples (two
// boxes of 4), whose zeros sit on 12 and 24 kHz, exactly where anything
// that would fold onto the low bins comes from.
float low_passed(int i) {
    return (sample_at(i - 3) + 2.0 * sample_at(i - 2) + 3.0 * sample_at(i - 1) + 4.0 * sample_at(i) + 3.0 * sample_at(i + 1) + 2.0 * sample_at(i + 2) + sample_at(i + 3)) / 16.0;
}

float blackman_harris(uint i) {
    float a = 2.0 * PI * float(i) / float(W - 1);
    return A0 - A1 * cos(a) + A2 * cos(2.0 * a) - A3 * cos(3.0 * a);
}

vec2 mul(vec2 a, vec2 b) {
    return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}

float windowed(int centre, uint i, bool long_window) {
    if (long_window) return low_passed(centre + (int(i) - int(W / 2)) * int(DECIMATE)) * blackman_harris(i);
    return sample_at(centre + int(i) - int(W / 2)) * blackman_harris(i);
}

// Adds the power spectrum of the window centred on sample `centre`.
void analyse(int centre, bool long_window) {
    uint t = gl_LocalInvocationID.x;
    // Even and odd samples packed as one complex sequence, in bit-reversed
    // order for the in-place transform. Past W it is all padding.
    for (uint i = t; i < HALF; i += THREADS) {
        vec2 v = vec2(0.0);
        if (2u * i < W) v = vec2(windowed(centre, 2u * i, long_window), windowed(centre, 2u * i + 1u, long_window));
        z[bitfieldReverse(i) >> (32u - LOG2_HALF)] = v;
    }
    barrier();
    for (uint s = 1u; s <= LOG2_HALF; s++) {
        uint half_size = 1u << (s - 1u);
        for (uint j = t; j < HALF / 2u; j += THREADS) {
            uint k = j & (half_size - 1u);
            uint i0 = ((j >> (s - 1u)) << s) + k;
            uint i1 = i0 + half_size;
            float angle = -PI * float(k) / float(half_size);
            vec2 b = mul(vec2(cos(angle), sin(angle)), z[i1]);
            vec2 a = z[i0];
            z[i0] = a + b;
            z[i1] = a - b;
        }
        barrier();
    }
    // The real signal's bins from the packed transform:
    // X[k] = E[k] + e^(-2 pi i k / N) O[k], with E and O the even and odd
    // samples' spectra, recovered from Z[k] and conj(Z[HALF - k]).
    uint count = long_window ? LONG_BINS : HALF + 1u;
    for (uint k = t; k < count; k += THREADS) {
        vec2 zk = z[k % HALF];
        vec2 zc = z[(HALF - k) % HALF];
        zc.y = -zc.y;
        vec2 even = 0.5 * (zk + zc);
        vec2 d = 0.5 * (zk - zc);
        vec2 odd = vec2(d.y, -d.x);
        float angle = -2.0 * PI * float(k) / float(N);
        vec2 x = even + mul(vec2(cos(angle), sin(angle)), odd);
        if (long_window) long_bins[k] += dot(x, x);
        else short_bins[k] += dot(x, x);
    }
    barrier();
}

float bin_level(uint k, bool long_window) {
    return long_window ? long_bins[min(k, LONG_BINS - 1u)] : short_bins[min(k, HALF)];
}

// The level of the row spanning bins `lo` to `hi` of one analysis.
float row_level(float lo, float hi, bool long_window) {
    if (hi - lo < 1.0) {
        float mid = sqrt(lo * hi);
        uint i = uint(floor(mid));
        float f = mid - floor(mid);
        float p0 = bin_level(max(i, 1u) - 1u, long_window);
        float p1 = bin_level(i, long_window);
        float p2 = bin_level(i + 1u, long_window);
        float p3 = bin_level(i + 2u, long_window);
        float v = p1 + 0.5 * f * (p2 - p0 + f * (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3 + f * (3.0 * (p1 - p2) + p3 - p0)));
        return clamp(v, SILENCE, max(max(p1, p2), max(p0, p3)));
    }
    float v = SILENCE;
    for (uint i = uint(floor(lo)); i <= uint(ceil(hi)); i++) v = max(v, bin_level(i, long_window));
    return v;
}

// Average power over `count` windows to dBFS: a full-scale sine at a bin's
// centre gives (W * A0 / 2) squared.
float to_db(float power, uint count) {
    float ratio = power / float(count) / (0.25 * float(W) * A0 * float(W) * A0);
    return ratio > 0.0 ? max(10.0 * log(ratio) / log(10.0), SILENCE) : SILENCE;
}

void main() {
    uint c = gl_WorkGroupID.x;
    uint t = gl_LocalInvocationID.x;
    for (uint k = t; k <= HALF; k += THREADS) short_bins[k] = 0.0;
    for (uint k = t; k < LONG_BINS; k += THREADS) long_bins[k] = 0.0;
    barrier();

    float start = float(c) * u.spp;
    uint short_windows = uint(clamp(ceil(u.spp / float(W / 2u)), 1.0, 4.0));
    for (uint w = 0u; w < short_windows; w++) {
        analyse(int(float(u.first) + start + u.spp * (float(w) + 0.5) / float(short_windows)), false);
    }
    // Zoomed out, the windows spread across the column average already;
    // zoomed in, WELCH windows a quarter window apart around its centre.
    uint long_windows = uint(clamp(ceil(u.spp / float(W * DECIMATE / 2u)), 1.0, 4.0));
    if (long_windows == 1u) {
        int centre = int(float(u.first) + start + 0.5 * u.spp);
        for (uint w = 0u; w < WELCH; w++) analyse(centre + (int(w) - int(WELCH / 2u)) * int(W * DECIMATE / 4u), true);
        long_windows = WELCH;
    } else {
        for (uint w = 0u; w < long_windows; w++) {
            analyse(int(float(u.first) + start + u.spp * (float(w) + 0.5) / float(long_windows)), true);
        }
    }

    for (uint k = t; k <= HALF; k += THREADS) short_bins[k] = to_db(short_bins[k], short_windows);
    for (uint k = t; k < LONG_BINS; k += THREADS) long_bins[k] = to_db(long_bins[k], long_windows);
    barrier();

    bool inside = float(u.first) + start < float(u.samples);
    float short_hz = u.rate / float(N);
    float long_hz = short_hz / float(DECIMATE);
    float ratio = log(u.max_freq / u.min_freq);
    for (uint r = t; r < u.rows; r += THREADS) {
        float lo = u.min_freq * exp(ratio * float(r) / float(u.rows));
        float hi = u.min_freq * exp(ratio * float(r + 1u) / float(u.rows));
        float blend = smoothstep(BLEND_LO, BLEND_HI, sqrt(lo * hi));
        float v = 0.0;
        if (blend < 1.0) v += (1.0 - blend) * row_level(lo / long_hz, hi / long_hz, true);
        if (blend > 0.0) v += blend * row_level(lo / short_hz, hi / short_hz, false);
        levels[r * MAX_COLUMNS + (u.slot + c) % u.ring] = inside ? v : NO_TAPE;
    }
}