summaryrefslogtreecommitdiff
path: root/src/scope/shaders/spectrum.frag
diff options
context:
space:
mode:
Diffstat (limited to 'src/scope/shaders/spectrum.frag')
-rw-r--r--src/scope/shaders/spectrum.frag51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/scope/shaders/spectrum.frag b/src/scope/shaders/spectrum.frag
new file mode 100644
index 0000000..e88b091
--- /dev/null
+++ b/src/scope/shaders/spectrum.frag
@@ -0,0 +1,51 @@
+#version 450
+#extension GL_GOOGLE_include_directive : require
+// The spectrum from its columns (spectrum.comp): one column per pixel on a
+// log frequency axis. The curve is drawn by its distance along the normal,
+// from the neighbours' slope, so steep parts stay as thick as flat ones.
+layout(location = 0) out vec4 out_color;
+
+#include "SpectrumView.glsl"
+
+layout(set = 2, binding = 0) readonly buffer Columns { vec2 columns[]; };
+
+float y_of(float db) {
+ return u.size.y * (1.0 - clamp((db - u.floor_db) / (u.ceil_db - u.floor_db), 0.0, 1.0));
+}
+
+float x_of(float hz) {
+ return u.size.x * log(hz / u.min_freq) / log(u.max_freq / u.min_freq);
+}
+
+// How close `p` is to a grid line, 0..1.
+float grid(vec2 p) {
+ const float lines[10] = float[](20.0, 50.0, 100.0, 200.0, 500.0, 1000.0, 2000.0, 5000.0, 10000.0, 20000.0);
+ float near = 1e9;
+ for (int i = 0; i < 10; i++) near = min(near, abs(p.x - x_of(lines[i])));
+ float step = u.size.y * 12.0 / (u.ceil_db - u.floor_db);
+ float db = mod(p.y, step);
+ near = min(near, min(db, step - db));
+ return clamp(1.0 - near, 0.0, 1.0);
+}
+
+void main() {
+ vec2 p = gl_FragCoord.xy;
+ int n = int(u.size.x);
+ int x = int(p.x);
+ float y = y_of(columns[x].x);
+ float slope = (y_of(columns[min(x + 1, n - 1)].x) - y_of(columns[max(x - 1, 0)].x)) * 0.5;
+ float distance = abs(p.y - y) / sqrt(1.0 + slope * slope);
+
+ vec3 color = vec3(0.055, 0.06, 0.07);
+ color = mix(color, vec3(0.16, 0.17, 0.2), grid(p));
+
+ // Fill under the curve, by frequency, fading downwards.
+ float along = p.x / u.size.x;
+ vec3 hue = mix(vec3(0.2, 0.75, 0.9), vec3(0.85, 0.35, 0.9), along);
+ if (p.y > y) color = mix(color, hue, 0.35 * (1.0 - (p.y - y) / max(u.size.y - y, 1.0)) + 0.08);
+ // Peaks, then the curve.
+ float peak = abs(p.y - y_of(columns[x].y));
+ color = mix(color, vec3(0.9, 0.9, 0.95), clamp(1.0 - peak, 0.0, 1.0) * 0.45);
+ color = mix(color, mix(hue, vec3(1.0), 0.5), clamp(1.6 - distance, 0.0, 1.0));
+ out_color = vec4(color, 1.0);
+}