summaryrefslogtreecommitdiff
path: root/src/scope/shaders/spectrum.comp
blob: e576e304048e42c471928e00c22bd097f1e3b0a4 (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
#version 450
#extension GL_GOOGLE_include_directive : require
// The spectrum at the playhead, entirely on the GPU, in one workgroup:
//
// 1. a Blackman-Harris window of the tape ending at `u.end`, zero-padded to
//    N. Its sidelobes sit below -92 dB, so the finely sampled spectrum shows
//    the signal rather than the window's ripples;
// 2. a radix-2 Stockham FFT, ping-ponging between the scratch halves;
// 3. levels in dBFS per bin;
// 4. one value per log-spaced column. Where columns are narrower than a bin
//    (the low end), a Catmull-Rom spline through the bins' levels; where a
//    column spans several bins (the high end), their loudest. The zero
//    padding already interpolates between the window's own bins, so the
//    curve is continuous at every zoom;
// 5. smoothing in time (fast attack, slow release) and falling peaks, kept
//    in the column buffer between frames.
layout(local_size_x = 256) in;

#include "SpectrumParams.glsl"

layout(set = 0, binding = 0) readonly buffer Tape { float tape[]; };
// Two halves of N complex values, which the workgroup's threads read from
// each other: coherent, for Vulkan; WGSL's storageBarrier covers it.
#ifdef SKETCHBOOK_WEB
layout(set = 1, binding = 0) buffer Scratch { vec2 scratch[]; };
#else
layout(set = 1, binding = 0) coherent buffer Scratch { vec2 scratch[]; };
#endif
// x: smoothed level, y: peak, both dBFS.
layout(set = 1, binding = 1) buffer Columns { vec2 columns[]; };

// Match layouts.zig.
const uint N = 8192;
const uint LOG2_N = 13;
const uint W = 4096;
const uint THREADS = 256;
const float PI = 3.14159265358979;
const float SILENCE = -160.0;
// Blackman-Harris, 4 terms. A0 is also its coherent gain.
const float A0 = 0.35875;
const float A1 = 0.48829;
const float A2 = 0.14128;
const float A3 = 0.01168;

void sync() {
    memoryBarrierBuffer();
    barrier();
}

void main() {
    uint t = gl_LocalInvocationID.x;

    // 1. Window.
    int first = int(u.end) - int(W);
    for (uint i = t; i < N; i += THREADS) {
        float x = 0.0;
        int at = first + int(i);
        if (i < W && at >= 0 && at < int(u.samples)) {
            float a = 2.0 * PI * float(i) / float(W - 1);
            x = tape[u.offset + uint(at)] * (A0 - A1 * cos(a) + A2 * cos(2.0 * a) - A3 * cos(3.0 * a));
        }
        scratch[i] = vec2(x, 0.0);
    }
    sync();

    // 2. FFT: after stage s, sub-transforms of 2p = 2^(s+1) points, in order.
    for (uint s = 0; s < LOG2_N; s++) {
        uint p = 1u << s;
        uint from = (s & 1u) * N;
        uint to = N - from;
        for (uint i = t; i < N / 2; i += THREADS) {
            uint k = i & (p - 1u);
            vec2 a = scratch[from + i];
            vec2 b = scratch[from + i + N / 2];
            float angle = -PI * float(k) / float(p);
            vec2 w = vec2(cos(angle), sin(angle));
            b = vec2(b.x * w.x - b.y * w.y, b.x * w.y + b.y * w.x);
            uint j = (i << 1u) - k;
            scratch[to + j] = a + b;
            scratch[to + j + p] = a - b;
        }
        sync();
    }
    uint result = (LOG2_N & 1u) * N;

    // 3. Levels into the other half. A full-scale sine peaks at W * A0 / 2:
    // the window sums to W * A0, and a real sine splits between ±f.
    uint levels = N - result;
    for (uint i = t; i <= N / 2; i += THREADS) {
        float amplitude = length(scratch[result + i]) * 2.0 / (float(W) * A0);
        scratch[levels + i].x = amplitude > 0.0 ? max(20.0 * log(amplitude) / log(10.0), SILENCE) : SILENCE;
    }
    sync();

    // 4 and 5. Columns.
    float bin_hz = u.rate / float(N);
    float ratio = log(u.max_freq / u.min_freq);
    for (uint c = t; c < u.columns; c += THREADS) {
        float lo = u.min_freq * exp(ratio * float(c) / float(u.columns)) / bin_hz;
        float hi = u.min_freq * exp(ratio * float(c + 1u) / float(u.columns)) / bin_hz;
        float mid = sqrt(lo * hi);
        float v;
        if (hi - lo < 1.0) {
            uint i = uint(floor(mid));
            float f = mid - floor(mid);
            float p0 = scratch[levels + min(max(i, 1u) - 1u, N / 2)].x;
            float p1 = scratch[levels + min(i, N / 2)].x;
            float p2 = scratch[levels + min(i + 1u, N / 2)].x;
            float p3 = scratch[levels + min(i + 2u, N / 2)].x;
            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)));
            v = clamp(v, SILENCE, max(max(p1, p2), max(p0, p3)));
        } else {
            v = SILENCE;
            uint end = min(uint(ceil(hi)), N / 2);
            for (uint i = uint(floor(lo)); i <= end; i++) v = max(v, scratch[levels + i].x);
        }
        vec2 old = columns[c];
        vec2 now;
        if (u.reset != 0u) {
            now = vec2(v, v);
        } else {
            float rate = v > old.x ? 40.0 : 6.0;
            now.x = mix(old.x, v, 1.0 - exp(-rate * u.dt));
            now.y = max(now.x, old.y - 24.0 * u.dt);
        }
        columns[c] = now;
    }
}