基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-04 ddd6889459bccf5015cd0c9629cdf4f4ce8fb8e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
<!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>