<!doctype html>
|
<html lang="zh-CN">
|
<head>
|
<meta charset="utf-8">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<title>Audio Visualizer WS Test</title>
|
<style>
|
:root {
|
color-scheme: dark;
|
--hot: #ff2fb3;
|
--cool: #29d8ff;
|
--accent: #f8fbff;
|
--panel: rgba(9, 12, 18, 0.72);
|
--line: rgba(255, 255, 255, 0.14);
|
--text: #edf4ff;
|
--muted: #8ea2b8;
|
--ok: #42f5b0;
|
--warn: #ffd166;
|
--bad: #ff6b6b;
|
}
|
|
html,
|
body {
|
width: 100%;
|
height: 100%;
|
margin: 0;
|
overflow: hidden;
|
background: #05070b;
|
color: var(--text);
|
font-family: "Segoe UI", Arial, sans-serif;
|
}
|
|
body {
|
display: grid;
|
grid-template-rows: auto 1fr;
|
}
|
|
.toolbar {
|
display: grid;
|
grid-template-columns: minmax(220px, 1fr) auto auto;
|
gap: 10px;
|
align-items: center;
|
padding: 10px 12px;
|
background: var(--panel);
|
border-bottom: 1px solid var(--line);
|
box-sizing: border-box;
|
}
|
|
.url {
|
min-width: 0;
|
height: 32px;
|
padding: 0 10px;
|
border: 1px solid var(--line);
|
border-radius: 6px;
|
box-sizing: border-box;
|
background: rgba(255, 255, 255, 0.06);
|
color: var(--text);
|
font: inherit;
|
}
|
|
.button {
|
height: 32px;
|
padding: 0 12px;
|
border: 1px solid rgba(41, 216, 255, 0.45);
|
border-radius: 6px;
|
background: rgba(41, 216, 255, 0.14);
|
color: var(--text);
|
cursor: pointer;
|
font: inherit;
|
}
|
|
.button:hover {
|
background: rgba(41, 216, 255, 0.24);
|
}
|
|
.status {
|
display: flex;
|
align-items: center;
|
gap: 8px;
|
min-width: 132px;
|
color: var(--muted);
|
font-size: 13px;
|
white-space: nowrap;
|
}
|
|
.dot {
|
width: 9px;
|
height: 9px;
|
border-radius: 50%;
|
background: var(--warn);
|
box-shadow: 0 0 12px currentColor;
|
}
|
|
.status[data-state="open"] {
|
color: var(--ok);
|
}
|
|
.status[data-state="closed"],
|
.status[data-state="error"] {
|
color: var(--bad);
|
}
|
|
.status[data-state="open"] .dot {
|
background: var(--ok);
|
}
|
|
.status[data-state="closed"] .dot,
|
.status[data-state="error"] .dot {
|
background: var(--bad);
|
}
|
|
#stage {
|
position: relative;
|
width: 100vw;
|
height: 100%;
|
min-height: 0;
|
overflow: hidden;
|
background:
|
linear-gradient(180deg, rgba(41, 216, 255, 0.08), transparent 28%),
|
radial-gradient(circle at 50% 58%, rgba(255, 47, 179, 0.18), transparent 34%),
|
#05070b;
|
--base-y: 60%;
|
}
|
|
#stage::before {
|
content: "";
|
position: absolute;
|
left: 0;
|
right: 0;
|
top: var(--base-y);
|
height: 1px;
|
background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.72), transparent);
|
box-shadow: 0 0 8px rgba(255, 255, 255, 0.68), 0 0 22px rgba(255, 47, 179, 0.42);
|
opacity: 0.86;
|
}
|
|
.bars {
|
position: absolute;
|
inset: 0 14px;
|
display: flex;
|
justify-content: space-between;
|
align-items: flex-start;
|
gap: 2px;
|
}
|
|
.visualizer {
|
position: absolute;
|
inset: 0;
|
width: 100%;
|
height: 100%;
|
display: block;
|
}
|
|
.bar {
|
--bar-height: 1%;
|
--bar-top: 59%;
|
--bar-opacity: 0.24;
|
--reflection-height: 1vh;
|
--reflection-opacity: 0.04;
|
position: relative;
|
flex: 1 1 auto;
|
min-width: 2px;
|
max-width: 7px;
|
height: var(--bar-height);
|
margin-top: var(--bar-top);
|
border-radius: 999px;
|
background: linear-gradient(180deg, var(--accent), var(--hot) 36%, var(--cool));
|
box-shadow: 0 0 5px rgba(255, 255, 255, 0.72), 0 0 13px rgba(255, 47, 179, 0.46);
|
opacity: var(--bar-opacity);
|
transition: height 58ms linear, margin-top 58ms linear, opacity 58ms linear;
|
}
|
|
.bar::after {
|
content: "";
|
position: absolute;
|
width: inherit;
|
min-width: inherit;
|
max-width: inherit;
|
height: var(--reflection-height);
|
transform: translateY(100%) scaleY(0.72);
|
transform-origin: top center;
|
border-radius: inherit;
|
background: linear-gradient(180deg, rgba(255, 47, 179, 0.24), rgba(41, 216, 255, 0.02));
|
filter: blur(1px);
|
opacity: var(--reflection-opacity);
|
}
|
|
.metrics {
|
position: absolute;
|
left: 12px;
|
bottom: 12px;
|
width: min(680px, calc(100vw - 24px));
|
display: grid;
|
grid-template-columns: repeat(4, minmax(0, 1fr));
|
gap: 8px;
|
padding: 10px;
|
border: 1px solid var(--line);
|
border-radius: 8px;
|
background: rgba(9, 12, 18, 0.72);
|
box-sizing: border-box;
|
backdrop-filter: blur(10px);
|
}
|
|
.metric {
|
min-width: 0;
|
}
|
|
.label {
|
display: block;
|
color: var(--muted);
|
font-size: 11px;
|
line-height: 1.4;
|
}
|
|
.value {
|
display: block;
|
overflow: hidden;
|
text-overflow: ellipsis;
|
white-space: nowrap;
|
font-size: 13px;
|
line-height: 1.5;
|
}
|
|
.raw {
|
position: absolute;
|
right: 12px;
|
bottom: 12px;
|
width: min(460px, calc(100vw - 24px));
|
max-height: 136px;
|
margin: 0;
|
padding: 10px;
|
overflow: auto;
|
border: 1px solid var(--line);
|
border-radius: 8px;
|
background: rgba(9, 12, 18, 0.72);
|
color: #b6c7dc;
|
box-sizing: border-box;
|
font: 12px/1.45 Consolas, "Cascadia Mono", monospace;
|
backdrop-filter: blur(10px);
|
}
|
|
@media (max-width: 860px) {
|
.toolbar {
|
grid-template-columns: 1fr auto;
|
}
|
|
.status {
|
grid-column: 1 / -1;
|
}
|
|
.metrics {
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
}
|
|
.raw {
|
display: none;
|
}
|
}
|
</style>
|
</head>
|
<body>
|
<header class="toolbar">
|
<input id="wsUrl" class="url" value="ws://127.0.0.1:8765/ws/events" aria-label="WebSocket URL">
|
<button id="connect" class="button" type="button">Connect</button>
|
<div id="status" class="status" data-state="closed">
|
<span class="dot"></span>
|
<span id="statusText">未连接</span>
|
</div>
|
</header>
|
<main id="stage">
|
<canvas id="visualizer" class="visualizer"></canvas>
|
<div id="bars" class="bars"></div>
|
<section class="metrics" aria-label="WebSocket audio metrics">
|
<div class="metric">
|
<span class="label">EVENT</span>
|
<span id="eventName" class="value">-</span>
|
</div>
|
<div class="metric">
|
<span class="label">FRAMES</span>
|
<span id="frameCount" class="value">0</span>
|
</div>
|
<div class="metric">
|
<span class="label">RMS</span>
|
<span id="rmsValue" class="value">-</span>
|
</div>
|
<div class="metric">
|
<span class="label">LEVELS</span>
|
<span id="levelsCount" class="value">-</span>
|
</div>
|
</section>
|
<pre id="raw" class="raw">等待 ws://127.0.0.1:8765/ws/events 数据...</pre>
|
</main>
|
<script>
|
const barCount = 192;
|
const reconnectDelayMs = 1200;
|
const compressionPower = 0.52;
|
const smoothingAttack = 0.48;
|
const smoothingRelease = 0.18;
|
const gaussianSigma = 0.42;
|
const canvas = document.getElementById("visualizer");
|
const context = canvas.getContext("2d");
|
const barsRoot = document.getElementById("bars");
|
const wsUrlInput = document.getElementById("wsUrl");
|
const connectButton = document.getElementById("connect");
|
const statusNode = document.getElementById("status");
|
const statusText = document.getElementById("statusText");
|
const eventNameNode = document.getElementById("eventName");
|
const frameCountNode = document.getElementById("frameCount");
|
const rmsValueNode = document.getElementById("rmsValue");
|
const levelsCountNode = document.getElementById("levelsCount");
|
const rawNode = document.getElementById("raw");
|
const bars = Array.from({ length: barCount }, () => {
|
const node = document.createElement("i");
|
node.className = "bar";
|
barsRoot.appendChild(node);
|
return node;
|
});
|
let socket = null;
|
let reconnectTimer = 0;
|
let manualClose = false;
|
let rawLevels = new Array(64).fill(0);
|
let visualLevels = new Array(barCount).fill(0.01);
|
let smoothedLevels = new Array(barCount).fill(0.01);
|
let frames = 0;
|
|
function resizeCanvas() {
|
const rect = canvas.getBoundingClientRect();
|
const ratio = window.devicePixelRatio || 1;
|
const width = Math.max(1, Math.floor(rect.width * ratio));
|
const height = Math.max(1, Math.floor(rect.height * ratio));
|
if (canvas.width !== width || canvas.height !== height) {
|
canvas.width = width;
|
canvas.height = height;
|
}
|
context.setTransform(ratio, 0, 0, ratio, 0, 0);
|
}
|
|
function setStatus(state, text) {
|
statusNode.dataset.state = state;
|
statusText.textContent = text;
|
}
|
|
function connect() {
|
manualClose = false;
|
clearTimeout(reconnectTimer);
|
if (socket) {
|
socket.close();
|
}
|
|
setStatus("connecting", "连接中");
|
socket = new WebSocket(wsUrlInput.value.trim() || "ws://127.0.0.1:8765/ws/events");
|
socket.addEventListener("open", () => setStatus("open", "已连接"));
|
socket.addEventListener("message", event => handleMessage(event.data));
|
socket.addEventListener("error", () => setStatus("error", "连接错误"));
|
socket.addEventListener("close", () => {
|
setStatus("closed", manualClose ? "已断开" : "已断开,重连中");
|
if (!manualClose) {
|
reconnectTimer = window.setTimeout(connect, reconnectDelayMs);
|
}
|
});
|
}
|
|
function disconnect() {
|
manualClose = true;
|
clearTimeout(reconnectTimer);
|
if (socket) {
|
socket.close();
|
socket = null;
|
}
|
setStatus("closed", "已断开");
|
}
|
|
function handleMessage(text) {
|
let message;
|
try {
|
message = JSON.parse(text);
|
} catch {
|
rawNode.textContent = text;
|
return;
|
}
|
|
eventNameNode.textContent = message.name || "-";
|
rawNode.textContent = JSON.stringify(message, null, 2).slice(0, 1600);
|
if (message.name !== "audio.spectrum") {
|
return;
|
}
|
|
const payload = message.payload || {};
|
const nextLevels = payload.levels || payload.Levels;
|
if (!Array.isArray(nextLevels) || nextLevels.length === 0) {
|
return;
|
}
|
|
rawLevels = nextLevels.map(value => Math.max(0, Math.min(1, Number(value) || 0)));
|
visualLevels = processSpectrum(rawLevels);
|
const peak = Math.max(...rawLevels);
|
frames += 1;
|
frameCountNode.textContent = String(frames);
|
levelsCountNode.textContent = `${rawLevels.length} -> ${visualLevels.length} / peak ${peak.toFixed(3)}`;
|
rmsValueNode.textContent = Number(payload.rms ?? payload.Rms ?? 0).toFixed(4);
|
}
|
|
function processSpectrum(input) {
|
const binned = logBin(input, barCount);
|
const peak = Math.max(...binned);
|
const gain = peak > 0 ? Math.min(18, Math.max(2, 0.86 / peak)) : 1;
|
return binned.map((value, index) => {
|
const compressed = Math.pow(Math.min(1, value * gain), compressionPower);
|
return Math.max(0.01, Math.min(1, compressed * gaussianEnvelope(index, binned.length)));
|
});
|
}
|
|
function logBin(input, outputCount) {
|
if (input.length === 0) {
|
return new Array(outputCount).fill(0);
|
}
|
|
const last = input.length - 1;
|
const logMax = Math.log(input.length);
|
return Array.from({ length: outputCount }, (_, index) => {
|
const start = Math.max(0, Math.floor(Math.exp(logMax * (index / outputCount)) - 1));
|
const end = Math.min(last, Math.ceil(Math.exp(logMax * ((index + 1) / outputCount)) - 1));
|
let sum = 0;
|
let weight = 0;
|
for (let bin = start; bin <= end; bin++) {
|
const localWeight = 1 + (bin / Math.max(1, last)) * 0.35;
|
sum += (input[bin] || 0) * localWeight;
|
weight += localWeight;
|
}
|
return weight > 0 ? sum / weight : 0;
|
});
|
}
|
|
function gaussianEnvelope(index, count) {
|
const x = count > 1 ? (index / (count - 1)) * 2 - 1 : 0;
|
const gaussian = Math.exp(-(x * x) / (2 * gaussianSigma * gaussianSigma));
|
return 0.28 + gaussian * 0.82;
|
}
|
|
function sample(index) {
|
const target = visualLevels[index] || 0.01;
|
const current = smoothedLevels[index] || 0.01;
|
const rate = target > current ? smoothingAttack : smoothingRelease;
|
const value = current + (target - current) * rate;
|
smoothedLevels[index] = value;
|
return Math.max(0.01, Math.min(1, value));
|
}
|
|
function draw() {
|
resizeCanvas();
|
const frameLevels = visualLevels.map((_, index) => sample(index));
|
drawCanvas(frameLevels);
|
bars.forEach((bar, index) => {
|
const centerBias = Math.sin((index / Math.max(1, bars.length - 1)) * Math.PI);
|
const value = Math.min(1, frameLevels[index] * (0.72 + centerBias * 0.36));
|
const height = value * 46;
|
const top = Math.max(0, 60 - height);
|
bar.style.setProperty("--bar-height", `${height.toFixed(2)}%`);
|
bar.style.setProperty("--bar-top", `${top.toFixed(2)}%`);
|
bar.style.setProperty("--bar-opacity", (0.24 + value * 0.76).toFixed(3));
|
bar.style.setProperty("--reflection-height", `${(value * 26).toFixed(2)}vh`);
|
bar.style.setProperty("--reflection-opacity", (value * 0.34).toFixed(3));
|
});
|
requestAnimationFrame(draw);
|
}
|
|
function drawCanvas(frameLevels) {
|
const width = canvas.clientWidth;
|
const height = canvas.clientHeight;
|
const baseY = height * 0.6;
|
context.clearRect(0, 0, width, height);
|
|
const lineGradient = context.createLinearGradient(0, 0, width, 0);
|
lineGradient.addColorStop(0, "rgba(255,255,255,0)");
|
lineGradient.addColorStop(0.5, "rgba(255,255,255,0.72)");
|
lineGradient.addColorStop(1, "rgba(255,255,255,0)");
|
context.fillStyle = lineGradient;
|
context.shadowBlur = 18;
|
context.shadowColor = "rgba(255, 47, 179, 0.55)";
|
context.fillRect(0, baseY, width, 1);
|
context.shadowBlur = 0;
|
|
const gap = 2;
|
const barWidth = Math.max(2, Math.min(7, (width - gap * (barCount - 1)) / barCount));
|
for (let index = 0; index < barCount; index++) {
|
const centerBias = Math.sin((index / Math.max(1, barCount - 1)) * Math.PI);
|
const value = Math.min(1, frameLevels[index] * (0.72 + centerBias * 0.36));
|
const barHeight = Math.max(2, value * height * 0.48);
|
const x = index * (barWidth + gap);
|
const y = baseY - barHeight;
|
const gradient = context.createLinearGradient(0, y, 0, baseY);
|
gradient.addColorStop(0, "#f8fbff");
|
gradient.addColorStop(0.36, "#ff2fb3");
|
gradient.addColorStop(1, "#29d8ff");
|
context.globalAlpha = 0.28 + value * 0.72;
|
context.fillStyle = gradient;
|
context.shadowBlur = 13;
|
context.shadowColor = "rgba(255, 47, 179, 0.5)";
|
roundRect(context, x, y, barWidth, barHeight, barWidth / 2);
|
context.fill();
|
|
context.globalAlpha = value * 0.24;
|
context.shadowBlur = 0;
|
context.fillStyle = "rgba(255, 47, 179, 0.38)";
|
roundRect(context, x, baseY, barWidth, barHeight * 0.62, barWidth / 2);
|
context.fill();
|
}
|
context.globalAlpha = 1;
|
context.shadowBlur = 0;
|
}
|
|
function roundRect(target, x, y, width, height, radius) {
|
const safeRadius = Math.min(radius, width / 2, height / 2);
|
target.beginPath();
|
target.moveTo(x + safeRadius, y);
|
target.lineTo(x + width - safeRadius, y);
|
target.quadraticCurveTo(x + width, y, x + width, y + safeRadius);
|
target.lineTo(x + width, y + height - safeRadius);
|
target.quadraticCurveTo(x + width, y + height, x + width - safeRadius, y + height);
|
target.lineTo(x + safeRadius, y + height);
|
target.quadraticCurveTo(x, y + height, x, y + height - safeRadius);
|
target.lineTo(x, y + safeRadius);
|
target.quadraticCurveTo(x, y, x + safeRadius, y);
|
target.closePath();
|
}
|
|
connectButton.addEventListener("click", () => {
|
if (socket && socket.readyState === WebSocket.OPEN) {
|
disconnect();
|
connectButton.textContent = "Connect";
|
return;
|
}
|
|
connect();
|
connectButton.textContent = "Disconnect";
|
});
|
|
wsUrlInput.addEventListener("keydown", event => {
|
if (event.key === "Enter") {
|
connect();
|
connectButton.textContent = "Disconnect";
|
}
|
});
|
|
window.addEventListener("resize", resizeCanvas);
|
draw();
|
connect();
|
connectButton.textContent = "Disconnect";
|
</script>
|
</body>
|
</html>
|