- KDE/Windows accent-driven Material 3 theme, live updating - Library, tabbed playlists, smart/saved lists, queue, ratings - mpv and Qt Multimedia engines, EQ, ReplayGain, gapless - Synced lyrics (LRCLIB, NetEase, local .lrc), covers, MusicBrainz tags - Last.fm/ListenBrainz scrobbling, MPRIS, Windows media keys - Audio CD playback and ripping - PyInstaller, Inno Setup and AppImage builds via GitHub Actions Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01J7imThHV9BMkG24GaxPDeq
108 lines
3.7 KiB
Python
108 lines
3.7 KiB
Python
"""Strawberry-style block spectrum analyzer for the bottom bar.
|
|
|
|
Drawn in three batched passes per frame (cached dim grid, one gradient bar per
|
|
band, gap mask, peaks) instead of hundreds of individual blocks.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from PyQt6.QtCore import QRectF, Qt
|
|
from PyQt6.QtGui import QLinearGradient, QPainter, QPixmap
|
|
from PyQt6.QtWidgets import QSizePolicy, QWidget
|
|
|
|
from .. import theme as Th
|
|
from .m3 import C, with_alpha
|
|
|
|
BW, GAP, BH, BGAP = 6, 2, 3, 1
|
|
|
|
|
|
class Analyzer(QWidget):
|
|
def __init__(self, spectrum, parent=None):
|
|
super().__init__(parent)
|
|
self.spectrum = spectrum
|
|
self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
|
|
self.setFixedHeight(38)
|
|
self._acq = False
|
|
self._grid: QPixmap | None = None
|
|
self._mask: QPixmap | None = None
|
|
self._key = None
|
|
spectrum.updated.connect(self._upd)
|
|
self.setToolTip("Spectrum analyzer")
|
|
|
|
def _upd(self):
|
|
if self.isVisible():
|
|
self.update()
|
|
|
|
def showEvent(self, e):
|
|
if not self._acq:
|
|
self.spectrum.acquire()
|
|
self._acq = True
|
|
|
|
def hideEvent(self, e):
|
|
if self._acq:
|
|
self.spectrum.release()
|
|
self._acq = False
|
|
|
|
def _layout(self):
|
|
n = max(4, (self.width() + GAP) // (BW + GAP))
|
|
rows = max(2, (self.height() + BGAP) // (BH + BGAP))
|
|
return n, rows
|
|
|
|
def _build(self, n, rows):
|
|
dpr = self.devicePixelRatioF()
|
|
w, h = self.width(), self.height()
|
|
grid = QPixmap(int(w * dpr), int(h * dpr))
|
|
grid.setDevicePixelRatio(dpr)
|
|
grid.fill(Qt.GlobalColor.transparent)
|
|
p = QPainter(grid)
|
|
dim = with_alpha(C("onSurfaceVariant"), 0.10)
|
|
for i in range(n):
|
|
for j in range(rows):
|
|
p.fillRect(QRectF(i * (BW + GAP), h - (j + 1) * (BH + BGAP) + BGAP, BW, BH), dim)
|
|
p.end()
|
|
# mask = horizontal gaps painted in the bar's background colour
|
|
mask = QPixmap(int(w * dpr), int(h * dpr))
|
|
mask.setDevicePixelRatio(dpr)
|
|
mask.fill(Qt.GlobalColor.transparent)
|
|
p = QPainter(mask)
|
|
bg = Th.theme.c["surface"]
|
|
for j in range(rows + 1):
|
|
y = h - j * (BH + BGAP)
|
|
p.fillRect(QRectF(0, y, w, BGAP), bg)
|
|
p.fillRect(QRectF(0, 0, w, max(0, h - rows * (BH + BGAP))), bg)
|
|
p.end()
|
|
self._grid, self._mask = grid, mask
|
|
|
|
def paintEvent(self, e):
|
|
n, rows = self._layout()
|
|
key = (self.width(), self.height(), self.devicePixelRatioF(), Th.theme.c["surface"].rgb(),
|
|
Th.theme.c["onSurfaceVariant"].rgb())
|
|
if key != self._key:
|
|
self._build(n, rows)
|
|
self._key = key
|
|
p = QPainter(self)
|
|
p.drawPixmap(0, 0, self._grid)
|
|
h = self.height()
|
|
unit = BH + BGAP
|
|
lv = self.spectrum.resample(n)
|
|
pk = self.spectrum.resample_peaks(n)
|
|
prim = C("primary")
|
|
g = QLinearGradient(0, h, 0, 0)
|
|
g.setColorAt(0, with_alpha(prim, 0.38))
|
|
g.setColorAt(1, with_alpha(prim, 1.0))
|
|
bars, peaks = [], []
|
|
for i in range(n):
|
|
lit = int(round(float(lv[i]) * rows))
|
|
x = i * (BW + GAP)
|
|
if lit:
|
|
bars.append(QRectF(x, h - lit * unit, BW, lit * unit))
|
|
pki = int(round(float(pk[i]) * rows))
|
|
if pki > lit and pki > 0:
|
|
peaks.append(QRectF(x, h - pki * unit + BGAP, BW, BH))
|
|
p.setPen(Qt.PenStyle.NoPen)
|
|
if bars:
|
|
p.setBrush(g)
|
|
p.drawRects(bars)
|
|
p.drawPixmap(0, 0, self._mask)
|
|
if peaks:
|
|
p.setBrush(with_alpha(C("tertiary"), 0.95))
|
|
p.drawRects(peaks)
|