Files
lucid/Cava.qml
T
Ciro Rivera b75f87f502 Visualiser: cava rests while nothing plays
With a visualiser card on the desktop, cava ran all the time and sent sixty
frames of silence a second for the shell to parse. It now starts when a
playback stream appears and stops when the last one goes, using the list
Audio already keeps (combined-output feeds and recording streams don't count).
2026-10-28 18:19:22 -06:00

145 lines
4.7 KiB
QML

import QtQuick
import Quickshell
import Quickshell.Io
pragma Singleton
// one cava for every desktop visualiser on screen. the bar's media strip keeps
// its own process on purpose: it runs at a fixed band count, and this one
// follows whatever the widest card asks for
Singleton {
id: root
readonly property string confPath: Quickshell.env("HOME") + "/.cache/quickshell/lucid-cava.conf"
readonly property int minBands: 8
// measured: a full-width card at 320 costs nothing over one at 128, so this is
// a ceiling on cava and on delegate count rather than a performance line
readonly property int maxBands: 320
// uid -> bands, so several cards share one process at the finest resolution asked for
property var demands: ({})
readonly property int bands: {
var best = 0;
for (var k in root.demands) best = Math.max(best, root.demands[k]);
return Math.min(root.maxBands, best);
}
// 0..1 per band, smoothed
property var levels: []
// bands the process actually running was started with
property int liveBands: 0
property double lastMotion: 0
// false once nothing has moved for a moment, so a card can dim itself
property bool active: false
// with nothing playing cava would still send 60 frames of silence a second
readonly property bool sounding: Audio.playbackStreams.length > 0
function want(uid, n) {
if (uid === "")
return ;
var v = Math.max(root.minBands, Math.min(root.maxBands, Math.round(n)));
if (root.demands[uid] === v)
return ;
// a fresh object, or the var property never reports the change
var d = {};
for (var k in root.demands) d[k] = root.demands[k]
d[uid] = v;
root.demands = d;
}
function drop(uid) {
if (uid === "" || root.demands[uid] === undefined)
return ;
var d = {};
for (var k in root.demands) {
if (k !== uid)
d[k] = root.demands[k];
}
root.demands = d;
}
function confText(n) {
return "# generated by lucid, rewritten whenever a visualiser resizes\n" + "[general]\nbars = " + n + "\nframerate = 60\nautosens = 1\n\n" + "[smoothing]\nnoise_reduction = 45\n\n" + "[output]\nmethod = raw\nraw_target = /dev/stdout\ndata_format = ascii\n" + "ascii_max_range = 100\nbar_delimiter = 32\nframe_delimiter = 10\nchannels = mono\n";
}
function apply() {
if (root.bands <= 0 || !root.sounding) {
proc.running = false;
root.liveBands = 0;
root.levels = [];
root.active = false;
return ;
}
if (proc.running && root.liveBands === root.bands)
return ;
root.liveBands = root.bands;
proc.running = false;
// cava only takes its band count from a file, so write one and exec over it
proc.command = ["sh", "-c", "mkdir -p \"${2%/*}\" && printf '%s' \"$1\" > \"$2\" && exec cava -p \"$2\"", "lucid-cava", root.confText(root.bands), root.confPath];
proc.running = true;
}
onBandsChanged: restart.restart()
onSoundingChanged: restart.restart()
// a resize walks the band count across many values, so settle before respawning
Timer {
id: restart
interval: 260
onTriggered: root.apply()
}
// the poll is a fifth of the window, or the real hide drifts well past it
Timer {
interval: 100
repeat: true
running: proc.running
onTriggered: root.active = Date.now() - root.lastMotion < 500
}
Process {
id: proc
running: false
onRunningChanged: {
if (!proc.running) {
root.levels = [];
root.active = false;
}
}
stdout: SplitParser {
onRead: (line) => {
var raw = line.trim();
if (raw === "")
return ;
var parts = raw.split(" ");
var prev = root.levels;
var out = new Array(parts.length);
var moving = false;
for (var i = 0; i < parts.length; i++) {
var v = (parseInt(parts[i]) || 0) / 100;
var p = prev[i];
// jump up with the beat, fall on a ramp, same shape the bar's strip uses
var s = (p === undefined || v >= p) ? v : p * 0.78 + v * 0.22;
out[i] = s;
if (s > 0.015)
moving = true;
}
root.levels = out;
if (moving) {
root.lastMotion = Date.now();
root.active = true;
}
}
}
}
}