{"version":3,"file":"driver.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/VoiceVisualization/driver.ts"],"sourcesContent":["import { VoiceVisualizationBounce } from './bounce';\nimport { MAXIMUM_COLORS } from './palette';\nimport type { Hsv, VoiceAgentState } from './types';\n\nexport type VoiceDriverInput = {\n  /** One to five colors. */\n  colors: Hsv[];\n  /** From 0 to 2. */\n  expressivity: number;\n  reduceMotion: boolean;\n  state: VoiceAgentState;\n  /** From 0 to 1, or null when no live audio drives the motion. */\n  audioLevel?: number | null;\n};\n\n/**\n * How a state moves. `load`, `flow` and `react` each select a motion pattern, one per state, so\n * easing between states crossfades the patterns.\n */\ntype VoiceMotion = {\n  bright: number;\n  flow: number;\n  load: number;\n  react: number;\n  sat: number;\n  speed: number;\n};\n\nconst motion = ({\n  flow = 0,\n  load = 0,\n  react = 0,\n  speed,\n}: Partial<VoiceMotion> & { speed: number }): VoiceMotion => ({\n  bright: 1,\n  flow,\n  load,\n  react,\n  sat: 1,\n  speed,\n});\n\nconst MOTIONS: Record<VoiceAgentState, VoiceMotion> = {\n  connecting: motion({ load: 1, speed: 1 }),\n  idle: motion({ react: 0.12, speed: 0.4 }),\n  listening: motion({ react: 0.45, speed: 0.9 }),\n  speaking: motion({ react: 1, speed: 1.25 }),\n  thinking: motion({ flow: 1, speed: 1.25 }),\n};\n\nexport const NO_TAP = 100;\nconst MAX_FRAME = 0.05;\n/** How fast time runs under reduced motion. */\nexport const REDUCED = 0.07;\nconst DRIVERS = 0.08;\nconst GENTLE = 0.12;\nconst VOICE = 0.1;\n/** Per-frame easing at 60 Hz of a live audio level. */\nexport const MIC = 0.35;\n\n/** Expressivity scales amplitude and speed, each with a floor so the motion breathes at 0. */\nexport const scales = (expressivity: number) => ({\n  amplitude: 0.15 + 0.85 * expressivity,\n  speed: 0.6 + 0.4 * expressivity,\n});\n\n/**\n * The share of the way a value moves in `dt` seconds when it moves `perFrame` of the way each\n * frame at 60 Hz.\n */\nexport const ease = (perFrame: number, dt: number) => 1 - Math.pow(1 - perFrame, dt * 60);\n\nconst rem = (x: number, m: number) => ((x % m) + m) % m;\n\n/** Eases an HSV color in place, its hue taking the shorter way round the wheel. */\nconst easeHsv = (color: Hsv, target: Hsv, fraction: number) => {\n  const hueStep = (rem(target.hue - color.hue + 1.5, 1) - 0.5) * fraction;\n  color.hue = rem(color.hue + hueStep + 1, 1);\n  color.saturation += (target.saturation - color.saturation) * fraction;\n  color.value += (target.value - color.value) * fraction;\n};\n\n/** The five color slots the renderers read; past the palette they repeat its first color. */\nexport const slots = (colors: Hsv[]): Hsv[] => {\n  const first = colors[0] ?? { hue: 0, saturation: 0, value: 0 };\n  return Array.from({ length: MAXIMUM_COLORS }, (_, i) => colors[i] ?? first);\n};\n\n/**\n * The motion of a voice visualization: where it is in time, how it moves, and its colors.\n *\n * Each value eases toward the state's motion by a fixed share per frame at 60 Hz, converted to\n * time, so it moves the same at any refresh rate. The shader and the Canvas drawing read the\n * same values, so the renderers move alike.\n */\nexport class VoiceVisualizationDriver {\n  speed: number;\n  bright: number;\n  sat: number;\n  load: number;\n  flow: number;\n  expressivity: number;\n  time = 0;\n  flowSpin = 0;\n  orbSpin = 0;\n  count: number;\n  tapX = 0;\n  tapY = 0;\n  tapAge = NO_TAP;\n  hoverX = 0;\n  hoverY = 0;\n  hoverAmount = 0;\n  mic = 0;\n  voice = 0;\n\n  private baseReact: number;\n  /** The colors drawn, eased in place toward `targets`, the palette's slots. */\n  private colors: Hsv[];\n  private targets: Hsv[];\n  private isHovering = false;\n  private bounce = new VoiceVisualizationBounce();\n  private currentInput: VoiceDriverInput;\n\n  // Starts settled on the input, so the first frame shows it rather than easing into it.\n  constructor(input: VoiceDriverInput) {\n    this.currentInput = input;\n    const m = MOTIONS[input.state];\n    const s = scales(input.expressivity);\n    this.speed = m.speed * s.speed;\n    this.bright = m.bright;\n    this.sat = m.sat;\n    this.load = m.load * s.amplitude;\n    this.flow = m.flow * s.amplitude;\n    this.baseReact = m.react * s.amplitude;\n    this.expressivity = input.expressivity;\n    this.targets = slots(input.colors);\n    // Copies, as they are eased in place and the palette belongs to the caller.\n    this.colors = this.targets.map((color) => ({ ...color }));\n    this.count = input.colors.length;\n  }\n\n  get input() {\n    return this.currentInput;\n  }\n\n  set input(value: VoiceDriverInput) {\n    // The view hands in a fresh input every frame for the audio level, with the same palette.\n    if (value.colors !== this.currentInput.colors) this.targets = slots(value.colors);\n    this.currentInput = value;\n    if (value.reduceMotion) this.isHovering = false;\n  }\n\n  /** Live audio lifts the motion to its level, whatever the state. */\n  get react() {\n    return (\n      this.baseReact + (Math.max(this.baseReact, this.mic) - this.baseReact) * this.voice\n    );\n  }\n\n  /** The scale of a tap's bounce. */\n  get scale() {\n    return this.bounce.scale;\n  }\n\n  /** Color slot `index`, 0 to 4; past the palette the slots repeat its first color. */\n  color(index: number) {\n    return this.colors[index];\n  }\n\n  tap(x: number, y: number) {\n    if (this.currentInput.reduceMotion) return;\n    this.tapX = x;\n    this.tapY = y;\n    this.tapAge = 0;\n    this.bounce.start();\n  }\n\n  /** Where a pointer hovers, or null once it has left. */\n  hover(x: number | null, y: number | null) {\n    if (x === null || y === null || this.currentInput.reduceMotion) {\n      this.isHovering = false;\n      return;\n    }\n    this.hoverX = x;\n    this.hoverY = y;\n    this.isHovering = true;\n  }\n\n  advance(seconds: number) {\n    // Clamped so a frame after a stall cannot leap the motion.\n    const dt = Math.min(Math.max(seconds, 0), MAX_FRAME);\n    const input = this.currentInput;\n    const m = MOTIONS[input.state];\n    const s = scales(input.expressivity);\n    const drivers = ease(DRIVERS, dt);\n    this.speed += (m.speed * s.speed - this.speed) * drivers;\n    this.bright += (m.bright - this.bright) * drivers;\n    this.sat += (m.sat - this.sat) * drivers;\n    this.load += (m.load * s.amplitude - this.load) * drivers;\n    this.flow += (m.flow * s.amplitude - this.flow) * drivers;\n    this.baseReact += (m.react * s.amplitude - this.baseReact) * drivers;\n    this.expressivity += (input.expressivity - this.expressivity) * drivers;\n    // Integrated rather than computed as time times a weight, so a change of state eases the\n    // turns instead of jumping their angles.\n    const step = dt * (input.reduceMotion ? REDUCED : 1) * this.speed;\n    this.time += step;\n    this.flowSpin += step * this.flow;\n    this.orbSpin += step * (1 + 0.5 * this.flow + 0.1 * this.baseReact);\n    this.tapAge += dt;\n    this.bounce.advance(dt);\n    const gentle = ease(GENTLE, dt);\n    this.hoverAmount += ((this.isHovering ? 1 : 0) - this.hoverAmount) * gentle;\n    const level = input.audioLevel ?? null;\n    this.voice += ((level === null ? 0 : 1) - this.voice) * ease(VOICE, dt);\n    this.mic += ((level ?? 0) - this.mic) * ease(MIC, dt);\n    for (let i = 0; i < this.colors.length; i++) {\n      easeHsv(this.colors[i], this.targets[i], gentle);\n    }\n    this.count += (input.colors.length - this.count) * gentle;\n  }\n}\n"],"mappings":";;AA4BA,IAAM,UAAU,EACd,OAAO,GACP,OAAO,GACP,QAAQ,GACR,aAC4D;CAC5D,QAAQ;CACR;CACA;CACA;CACA,KAAK;CACL;AACF;AAEA,IAAM,UAAgD;CACpD,YAAY,OAAO;EAAE,MAAM;EAAG,OAAO;CAAE,CAAC;CACxC,MAAM,OAAO;EAAE,OAAO;EAAM,OAAO;CAAI,CAAC;CACxC,WAAW,OAAO;EAAE,OAAO;EAAM,OAAO;CAAI,CAAC;CAC7C,UAAU,OAAO;EAAE,OAAO;EAAG,OAAO;CAAK,CAAC;CAC1C,UAAU,OAAO;EAAE,MAAM;EAAG,OAAO;CAAK,CAAC;AAC3C;AAGA,IAAM,YAAY;;AAElB,IAAa,UAAU;AACvB,IAAM,UAAU;AAChB,IAAM,SAAS;AACf,IAAM,QAAQ;;AAEd,IAAa,MAAM;;AAGnB,IAAa,UAAU,kBAA0B;CAC/C,WAAW,MAAO,MAAO;CACzB,OAAO,KAAM,KAAM;AACrB;;;;;AAMA,IAAa,QAAQ,UAAkB,OAAe,IAAI,KAAK,IAAI,IAAI,UAAU,KAAK,EAAE;AAExF,IAAM,OAAO,GAAW,OAAgB,IAAI,IAAK,KAAK;;AAGt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