基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-03 9806138b910904d2f982ebf9471b295fb6939715
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
<!doctype html>
<html lang="zh-CN">
 
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Music Visualizer</title>
  <style>
    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0;
      overflow: hidden;
      background: transparent;
      user-select: none;
    }
 
    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.45), rgba(20, 0, 35, 0.45)); */
      background: transparent;
    }
 
    #stage {
      position: fixed;
      inset: 0;
      width: 100vw;
      height: 100vh;
      overflow: hidden;
      background: transparent;
    }
 
    #visualizer {
      position: absolute;
      inset: 0;
      width: 100%;
      height: 100%;
      display: block;
    }
  </style>
</head>
 
<body>
  <main id="stage" aria-hidden="true">
    <canvas id="visualizer"></canvas>
  </main>
  <script>
    const canvas = document.getElementById("visualizer");
    const ctx = canvas.getContext("2d", { alpha: true });
    const defaultSettings = {
      barCount: 256,
      fallbackLevelCount: 128,
      primaryColor: "#ff2fb3",
      secondaryColor: "#29d8ff",
      accentColor: "#f8fbff",
      sensitivity: 5.5,
      centerYRatio: 0.66,
      maxHeightRatio: 0.5,
      smooth: 0.3,
      decay: 0.5,
      compressionPower: 0.7,
      silenceThreshold: 0.001,
      fallbackTimeoutMs: 900,
      fallbackPhaseStep: 0.018,
      beatThreshold: 1.12,
      beatDecay: 0.9,
      beatBassBoost: 0.34,
      beatTrebleBoost: 0.1,
      barGapSmall: 2,
      barGapLarge: 3,
      barGapBreakpoint: 900,
      barWidthMin: 1.2,
      barWidthMax: 5,
      barRadiusMax: 3,
      barMinHeight: 1.2,
      lineOpacity: 0.72,
      lineShadowBlur: 10,
      lineShadowOpacity: 0.65,
      barShadowBlur: 8,
      colorHueStart: 330,
      colorHueEnd: 292,
      colorLightnessStart: 62,
      colorLightnessEnd: 70,
      colorAlpha: 0.35,
      gradientTopAlpha: 0.35,
      gradientBottomAlpha: 0.35,
      bandGains: {
        bass: 2.15,
        mid: 1.75,
        treble: 2.35,
        tail: 1.95
      },
      bandStops: {
        midEnd: 0.35,
        trebleEnd: 0.58,
        bassEnd: 0.82,
        trebleBlend: 0.07,
        tailBlend: 0.035,
        bassFrequencyEnd: 0.16,
        midFrequencyEnd: 0.55
      }
    };
    const settings = structuredClone(defaultSettings);
 
    let width = 1;
    let height = 1;
    let ratio = 1;
    let sourceLevels = new Array(settings.fallbackLevelCount).fill(0);
    let displayLevels = new Array(settings.barCount).fill(0.01);
    let smoothedLevels = new Array(settings.barCount).fill(0.01);
    let lastHostSampleAt = 0;
    let lastEnergy = 0;
    let beat = 0;
    let phase = 0;
 
    function safeNumber(value, fallback = 0) {
      const number = Number(value);
      return Number.isFinite(number) ? number : fallback;
    }
 
    function clamp(value, min, max) {
      return Math.max(min, Math.min(max, safeNumber(value, min)));
    }
 
    function lerp(a, b, t) {
      return a + (b - a) * t;
    }
 
    function smoothstep(edge0, edge1, x) {
      const span = edge1 - edge0 || 1;
      x = clamp((x - edge0) / span, 0, 1);
      return x * x * (3 - 2 * x);
    }
 
    function parseHexColor(color) {
      const value = String(color || "").trim();
      const match = value.match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i);
      if (!match) {
        return null;
      }
 
      const hex = match[1].length === 3
        ? match[1].split("").map(char => char + char).join("")
        : match[1];
      return {
        r: parseInt(hex.slice(0, 2), 16),
        g: parseInt(hex.slice(2, 4), 16),
        b: parseInt(hex.slice(4, 6), 16)
      };
    }
 
    function colorWithAlpha(color, alpha) {
      const rgb = parseHexColor(color);
      return rgb ? `rgba(${rgb.r},${rgb.g},${rgb.b},${alpha})` : color;
    }
 
    function mixHexColor(start, end, t, alpha) {
      const a = parseHexColor(start);
      const b = parseHexColor(end);
      if (!a || !b) {
        return null;
      }
 
      const k = smoothstep(0, 1, t);
      const r = Math.round(lerp(a.r, b.r, k));
      const g = Math.round(lerp(a.g, b.g, k));
      const blue = Math.round(lerp(a.b, b.b, k));
      return `rgba(${r},${g},${blue},${alpha})`;
    }
 
    function clampInt(value, min, max, fallback) {
      return Math.round(clamp(safeNumber(value, fallback), min, max));
    }
 
    function mergeSettings(config) {
      if (!config || typeof config !== "object") {
        return;
      }
 
      Object.assign(settings, config);
      settings.bandGains = {
        ...defaultSettings.bandGains,
        ...(config.bandGains || {})
      };
      settings.bandStops = {
        ...defaultSettings.bandStops,
        ...(config.bandStops || {})
      };
    }
 
    function syncLevelBuffers() {
      const nextBarCount = settings.barCount;
      if (displayLevels.length !== nextBarCount) {
        displayLevels = buildBars(sourceLevels, nextBarCount);
        smoothedLevels = new Array(nextBarCount).fill(0.01);
      }
 
      if (!sourceLevels.length) {
        sourceLevels = new Array(settings.fallbackLevelCount).fill(0);
      }
    }
 
    function resize() {
      ratio = window.devicePixelRatio || 1;
      width = Math.max(1, window.innerWidth);
      height = Math.max(1, window.innerHeight);
      canvas.width = Math.floor(width * ratio);
      canvas.height = Math.floor(height * ratio);
      canvas.style.width = `${width}px`;
      canvas.style.height = `${height}px`;
      ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
    }
 
    function applyConfig(config) {
      mergeSettings(config);
      settings.barCount = clampInt(settings.barCount, 8, 1024, defaultSettings.barCount);
      settings.fallbackLevelCount = clampInt(settings.fallbackLevelCount, 8, 512, defaultSettings.fallbackLevelCount);
      settings.sensitivity = clamp(settings.sensitivity, 0.2, 8);
      settings.centerYRatio = clamp(settings.centerYRatio, 0.2, 0.9);
      settings.maxHeightRatio = clamp(settings.maxHeightRatio, 0.06, 0.5);
      settings.smooth = clamp(settings.smooth, 0.01, 1);
      settings.decay = clamp(settings.decay, 0.01, 0.99);
      settings.compressionPower = clamp(settings.compressionPower, 0.2, 2);
      settings.silenceThreshold = clamp(settings.silenceThreshold, 0, 0.1);
      settings.fallbackTimeoutMs = clamp(settings.fallbackTimeoutMs, 120, 10000);
      settings.fallbackPhaseStep = clamp(settings.fallbackPhaseStep, 0.001, 0.12);
      settings.beatThreshold = clamp(settings.beatThreshold, 1.01, 2);
      settings.beatDecay = clamp(settings.beatDecay, 0.01, 0.99);
      settings.beatBassBoost = clamp(settings.beatBassBoost, 0, 2);
      settings.beatTrebleBoost = clamp(settings.beatTrebleBoost, 0, 2);
      settings.barGapSmall = clamp(settings.barGapSmall, 0, 24);
      settings.barGapLarge = clamp(settings.barGapLarge, 0, 24);
      settings.barGapBreakpoint = clamp(settings.barGapBreakpoint, 320, 4096);
      settings.barWidthMin = clamp(settings.barWidthMin, 0.5, 64);
      settings.barWidthMax = Math.max(settings.barWidthMin, clamp(settings.barWidthMax, 0.5, 96));
      settings.barRadiusMax = clamp(settings.barRadiusMax, 0, 48);
      settings.barMinHeight = clamp(settings.barMinHeight, 0.5, 16);
      settings.lineOpacity = clamp(settings.lineOpacity, 0, 1);
      settings.lineShadowBlur = clamp(settings.lineShadowBlur, 0, 64);
      settings.lineShadowOpacity = clamp(settings.lineShadowOpacity, 0, 1);
      settings.barShadowBlur = clamp(settings.barShadowBlur, 0, 64);
      settings.colorHueStart = clamp(settings.colorHueStart, 0, 360);
      settings.colorHueEnd = clamp(settings.colorHueEnd, 0, 360);
      settings.colorLightnessStart = clamp(settings.colorLightnessStart, 0, 100);
      settings.colorLightnessEnd = clamp(settings.colorLightnessEnd, 0, 100);
      settings.colorAlpha = clamp(settings.colorAlpha, 0, 1);
      settings.gradientTopAlpha = clamp(settings.gradientTopAlpha, 0, 1);
      settings.gradientBottomAlpha = clamp(settings.gradientBottomAlpha, 0, 1);
      settings.bandStops.midEnd = clamp(settings.bandStops.midEnd, 0.05, 0.9);
      settings.bandStops.trebleEnd = clamp(settings.bandStops.trebleEnd, settings.bandStops.midEnd, 0.95);
      settings.bandStops.bassEnd = clamp(settings.bandStops.bassEnd, settings.bandStops.trebleEnd, 0.98);
      settings.bandStops.trebleBlend = clamp(settings.bandStops.trebleBlend, 0, 0.2);
      settings.bandStops.tailBlend = clamp(settings.bandStops.tailBlend, 0, 0.2);
      settings.bandStops.bassFrequencyEnd = clamp(settings.bandStops.bassFrequencyEnd, 0.02, 0.45);
      settings.bandStops.midFrequencyEnd = clamp(settings.bandStops.midFrequencyEnd, settings.bandStops.bassFrequencyEnd, 0.95);
      for (const band of Object.keys(settings.bandGains)) {
        settings.bandGains[band] = clamp(settings.bandGains[band], 0, 8);
      }
      syncLevelBuffers();
    }
 
    async function loadHostConfig() {
      if (!window.moliDesktop?.getConfig) {
        return;
      }
 
      try {
        applyConfig(await window.moliDesktop.getConfig());
        await window.moliDesktop.notify?.({
          level: "info",
          message: "Audio visualizer loaded through moliDesktop bridge."
        });
      } catch {
        // The visualizer should keep rendering even if a host bridge call fails.
      }
    }
 
    function acceptLevels(levels) {
      if (!Array.isArray(levels) || levels.length === 0) {
        return;
      }
 
      sourceLevels = levels.map(value => clamp(value, 0, 1));
      displayLevels = buildBars(sourceLevels, settings.barCount);
      lastHostSampleAt = performance.now();
    }
 
    function sampleLevel(levels, index) {
      if (!Array.isArray(levels) || levels.length === 0) {
        return 0;
      }
 
      const last = levels.length - 1;
      const safeIndex = clamp(index, 0, last);
      const i0 = Math.floor(safeIndex);
      const i1 = Math.min(last, i0 + 1);
      const t = safeIndex - i0;
      return lerp(safeNumber(levels[i0]), safeNumber(levels[i1]), t);
    }
 
    function calcMidBar(levels, t, count, bassEnd, midEnd, highEnd) {
      const p = t / settings.bandStops.midEnd;
      const mid = sampleLevel(levels, lerp(bassEnd, midEnd, Math.pow(p, 0.9)));
      const high = sampleLevel(levels, lerp(midEnd, highEnd, Math.pow(p, 0.7)));
      return {
        value: mid * 0.72 + high * 0.28,
        shape: 0.06 + p * 0.1 + (0.18 + Math.pow(p, 0.9) * 0.48) * smoothstep(0, 0.42, p),
        band: "mid"
      };
    }
 
    function calcTrebleBar(levels, t, count, bassEnd, midEnd, highEnd) {
      const p = (t - settings.bandStops.midEnd) / (settings.bandStops.trebleEnd - settings.bandStops.midEnd || 1);
      const high = sampleLevel(levels, lerp(midEnd, highEnd, Math.pow(p, 0.8)));
      const mid = sampleLevel(levels, lerp(midEnd * 0.75, midEnd, p));
      return {
        value: high * 0.82 + mid * 0.18,
        shape: 0.22 + Math.sin(p * Math.PI) * 0.28,
        band: "treble"
      };
    }
 
    function calcBassBar(levels, t, count, bassEnd, midEnd, highEnd) {
      const p = (t - settings.bandStops.trebleEnd) / (settings.bandStops.bassEnd - settings.bandStops.trebleEnd || 1);
      const bass = sampleLevel(levels, lerp(0, bassEnd, Math.pow(p, 0.55)));
      const midBass = sampleLevel(levels, lerp(bassEnd, Math.floor(count * 0.32), p));
      const high = sampleLevel(levels, lerp(midEnd, highEnd, p));
      const peak = Math.exp(-Math.pow((p - 0.58) / 0.36, 2));
      return {
        value: bass * 0.58 + midBass * 0.24 + high * 0.18,
        shape: 0.16 + smoothstep(0, 0.38, p) * (0.08 + peak * 0.72),
        band: "bass"
      };
    }
 
    function calcTailBar(levels, t, count, bassEnd, midEnd, highEnd) {
      const p = (t - settings.bandStops.bassEnd) / (1 - settings.bandStops.bassEnd || 1);
      const high = sampleLevel(levels, lerp(midEnd, highEnd, p));
      const bass = sampleLevel(levels, bassEnd * 0.45);
      const mid = sampleLevel(levels, lerp(bassEnd, midEnd * 0.7, 1 - p));
      return {
        value: high * 0.62 + bass * 0.16 + mid * 0.1,
        shape: Math.pow(1 - p, 1.65) * 0.38 + 0.06,
        band: "tail"
      };
    }
 
    function mixBar(a, b, k) {
      const ratio = smoothstep(0, 1, k);
      return {
        value: lerp(a.value, b.value, ratio),
        shape: lerp(a.shape, b.shape, ratio),
        band: ratio < 0.5 ? a.band : b.band
      };
    }
 
    function getBarByPosition(levels, t, count, bassEnd, midEnd, highEnd) {
      const midEndPosition = settings.bandStops.midEnd;
      const trebleEndPosition = settings.bandStops.trebleEnd;
      const bassEndPosition = settings.bandStops.bassEnd;
      const blendTreble = settings.bandStops.trebleBlend;
      const blendTail = settings.bandStops.tailBlend;
      if (t < midEndPosition) {
        return calcMidBar(levels, t, count, bassEnd, midEnd, highEnd);
      }
      if (t < trebleEndPosition - blendTreble) {
        return calcTrebleBar(levels, t, count, bassEnd, midEnd, highEnd);
      }
      if (t < trebleEndPosition + blendTreble) {
        const k = blendTreble > 0 ? (t - (trebleEndPosition - blendTreble)) / (blendTreble * 2) : 1;
        return mixBar(
          calcTrebleBar(levels, t, count, bassEnd, midEnd, highEnd),
          calcBassBar(levels, t, count, bassEnd, midEnd, highEnd),
          k);
      }
      if (t < bassEndPosition - blendTail) {
        return calcBassBar(levels, t, count, bassEnd, midEnd, highEnd);
      }
      if (t < bassEndPosition + blendTail) {
        const k = blendTail > 0 ? (t - (bassEndPosition - blendTail)) / (blendTail * 2) : 1;
        return mixBar(
          calcBassBar(levels, t, count, bassEnd, midEnd, highEnd),
          calcTailBar(levels, t, count, bassEnd, midEnd, highEnd),
          k);
      }
      return calcTailBar(levels, t, count, bassEnd, midEnd, highEnd);
    }
 
    function applyBandGain(value, band) {
      return value * safeNumber(settings.bandGains[band], 1);
    }
 
    function applyCompression(value) {
      const compressed = Math.pow(value * settings.sensitivity, settings.compressionPower);
      return compressed / (1 + compressed);
    }
 
    function buildBars(levels, count) {
      const inputCount = levels.length || settings.fallbackLevelCount;
      const bassEnd = Math.floor(inputCount * settings.bandStops.bassFrequencyEnd);
      const midEnd = Math.floor(inputCount * settings.bandStops.midFrequencyEnd);
      const highEnd = inputCount - 1;
      return Array.from({ length: count }, (_, index) => {
        const t = index / Math.max(1, count - 1);
        const item = getBarByPosition(levels, t, inputCount, bassEnd, midEnd, highEnd);
        let value = applyCompression(applyBandGain(item.value, item.band));
        const grain = 0.92 + 0.06 * Math.sin(index * 0.47) + 0.04 * Math.sin(index * 1.13);
        const sparkle = item.band === "treble" || item.band === "tail"
          ? 0.92 + 0.12 * Math.sin(performance.now() * 0.018 + index * 0.9)
          : 1;
        return clamp(value * item.shape * grain * sparkle, 0.01, 1);
      });
    }
 
    function buildFallbackLevels() {
      phase += settings.fallbackPhaseStep;
      const levels = new Array(settings.fallbackLevelCount);
      for (let index = 0; index < levels.length; index++) {
        const t = index / Math.max(1, levels.length - 1);
        const bass = Math.exp(-Math.pow((t - 0.12) / 0.12, 2)) * (0.35 + Math.sin(phase * 1.7) * 0.25);
        const mid = Math.exp(-Math.pow((t - 0.48) / 0.18, 2)) * (0.22 + Math.sin(phase * 2.8 + index * 0.08) * 0.16);
        const high = Math.exp(-Math.pow((t - 0.78) / 0.22, 2)) * (0.16 + Math.sin(phase * 5.2 + index * 0.3) * 0.12);
        levels[index] = clamp(0.02 + bass + mid + high, 0, 1);
      }
      return levels;
    }
 
    function colorByPosition(t) {
      // const hue = lerp(330, 292, smoothstep(0, 1, t));
      // return `hsl(${hue}, 100%, ${lerp(62, 70, t)}%)`;
      const mixed = mixHexColor(settings.primaryColor, settings.secondaryColor, t, settings.colorAlpha);
      if (mixed) {
        return mixed;
      }
 
      const hue = lerp(settings.colorHueStart, settings.colorHueEnd, smoothstep(0, 1, t));
      const lightness = lerp(settings.colorLightnessStart, settings.colorLightnessEnd, t);
      return `hsla(${hue}, 100%, ${lightness}%, ${settings.colorAlpha})`;
    }
 
    function roundedBar(x, y, barWidth, barHeight, radius) {
      const safeRadius = Math.min(radius, barWidth / 2, barHeight / 2);
      ctx.beginPath();
      ctx.moveTo(x + safeRadius, y);
      ctx.lineTo(x + barWidth - safeRadius, y);
      ctx.quadraticCurveTo(x + barWidth, y, x + barWidth, y + safeRadius);
      ctx.lineTo(x + barWidth, y + barHeight - safeRadius);
      ctx.quadraticCurveTo(x + barWidth, y + barHeight, x + barWidth - safeRadius, y + barHeight);
      ctx.lineTo(x + safeRadius, y + barHeight);
      ctx.quadraticCurveTo(x, y + barHeight, x, y + barHeight - safeRadius);
      ctx.lineTo(x, y + safeRadius);
      ctx.quadraticCurveTo(x, y, x + safeRadius, y);
      ctx.closePath();
    }
 
    function drawBackground(centerY) {
      // const cyan = ctx.createRadialGradient(width * 0.68, centerY, 0, width * 0.68, centerY, width * 0.22);
      // cyan.addColorStop(0, "rgba(0, 220, 255, 0.18)");
      // cyan.addColorStop(0.45, "rgba(0, 120, 255, 0.08)");
      // cyan.addColorStop(1, "rgba(0, 0, 0, 0)");
      // ctx.fillStyle = cyan;
      // ctx.fillRect(0, 0, width, height);
 
      // const magenta = ctx.createRadialGradient(width * 0.5, centerY, 0, width * 0.5, centerY, width * 0.42);
      // magenta.addColorStop(0, "rgba(255, 0, 220, 0.12)");
      // magenta.addColorStop(0.55, "rgba(110, 0, 255, 0.07)");
      // magenta.addColorStop(1, "rgba(0, 0, 0, 0)");
      // ctx.fillStyle = magenta;
      // ctx.fillRect(0, 0, width, height);
    }
 
    function draw() {
      resize();
      if (performance.now() - lastHostSampleAt > settings.fallbackTimeoutMs) {
        // acceptLevels(buildFallbackLevels());
      }
 
      ctx.clearRect(0, 0, width, height);
      const centerY = height * settings.centerYRatio;
      const maxHalfHeight = height * settings.maxHeightRatio;
      drawBackground(centerY);
 
      const energy = sourceLevels.reduce((sum, value) => sum + value, 0);
      beat = energy > lastEnergy * settings.beatThreshold ? 1 : beat * settings.beatDecay;
      lastEnergy = energy;
 
      const isSilent = energy < settings.silenceThreshold;
 
      const line = ctx.createLinearGradient(0, 0, width, 0);
      line.addColorStop(0, "rgba(255,255,255,0)");
      line.addColorStop(0.5, colorWithAlpha(settings.accentColor, settings.lineOpacity));
      line.addColorStop(1, "rgba(255,255,255,0)");
      ctx.save();
      ctx.fillStyle = line;
      ctx.shadowBlur = settings.lineShadowBlur;
      ctx.shadowColor = colorWithAlpha(settings.accentColor, settings.lineShadowOpacity);
      ctx.fillRect(0, centerY - 0.5, width, 1);
      ctx.restore();
 
      const barCount = settings.barCount;
      const gap = width < settings.barGapBreakpoint ? settings.barGapSmall : settings.barGapLarge;
      const barWidth = Math.max(settings.barWidthMin, Math.min(settings.barWidthMax, (width - gap * (barCount - 1)) / barCount));
      const totalWidth = barCount * barWidth + (barCount - 1) * gap;
      let x = (width - totalWidth) / 2;
 
      if (isSilent) {
        requestAnimationFrame(draw);
        return;
      }
 
      for (let index = 0; index < barCount; index++) {
        const t = index / Math.max(1, barCount - 1);
        const target = displayLevels[index] * (t > settings.bandStops.trebleEnd && t < settings.bandStops.bassEnd
          ? 1 + beat * settings.beatBassBoost
          : t >= settings.bandStops.midEnd && t < settings.bandStops.trebleEnd
            ? 1 + beat * settings.beatTrebleBoost
            : 1);
        smoothedLevels[index] = Math.max(
          target * settings.smooth + smoothedLevels[index] * (1 - settings.smooth),
          smoothedLevels[index] * settings.decay);
 
        const value = clamp(smoothedLevels[index], 0.01, 1);
        const barHeight = Math.max(settings.barMinHeight, value * maxHalfHeight);
        const color = colorByPosition(t);
        const gradient = ctx.createLinearGradient(0, centerY - barHeight, 0, centerY + barHeight);
        // gradient.addColorStop(0, "rgba(255,255,255,0.95)");
        // gradient.addColorStop(0.28, color);
        // gradient.addColorStop(0.72, color);
        // gradient.addColorStop(1, "rgba(0,230,255,0.85)");
        gradient.addColorStop(0, colorWithAlpha(settings.accentColor, settings.gradientTopAlpha));
        gradient.addColorStop(0.28, color);
        gradient.addColorStop(0.72, color);
        gradient.addColorStop(1, colorWithAlpha(settings.secondaryColor, settings.gradientBottomAlpha));
 
        ctx.save();
        ctx.fillStyle = gradient;
        ctx.shadowBlur = settings.barShadowBlur;
        ctx.shadowColor = color;
        roundedBar(x, centerY - barHeight, barWidth, barHeight * 2, Math.min(settings.barRadiusMax, barWidth / 2));
        ctx.fill();
        ctx.restore();
        x += barWidth + gap;
      }
 
      requestAnimationFrame(draw);
    }
 
    window.addEventListener("resize", resize);
    window.addEventListener("moli:widget-loaded", loadHostConfig);
    window.addEventListener("moli:config-changed", event => applyConfig(event.detail));
    window.addEventListener("moli:audio-spectrum", event => acceptLevels(event.detail?.levels || event.detail?.Levels));
 
    applyConfig();
    loadHostConfig();
    resize();
    draw();
  </script>
</body>
 
</html>