fix(audio): replace NWaveSpectrum CurveRenderer with shader to fix SIGSEGV crash

This commit is contained in:
Lemmy
2026-03-09 11:55:35 -04:00
parent d3732cb197
commit e4593216f0
3 changed files with 117 additions and 101 deletions
+74
View File
@@ -0,0 +1,74 @@
#version 450
layout(location = 0) in vec2 qt_TexCoord0;
layout(location = 0) out vec4 fragColor;
layout(binding = 1) uniform sampler2D dataSource;
layout(std140, binding = 0) uniform buf {
mat4 qt_Matrix;
float qt_Opacity;
vec4 fillColor;
float count;
float texWidth;
float vertical;
};
// Sample amplitude from data texture (R channel)
float fetchData(float idx) {
float i = clamp(idx, 0.0, texWidth - 1.0);
float u = (floor(i) + 0.5) / texWidth;
return texture(dataSource, vec2(u, 0.5)).r;
}
// Cubic Hermite interpolation for smooth wave curves
float cubicHermite(float y0, float y1, float y2, float y3, float t) {
float m1 = (y2 - y0) * 0.25;
float m2 = (y3 - y1) * 0.25;
float t2 = t * t;
float t3 = t2 * t;
return (2.0 * t3 - 3.0 * t2 + 1.0) * y1
+ (t3 - 2.0 * t2 + t) * m1
+ (-2.0 * t3 + 3.0 * t2) * y2
+ (t3 - t2) * m2;
}
// Evaluate interpolated amplitude at fractional data index
float evalCurve(float dataIdx) {
float i = floor(dataIdx);
float t = dataIdx - i;
return cubicHermite(
fetchData(i - 1.0),
fetchData(i),
fetchData(i + 1.0),
fetchData(i + 2.0),
t
);
}
void main() {
vec2 uv = qt_TexCoord0;
// Swap axes for vertical mode
float axisPos = (vertical > 0.5) ? uv.y : uv.x;
float crossPos = (vertical > 0.5) ? uv.x : uv.y;
// Mirror: value[0] at center, value[count-1] at edges
float distFromCenter = abs(axisPos - 0.5) * 2.0;
float dataIdx = distFromCenter * max(count - 1.0, 1.0);
// Interpolated amplitude, clamped to valid range
float amplitude = clamp(evalCurve(dataIdx), 0.0, 1.0);
// Wave fills center ± amplitude/2 in the cross axis
float halfAmp = amplitude * 0.5;
float distFromMid = abs(crossPos - 0.5);
// Antialiased edge (~1px smooth transition)
float edge = fwidth(crossPos) * 1.5;
float mask = smoothstep(halfAmp + edge, halfAmp - edge, distFromMid);
// Premultiplied alpha output
float a = mask * fillColor.a;
fragColor = vec4(fillColor.rgb * a, a) * qt_Opacity;
}
Binary file not shown.
+43 -101
View File
@@ -1,5 +1,5 @@
import QtQuick import QtQuick
import QtQuick.Shapes import Quickshell
import qs.Commons import qs.Commons
Item { Item {
@@ -14,115 +14,57 @@ Item {
property bool showMinimumSignal: false property bool showMinimumSignal: false
property real minimumSignalValue: 0.05 // Default to 5% of height property real minimumSignalValue: 0.05 // Default to 5% of height
// Safe degenerate-path fallback: valid off-screen line that renders nothing visible. readonly property int valuesCount: (values && Array.isArray(values)) ? values.length : 0
// Bare move-to paths like "M 0 0" can crash Qt's CurveRenderer triangulation. readonly property bool hasData: valuesCount >= 2
readonly property string _safeFallbackPath: "M -1 -1 L -1 0"
// Reactive path that updates when values change // Data texture: one pixel per value, R channel = amplitude
readonly property string svgPath: { Item {
if (!values || !Array.isArray(values) || values.length === 0) { id: dataRow
return _safeFallbackPath; width: Math.max(root.valuesCount, 4)
} height: 1
if (!isFinite(width) || !isFinite(height) || width <= 0 || height <= 0) Repeater {
return _safeFallbackPath; model: dataRow.width
// Apply minimum signal if enabled Rectangle {
const processedValues = showMinimumSignal ? values.map(v => v === 0 ? minimumSignalValue : v) : values; required property int index
x: index
// Create the mirrored values width: 1
const partToMirror = processedValues.slice(1).reverse(); height: 1
const mirroredValues = partToMirror.concat(processedValues); color: {
if (index >= root.valuesCount)
if (mirroredValues.length < 2) { return Qt.rgba(0, 0, 0, 1);
return _safeFallbackPath; var v = root.values[index];
} if (v === undefined || v === null || !isFinite(v))
v = 0;
const count = mirroredValues.length; if (root.showMinimumSignal && v === 0)
v = root.minimumSignalValue;
for (let i = 0; i < count; i++) { return Qt.rgba(Math.max(0, Math.min(1, v)), 0, 0, 1);
if (!isFinite(mirroredValues[i])) }
return _safeFallbackPath;
}
if (vertical) {
const stepY = height / (count - 1);
const centerX = width / 2;
const amplitude = width / 2;
if (!isFinite(stepY) || !isFinite(centerX) || !isFinite(amplitude))
return _safeFallbackPath;
let xOffset = mirroredValues[0] * amplitude;
if (!isFinite(xOffset))
return _safeFallbackPath;
let path = `M ${centerX - xOffset} 0`;
for (let i = 1; i < count; i++) {
const y = i * stepY;
xOffset = mirroredValues[i] * amplitude;
if (!isFinite(y) || !isFinite(xOffset))
return _safeFallbackPath;
path += ` L ${centerX - xOffset} ${y}`;
} }
for (let i = count - 1; i >= 0; i--) {
const y = i * stepY;
xOffset = mirroredValues[i] * amplitude;
if (!isFinite(y) || !isFinite(xOffset))
return _safeFallbackPath;
path += ` L ${centerX + xOffset} ${y}`;
}
return path + " Z";
} else {
const stepX = width / (count - 1);
const centerY = height / 2;
const amplitude = height / 2;
if (!isFinite(stepX) || !isFinite(centerY) || !isFinite(amplitude))
return _safeFallbackPath;
let yOffset = mirroredValues[0] * amplitude;
if (!isFinite(yOffset))
return _safeFallbackPath;
let path = `M 0 ${centerY - yOffset}`;
for (let i = 1; i < count; i++) {
const x = i * stepX;
yOffset = mirroredValues[i] * amplitude;
if (!isFinite(x) || !isFinite(yOffset))
return _safeFallbackPath;
path += ` L ${x} ${centerY - yOffset}`;
}
for (let i = count - 1; i >= 0; i--) {
const x = i * stepX;
yOffset = mirroredValues[i] * amplitude;
if (!isFinite(x) || !isFinite(yOffset))
return _safeFallbackPath;
path += ` L ${x} ${centerY + yOffset}`;
}
return path + " Z";
} }
} }
Shape { ShaderEffectSource {
id: shape id: dataTex
sourceItem: dataRow
textureSize: Qt.size(dataRow.width, 1)
live: true
smooth: false
hideSource: true
}
ShaderEffect {
anchors.fill: parent anchors.fill: parent
preferredRendererType: Shape.CurveRenderer visible: root.hasData && root.width > 0 && root.height > 0
containsMode: Shape.FillContains
ShapePath { property variant dataSource: dataTex
id: shapePath property color fillColor: root.fillColor
fillColor: root.fillColor property real count: root.valuesCount
strokeColor: root.strokeWidth > 0 ? root.strokeColor : "transparent" property real texWidth: dataRow.width
strokeWidth: root.strokeWidth property real vertical: root.vertical ? 1.0 : 0.0
PathSvg { fragmentShader: Qt.resolvedUrl(Quickshell.shellDir + "/Shaders/qsb/wave_spectrum.frag.qsb")
path: root.svgPath blending: true
}
}
} }
} }