<!DOCTYPE html>
|
<html lang="zh-CN">
|
|
<head>
|
<meta charset="UTF-8" />
|
<title>Moli Desktop Audio Waveform</title>
|
<style>
|
html,
|
body {
|
margin: 0;
|
width: 100%;
|
height: 100%;
|
overflow: hidden;
|
background: transparent;
|
}
|
|
body {
|
background:
|
radial-gradient(circle at 68% 66%, rgba(0, 210, 255, 0.18), transparent 18%),
|
radial-gradient(circle at 50% 66%, rgba(255, 0, 200, 0.16), transparent 32%),
|
linear-gradient(90deg, rgba(0, 12, 22, 0.55), rgba(20, 0, 35, 0.55));
|
}
|
|
canvas {
|
width: 100vw;
|
height: 100vh;
|
display: block;
|
}
|
|
#status {
|
position: fixed;
|
left: 10px;
|
top: 8px;
|
font-size: 12px;
|
font-family: Consolas, monospace;
|
color: rgba(255, 255, 255, .55);
|
pointer-events: none;
|
}
|
</style>
|
</head>
|
|
<body>
|
<canvas id="canvas"></canvas>
|
<div id="status">connecting...</div>
|
|
<script>
|
const WS_URL = "ws://127.0.0.1:8765/ws/events";
|
|
const canvas = document.getElementById("canvas");
|
const ctx = canvas.getContext("2d");
|
const statusEl = document.getElementById("status");
|
|
let W = 0;
|
let H = 0;
|
let dpr = 1;
|
|
let sourceLevels = new Array(64).fill(0);
|
let displayBars = [];
|
let lastEnergy = 0;
|
let beat = 0;
|
|
const config = {
|
barCount: 150,
|
|
// 屏幕下方 1/3 附近
|
centerYRatio: 0.66,
|
|
// 单边最大高度,总高度约为 2 倍
|
maxHeightRatio: 0.20,
|
|
barWidth: 4,
|
gap: 6,
|
radius: 3,
|
|
smooth: 0.5,
|
decay: 0.5,
|
|
sensitivity: 1.5,
|
compressionPower: 0.70,
|
|
showStatus: true
|
};
|
|
function safeNumber(v, fallback = 0) {
|
v = Number(v);
|
return Number.isFinite(v) ? v : fallback;
|
}
|
|
function clamp(v, min, max) {
|
v = safeNumber(v, min);
|
return Math.max(min, Math.min(max, v));
|
}
|
|
function sampleLevel(levels, idx) {
|
if (!Array.isArray(levels) || levels.length === 0) return 0;
|
|
const n = levels.length;
|
idx = clamp(idx, 0, n - 1);
|
|
const i0 = Math.floor(idx);
|
const i1 = Math.min(n - 1, i0 + 1);
|
const f = idx - i0;
|
|
const a = safeNumber(levels[i0], 0);
|
const b = safeNumber(levels[i1], 0);
|
|
return a * (1 - f) + b * f;
|
}
|
|
function resize() {
|
dpr = window.devicePixelRatio || 1;
|
W = window.innerWidth;
|
H = window.innerHeight;
|
|
canvas.width = Math.floor(W * dpr);
|
canvas.height = Math.floor(H * dpr);
|
canvas.style.width = W + "px";
|
canvas.style.height = H + "px";
|
|
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
displayBars = new Array(config.barCount).fill(0);
|
}
|
|
window.addEventListener("resize", resize);
|
resize();
|
|
function connect() {
|
const ws = new WebSocket(WS_URL);
|
|
ws.onopen = () => {
|
statusEl.textContent = "connected";
|
};
|
|
ws.onclose = () => {
|
statusEl.textContent = "disconnected, reconnecting...";
|
setTimeout(connect, 1000);
|
};
|
|
ws.onerror = () => {
|
statusEl.textContent = "websocket error";
|
};
|
|
ws.onmessage = (event) => {
|
try {
|
const msg = JSON.parse(event.data);
|
if (msg.name !== "audio.spectrum") return;
|
|
const levels = msg.payload && msg.payload.levels;
|
if (!Array.isArray(levels)) return;
|
|
sourceLevels = levels.map(v => Math.max(0, Number(v) || 0));
|
|
if (config.showStatus) {
|
const rms = Number(msg.payload.rms || 0);
|
statusEl.textContent =
|
`audio.spectrum | levels ${sourceLevels.length} -> ${config.barCount} | rms ${rms.toFixed(4)}`;
|
} else {
|
statusEl.style.display = "none";
|
}
|
} catch (e) { }
|
};
|
}
|
|
connect();
|
|
function clamp(v, min, max) {
|
return Math.max(min, Math.min(max, v));
|
}
|
|
function lerp(a, b, t) {
|
return a + (b - a) * t;
|
}
|
|
function sampleLevel(levels, idx) {
|
const n = levels.length;
|
if (!n) return 0;
|
|
idx = clamp(idx, 0, n - 1);
|
|
const i0 = Math.floor(idx);
|
const i1 = Math.min(n - 1, i0 + 1);
|
const f = idx - i0;
|
|
return levels[i0] * (1 - f) + levels[i1] * f;
|
}
|
|
function smoothstep(edge0, edge1, x) {
|
x = clamp((x - edge0) / (edge1 - edge0), 0, 1);
|
return x * x * (3 - 2 * x);
|
}
|
|
/**
|
* 视觉目标:
|
* - 左侧:中频 + 少量高频,小而细碎
|
* - 中间:高频,细密闪烁
|
* - 偏右 2/3:低频 / 中低频,大波峰
|
* - 最右:高频尾巴,快速衰减
|
*
|
* 这不是物理频谱,而是接近参考图的“设计型频谱重排”。
|
*/
|
function calcMidBar(levels, t, n, bassEnd, midEnd, highEnd) {
|
const p = t / 0.35;
|
|
const midIdx = lerp(bassEnd, midEnd, Math.pow(p, 0.9));
|
const highIdx = lerp(midEnd, highEnd, Math.pow(p, 0.7));
|
|
const mid = sampleLevel(levels, midIdx);
|
const high = sampleLevel(levels, highIdx);
|
|
const fadeIn = smoothstep(0.0, 0.42, p);
|
const introFloor = 0.06 + p * 0.10;
|
const mainShape = 0.18 + Math.pow(p, 0.9) * 0.48;
|
|
return {
|
v: mid * 0.72 + high * 0.28,
|
shape: introFloor + mainShape * fadeIn,
|
band: "mid"
|
};
|
}
|
|
function calcTrebleBar(levels, t, n, bassEnd, midEnd, highEnd) {
|
const p = (t - 0.35) / 0.23;
|
|
const highIdx = lerp(midEnd, highEnd, Math.pow(p, 0.8));
|
const midIdx = lerp(midEnd * 0.75, midEnd, p);
|
|
const high = sampleLevel(levels, highIdx);
|
const mid = sampleLevel(levels, midIdx);
|
|
return {
|
v: high * 0.82 + mid * 0.18,
|
shape: 0.22 + Math.sin(p * Math.PI) * 0.28,
|
band: "treble"
|
};
|
}
|
|
function calcBassBar(levels, t, n, bassEnd, midEnd, highEnd) {
|
const p = (t - 0.58) / 0.24;
|
|
const bassIdx = lerp(0, bassEnd, Math.pow(p, 0.55));
|
const midBassIdx = lerp(bassEnd, Math.floor(n * 0.32), p);
|
const highIdx = lerp(midEnd, highEnd, p);
|
|
const bass = sampleLevel(levels, bassIdx);
|
const midBass = sampleLevel(levels, midBassIdx);
|
const high = sampleLevel(levels, highIdx);
|
|
const peak = Math.exp(-Math.pow((p - 0.58) / 0.36, 2));
|
const enter = smoothstep(0.0, 0.38, p);
|
|
return {
|
v: bass * 0.58 + midBass * 0.24 + high * 0.18,
|
shape: 0.16 + enter * (0.08 + peak * 0.72),
|
band: "bass"
|
};
|
}
|
|
function calcTailBar(levels, t, n, bassEnd, midEnd, highEnd) {
|
const p = (t - 0.82) / 0.18;
|
|
const highIdx = lerp(midEnd, highEnd, p);
|
const bassIdx = bassEnd * 0.45;
|
const midIdx = lerp(bassEnd, midEnd * 0.7, 1 - p);
|
|
const high = sampleLevel(levels, highIdx);
|
const bass = sampleLevel(levels, bassIdx);
|
const mid = sampleLevel(levels, midIdx);
|
|
return {
|
v: high * 0.62 + bass * 0.16 + mid * 0.10,
|
shape: Math.pow(1 - p, 1.65) * 0.38 + 0.06,
|
band: "tail"
|
};
|
}
|
|
function mixBar(a, b, k) {
|
k = smoothstep(0, 1, k);
|
|
// 原始平滑混合
|
const mixedV = lerp(a.v, b.v, k);
|
|
// 过渡区补底:避免 a、b 在切换时同时偏低
|
const bridge = Math.max(a.v, b.v) * 0.25;
|
|
return {
|
v: mixedV + bridge,
|
shape: lerp(a.shape, b.shape, k),
|
band: k < 0.5 ? a.band : b.band
|
};
|
}
|
|
function applyBandGain(v, band) {
|
if (band === "bass") return v * 2.15;
|
if (band === "mid") return v * 1.75;
|
if (band === "treble") return v * 2.35;
|
if (band === "tail") return v * 1.95;
|
return v;
|
}
|
|
function applyCompression(v) {
|
v = Math.pow(v * config.sensitivity, config.compressionPower);
|
return v / (1 + v);
|
}
|
|
function applyVisualTexture(v, band, i, shape) {
|
const grain =
|
0.92 +
|
0.06 * Math.sin(i * 0.47) +
|
0.04 * Math.sin(i * 1.13);
|
|
const sparkle =
|
band === "treble" || band === "tail"
|
? 0.92 + 0.12 * Math.sin(performance.now() * 0.018 + i * 0.9)
|
: 1;
|
|
return clamp(v * shape * grain * sparkle, 0, 1);
|
}
|
|
function getBarByPosition(levels, t, n, bassEnd, midEnd, highEnd) {
|
const blend58 = 0.00;
|
const blend82 = 0.035;
|
|
if (t < 0.35) {
|
return calcMidBar(levels, t, n, bassEnd, midEnd, highEnd);
|
}
|
|
if (t < 0.58 - blend58) {
|
return calcTrebleBar(levels, t, n, bassEnd, midEnd, highEnd);
|
}
|
|
if (t < 0.58 + blend58) {
|
const k = (t - (0.58 - blend58)) / (blend58 * 2);
|
return mixBar(
|
calcTrebleBar(levels, t, n, bassEnd, midEnd, highEnd),
|
calcBassBar(levels, t, n, bassEnd, midEnd, highEnd),
|
k
|
);
|
}
|
|
if (t < 0.82 - blend82) {
|
return calcBassBar(levels, t, n, bassEnd, midEnd, highEnd);
|
}
|
|
if (t < 0.82 + blend82) {
|
const k = (t - (0.82 - blend82)) / (blend82 * 2);
|
return mixBar(
|
calcBassBar(levels, t, n, bassEnd, midEnd, highEnd),
|
calcTailBar(levels, t, n, bassEnd, midEnd, highEnd),
|
k
|
);
|
}
|
|
return calcTailBar(levels, t, n, bassEnd, midEnd, highEnd);
|
}
|
|
function buildBars(levels, count) {
|
const res = new Array(count);
|
const n = levels.length || 64;
|
|
const bassEnd = Math.floor(n * 0.16);
|
const midEnd = Math.floor(n * 0.55);
|
const highEnd = n - 1;
|
|
for (let i = 0; i < count; i++) {
|
const t = i / (count - 1);
|
|
const item = getBarByPosition(
|
levels,
|
t,
|
n,
|
bassEnd,
|
midEnd,
|
highEnd
|
);
|
|
let v = applyBandGain(item.v, item.band);
|
v = applyCompression(v);
|
|
res[i] = applyVisualTexture(v, item.band, i, item.shape);
|
}
|
|
return res;
|
}
|
|
// function colorByPosition(t) {
|
// if (t < 0.18) return "hsl(330, 100%, 58%)";
|
// if (t < 0.35) return "hsl(95, 100%, 58%)";
|
// if (t < 0.58) return "hsl(285, 100%, 62%)";
|
// if (t < 0.72) return "hsl(140, 100%, 58%)";
|
// if (t < 0.82) return "hsl(190, 100%, 60%)";
|
// return "hsl(260, 100%, 64%)";
|
// }
|
// ===== 连续渐变色(替换 colorByPosition) =====
|
|
function hslToString(h, s, l) {
|
return `hsl(${h}, ${s}%, ${l}%)`;
|
}
|
|
function mixColorStop(a, b, k) {
|
// 使用 smoothstep,让过渡更丝滑
|
k = smoothstep(0, 1, k);
|
|
return {
|
h: lerp(a.h, b.h, k),
|
s: lerp(a.s, b.s, k),
|
l: lerp(a.l, b.l, k)
|
};
|
}
|
|
function colorByPosition(t) {
|
// 👉 整体偏霓虹粉紫(接近你参考图)
|
const stops = [
|
{ pos: 0.00, h: 330, s: 100, l: 62 }, // 亮粉
|
{ pos: 0.35, h: 323, s: 100, l: 66 }, // 粉紫
|
{ pos: 0.65, h: 310, s: 100, l: 68 }, // 洋红
|
{ pos: 1.00, h: 292, s: 100, l: 70 } // 紫
|
];
|
|
// 找到所在区间并插值
|
for (let i = 0; i < stops.length - 1; i++) {
|
const a = stops[i];
|
const b = stops[i + 1];
|
|
if (t >= a.pos && t <= b.pos) {
|
const k = (t - a.pos) / (b.pos - a.pos);
|
const c = mixColorStop(a, b, k);
|
return hslToString(c.h, c.s, c.l);
|
}
|
}
|
|
// fallback(理论不会走到)
|
const last = stops[stops.length - 1];
|
return hslToString(last.h, last.s, last.l);
|
}
|
|
function roundedBar(x, y, w, h, r) {
|
const rr = Math.min(r, w / 2, h / 2);
|
|
ctx.beginPath();
|
ctx.moveTo(x + rr, y);
|
ctx.lineTo(x + w - rr, y);
|
ctx.quadraticCurveTo(x + w, y, x + w, y + rr);
|
ctx.lineTo(x + w, y + h - rr);
|
ctx.quadraticCurveTo(x + w, y + h, x + w - rr, y + h);
|
ctx.lineTo(x + rr, y + h);
|
ctx.quadraticCurveTo(x, y + h, x, y + h - rr);
|
ctx.lineTo(x, y + rr);
|
ctx.quadraticCurveTo(x, y, x + rr, y);
|
ctx.closePath();
|
ctx.fill();
|
}
|
|
function drawBackground(centerY) {
|
const g1 = ctx.createRadialGradient(W * 0.68, centerY, 0, W * 0.68, centerY, W * 0.22);
|
g1.addColorStop(0, "rgba(0, 220, 255, 0.18)");
|
g1.addColorStop(0.45, "rgba(0, 120, 255, 0.08)");
|
g1.addColorStop(1, "rgba(0, 0, 0, 0)");
|
|
ctx.fillStyle = g1;
|
ctx.fillRect(0, 0, W, H);
|
|
const g2 = ctx.createRadialGradient(W * 0.50, centerY, 0, W * 0.50, centerY, W * 0.42);
|
g2.addColorStop(0, "rgba(255, 0, 220, 0.12)");
|
g2.addColorStop(0.55, "rgba(110, 0, 255, 0.07)");
|
g2.addColorStop(1, "rgba(0, 0, 0, 0)");
|
|
ctx.fillStyle = g2;
|
ctx.fillRect(0, 0, W, H);
|
}
|
|
function bellShapeSmooth(data) {
|
const n = data.length;
|
if (n <= 1) return data.slice();
|
|
const sorted = data.slice().sort((a, b) => a - b);
|
const result = new Array(n);
|
|
let leftPos = Math.floor((n - 1) / 2);
|
let rightPos = leftPos + 1;
|
|
// 最大值放中间,然后次大值左右交替
|
for (let i = n - 1, toggle = 0; i >= 0; i--, toggle ^= 1) {
|
if (toggle === 0 && leftPos >= 0) {
|
result[leftPos--] = sorted[i];
|
} else if (rightPos < n) {
|
result[rightPos++] = sorted[i];
|
} else {
|
result[leftPos--] = sorted[i];
|
}
|
}
|
|
return result;
|
}
|
|
function draw() {
|
ctx.clearRect(0, 0, W, H);
|
|
const centerY = H * config.centerYRatio;
|
const maxHalfH = H * config.maxHeightRatio;
|
|
drawBackground(centerY);
|
|
let bars = buildBars(sourceLevels, config.barCount);
|
|
// 简单 beat:主要给右侧低频主峰一点呼吸感
|
const energy = sourceLevels.reduce((a, b) => a + b, 0);
|
if (energy > lastEnergy * 1.12) {
|
beat = 1;
|
} else {
|
beat *= 0.90;
|
}
|
lastEnergy = energy;
|
|
const totalW =
|
config.barCount * config.barWidth +
|
(config.barCount - 1) * config.gap;
|
|
let x = (W - totalW) / 2;
|
|
// 基线
|
ctx.save();
|
ctx.shadowBlur = 10;
|
ctx.shadowColor = "rgba(255,255,255,.65)";
|
ctx.fillStyle = "rgba(255,255,255,.42)";
|
ctx.fillRect(0, centerY - 0.5, W, 1);
|
ctx.restore();
|
|
for (let i = 0; i < config.barCount; i++) {
|
const t = i / (config.barCount - 1);
|
|
let target = bars[i];
|
|
// 低频主峰区域额外 beat boost
|
if (t > 0.58 && t < 0.82) {
|
target *= 1 + beat * 0.34;
|
} else if (t >= 0.35 && t < 0.58) {
|
target *= 1 + beat * 0.10;
|
}
|
|
displayBars[i] = Math.max(
|
target * config.smooth + displayBars[i] * (1 - config.smooth),
|
displayBars[i] * config.decay
|
);
|
|
// const h = Math.max(1.2, displayBars[i] * maxHalfH);
|
const color = colorByPosition(t);
|
|
const rawH = displayBars[i] * maxHalfH;
|
const h = Math.max(1.2, safeNumber(rawH, 1.2));
|
|
if (!Number.isFinite(h)) {
|
continue;
|
}
|
|
const grad = ctx.createLinearGradient(
|
0,
|
centerY - h,
|
0,
|
centerY + h
|
);
|
grad.addColorStop(0, "rgba(255,255,255,0.95)");
|
grad.addColorStop(0.28, color);
|
grad.addColorStop(0.72, color);
|
grad.addColorStop(1, "rgba(0,230,255,0.85)");
|
|
ctx.save();
|
ctx.fillStyle = grad;
|
ctx.shadowBlur = 11;
|
ctx.shadowColor = color;
|
roundedBar(x, centerY - h, config.barWidth, h * 2, config.radius);
|
ctx.restore();
|
|
x += config.barWidth + config.gap;
|
}
|
|
requestAnimationFrame(draw);
|
}
|
|
draw();
|
</script>
|
</body>
|
|
</html>
|