| | |
| | | <canvas id="visualizer"></canvas> |
| | | </main> |
| | | <script> |
| | | const DISPLAY_BAR_COUNT = 256; |
| | | const FALLBACK_LEVEL_COUNT = 128; |
| | | const canvas = document.getElementById("visualizer"); |
| | | const ctx = canvas.getContext("2d", { alpha: true }); |
| | | const settings = { |
| | | const defaultSettings = { |
| | | barCount: 256, |
| | | fallbackLevelCount: 128, |
| | | primaryColor: "#ff2fb3", |
| | | secondaryColor: "#29d8ff", |
| | | accentColor: "#f8fbff", |
| | | sensitivity: 5.5, |
| | | centerYRatio: 0.66, |
| | | maxHeightRatio: 3.5, |
| | | maxHeightRatio: 0.5, |
| | | smooth: 0.3, |
| | | decay: 0.5, |
| | | compressionPower: 0.7 |
| | | 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(FALLBACK_LEVEL_COUNT).fill(0); |
| | | let displayLevels = new Array(DISPLAY_BAR_COUNT).fill(0.01); |
| | | let smoothedLevels = new Array(DISPLAY_BAR_COUNT).fill(0.01); |
| | | 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; |
| | |
| | | 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); |
| | |
| | | } |
| | | |
| | | function applyConfig(config) { |
| | | Object.assign(settings, config || {}); |
| | | settings.sensitivity = clamp(settings.sensitivity, 0.2, 6); |
| | | 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.48); |
| | | 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() { |
| | |
| | | } |
| | | |
| | | sourceLevels = levels.map(value => clamp(value, 0, 1)); |
| | | displayLevels = buildBars(sourceLevels, DISPLAY_BAR_COUNT); |
| | | displayLevels = buildBars(sourceLevels, settings.barCount); |
| | | lastHostSampleAt = performance.now(); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | function calcMidBar(levels, t, count, bassEnd, midEnd, highEnd) { |
| | | const p = t / 0.35; |
| | | 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 { |
| | |
| | | } |
| | | |
| | | function calcTrebleBar(levels, t, count, bassEnd, midEnd, highEnd) { |
| | | const p = (t - 0.35) / 0.23; |
| | | 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 { |
| | |
| | | } |
| | | |
| | | function calcBassBar(levels, t, count, bassEnd, midEnd, highEnd) { |
| | | const p = (t - 0.58) / 0.24; |
| | | 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)); |
| | |
| | | } |
| | | |
| | | function calcTailBar(levels, t, count, bassEnd, midEnd, highEnd) { |
| | | const p = (t - 0.82) / 0.18; |
| | | 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)); |
| | |
| | | } |
| | | |
| | | function getBarByPosition(levels, t, count, bassEnd, midEnd, highEnd) { |
| | | const blend58 = 0.07; |
| | | const blend82 = 0.035; |
| | | if (t < 0.35) { |
| | | 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 < 0.58 - blend58) { |
| | | if (t < trebleEndPosition - blendTreble) { |
| | | return calcTrebleBar(levels, t, count, bassEnd, midEnd, highEnd); |
| | | } |
| | | if (t < 0.58 + blend58) { |
| | | const k = (t - (0.58 - blend58)) / (blend58 * 2); |
| | | 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 < 0.82 - blend82) { |
| | | if (t < bassEndPosition - blendTail) { |
| | | return calcBassBar(levels, t, count, bassEnd, midEnd, highEnd); |
| | | } |
| | | if (t < 0.82 + blend82) { |
| | | const k = (t - (0.82 - blend82)) / (blend82 * 2); |
| | | 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), |
| | |
| | | } |
| | | |
| | | function applyBandGain(value, band) { |
| | | if (band === "bass") return value * 2.15; |
| | | if (band === "mid") return value * 1.75; |
| | | if (band === "treble") return value * 2.35; |
| | | if (band === "tail") return value * 1.95; |
| | | return value; |
| | | return value * safeNumber(settings.bandGains[band], 1); |
| | | } |
| | | |
| | | function applyCompression(value) { |
| | |
| | | } |
| | | |
| | | function buildBars(levels, count) { |
| | | const inputCount = levels.length || FALLBACK_LEVEL_COUNT; |
| | | const bassEnd = Math.floor(inputCount * 0.16); |
| | | const midEnd = Math.floor(inputCount * 0.55); |
| | | 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); |
| | |
| | | } |
| | | |
| | | function buildFallbackLevels() { |
| | | phase += 0.018; |
| | | const levels = new Array(FALLBACK_LEVEL_COUNT); |
| | | 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); |
| | |
| | | function colorByPosition(t) { |
| | | // const hue = lerp(330, 292, smoothstep(0, 1, t)); |
| | | // return `hsl(${hue}, 100%, ${lerp(62, 70, t)}%)`; |
| | | const hue = lerp(330, 292, smoothstep(0, 1, t)); |
| | | return `hsla(${hue}, 100%, ${lerp(62, 70, t)}%, 0.35)`; |
| | | 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) { |
| | |
| | | |
| | | function draw() { |
| | | resize(); |
| | | if (performance.now() - lastHostSampleAt > 900) { |
| | | if (performance.now() - lastHostSampleAt > settings.fallbackTimeoutMs) { |
| | | acceptLevels(buildFallbackLevels()); |
| | | } |
| | | |
| | | ctx.clearRect(0, 0, width, height); |
| | | const centerY = height * settings.centerYRatio; |
| | | // const maxHalfHeight = height * settings.maxHeightRatio; |
| | | const maxHalfHeight = height / 2; |
| | | const maxHalfHeight = height * settings.maxHeightRatio; |
| | | drawBackground(centerY); |
| | | |
| | | const energy = sourceLevels.reduce((sum, value) => sum + value, 0); |
| | | beat = energy > lastEnergy * 1.12 ? 1 : beat * 0.9; |
| | | beat = energy > lastEnergy * settings.beatThreshold ? 1 : beat * settings.beatDecay; |
| | | lastEnergy = energy; |
| | | |
| | | const SILENCE_THRESHOLD = 0.001; |
| | | const isSilent = energy < SILENCE_THRESHOLD; |
| | | 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, "rgba(255,255,255,0.72)"); |
| | | line.addColorStop(0.5, colorWithAlpha(settings.accentColor, settings.lineOpacity)); |
| | | line.addColorStop(1, "rgba(255,255,255,0)"); |
| | | ctx.save(); |
| | | ctx.fillStyle = line; |
| | | ctx.shadowBlur = 10; |
| | | ctx.shadowColor = "rgba(255,255,255,0.65)"; |
| | | ctx.shadowBlur = settings.lineShadowBlur; |
| | | ctx.shadowColor = colorWithAlpha(settings.accentColor, settings.lineShadowOpacity); |
| | | ctx.fillRect(0, centerY - 0.5, width, 1); |
| | | ctx.restore(); |
| | | |
| | | const gap = width < 900 ? 2 : 3; |
| | | const barWidth = Math.max(1.2, Math.min(5, (width - gap * (DISPLAY_BAR_COUNT - 1)) / DISPLAY_BAR_COUNT)); |
| | | const totalWidth = DISPLAY_BAR_COUNT * barWidth + (DISPLAY_BAR_COUNT - 1) * gap; |
| | | 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) { |
| | |
| | | return; |
| | | } |
| | | |
| | | for (let index = 0; index < DISPLAY_BAR_COUNT; index++) { |
| | | const t = index / Math.max(1, DISPLAY_BAR_COUNT - 1); |
| | | const target = displayLevels[index] * (t > 0.58 && t < 0.82 ? 1 + beat * 0.34 : t >= 0.35 && t < 0.58 ? 1 + beat * 0.1 : 1); |
| | | 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(1.2, value * maxHalfHeight); |
| | | 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, "rgba(255,255,255,0.35)"); |
| | | gradient.addColorStop(0, colorWithAlpha(settings.accentColor, settings.gradientTopAlpha)); |
| | | gradient.addColorStop(0.28, color); |
| | | gradient.addColorStop(0.72, color); |
| | | gradient.addColorStop(1, "rgba(0,230,255,0.35)"); |
| | | gradient.addColorStop(1, colorWithAlpha(settings.secondaryColor, settings.gradientBottomAlpha)); |
| | | |
| | | ctx.save(); |
| | | ctx.fillStyle = gradient; |
| | | ctx.shadowBlur = 8; |
| | | ctx.shadowBlur = settings.barShadowBlur; |
| | | ctx.shadowColor = color; |
| | | roundedBar(x, centerY - barHeight, barWidth, barHeight * 2, Math.min(3, barWidth / 2)); |
| | | roundedBar(x, centerY - barHeight, barWidth, barHeight * 2, Math.min(settings.barRadiusMax, barWidth / 2)); |
| | | ctx.fill(); |
| | | ctx.restore(); |
| | | x += barWidth + gap; |