{"version":3,"file":"math.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/VoiceVisualization/math.ts"],"sourcesContent":["import { snoise } from './simplex';\nimport type { Hsv } from './types';\n\n/** The shader math the Canvas renderers run on the CPU, once per frame. */\n\nexport const mix = (a: number, b: number, t: number) => a + (b - a) * t;\n\nexport const fract = (x: number) => x - Math.floor(x);\n\nexport const clamp = (x: number, low: number, high: number) =>\n  Math.min(Math.max(x, low), high);\n\n/** GLSL's smoothstep, evaluated the same with its edges either way round. */\nexport const smoothstep = (edge0: number, edge1: number, x: number) => {\n  const t = clamp((x - edge0) / (edge1 - edge0), 0, 1);\n  return t * t * (3 - 2 * t);\n};\n\n/** HSV, hue a fraction of a turn, into `out` as RGB. */\nexport const hsvToRgb = (\n  hue: number,\n  saturation: number,\n  value: number,\n  out: number[],\n) => {\n  for (let i = 0; i < 3; i++) {\n    const k = i === 0 ? 1 : i === 1 ? 2 / 3 : 1 / 3;\n    const p = Math.abs(fract(hue + k) * 6 - 3);\n    out[i] = value * mix(1, clamp(p - 1, 0, 1), saturation);\n  }\n};\n\n/** A palette color as is; dark mode lifts saturation a touch so it stays vivid on black. */\nexport const vivid = (color: Hsv, dark: number, out: number[]) =>\n  hsvToRgb(color.hue, clamp(color.saturation + dark * 0.06, 0, 1), color.value, out);\n\n/** Pushes `c` away from its luminance by `amount`; below 1 desaturates. */\nexport const saturate = (c: number[], amount: number) => {\n  const l = 0.299 * c[0] + 0.587 * c[1] + 0.114 * c[2];\n  for (let i = 0; i < 3; i++) c[i] = clamp(mix(l, c[i], amount), 0, 1);\n};\n\n/**\n * How much the agent is talking, 0 to 1: a slow phrase gate times a faster syllable wobble. The\n * shader's own envelope, on the same simplex noise, so every renderer and iOS speak alike.\n * `syllableDepth` is how far the syllables swing it, the shader's 0.45 unless a renderer wants it\n * calmer.\n */\nexport const speechEnv = (t: number, syllableDepth = 0.45) => {\n  const gate = smoothstep(-0.25, 0.35, snoise(t * 0.6, 21, 0));\n  const syllable = 1 - syllableDepth + syllableDepth * snoise(t * 3, 5, 0);\n  return clamp(gate * syllable, 0, 1);\n};\n\n/** `expressivity` from 0 to 2; one that is not a number gets the default, 1. */\nexport const expressivityInput = (expressivity: number | undefined) =>\n  typeof expressivity === 'number' && Number.isFinite(expressivity)\n    ? clamp(expressivity, 0, 2)\n    : 1;\n\n/** `level` from 0 to 1; one that is not a number counts as no live audio, so it can't stall the motion. */\nexport const audioLevelInput = (level: number | null | undefined) =>\n  typeof level === 'number' && Number.isFinite(level) ? clamp(level, 0, 1) : null;\n"],"mappings":";;;AAKA,IAAa,OAAO,GAAW,GAAW,MAAc,KAAK,IAAI,KAAK;AAEtE,IAAa,SAAS,MAAc,IAAI,KAAK,MAAM,CAAC;AAEpD,IAAa,SAAS,GAAW,KAAa,SAC5C,KAAK,IAAI,KAAK,IAAI,GAAG,GAAG,GAAG,IAAI;;AAGjC,IAAa,cAAc,OAAe,OAAe,MAAc;CACrE,MAAM,IAAI,OAAO,IAAI,UAAU,QAAQ,QAAQ,GAAG,CAAC;CACnD,OAAO,IAAI,KAAK,IAAI,IAAI;AAC1B;;AAGA,IAAa,YACX,KACA,YACA,OACA,QACG;CACH,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAGrB,IAAI,KAAK,QAAQ,IAAI,GAAG,MADd,KAAK,IAAI,MAAM,OADf,MAAM,IAAI,IAAI,MAAM,IAAI,IAAI,IAAI,IAAI,EACd,IAAI,IAAI,CACV,IAAI,GAAG,GAAG,CAAC,GAAG,UAAU;AAE1D;;AAGA,IAAa,SAAS,OAAY,MAAc,QAC9C,SAAS,MAAM,KAAK,MAAM,MAAM,aAAa,OAAO,KAAM,GAAG,CAAC,GAAG,MAAM,OAAO,GAAG;;AAGnF,IAAa,YAAY,GAAa,WAAmB;CACvD,MAAM,IAAI,OAAQ,EAAE,KAAK,OAAQ,EAAE,KAAK,OAAQ,EAAE;CAClD,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,EAAE,KAAK,MAAM,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