All files / web/src/components/toys/number-line/constants/demos useConstantDemoNarration.ts

0% Statements 0/314
0% Branches 0/1
0% Functions 0/1
0% Lines 0/314

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import { useRef, useCallback, useEffect } from 'react'
import type { DemoState } from './useConstantDemo'
import { useAudioManagerInstance } from '@/contexts/AudioManagerContext'
import { SUBTITLE_TOP_OFFSET, SUBTITLE_BOTTOM_OFFSET } from '../../viewportAnimation'
import { useNarrationSequencer } from '../../useNarrationSequencer'

// ── Shared types ────────────────────────────────────────────────────

export interface DemoNarrationSegment {
  /** Text sent to TTS for this segment */
  ttsText: string
  /** TTS tone override — falls back to the config's tone */
  ttsTone?: string
  /** revealProgress value at which this segment starts */
  startProgress: number
  /** revealProgress value at which this segment ends */
  endProgress: number
  /** Minimum wall-clock time (ms) to sweep from start to end */
  animationDurationMs: number
  /** Short label (max 4 words) shown on the scrubber during drag */
  scrubberLabel?: string
}

export interface DemoNarrationConfig {
  segments: DemoNarrationSegment[]
  tone: string
}

// ── Hook ────────────────────────────────────────────────────────────

/**
 * Generic TTS narration orchestrator for constant demos.
 *
 * Takes over `revealProgress` control from `useConstantDemo`'s 15s auto-play
 * by calling `setRevealProgress()` every frame. Each narration segment sweeps
 * progress from its startProgress to endProgress over animationDurationMs,
 * pausing at segment boundaries until both animation AND TTS complete.
 *
 * Supports multiple constants — pass a config map keyed by constantId.
 * Only constants present in the map get narration; others play silently
 * with the default 15s auto-play.
 *
 * When audio is disabled, `speak()` resolves immediately — gating depends
 * solely on `animationDurationMs`, producing a paced demo.
 *
 * @param configs  Static map: constantId → { segments, tone }. Must be
 *                 stable across renders (define at module level).
 */
export function useConstantDemoNarration(
  demoStateRef: React.MutableRefObject<DemoState>,
  setRevealProgress: (value: number) => void,
  configs: Record<string, DemoNarrationConfig>
) {
  const audioManager = useAudioManagerInstance()

  // Shared narration sequencer (handles TTS + segment gating)
  const {
    start: seqStart,
    startFrom: seqStartFrom,
    tick: seqTick,
    stop: seqStop,
    mutedRef: seqMutedRef,
    releaseTtsGate: seqReleaseTtsGate,
    segmentIndexRef: seqSegmentIndexRef,
  } = useNarrationSequencer()

  // Playback speed multiplier (1 = normal, 0.5 = half, 2 = double)
  const playbackSpeedRef = useRef(1)

  // Narration state refs
  const isNarratingRef = useRef(false)
  const activeConstantRef = useRef<string | null>(null)
  const activeConfigRef = useRef<DemoNarrationConfig | null>(null)
  const rafRef = useRef<number>(0)

  // Tracks which constantId we've already auto-started for, so we
  // don't re-trigger after the user scrubs or the demo restarts.
  const triggeredForRef = useRef<string | null>(null)

  /** Find the segment whose [startProgress, endProgress) range contains the given progress. */
  const findSegmentForProgress = (segments: DemoNarrationSegment[], progress: number): number => {
    // At or past the end — no segment to resume from
    if (progress >= 1) return -1
    for (let i = segments.length - 1; i >= 0; i--) {
      if (progress >= segments[i].startProgress) return i
    }
    return 0
  }

  /** Stop narration: cancel RAF, stop TTS, reset subtitle positioning. */
  const stopNarration = useCallback(() => {
    if (!isNarratingRef.current) return
    isNarratingRef.current = false
    activeConstantRef.current = null
    activeConfigRef.current = null
    if (rafRef.current) {
      cancelAnimationFrame(rafRef.current)
      rafRef.current = 0
    }
    seqStop()
    audioManager.configure({
      subtitleAnchor: 'bottom',
      subtitleBottomOffset: SUBTITLE_BOTTOM_OFFSET,
    })
  }, [seqStop, audioManager])

  /** Start the RAF tick loop that drives progress via the sequencer. */
  const startTickLoop = useCallback(() => {
    const tick = () => {
      if (!isNarratingRef.current) return

      // Check if the demo is still active for this constant
      const ds = demoStateRef.current
      if (
        ds.phase === 'idle' ||
        ds.phase === 'fading' ||
        ds.constantId !== activeConstantRef.current
      ) {
        stopNarration()
        return
      }

      const cfg = activeConfigRef.current
      if (!cfg) {
        stopNarration()
        return
      }

      // Drive progress via the shared sequencer
      const result = seqTick(playbackSpeedRef.current)
      if (!result) {
        // Sequencer finished or inactive — hold at final progress
        setRevealProgress(1)
        stopNarration()
        return
      }

      const seg = cfg.segments[result.segmentIndex]
      if (!seg) {
        setRevealProgress(1)
        stopNarration()
        return
      }

      // Sweep revealProgress linearly from seg.startProgress to seg.endProgress
      const targetProgress =
        seg.startProgress + (seg.endProgress - seg.startProgress) * result.animFrac
      setRevealProgress(targetProgress)

      if (result.allDone) {
        setRevealProgress(1)
        stopNarration()
        return
      }

      rafRef.current = requestAnimationFrame(tick)
    }

    rafRef.current = requestAnimationFrame(tick)
  }, [demoStateRef, setRevealProgress, seqTick, stopNarration])

  /** Start narration for the given constant. */
  const startNarration = useCallback(
    (constantId: string) => {
      const config = configs[constantId]
      if (!config || config.segments.length === 0) return
      if (isNarratingRef.current) stopNarration()

      isNarratingRef.current = true
      activeConstantRef.current = constantId
      activeConfigRef.current = config

      // Position subtitles at top of viewport
      audioManager.configure({
        subtitleAnchor: 'top',
        subtitleBottomOffset: SUBTITLE_TOP_OFFSET,
      })

      // Start the sequencer (speaks segment 0 immediately)
      seqStart(config.segments, config.tone)

      // Start RAF tick loop
      startTickLoop()
    },
    [configs, audioManager, seqStart, stopNarration, startTickLoop]
  )

  /**
   * Resume narration from the current revealProgress position.
   * Computes how far through the current segment the user scrubbed and
   * pre-advances the sequencer timer by that amount so animation picks
   * up from the exact scrubbed point. TTS audio also seeks to match,
   * so narration resumes mid-sentence.
   */
  const resumeNarration = useCallback(
    (constantId: string) => {
      const config = configs[constantId]
      if (!config || config.segments.length === 0) return

      const ds = demoStateRef.current
      if (ds.phase === 'idle' || ds.phase === 'fading') return
      if (ds.revealProgress >= 1) return // already at the end — no-op

      const segIdx = findSegmentForProgress(config.segments, ds.revealProgress)
      if (segIdx < 0) return

      // Compute how far through this segment the scrubbed position is
      const seg = config.segments[segIdx]
      const segSpan = seg.endProgress - seg.startProgress
      const frac =
        segSpan > 0
          ? Math.max(0, Math.min(1, (ds.revealProgress - seg.startProgress) / segSpan))
          : 0

      if (isNarratingRef.current) stopNarration()

      isNarratingRef.current = true
      activeConstantRef.current = constantId
      activeConfigRef.current = config

      // Position subtitles at top of viewport
      audioManager.configure({
        subtitleAnchor: 'top',
        subtitleBottomOffset: SUBTITLE_TOP_OFFSET,
      })

      // Queue a seek so the TTS clip starts mid-sentence to match the
      // scrubbed position. playMp3() will apply it when the audio loads.
      const seekMs = frac * seg.animationDurationMs
      audioManager.seekNextAudio(seekMs)

      // Start the sequencer from the found segment with the initial fraction.
      // The sequencer maps elapsed time from frac→1 so the animation picks
      // up exactly where the user scrubbed, regardless of adaptive timing.
      seqStartFrom(config.segments, config.tone, segIdx, frac)

      // Start RAF tick loop
      startTickLoop()
    },
    [configs, demoStateRef, audioManager, seqStartFrom, stopNarration, startTickLoop]
  )

  /**
   * Idempotent auto-start: call from draw() after tickDemo().
   * Starts narration if the given constantId has a config AND narration
   * hasn't already been triggered for this demo session.
   */
  const startIfNeeded = useCallback(
    (constantId: string | null) => {
      if (!constantId) return
      if (isNarratingRef.current) return
      if (triggeredForRef.current === constantId) return
      if (!configs[constantId]) return

      console.log(`[DemoNarration] startIfNeeded → starting narration for "${constantId}"`)
      triggeredForRef.current = constantId
      startNarration(constantId)
    },
    [configs, startNarration]
  )

  /**
   * Mark a constantId as already triggered without starting narration.
   * Used when restoring a demo from URL params — suppresses the auto-start
   * in startIfNeeded() so the restored demo stays paused.
   */
  const markTriggered = useCallback((constantId: string) => {
    triggeredForRef.current = constantId
  }, [])

  /**
   * Reset the auto-start trigger — call when a new demo session begins
   * (e.g. handleExploreConstant) so narration can re-trigger.
   */
  const reset = useCallback(() => {
    stopNarration()
    triggeredForRef.current = null
    // Reset playback speed to 1x so it doesn't carry over between demos
    playbackSpeedRef.current = 1
    audioManager.configure({ playbackRate: 1 })
  }, [stopNarration, audioManager])

  // Cleanup on unmount
  useEffect(() => {
    return () => {
      if (isNarratingRef.current) {
        isNarratingRef.current = false
        if (rafRef.current) {
          cancelAnimationFrame(rafRef.current)
          rafRef.current = 0
        }
        seqStop()
        audioManager.configure({
          subtitleAnchor: 'bottom',
          subtitleBottomOffset: SUBTITLE_BOTTOM_OFFSET,
          playbackRate: 1,
        })
      }
    }
  }, [seqStop, audioManager])

  return {
    startIfNeeded,
    stop: stopNarration,
    resume: resumeNarration,
    isNarrating: isNarratingRef,
    markTriggered,
    reset,
    mutedRef: seqMutedRef,
    releaseTtsGate: seqReleaseTtsGate,
    segmentIndexRef: seqSegmentIndexRef,
    playbackSpeedRef,
  }
}