All files / web/src/arcade-games/know-your-world/music useMusicEngine.ts

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

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 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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
/**
 * Music Engine Hook for Know Your World
 *
 * Uses Strudel for generative ambient background music.
 * Handles initialization, playback control, and pattern management.
 *
 * @see https://strudel.cc/technical-manual/project-start/
 */

'use client'

import { useCallback, useEffect, useMemo, useRef, useState } from 'react'

// Declare global Strudel functions (made available after initStrudel)
declare global {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  function evaluate(code: string): Promise<any>
  function hush(): void
}

interface MusicEngineState {
  isInitialized: boolean
  isPlaying: boolean
  isMuted: boolean
  volume: number
  error: string | null
  /** Current pattern being played (for debugging) */
  currentPattern: string
}

interface MusicEngineControls {
  /** Initialize the audio engine (requires user interaction) */
  initialize: () => Promise<void>
  /** Start playing the current pattern */
  play: () => Promise<void>
  /** Stop all playback */
  stop: () => void
  /** Toggle mute state */
  toggleMute: () => void
  /** Set mute state */
  setMuted: (muted: boolean) => void
  /** Set volume (0-1) */
  setVolume: (volume: number) => void
  /** Evaluate a new pattern */
  evaluatePattern: (pattern: string) => Promise<void>
  /** Evaluate a pattern at full volume (bypasses volume slider) */
  evaluatePatternFullVolume: (pattern: string) => Promise<void>
  /** Play a one-shot sound effect (works even when music is off) */
  playOneShot: (pattern: string) => Promise<void>
  /** Stop one-shot sound without killing the scheduler */
  stopOneShot: () => Promise<void>
}

export type MusicEngine = MusicEngineState & MusicEngineControls

// Default ambient pattern - simple drone with sine waves
// Uses Strudel mini notation for a gentle, evolving soundscape
const DEFAULT_PATTERN = `
stack(
  note("c2").sound("sine").lpf(300).gain(0.08).slow(16),
  note("g2").sound("sine").lpf(400).gain(0.05).slow(24),
  note("c3 e3 g3").sound("sine").lpf(500).gain(0.03).slow(32)
).room(0.8)
`

export function useMusicEngine(): MusicEngine {
  const [state, setState] = useState<MusicEngineState>({
    isInitialized: false,
    isPlaying: false,
    isMuted: true, // Default to muted
    volume: 0.5, // 50% default volume
    error: null,
    currentPattern: '',
  })

  const currentPatternRef = useRef<string>(DEFAULT_PATTERN)
  const initializingRef = useRef(false)
  const volumeRef = useRef(0.5) // Track volume for immediate access
  // Store Strudel repl reference for direct control
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const strudelReplRef = useRef<any>(null)

  // Wrap pattern with volume control
  const wrapWithVolume = useCallback((pattern: string, vol: number): string => {
    // Wrap the entire pattern with a gain multiplier
    return `(${pattern}).gain(${vol.toFixed(2)})`
  }, [])

  // Initialize Strudel (must be called from user interaction)
  const initialize = useCallback(async () => {
    if (state.isInitialized || initializingRef.current) return

    initializingRef.current = true
    setState((s) => ({ ...s, error: null }))

    try {
      // Dynamic import to avoid SSR issues
      // @ts-expect-error - @strudel/web doesn't have type declarations
      const { initStrudel, samples } = await import('@strudel/web')

      // Initialize Strudel with default samples loaded
      // This makes global functions available (evaluate, hush, etc.)
      // and loads drum samples (bd, hh, cp, sd, etc.) from dirt-samples
      const repl = await initStrudel({
        prebake: () => samples('github:tidalcycles/dirt-samples'),
      })

      // Store repl reference for direct control
      strudelReplRef.current = repl
      console.log('[MusicEngine] Strudel repl:', repl)
      console.log('[MusicEngine] repl methods:', repl ? Object.keys(repl) : 'null')

      setState((s) => ({
        ...s,
        isInitialized: true,
        error: null,
      }))

      console.log('[MusicEngine] Strudel initialized successfully')
    } catch (err) {
      const message = err instanceof Error ? err.message : 'Failed to initialize music engine'
      console.error('[MusicEngine] Initialization failed:', err)
      setState((s) => ({ ...s, error: message }))
    } finally {
      initializingRef.current = false
    }
  }, [state.isInitialized])

  // Play the current pattern
  const play = useCallback(async () => {
    console.log('[MusicEngine] play() called, state:', {
      isInitialized: state.isInitialized,
      isMuted: state.isMuted,
      isPlaying: state.isPlaying,
    })

    if (!state.isInitialized) {
      console.warn('[MusicEngine] Cannot play: not initialized')
      return
    }

    if (state.isMuted) {
      console.warn('[MusicEngine] Cannot play: muted')
      return
    }

    if (state.isPlaying) {
      console.warn('[MusicEngine] Already playing, skipping')
      return
    }

    try {
      // Wrap pattern with current volume and evaluate
      const patternWithVolume = wrapWithVolume(currentPatternRef.current, volumeRef.current)
      console.log('[MusicEngine] Calling evaluate()...')
      await evaluate(patternWithVolume)
      setState((s) => ({
        ...s,
        isPlaying: true,
        error: null,
        currentPattern: currentPatternRef.current,
      }))
      console.log('[MusicEngine] Playing')
    } catch (err) {
      const message = err instanceof Error ? err.message : 'Failed to play pattern'
      console.error('[MusicEngine] Play failed:', err)
      setState((s) => ({ ...s, error: message }))
    }
  }, [state.isInitialized, state.isMuted, state.isPlaying, wrapWithVolume])

  // Stop playback - always try to hush regardless of state
  const stop = useCallback(async () => {
    console.log('[MusicEngine] stop() called')
    try {
      // Try multiple ways to stop Strudel

      // 1. Try the stored repl reference first
      if (strudelReplRef.current) {
        console.log('[MusicEngine] repl ref exists, methods:', Object.keys(strudelReplRef.current))
        if (typeof strudelReplRef.current.stop === 'function') {
          console.log('[MusicEngine] Calling repl.stop()...')
          strudelReplRef.current.stop()
        }
        if (typeof strudelReplRef.current.hush === 'function') {
          console.log('[MusicEngine] Calling repl.hush()...')
          strudelReplRef.current.hush()
        }
      }

      // 2. Try the global hush function
      if (typeof hush === 'function') {
        console.log('[MusicEngine] Calling global hush()...')
        hush()
      }

      // 3. Try window.hush as fallback
      if (typeof (window as any).hush === 'function') {
        console.log('[MusicEngine] Calling window.hush()...')
        ;(window as any).hush()
      }

      // 4. Try evaluating silence pattern as nuclear option
      try {
        console.log('[MusicEngine] Evaluating silence pattern...')
        await evaluate('silence')
      } catch {
        // Ignore errors from silence pattern
      }

      setState((s) => {
        console.log('[MusicEngine] Setting isPlaying=false, was:', s.isPlaying)
        return { ...s, isPlaying: false }
      })
    } catch (err) {
      console.error('[MusicEngine] Stop failed:', err)
    }
  }, [])

  // Toggle mute
  const toggleMute = useCallback(() => {
    setState((s) => {
      const newMuted = !s.isMuted
      if (newMuted) {
        // Always try to stop playback when muting
        try {
          if (typeof hush === 'function') {
            hush()
          }
        } catch {
          // Ignore errors
        }
        return { ...s, isMuted: true, isPlaying: false }
      }
      return { ...s, isMuted: false }
    })
  }, [])

  // Set mute state - always stop when muting regardless of current state
  const setMuted = useCallback((muted: boolean) => {
    console.log('[MusicEngine] setMuted called with:', muted)
    if (muted) {
      // Always try to stop when muting - don't rely on state.isPlaying
      // which might be stale in the closure
      try {
        if (typeof hush === 'function') {
          console.log('[MusicEngine] setMuted: calling hush()...')
          hush()
        }
      } catch (err) {
        console.error('[MusicEngine] setMuted: hush failed:', err)
      }
      setState((s) => ({ ...s, isMuted: true, isPlaying: false }))
    } else {
      setState((s) => ({ ...s, isMuted: false }))
    }
  }, [])

  // Set volume - re-evaluates pattern with new gain level
  const setVolume = useCallback(
    async (volume: number) => {
      const clampedVolume = Math.max(0, Math.min(1, volume))
      volumeRef.current = clampedVolume
      setState((s) => ({ ...s, volume: clampedVolume }))

      // If playing, re-evaluate pattern with new volume
      if (state.isPlaying && state.isInitialized && !state.isMuted) {
        try {
          const patternWithVolume = wrapWithVolume(currentPatternRef.current, clampedVolume)
          await evaluate(patternWithVolume)
        } catch (err) {
          console.error('[MusicEngine] Failed to update volume:', err)
        }
      }
    },
    [state.isPlaying, state.isInitialized, state.isMuted, wrapWithVolume]
  )

  // Evaluate a new pattern
  const evaluatePattern = useCallback(
    async (pattern: string) => {
      console.log('[MusicEngine] evaluatePattern() called, isPlaying:', state.isPlaying)
      currentPatternRef.current = pattern
      // Always update the displayed pattern
      setState((s) => ({ ...s, currentPattern: pattern }))

      if (state.isPlaying && state.isInitialized && !state.isMuted) {
        try {
          const patternWithVolume = wrapWithVolume(pattern, volumeRef.current)
          console.log('[MusicEngine] evaluatePattern: calling evaluate()...')
          await evaluate(patternWithVolume)
        } catch (err) {
          console.error('[MusicEngine] Pattern evaluation failed:', err)
        }
      } else {
        console.log('[MusicEngine] evaluatePattern: not playing, skipped evaluate()')
      }
    },
    [state.isPlaying, state.isInitialized, state.isMuted, wrapWithVolume]
  )

  // Evaluate a pattern at full volume (bypasses volume slider)
  // Used for celebrations where we need precise volume control
  const evaluatePatternFullVolume = useCallback(
    async (pattern: string) => {
      console.log('[MusicEngine] evaluatePatternFullVolume() called, isPlaying:', state.isPlaying)
      // Update the displayed pattern
      setState((s) => ({ ...s, currentPattern: pattern }))

      if (state.isPlaying && state.isInitialized && !state.isMuted) {
        try {
          console.log(
            '[MusicEngine] evaluatePatternFullVolume: calling evaluate() without volume wrapper...'
          )
          await evaluate(pattern)
        } catch (err) {
          console.error('[MusicEngine] Pattern evaluation failed:', err)
        }
      } else {
        console.log('[MusicEngine] evaluatePatternFullVolume: not playing, skipped evaluate()')
      }
    },
    [state.isPlaying, state.isInitialized, state.isMuted]
  )

  // Play a one-shot sound effect (works even when music is off)
  // Ignores isPlaying and isMuted state - just plays the pattern if initialized
  const playOneShot = useCallback(
    async (pattern: string) => {
      console.log('[MusicEngine] playOneShot() called, isInitialized:', state.isInitialized)

      if (!state.isInitialized) {
        console.warn('[MusicEngine] playOneShot: not initialized, cannot play')
        return
      }

      try {
        console.log('[MusicEngine] playOneShot: evaluating pattern...')
        await evaluate(pattern)
      } catch (err) {
        console.error('[MusicEngine] playOneShot failed:', err)
      }
    },
    [state.isInitialized]
  )

  // Stop one-shot sound without killing the scheduler
  // Evaluates a silent rest pattern to replace the current sound
  const stopOneShot = useCallback(async () => {
    console.log('[MusicEngine] stopOneShot() called')
    try {
      // Evaluate a rest pattern - this replaces current sound with silence
      // but keeps the scheduler running for future sounds
      await evaluate('note("~")')
    } catch (err) {
      console.error('[MusicEngine] stopOneShot failed:', err)
    }
  }, [])

  // Cleanup on unmount - use refs to avoid stale closures
  useEffect(() => {
    return () => {
      console.log('[MusicEngine] Cleanup on unmount')
      try {
        // Stop the Strudel scheduler via repl reference
        if (strudelReplRef.current) {
          console.log('[MusicEngine] Stopping Strudel repl...')
          if (typeof strudelReplRef.current.stop === 'function') {
            strudelReplRef.current.stop()
          }
        }
        // Also try global hush as backup
        if (typeof hush === 'function') {
          hush()
        }
      } catch (err) {
        console.error('[MusicEngine] Cleanup error:', err)
      }
      // Clear the repl reference
      strudelReplRef.current = null
    }
  }, []) // Empty deps - only run on unmount

  // Destructure state for stable memoization (object reference changes on every update)
  const { isInitialized, isPlaying, isMuted, volume, error, currentPattern } = state

  return useMemo(
    () => ({
      isInitialized,
      isPlaying,
      isMuted,
      volume,
      error,
      currentPattern,
      initialize,
      play,
      stop,
      toggleMute,
      setMuted,
      setVolume,
      evaluatePattern,
      evaluatePatternFullVolume,
      playOneShot,
      stopOneShot,
    }),
    [
      isInitialized,
      isPlaying,
      isMuted,
      volume,
      error,
      currentPattern,
      initialize,
      play,
      stop,
      toggleMute,
      setMuted,
      setVolume,
      evaluatePattern,
      evaluatePatternFullVolume,
      playOneShot,
      stopOneShot,
    ]
  )
}