1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-01-28 15:32:50 -05:00

audio: optimize visualizations

This commit is contained in:
bbedward
2025-11-24 11:37:24 -05:00
parent f618df46d8
commit 7fa87125b5
4 changed files with 131 additions and 98 deletions

View File

@@ -1,5 +1,4 @@
import QtQuick
import QtQuick.Effects
import QtQuick.Shapes
import Quickshell.Services.Mpris
import qs.Common
@@ -18,7 +17,7 @@ Item {
onArtUrlChanged: {
if (artUrl && albumArt.status !== Image.Error) {
lastValidArtUrl = artUrl
lastValidArtUrl = artUrl;
}
}
@@ -36,7 +35,7 @@ Item {
width: parent.width * 1.1
height: parent.height * 1.1
anchors.centerIn: parent
visible: activePlayer?.playbackState === MprisPlaybackState.Playing && showAnimation
visible: CavaService.cavaAvailable && activePlayer?.playbackState === MprisPlaybackState.Playing && showAnimation
asynchronous: false
antialiasing: true
preferredRendererType: Shape.CurveRenderer
@@ -50,19 +49,21 @@ Item {
property var audioLevels: {
if (!CavaService.cavaAvailable || CavaService.values.length === 0) {
return [0.5, 0.3, 0.7, 0.4, 0.6, 0.5, 0.8, 0.2, 0.9, 0.6]
return [0.5, 0.3, 0.7, 0.4, 0.6, 0.5, 0.8, 0.2, 0.9, 0.6];
}
return CavaService.values
return CavaService.values;
}
property var smoothedLevels: [0.5, 0.3, 0.7, 0.4, 0.6, 0.5, 0.8, 0.2, 0.9, 0.6]
property var cubics: []
onAudioLevelsChanged: updatePath()
FrameAnimation {
running: morphingBlob.visible
onTriggered: morphingBlob.updatePath()
Connections {
target: CavaService
function onValuesChanged() {
if (morphingBlob.visible) {
morphingBlob.updatePath();
}
}
}
Component {
@@ -71,69 +72,61 @@ Item {
}
Component.onCompleted: {
shapePath.pathElements.push(Qt.createQmlObject(
'import QtQuick; import QtQuick.Shapes; PathMove {}', shapePath
))
shapePath.pathElements.push(Qt.createQmlObject('import QtQuick; import QtQuick.Shapes; PathMove {}', shapePath));
for (let i = 0; i < segments; i++) {
const seg = cubicSegment.createObject(shapePath)
shapePath.pathElements.push(seg)
cubics.push(seg)
const seg = cubicSegment.createObject(shapePath);
shapePath.pathElements.push(seg);
cubics.push(seg);
}
updatePath()
}
function expSmooth(prev, next, alpha) {
return prev + alpha * (next - prev)
updatePath();
}
function updatePath() {
if (cubics.length === 0) return
if (cubics.length === 0)
return;
for (let i = 0; i < Math.min(smoothedLevels.length, audioLevels.length); i++) {
smoothedLevels[i] = expSmooth(smoothedLevels[i], audioLevels[i], 0.35)
const alpha = 0.35;
const minLen = Math.min(smoothedLevels.length, audioLevels.length);
for (let i = 0; i < minLen; i++) {
smoothedLevels[i] += alpha * (audioLevels[i] - smoothedLevels[i]);
}
const points = []
const angleStep = 2 * Math.PI / segments;
const tension3 = 0.16666667;
const startMove = shapePath.pathElements[0];
const points = new Array(segments);
for (let i = 0; i < segments; i++) {
const angle = (i / segments) * 2 * Math.PI
const audioIndex = i % Math.min(smoothedLevels.length, 10)
const rawLevel = smoothedLevels[audioIndex] || 0
const scaledLevel = Math.sqrt(Math.min(Math.max(rawLevel, 0), 100) / 100) * 100
const normalizedLevel = scaledLevel / 100
const audioLevel = Math.max(0.15, normalizedLevel) * 0.5
const radius = baseRadius * (1.0 + audioLevel)
const x = centerX + Math.cos(angle) * radius
const y = centerY + Math.sin(angle) * radius
points.push({x: x, y: y})
const angle = i * angleStep;
const audioIndex = i % 10;
const rawLevel = smoothedLevels[audioIndex] || 0;
const clampedLevel = rawLevel < 0 ? 0 : (rawLevel > 100 ? 100 : rawLevel);
const audioLevel = Math.max(0.15, Math.sqrt(clampedLevel * 0.01)) * 0.5;
const radius = baseRadius * (1.0 + audioLevel);
points[i] = {
x: centerX + Math.cos(angle) * radius,
y: centerY + Math.sin(angle) * radius
};
}
const startMove = shapePath.pathElements[0]
startMove.x = points[0].x
startMove.y = points[0].y
startMove.x = points[0].x;
startMove.y = points[0].y;
const tension = 0.5
for (let i = 0; i < segments; i++) {
const p0 = points[(i - 1 + segments) % segments]
const p1 = points[i]
const p2 = points[(i + 1) % segments]
const p3 = points[(i + 2) % segments]
const p0 = points[(i + segments - 1) % segments];
const p1 = points[i];
const p2 = points[(i + 1) % segments];
const p3 = points[(i + 2) % segments];
const c1x = p1.x + (p2.x - p0.x) * tension / 3
const c1y = p1.y + (p2.y - p0.y) * tension / 3
const c2x = p2.x - (p3.x - p1.x) * tension / 3
const c2y = p2.y - (p3.y - p1.y) * tension / 3
const seg = cubics[i]
seg.control1X = c1x
seg.control1Y = c1y
seg.control2X = c2x
seg.control2Y = c2y
seg.x = p2.x
seg.y = p2.y
const seg = cubics[i];
seg.control1X = p1.x + (p2.x - p0.x) * tension3;
seg.control1Y = p1.y + (p2.y - p0.y) * tension3;
seg.control2X = p2.x - (p3.x - p1.x) * tension3;
seg.control2Y = p2.y - (p3.y - p1.y) * tension3;
seg.x = p2.x;
seg.y = p2.y;
}
}
@@ -161,8 +154,8 @@ Item {
onImageSourceChanged: {
if (imageSource && imageStatus !== Image.Error) {
lastValidArtUrl = imageSource
lastValidArtUrl = imageSource;
}
}
}
}
}