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

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
/**
 * Dynamic Music Effects
 *
 * Provides real-time musical reactivity:
 * - Temperature modulation based on hot/cold feedback
 * - Celebration flourishes when finding regions
 *
 * Uses Strudel's pattern modifiers to adjust running patterns.
 */

import type { FeedbackType } from '../../utils/hotColdPhrases'
import type { CelebrationType } from '../../Provider'

/**
 * Temperature effect settings.
 * Affects filter cutoff, speed, and brightness.
 */
export interface TemperatureEffect {
  /** Low-pass filter cutoff (higher = brighter) */
  lpfMultiplier: number
  /** Speed modifier (faster when hotter) */
  speedMultiplier: number
  /** Gain adjustment */
  gainMultiplier: number
  /** Room reverb adjustment */
  roomMultiplier: number
}

/**
 * Map hot/cold feedback types to musical temperature effects.
 * Hot = brighter, faster, more present
 * Cold = darker, slower, more distant
 */
export function getTemperatureEffect(feedbackType: FeedbackType | null): TemperatureEffect {
  const neutral: TemperatureEffect = {
    lpfMultiplier: 1.0,
    speedMultiplier: 1.0,
    gainMultiplier: 1.0,
    roomMultiplier: 1.0,
  }

  if (!feedbackType) return neutral

  switch (feedbackType) {
    // Very hot - brightest, most energetic
    case 'on_fire':
      return {
        lpfMultiplier: 2.0,
        speedMultiplier: 1.3,
        gainMultiplier: 1.2,
        roomMultiplier: 0.6,
      }

    // Hot - bright and lively
    case 'hot':
      return {
        lpfMultiplier: 1.6,
        speedMultiplier: 1.15,
        gainMultiplier: 1.1,
        roomMultiplier: 0.75,
      }

    // Getting warmer - subtle brightness
    case 'warmer':
      return {
        lpfMultiplier: 1.3,
        speedMultiplier: 1.05,
        gainMultiplier: 1.05,
        roomMultiplier: 0.9,
      }

    // Found it - brief excitation
    case 'found_it':
      return {
        lpfMultiplier: 2.5,
        speedMultiplier: 1.4,
        gainMultiplier: 1.3,
        roomMultiplier: 0.5,
      }

    // Getting colder - subtle darkness
    case 'colder':
      return {
        lpfMultiplier: 0.8,
        speedMultiplier: 0.95,
        gainMultiplier: 0.95,
        roomMultiplier: 1.1,
      }

    // Cold - darker, slower
    case 'cold':
      return {
        lpfMultiplier: 0.6,
        speedMultiplier: 0.9,
        gainMultiplier: 0.85,
        roomMultiplier: 1.3,
      }

    // Freezing - darkest, most distant
    case 'freezing':
      return {
        lpfMultiplier: 0.4,
        speedMultiplier: 0.8,
        gainMultiplier: 0.7,
        roomMultiplier: 1.5,
      }

    // Overshot - slight confusion
    case 'overshot':
      return {
        lpfMultiplier: 0.7,
        speedMultiplier: 0.85,
        gainMultiplier: 0.9,
        roomMultiplier: 1.2,
      }

    // Stuck - murky, uncertain
    case 'stuck':
      return {
        lpfMultiplier: 0.5,
        speedMultiplier: 0.85,
        gainMultiplier: 0.8,
        roomMultiplier: 1.4,
      }

    default:
      return neutral
  }
}

/**
 * Apply temperature effect modifiers to a pattern string.
 * Wraps the pattern with Strudel effects based on temperature.
 */
export function applyTemperatureToPattern(pattern: string, effect: TemperatureEffect): string {
  // Skip if neutral
  if (
    effect.lpfMultiplier === 1.0 &&
    effect.speedMultiplier === 1.0 &&
    effect.gainMultiplier === 1.0 &&
    effect.roomMultiplier === 1.0
  ) {
    return pattern
  }

  // Wrap the pattern with temperature modifiers
  // Note: We apply these as outer wrappers to affect the whole pattern
  let modifiedPattern = pattern

  // Apply speed if not 1.0
  if (effect.speedMultiplier !== 1.0) {
    if (effect.speedMultiplier > 1.0) {
      modifiedPattern = `(${modifiedPattern}).fast(${effect.speedMultiplier.toFixed(2)})`
    } else {
      modifiedPattern = `(${modifiedPattern}).slow(${(1 / effect.speedMultiplier).toFixed(2)})`
    }
  }

  return modifiedPattern
}

/**
 * Celebration flourish patterns.
 * Short, one-shot musical phrases to mark finding a region.
 * Duration controls how long the flourish plays before restoring the base pattern.
 */
export interface CelebrationFlourish {
  /** Strudel pattern for the flourish */
  pattern: string
  /** Duration in milliseconds before restoring base pattern */
  duration: number
}

/**
 * Get celebration flourish based on celebration type and continent.
 * Each continent can have its own flourish style.
 * The flourish uses .first(n) so it plays once and naturally stops.
 */
export function getCelebrationFlourish(
  type: CelebrationType,
  continentId: string
): CelebrationFlourish {
  // Base flourish patterns by celebration type
  // Designed to match the original Web Audio celebration sounds:
  // - Triangle waves for brightness with smooth tone
  // - Musical intervals and chords
  // - Quick, pleasant chime-like sounds
  // These patterns bypass the volume slider via evaluatePatternFullVolume
  const flourishes: Record<CelebrationType, CelebrationFlourish> = {
    // Lightning - quick sparkle: ascending pitch sweep (glissando)
    lightning: {
      pattern: `note("c6 e7 c7")
        .sound("triangle")
        .glide(0.08)
        .decay(0.15)
        .sustain(0)
        .gain(8)
        .slow(0.4)`,
      duration: 250,
    },

    // Standard - two-note chime: C6 then E6 (higher octave for brightness)
    standard: {
      pattern: `note("[c6 e6]")
        .sound("triangle")
        .decay(0.3)
        .sustain(0.2)
        .release(0.2)
        .gain(8)
        .room(0.2)
        .slow(0.8)`,
      duration: 500,
    },

    // Hard-earned - triumphant arpeggio then bright chord
    'hard-earned': {
      pattern: `stack(
        note("c5 e5 g5 ~").sound("triangle").decay(0.2).sustain(0.1).gain(7),
        note("~ ~ ~ [c6,e6,g6]").sound("triangle").decay(0.4).sustain(0.3).release(0.3).gain(6)
      ).room(0.3).slow(1.5)`,
      duration: 800,
    },
  }

  // Get base flourish
  const flourish = flourishes[type]

  // For now, use the same pattern for all continents (simplified for debugging)
  // Continental flavor can be added back once basic celebrations work
  return flourish
}

/**
 * Build a pattern that includes a celebration flourish.
 * The flourish is layered on top of the base pattern using stack().
 * The base pattern is heavily ducked so the flourish stands out.
 * This pattern bypasses the volume slider (evaluatePatternFullVolume).
 * A JavaScript timeout should be used to restore the base pattern after the flourish.
 */
export function buildPatternWithFlourish(basePattern: string, flourishPattern: string): string {
  // Stack the base pattern (ducked to 15% volume) with the flourish at high gain
  // Since this bypasses the volume slider, celebration at gain(5) is ~33x louder than base
  return `stack(
    (${basePattern}).gain(0.15),
    ${flourishPattern}
  )`
}